(gnus-thread-sort-functions)
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
48 (autoload 'mm-uu-dissect "mm-uu")
49 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
50   "Deuglify broken Outlook (Express) articles and redisplay."
51   t)
52 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
53 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
54 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
55
56 (defcustom gnus-kill-summary-on-exit t
57   "*If non-nil, kill the summary buffer when you exit from it.
58 If nil, the summary will become a \"*Dead Summary*\" buffer, and
59 it will be killed sometime later."
60   :group 'gnus-summary-exit
61   :type 'boolean)
62
63 (defcustom gnus-fetch-old-headers nil
64   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
65 If an unread article in the group refers to an older, already read (or
66 just marked as read) article, the old article will not normally be
67 displayed in the Summary buffer.  If this variable is non-nil, Gnus
68 will attempt to grab the headers to the old articles, and thereby
69 build complete threads.  If it has the value `some', only enough
70 headers to connect otherwise loose threads will be displayed.  This
71 variable can also be a number.  In that case, no more than that number
72 of old headers will be fetched.  If it has the value `invisible', all
73 old headers will be fetched, but none will be displayed.
74
75 The server has to support NOV for any of this to work."
76   :group 'gnus-thread
77   :type '(choice (const :tag "off" nil)
78                  (const some)
79                  number
80                  (sexp :menu-tag "other" t)))
81
82 (defcustom gnus-refer-thread-limit 200
83   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
84 If t, fetch all the available old headers."
85   :group 'gnus-thread
86   :type '(choice number
87                  (sexp :menu-tag "other" t)))
88
89 (defcustom gnus-summary-make-false-root 'adopt
90   "*nil means that Gnus won't gather loose threads.
91 If the root of a thread has expired or been read in a previous
92 session, the information necessary to build a complete thread has been
93 lost.  Instead of having many small sub-threads from this original thread
94 scattered all over the summary buffer, Gnus can gather them.
95
96 If non-nil, Gnus will try to gather all loose sub-threads from an
97 original thread into one large thread.
98
99 If this variable is non-nil, it should be one of `none', `adopt',
100 `dummy' or `empty'.
101
102 If this variable is `none', Gnus will not make a false root, but just
103 present the sub-threads after another.
104 If this variable is `dummy', Gnus will create a dummy root that will
105 have all the sub-threads as children.
106 If this variable is `adopt', Gnus will make one of the \"children\"
107 the parent and mark all the step-children as such.
108 If this variable is `empty', the \"children\" are printed with empty
109 subject fields.  (Or rather, they will be printed with a string
110 given by the `gnus-summary-same-subject' variable.)"
111   :group 'gnus-thread
112   :type '(choice (const :tag "off" nil)
113                  (const none)
114                  (const dummy)
115                  (const adopt)
116                  (const empty)))
117
118 (defcustom gnus-summary-make-false-root-always nil
119   "Always make a false dummy root."
120   :group 'gnus-thread
121   :type 'boolean)
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include:
151 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
152 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 \(they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-default-high-score 0
217   "*Default threshold for a high scored article.
218 An article will be highlighted as high scored if its score is greater
219 than this score."
220   :group 'gnus-score-default
221   :type 'integer)
222
223 (defcustom gnus-summary-default-low-score 0
224   "*Default threshold for a low scored article.
225 An article will be highlighted as low scored if its score is smaller
226 than this score."
227   :group 'gnus-score-default
228   :type 'integer)
229
230 (defcustom gnus-summary-zcore-fuzz 0
231   "*Fuzziness factor for the zcore in the summary buffer.
232 Articles with scores closer than this to `gnus-summary-default-score'
233 will not be marked."
234   :group 'gnus-summary-format
235   :type 'integer)
236
237 (defcustom gnus-simplify-subject-fuzzy-regexp nil
238   "*Strings to be removed when doing fuzzy matches.
239 This can either be a regular expression or list of regular expressions
240 that will be removed from subject strings if fuzzy subject
241 simplification is selected."
242   :group 'gnus-thread
243   :type '(repeat regexp))
244
245 (defcustom gnus-show-threads t
246   "*If non-nil, display threads in summary mode."
247   :group 'gnus-thread
248   :type 'boolean)
249
250 (defcustom gnus-thread-hide-subtree nil
251   "*If non-nil, hide all threads initially.
252 This can be a predicate specifier which says which threads to hide.
253 If threads are hidden, you have to run the command
254 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
255 to expose hidden threads."
256   :group 'gnus-thread
257   :type '(radio (sexp :format "Non-nil\n"
258                       :match (lambda (widget value)
259                                (not (or (consp value) (functionp value))))
260                       :value t)
261                 (const nil)
262                 (sexp :tag "Predicate specifier" :size 0)))
263
264 (defcustom gnus-thread-hide-killed t
265   "*If non-nil, hide killed threads automatically."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-ignore-subject t
270   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
271 If nil, articles that have different subjects from their parents will
272 start separate threads."
273   :group 'gnus-thread
274   :type 'boolean)
275
276 (defcustom gnus-thread-operation-ignore-subject t
277   "*If non-nil, subjects will be ignored when doing thread commands.
278 This affects commands like `gnus-summary-kill-thread' and
279 `gnus-summary-lower-thread'.
280
281 If this variable is nil, articles in the same thread with different
282 subjects will not be included in the operation in question.  If this
283 variable is `fuzzy', only articles that have subjects that are fuzzily
284 equal will be included."
285   :group 'gnus-thread
286   :type '(choice (const :tag "off" nil)
287                  (const fuzzy)
288                  (sexp :tag "on" t)))
289
290 (defcustom gnus-thread-indent-level 4
291   "*Number that says how much each sub-thread should be indented."
292   :group 'gnus-thread
293   :type 'integer)
294
295 (defcustom gnus-auto-extend-newsgroup t
296   "*If non-nil, extend newsgroup forward and backward when requested."
297   :group 'gnus-summary-choose
298   :type 'boolean)
299
300 (defcustom gnus-auto-select-first t
301   "*If non-nil, select the article under point.
302 Which article this is is controlled by the `gnus-auto-select-subject'
303 variable.
304
305 If you want to prevent automatic selection of articles in some
306 newsgroups, set the variable to nil in `gnus-select-group-hook'."
307   :group 'gnus-group-select
308   :type '(choice (const :tag "none" nil)
309                  (sexp :menu-tag "first" t)))
310
311 (defcustom gnus-auto-select-subject 'unread
312   "*Says what subject to place under point when entering a group.
313
314 This variable can either be the symbols `first' (place point on the
315 first subject), `unread' (place point on the subject line of the first
316 unread article), `best' (place point on the subject line of the
317 higest-scored article), `unseen' (place point on the subject line of
318 the first unseen article), 'unseen-or-unread' (place point on the subject
319 line of the first unseen article or, if all article have been seen, on the
320 subject line of the first unread article), or a function to be called to
321 place point on some subject line."
322   :group 'gnus-group-select
323   :type '(choice (const best)
324                  (const unread)
325                  (const first)
326                  (const unseen)
327                  (const unseen-or-unread)))
328
329 (defcustom gnus-auto-select-next t
330   "*If non-nil, offer to go to the next group from the end of the previous.
331 If the value is t and the next newsgroup is empty, Gnus will exit
332 summary mode and go back to group mode.  If the value is neither nil
333 nor t, Gnus will select the following unread newsgroup.  In
334 particular, if the value is the symbol `quietly', the next unread
335 newsgroup will be selected without any confirmation, and if it is
336 `almost-quietly', the next group will be selected without any
337 confirmation if you are located on the last article in the group.
338 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
339 will go to the next group without confirmation."
340   :group 'gnus-summary-maneuvering
341   :type '(choice (const :tag "off" nil)
342                  (const quietly)
343                  (const almost-quietly)
344                  (const slightly-quietly)
345                  (sexp :menu-tag "on" t)))
346
347 (defcustom gnus-auto-select-same nil
348   "*If non-nil, select the next article with the same subject.
349 If there are no more articles with the same subject, go to
350 the first unread article."
351   :group 'gnus-summary-maneuvering
352   :type 'boolean)
353
354 (defcustom gnus-auto-goto-ignores 'unfetched
355   "*Says how to handle unfetched articles when maneuvering.
356
357 This variable can either be the symbols nil (maneuver to any
358 article), `undownloaded' (maneuvering while unplugged ignores articles
359 that have not been fetched), `always-undownloaded' (maneuvering always
360 ignores articles that have not been fetched), `unfetched' (maneuvering
361 ignores articles whose headers have not been fetched).
362
363 NOTE: The list of unfetched articles will always be nil when plugged
364 and, when unplugged, a subset of the undownloaded article list."
365   :group 'gnus-summary-maneuvering
366   :type '(choice (const :tag "None" nil)
367                  (const :tag "Undownloaded when unplugged" undownloaded)
368                  (const :tag "Undownloaded" always-undownloaded)
369                  (const :tag "Unfetched" unfetched)))
370
371 (defcustom gnus-summary-check-current nil
372   "*If non-nil, consider the current article when moving.
373 The \"unread\" movement commands will stay on the same line if the
374 current article is unread."
375   :group 'gnus-summary-maneuvering
376   :type 'boolean)
377
378 (defcustom gnus-auto-center-summary t
379   "*If non-nil, always center the current summary buffer.
380 In particular, if `vertical' do only vertical recentering.  If non-nil
381 and non-`vertical', do both horizontal and vertical recentering."
382   :group 'gnus-summary-maneuvering
383   :type '(choice (const :tag "none" nil)
384                  (const vertical)
385                  (integer :tag "height")
386                  (sexp :menu-tag "both" t)))
387
388 (defvar gnus-auto-center-group t
389   "*If non-nil, always center the group buffer.")
390
391 (defcustom gnus-show-all-headers nil
392   "*If non-nil, don't hide any headers."
393   :group 'gnus-article-hiding
394   :group 'gnus-article-headers
395   :type 'boolean)
396
397 (defcustom gnus-summary-ignore-duplicates nil
398   "*If non-nil, ignore articles with identical Message-ID headers."
399   :group 'gnus-summary
400   :type 'boolean)
401
402 (defcustom gnus-single-article-buffer t
403   "*If non-nil, display all articles in the same buffer.
404 If nil, each group will get its own article buffer."
405   :group 'gnus-article-various
406   :type 'boolean)
407
408 (defcustom gnus-break-pages t
409   "*If non-nil, do page breaking on articles.
410 The page delimiter is specified by the `gnus-page-delimiter'
411 variable."
412   :group 'gnus-article-various
413   :type 'boolean)
414
415 (defcustom gnus-move-split-methods nil
416   "*Variable used to suggest where articles are to be moved to.
417 It uses the same syntax as the `gnus-split-methods' variable.
418 However, whereas `gnus-split-methods' specifies file names as targets,
419 this variable specifies group names."
420   :group 'gnus-summary-mail
421   :type '(repeat (choice (list :value (fun) function)
422                          (cons :value ("" "") regexp (repeat string))
423                          (sexp :value nil))))
424
425 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
426   "Function used to compute default prefix for article move/copy/etc prompts.
427 The function should take one argument, a group name, and return a
428 string with the suggested prefix."
429   :group 'gnus-summary-mail
430   :type 'function)
431
432 (defcustom gnus-unread-mark ?           ;Whitespace
433   "*Mark used for unread articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-ticked-mark ?!
438   "*Mark used for ticked articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-dormant-mark ??
443   "*Mark used for dormant articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-del-mark ?r
448   "*Mark used for del'd articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-read-mark ?R
453   "*Mark used for read articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-expirable-mark ?E
458   "*Mark used for expirable articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-killed-mark ?K
463   "*Mark used for killed articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-spam-mark ?$
468   "*Mark used for spam articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-souped-mark ?F
473   "*Mark used for souped articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-kill-file-mark ?X
478   "*Mark used for articles killed by kill files."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-low-score-mark ?Y
483   "*Mark used for articles with a low score."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-catchup-mark ?C
488   "*Mark used for articles that are caught up."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-replied-mark ?A
493   "*Mark used for articles that have been replied to."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-forwarded-mark ?F
498   "*Mark used for articles that have been forwarded."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-recent-mark ?N
503   "*Mark used for articles that are recent."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-cached-mark ?*
508   "*Mark used for articles that are in the cache."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-saved-mark ?S
513   "*Mark used for articles that have been saved."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-unseen-mark ?.
518   "*Mark used for articles that haven't been seen."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-no-mark ?               ;Whitespace
523   "*Mark used for articles that have no other secondary mark."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-ancient-mark ?O
528   "*Mark used for ancient articles."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-sparse-mark ?Q
533   "*Mark used for sparsely reffed articles."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-canceled-mark ?G
538   "*Mark used for canceled articles."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-duplicate-mark ?M
543   "*Mark used for duplicate articles."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-undownloaded-mark ?-
548   "*Mark used for articles that weren't downloaded."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-downloaded-mark ?+
553   "*Mark used for articles that were downloaded."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-downloadable-mark ?%
558   "*Mark used for articles that are to be downloaded."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-unsendable-mark ?=
563   "*Mark used for articles that won't be sent."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-score-over-mark ?+
568   "*Score mark used for articles with high scores."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-score-below-mark ?-
573   "*Score mark used for articles with low scores."
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-empty-thread-mark ?     ;Whitespace
578   "*There is no thread under the article."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-not-empty-thread-mark ?=
583   "*There is a thread under the article."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-view-pseudo-asynchronously nil
588   "*If non-nil, Gnus will view pseudo-articles asynchronously."
589   :group 'gnus-extract-view
590   :type 'boolean)
591
592 (defcustom gnus-auto-expirable-marks
593   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
594         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
595         gnus-souped-mark gnus-duplicate-mark)
596   "*The list of marks converted into expiration if a group is auto-expirable."
597   :version "21.1"
598   :group 'gnus-summary
599   :type '(repeat character))
600
601 (defcustom gnus-inhibit-user-auto-expire t
602   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
603   :version "21.1"
604   :group 'gnus-summary
605   :type 'boolean)
606
607 (defcustom gnus-view-pseudos nil
608   "*If `automatic', pseudo-articles will be viewed automatically.
609 If `not-confirm', pseudos will be viewed automatically, and the user
610 will not be asked to confirm the command."
611   :group 'gnus-extract-view
612   :type '(choice (const :tag "off" nil)
613                  (const automatic)
614                  (const not-confirm)))
615
616 (defcustom gnus-view-pseudos-separately t
617   "*If non-nil, one pseudo-article will be created for each file to be viewed.
618 If nil, all files that use the same viewing command will be given as a
619 list of parameters to that command."
620   :group 'gnus-extract-view
621   :type 'boolean)
622
623 (defcustom gnus-insert-pseudo-articles t
624   "*If non-nil, insert pseudo-articles when decoding articles."
625   :group 'gnus-extract-view
626   :type 'boolean)
627
628 (defcustom gnus-summary-dummy-line-format
629   "   %(:                             :%) %S\n"
630   "*The format specification for the dummy roots in the summary buffer.
631 It works along the same lines as a normal formatting string,
632 with some simple extensions.
633
634 %S  The subject
635
636 General format specifiers can also be used.
637 See `(gnus)Formatting Variables'."
638   :link '(custom-manual "(gnus)Formatting Variables")
639   :group 'gnus-threading
640   :type 'string)
641
642 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
643   "*The format specification for the summary mode line.
644 It works along the same lines as a normal formatting string,
645 with some simple extensions:
646
647 %G  Group name
648 %p  Unprefixed group name
649 %A  Current article number
650 %z  Current article score
651 %V  Gnus version
652 %U  Number of unread articles in the group
653 %e  Number of unselected articles in the group
654 %Z  A string with unread/unselected article counts
655 %g  Shortish group name
656 %S  Subject of the current article
657 %u  User-defined spec
658 %s  Current score file name
659 %d  Number of dormant articles
660 %r  Number of articles that have been marked as read in this session
661 %E  Number of articles expunged by the score files"
662   :group 'gnus-summary-format
663   :type 'string)
664
665 (defcustom gnus-list-identifiers nil
666   "Regexp that matches list identifiers to be removed from subject.
667 This can also be a list of regexps."
668   :version "21.1"
669   :group 'gnus-summary-format
670   :group 'gnus-article-hiding
671   :type '(choice (const :tag "none" nil)
672                  (regexp :value ".*")
673                  (repeat :value (".*") regexp)))
674
675 (defcustom gnus-summary-mark-below 0
676   "*Mark all articles with a score below this variable as read.
677 This variable is local to each summary buffer and usually set by the
678 score file."
679   :group 'gnus-score-default
680   :type 'integer)
681
682 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
683   "*List of functions used for sorting articles in the summary buffer.
684
685 Each function takes two articles and returns non-nil if the first
686 article should be sorted before the other.  If you use more than one
687 function, the primary sort function should be the last.  You should
688 probably always include `gnus-article-sort-by-number' in the list of
689 sorting functions -- preferably first.  Also note that sorting by date
690 is often much slower than sorting by number, and the sorting order is
691 very similar.  (Sorting by date means sorting by the time the message
692 was sent, sorting by number means sorting by arrival time.)
693
694 Each item can also be a list `(not F)' where F is a function;
695 this reverses the sort order.
696
697 Ready-made functions include `gnus-article-sort-by-number',
698 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
699 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
700 and `gnus-article-sort-by-score'.
701
702 When threading is turned on, the variable `gnus-thread-sort-functions'
703 controls how articles are sorted."
704   :group 'gnus-summary-sort
705   :type '(repeat (choice (function-item gnus-article-sort-by-number)
706                          (function-item gnus-article-sort-by-author)
707                          (function-item gnus-article-sort-by-subject)
708                          (function-item gnus-article-sort-by-date)
709                          (function-item gnus-article-sort-by-score)
710                          (function-item gnus-article-sort-by-random)
711                          (function :tag "other"))))
712
713 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
714   "*List of functions used for sorting threads in the summary buffer.
715 By default, threads are sorted by article number.
716
717 Each function takes two threads and returns non-nil if the first
718 thread should be sorted before the other.  If you use more than one
719 function, the primary sort function should be the last.  You should
720 probably always include `gnus-thread-sort-by-number' in the list of
721 sorting functions -- preferably first.  Also note that sorting by date
722 is often much slower than sorting by number, and the sorting order is
723 very similar.  (Sorting by date means sorting by the time the message
724 was sent, sorting by number means sorting by arrival time.)
725
726 Each list item can also be a list `(not F)' where F is a
727 function; this specifies reversed sort order.
728
729 Ready-made functions include `gnus-thread-sort-by-number',
730 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
731 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
732 `gnus-thread-sort-by-most-recent-number',
733 `gnus-thread-sort-by-most-recent-date',
734 `gnus-thread-sort-by-random', and
735 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
736
737 When threading is turned off, the variable
738 `gnus-article-sort-functions' controls how articles are sorted."
739   :group 'gnus-summary-sort
740   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
741                          (function-item gnus-thread-sort-by-author)
742                          (function-item gnus-thread-sort-by-subject)
743                          (function-item gnus-thread-sort-by-date)
744                          (function-item gnus-thread-sort-by-score)
745                          (function-item gnus-thread-sort-by-most-recent-number)
746                          (function-item gnus-thread-sort-by-most-recent-date)
747                          (function-item gnus-thread-sort-by-random)
748                          (function-item gnus-thread-sort-by-total-score)
749                          (function :tag "other"))))
750
751 (defcustom gnus-thread-score-function '+
752   "*Function used for calculating the total score of a thread.
753
754 The function is called with the scores of the article and each
755 subthread and should then return the score of the thread.
756
757 Some functions you can use are `+', `max', or `min'."
758   :group 'gnus-summary-sort
759   :type 'function)
760
761 (defcustom gnus-summary-expunge-below nil
762   "All articles that have a score less than this variable will be expunged.
763 This variable is local to the summary buffers."
764   :group 'gnus-score-default
765   :type '(choice (const :tag "off" nil)
766                  integer))
767
768 (defcustom gnus-thread-expunge-below nil
769   "All threads that have a total score less than this variable will be expunged.
770 See `gnus-thread-score-function' for en explanation of what a
771 \"thread score\" is.
772
773 This variable is local to the summary buffers."
774   :group 'gnus-threading
775   :group 'gnus-score-default
776   :type '(choice (const :tag "off" nil)
777                  integer))
778
779 (defcustom gnus-summary-mode-hook nil
780   "*A hook for Gnus summary mode.
781 This hook is run before any variables are set in the summary buffer."
782   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
783   :group 'gnus-summary-various
784   :type 'hook)
785
786 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
787 (when (featurep 'xemacs)
788   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
789   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
790   (add-hook 'gnus-summary-mode-hook
791             'gnus-xmas-switch-horizontal-scrollbar-off))
792
793 (defcustom gnus-summary-menu-hook nil
794   "*Hook run after the creation of the summary mode menu."
795   :group 'gnus-summary-visual
796   :type 'hook)
797
798 (defcustom gnus-summary-exit-hook nil
799   "*A hook called on exit from the summary buffer.
800 It will be called with point in the group buffer."
801   :group 'gnus-summary-exit
802   :type 'hook)
803
804 (defcustom gnus-summary-prepare-hook nil
805   "*A hook called after the summary buffer has been generated.
806 If you want to modify the summary buffer, you can use this hook."
807   :group 'gnus-summary-various
808   :type 'hook)
809
810 (defcustom gnus-summary-prepared-hook nil
811   "*A hook called as the last thing after the summary buffer has been generated."
812   :group 'gnus-summary-various
813   :type 'hook)
814
815 (defcustom gnus-summary-generate-hook nil
816   "*A hook run just before generating the summary buffer.
817 This hook is commonly used to customize threading variables and the
818 like."
819   :group 'gnus-summary-various
820   :type 'hook)
821
822 (defcustom gnus-select-group-hook nil
823   "*A hook called when a newsgroup is selected.
824
825 If you'd like to simplify subjects like the
826 `gnus-summary-next-same-subject' command does, you can use the
827 following hook:
828
829  (add-hook gnus-select-group-hook
830            (lambda ()
831              (mapcar (lambda (header)
832                        (mail-header-set-subject
833                         header
834                         (gnus-simplify-subject
835                          (mail-header-subject header) 're-only)))
836                      gnus-newsgroup-headers)))"
837   :group 'gnus-group-select
838   :type 'hook)
839
840 (defcustom gnus-select-article-hook nil
841   "*A hook called when an article is selected."
842   :group 'gnus-summary-choose
843   :options '(gnus-agent-fetch-selected-article)
844   :type 'hook)
845
846 (defcustom gnus-visual-mark-article-hook
847   (list 'gnus-highlight-selected-summary)
848   "*Hook run after selecting an article in the summary buffer.
849 It is meant to be used for highlighting the article in some way.  It
850 is not run if `gnus-visual' is nil."
851   :group 'gnus-summary-visual
852   :type 'hook)
853
854 (defcustom gnus-parse-headers-hook nil
855   "*A hook called before parsing the headers."
856   :group 'gnus-various
857   :type 'hook)
858
859 (defcustom gnus-exit-group-hook nil
860   "*A hook called when exiting summary mode.
861 This hook is not called from the non-updating exit commands like `Q'."
862   :group 'gnus-various
863   :type 'hook)
864
865 (defcustom gnus-summary-update-hook
866   (list 'gnus-summary-highlight-line)
867   "*A hook called when a summary line is changed.
868 The hook will not be called if `gnus-visual' is nil.
869
870 The default function `gnus-summary-highlight-line' will
871 highlight the line according to the `gnus-summary-highlight'
872 variable."
873   :group 'gnus-summary-visual
874   :type 'hook)
875
876 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
877   "*A hook called when an article is selected for the first time.
878 The hook is intended to mark an article as read (or unread)
879 automatically when it is selected."
880   :group 'gnus-summary-choose
881   :type 'hook)
882
883 (defcustom gnus-group-no-more-groups-hook nil
884   "*A hook run when returning to group mode having no more (unread) groups."
885   :group 'gnus-group-select
886   :type 'hook)
887
888 (defcustom gnus-ps-print-hook nil
889   "*A hook run before ps-printing something from Gnus."
890   :group 'gnus-summary
891   :type 'hook)
892
893 (defcustom gnus-summary-article-move-hook nil
894   "*A hook called after an article is moved, copied, respooled, or crossposted."
895   :group 'gnus-summary
896   :type 'hook)
897
898 (defcustom gnus-summary-article-delete-hook nil
899   "*A hook called after an article is deleted."
900   :group 'gnus-summary
901   :type 'hook)
902
903 (defcustom gnus-summary-article-expire-hook nil
904   "*A hook called after an article is expired."
905   :group 'gnus-summary
906   :type 'hook)
907
908 (defcustom gnus-summary-display-arrow
909   (and (fboundp 'display-graphic-p)
910        (display-graphic-p))
911   "*If non-nil, display an arrow highlighting the current article."
912   :version "21.1"
913   :group 'gnus-summary
914   :type 'boolean)
915
916 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
917   "Face used for highlighting the current article in the summary buffer."
918   :group 'gnus-summary-visual
919   :type 'face)
920
921 (defvar gnus-tmp-downloaded nil)
922
923 (defcustom gnus-summary-highlight
924   '(((eq mark gnus-canceled-mark)
925      . gnus-summary-cancelled-face)
926     ((and uncached (> score default-high))
927      . gnus-summary-high-undownloaded-face)
928     ((and uncached (< score default-low))
929      . gnus-summary-low-undownloaded-face)
930     (uncached
931      . gnus-summary-normal-undownloaded-face)
932     ((and (> score default-high)
933           (or (eq mark gnus-dormant-mark)
934               (eq mark gnus-ticked-mark)))
935      . gnus-summary-high-ticked-face)
936     ((and (< score default-low)
937           (or (eq mark gnus-dormant-mark)
938               (eq mark gnus-ticked-mark)))
939      . gnus-summary-low-ticked-face)
940     ((or (eq mark gnus-dormant-mark)
941          (eq mark gnus-ticked-mark))
942      . gnus-summary-normal-ticked-face)
943     ((and (> score default-high) (eq mark gnus-ancient-mark))
944      . gnus-summary-high-ancient-face)
945     ((and (< score default-low) (eq mark gnus-ancient-mark))
946      . gnus-summary-low-ancient-face)
947     ((eq mark gnus-ancient-mark)
948      . gnus-summary-normal-ancient-face)
949     ((and (> score default-high) (eq mark gnus-unread-mark))
950      . gnus-summary-high-unread-face)
951     ((and (< score default-low) (eq mark gnus-unread-mark))
952      . gnus-summary-low-unread-face)
953     ((eq mark gnus-unread-mark)
954      . gnus-summary-normal-unread-face)
955     ((> score default-high)
956      . gnus-summary-high-read-face)
957     ((< score default-low)
958      . gnus-summary-low-read-face)
959     (t
960      . gnus-summary-normal-read-face))
961   "*Controls the highlighting of summary buffer lines.
962
963 A list of (FORM . FACE) pairs.  When deciding how a a particular
964 summary line should be displayed, each form is evaluated.  The content
965 of the face field after the first true form is used.  You can change
966 how those summary lines are displayed, by editing the face field.
967
968 You can use the following variables in the FORM field.
969
970 score:        The article's score
971 default:      The default article score.
972 default-high: The default score for high scored articles.
973 default-low:  The default score for low scored articles.
974 below:        The score below which articles are automatically marked as read.
975 mark:         The article's mark.
976 uncached:     Non-nil if the article is uncached."
977   :group 'gnus-summary-visual
978   :type '(repeat (cons (sexp :tag "Form" nil)
979                        face)))
980
981 (defcustom gnus-alter-header-function nil
982   "Function called to allow alteration of article header structures.
983 The function is called with one parameter, the article header vector,
984 which it may alter in any way."
985   :type '(choice (const :tag "None" nil)
986                  function)
987   :group 'gnus-summary)
988
989 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
990   "Variable that says which function should be used to decode a string with encoded words.")
991
992 (defcustom gnus-extra-headers '(To Newsgroups)
993   "*Extra headers to parse."
994   :version "21.1"
995   :group 'gnus-summary
996   :type '(repeat symbol))
997
998 (defcustom gnus-ignored-from-addresses
999   (and user-mail-address (regexp-quote user-mail-address))
1000   "*Regexp of From headers that may be suppressed in favor of To headers."
1001   :version "21.1"
1002   :group 'gnus-summary
1003   :type 'regexp)
1004
1005 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1006   "List of charsets that should be ignored.
1007 When these charsets are used in the \"charset\" parameter, the
1008 default charset will be used instead."
1009   :version "21.1"
1010   :type '(repeat symbol)
1011   :group 'gnus-charset)
1012
1013 (gnus-define-group-parameter
1014  ignored-charsets
1015  :type list
1016  :function-document
1017  "Return the ignored charsets of GROUP."
1018  :variable gnus-group-ignored-charsets-alist
1019  :variable-default
1020  '(("alt\\.chinese\\.text" iso-8859-1))
1021  :variable-document
1022  "Alist of regexps (to match group names) and charsets that should be ignored.
1023 When these charsets are used in the \"charset\" parameter, the
1024 default charset will be used instead."
1025  :variable-group gnus-charset
1026  :variable-type '(repeat (cons (regexp :tag "Group")
1027                                (repeat symbol)))
1028  :parameter-type '(choice :tag "Ignored charsets"
1029                           :value nil
1030                           (repeat (symbol)))
1031  :parameter-document       "\
1032 List of charsets that should be ignored.
1033
1034 When these charsets are used in the \"charset\" parameter, the
1035 default charset will be used instead.")
1036
1037 (defcustom gnus-group-highlight-words-alist nil
1038   "Alist of group regexps and highlight regexps.
1039 This variable uses the same syntax as `gnus-emphasis-alist'."
1040   :version "21.1"
1041   :type '(repeat (cons (regexp :tag "Group")
1042                        (repeat (list (regexp :tag "Highlight regexp")
1043                                      (number :tag "Group for entire word" 0)
1044                                      (number :tag "Group for displayed part" 0)
1045                                      (symbol :tag "Face"
1046                                              gnus-emphasis-highlight-words)))))
1047   :group 'gnus-summary-visual)
1048
1049 (defcustom gnus-summary-show-article-charset-alist
1050   nil
1051   "Alist of number and charset.
1052 The article will be shown with the charset corresponding to the
1053 numbered argument.
1054 For example: ((1 . cn-gb-2312) (2 . big5))."
1055   :version "21.1"
1056   :type '(repeat (cons (number :tag "Argument" 1)
1057                        (symbol :tag "Charset")))
1058   :group 'gnus-charset)
1059
1060 (defcustom gnus-preserve-marks t
1061   "Whether marks are preserved when moving, copying and respooling messages."
1062   :version "21.1"
1063   :type 'boolean
1064   :group 'gnus-summary-marks)
1065
1066 (defcustom gnus-alter-articles-to-read-function nil
1067   "Function to be called to alter the list of articles to be selected."
1068   :type '(choice (const nil) function)
1069   :group 'gnus-summary)
1070
1071 (defcustom gnus-orphan-score nil
1072   "*All orphans get this score added.  Set in the score file."
1073   :group 'gnus-score-default
1074   :type '(choice (const nil)
1075                  integer))
1076
1077 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1078   "*A regexp to match MIME parts when saving multiple parts of a
1079 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1080 This regexp will be used by default when prompting the user for which
1081 type of files to save."
1082   :group 'gnus-summary
1083   :type 'regexp)
1084
1085 (defcustom gnus-read-all-available-headers nil
1086   "Whether Gnus should parse all headers made available to it.
1087 This is mostly relevant for slow back ends where the user may
1088 wish to widen the summary buffer to include all headers
1089 that were fetched.  Say, for nnultimate groups."
1090   :group 'gnus-summary
1091   :type '(choice boolean regexp))
1092
1093 (defcustom gnus-summary-muttprint-program "muttprint"
1094   "Command (and optional arguments) used to run Muttprint."
1095   :version "21.3"
1096   :group 'gnus-summary
1097   :type 'string)
1098
1099 (defcustom gnus-article-loose-mime nil
1100   "If non-nil, don't require MIME-Version header.
1101 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1102 supply the MIME-Version header or deliberately strip it From the mail.
1103 Set it to non-nil, Gnus will treat some articles as MIME even if
1104 the MIME-Version header is missed."
1105   :version "21.3"
1106   :type 'boolean
1107   :group 'gnus-article-mime)
1108
1109 (defcustom gnus-article-emulate-mime t
1110   "If non-nil, use MIME emulation for uuencode and the like.
1111 This means that Gnus will search message bodies for text that look
1112 like uuencoded bits, yEncoded bits, and so on, and present that using
1113 the normal Gnus MIME machinery."
1114   :type 'boolean
1115   :group 'gnus-article-mime)
1116
1117 ;;; Internal variables
1118
1119 (defvar gnus-summary-display-cache nil)
1120 (defvar gnus-article-mime-handles nil)
1121 (defvar gnus-article-decoded-p nil)
1122 (defvar gnus-article-charset nil)
1123 (defvar gnus-article-ignored-charsets nil)
1124 (defvar gnus-scores-exclude-files nil)
1125 (defvar gnus-page-broken nil)
1126
1127 (defvar gnus-original-article nil)
1128 (defvar gnus-article-internal-prepare-hook nil)
1129 (defvar gnus-newsgroup-process-stack nil)
1130
1131 (defvar gnus-thread-indent-array nil)
1132 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1133 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1134   "Function called to sort the articles within a thread after it has been gathered together.")
1135
1136 (defvar gnus-summary-save-parts-type-history nil)
1137 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1138
1139 ;; Avoid highlighting in kill files.
1140 (defvar gnus-summary-inhibit-highlight nil)
1141 (defvar gnus-newsgroup-selected-overlay nil)
1142 (defvar gnus-inhibit-limiting nil)
1143 (defvar gnus-newsgroup-adaptive-score-file nil)
1144 (defvar gnus-current-score-file nil)
1145 (defvar gnus-current-move-group nil)
1146 (defvar gnus-current-copy-group nil)
1147 (defvar gnus-current-crosspost-group nil)
1148 (defvar gnus-newsgroup-display nil)
1149
1150 (defvar gnus-newsgroup-dependencies nil)
1151 (defvar gnus-newsgroup-adaptive nil)
1152 (defvar gnus-summary-display-article-function nil)
1153 (defvar gnus-summary-highlight-line-function nil
1154   "Function called after highlighting a summary line.")
1155
1156 (defvar gnus-summary-line-format-alist
1157   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1158     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1159     (?s gnus-tmp-subject-or-nil ?s)
1160     (?n gnus-tmp-name ?s)
1161     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1162         ?s)
1163     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1164             gnus-tmp-from) ?s)
1165     (?F gnus-tmp-from ?s)
1166     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1167     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1168     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1169     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1170     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1171     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1172     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1173     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1174     (?L gnus-tmp-lines ?s)
1175     (?O gnus-tmp-downloaded ?c)
1176     (?I gnus-tmp-indentation ?s)
1177     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1178     (?R gnus-tmp-replied ?c)
1179     (?\[ gnus-tmp-opening-bracket ?c)
1180     (?\] gnus-tmp-closing-bracket ?c)
1181     (?\> (make-string gnus-tmp-level ? ) ?s)
1182     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1183     (?i gnus-tmp-score ?d)
1184     (?z gnus-tmp-score-char ?c)
1185     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1186     (?U gnus-tmp-unread ?c)
1187     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1188         ?s)
1189     (?t (gnus-summary-number-of-articles-in-thread
1190          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1191         ?d)
1192     (?e (gnus-summary-number-of-articles-in-thread
1193          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1194         ?c)
1195     (?u gnus-tmp-user-defined ?s)
1196     (?P (gnus-pick-line-number) ?d)
1197     (?B gnus-tmp-thread-tree-header-string ?s)
1198     (user-date (gnus-user-date
1199                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1200   "An alist of format specifications that can appear in summary lines.
1201 These are paired with what variables they correspond with, along with
1202 the type of the variable (string, integer, character, etc).")
1203
1204 (defvar gnus-summary-dummy-line-format-alist
1205   `((?S gnus-tmp-subject ?s)
1206     (?N gnus-tmp-number ?d)
1207     (?u gnus-tmp-user-defined ?s)))
1208
1209 (defvar gnus-summary-mode-line-format-alist
1210   `((?G gnus-tmp-group-name ?s)
1211     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1212     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1213     (?A gnus-tmp-article-number ?d)
1214     (?Z gnus-tmp-unread-and-unselected ?s)
1215     (?V gnus-version ?s)
1216     (?U gnus-tmp-unread-and-unticked ?d)
1217     (?S gnus-tmp-subject ?s)
1218     (?e gnus-tmp-unselected ?d)
1219     (?u gnus-tmp-user-defined ?s)
1220     (?d (length gnus-newsgroup-dormant) ?d)
1221     (?t (length gnus-newsgroup-marked) ?d)
1222     (?h (length gnus-newsgroup-spam-marked) ?d)
1223     (?r (length gnus-newsgroup-reads) ?d)
1224     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1225     (?E gnus-newsgroup-expunged-tally ?d)
1226     (?s (gnus-current-score-file-nondirectory) ?s)))
1227
1228 (defvar gnus-last-search-regexp nil
1229   "Default regexp for article search command.")
1230
1231 (defvar gnus-last-shell-command nil
1232   "Default shell command on article.")
1233
1234 (defvar gnus-newsgroup-agentized nil
1235   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1236 (defvar gnus-newsgroup-begin nil)
1237 (defvar gnus-newsgroup-end nil)
1238 (defvar gnus-newsgroup-last-rmail nil)
1239 (defvar gnus-newsgroup-last-mail nil)
1240 (defvar gnus-newsgroup-last-folder nil)
1241 (defvar gnus-newsgroup-last-file nil)
1242 (defvar gnus-newsgroup-auto-expire nil)
1243 (defvar gnus-newsgroup-active nil)
1244
1245 (defvar gnus-newsgroup-data nil)
1246 (defvar gnus-newsgroup-data-reverse nil)
1247 (defvar gnus-newsgroup-limit nil)
1248 (defvar gnus-newsgroup-limits nil)
1249 (defvar gnus-summary-use-undownloaded-faces nil)
1250
1251 (defvar gnus-newsgroup-unreads nil
1252   "Sorted list of unread articles in the current newsgroup.")
1253
1254 (defvar gnus-newsgroup-unselected nil
1255   "Sorted list of unselected unread articles in the current newsgroup.")
1256
1257 (defvar gnus-newsgroup-reads nil
1258   "Alist of read articles and article marks in the current newsgroup.")
1259
1260 (defvar gnus-newsgroup-expunged-tally nil)
1261
1262 (defvar gnus-newsgroup-marked nil
1263   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1264
1265 (defvar gnus-newsgroup-spam-marked nil
1266   "List of ranges of articles that have been marked as spam.")
1267
1268 (defvar gnus-newsgroup-killed nil
1269   "List of ranges of articles that have been through the scoring process.")
1270
1271 (defvar gnus-newsgroup-cached nil
1272   "Sorted list of articles that come from the article cache.")
1273
1274 (defvar gnus-newsgroup-saved nil
1275   "List of articles that have been saved.")
1276
1277 (defvar gnus-newsgroup-kill-headers nil)
1278
1279 (defvar gnus-newsgroup-replied nil
1280   "List of articles that have been replied to in the current newsgroup.")
1281
1282 (defvar gnus-newsgroup-forwarded nil
1283   "List of articles that have been forwarded in the current newsgroup.")
1284
1285 (defvar gnus-newsgroup-recent nil
1286   "List of articles that have are recent in the current newsgroup.")
1287
1288 (defvar gnus-newsgroup-expirable nil
1289   "Sorted list of articles in the current newsgroup that can be expired.")
1290
1291 (defvar gnus-newsgroup-processable nil
1292   "List of articles in the current newsgroup that can be processed.")
1293
1294 (defvar gnus-newsgroup-downloadable nil
1295   "Sorted list of articles in the current newsgroup that can be processed.")
1296
1297 (defvar gnus-newsgroup-unfetched nil
1298   "Sorted list of articles in the current newsgroup whose headers have
1299 not been fetched into the agent.
1300
1301 This list will always be a subset of gnus-newsgroup-undownloaded.")
1302
1303 (defvar gnus-newsgroup-undownloaded nil
1304   "List of articles in the current newsgroup that haven't been downloaded.")
1305
1306 (defvar gnus-newsgroup-unsendable nil
1307   "List of articles in the current newsgroup that won't be sent.")
1308
1309 (defvar gnus-newsgroup-bookmarks nil
1310   "List of articles in the current newsgroup that have bookmarks.")
1311
1312 (defvar gnus-newsgroup-dormant nil
1313   "Sorted list of dormant articles in the current newsgroup.")
1314
1315 (defvar gnus-newsgroup-unseen nil
1316   "List of unseen articles in the current newsgroup.")
1317
1318 (defvar gnus-newsgroup-seen nil
1319   "Range of seen articles in the current newsgroup.")
1320
1321 (defvar gnus-newsgroup-articles nil
1322   "List of articles in the current newsgroup.")
1323
1324 (defvar gnus-newsgroup-scored nil
1325   "List of scored articles in the current newsgroup.")
1326
1327 (defvar gnus-newsgroup-headers nil
1328   "List of article headers in the current newsgroup.")
1329
1330 (defvar gnus-newsgroup-threads nil)
1331
1332 (defvar gnus-newsgroup-prepared nil
1333   "Whether the current group has been prepared properly.")
1334
1335 (defvar gnus-newsgroup-ancient nil
1336   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1337
1338 (defvar gnus-newsgroup-sparse nil)
1339
1340 (defvar gnus-current-article nil)
1341 (defvar gnus-article-current nil)
1342 (defvar gnus-current-headers nil)
1343 (defvar gnus-have-all-headers nil)
1344 (defvar gnus-last-article nil)
1345 (defvar gnus-newsgroup-history nil)
1346 (defvar gnus-newsgroup-charset nil)
1347 (defvar gnus-newsgroup-ephemeral-charset nil)
1348 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1349
1350 (defvar gnus-article-before-search nil)
1351
1352 (defvar gnus-summary-local-variables
1353   '(gnus-newsgroup-name
1354     gnus-newsgroup-begin gnus-newsgroup-end
1355     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1356     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1357     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1358     gnus-newsgroup-unselected gnus-newsgroup-marked
1359     gnus-newsgroup-spam-marked
1360     gnus-newsgroup-reads gnus-newsgroup-saved
1361     gnus-newsgroup-replied gnus-newsgroup-forwarded
1362     gnus-newsgroup-recent
1363     gnus-newsgroup-expirable
1364     gnus-newsgroup-processable gnus-newsgroup-killed
1365     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1366     gnus-newsgroup-unfetched
1367     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1368     gnus-newsgroup-seen gnus-newsgroup-articles
1369     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1370     gnus-newsgroup-headers gnus-newsgroup-threads
1371     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1372     gnus-current-article gnus-current-headers gnus-have-all-headers
1373     gnus-last-article gnus-article-internal-prepare-hook
1374     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1375     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1376     gnus-thread-expunge-below
1377     gnus-score-alist gnus-current-score-file
1378     (gnus-summary-expunge-below . global)
1379     (gnus-summary-mark-below . global)
1380     (gnus-orphan-score . global)
1381     gnus-newsgroup-active gnus-scores-exclude-files
1382     gnus-newsgroup-history gnus-newsgroup-ancient
1383     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1384     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1385     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1386     (gnus-newsgroup-expunged-tally . 0)
1387     gnus-cache-removable-articles gnus-newsgroup-cached
1388     gnus-newsgroup-data gnus-newsgroup-data-reverse
1389     gnus-newsgroup-limit gnus-newsgroup-limits
1390     gnus-newsgroup-charset gnus-newsgroup-display
1391     gnus-summary-use-undownloaded-faces)
1392   "Variables that are buffer-local to the summary buffers.")
1393
1394 (defvar gnus-newsgroup-variables nil
1395   "A list of variables that have separate values in different newsgroups.
1396 A list of newsgroup (summary buffer) local variables, or cons of
1397 variables and their default values (when the default values are not
1398 nil), that should be made global while the summary buffer is active.
1399 These variables can be used to set variables in the group parameters
1400 while still allowing them to affect operations done in other
1401 buffers. For example:
1402
1403 \(setq gnus-newsgroup-variables
1404      '(message-use-followup-to
1405        (gnus-visible-headers .
1406          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1407 ")
1408
1409 ;; Byte-compiler warning.
1410 (eval-when-compile
1411   ;; Bind features so that require will believe that gnus-sum has
1412   ;; already been loaded (avoids infinite recursion)
1413   (let ((features (cons 'gnus-sum features)))
1414     ;; Several of the declarations in gnus-sum are needed to load the
1415     ;; following files. Right now, these definitions have been
1416     ;; compiled but not defined (evaluated).  We could either do a
1417     ;; eval-and-compile about all of the declarations or evaluate the
1418     ;; source file.
1419     (if (boundp 'gnus-newsgroup-variables)
1420         nil
1421       (load "gnus-sum.el" t t t))
1422     (require 'gnus)
1423     (require 'gnus-agent)
1424     (require 'gnus-art)))
1425
1426 ;; MIME stuff.
1427
1428 (defvar gnus-decode-encoded-word-methods
1429   '(mail-decode-encoded-word-string)
1430   "List of methods used to decode encoded words.
1431
1432 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1433 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1434 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1435 whose names match REGEXP.
1436
1437 For example:
1438 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1439  mail-decode-encoded-word-string
1440  (\"chinese\" . rfc1843-decode-string))")
1441
1442 (defvar gnus-decode-encoded-word-methods-cache nil)
1443
1444 (defun gnus-multi-decode-encoded-word-string (string)
1445   "Apply the functions from `gnus-encoded-word-methods' that match."
1446   (unless (and gnus-decode-encoded-word-methods-cache
1447                (eq gnus-newsgroup-name
1448                    (car gnus-decode-encoded-word-methods-cache)))
1449     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1450     (mapcar (lambda (x)
1451               (if (symbolp x)
1452                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1453                 (if (and gnus-newsgroup-name
1454                          (string-match (car x) gnus-newsgroup-name))
1455                     (nconc gnus-decode-encoded-word-methods-cache
1456                            (list (cdr x))))))
1457             gnus-decode-encoded-word-methods))
1458   (let ((xlist gnus-decode-encoded-word-methods-cache))
1459     (pop xlist)
1460     (while xlist
1461       (setq string (funcall (pop xlist) string))))
1462   string)
1463
1464 ;; Subject simplification.
1465
1466 (defun gnus-simplify-whitespace (str)
1467   "Remove excessive whitespace from STR."
1468   ;; Multiple spaces.
1469   (while (string-match "[ \t][ \t]+" str)
1470     (setq str (concat (substring str 0 (match-beginning 0))
1471                         " "
1472                         (substring str (match-end 0)))))
1473   ;; Leading spaces.
1474   (when (string-match "^[ \t]+" str)
1475     (setq str (substring str (match-end 0))))
1476   ;; Trailing spaces.
1477   (when (string-match "[ \t]+$" str)
1478     (setq str (substring str 0 (match-beginning 0))))
1479   str)
1480
1481 (defun gnus-simplify-all-whitespace (str)
1482   "Remove all whitespace from STR."
1483   (while (string-match "[ \t\n]+" str)
1484     (setq str (replace-match "" nil nil str)))
1485   str)
1486
1487 (defsubst gnus-simplify-subject-re (subject)
1488   "Remove \"Re:\" from subject lines."
1489   (if (string-match message-subject-re-regexp subject)
1490       (substring subject (match-end 0))
1491     subject))
1492
1493 (defun gnus-simplify-subject (subject &optional re-only)
1494   "Remove `Re:' and words in parentheses.
1495 If RE-ONLY is non-nil, strip leading `Re:'s only."
1496   (let ((case-fold-search t))           ;Ignore case.
1497     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1498     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1499       (setq subject (substring subject (match-end 0))))
1500     ;; Remove uninteresting prefixes.
1501     (when (and (not re-only)
1502                gnus-simplify-ignored-prefixes
1503                (string-match gnus-simplify-ignored-prefixes subject))
1504       (setq subject (substring subject (match-end 0))))
1505     ;; Remove words in parentheses from end.
1506     (unless re-only
1507       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1508         (setq subject (substring subject 0 (match-beginning 0)))))
1509     ;; Return subject string.
1510     subject))
1511
1512 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1513 ;; all whitespace.
1514 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1515   (goto-char (point-min))
1516   (while (re-search-forward regexp nil t)
1517     (replace-match (or newtext ""))))
1518
1519 (defun gnus-simplify-buffer-fuzzy ()
1520   "Simplify string in the buffer fuzzily.
1521 The string in the accessible portion of the current buffer is simplified.
1522 It is assumed to be a single-line subject.
1523 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1524 matter is removed.  Additional things can be deleted by setting
1525 `gnus-simplify-subject-fuzzy-regexp'."
1526   (let ((case-fold-search t)
1527         (modified-tick))
1528     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1529
1530     (while (not (eq modified-tick (buffer-modified-tick)))
1531       (setq modified-tick (buffer-modified-tick))
1532       (cond
1533        ((listp gnus-simplify-subject-fuzzy-regexp)
1534         (mapcar 'gnus-simplify-buffer-fuzzy-step
1535                 gnus-simplify-subject-fuzzy-regexp))
1536        (gnus-simplify-subject-fuzzy-regexp
1537         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1538       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1539       (gnus-simplify-buffer-fuzzy-step
1540        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1541       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1542
1543     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1544     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1545     (gnus-simplify-buffer-fuzzy-step " $")
1546     (gnus-simplify-buffer-fuzzy-step "^ +")))
1547
1548 (defun gnus-simplify-subject-fuzzy (subject)
1549   "Simplify a subject string fuzzily.
1550 See `gnus-simplify-buffer-fuzzy' for details."
1551   (save-excursion
1552     (gnus-set-work-buffer)
1553     (let ((case-fold-search t))
1554       ;; Remove uninteresting prefixes.
1555       (when (and gnus-simplify-ignored-prefixes
1556                  (string-match gnus-simplify-ignored-prefixes subject))
1557         (setq subject (substring subject (match-end 0))))
1558       (insert subject)
1559       (inline (gnus-simplify-buffer-fuzzy))
1560       (buffer-string))))
1561
1562 (defsubst gnus-simplify-subject-fully (subject)
1563   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1564   (cond
1565    (gnus-simplify-subject-functions
1566     (gnus-map-function gnus-simplify-subject-functions subject))
1567    ((null gnus-summary-gather-subject-limit)
1568     (gnus-simplify-subject-re subject))
1569    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1570     (gnus-simplify-subject-fuzzy subject))
1571    ((numberp gnus-summary-gather-subject-limit)
1572     (gnus-limit-string (gnus-simplify-subject-re subject)
1573                        gnus-summary-gather-subject-limit))
1574    (t
1575     subject)))
1576
1577 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1578   "Check whether two subjects are equal.
1579 If optional argument SIMPLE-FIRST is t, first argument is already
1580 simplified."
1581   (cond
1582    ((null simple-first)
1583     (equal (gnus-simplify-subject-fully s1)
1584            (gnus-simplify-subject-fully s2)))
1585    (t
1586     (equal s1
1587            (gnus-simplify-subject-fully s2)))))
1588
1589 (defun gnus-summary-bubble-group ()
1590   "Increase the score of the current group.
1591 This is a handy function to add to `gnus-summary-exit-hook' to
1592 increase the score of each group you read."
1593   (gnus-group-add-score gnus-newsgroup-name))
1594
1595 \f
1596 ;;;
1597 ;;; Gnus summary mode
1598 ;;;
1599
1600 (put 'gnus-summary-mode 'mode-class 'special)
1601
1602 (defvar gnus-article-commands-menu)
1603
1604 ;; Non-orthogonal keys
1605
1606 (gnus-define-keys gnus-summary-mode-map
1607   " " gnus-summary-next-page
1608   "\177" gnus-summary-prev-page
1609   [delete] gnus-summary-prev-page
1610   [backspace] gnus-summary-prev-page
1611   "\r" gnus-summary-scroll-up
1612   "\M-\r" gnus-summary-scroll-down
1613   "n" gnus-summary-next-unread-article
1614   "p" gnus-summary-prev-unread-article
1615   "N" gnus-summary-next-article
1616   "P" gnus-summary-prev-article
1617   "\M-\C-n" gnus-summary-next-same-subject
1618   "\M-\C-p" gnus-summary-prev-same-subject
1619   "\M-n" gnus-summary-next-unread-subject
1620   "\M-p" gnus-summary-prev-unread-subject
1621   "." gnus-summary-first-unread-article
1622   "," gnus-summary-best-unread-article
1623   "\M-s" gnus-summary-search-article-forward
1624   "\M-r" gnus-summary-search-article-backward
1625   "<" gnus-summary-beginning-of-article
1626   ">" gnus-summary-end-of-article
1627   "j" gnus-summary-goto-article
1628   "^" gnus-summary-refer-parent-article
1629   "\M-^" gnus-summary-refer-article
1630   "u" gnus-summary-tick-article-forward
1631   "!" gnus-summary-tick-article-forward
1632   "U" gnus-summary-tick-article-backward
1633   "d" gnus-summary-mark-as-read-forward
1634   "D" gnus-summary-mark-as-read-backward
1635   "E" gnus-summary-mark-as-expirable
1636   "\M-u" gnus-summary-clear-mark-forward
1637   "\M-U" gnus-summary-clear-mark-backward
1638   "k" gnus-summary-kill-same-subject-and-select
1639   "\C-k" gnus-summary-kill-same-subject
1640   "\M-\C-k" gnus-summary-kill-thread
1641   "\M-\C-l" gnus-summary-lower-thread
1642   "e" gnus-summary-edit-article
1643   "#" gnus-summary-mark-as-processable
1644   "\M-#" gnus-summary-unmark-as-processable
1645   "\M-\C-t" gnus-summary-toggle-threads
1646   "\M-\C-s" gnus-summary-show-thread
1647   "\M-\C-h" gnus-summary-hide-thread
1648   "\M-\C-f" gnus-summary-next-thread
1649   "\M-\C-b" gnus-summary-prev-thread
1650   [(meta down)] gnus-summary-next-thread
1651   [(meta up)] gnus-summary-prev-thread
1652   "\M-\C-u" gnus-summary-up-thread
1653   "\M-\C-d" gnus-summary-down-thread
1654   "&" gnus-summary-execute-command
1655   "c" gnus-summary-catchup-and-exit
1656   "\C-w" gnus-summary-mark-region-as-read
1657   "\C-t" gnus-summary-toggle-truncation
1658   "?" gnus-summary-mark-as-dormant
1659   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1660   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1661   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1662   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1663   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1664   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1665   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1666   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1667   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1668   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1669   "=" gnus-summary-expand-window
1670   "\C-x\C-s" gnus-summary-reselect-current-group
1671   "\M-g" gnus-summary-rescan-group
1672   "w" gnus-summary-stop-page-breaking
1673   "\C-c\C-r" gnus-summary-caesar-message
1674   "f" gnus-summary-followup
1675   "F" gnus-summary-followup-with-original
1676   "C" gnus-summary-cancel-article
1677   "r" gnus-summary-reply
1678   "R" gnus-summary-reply-with-original
1679   "\C-c\C-f" gnus-summary-mail-forward
1680   "o" gnus-summary-save-article
1681   "\C-o" gnus-summary-save-article-mail
1682   "|" gnus-summary-pipe-output
1683   "\M-k" gnus-summary-edit-local-kill
1684   "\M-K" gnus-summary-edit-global-kill
1685   ;; "V" gnus-version
1686   "\C-c\C-d" gnus-summary-describe-group
1687   "q" gnus-summary-exit
1688   "Q" gnus-summary-exit-no-update
1689   "\C-c\C-i" gnus-info-find-node
1690   gnus-mouse-2 gnus-mouse-pick-article
1691   "m" gnus-summary-mail-other-window
1692   "a" gnus-summary-post-news
1693   "i" gnus-summary-news-other-window
1694   "x" gnus-summary-limit-to-unread
1695   "s" gnus-summary-isearch-article
1696   "t" gnus-summary-toggle-header
1697   "g" gnus-summary-show-article
1698   "l" gnus-summary-goto-last-article
1699   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1700   "\C-d" gnus-summary-enter-digest-group
1701   "\M-\C-d" gnus-summary-read-document
1702   "\M-\C-e" gnus-summary-edit-parameters
1703   "\M-\C-a" gnus-summary-customize-parameters
1704   "\C-c\C-b" gnus-bug
1705   "*" gnus-cache-enter-article
1706   "\M-*" gnus-cache-remove-article
1707   "\M-&" gnus-summary-universal-argument
1708   "\C-l" gnus-recenter
1709   "I" gnus-summary-increase-score
1710   "L" gnus-summary-lower-score
1711   "\M-i" gnus-symbolic-argument
1712   "h" gnus-summary-select-article-buffer
1713
1714   "b" gnus-article-view-part
1715   "\M-t" gnus-summary-toggle-display-buttonized
1716
1717   "V" gnus-summary-score-map
1718   "X" gnus-uu-extract-map
1719   "S" gnus-summary-send-map)
1720
1721 ;; Sort of orthogonal keymap
1722 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1723   "t" gnus-summary-tick-article-forward
1724   "!" gnus-summary-tick-article-forward
1725   "d" gnus-summary-mark-as-read-forward
1726   "r" gnus-summary-mark-as-read-forward
1727   "c" gnus-summary-clear-mark-forward
1728   " " gnus-summary-clear-mark-forward
1729   "e" gnus-summary-mark-as-expirable
1730   "x" gnus-summary-mark-as-expirable
1731   "?" gnus-summary-mark-as-dormant
1732   "b" gnus-summary-set-bookmark
1733   "B" gnus-summary-remove-bookmark
1734   "#" gnus-summary-mark-as-processable
1735   "\M-#" gnus-summary-unmark-as-processable
1736   "S" gnus-summary-limit-include-expunged
1737   "C" gnus-summary-catchup
1738   "H" gnus-summary-catchup-to-here
1739   "h" gnus-summary-catchup-from-here
1740   "\C-c" gnus-summary-catchup-all
1741   "k" gnus-summary-kill-same-subject-and-select
1742   "K" gnus-summary-kill-same-subject
1743   "P" gnus-uu-mark-map)
1744
1745 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1746   "c" gnus-summary-clear-above
1747   "u" gnus-summary-tick-above
1748   "m" gnus-summary-mark-above
1749   "k" gnus-summary-kill-below)
1750
1751 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1752   "/" gnus-summary-limit-to-subject
1753   "n" gnus-summary-limit-to-articles
1754   "w" gnus-summary-pop-limit
1755   "s" gnus-summary-limit-to-subject
1756   "a" gnus-summary-limit-to-author
1757   "u" gnus-summary-limit-to-unread
1758   "m" gnus-summary-limit-to-marks
1759   "M" gnus-summary-limit-exclude-marks
1760   "v" gnus-summary-limit-to-score
1761   "*" gnus-summary-limit-include-cached
1762   "D" gnus-summary-limit-include-dormant
1763   "T" gnus-summary-limit-include-thread
1764   "d" gnus-summary-limit-exclude-dormant
1765   "t" gnus-summary-limit-to-age
1766   "." gnus-summary-limit-to-unseen
1767   "x" gnus-summary-limit-to-extra
1768   "p" gnus-summary-limit-to-display-predicate
1769   "E" gnus-summary-limit-include-expunged
1770   "c" gnus-summary-limit-exclude-childless-dormant
1771   "C" gnus-summary-limit-mark-excluded-as-read
1772   "o" gnus-summary-insert-old-articles
1773   "N" gnus-summary-insert-new-articles
1774   "r" gnus-summary-limit-to-replied)
1775
1776 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1777   "n" gnus-summary-next-unread-article
1778   "p" gnus-summary-prev-unread-article
1779   "N" gnus-summary-next-article
1780   "P" gnus-summary-prev-article
1781   "\C-n" gnus-summary-next-same-subject
1782   "\C-p" gnus-summary-prev-same-subject
1783   "\M-n" gnus-summary-next-unread-subject
1784   "\M-p" gnus-summary-prev-unread-subject
1785   "f" gnus-summary-first-unread-article
1786   "b" gnus-summary-best-unread-article
1787   "j" gnus-summary-goto-article
1788   "g" gnus-summary-goto-subject
1789   "l" gnus-summary-goto-last-article
1790   "o" gnus-summary-pop-article)
1791
1792 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1793   "k" gnus-summary-kill-thread
1794   "l" gnus-summary-lower-thread
1795   "i" gnus-summary-raise-thread
1796   "T" gnus-summary-toggle-threads
1797   "t" gnus-summary-rethread-current
1798   "^" gnus-summary-reparent-thread
1799   "s" gnus-summary-show-thread
1800   "S" gnus-summary-show-all-threads
1801   "h" gnus-summary-hide-thread
1802   "H" gnus-summary-hide-all-threads
1803   "n" gnus-summary-next-thread
1804   "p" gnus-summary-prev-thread
1805   "u" gnus-summary-up-thread
1806   "o" gnus-summary-top-thread
1807   "d" gnus-summary-down-thread
1808   "#" gnus-uu-mark-thread
1809   "\M-#" gnus-uu-unmark-thread)
1810
1811 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1812   "g" gnus-summary-prepare
1813   "c" gnus-summary-insert-cached-articles
1814   "d" gnus-summary-insert-dormant-articles)
1815
1816 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1817   "c" gnus-summary-catchup-and-exit
1818   "C" gnus-summary-catchup-all-and-exit
1819   "E" gnus-summary-exit-no-update
1820   "Q" gnus-summary-exit
1821   "Z" gnus-summary-exit
1822   "n" gnus-summary-catchup-and-goto-next-group
1823   "R" gnus-summary-reselect-current-group
1824   "G" gnus-summary-rescan-group
1825   "N" gnus-summary-next-group
1826   "s" gnus-summary-save-newsrc
1827   "P" gnus-summary-prev-group)
1828
1829 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1830   " " gnus-summary-next-page
1831   "n" gnus-summary-next-page
1832   "\177" gnus-summary-prev-page
1833   [delete] gnus-summary-prev-page
1834   "p" gnus-summary-prev-page
1835   "\r" gnus-summary-scroll-up
1836   "\M-\r" gnus-summary-scroll-down
1837   "<" gnus-summary-beginning-of-article
1838   ">" gnus-summary-end-of-article
1839   "b" gnus-summary-beginning-of-article
1840   "e" gnus-summary-end-of-article
1841   "^" gnus-summary-refer-parent-article
1842   "r" gnus-summary-refer-parent-article
1843   "D" gnus-summary-enter-digest-group
1844   "R" gnus-summary-refer-references
1845   "T" gnus-summary-refer-thread
1846   "g" gnus-summary-show-article
1847   "s" gnus-summary-isearch-article
1848   "P" gnus-summary-print-article
1849   "M" gnus-mailing-list-insinuate
1850   "t" gnus-article-babel)
1851
1852 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1853   "b" gnus-article-add-buttons
1854   "B" gnus-article-add-buttons-to-head
1855   "o" gnus-article-treat-overstrike
1856   "e" gnus-article-emphasize
1857   "w" gnus-article-fill-cited-article
1858   "Q" gnus-article-fill-long-lines
1859   "C" gnus-article-capitalize-sentences
1860   "c" gnus-article-remove-cr
1861   "q" gnus-article-de-quoted-unreadable
1862   "6" gnus-article-de-base64-unreadable
1863   "Z" gnus-article-decode-HZ
1864   "A" gnus-article-treat-ansi-sequences
1865   "h" gnus-article-wash-html
1866   "u" gnus-article-unsplit-urls
1867   "s" gnus-summary-force-verify-and-decrypt
1868   "f" gnus-article-display-x-face
1869   "l" gnus-summary-stop-page-breaking
1870   "r" gnus-summary-caesar-message
1871   "m" gnus-summary-morse-message
1872   "t" gnus-summary-toggle-header
1873   "g" gnus-treat-smiley
1874   "v" gnus-summary-verbose-headers
1875   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1876   "p" gnus-article-verify-x-pgp-sig
1877   "d" gnus-article-treat-dumbquotes)
1878
1879 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1880   ;; mnemonic: deuglif*Y*
1881   "u" gnus-article-outlook-unwrap-lines
1882   "a" gnus-article-outlook-repair-attribution
1883   "c" gnus-article-outlook-rearrange-citation
1884   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1885
1886 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1887   "a" gnus-article-hide
1888   "h" gnus-article-hide-headers
1889   "b" gnus-article-hide-boring-headers
1890   "s" gnus-article-hide-signature
1891   "c" gnus-article-hide-citation
1892   "C" gnus-article-hide-citation-in-followups
1893   "l" gnus-article-hide-list-identifiers
1894   "B" gnus-article-strip-banner
1895   "P" gnus-article-hide-pem
1896   "\C-c" gnus-article-hide-citation-maybe)
1897
1898 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1899   "a" gnus-article-highlight
1900   "h" gnus-article-highlight-headers
1901   "c" gnus-article-highlight-citation
1902   "s" gnus-article-highlight-signature)
1903
1904 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1905   "f" gnus-article-treat-fold-headers
1906   "u" gnus-article-treat-unfold-headers
1907   "n" gnus-article-treat-fold-newsgroups)
1908
1909 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1910   "x" gnus-article-display-x-face
1911   "d" gnus-article-display-face
1912   "s" gnus-treat-smiley
1913   "D" gnus-article-remove-images
1914   "f" gnus-treat-from-picon
1915   "m" gnus-treat-mail-picon
1916   "n" gnus-treat-newsgroups-picon)
1917
1918 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1919   "w" gnus-article-decode-mime-words
1920   "c" gnus-article-decode-charset
1921   "v" gnus-mime-view-all-parts
1922   "b" gnus-article-view-part)
1923
1924 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1925   "z" gnus-article-date-ut
1926   "u" gnus-article-date-ut
1927   "l" gnus-article-date-local
1928   "p" gnus-article-date-english
1929   "e" gnus-article-date-lapsed
1930   "o" gnus-article-date-original
1931   "i" gnus-article-date-iso8601
1932   "s" gnus-article-date-user)
1933
1934 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1935   "t" gnus-article-remove-trailing-blank-lines
1936   "l" gnus-article-strip-leading-blank-lines
1937   "m" gnus-article-strip-multiple-blank-lines
1938   "a" gnus-article-strip-blank-lines
1939   "A" gnus-article-strip-all-blank-lines
1940   "s" gnus-article-strip-leading-space
1941   "e" gnus-article-strip-trailing-space
1942   "w" gnus-article-remove-leading-whitespace)
1943
1944 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1945   "v" gnus-version
1946   "f" gnus-summary-fetch-faq
1947   "d" gnus-summary-describe-group
1948   "h" gnus-summary-describe-briefly
1949   "i" gnus-info-find-node
1950   "c" gnus-group-fetch-charter
1951   "C" gnus-group-fetch-control)
1952
1953 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1954   "e" gnus-summary-expire-articles
1955   "\M-\C-e" gnus-summary-expire-articles-now
1956   "\177" gnus-summary-delete-article
1957   [delete] gnus-summary-delete-article
1958   [backspace] gnus-summary-delete-article
1959   "m" gnus-summary-move-article
1960   "r" gnus-summary-respool-article
1961   "w" gnus-summary-edit-article
1962   "c" gnus-summary-copy-article
1963   "B" gnus-summary-crosspost-article
1964   "q" gnus-summary-respool-query
1965   "t" gnus-summary-respool-trace
1966   "i" gnus-summary-import-article
1967   "I" gnus-summary-create-article
1968   "p" gnus-summary-article-posted-p)
1969
1970 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1971   "o" gnus-summary-save-article
1972   "m" gnus-summary-save-article-mail
1973   "F" gnus-summary-write-article-file
1974   "r" gnus-summary-save-article-rmail
1975   "f" gnus-summary-save-article-file
1976   "b" gnus-summary-save-article-body-file
1977   "h" gnus-summary-save-article-folder
1978   "v" gnus-summary-save-article-vm
1979   "p" gnus-summary-pipe-output
1980   "P" gnus-summary-muttprint
1981   "s" gnus-soup-add-article)
1982
1983 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1984   "b" gnus-summary-display-buttonized
1985   "m" gnus-summary-repair-multipart
1986   "v" gnus-article-view-part
1987   "o" gnus-article-save-part
1988   "c" gnus-article-copy-part
1989   "C" gnus-article-view-part-as-charset
1990   "e" gnus-article-view-part-externally
1991   "E" gnus-article-encrypt-body
1992   "i" gnus-article-inline-part
1993   "|" gnus-article-pipe-part)
1994
1995 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1996   "p" gnus-summary-mark-as-processable
1997   "u" gnus-summary-unmark-as-processable
1998   "U" gnus-summary-unmark-all-processable
1999   "v" gnus-uu-mark-over
2000   "s" gnus-uu-mark-series
2001   "r" gnus-uu-mark-region
2002   "g" gnus-uu-unmark-region
2003   "R" gnus-uu-mark-by-regexp
2004   "G" gnus-uu-unmark-by-regexp
2005   "t" gnus-uu-mark-thread
2006   "T" gnus-uu-unmark-thread
2007   "a" gnus-uu-mark-all
2008   "b" gnus-uu-mark-buffer
2009   "S" gnus-uu-mark-sparse
2010   "k" gnus-summary-kill-process-mark
2011   "y" gnus-summary-yank-process-mark
2012   "w" gnus-summary-save-process-mark
2013   "i" gnus-uu-invert-processable)
2014
2015 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2016   ;;"x" gnus-uu-extract-any
2017   "m" gnus-summary-save-parts
2018   "u" gnus-uu-decode-uu
2019   "U" gnus-uu-decode-uu-and-save
2020   "s" gnus-uu-decode-unshar
2021   "S" gnus-uu-decode-unshar-and-save
2022   "o" gnus-uu-decode-save
2023   "O" gnus-uu-decode-save
2024   "b" gnus-uu-decode-binhex
2025   "B" gnus-uu-decode-binhex
2026   "p" gnus-uu-decode-postscript
2027   "P" gnus-uu-decode-postscript-and-save)
2028
2029 (gnus-define-keys
2030     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2031   "u" gnus-uu-decode-uu-view
2032   "U" gnus-uu-decode-uu-and-save-view
2033   "s" gnus-uu-decode-unshar-view
2034   "S" gnus-uu-decode-unshar-and-save-view
2035   "o" gnus-uu-decode-save-view
2036   "O" gnus-uu-decode-save-view
2037   "b" gnus-uu-decode-binhex-view
2038   "B" gnus-uu-decode-binhex-view
2039   "p" gnus-uu-decode-postscript-view
2040   "P" gnus-uu-decode-postscript-and-save-view)
2041
2042 (defvar gnus-article-post-menu nil)
2043
2044 (defconst gnus-summary-menu-maxlen 20)
2045
2046 (defun gnus-summary-menu-split (menu)
2047   ;; If we have lots of elements, divide them into groups of 20
2048   ;; and make a pane (or submenu) for each one.
2049   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2050       (let ((menu menu) sublists next
2051             (i 1))
2052         (while menu
2053           ;; Pull off the next gnus-summary-menu-maxlen elements
2054           ;; and make them the next element of sublist.
2055           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2056           (if next
2057               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2058                       nil))
2059           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2060                                              (aref (car (last menu)) 0)) menu)
2061                                sublists))
2062           (setq i (1+ i))
2063           (setq menu next))
2064         (nreverse sublists))
2065     ;; Few elements--put them all in one pane.
2066     menu))
2067
2068 (defun gnus-summary-make-menu-bar ()
2069   (gnus-turn-off-edit-menu 'summary)
2070
2071   (unless (boundp 'gnus-summary-misc-menu)
2072
2073     (easy-menu-define
2074       gnus-summary-kill-menu gnus-summary-mode-map ""
2075       (cons
2076        "Score"
2077        (nconc
2078         (list
2079          ["Customize" gnus-score-customize t])
2080         (gnus-make-score-map 'increase)
2081         (gnus-make-score-map 'lower)
2082         '(("Mark"
2083            ["Kill below" gnus-summary-kill-below t]
2084            ["Mark above" gnus-summary-mark-above t]
2085            ["Tick above" gnus-summary-tick-above t]
2086            ["Clear above" gnus-summary-clear-above t])
2087           ["Current score" gnus-summary-current-score t]
2088           ["Set score" gnus-summary-set-score t]
2089           ["Switch current score file..." gnus-score-change-score-file t]
2090           ["Set mark below..." gnus-score-set-mark-below t]
2091           ["Set expunge below..." gnus-score-set-expunge-below t]
2092           ["Edit current score file" gnus-score-edit-current-scores t]
2093           ["Edit score file" gnus-score-edit-file t]
2094           ["Trace score" gnus-score-find-trace t]
2095           ["Find words" gnus-score-find-favourite-words t]
2096           ["Rescore buffer" gnus-summary-rescore t]
2097           ["Increase score..." gnus-summary-increase-score t]
2098           ["Lower score..." gnus-summary-lower-score t]))))
2099
2100     ;; Define both the Article menu in the summary buffer and the
2101     ;; equivalent Commands menu in the article buffer here for
2102     ;; consistency.
2103     (let ((innards
2104            `(("Hide"
2105               ["All" gnus-article-hide t]
2106               ["Headers" gnus-article-hide-headers t]
2107               ["Signature" gnus-article-hide-signature t]
2108               ["Citation" gnus-article-hide-citation t]
2109               ["List identifiers" gnus-article-hide-list-identifiers t]
2110               ["Banner" gnus-article-strip-banner t]
2111               ["Boring headers" gnus-article-hide-boring-headers t])
2112              ("Highlight"
2113               ["All" gnus-article-highlight t]
2114               ["Headers" gnus-article-highlight-headers t]
2115               ["Signature" gnus-article-highlight-signature t]
2116               ["Citation" gnus-article-highlight-citation t])
2117              ("MIME"
2118               ["Words" gnus-article-decode-mime-words t]
2119               ["Charset" gnus-article-decode-charset t]
2120               ["QP" gnus-article-de-quoted-unreadable t]
2121               ["Base64" gnus-article-de-base64-unreadable t]
2122               ["View MIME buttons" gnus-summary-display-buttonized t]
2123               ["View all" gnus-mime-view-all-parts t]
2124               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2125               ["Encrypt body" gnus-article-encrypt-body
2126                :active (not (gnus-group-read-only-p))
2127                ,@(if (featurep 'xemacs) nil
2128                    '(:help "Encrypt the message body on disk"))]
2129               ["Extract all parts..." gnus-summary-save-parts t]
2130               ("Multipart"
2131                ["Repair multipart" gnus-summary-repair-multipart t]
2132                ["Pipe part..." gnus-article-pipe-part t]
2133                ["Inline part" gnus-article-inline-part t]
2134                ["Encrypt body" gnus-article-encrypt-body
2135                 :active (not (gnus-group-read-only-p))
2136                ,@(if (featurep 'xemacs) nil
2137                    '(:help "Encrypt the message body on disk"))]
2138                ["View part externally" gnus-article-view-part-externally t]
2139                ["View part with charset..." gnus-article-view-part-as-charset t]
2140                ["Copy part" gnus-article-copy-part t]
2141                ["Save part..." gnus-article-save-part t]
2142                ["View part" gnus-article-view-part t]))
2143              ("Date"
2144               ["Local" gnus-article-date-local t]
2145               ["ISO8601" gnus-article-date-iso8601 t]
2146               ["UT" gnus-article-date-ut t]
2147               ["Original" gnus-article-date-original t]
2148               ["Lapsed" gnus-article-date-lapsed t]
2149               ["User-defined" gnus-article-date-user t])
2150              ("Display"
2151               ["Remove images" gnus-article-remove-images t]
2152               ["Toggle smiley" gnus-treat-smiley t]
2153               ["Show X-Face" gnus-article-display-x-face t]
2154               ["Show picons in From" gnus-treat-from-picon t]
2155               ["Show picons in mail headers" gnus-treat-mail-picon t]
2156               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2157               ("View as different encoding"
2158                ,@(gnus-summary-menu-split
2159                   (mapcar
2160                    (lambda (cs)
2161                      ;; Since easymenu under Emacs doesn't allow
2162                      ;; lambda forms for menu commands, we should
2163                      ;; provide intern'ed function symbols.
2164                      (let ((command (intern (format "\
2165 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2166                        (fset command
2167                              `(lambda ()
2168                                 (interactive)
2169                                 (let ((gnus-summary-show-article-charset-alist
2170                                        '((1 . ,cs))))
2171                                   (gnus-summary-show-article 1))))
2172                        `[,(symbol-name cs) ,command t]))
2173                    (sort (if (fboundp 'coding-system-list)
2174                              (coding-system-list)
2175                            (mapcar 'car mm-mime-mule-charset-alist))
2176                          'string<)))))
2177              ("Washing"
2178               ("Remove Blanks"
2179                ["Leading" gnus-article-strip-leading-blank-lines t]
2180                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2181                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2182                ["All of the above" gnus-article-strip-blank-lines t]
2183                ["All" gnus-article-strip-all-blank-lines t]
2184                ["Leading space" gnus-article-strip-leading-space t]
2185                ["Trailing space" gnus-article-strip-trailing-space t]
2186                ["Leading space in headers"
2187                 gnus-article-remove-leading-whitespace t])
2188               ["Overstrike" gnus-article-treat-overstrike t]
2189               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2190               ["Emphasis" gnus-article-emphasize t]
2191               ["Word wrap" gnus-article-fill-cited-article t]
2192               ["Fill long lines" gnus-article-fill-long-lines t]
2193               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2194               ["Remove CR" gnus-article-remove-cr t]
2195               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2196               ["Base64" gnus-article-de-base64-unreadable t]
2197               ["Rot 13" gnus-summary-caesar-message
2198                ,@(if (featurep 'xemacs) '(t)
2199                    '(:help "\"Caesar rotate\" article by 13"))]
2200               ["Morse decode" gnus-summary-morse-message t]
2201               ["Unix pipe..." gnus-summary-pipe-message t]
2202               ["Add buttons" gnus-article-add-buttons t]
2203               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2204               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2205               ["Verbose header" gnus-summary-verbose-headers t]
2206               ["Toggle header" gnus-summary-toggle-header t]
2207               ["Unfold headers" gnus-article-treat-unfold-headers t]
2208               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2209               ["Html" gnus-article-wash-html t]
2210               ["Unsplit URLs" gnus-article-unsplit-urls t]
2211               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2212               ["Decode HZ" gnus-article-decode-HZ t]
2213               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2214               ("(Outlook) Deuglify"
2215                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2216                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2217                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2218                ["Full (Outlook) deuglify"
2219                 gnus-article-outlook-deuglify-article t])
2220               )
2221              ("Output"
2222               ["Save in default format..." gnus-summary-save-article
2223                ,@(if (featurep 'xemacs) '(t)
2224                    '(:help "Save article using default method"))]
2225               ["Save in file..." gnus-summary-save-article-file
2226                ,@(if (featurep 'xemacs) '(t)
2227                    '(:help "Save article in file"))]
2228               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2229               ["Save in MH folder..." gnus-summary-save-article-folder t]
2230               ["Save in VM folder..." gnus-summary-save-article-vm t]
2231               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2232               ["Save body in file..." gnus-summary-save-article-body-file t]
2233               ["Pipe through a filter..." gnus-summary-pipe-output t]
2234               ["Add to SOUP packet" gnus-soup-add-article t]
2235               ["Print with Muttprint..." gnus-summary-muttprint t]
2236               ["Print" gnus-summary-print-article t])
2237              ("Backend"
2238               ["Respool article..." gnus-summary-respool-article t]
2239               ["Move article..." gnus-summary-move-article
2240                (gnus-check-backend-function
2241                 'request-move-article gnus-newsgroup-name)]
2242               ["Copy article..." gnus-summary-copy-article t]
2243               ["Crosspost article..." gnus-summary-crosspost-article
2244                (gnus-check-backend-function
2245                 'request-replace-article gnus-newsgroup-name)]
2246               ["Import file..." gnus-summary-import-article
2247                (gnus-check-backend-function
2248                 'request-accept-article gnus-newsgroup-name)]
2249               ["Create article..." gnus-summary-create-article
2250                (gnus-check-backend-function
2251                 'request-accept-article gnus-newsgroup-name)]
2252               ["Check if posted" gnus-summary-article-posted-p t]
2253               ["Edit article" gnus-summary-edit-article
2254                (not (gnus-group-read-only-p))]
2255               ["Delete article" gnus-summary-delete-article
2256                (gnus-check-backend-function
2257                 'request-expire-articles gnus-newsgroup-name)]
2258               ["Query respool" gnus-summary-respool-query t]
2259               ["Trace respool" gnus-summary-respool-trace t]
2260               ["Delete expirable articles" gnus-summary-expire-articles-now
2261                (gnus-check-backend-function
2262                 'request-expire-articles gnus-newsgroup-name)])
2263              ("Extract"
2264               ["Uudecode" gnus-uu-decode-uu
2265                ,@(if (featurep 'xemacs) '(t)
2266                    '(:help "Decode uuencoded article(s)"))]
2267               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2268               ["Unshar" gnus-uu-decode-unshar t]
2269               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2270               ["Save" gnus-uu-decode-save t]
2271               ["Binhex" gnus-uu-decode-binhex t]
2272               ["Postscript" gnus-uu-decode-postscript t]
2273               ["All MIME parts" gnus-summary-save-parts t])
2274              ("Cache"
2275               ["Enter article" gnus-cache-enter-article t]
2276               ["Remove article" gnus-cache-remove-article t])
2277              ["Translate" gnus-article-babel t]
2278              ["Select article buffer" gnus-summary-select-article-buffer t]
2279              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2280              ["Isearch article..." gnus-summary-isearch-article t]
2281              ["Beginning of the article" gnus-summary-beginning-of-article t]
2282              ["End of the article" gnus-summary-end-of-article t]
2283              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2284              ["Fetch referenced articles" gnus-summary-refer-references t]
2285              ["Fetch current thread" gnus-summary-refer-thread t]
2286              ["Fetch article with id..." gnus-summary-refer-article t]
2287              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2288              ["Redisplay" gnus-summary-show-article t]
2289              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2290       (easy-menu-define
2291         gnus-summary-article-menu gnus-summary-mode-map ""
2292         (cons "Article" innards))
2293
2294       (if (not (keymapp gnus-summary-article-menu))
2295           (easy-menu-define
2296             gnus-article-commands-menu gnus-article-mode-map ""
2297             (cons "Commands" innards))
2298         ;; in Emacs, don't share menu.
2299         (setq gnus-article-commands-menu
2300               (copy-keymap gnus-summary-article-menu))
2301         (define-key gnus-article-mode-map [menu-bar commands]
2302           (cons "Commands" gnus-article-commands-menu))))
2303
2304     (easy-menu-define
2305       gnus-summary-thread-menu gnus-summary-mode-map ""
2306       '("Threads"
2307         ["Find all messages in thread" gnus-summary-refer-thread t]
2308         ["Toggle threading" gnus-summary-toggle-threads t]
2309         ["Hide threads" gnus-summary-hide-all-threads t]
2310         ["Show threads" gnus-summary-show-all-threads t]
2311         ["Hide thread" gnus-summary-hide-thread t]
2312         ["Show thread" gnus-summary-show-thread t]
2313         ["Go to next thread" gnus-summary-next-thread t]
2314         ["Go to previous thread" gnus-summary-prev-thread t]
2315         ["Go down thread" gnus-summary-down-thread t]
2316         ["Go up thread" gnus-summary-up-thread t]
2317         ["Top of thread" gnus-summary-top-thread t]
2318         ["Mark thread as read" gnus-summary-kill-thread t]
2319         ["Lower thread score" gnus-summary-lower-thread t]
2320         ["Raise thread score" gnus-summary-raise-thread t]
2321         ["Rethread current" gnus-summary-rethread-current t]))
2322
2323     (easy-menu-define
2324       gnus-summary-post-menu gnus-summary-mode-map ""
2325       `("Post"
2326         ["Send a message (mail or news)" gnus-summary-post-news
2327          ,@(if (featurep 'xemacs) '(t)
2328              '(:help "Post an article"))]
2329         ["Followup" gnus-summary-followup
2330          ,@(if (featurep 'xemacs) '(t)
2331              '(:help "Post followup to this article"))]
2332         ["Followup and yank" gnus-summary-followup-with-original
2333          ,@(if (featurep 'xemacs) '(t)
2334              '(:help "Post followup to this article, quoting its contents"))]
2335         ["Supersede article" gnus-summary-supersede-article t]
2336         ["Cancel article" gnus-summary-cancel-article
2337          ,@(if (featurep 'xemacs) '(t)
2338              '(:help "Cancel an article you posted"))]
2339         ["Reply" gnus-summary-reply t]
2340         ["Reply and yank" gnus-summary-reply-with-original t]
2341         ["Wide reply" gnus-summary-wide-reply t]
2342         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2343          ,@(if (featurep 'xemacs) '(t)
2344              '(:help "Mail a reply, quoting this article"))]
2345         ["Very wide reply" gnus-summary-very-wide-reply t]
2346         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2347          ,@(if (featurep 'xemacs) '(t)
2348              '(:help "Mail a very wide reply, quoting this article"))]
2349         ["Mail forward" gnus-summary-mail-forward t]
2350         ["Post forward" gnus-summary-post-forward t]
2351         ["Digest and mail" gnus-uu-digest-mail-forward t]
2352         ["Digest and post" gnus-uu-digest-post-forward t]
2353         ["Resend message" gnus-summary-resend-message t]
2354         ["Resend message edit" gnus-summary-resend-message-edit t]
2355         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2356         ["Send a mail" gnus-summary-mail-other-window t]
2357         ["Create a local message" gnus-summary-news-other-window t]
2358         ["Uuencode and post" gnus-uu-post-news
2359          ,@(if (featurep 'xemacs) '(t)
2360              '(:help "Post a uuencoded article"))]
2361         ["Followup via news" gnus-summary-followup-to-mail t]
2362         ["Followup via news and yank"
2363          gnus-summary-followup-to-mail-with-original t]
2364         ;;("Draft"
2365         ;;["Send" gnus-summary-send-draft t]
2366         ;;["Send bounced" gnus-resend-bounced-mail t])
2367         ))
2368
2369     (cond
2370      ((not (keymapp gnus-summary-post-menu))
2371       (setq gnus-article-post-menu gnus-summary-post-menu))
2372      ((not gnus-article-post-menu)
2373       ;; Don't share post menu.
2374       (setq gnus-article-post-menu
2375             (copy-keymap gnus-summary-post-menu))))
2376     (define-key gnus-article-mode-map [menu-bar post]
2377       (cons "Post" gnus-article-post-menu))
2378
2379     (easy-menu-define
2380       gnus-summary-misc-menu gnus-summary-mode-map ""
2381       `("Gnus"
2382         ("Mark Read"
2383          ["Mark as read" gnus-summary-mark-as-read-forward t]
2384          ["Mark same subject and select"
2385           gnus-summary-kill-same-subject-and-select t]
2386          ["Mark same subject" gnus-summary-kill-same-subject t]
2387          ["Catchup" gnus-summary-catchup
2388           ,@(if (featurep 'xemacs) '(t)
2389               '(:help "Mark unread articles in this group as read"))]
2390          ["Catchup all" gnus-summary-catchup-all t]
2391          ["Catchup to here" gnus-summary-catchup-to-here t]
2392          ["Catchup from here" gnus-summary-catchup-from-here t]
2393          ["Catchup region" gnus-summary-mark-region-as-read
2394           (gnus-mark-active-p)]
2395          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2396         ("Mark Various"
2397          ["Tick" gnus-summary-tick-article-forward t]
2398          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2399          ["Remove marks" gnus-summary-clear-mark-forward t]
2400          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2401          ["Set bookmark" gnus-summary-set-bookmark t]
2402          ["Remove bookmark" gnus-summary-remove-bookmark t])
2403         ("Limit to"
2404          ["Marks..." gnus-summary-limit-to-marks t]
2405          ["Subject..." gnus-summary-limit-to-subject t]
2406          ["Author..." gnus-summary-limit-to-author t]
2407          ["Age..." gnus-summary-limit-to-age t]
2408          ["Extra..." gnus-summary-limit-to-extra t]
2409          ["Score..." gnus-summary-limit-to-score t]
2410          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2411          ["Unread" gnus-summary-limit-to-unread t]
2412          ["Unseen" gnus-summary-limit-to-unseen t]
2413          ["Replied" gnus-summary-limit-to-replied t]
2414          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2415          ["Next articles" gnus-summary-limit-to-articles t]
2416          ["Pop limit" gnus-summary-pop-limit t]
2417          ["Show dormant" gnus-summary-limit-include-dormant t]
2418          ["Hide childless dormant"
2419           gnus-summary-limit-exclude-childless-dormant t]
2420          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2421          ["Hide marked" gnus-summary-limit-exclude-marks t]
2422          ["Show expunged" gnus-summary-limit-include-expunged t])
2423         ("Process Mark"
2424          ["Set mark" gnus-summary-mark-as-processable t]
2425          ["Remove mark" gnus-summary-unmark-as-processable t]
2426          ["Remove all marks" gnus-summary-unmark-all-processable t]
2427          ["Mark above" gnus-uu-mark-over t]
2428          ["Mark series" gnus-uu-mark-series t]
2429          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2430          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2431          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2432          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2433          ["Mark all" gnus-uu-mark-all t]
2434          ["Mark buffer" gnus-uu-mark-buffer t]
2435          ["Mark sparse" gnus-uu-mark-sparse t]
2436          ["Mark thread" gnus-uu-mark-thread t]
2437          ["Unmark thread" gnus-uu-unmark-thread t]
2438          ("Process Mark Sets"
2439           ["Kill" gnus-summary-kill-process-mark t]
2440           ["Yank" gnus-summary-yank-process-mark
2441            gnus-newsgroup-process-stack]
2442           ["Save" gnus-summary-save-process-mark t]
2443           ["Run command on marked..." gnus-summary-universal-argument t]))
2444         ("Scroll article"
2445          ["Page forward" gnus-summary-next-page
2446           ,@(if (featurep 'xemacs) '(t)
2447               '(:help "Show next page of article"))]
2448          ["Page backward" gnus-summary-prev-page
2449           ,@(if (featurep 'xemacs) '(t)
2450               '(:help "Show previous page of article"))]
2451          ["Line forward" gnus-summary-scroll-up t])
2452         ("Move"
2453          ["Next unread article" gnus-summary-next-unread-article t]
2454          ["Previous unread article" gnus-summary-prev-unread-article t]
2455          ["Next article" gnus-summary-next-article t]
2456          ["Previous article" gnus-summary-prev-article t]
2457          ["Next unread subject" gnus-summary-next-unread-subject t]
2458          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2459          ["Next article same subject" gnus-summary-next-same-subject t]
2460          ["Previous article same subject" gnus-summary-prev-same-subject t]
2461          ["First unread article" gnus-summary-first-unread-article t]
2462          ["Best unread article" gnus-summary-best-unread-article t]
2463          ["Go to subject number..." gnus-summary-goto-subject t]
2464          ["Go to article number..." gnus-summary-goto-article t]
2465          ["Go to the last article" gnus-summary-goto-last-article t]
2466          ["Pop article off history" gnus-summary-pop-article t])
2467         ("Sort"
2468          ["Sort by number" gnus-summary-sort-by-number t]
2469          ["Sort by author" gnus-summary-sort-by-author t]
2470          ["Sort by subject" gnus-summary-sort-by-subject t]
2471          ["Sort by date" gnus-summary-sort-by-date t]
2472          ["Sort by score" gnus-summary-sort-by-score t]
2473          ["Sort by lines" gnus-summary-sort-by-lines t]
2474          ["Sort by characters" gnus-summary-sort-by-chars t]
2475          ["Randomize" gnus-summary-sort-by-random t]
2476          ["Original sort" gnus-summary-sort-by-original t])
2477         ("Help"
2478          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2479          ["Describe group" gnus-summary-describe-group t]
2480          ["Fetch charter" gnus-group-fetch-charter
2481           ,@(if (featurep 'xemacs) nil
2482               '(:help "Display the charter of the current group"))]
2483          ["Fetch control message" gnus-group-fetch-control
2484           ,@(if (featurep 'xemacs) nil
2485               '(:help "Display the archived control message for the current group"))]
2486          ["Read manual" gnus-info-find-node t])
2487         ("Modes"
2488          ["Pick and read" gnus-pick-mode t]
2489          ["Binary" gnus-binary-mode t])
2490         ("Regeneration"
2491          ["Regenerate" gnus-summary-prepare t]
2492          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2493          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2494          ["Toggle threading" gnus-summary-toggle-threads t])
2495         ["See old articles" gnus-summary-insert-old-articles t]
2496         ["See new articles" gnus-summary-insert-new-articles t]
2497         ["Filter articles..." gnus-summary-execute-command t]
2498         ["Run command on articles..." gnus-summary-universal-argument t]
2499         ["Search articles forward..." gnus-summary-search-article-forward t]
2500         ["Search articles backward..." gnus-summary-search-article-backward t]
2501         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2502         ["Expand window" gnus-summary-expand-window t]
2503         ["Expire expirable articles" gnus-summary-expire-articles
2504          (gnus-check-backend-function
2505           'request-expire-articles gnus-newsgroup-name)]
2506         ["Edit local kill file" gnus-summary-edit-local-kill t]
2507         ["Edit main kill file" gnus-summary-edit-global-kill t]
2508         ["Edit group parameters" gnus-summary-edit-parameters t]
2509         ["Customize group parameters" gnus-summary-customize-parameters t]
2510         ["Send a bug report" gnus-bug t]
2511         ("Exit"
2512          ["Catchup and exit" gnus-summary-catchup-and-exit
2513           ,@(if (featurep 'xemacs) '(t)
2514               '(:help "Mark unread articles in this group as read, then exit"))]
2515          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2516          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2517          ["Exit group" gnus-summary-exit
2518           ,@(if (featurep 'xemacs) '(t)
2519               '(:help "Exit current group, return to group selection mode"))]
2520          ["Exit group without updating" gnus-summary-exit-no-update t]
2521          ["Exit and goto next group" gnus-summary-next-group t]
2522          ["Exit and goto prev group" gnus-summary-prev-group t]
2523          ["Reselect group" gnus-summary-reselect-current-group t]
2524          ["Rescan group" gnus-summary-rescan-group t]
2525          ["Update dribble" gnus-summary-save-newsrc t])))
2526
2527     (gnus-run-hooks 'gnus-summary-menu-hook)))
2528
2529 (defvar gnus-summary-tool-bar-map nil)
2530
2531 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2532 (defun gnus-summary-make-tool-bar ()
2533   (if (and (fboundp 'tool-bar-add-item-from-menu)
2534            (default-value 'tool-bar-mode)
2535            (not gnus-summary-tool-bar-map))
2536       (setq gnus-summary-tool-bar-map
2537             (let ((tool-bar-map (make-sparse-keymap))
2538                   (load-path (mm-image-load-path)))
2539               (tool-bar-add-item-from-menu
2540                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2541               (tool-bar-add-item-from-menu
2542                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2543               (tool-bar-add-item-from-menu
2544                'gnus-summary-post-news "post" gnus-summary-mode-map)
2545               (tool-bar-add-item-from-menu
2546                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2547               (tool-bar-add-item-from-menu
2548                'gnus-summary-followup "followup" gnus-summary-mode-map)
2549               (tool-bar-add-item-from-menu
2550                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2551               (tool-bar-add-item-from-menu
2552                'gnus-summary-reply "reply" gnus-summary-mode-map)
2553               (tool-bar-add-item-from-menu
2554                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2555               (tool-bar-add-item-from-menu
2556                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2557               (tool-bar-add-item-from-menu
2558                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2559               (tool-bar-add-item-from-menu
2560                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2561               (tool-bar-add-item-from-menu
2562                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2563               (tool-bar-add-item-from-menu
2564                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2565               (tool-bar-add-item-from-menu
2566                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2567               (tool-bar-add-item-from-menu
2568                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2569               tool-bar-map)))
2570   (if gnus-summary-tool-bar-map
2571       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2572
2573 (defun gnus-score-set-default (var value)
2574   "A version of set that updates the GNU Emacs menu-bar."
2575   (set var value)
2576   ;; It is the message that forces the active status to be updated.
2577   (message ""))
2578
2579 (defun gnus-make-score-map (type)
2580   "Make a summary score map of type TYPE."
2581   (if t
2582       nil
2583     (let ((headers '(("author" "from" string)
2584                      ("subject" "subject" string)
2585                      ("article body" "body" string)
2586                      ("article head" "head" string)
2587                      ("xref" "xref" string)
2588                      ("extra header" "extra" string)
2589                      ("lines" "lines" number)
2590                      ("followups to author" "followup" string)))
2591           (types '((number ("less than" <)
2592                            ("greater than" >)
2593                            ("equal" =))
2594                    (string ("substring" s)
2595                            ("exact string" e)
2596                            ("fuzzy string" f)
2597                            ("regexp" r))))
2598           (perms '(("temporary" (current-time-string))
2599                    ("permanent" nil)
2600                    ("immediate" now)))
2601           header)
2602       (list
2603        (apply
2604         'nconc
2605         (list
2606          (if (eq type 'lower)
2607              "Lower score"
2608            "Increase score"))
2609         (let (outh)
2610           (while headers
2611             (setq header (car headers))
2612             (setq outh
2613                   (cons
2614                    (apply
2615                     'nconc
2616                     (list (car header))
2617                     (let ((ts (cdr (assoc (nth 2 header) types)))
2618                           outt)
2619                       (while ts
2620                         (setq outt
2621                               (cons
2622                                (apply
2623                                 'nconc
2624                                 (list (caar ts))
2625                                 (let ((ps perms)
2626                                       outp)
2627                                   (while ps
2628                                     (setq outp
2629                                           (cons
2630                                            (vector
2631                                             (caar ps)
2632                                             (list
2633                                              'gnus-summary-score-entry
2634                                              (nth 1 header)
2635                                              (if (or (string= (nth 1 header)
2636                                                               "head")
2637                                                      (string= (nth 1 header)
2638                                                               "body"))
2639                                                  ""
2640                                                (list 'gnus-summary-header
2641                                                      (nth 1 header)))
2642                                              (list 'quote (nth 1 (car ts)))
2643                                              (list 'gnus-score-delta-default
2644                                                    nil)
2645                                              (nth 1 (car ps))
2646                                              t)
2647                                             t)
2648                                            outp))
2649                                     (setq ps (cdr ps)))
2650                                   (list (nreverse outp))))
2651                                outt))
2652                         (setq ts (cdr ts)))
2653                       (list (nreverse outt))))
2654                    outh))
2655             (setq headers (cdr headers)))
2656           (list (nreverse outh))))))))
2657
2658 \f
2659
2660 (defun gnus-summary-mode (&optional group)
2661   "Major mode for reading articles.
2662
2663 All normal editing commands are switched off.
2664 \\<gnus-summary-mode-map>
2665 Each line in this buffer represents one article.  To read an
2666 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2667 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2668 respectively.
2669
2670 You can also post articles and send mail from this buffer.  To
2671 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2672 of an article, type `\\[gnus-summary-reply]'.
2673
2674 There are approx. one gazillion commands you can execute in this
2675 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2676
2677 The following commands are available:
2678
2679 \\{gnus-summary-mode-map}"
2680   (interactive)
2681   (kill-all-local-variables)
2682   (when (gnus-visual-p 'summary-menu 'menu)
2683     (gnus-summary-make-menu-bar)
2684     (gnus-summary-make-tool-bar))
2685   (gnus-summary-make-local-variables)
2686   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2687     (gnus-summary-make-local-variables))
2688   (gnus-make-thread-indent-array)
2689   (gnus-simplify-mode-line)
2690   (setq major-mode 'gnus-summary-mode)
2691   (setq mode-name "Summary")
2692   (make-local-variable 'minor-mode-alist)
2693   (use-local-map gnus-summary-mode-map)
2694   (buffer-disable-undo)
2695   (setq buffer-read-only t)             ;Disable modification
2696   (setq truncate-lines t)
2697   (setq selective-display t)
2698   (setq selective-display-ellipses t)   ;Display `...'
2699   (gnus-summary-set-display-table)
2700   (gnus-set-default-directory)
2701   (setq gnus-newsgroup-name group)
2702   (make-local-variable 'gnus-summary-line-format)
2703   (make-local-variable 'gnus-summary-line-format-spec)
2704   (make-local-variable 'gnus-summary-dummy-line-format)
2705   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2706   (make-local-variable 'gnus-summary-mark-positions)
2707   (gnus-make-local-hook 'pre-command-hook)
2708   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2709   (gnus-run-hooks 'gnus-summary-mode-hook)
2710   (turn-on-gnus-mailing-list-mode)
2711   (mm-enable-multibyte)
2712   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2713   (gnus-update-summary-mark-positions))
2714
2715 (defun gnus-summary-make-local-variables ()
2716   "Make all the local summary buffer variables."
2717   (let (global)
2718     (dolist (local gnus-summary-local-variables)
2719       (if (consp local)
2720           (progn
2721             (if (eq (cdr local) 'global)
2722                 ;; Copy the global value of the variable.
2723                 (setq global (symbol-value (car local)))
2724               ;; Use the value from the list.
2725               (setq global (eval (cdr local))))
2726             (set (make-local-variable (car local)) global))
2727         ;; Simple nil-valued local variable.
2728         (set (make-local-variable local) nil)))))
2729
2730 (defun gnus-summary-clear-local-variables ()
2731   (let ((locals gnus-summary-local-variables))
2732     (while locals
2733       (if (consp (car locals))
2734           (and (vectorp (caar locals))
2735                (set (caar locals) nil))
2736         (and (vectorp (car locals))
2737              (set (car locals) nil)))
2738       (setq locals (cdr locals)))))
2739
2740 ;; Summary data functions.
2741
2742 (defmacro gnus-data-number (data)
2743   `(car ,data))
2744
2745 (defmacro gnus-data-set-number (data number)
2746   `(setcar ,data ,number))
2747
2748 (defmacro gnus-data-mark (data)
2749   `(nth 1 ,data))
2750
2751 (defmacro gnus-data-set-mark (data mark)
2752   `(setcar (nthcdr 1 ,data) ,mark))
2753
2754 (defmacro gnus-data-pos (data)
2755   `(nth 2 ,data))
2756
2757 (defmacro gnus-data-set-pos (data pos)
2758   `(setcar (nthcdr 2 ,data) ,pos))
2759
2760 (defmacro gnus-data-header (data)
2761   `(nth 3 ,data))
2762
2763 (defmacro gnus-data-set-header (data header)
2764   `(setf (nth 3 ,data) ,header))
2765
2766 (defmacro gnus-data-level (data)
2767   `(nth 4 ,data))
2768
2769 (defmacro gnus-data-unread-p (data)
2770   `(= (nth 1 ,data) gnus-unread-mark))
2771
2772 (defmacro gnus-data-read-p (data)
2773   `(/= (nth 1 ,data) gnus-unread-mark))
2774
2775 (defmacro gnus-data-pseudo-p (data)
2776   `(consp (nth 3 ,data)))
2777
2778 (defmacro gnus-data-find (number)
2779   `(assq ,number gnus-newsgroup-data))
2780
2781 (defmacro gnus-data-find-list (number &optional data)
2782   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2783      (memq (assq ,number bdata)
2784            bdata)))
2785
2786 (defmacro gnus-data-make (number mark pos header level)
2787   `(list ,number ,mark ,pos ,header ,level))
2788
2789 (defun gnus-data-enter (after-article number mark pos header level offset)
2790   (let ((data (gnus-data-find-list after-article)))
2791     (unless data
2792       (error "No such article: %d" after-article))
2793     (setcdr data (cons (gnus-data-make number mark pos header level)
2794                        (cdr data)))
2795     (setq gnus-newsgroup-data-reverse nil)
2796     (gnus-data-update-list (cddr data) offset)))
2797
2798 (defun gnus-data-enter-list (after-article list &optional offset)
2799   (when list
2800     (let ((data (and after-article (gnus-data-find-list after-article)))
2801           (ilist list))
2802       (if (not (or data
2803                    after-article))
2804           (let ((odata gnus-newsgroup-data))
2805             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2806             (when offset
2807               (gnus-data-update-list odata offset)))
2808       ;; Find the last element in the list to be spliced into the main
2809         ;; list.
2810         (while (cdr list)
2811           (setq list (cdr list)))
2812         (if (not data)
2813             (progn
2814               (setcdr list gnus-newsgroup-data)
2815               (setq gnus-newsgroup-data ilist)
2816               (when offset
2817                 (gnus-data-update-list (cdr list) offset)))
2818           (setcdr list (cdr data))
2819           (setcdr data ilist)
2820           (when offset
2821             (gnus-data-update-list (cdr list) offset))))
2822       (setq gnus-newsgroup-data-reverse nil))))
2823
2824 (defun gnus-data-remove (article &optional offset)
2825   (let ((data gnus-newsgroup-data))
2826     (if (= (gnus-data-number (car data)) article)
2827         (progn
2828           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2829                 gnus-newsgroup-data-reverse nil)
2830           (when offset
2831             (gnus-data-update-list gnus-newsgroup-data offset)))
2832       (while (cdr data)
2833         (when (= (gnus-data-number (cadr data)) article)
2834           (setcdr data (cddr data))
2835           (when offset
2836             (gnus-data-update-list (cdr data) offset))
2837           (setq data nil
2838                 gnus-newsgroup-data-reverse nil))
2839         (setq data (cdr data))))))
2840
2841 (defmacro gnus-data-list (backward)
2842   `(if ,backward
2843        (or gnus-newsgroup-data-reverse
2844            (setq gnus-newsgroup-data-reverse
2845                  (reverse gnus-newsgroup-data)))
2846      gnus-newsgroup-data))
2847
2848 (defun gnus-data-update-list (data offset)
2849   "Add OFFSET to the POS of all data entries in DATA."
2850   (setq gnus-newsgroup-data-reverse nil)
2851   (while data
2852     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2853     (setq data (cdr data))))
2854
2855 (defun gnus-summary-article-pseudo-p (article)
2856   "Say whether this article is a pseudo article or not."
2857   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2858
2859 (defmacro gnus-summary-article-sparse-p (article)
2860   "Say whether this article is a sparse article or not."
2861   `(memq ,article gnus-newsgroup-sparse))
2862
2863 (defmacro gnus-summary-article-ancient-p (article)
2864   "Say whether this article is a sparse article or not."
2865   `(memq ,article gnus-newsgroup-ancient))
2866
2867 (defun gnus-article-parent-p (number)
2868   "Say whether this article is a parent or not."
2869   (let ((data (gnus-data-find-list number)))
2870     (and (cdr data)              ; There has to be an article after...
2871          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2872             (gnus-data-level (nth 1 data))))))
2873
2874 (defun gnus-article-children (number)
2875   "Return a list of all children to NUMBER."
2876   (let* ((data (gnus-data-find-list number))
2877          (level (gnus-data-level (car data)))
2878          children)
2879     (setq data (cdr data))
2880     (while (and data
2881                 (= (gnus-data-level (car data)) (1+ level)))
2882       (push (gnus-data-number (car data)) children)
2883       (setq data (cdr data)))
2884     children))
2885
2886 (defmacro gnus-summary-skip-intangible ()
2887   "If the current article is intangible, then jump to a different article."
2888   '(let ((to (get-text-property (point) 'gnus-intangible)))
2889      (and to (gnus-summary-goto-subject to))))
2890
2891 (defmacro gnus-summary-article-intangible-p ()
2892   "Say whether this article is intangible or not."
2893   '(get-text-property (point) 'gnus-intangible))
2894
2895 (defun gnus-article-read-p (article)
2896   "Say whether ARTICLE is read or not."
2897   (not (or (memq article gnus-newsgroup-marked)
2898            (memq article gnus-newsgroup-spam-marked)
2899            (memq article gnus-newsgroup-unreads)
2900            (memq article gnus-newsgroup-unselected)
2901            (memq article gnus-newsgroup-dormant))))
2902
2903 ;; Some summary mode macros.
2904
2905 (defmacro gnus-summary-article-number ()
2906   "The article number of the article on the current line.
2907 If there isn't an article number here, then we return the current
2908 article number."
2909   '(progn
2910      (gnus-summary-skip-intangible)
2911      (or (get-text-property (point) 'gnus-number)
2912          (gnus-summary-last-subject))))
2913
2914 (defmacro gnus-summary-article-header (&optional number)
2915   "Return the header of article NUMBER."
2916   `(gnus-data-header (gnus-data-find
2917                       ,(or number '(gnus-summary-article-number)))))
2918
2919 (defmacro gnus-summary-thread-level (&optional number)
2920   "Return the level of thread that starts with article NUMBER."
2921   `(if (and (eq gnus-summary-make-false-root 'dummy)
2922             (get-text-property (point) 'gnus-intangible))
2923        0
2924      (gnus-data-level (gnus-data-find
2925                        ,(or number '(gnus-summary-article-number))))))
2926
2927 (defmacro gnus-summary-article-mark (&optional number)
2928   "Return the mark of article NUMBER."
2929   `(gnus-data-mark (gnus-data-find
2930                     ,(or number '(gnus-summary-article-number)))))
2931
2932 (defmacro gnus-summary-article-pos (&optional number)
2933   "Return the position of the line of article NUMBER."
2934   `(gnus-data-pos (gnus-data-find
2935                    ,(or number '(gnus-summary-article-number)))))
2936
2937 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2938 (defmacro gnus-summary-article-subject (&optional number)
2939   "Return current subject string or nil if nothing."
2940   `(let ((headers
2941           ,(if number
2942                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2943              '(gnus-data-header (assq (gnus-summary-article-number)
2944                                       gnus-newsgroup-data)))))
2945      (and headers
2946           (vectorp headers)
2947           (mail-header-subject headers))))
2948
2949 (defmacro gnus-summary-article-score (&optional number)
2950   "Return current article score."
2951   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2952                   gnus-newsgroup-scored))
2953        gnus-summary-default-score 0))
2954
2955 (defun gnus-summary-article-children (&optional number)
2956   "Return a list of article numbers that are children of article NUMBER."
2957   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2958          (level (gnus-data-level (car data)))
2959          l children)
2960     (while (and (setq data (cdr data))
2961                 (> (setq l (gnus-data-level (car data))) level))
2962       (and (= (1+ level) l)
2963            (push (gnus-data-number (car data))
2964                  children)))
2965     (nreverse children)))
2966
2967 (defun gnus-summary-article-parent (&optional number)
2968   "Return the article number of the parent of article NUMBER."
2969   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2970                                     (gnus-data-list t)))
2971          (level (gnus-data-level (car data))))
2972     (if (zerop level)
2973         ()                              ; This is a root.
2974       ;; We search until we find an article with a level less than
2975       ;; this one.  That function has to be the parent.
2976       (while (and (setq data (cdr data))
2977                   (not (< (gnus-data-level (car data)) level))))
2978       (and data (gnus-data-number (car data))))))
2979
2980 (defun gnus-unread-mark-p (mark)
2981   "Say whether MARK is the unread mark."
2982   (= mark gnus-unread-mark))
2983
2984 (defun gnus-read-mark-p (mark)
2985   "Say whether MARK is one of the marks that mark as read.
2986 This is all marks except unread, ticked, dormant, and expirable."
2987   (not (or (= mark gnus-unread-mark)
2988            (= mark gnus-ticked-mark)
2989            (= mark gnus-spam-mark)
2990            (= mark gnus-dormant-mark)
2991            (= mark gnus-expirable-mark))))
2992
2993 (defmacro gnus-article-mark (number)
2994   "Return the MARK of article NUMBER.
2995 This macro should only be used when computing the mark the \"first\"
2996 time; i.e., when generating the summary lines.  After that,
2997 `gnus-summary-article-mark' should be used to examine the
2998 marks of articles."
2999   `(cond
3000     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3001     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3002     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3003     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3004     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3005     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3006     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3007     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3008            gnus-ancient-mark))))
3009
3010 ;; Saving hidden threads.
3011
3012 (defmacro gnus-save-hidden-threads (&rest forms)
3013   "Save hidden threads, eval FORMS, and restore the hidden threads."
3014   (let ((config (make-symbol "config")))
3015     `(let ((,config (gnus-hidden-threads-configuration)))
3016        (unwind-protect
3017            (save-excursion
3018              ,@forms)
3019          (gnus-restore-hidden-threads-configuration ,config)))))
3020 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3021 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3022
3023 (defun gnus-data-compute-positions ()
3024   "Compute the positions of all articles."
3025   (setq gnus-newsgroup-data-reverse nil)
3026   (let ((data gnus-newsgroup-data))
3027     (save-excursion
3028       (gnus-save-hidden-threads
3029         (gnus-summary-show-all-threads)
3030         (goto-char (point-min))
3031         (while data
3032           (while (get-text-property (point) 'gnus-intangible)
3033             (forward-line 1))
3034           (gnus-data-set-pos (car data) (+ (point) 3))
3035           (setq data (cdr data))
3036           (forward-line 1))))))
3037
3038 (defun gnus-hidden-threads-configuration ()
3039   "Return the current hidden threads configuration."
3040   (save-excursion
3041     (let (config)
3042       (goto-char (point-min))
3043       (while (search-forward "\r" nil t)
3044         (push (1- (point)) config))
3045       config)))
3046
3047 (defun gnus-restore-hidden-threads-configuration (config)
3048   "Restore hidden threads configuration from CONFIG."
3049   (save-excursion
3050     (let (point buffer-read-only)
3051       (while (setq point (pop config))
3052         (when (and (< point (point-max))
3053                    (goto-char point)
3054                    (eq (char-after) ?\n))
3055           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3056
3057 ;; Various summary mode internalish functions.
3058
3059 (defun gnus-mouse-pick-article (e)
3060   (interactive "e")
3061   (mouse-set-point e)
3062   (gnus-summary-next-page nil t))
3063
3064 (defun gnus-summary-set-display-table ()
3065   "Change the display table.
3066 Odd characters have a tendency to mess
3067 up nicely formatted displays - we make all possible glyphs
3068 display only a single character."
3069
3070   ;; We start from the standard display table, if any.
3071   (let ((table (or (copy-sequence standard-display-table)
3072                    (make-display-table)))
3073         (i 32))
3074     ;; Nix out all the control chars...
3075     (while (>= (setq i (1- i)) 0)
3076       (aset table i [??]))
3077    ;; ... but not newline and cr, of course.  (cr is necessary for the
3078     ;; selective display).
3079     (aset table ?\n nil)
3080     (aset table ?\r nil)
3081     ;; We keep TAB as well.
3082     (aset table ?\t nil)
3083     ;; We nix out any glyphs over 126 that are not set already.
3084     (let ((i 256))
3085       (while (>= (setq i (1- i)) 127)
3086         ;; Only modify if the entry is nil.
3087         (unless (aref table i)
3088           (aset table i [??]))))
3089     (setq buffer-display-table table)))
3090
3091 (defun gnus-summary-set-article-display-arrow (pos)
3092   "Update the overlay arrow to point to line at position POS."
3093   (when (and gnus-summary-display-arrow
3094              (boundp 'overlay-arrow-position)
3095              (boundp 'overlay-arrow-string))
3096     (save-excursion
3097       (goto-char pos)
3098       (beginning-of-line)
3099       (unless overlay-arrow-position
3100         (setq overlay-arrow-position (make-marker)))
3101       (setq overlay-arrow-string "=>"
3102             overlay-arrow-position (set-marker overlay-arrow-position
3103                                                (point)
3104                                                (current-buffer))))))
3105
3106 (defun gnus-summary-setup-buffer (group)
3107   "Initialize summary buffer."
3108   (let ((buffer (gnus-summary-buffer-name group))
3109         (dead-name (concat "*Dead Summary "
3110                            (gnus-group-decoded-name group) "*")))
3111     ;; If a dead summary buffer exists, we kill it.
3112     (when (gnus-buffer-live-p dead-name)
3113       (gnus-kill-buffer dead-name))
3114     (if (get-buffer buffer)
3115         (progn
3116           (set-buffer buffer)
3117           (setq gnus-summary-buffer (current-buffer))
3118           (not gnus-newsgroup-prepared))
3119       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3120       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3121       (gnus-summary-mode group)
3122       (when gnus-carpal
3123         (gnus-carpal-setup-buffer 'summary))
3124       (unless gnus-single-article-buffer
3125         (make-local-variable 'gnus-article-buffer)
3126         (make-local-variable 'gnus-article-current)
3127         (make-local-variable 'gnus-original-article-buffer))
3128       (setq gnus-newsgroup-name group)
3129       ;; Set any local variables in the group parameters.
3130       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3131       t)))
3132
3133 (defun gnus-set-global-variables ()
3134   "Set the global equivalents of the buffer-local variables.
3135 They are set to the latest values they had.  These reflect the summary
3136 buffer that was in action when the last article was fetched."
3137   (when (eq major-mode 'gnus-summary-mode)
3138     (setq gnus-summary-buffer (current-buffer))
3139     (let ((name gnus-newsgroup-name)
3140           (marked gnus-newsgroup-marked)
3141           (spam gnus-newsgroup-spam-marked)
3142           (unread gnus-newsgroup-unreads)
3143           (headers gnus-current-headers)
3144           (data gnus-newsgroup-data)
3145           (summary gnus-summary-buffer)
3146           (article-buffer gnus-article-buffer)
3147           (original gnus-original-article-buffer)
3148           (gac gnus-article-current)
3149           (reffed gnus-reffed-article-number)
3150           (score-file gnus-current-score-file)
3151           (default-charset gnus-newsgroup-charset)
3152           vlist)
3153       (let ((locals gnus-newsgroup-variables))
3154         (while locals
3155           (if (consp (car locals))
3156               (push (eval (caar locals)) vlist)
3157             (push (eval (car locals)) vlist))
3158           (setq locals (cdr locals)))
3159         (setq vlist (nreverse vlist)))
3160       (save-excursion
3161         (set-buffer gnus-group-buffer)
3162         (setq gnus-newsgroup-name name
3163               gnus-newsgroup-marked marked
3164               gnus-newsgroup-spam-marked spam
3165               gnus-newsgroup-unreads unread
3166               gnus-current-headers headers
3167               gnus-newsgroup-data data
3168               gnus-article-current gac
3169               gnus-summary-buffer summary
3170               gnus-article-buffer article-buffer
3171               gnus-original-article-buffer original
3172               gnus-reffed-article-number reffed
3173               gnus-current-score-file score-file
3174               gnus-newsgroup-charset default-charset)
3175         (let ((locals gnus-newsgroup-variables))
3176           (while locals
3177             (if (consp (car locals))
3178                 (set (caar locals) (pop vlist))
3179               (set (car locals) (pop vlist)))
3180             (setq locals (cdr locals))))
3181         ;; The article buffer also has local variables.
3182         (when (gnus-buffer-live-p gnus-article-buffer)
3183           (set-buffer gnus-article-buffer)
3184           (setq gnus-summary-buffer summary))))))
3185
3186 (defun gnus-summary-article-unread-p (article)
3187   "Say whether ARTICLE is unread or not."
3188   (memq article gnus-newsgroup-unreads))
3189
3190 (defun gnus-summary-first-article-p (&optional article)
3191   "Return whether ARTICLE is the first article in the buffer."
3192   (if (not (setq article (or article (gnus-summary-article-number))))
3193       nil
3194     (eq article (caar gnus-newsgroup-data))))
3195
3196 (defun gnus-summary-last-article-p (&optional article)
3197   "Return whether ARTICLE is the last article in the buffer."
3198   (if (not (setq article (or article (gnus-summary-article-number))))
3199       ;; All non-existent numbers are the last article.  :-)
3200       t
3201     (not (cdr (gnus-data-find-list article)))))
3202
3203 (defun gnus-make-thread-indent-array ()
3204   (let ((n 200))
3205     (unless (and gnus-thread-indent-array
3206                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3207       (setq gnus-thread-indent-array (make-vector 201 "")
3208             gnus-thread-indent-array-level gnus-thread-indent-level)
3209       (while (>= n 0)
3210         (aset gnus-thread-indent-array n
3211               (make-string (* n gnus-thread-indent-level) ? ))
3212         (setq n (1- n))))))
3213
3214 (defun gnus-update-summary-mark-positions ()
3215   "Compute where the summary marks are to go."
3216   (save-excursion
3217     (when (gnus-buffer-exists-p gnus-summary-buffer)
3218       (set-buffer gnus-summary-buffer))
3219     (let ((gnus-replied-mark 129)
3220           (gnus-score-below-mark 130)
3221           (gnus-score-over-mark 130)
3222           (gnus-undownloaded-mark 131)
3223           (spec gnus-summary-line-format-spec)
3224           gnus-visual pos)
3225       (save-excursion
3226         (gnus-set-work-buffer)
3227         (let ((gnus-summary-line-format-spec spec)
3228               (gnus-newsgroup-downloadable '(0)))
3229           (gnus-summary-insert-line
3230            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3231            0 nil t 128 t nil "" nil 1)
3232           (goto-char (point-min))
3233           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3234                                              (- (point) (point-min) 1)))))
3235           (goto-char (point-min))
3236           (push (cons 'replied (and (search-forward "\201" nil t)
3237                                     (- (point) (point-min) 1)))
3238                 pos)
3239           (goto-char (point-min))
3240           (push (cons 'score (and (search-forward "\202" nil t)
3241                                   (- (point) (point-min) 1)))
3242                 pos)
3243           (goto-char (point-min))
3244           (push (cons 'download
3245                       (and (search-forward "\203" nil t)
3246                            (- (point) (point-min) 1)))
3247                 pos)))
3248       (setq gnus-summary-mark-positions pos))))
3249
3250 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3251   "Insert a dummy root in the summary buffer."
3252   (beginning-of-line)
3253   (gnus-add-text-properties
3254    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3255    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3256
3257 (defun gnus-summary-extract-address-component (from)
3258   (or (car (funcall gnus-extract-address-components from))
3259       from))
3260
3261 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3262   (let ((mail-parse-charset gnus-newsgroup-charset)
3263         ; Is it really necessary to do this next part for each summary line?
3264         ; Luckily, doesn't seem to slow things down much.
3265         (mail-parse-ignored-charsets
3266          (with-current-buffer gnus-summary-buffer
3267            gnus-newsgroup-ignored-charsets)))
3268     (or
3269      (and gnus-ignored-from-addresses
3270           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3271           (let ((extra-headers (mail-header-extra header))
3272                 to
3273                 newsgroups)
3274             (cond
3275              ((setq to (cdr (assq 'To extra-headers)))
3276               (concat "-> "
3277                       (inline
3278                         (gnus-summary-extract-address-component
3279                          (funcall gnus-decode-encoded-word-function to)))))
3280              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3281               (concat "=> " newsgroups)))))
3282      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3283
3284 (defun gnus-summary-insert-line (gnus-tmp-header
3285                                  gnus-tmp-level gnus-tmp-current
3286                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3287                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3288                                  &optional gnus-tmp-dummy gnus-tmp-score
3289                                  gnus-tmp-process)
3290   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3291          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3292          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3293          (gnus-tmp-score-char
3294           (if (or (null gnus-summary-default-score)
3295                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3296                       gnus-summary-zcore-fuzz))
3297               ?                         ;Whitespace
3298             (if (< gnus-tmp-score gnus-summary-default-score)
3299                 gnus-score-below-mark gnus-score-over-mark)))
3300          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3301          (gnus-tmp-replied
3302           (cond (gnus-tmp-process gnus-process-mark)
3303                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3304                  gnus-cached-mark)
3305                 (gnus-tmp-replied gnus-replied-mark)
3306                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3307                  gnus-forwarded-mark)
3308                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3309                  gnus-saved-mark)
3310                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3311                  gnus-recent-mark)
3312                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3313                  gnus-unseen-mark)
3314                 (t gnus-no-mark)))
3315          (gnus-tmp-downloaded
3316           (cond (undownloaded
3317                  gnus-undownloaded-mark)
3318                 (gnus-newsgroup-agentized
3319                  gnus-downloaded-mark)
3320                 (t
3321                  gnus-no-mark)))
3322          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3323          (gnus-tmp-name
3324           (cond
3325            ((string-match "<[^>]+> *$" gnus-tmp-from)
3326             (let ((beg (match-beginning 0)))
3327               (or (and (string-match "^\".+\"" gnus-tmp-from)
3328                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3329                   (substring gnus-tmp-from 0 beg))))
3330            ((string-match "(.+)" gnus-tmp-from)
3331             (substring gnus-tmp-from
3332                        (1+ (match-beginning 0)) (1- (match-end 0))))
3333            (t gnus-tmp-from)))
3334          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3335          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3336          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3337          (buffer-read-only nil))
3338     (when (string= gnus-tmp-name "")
3339       (setq gnus-tmp-name gnus-tmp-from))
3340     (unless (numberp gnus-tmp-lines)
3341       (setq gnus-tmp-lines -1))
3342     (if (= gnus-tmp-lines -1)
3343         (setq gnus-tmp-lines "?")
3344       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3345       (gnus-put-text-property
3346      (point)
3347      (progn (eval gnus-summary-line-format-spec) (point))
3348        'gnus-number gnus-tmp-number)
3349     (when (gnus-visual-p 'summary-highlight 'highlight)
3350       (forward-line -1)
3351       (gnus-run-hooks 'gnus-summary-update-hook)
3352       (forward-line 1))))
3353
3354 (defun gnus-summary-update-line (&optional dont-update)
3355   "Update summary line after change."
3356   (when (and gnus-summary-default-score
3357              (not gnus-summary-inhibit-highlight))
3358     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3359            (article (gnus-summary-article-number))
3360            (score (gnus-summary-article-score article)))
3361       (unless dont-update
3362         (if (and gnus-summary-mark-below
3363                  (< (gnus-summary-article-score)
3364                     gnus-summary-mark-below))
3365             ;; This article has a low score, so we mark it as read.
3366             (when (memq article gnus-newsgroup-unreads)
3367               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3368           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3369             ;; This article was previously marked as read on account
3370             ;; of a low score, but now it has risen, so we mark it as
3371             ;; unread.
3372             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3373         (gnus-summary-update-mark
3374          (if (or (null gnus-summary-default-score)
3375                  (<= (abs (- score gnus-summary-default-score))
3376                      gnus-summary-zcore-fuzz))
3377              ?                          ;Whitespace
3378            (if (< score gnus-summary-default-score)
3379                gnus-score-below-mark gnus-score-over-mark))
3380          'score))
3381       ;; Do visual highlighting.
3382       (when (gnus-visual-p 'summary-highlight 'highlight)
3383         (gnus-run-hooks 'gnus-summary-update-hook)))))
3384
3385 (defvar gnus-tmp-new-adopts nil)
3386
3387 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3388   "Return the number of articles in THREAD.
3389 This may be 0 in some cases -- if none of the articles in
3390 the thread are to be displayed."
3391   (let* ((number
3392          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3393           (cond
3394            ((not (listp thread))
3395             1)
3396            ((and (consp thread) (cdr thread))
3397             (apply
3398              '+ 1 (mapcar
3399                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3400            ((null thread)
3401             1)
3402            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3403             1)
3404            (t 0))))
3405     (when (and level (zerop level) gnus-tmp-new-adopts)
3406       (incf number
3407             (apply '+ (mapcar
3408                        'gnus-summary-number-of-articles-in-thread
3409                        gnus-tmp-new-adopts))))
3410     (if char
3411         (if (> number 1) gnus-not-empty-thread-mark
3412           gnus-empty-thread-mark)
3413       number)))
3414
3415 (defsubst gnus-summary-line-message-size (head)
3416   "Return pretty-printed version of message size.
3417 This function is intended to be used in
3418 `gnus-summary-line-format-alist'."
3419   (let ((c (or (mail-header-chars head) -1)))
3420     (cond ((< c 0) "n/a")               ; chars not available
3421           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3422           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3423           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3424           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3425
3426
3427 (defun gnus-summary-set-local-parameters (group)
3428   "Go through the local params of GROUP and set all variable specs in that list."
3429   (let ((params (gnus-group-find-parameter group))
3430         (vars '(quit-config))           ; Ignore quit-config.
3431         elem)
3432     (while params
3433       (setq elem (car params)
3434             params (cdr params))
3435       (and (consp elem)                 ; Has to be a cons.
3436            (consp (cdr elem))           ; The cdr has to be a list.
3437            (symbolp (car elem))         ; Has to be a symbol in there.
3438            (not (memq (car elem) vars))
3439            (ignore-errors               ; So we set it.
3440              (push (car elem) vars)
3441              (make-local-variable (car elem))
3442              (set (car elem) (eval (nth 1 elem))))))))
3443
3444 (defun gnus-summary-read-group (group &optional show-all no-article
3445                                       kill-buffer no-display backward
3446                                       select-articles)
3447   "Start reading news in newsgroup GROUP.
3448 If SHOW-ALL is non-nil, already read articles are also listed.
3449 If NO-ARTICLE is non-nil, no article is selected initially.
3450 If NO-DISPLAY, don't generate a summary buffer."
3451   (let (result)
3452     (while (and group
3453                 (null (setq result
3454                             (let ((gnus-auto-select-next nil))
3455                               (or (gnus-summary-read-group-1
3456                                    group show-all no-article
3457                                    kill-buffer no-display
3458                                    select-articles)
3459                                   (setq show-all nil
3460                                         select-articles nil)))))
3461                 (eq gnus-auto-select-next 'quietly))
3462       (set-buffer gnus-group-buffer)
3463       ;; The entry function called above goes to the next
3464       ;; group automatically, so we go two groups back
3465       ;; if we are searching for the previous group.
3466       (when backward
3467         (gnus-group-prev-unread-group 2))
3468       (if (not (equal group (gnus-group-group-name)))
3469           (setq group (gnus-group-group-name))
3470         (setq group nil)))
3471     result))
3472
3473 (defun gnus-summary-read-group-1 (group show-all no-article
3474                                         kill-buffer no-display
3475                                         &optional select-articles)
3476   ;; Killed foreign groups can't be entered.
3477   ;;  (when (and (not (gnus-group-native-p group))
3478   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3479   ;;    (error "Dead non-native groups can't be entered"))
3480   (gnus-message 5 "Retrieving newsgroup: %s..."
3481                 (gnus-group-decoded-name group))
3482   (let* ((new-group (gnus-summary-setup-buffer group))
3483          (quit-config (gnus-group-quit-config group))
3484          (did-select (and new-group (gnus-select-newsgroup
3485                                      group show-all select-articles))))
3486     (cond
3487      ;; This summary buffer exists already, so we just select it.
3488      ((not new-group)
3489       (gnus-set-global-variables)
3490       (when kill-buffer
3491         (gnus-kill-or-deaden-summary kill-buffer))
3492       (gnus-configure-windows 'summary 'force)
3493       (gnus-set-mode-line 'summary)
3494       (gnus-summary-position-point)
3495       (message "")
3496       t)
3497      ;; We couldn't select this group.
3498      ((null did-select)
3499       (when (and (eq major-mode 'gnus-summary-mode)
3500                  (not (equal (current-buffer) kill-buffer)))
3501         (kill-buffer (current-buffer))
3502         (if (not quit-config)
3503             (progn
3504               ;; Update the info -- marks might need to be removed,
3505               ;; for instance.
3506               (gnus-summary-update-info)
3507               (set-buffer gnus-group-buffer)
3508               (gnus-group-jump-to-group group)
3509               (gnus-group-next-unread-group 1))
3510           (gnus-handle-ephemeral-exit quit-config)))
3511       (let ((grpinfo (gnus-get-info group)))
3512         (if (null (gnus-info-read grpinfo))
3513             (gnus-message 3 "Group %s contains no messages"
3514                           (gnus-group-decoded-name group))
3515           (gnus-message 3 "Can't select group")))
3516       nil)
3517      ;; The user did a `C-g' while prompting for number of articles,
3518      ;; so we exit this group.
3519      ((eq did-select 'quit)
3520       (and (eq major-mode 'gnus-summary-mode)
3521            (not (equal (current-buffer) kill-buffer))
3522            (kill-buffer (current-buffer)))
3523       (when kill-buffer
3524         (gnus-kill-or-deaden-summary kill-buffer))
3525       (if (not quit-config)
3526           (progn
3527             (set-buffer gnus-group-buffer)
3528             (gnus-group-jump-to-group group)
3529             (gnus-group-next-unread-group 1)
3530             (gnus-configure-windows 'group 'force))
3531         (gnus-handle-ephemeral-exit quit-config))
3532       ;; Finally signal the quit.
3533       (signal 'quit nil))
3534      ;; The group was successfully selected.
3535      (t
3536       (gnus-set-global-variables)
3537       ;; Save the active value in effect when the group was entered.
3538       (setq gnus-newsgroup-active
3539             (gnus-copy-sequence
3540              (gnus-active gnus-newsgroup-name)))
3541       ;; You can change the summary buffer in some way with this hook.
3542       (gnus-run-hooks 'gnus-select-group-hook)
3543       (gnus-update-format-specifications
3544        nil 'summary 'summary-mode 'summary-dummy)
3545       (gnus-update-summary-mark-positions)
3546       ;; Do score processing.
3547       (when gnus-use-scoring
3548         (gnus-possibly-score-headers))
3549       ;; Check whether to fill in the gaps in the threads.
3550       (when gnus-build-sparse-threads
3551         (gnus-build-sparse-threads))
3552       ;; Find the initial limit.
3553       (if gnus-show-threads
3554           (if show-all
3555               (let ((gnus-newsgroup-dormant nil))
3556                 (gnus-summary-initial-limit show-all))
3557             (gnus-summary-initial-limit show-all))
3558         ;; When unthreaded, all articles are always shown.
3559         (setq gnus-newsgroup-limit
3560               (mapcar
3561                (lambda (header) (mail-header-number header))
3562                gnus-newsgroup-headers)))
3563       ;; Generate the summary buffer.
3564       (unless no-display
3565         (gnus-summary-prepare))
3566       (when gnus-use-trees
3567         (gnus-tree-open group)
3568         (setq gnus-summary-highlight-line-function
3569               'gnus-tree-highlight-article))
3570       ;; If the summary buffer is empty, but there are some low-scored
3571       ;; articles or some excluded dormants, we include these in the
3572       ;; buffer.
3573       (when (and (zerop (buffer-size))
3574                  (not no-display))
3575         (cond (gnus-newsgroup-dormant
3576                (gnus-summary-limit-include-dormant))
3577               ((and gnus-newsgroup-scored show-all)
3578                (gnus-summary-limit-include-expunged t))))
3579       ;; Function `gnus-apply-kill-file' must be called in this hook.
3580       (gnus-run-hooks 'gnus-apply-kill-hook)
3581       (if (and (zerop (buffer-size))
3582                (not no-display))
3583           (progn
3584             ;; This newsgroup is empty.
3585             (gnus-summary-catchup-and-exit nil t)
3586             (gnus-message 6 "No unread news")
3587             (when kill-buffer
3588               (gnus-kill-or-deaden-summary kill-buffer))
3589             ;; Return nil from this function.
3590             nil)
3591         ;; Hide conversation thread subtrees.  We cannot do this in
3592         ;; gnus-summary-prepare-hook since kill processing may not
3593         ;; work with hidden articles.
3594         (gnus-summary-maybe-hide-threads)
3595         (when kill-buffer
3596           (gnus-kill-or-deaden-summary kill-buffer))
3597         (gnus-summary-auto-select-subject)
3598         ;; Show first unread article if requested.
3599         (if (and (not no-article)
3600                  (not no-display)
3601                  gnus-newsgroup-unreads
3602                  gnus-auto-select-first)
3603             (progn
3604               (gnus-configure-windows 'summary)
3605               (let ((art (gnus-summary-article-number)))
3606                 (unless (and (not gnus-plugged)
3607                              (or (memq art gnus-newsgroup-undownloaded)
3608                                  (memq art gnus-newsgroup-downloadable)))
3609                   (gnus-summary-goto-article art))))
3610           ;; Don't select any articles.
3611           (gnus-summary-position-point)
3612           (gnus-configure-windows 'summary 'force)
3613           (gnus-set-mode-line 'summary))
3614         (when (and gnus-auto-center-group
3615                    (get-buffer-window gnus-group-buffer t))
3616           ;; Gotta use windows, because recenter does weird stuff if
3617           ;; the current buffer ain't the displayed window.
3618           (let ((owin (selected-window)))
3619             (select-window (get-buffer-window gnus-group-buffer t))
3620             (when (gnus-group-goto-group group)
3621               (recenter))
3622             (select-window owin)))
3623         ;; Mark this buffer as "prepared".
3624         (setq gnus-newsgroup-prepared t)
3625         (gnus-run-hooks 'gnus-summary-prepared-hook)
3626         (unless (gnus-ephemeral-group-p group)
3627           (gnus-group-update-group group))
3628         t)))))
3629
3630 (defun gnus-summary-auto-select-subject ()
3631   "Select the subject line on initial group entry."
3632   (goto-char (point-min))
3633   (cond
3634    ((eq gnus-auto-select-subject 'best)
3635     (gnus-summary-best-unread-subject))
3636    ((eq gnus-auto-select-subject 'unread)
3637     (gnus-summary-first-unread-subject))
3638    ((eq gnus-auto-select-subject 'unseen)
3639     (gnus-summary-first-unseen-subject))
3640    ((eq gnus-auto-select-subject 'unseen-or-unread)
3641     (gnus-summary-first-unseen-or-unread-subject))
3642    ((eq gnus-auto-select-subject 'first)
3643     ;; Do nothing.
3644     )
3645    ((functionp gnus-auto-select-subject)
3646     (funcall gnus-auto-select-subject))))
3647
3648 (defun gnus-summary-prepare ()
3649   "Generate the summary buffer."
3650   (interactive)
3651   (let ((buffer-read-only nil))
3652     (erase-buffer)
3653     (setq gnus-newsgroup-data nil
3654           gnus-newsgroup-data-reverse nil)
3655     (gnus-run-hooks 'gnus-summary-generate-hook)
3656     ;; Generate the buffer, either with threads or without.
3657     (when gnus-newsgroup-headers
3658       (gnus-summary-prepare-threads
3659        (if gnus-show-threads
3660            (gnus-sort-gathered-threads
3661             (funcall gnus-summary-thread-gathering-function
3662                      (gnus-sort-threads
3663                       (gnus-cut-threads (gnus-make-threads)))))
3664          ;; Unthreaded display.
3665          (gnus-sort-articles gnus-newsgroup-headers))))
3666     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3667     ;; Call hooks for modifying summary buffer.
3668     (goto-char (point-min))
3669     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3670
3671 (defsubst gnus-general-simplify-subject (subject)
3672   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3673   (setq subject
3674         (cond
3675          ;; Truncate the subject.
3676          (gnus-simplify-subject-functions
3677           (gnus-map-function gnus-simplify-subject-functions subject))
3678          ((numberp gnus-summary-gather-subject-limit)
3679           (setq subject (gnus-simplify-subject-re subject))
3680           (if (> (length subject) gnus-summary-gather-subject-limit)
3681               (substring subject 0 gnus-summary-gather-subject-limit)
3682             subject))
3683          ;; Fuzzily simplify it.
3684          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3685           (gnus-simplify-subject-fuzzy subject))
3686          ;; Just remove the leading "Re:".
3687          (t
3688           (gnus-simplify-subject-re subject))))
3689
3690   (if (and gnus-summary-gather-exclude-subject
3691            (string-match gnus-summary-gather-exclude-subject subject))
3692       nil                         ; This article shouldn't be gathered
3693     subject))
3694
3695 (defun gnus-summary-simplify-subject-query ()
3696   "Query where the respool algorithm would put this article."
3697   (interactive)
3698   (gnus-summary-select-article)
3699   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3700
3701 (defun gnus-gather-threads-by-subject (threads)
3702   "Gather threads by looking at Subject headers."
3703   (if (not gnus-summary-make-false-root)
3704       threads
3705     (let ((hashtb (gnus-make-hashtable 1024))
3706           (prev threads)
3707           (result threads)
3708           subject hthread whole-subject)
3709       (while threads
3710         (setq subject (gnus-general-simplify-subject
3711                        (setq whole-subject (mail-header-subject
3712                                             (caar threads)))))
3713         (when subject
3714           (if (setq hthread (gnus-gethash subject hashtb))
3715               (progn
3716                 ;; We enter a dummy root into the thread, if we
3717                 ;; haven't done that already.
3718                 (unless (stringp (caar hthread))
3719                   (setcar hthread (list whole-subject (car hthread))))
3720                 ;; We add this new gathered thread to this gathered
3721                 ;; thread.
3722                 (setcdr (car hthread)
3723                         (nconc (cdar hthread) (list (car threads))))
3724                 ;; Remove it from the list of threads.
3725                 (setcdr prev (cdr threads))
3726                 (setq threads prev))
3727             ;; Enter this thread into the hash table.
3728             (gnus-sethash subject
3729                           (if gnus-summary-make-false-root-always
3730                               (progn
3731                                 ;; If you want a dummy root above all
3732                                 ;; threads...
3733                                 (setcar threads (list whole-subject
3734                                                       (car threads)))
3735                                 threads)
3736                             threads)
3737                           hashtb)))
3738         (setq prev threads)
3739         (setq threads (cdr threads)))
3740       result)))
3741
3742 (defun gnus-gather-threads-by-references (threads)
3743   "Gather threads by looking at References headers."
3744   (let ((idhashtb (gnus-make-hashtable 1024))
3745         (thhashtb (gnus-make-hashtable 1024))
3746         (prev threads)
3747         (result threads)
3748         ids references id gthread gid entered ref)
3749     (while threads
3750       (when (setq references (mail-header-references (caar threads)))
3751         (setq id (mail-header-id (caar threads))
3752               ids (inline (gnus-split-references references))
3753               entered nil)
3754         (while (setq ref (pop ids))
3755           (setq ids (delete ref ids))
3756           (if (not (setq gid (gnus-gethash ref idhashtb)))
3757               (progn
3758                 (gnus-sethash ref id idhashtb)
3759                 (gnus-sethash id threads thhashtb))
3760             (setq gthread (gnus-gethash gid thhashtb))
3761             (unless entered
3762               ;; We enter a dummy root into the thread, if we
3763               ;; haven't done that already.
3764               (unless (stringp (caar gthread))
3765                 (setcar gthread (list (mail-header-subject (caar gthread))
3766                                       (car gthread))))
3767               ;; We add this new gathered thread to this gathered
3768               ;; thread.
3769               (setcdr (car gthread)
3770                       (nconc (cdar gthread) (list (car threads)))))
3771             ;; Add it into the thread hash table.
3772             (gnus-sethash id gthread thhashtb)
3773             (setq entered t)
3774             ;; Remove it from the list of threads.
3775             (setcdr prev (cdr threads))
3776             (setq threads prev))))
3777       (setq prev threads)
3778       (setq threads (cdr threads)))
3779     result))
3780
3781 (defun gnus-sort-gathered-threads (threads)
3782   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3783   (let ((result threads))
3784     (while threads
3785       (when (stringp (caar threads))
3786         (setcdr (car threads)
3787                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3788       (setq threads (cdr threads)))
3789     result))
3790
3791 (defun gnus-thread-loop-p (root thread)
3792   "Say whether ROOT is in THREAD."
3793   (let ((stack (list thread))
3794         (infloop 0)
3795         th)
3796     (while (setq thread (pop stack))
3797       (setq th (cdr thread))
3798       (while (and th
3799                   (not (eq (caar th) root)))
3800         (pop th))
3801       (if th
3802           ;; We have found a loop.
3803           (let (ref-dep)
3804             (setcdr thread (delq (car th) (cdr thread)))
3805             (if (boundp (setq ref-dep (intern "none"
3806                                               gnus-newsgroup-dependencies)))
3807                 (setcdr (symbol-value ref-dep)
3808                         (nconc (cdr (symbol-value ref-dep))
3809                                (list (car th))))
3810               (set ref-dep (list nil (car th))))
3811             (setq infloop 1
3812                   stack nil))
3813         ;; Push all the subthreads onto the stack.
3814         (push (cdr thread) stack)))
3815     infloop))
3816
3817 (defun gnus-make-threads ()
3818   "Go through the dependency hashtb and find the roots.  Return all threads."
3819   (let (threads)
3820     (while (catch 'infloop
3821              (mapatoms
3822               (lambda (refs)
3823                 ;; Deal with self-referencing References loops.
3824                 (when (and (car (symbol-value refs))
3825                            (not (zerop
3826                                  (apply
3827                                   '+
3828                                   (mapcar
3829                                    (lambda (thread)
3830                                      (gnus-thread-loop-p
3831                                       (car (symbol-value refs)) thread))
3832                                    (cdr (symbol-value refs)))))))
3833                   (setq threads nil)
3834                   (throw 'infloop t))
3835                 (unless (car (symbol-value refs))
3836                   ;; These threads do not refer back to any other
3837                   ;; articles, so they're roots.
3838                   (setq threads (append (cdr (symbol-value refs)) threads))))
3839               gnus-newsgroup-dependencies)))
3840     threads))
3841
3842 ;; Build the thread tree.
3843 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3844   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3845
3846 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3847 if it was already present.
3848
3849 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3850 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3851 Message-IDs will be renamed to a unique Message-ID before being
3852 entered.
3853
3854 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3855   (let* ((id (mail-header-id header))
3856          (id-dep (and id (intern id dependencies)))
3857          parent-id ref ref-dep ref-header replaced)
3858     ;; Enter this `header' in the `dependencies' table.
3859     (cond
3860      ((not id-dep)
3861       (setq header nil))
3862      ;; The first two cases do the normal part: enter a new `header'
3863      ;; in the `dependencies' table.
3864      ((not (boundp id-dep))
3865       (set id-dep (list header)))
3866      ((null (car (symbol-value id-dep)))
3867       (setcar (symbol-value id-dep) header))
3868
3869      ;; From here the `header' was already present in the
3870      ;; `dependencies' table.
3871      (force-new
3872       ;; Overrides an existing entry;
3873       ;; just set the header part of the entry.
3874       (setcar (symbol-value id-dep) header)
3875       (setq replaced t))
3876
3877      ;; Renames the existing `header' to a unique Message-ID.
3878      ((not gnus-summary-ignore-duplicates)
3879       ;; An article with this Message-ID has already been seen.
3880       ;; We rename the Message-ID.
3881       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3882            (list header))
3883       (mail-header-set-id header id))
3884
3885      ;; The last case ignores an existing entry, except it adds any
3886      ;; additional Xrefs (in case the two articles came from different
3887      ;; servers.
3888      ;; Also sets `header' to `nil' meaning that the `dependencies'
3889      ;; table was *not* modified.
3890      (t
3891       (mail-header-set-xref
3892        (car (symbol-value id-dep))
3893        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3894                    "")
3895                (or (mail-header-xref header) "")))
3896       (setq header nil)))
3897
3898     (when (and header (not replaced))
3899       ;; First check that we are not creating a References loop.
3900       (setq parent-id (gnus-parent-id (mail-header-references header)))
3901       (setq ref parent-id)
3902       (while (and ref
3903                   (setq ref-dep (intern-soft ref dependencies))
3904                   (boundp ref-dep)
3905                   (setq ref-header (car (symbol-value ref-dep))))
3906         (if (string= id ref)
3907             ;; Yuk!  This is a reference loop.  Make the article be a
3908             ;; root article.
3909             (progn
3910               (mail-header-set-references (car (symbol-value id-dep)) "none")
3911               (setq ref nil)
3912               (setq parent-id nil))
3913           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3914       (setq ref-dep (intern (or parent-id "none") dependencies))
3915       (if (boundp ref-dep)
3916           (setcdr (symbol-value ref-dep)
3917                   (nconc (cdr (symbol-value ref-dep))
3918                          (list (symbol-value id-dep))))
3919         (set ref-dep (list nil (symbol-value id-dep)))))
3920     header))
3921
3922 (defun gnus-extract-message-id-from-in-reply-to (string)
3923   (if (string-match "<[^>]+>" string)
3924       (substring string (match-beginning 0) (match-end 0))
3925     nil))
3926
3927 (defun gnus-build-sparse-threads ()
3928   (let ((headers gnus-newsgroup-headers)
3929         (mail-parse-charset gnus-newsgroup-charset)
3930         (gnus-summary-ignore-duplicates t)
3931         header references generation relations
3932         subject child end new-child date)
3933     ;; First we create an alist of generations/relations, where
3934     ;; generations is how much we trust the relation, and the relation
3935     ;; is parent/child.
3936     (gnus-message 7 "Making sparse threads...")
3937     (save-excursion
3938       (nnheader-set-temp-buffer " *gnus sparse threads*")
3939       (while (setq header (pop headers))
3940         (when (and (setq references (mail-header-references header))
3941                    (not (string= references "")))
3942           (insert references)
3943           (setq child (mail-header-id header)
3944                 subject (mail-header-subject header)
3945                 date (mail-header-date header)
3946                 generation 0)
3947           (while (search-backward ">" nil t)
3948             (setq end (1+ (point)))
3949             (when (search-backward "<" nil t)
3950               (setq new-child (buffer-substring (point) end))
3951               (push (list (incf generation)
3952                           child (setq child new-child)
3953                           subject date)
3954                     relations)))
3955           (when child
3956             (push (list (1+ generation) child nil subject) relations))
3957           (erase-buffer)))
3958       (kill-buffer (current-buffer)))
3959     ;; Sort over trustworthiness.
3960     (mapcar
3961      (lambda (relation)
3962        (when (gnus-dependencies-add-header
3963               (make-full-mail-header
3964                gnus-reffed-article-number
3965                (nth 3 relation) "" (or (nth 4 relation) "")
3966                (nth 1 relation)
3967                (or (nth 2 relation) "") 0 0 "")
3968               gnus-newsgroup-dependencies nil)
3969          (push gnus-reffed-article-number gnus-newsgroup-limit)
3970          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3971          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3972                gnus-newsgroup-reads)
3973          (decf gnus-reffed-article-number)))
3974      (sort relations 'car-less-than-car))
3975     (gnus-message 7 "Making sparse threads...done")))
3976
3977 (defun gnus-build-old-threads ()
3978   ;; Look at all the articles that refer back to old articles, and
3979   ;; fetch the headers for the articles that aren't there.  This will
3980   ;; build complete threads - if the roots haven't been expired by the
3981   ;; server, that is.
3982   (let ((mail-parse-charset gnus-newsgroup-charset)
3983         id heads)
3984     (mapatoms
3985      (lambda (refs)
3986        (when (not (car (symbol-value refs)))
3987          (setq heads (cdr (symbol-value refs)))
3988          (while heads
3989            (if (memq (mail-header-number (caar heads))
3990                      gnus-newsgroup-dormant)
3991                (setq heads (cdr heads))
3992              (setq id (symbol-name refs))
3993              (while (and (setq id (gnus-build-get-header id))
3994                          (not (car (gnus-id-to-thread id)))))
3995              (setq heads nil)))))
3996      gnus-newsgroup-dependencies)))
3997
3998 (defsubst gnus-remove-odd-characters (string)
3999   "Translate STRING into something that doesn't contain weird characters."
4000   (mm-subst-char-in-string
4001    ?\r ?\-
4002    (mm-subst-char-in-string ?\n ?\- string t) t))
4003
4004 ;; This function has to be called with point after the article number
4005 ;; on the beginning of the line.
4006 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4007   (let ((eol (point-at-eol))
4008         (buffer (current-buffer))
4009         header references in-reply-to)
4010
4011     ;; overview: [num subject from date id refs chars lines misc]
4012     (unwind-protect
4013         (let (x)
4014           (narrow-to-region (point) eol)
4015           (unless (eobp)
4016             (forward-char))
4017
4018           (setq header
4019                 (make-full-mail-header
4020                  number                 ; number
4021                  (condition-case ()     ; subject
4022                      (gnus-remove-odd-characters
4023                       (funcall gnus-decode-encoded-word-function
4024                                (setq x (nnheader-nov-field))))
4025                    (error x))
4026                  (condition-case ()     ; from
4027                      (gnus-remove-odd-characters
4028                       (funcall gnus-decode-encoded-word-function
4029                                (setq x (nnheader-nov-field))))
4030                    (error x))
4031                  (nnheader-nov-field)   ; date
4032                  (nnheader-nov-read-message-id) ; id
4033                  (setq references (nnheader-nov-field)) ; refs
4034                  (nnheader-nov-read-integer) ; chars
4035                  (nnheader-nov-read-integer) ; lines
4036                  (unless (eobp)
4037                    (if (looking-at "Xref: ")
4038                        (goto-char (match-end 0)))
4039                    (nnheader-nov-field)) ; Xref
4040                  (nnheader-nov-parse-extra)))) ; extra
4041
4042       (widen))
4043
4044     (when (and (string= references "")
4045                (setq in-reply-to (mail-header-extra header))
4046                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4047       (mail-header-set-references
4048        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4049
4050     (when gnus-alter-header-function
4051       (funcall gnus-alter-header-function header))
4052     (gnus-dependencies-add-header header dependencies force-new)))
4053
4054 (defun gnus-build-get-header (id)
4055   "Look through the buffer of NOV lines and find the header to ID.
4056 Enter this line into the dependencies hash table, and return
4057 the id of the parent article (if any)."
4058   (let ((deps gnus-newsgroup-dependencies)
4059         found header)
4060     (prog1
4061         (save-excursion
4062           (set-buffer nntp-server-buffer)
4063           (let ((case-fold-search nil))
4064             (goto-char (point-min))
4065             (while (and (not found)
4066                         (search-forward id nil t))
4067               (beginning-of-line)
4068               (setq found (looking-at
4069                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4070                                    (regexp-quote id))))
4071               (or found (beginning-of-line 2)))
4072             (when found
4073               (beginning-of-line)
4074               (and
4075                (setq header (gnus-nov-parse-line
4076                              (read (current-buffer)) deps))
4077                (gnus-parent-id (mail-header-references header))))))
4078       (when header
4079         (let ((number (mail-header-number header)))
4080           (push number gnus-newsgroup-limit)
4081           (push header gnus-newsgroup-headers)
4082           (if (memq number gnus-newsgroup-unselected)
4083               (progn
4084                 (setq gnus-newsgroup-unreads
4085                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4086                                                number))
4087                 (setq gnus-newsgroup-unselected
4088                       (delq number gnus-newsgroup-unselected)))
4089             (push number gnus-newsgroup-ancient)))))))
4090
4091 (defun gnus-build-all-threads ()
4092   "Read all the headers."
4093   (let ((gnus-summary-ignore-duplicates t)
4094         (mail-parse-charset gnus-newsgroup-charset)
4095         (dependencies gnus-newsgroup-dependencies)
4096         header article)
4097     (save-excursion
4098       (set-buffer nntp-server-buffer)
4099       (let ((case-fold-search nil))
4100         (goto-char (point-min))
4101         (while (not (eobp))
4102           (ignore-errors
4103             (setq article (read (current-buffer))
4104                   header (gnus-nov-parse-line article dependencies)))
4105           (when header
4106             (save-excursion
4107               (set-buffer gnus-summary-buffer)
4108               (push header gnus-newsgroup-headers)
4109               (if (memq (setq article (mail-header-number header))
4110                         gnus-newsgroup-unselected)
4111                   (progn
4112                     (setq gnus-newsgroup-unreads
4113                           (gnus-add-to-sorted-list
4114                            gnus-newsgroup-unreads article))
4115                     (setq gnus-newsgroup-unselected
4116                           (delq article gnus-newsgroup-unselected)))
4117                 (push article gnus-newsgroup-ancient)))
4118             (forward-line 1)))))))
4119
4120 (defun gnus-summary-update-article-line (article header)
4121   "Update the line for ARTICLE using HEADER."
4122   (let* ((id (mail-header-id header))
4123          (thread (gnus-id-to-thread id)))
4124     (unless thread
4125       (error "Article in no thread"))
4126     ;; Update the thread.
4127     (setcar thread header)
4128     (gnus-summary-goto-subject article)
4129     (let* ((datal (gnus-data-find-list article))
4130            (data (car datal))
4131            (buffer-read-only nil)
4132            (level (gnus-summary-thread-level)))
4133       (gnus-delete-line)
4134       (let ((inserted (- (point)
4135                          (progn
4136                            (gnus-summary-insert-line
4137                             header level nil
4138                             (memq article gnus-newsgroup-undownloaded)
4139                             (gnus-article-mark article)
4140                             (memq article gnus-newsgroup-replied)
4141                             (memq article gnus-newsgroup-expirable)
4142                             ;; Only insert the Subject string when it's different
4143                             ;; from the previous Subject string.
4144                             (if (and
4145                                  gnus-show-threads
4146                                  (gnus-subject-equal
4147                                   (condition-case ()
4148                                       (mail-header-subject
4149                                        (gnus-data-header
4150                                         (cadr
4151                                          (gnus-data-find-list
4152                                           article
4153                                           (gnus-data-list t)))))
4154                                     ;; Error on the side of excessive subjects.
4155                                     (error ""))
4156                                   (mail-header-subject header)))
4157                                 ""
4158                               (mail-header-subject header))
4159                             nil (cdr (assq article gnus-newsgroup-scored))
4160                             (memq article gnus-newsgroup-processable))
4161                            (point)))))
4162         (when (cdr datal)
4163           (gnus-data-update-list
4164            (cdr datal)
4165            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4166
4167 (defun gnus-summary-update-article (article &optional iheader)
4168   "Update ARTICLE in the summary buffer."
4169   (set-buffer gnus-summary-buffer)
4170   (let* ((header (gnus-summary-article-header article))
4171          (id (mail-header-id header))
4172          (data (gnus-data-find article))
4173          (thread (gnus-id-to-thread id))
4174          (references (mail-header-references header))
4175          (parent
4176           (gnus-id-to-thread
4177            (or (gnus-parent-id
4178                 (when (and references
4179                            (not (equal "" references)))
4180                   references))
4181                "none")))
4182          (buffer-read-only nil)
4183          (old (car thread)))
4184     (when thread
4185       (unless iheader
4186         (setcar thread nil)
4187         (when parent
4188           (delq thread parent)))
4189       (if (gnus-summary-insert-subject id header)
4190           ;; Set the (possibly) new article number in the data structure.
4191           (gnus-data-set-number data (gnus-id-to-article id))
4192         (setcar thread old)
4193         nil))))
4194
4195 (defun gnus-rebuild-thread (id &optional line)
4196   "Rebuild the thread containing ID.
4197 If LINE, insert the rebuilt thread starting on line LINE."
4198   (let ((buffer-read-only nil)
4199         old-pos current thread data)
4200     (if (not gnus-show-threads)
4201         (setq thread (list (car (gnus-id-to-thread id))))
4202       ;; Get the thread this article is part of.
4203       (setq thread (gnus-remove-thread id)))
4204     (setq old-pos (point-at-bol))
4205     (setq current (save-excursion
4206                     (and (re-search-backward "[\r\n]" nil t)
4207                          (gnus-summary-article-number))))
4208     ;; If this is a gathered thread, we have to go some re-gathering.
4209     (when (stringp (car thread))
4210       (let ((subject (car thread))
4211             roots thr)
4212         (setq thread (cdr thread))
4213         (while thread
4214           (unless (memq (setq thr (gnus-id-to-thread
4215                                    (gnus-root-id
4216                                     (mail-header-id (caar thread)))))
4217                         roots)
4218             (push thr roots))
4219           (setq thread (cdr thread)))
4220         ;; We now have all (unique) roots.
4221         (if (= (length roots) 1)
4222             ;; All the loose roots are now one solid root.
4223             (setq thread (car roots))
4224           (setq thread (cons subject (gnus-sort-threads roots))))))
4225     (let (threads)
4226       ;; We then insert this thread into the summary buffer.
4227       (when line
4228         (goto-char (point-min))
4229         (forward-line (1- line)))
4230       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4231         (if gnus-show-threads
4232             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4233           (gnus-summary-prepare-unthreaded thread))
4234         (setq data (nreverse gnus-newsgroup-data))
4235         (setq threads gnus-newsgroup-threads))
4236       ;; We splice the new data into the data structure.
4237       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4238       ;;!!! then we want to insert at the beginning of the buffer.
4239       ;;!!! That happens to be true with Gnus now, but that may
4240       ;;!!! change in the future.  Perhaps.
4241       (gnus-data-enter-list
4242        (if line nil current) data (- (point) old-pos))
4243       (setq gnus-newsgroup-threads
4244             (nconc threads gnus-newsgroup-threads))
4245       (gnus-data-compute-positions))))
4246
4247 (defun gnus-number-to-header (number)
4248   "Return the header for article NUMBER."
4249   (let ((headers gnus-newsgroup-headers))
4250     (while (and headers
4251                 (not (= number (mail-header-number (car headers)))))
4252       (pop headers))
4253     (when headers
4254       (car headers))))
4255
4256 (defun gnus-parent-headers (in-headers &optional generation)
4257   "Return the headers of the GENERATIONeth parent of HEADERS."
4258   (unless generation
4259     (setq generation 1))
4260   (let ((parent t)
4261         (headers in-headers)
4262         references)
4263     (while (and parent
4264                 (not (zerop generation))
4265                 (setq references (mail-header-references headers)))
4266       (setq headers (if (and references
4267                              (setq parent (gnus-parent-id references)))
4268                         (car (gnus-id-to-thread parent))
4269                       nil))
4270       (decf generation))
4271     (and (not (eq headers in-headers))
4272          headers)))
4273
4274 (defun gnus-id-to-thread (id)
4275   "Return the (sub-)thread where ID appears."
4276   (gnus-gethash id gnus-newsgroup-dependencies))
4277
4278 (defun gnus-id-to-article (id)
4279   "Return the article number of ID."
4280   (let ((thread (gnus-id-to-thread id)))
4281     (when (and thread
4282                (car thread))
4283       (mail-header-number (car thread)))))
4284
4285 (defun gnus-id-to-header (id)
4286   "Return the article headers of ID."
4287   (car (gnus-id-to-thread id)))
4288
4289 (defun gnus-article-displayed-root-p (article)
4290   "Say whether ARTICLE is a root(ish) article."
4291   (let ((level (gnus-summary-thread-level article))
4292         (refs (mail-header-references  (gnus-summary-article-header article)))
4293         particle)
4294     (cond
4295      ((null level) nil)
4296      ((zerop level) t)
4297      ((null refs) t)
4298      ((null (gnus-parent-id refs)) t)
4299      ((and (= 1 level)
4300            (null (setq particle (gnus-id-to-article
4301                                  (gnus-parent-id refs))))
4302            (null (gnus-summary-thread-level particle)))))))
4303
4304 (defun gnus-root-id (id)
4305   "Return the id of the root of the thread where ID appears."
4306   (let (last-id prev)
4307     (while (and id (setq prev (car (gnus-id-to-thread id))))
4308       (setq last-id id
4309             id (gnus-parent-id (mail-header-references prev))))
4310     last-id))
4311
4312 (defun gnus-articles-in-thread (thread)
4313   "Return the list of articles in THREAD."
4314   (cons (mail-header-number (car thread))
4315         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4316
4317 (defun gnus-remove-thread (id &optional dont-remove)
4318   "Remove the thread that has ID in it."
4319   (let (headers thread last-id)
4320     ;; First go up in this thread until we find the root.
4321     (setq last-id (gnus-root-id id)
4322           headers (message-flatten-list (gnus-id-to-thread last-id)))
4323     ;; We have now found the real root of this thread.  It might have
4324     ;; been gathered into some loose thread, so we have to search
4325     ;; through the threads to find the thread we wanted.
4326     (let ((threads gnus-newsgroup-threads)
4327           sub)
4328       (while threads
4329         (setq sub (car threads))
4330         (if (stringp (car sub))
4331             ;; This is a gathered thread, so we look at the roots
4332             ;; below it to find whether this article is in this
4333             ;; gathered root.
4334             (progn
4335               (setq sub (cdr sub))
4336               (while sub
4337                 (when (member (caar sub) headers)
4338                   (setq thread (car threads)
4339                         threads nil
4340                         sub nil))
4341                 (setq sub (cdr sub))))
4342           ;; It's an ordinary thread, so we check it.
4343           (when (eq (car sub) (car headers))
4344             (setq thread sub
4345                   threads nil)))
4346         (setq threads (cdr threads)))
4347       ;; If this article is in no thread, then it's a root.
4348       (if thread
4349           (unless dont-remove
4350             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4351         (setq thread (gnus-id-to-thread last-id)))
4352       (when thread
4353         (prog1
4354             thread                      ; We return this thread.
4355           (unless dont-remove
4356             (if (stringp (car thread))
4357                 (progn
4358                   ;; If we use dummy roots, then we have to remove the
4359                   ;; dummy root as well.
4360                   (when (eq gnus-summary-make-false-root 'dummy)
4361                     ;; We go to the dummy root by going to
4362                     ;; the first sub-"thread", and then one line up.
4363                     (gnus-summary-goto-article
4364                      (mail-header-number (caadr thread)))
4365                     (forward-line -1)
4366                     (gnus-delete-line)
4367                     (gnus-data-compute-positions))
4368                   (setq thread (cdr thread))
4369                   (while thread
4370                     (gnus-remove-thread-1 (car thread))
4371                     (setq thread (cdr thread))))
4372               (gnus-remove-thread-1 thread))))))))
4373
4374 (defun gnus-remove-thread-1 (thread)
4375   "Remove the thread THREAD recursively."
4376   (let ((number (mail-header-number (pop thread)))
4377         d)
4378     (setq thread (reverse thread))
4379     (while thread
4380       (gnus-remove-thread-1 (pop thread)))
4381     (when (setq d (gnus-data-find number))
4382       (goto-char (gnus-data-pos d))
4383       (gnus-summary-show-thread)
4384       (gnus-data-remove
4385        number
4386        (- (point-at-bol)
4387           (prog1
4388               (1+ (point-at-eol))
4389             (gnus-delete-line)))))))
4390
4391 (defun gnus-sort-threads-1 (threads func)
4392   (sort (mapcar (lambda (thread)
4393                   (cons (car thread)
4394                         (and (cdr thread)
4395                              (gnus-sort-threads-1 (cdr thread) func))))
4396                 threads) func))
4397
4398 (defun gnus-sort-threads (threads)
4399   "Sort THREADS."
4400   (if (not gnus-thread-sort-functions)
4401       threads
4402     (gnus-message 8 "Sorting threads...")
4403     (let ((max-lisp-eval-depth 5000))
4404       (prog1 (gnus-sort-threads-1
4405          threads
4406          (gnus-make-sort-function gnus-thread-sort-functions))
4407         (gnus-message 8 "Sorting threads...done")))))
4408
4409 (defun gnus-sort-articles (articles)
4410   "Sort ARTICLES."
4411   (when gnus-article-sort-functions
4412     (gnus-message 7 "Sorting articles...")
4413     (prog1
4414         (setq gnus-newsgroup-headers
4415               (sort articles (gnus-make-sort-function
4416                               gnus-article-sort-functions)))
4417       (gnus-message 7 "Sorting articles...done"))))
4418
4419 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4420 (defmacro gnus-thread-header (thread)
4421   "Return header of first article in THREAD.
4422 Note that THREAD must never, ever be anything else than a variable -
4423 using some other form will lead to serious barfage."
4424   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4425   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4426   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4427         (vector thread) 2))
4428
4429 (defsubst gnus-article-sort-by-number (h1 h2)
4430   "Sort articles by article number."
4431   (< (mail-header-number h1)
4432      (mail-header-number h2)))
4433
4434 (defun gnus-thread-sort-by-number (h1 h2)
4435   "Sort threads by root article number."
4436   (gnus-article-sort-by-number
4437    (gnus-thread-header h1) (gnus-thread-header h2)))
4438
4439 (defsubst gnus-article-sort-by-random (h1 h2)
4440   "Sort articles by article number."
4441   (zerop (random 2)))
4442
4443 (defun gnus-thread-sort-by-random (h1 h2)
4444   "Sort threads by root article number."
4445   (gnus-article-sort-by-random
4446    (gnus-thread-header h1) (gnus-thread-header h2)))
4447
4448 (defsubst gnus-article-sort-by-lines (h1 h2)
4449   "Sort articles by article Lines header."
4450   (< (mail-header-lines h1)
4451      (mail-header-lines h2)))
4452
4453 (defun gnus-thread-sort-by-lines (h1 h2)
4454   "Sort threads by root article Lines header."
4455   (gnus-article-sort-by-lines
4456    (gnus-thread-header h1) (gnus-thread-header h2)))
4457
4458 (defsubst gnus-article-sort-by-chars (h1 h2)
4459   "Sort articles by octet length."
4460   (< (mail-header-chars h1)
4461      (mail-header-chars h2)))
4462
4463 (defun gnus-thread-sort-by-chars (h1 h2)
4464   "Sort threads by root article octet length."
4465   (gnus-article-sort-by-chars
4466    (gnus-thread-header h1) (gnus-thread-header h2)))
4467
4468 (defsubst gnus-article-sort-by-author (h1 h2)
4469   "Sort articles by root author."
4470   (string-lessp
4471    (let ((extract (funcall
4472                    gnus-extract-address-components
4473                    (mail-header-from h1))))
4474      (or (car extract) (cadr extract) ""))
4475    (let ((extract (funcall
4476                    gnus-extract-address-components
4477                    (mail-header-from h2))))
4478      (or (car extract) (cadr extract) ""))))
4479
4480 (defun gnus-thread-sort-by-author (h1 h2)
4481   "Sort threads by root author."
4482   (gnus-article-sort-by-author
4483    (gnus-thread-header h1)  (gnus-thread-header h2)))
4484
4485 (defsubst gnus-article-sort-by-subject (h1 h2)
4486   "Sort articles by root subject."
4487   (string-lessp
4488    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4489    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4490
4491 (defun gnus-thread-sort-by-subject (h1 h2)
4492   "Sort threads by root subject."
4493   (gnus-article-sort-by-subject
4494    (gnus-thread-header h1) (gnus-thread-header h2)))
4495
4496 (defsubst gnus-article-sort-by-date (h1 h2)
4497   "Sort articles by root article date."
4498   (time-less-p
4499    (gnus-date-get-time (mail-header-date h1))
4500    (gnus-date-get-time (mail-header-date h2))))
4501
4502 (defun gnus-thread-sort-by-date (h1 h2)
4503   "Sort threads by root article date."
4504   (gnus-article-sort-by-date
4505    (gnus-thread-header h1) (gnus-thread-header h2)))
4506
4507 (defsubst gnus-article-sort-by-score (h1 h2)
4508   "Sort articles by root article score.
4509 Unscored articles will be counted as having a score of zero."
4510   (> (or (cdr (assq (mail-header-number h1)
4511                     gnus-newsgroup-scored))
4512          gnus-summary-default-score 0)
4513      (or (cdr (assq (mail-header-number h2)
4514                     gnus-newsgroup-scored))
4515          gnus-summary-default-score 0)))
4516
4517 (defun gnus-thread-sort-by-score (h1 h2)
4518   "Sort threads by root article score."
4519   (gnus-article-sort-by-score
4520    (gnus-thread-header h1) (gnus-thread-header h2)))
4521
4522 (defun gnus-thread-sort-by-total-score (h1 h2)
4523   "Sort threads by the sum of all scores in the thread.
4524 Unscored articles will be counted as having a score of zero."
4525   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4526
4527 (defun gnus-thread-total-score (thread)
4528   ;; This function find the total score of THREAD.
4529   (cond
4530    ((null thread)
4531     0)
4532    ((consp thread)
4533     (if (stringp (car thread))
4534         (apply gnus-thread-score-function 0
4535                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4536       (gnus-thread-total-score-1 thread)))
4537    (t
4538     (gnus-thread-total-score-1 (list thread)))))
4539
4540 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4541   "Sort threads such that the thread with the most recently arrived article comes first."
4542   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4543
4544 (defun gnus-thread-highest-number (thread)
4545   "Return the highest article number in THREAD."
4546   (apply 'max (mapcar (lambda (header)
4547                         (mail-header-number header))
4548                       (message-flatten-list thread))))
4549
4550 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4551   "Sort threads such that the thread with the most recently dated article comes first."
4552   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4553
4554 (defun gnus-thread-latest-date (thread)
4555   "Return the highest article date in THREAD."
4556   (let ((previous-time 0))
4557     (apply 'max
4558            (mapcar
4559             (lambda (header)
4560               (setq previous-time
4561                     (condition-case ()
4562                         (time-to-seconds (mail-header-parse-date
4563                                           (mail-header-date header)))
4564                       (error previous-time))))
4565             (sort
4566              (message-flatten-list thread)
4567              (lambda (h1 h2)
4568                (< (mail-header-number h1)
4569                   (mail-header-number h2))))))))
4570
4571 (defun gnus-thread-total-score-1 (root)
4572   ;; This function find the total score of the thread below ROOT.
4573   (setq root (car root))
4574   (apply gnus-thread-score-function
4575          (or (append
4576               (mapcar 'gnus-thread-total-score
4577                       (cdr (gnus-id-to-thread (mail-header-id root))))
4578               (when (> (mail-header-number root) 0)
4579                 (list (or (cdr (assq (mail-header-number root)
4580                                      gnus-newsgroup-scored))
4581                           gnus-summary-default-score 0))))
4582              (list gnus-summary-default-score)
4583              '(0))))
4584
4585 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4586 (defvar gnus-tmp-prev-subject nil)
4587 (defvar gnus-tmp-false-parent nil)
4588 (defvar gnus-tmp-root-expunged nil)
4589 (defvar gnus-tmp-dummy-line nil)
4590
4591 (eval-when-compile (defvar gnus-tmp-header))
4592 (defun gnus-extra-header (type &optional header)
4593   "Return the extra header of TYPE."
4594   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4595       ""))
4596
4597 (defvar gnus-tmp-thread-tree-header-string "")
4598
4599 (defcustom gnus-sum-thread-tree-root "> "
4600   "With %B spec, used for the root of a thread.
4601 If nil, use subject instead."
4602   :type '(radio (const :format "%v  " nil) (string :size 0))
4603   :group 'gnus-thread)
4604 (defcustom gnus-sum-thread-tree-false-root "> "
4605   "With %B spec, used for a false root of a thread.
4606 If nil, use subject instead."
4607   :type '(radio (const :format "%v  " nil) (string :size 0))
4608   :group 'gnus-thread)
4609 (defcustom gnus-sum-thread-tree-single-indent ""
4610   "With %B spec, used for a thread with just one message.
4611 If nil, use subject instead."
4612   :type '(radio (const :format "%v  " nil) (string :size 0))
4613   :group 'gnus-thread)
4614 (defcustom gnus-sum-thread-tree-vertical "| "
4615   "With %B spec, used for drawing a vertical line."
4616   :type 'string
4617   :group 'gnus-thread)
4618 (defcustom gnus-sum-thread-tree-indent "  "
4619   "With %B spec, used for indenting."
4620   :type 'string
4621   :group 'gnus-thread)
4622 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4623   "With %B spec, used for a leaf with brothers."
4624   :type 'string
4625   :group 'gnus-thread)
4626 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4627   "With %B spec, used for a leaf without brothers."
4628   :type 'string
4629   :group 'gnus-thread)
4630
4631 (defun gnus-summary-prepare-threads (threads)
4632   "Prepare summary buffer from THREADS and indentation LEVEL.
4633 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4634 or a straight list of headers."
4635   (gnus-message 7 "Generating summary...")
4636
4637   (setq gnus-newsgroup-threads threads)
4638   (beginning-of-line)
4639
4640   (let ((gnus-tmp-level 0)
4641         (default-score (or gnus-summary-default-score 0))
4642         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4643         (building-line-count gnus-summary-display-while-building)
4644         (building-count (integerp gnus-summary-display-while-building))
4645         thread number subject stack state gnus-tmp-gathered beg-match
4646         new-roots gnus-tmp-new-adopts thread-end simp-subject
4647         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4648         gnus-tmp-replied gnus-tmp-subject-or-nil
4649         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4650         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4651         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4652         tree-stack)
4653
4654     (setq gnus-tmp-prev-subject nil
4655           gnus-tmp-thread-tree-header-string "")
4656
4657     (if (vectorp (car threads))
4658         ;; If this is a straight (sic) list of headers, then a
4659         ;; threaded summary display isn't required, so we just create
4660         ;; an unthreaded one.
4661         (gnus-summary-prepare-unthreaded threads)
4662
4663       ;; Do the threaded display.
4664
4665       (if gnus-summary-display-while-building
4666           (switch-to-buffer (buffer-name)))
4667       (while (or threads stack gnus-tmp-new-adopts new-roots)
4668
4669         (if (and (= gnus-tmp-level 0)
4670                  (or (not stack)
4671                      (= (caar stack) 0))
4672                  (not gnus-tmp-false-parent)
4673                  (or gnus-tmp-new-adopts new-roots))
4674             (if gnus-tmp-new-adopts
4675                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4676                       thread (list (car gnus-tmp-new-adopts))
4677                       gnus-tmp-header (caar thread)
4678                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4679               (when new-roots
4680                 (setq thread (list (car new-roots))
4681                       gnus-tmp-header (caar thread)
4682                       new-roots (cdr new-roots))))
4683
4684           (if threads
4685               ;; If there are some threads, we do them before the
4686               ;; threads on the stack.
4687               (setq thread threads
4688                     gnus-tmp-header (caar thread))
4689             ;; There were no current threads, so we pop something off
4690             ;; the stack.
4691             (setq state (car stack)
4692                   gnus-tmp-level (car state)
4693                   tree-stack (cadr state)
4694                   thread (caddr state)
4695                   stack (cdr stack)
4696                   gnus-tmp-header (caar thread))))
4697
4698         (setq gnus-tmp-false-parent nil)
4699         (setq gnus-tmp-root-expunged nil)
4700         (setq thread-end nil)
4701
4702         (if (stringp gnus-tmp-header)
4703             ;; The header is a dummy root.
4704             (cond
4705              ((eq gnus-summary-make-false-root 'adopt)
4706               ;; We let the first article adopt the rest.
4707               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4708                                                (cddar thread)))
4709               (setq gnus-tmp-gathered
4710                     (nconc (mapcar
4711                             (lambda (h) (mail-header-number (car h)))
4712                             (cddar thread))
4713                            gnus-tmp-gathered))
4714               (setq thread (cons (list (caar thread)
4715                                        (cadar thread))
4716                                  (cdr thread)))
4717               (setq gnus-tmp-level -1
4718                     gnus-tmp-false-parent t))
4719              ((eq gnus-summary-make-false-root 'empty)
4720               ;; We print adopted articles with empty subject fields.
4721               (setq gnus-tmp-gathered
4722                     (nconc (mapcar
4723                             (lambda (h) (mail-header-number (car h)))
4724                             (cddar thread))
4725                            gnus-tmp-gathered))
4726               (setq gnus-tmp-level -1))
4727              ((eq gnus-summary-make-false-root 'dummy)
4728               ;; We remember that we probably want to output a dummy
4729               ;; root.
4730               (setq gnus-tmp-dummy-line gnus-tmp-header)
4731               (setq gnus-tmp-prev-subject gnus-tmp-header))
4732              (t
4733               ;; We do not make a root for the gathered
4734               ;; sub-threads at all.
4735               (setq gnus-tmp-level -1)))
4736
4737           (setq number (mail-header-number gnus-tmp-header)
4738                 subject (mail-header-subject gnus-tmp-header)
4739                 simp-subject (gnus-simplify-subject-fully subject))
4740
4741           (cond
4742            ;; If the thread has changed subject, we might want to make
4743            ;; this subthread into a root.
4744            ((and (null gnus-thread-ignore-subject)
4745                  (not (zerop gnus-tmp-level))
4746                  gnus-tmp-prev-subject
4747                  (not (string= gnus-tmp-prev-subject simp-subject)))
4748             (setq new-roots (nconc new-roots (list (car thread)))
4749                   thread-end t
4750                   gnus-tmp-header nil))
4751            ;; If the article lies outside the current limit,
4752            ;; then we do not display it.
4753            ((not (memq number gnus-newsgroup-limit))
4754             (setq gnus-tmp-gathered
4755                   (nconc (mapcar
4756                           (lambda (h) (mail-header-number (car h)))
4757                           (cdar thread))
4758                          gnus-tmp-gathered))
4759             (setq gnus-tmp-new-adopts (if (cdar thread)
4760                                           (append gnus-tmp-new-adopts
4761                                                   (cdar thread))
4762                                         gnus-tmp-new-adopts)
4763                   thread-end t
4764                   gnus-tmp-header nil)
4765             (when (zerop gnus-tmp-level)
4766               (setq gnus-tmp-root-expunged t)))
4767            ;; Perhaps this article is to be marked as read?
4768            ((and gnus-summary-mark-below
4769                  (< (or (cdr (assq number gnus-newsgroup-scored))
4770                         default-score)
4771                     gnus-summary-mark-below)
4772                  ;; Don't touch sparse articles.
4773                  (not (gnus-summary-article-sparse-p number))
4774                  (not (gnus-summary-article-ancient-p number)))
4775             (setq gnus-newsgroup-unreads
4776                   (delq number gnus-newsgroup-unreads))
4777             (if gnus-newsgroup-auto-expire
4778                 (setq gnus-newsgroup-expirable
4779                       (gnus-add-to-sorted-list
4780                        gnus-newsgroup-expirable number))
4781               (push (cons number gnus-low-score-mark)
4782                     gnus-newsgroup-reads))))
4783
4784           (when gnus-tmp-header
4785             ;; We may have an old dummy line to output before this
4786             ;; article.
4787             (when (and gnus-tmp-dummy-line
4788                        (gnus-subject-equal
4789                         gnus-tmp-dummy-line
4790                         (mail-header-subject gnus-tmp-header)))
4791               (gnus-summary-insert-dummy-line
4792                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4793               (setq gnus-tmp-dummy-line nil))
4794
4795             ;; Compute the mark.
4796             (setq gnus-tmp-unread (gnus-article-mark number))
4797
4798             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4799                                   gnus-tmp-header gnus-tmp-level)
4800                   gnus-newsgroup-data)
4801
4802             ;; Actually insert the line.
4803             (setq
4804              gnus-tmp-subject-or-nil
4805              (cond
4806               ((and gnus-thread-ignore-subject
4807                     gnus-tmp-prev-subject
4808                     (not (string= gnus-tmp-prev-subject simp-subject)))
4809                subject)
4810               ((zerop gnus-tmp-level)
4811                (if (and (eq gnus-summary-make-false-root 'empty)
4812                         (memq number gnus-tmp-gathered)
4813                         gnus-tmp-prev-subject
4814                         (string= gnus-tmp-prev-subject simp-subject))
4815                    gnus-summary-same-subject
4816                  subject))
4817               (t gnus-summary-same-subject)))
4818             (if (and (eq gnus-summary-make-false-root 'adopt)
4819                      (= gnus-tmp-level 1)
4820                      (memq number gnus-tmp-gathered))
4821                 (setq gnus-tmp-opening-bracket ?\<
4822                       gnus-tmp-closing-bracket ?\>)
4823               (setq gnus-tmp-opening-bracket ?\[
4824                     gnus-tmp-closing-bracket ?\]))
4825             (setq
4826              gnus-tmp-indentation
4827              (aref gnus-thread-indent-array gnus-tmp-level)
4828              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4829              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4830                                 gnus-summary-default-score 0)
4831              gnus-tmp-score-char
4832              (if (or (null gnus-summary-default-score)
4833                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4834                          gnus-summary-zcore-fuzz))
4835                  ?                      ;Whitespace
4836                (if (< gnus-tmp-score gnus-summary-default-score)
4837                    gnus-score-below-mark gnus-score-over-mark))
4838              gnus-tmp-replied
4839              (cond ((memq number gnus-newsgroup-processable)
4840                     gnus-process-mark)
4841                    ((memq number gnus-newsgroup-cached)
4842                     gnus-cached-mark)
4843                    ((memq number gnus-newsgroup-replied)
4844                     gnus-replied-mark)
4845                    ((memq number gnus-newsgroup-forwarded)
4846                     gnus-forwarded-mark)
4847                    ((memq number gnus-newsgroup-saved)
4848                     gnus-saved-mark)
4849                    ((memq number gnus-newsgroup-recent)
4850                     gnus-recent-mark)
4851                    ((memq number gnus-newsgroup-unseen)
4852                     gnus-unseen-mark)
4853                    (t gnus-no-mark))
4854              gnus-tmp-downloaded
4855              (cond ((memq number gnus-newsgroup-undownloaded)
4856                     gnus-undownloaded-mark)
4857                    (gnus-newsgroup-agentized
4858                     gnus-downloaded-mark)
4859                    (t
4860                     gnus-no-mark))
4861              gnus-tmp-from (mail-header-from gnus-tmp-header)
4862              gnus-tmp-name
4863              (cond
4864               ((string-match "<[^>]+> *$" gnus-tmp-from)
4865                (setq beg-match (match-beginning 0))
4866                (or (and (string-match "^\".+\"" gnus-tmp-from)
4867                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4868                    (substring gnus-tmp-from 0 beg-match)))
4869               ((string-match "(.+)" gnus-tmp-from)
4870                (substring gnus-tmp-from
4871                           (1+ (match-beginning 0)) (1- (match-end 0))))
4872               (t gnus-tmp-from))
4873
4874              ;; Do the %B string
4875              gnus-tmp-thread-tree-header-string
4876              (cond
4877               ((not gnus-show-threads) "")
4878               ((zerop gnus-tmp-level)
4879                (cond ((cdar thread)
4880                       (or gnus-sum-thread-tree-root subject))
4881                      (gnus-tmp-new-adopts
4882                       (or gnus-sum-thread-tree-false-root subject))
4883                      (t
4884                       (or gnus-sum-thread-tree-single-indent subject))))
4885               (t
4886                (concat (apply 'concat
4887                               (mapcar (lambda (item)
4888                                         (if (= item 1)
4889                                             gnus-sum-thread-tree-vertical
4890                                           gnus-sum-thread-tree-indent))
4891                                       (cdr (reverse tree-stack))))
4892                        (if (nth 1 thread)
4893                            gnus-sum-thread-tree-leaf-with-other
4894                          gnus-sum-thread-tree-single-leaf)))))
4895             (when (string= gnus-tmp-name "")
4896               (setq gnus-tmp-name gnus-tmp-from))
4897             (unless (numberp gnus-tmp-lines)
4898               (setq gnus-tmp-lines -1))
4899             (if (= gnus-tmp-lines -1)
4900                 (setq gnus-tmp-lines "?")
4901               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4902               (gnus-put-text-property
4903              (point)
4904              (progn (eval gnus-summary-line-format-spec) (point))
4905                'gnus-number number)
4906             (when gnus-visual-p
4907               (forward-line -1)
4908               (gnus-run-hooks 'gnus-summary-update-hook)
4909               (forward-line 1))
4910
4911             (setq gnus-tmp-prev-subject simp-subject)))
4912
4913         (when (nth 1 thread)
4914           (push (list (max 0 gnus-tmp-level)
4915                       (copy-sequence tree-stack)
4916                       (nthcdr 1 thread))
4917                 stack))
4918         (push (if (nth 1 thread) 1 0) tree-stack)
4919         (incf gnus-tmp-level)
4920         (setq threads (if thread-end nil (cdar thread)))
4921         (if gnus-summary-display-while-building
4922             (if building-count
4923                 (progn
4924                   ;; use a set frequency
4925                   (setq building-line-count (1- building-line-count))
4926                   (when (= building-line-count 0)
4927                     (sit-for 0)
4928                     (setq building-line-count
4929                           gnus-summary-display-while-building)))
4930               ;; always
4931               (sit-for 0)))
4932         (unless threads
4933           (setq gnus-tmp-level 0)))))
4934   (gnus-message 7 "Generating summary...done"))
4935
4936 (defun gnus-summary-prepare-unthreaded (headers)
4937   "Generate an unthreaded summary buffer based on HEADERS."
4938   (let (header number mark)
4939
4940     (beginning-of-line)
4941
4942     (while headers
4943       ;; We may have to root out some bad articles...
4944       (when (memq (setq number (mail-header-number
4945                                 (setq header (pop headers))))
4946                   gnus-newsgroup-limit)
4947         ;; Mark article as read when it has a low score.
4948         (when (and gnus-summary-mark-below
4949                    (< (or (cdr (assq number gnus-newsgroup-scored))
4950                           gnus-summary-default-score 0)
4951                       gnus-summary-mark-below)
4952                    (not (gnus-summary-article-ancient-p number)))
4953           (setq gnus-newsgroup-unreads
4954                 (delq number gnus-newsgroup-unreads))
4955           (if gnus-newsgroup-auto-expire
4956               (push number gnus-newsgroup-expirable)
4957             (push (cons number gnus-low-score-mark)
4958                   gnus-newsgroup-reads)))
4959
4960         (setq mark (gnus-article-mark number))
4961         (push (gnus-data-make number mark (1+ (point)) header 0)
4962               gnus-newsgroup-data)
4963         (gnus-summary-insert-line
4964          header 0 number
4965          (memq number gnus-newsgroup-undownloaded)
4966          mark (memq number gnus-newsgroup-replied)
4967          (memq number gnus-newsgroup-expirable)
4968          (mail-header-subject header) nil
4969          (cdr (assq number gnus-newsgroup-scored))
4970          (memq number gnus-newsgroup-processable))))))
4971
4972 (defun gnus-summary-remove-list-identifiers ()
4973   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4974   (let ((regexp (if (consp gnus-list-identifiers)
4975                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4976                   gnus-list-identifiers))
4977         changed subject)
4978     (when regexp
4979       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
4980       (dolist (header gnus-newsgroup-headers)
4981         (setq subject (mail-header-subject header)
4982               changed nil)
4983         (while (string-match regexp subject)
4984           (setq subject
4985                 (concat (substring subject 0 (match-beginning 1))
4986                         (substring subject (match-end 0)))
4987                 changed t))
4988         (when changed
4989           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
4990             (setq subject
4991                   (concat (substring subject 0 (match-beginning 1))
4992                           (substring subject (match-end 1)))))
4993           (mail-header-set-subject header subject))))))
4994
4995 (defun gnus-fetch-headers (articles)
4996   "Fetch headers of ARTICLES."
4997   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4998     (gnus-message 5 "Fetching headers for %s..." name)
4999     (prog1
5000         (if (eq 'nov
5001                 (setq gnus-headers-retrieved-by
5002                       (gnus-retrieve-headers
5003                        articles gnus-newsgroup-name
5004                        ;; We might want to fetch old headers, but
5005                        ;; not if there is only 1 article.
5006                        (and (or (and
5007                                  (not (eq gnus-fetch-old-headers 'some))
5008                                  (not (numberp gnus-fetch-old-headers)))
5009                                 (> (length articles) 1))
5010                             gnus-fetch-old-headers))))
5011             (gnus-get-newsgroup-headers-xover
5012              articles nil nil gnus-newsgroup-name t)
5013           (gnus-get-newsgroup-headers))
5014       (gnus-message 5 "Fetching headers for %s...done" name))))
5015
5016 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5017   "Select newsgroup GROUP.
5018 If READ-ALL is non-nil, all articles in the group are selected.
5019 If SELECT-ARTICLES, only select those articles from GROUP."
5020   (let* ((entry (gnus-group-entry group))
5021          ;;!!! Dirty hack; should be removed.
5022          (gnus-summary-ignore-duplicates
5023           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5024               t
5025             gnus-summary-ignore-duplicates))
5026          (info (nth 2 entry))
5027          articles fetched-articles cached)
5028
5029     (unless (gnus-check-server
5030              (set (make-local-variable 'gnus-current-select-method)
5031                   (gnus-find-method-for-group group)))
5032       (error "Couldn't open server"))
5033
5034     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5035         (gnus-activate-group group)     ; Or we can activate it...
5036         (progn                          ; Or we bug out.
5037           (when (equal major-mode 'gnus-summary-mode)
5038             (gnus-kill-buffer (current-buffer)))
5039           (error "Couldn't activate group %s: %s"
5040                  group (gnus-status-message group))))
5041
5042     (unless (gnus-request-group group t)
5043       (when (equal major-mode 'gnus-summary-mode)
5044         (gnus-kill-buffer (current-buffer)))
5045       (error "Couldn't request group %s: %s"
5046              group (gnus-status-message group)))
5047
5048     (when gnus-agent
5049       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5050       
5051       (setq gnus-summary-use-undownloaded-faces
5052             (gnus-agent-find-parameter
5053              group
5054              'agent-enable-undownloaded-faces)))
5055
5056     (setq gnus-newsgroup-name group
5057           gnus-newsgroup-unselected nil
5058           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5059
5060     (let ((display (gnus-group-find-parameter group 'display)))
5061       (setq gnus-newsgroup-display
5062             (cond
5063              ((not (zerop (or (car-safe read-all) 0)))
5064               ;; The user entered the group with C-u SPC/RET, let's show
5065               ;; all articles.
5066               'gnus-not-ignore)
5067              ((eq display 'all)
5068               'gnus-not-ignore)
5069              ((arrayp display)
5070               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5071              ((numberp display)
5072               ;; The following is probably the "correct" solution, but
5073               ;; it makes Gnus fetch all headers and then limit the
5074               ;; articles (which is slow), so instead we hack the
5075               ;; select-articles parameter instead. -- Simon Josefsson
5076               ;; <jas@kth.se>
5077               ;;
5078               ;; (gnus-byte-compile
5079               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5080               ;;                         display)))))
5081               (setq select-articles
5082                     (gnus-uncompress-range
5083                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5084                              (if (> tmp 0)
5085                                  tmp
5086                                1))
5087                            (cdr (gnus-active group)))))
5088               nil)
5089              (t
5090               nil))))
5091
5092     (gnus-summary-setup-default-charset)
5093
5094     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5095     (when (gnus-virtual-group-p group)
5096       (setq cached gnus-newsgroup-cached))
5097
5098     (setq gnus-newsgroup-unreads
5099           (gnus-sorted-ndifference
5100            (gnus-sorted-ndifference gnus-newsgroup-unreads
5101                                     gnus-newsgroup-marked)
5102            gnus-newsgroup-dormant))
5103
5104     (setq gnus-newsgroup-processable nil)
5105
5106     (gnus-update-read-articles group gnus-newsgroup-unreads)
5107
5108     ;; Adjust and set lists of article marks.
5109     (when info
5110       (gnus-adjust-marked-articles info))
5111     (if (setq articles select-articles)
5112         (setq gnus-newsgroup-unselected
5113               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5114       (setq articles (gnus-articles-to-read group read-all)))
5115
5116     (cond
5117      ((null articles)
5118       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5119       'quit)
5120      ((eq articles 0) nil)
5121      (t
5122       ;; Init the dependencies hash table.
5123       (setq gnus-newsgroup-dependencies
5124             (gnus-make-hashtable (length articles)))
5125       (gnus-set-global-variables)
5126       ;; Retrieve the headers and read them in.
5127
5128       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5129
5130       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5131       (when cached
5132         (setq gnus-newsgroup-cached cached))
5133
5134       ;; Suppress duplicates?
5135       (when gnus-suppress-duplicates
5136         (gnus-dup-suppress-articles))
5137
5138       ;; Set the initial limit.
5139       (setq gnus-newsgroup-limit (copy-sequence articles))
5140       ;; Remove canceled articles from the list of unread articles.
5141       (setq fetched-articles
5142             (mapcar (lambda (headers) (mail-header-number headers))
5143                     gnus-newsgroup-headers))
5144       (setq gnus-newsgroup-articles fetched-articles)
5145       (setq gnus-newsgroup-unreads
5146             (gnus-sorted-nintersection
5147              gnus-newsgroup-unreads fetched-articles))
5148       (gnus-compute-unseen-list)
5149
5150       ;; Removed marked articles that do not exist.
5151       (gnus-update-missing-marks
5152        (gnus-sorted-difference articles fetched-articles))
5153       ;; We might want to build some more threads first.
5154       (when (and gnus-fetch-old-headers
5155                  (eq gnus-headers-retrieved-by 'nov))
5156         (if (eq gnus-fetch-old-headers 'invisible)
5157             (gnus-build-all-threads)
5158           (gnus-build-old-threads)))
5159       ;; Let the Gnus agent mark articles as read.
5160       (when gnus-agent
5161         (gnus-agent-get-undownloaded-list))
5162       ;; Remove list identifiers from subject
5163       (when gnus-list-identifiers
5164         (gnus-summary-remove-list-identifiers))
5165       ;; Check whether auto-expire is to be done in this group.
5166       (setq gnus-newsgroup-auto-expire
5167             (gnus-group-auto-expirable-p group))
5168       ;; Set up the article buffer now, if necessary.
5169       (unless gnus-single-article-buffer
5170         (gnus-article-setup-buffer))
5171       ;; First and last article in this newsgroup.
5172       (when gnus-newsgroup-headers
5173         (setq gnus-newsgroup-begin
5174               (mail-header-number (car gnus-newsgroup-headers))
5175               gnus-newsgroup-end
5176               (mail-header-number
5177                (gnus-last-element gnus-newsgroup-headers))))
5178       ;; GROUP is successfully selected.
5179       (or gnus-newsgroup-headers t)))))
5180
5181 (defun gnus-compute-unseen-list ()
5182   ;; The `seen' marks are treated specially.
5183   (if (not gnus-newsgroup-seen)
5184       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5185     (setq gnus-newsgroup-unseen
5186           (gnus-inverse-list-range-intersection
5187            gnus-newsgroup-articles gnus-newsgroup-seen))))
5188
5189 (defun gnus-summary-display-make-predicate (display)
5190   (require 'gnus-agent)
5191   (when (= (length display) 1)
5192     (setq display (car display)))
5193   (unless gnus-summary-display-cache
5194     (dolist (elem (append '((unread . unread)
5195                             (read . read)
5196                             (unseen . unseen))
5197                           gnus-article-mark-lists))
5198       (push (cons (cdr elem)
5199                   (gnus-byte-compile
5200                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5201             gnus-summary-display-cache)))
5202   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5203         (gnus-category-predicate-cache gnus-summary-display-cache))
5204     (gnus-get-predicate display)))
5205
5206 ;; Uses the dynamically bound `number' variable.
5207 (eval-when-compile
5208   (defvar number))
5209 (defun gnus-article-marked-p (type &optional article)
5210   (let ((article (or article number)))
5211     (cond
5212      ((eq type 'tick)
5213       (memq article gnus-newsgroup-marked))
5214      ((eq type 'spam)
5215       (memq article gnus-newsgroup-spam-marked))
5216      ((eq type 'unsend)
5217       (memq article gnus-newsgroup-unsendable))
5218      ((eq type 'undownload)
5219       (memq article gnus-newsgroup-undownloaded))
5220      ((eq type 'download)
5221       (memq article gnus-newsgroup-downloadable))
5222      ((eq type 'unread)
5223       (memq article gnus-newsgroup-unreads))
5224      ((eq type 'read)
5225       (memq article gnus-newsgroup-reads))
5226      ((eq type 'dormant)
5227       (memq article gnus-newsgroup-dormant) )
5228      ((eq type 'expire)
5229       (memq article gnus-newsgroup-expirable))
5230      ((eq type 'reply)
5231       (memq article gnus-newsgroup-replied))
5232      ((eq type 'killed)
5233       (memq article gnus-newsgroup-killed))
5234      ((eq type 'bookmark)
5235       (assq article gnus-newsgroup-bookmarks))
5236      ((eq type 'score)
5237       (assq article gnus-newsgroup-scored))
5238      ((eq type 'save)
5239       (memq article gnus-newsgroup-saved))
5240      ((eq type 'cache)
5241       (memq article gnus-newsgroup-cached))
5242      ((eq type 'forward)
5243       (memq article gnus-newsgroup-forwarded))
5244      ((eq type 'seen)
5245       (not (memq article gnus-newsgroup-unseen)))
5246      ((eq type 'recent)
5247       (memq article gnus-newsgroup-recent))
5248      (t t))))
5249
5250 (defun gnus-articles-to-read (group &optional read-all)
5251   "Find out what articles the user wants to read."
5252   (let* ((display (gnus-group-find-parameter group 'display))
5253          (articles
5254           ;; Select all articles if `read-all' is non-nil, or if there
5255           ;; are no unread articles.
5256           (if (or read-all
5257                   (and (zerop (length gnus-newsgroup-marked))
5258                        (zerop (length gnus-newsgroup-unreads)))
5259                   ;; Fetch all if the predicate is non-nil.
5260                   gnus-newsgroup-display)
5261               ;; We want to select the headers for all the articles in
5262               ;; the group, so we select either all the active
5263               ;; articles in the group, or (if that's nil), the
5264               ;; articles in the cache.
5265               (or
5266                (gnus-uncompress-range (gnus-active group))
5267                (gnus-cache-articles-in-group group))
5268             ;; Select only the "normal" subset of articles.
5269             (gnus-sorted-nunion
5270              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5271              gnus-newsgroup-unreads)))
5272          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5273          (scored (length scored-list))
5274          (number (length articles))
5275          (marked (+ (length gnus-newsgroup-marked)
5276                     (length gnus-newsgroup-dormant)))
5277          (select
5278           (cond
5279            ((numberp read-all)
5280             read-all)
5281            ((numberp gnus-newsgroup-display)
5282             gnus-newsgroup-display)
5283            (t
5284             (condition-case ()
5285                 (cond
5286                  ((and (or (<= scored marked) (= scored number))
5287                        (numberp gnus-large-newsgroup)
5288                        (> number gnus-large-newsgroup))
5289                   (let* ((cursor-in-echo-area nil)
5290                          (initial (gnus-parameter-large-newsgroup-initial
5291                                    gnus-newsgroup-name))
5292                          (input
5293                           (read-string
5294                            (format
5295                             "How many articles from %s (%s %d): "
5296                             (gnus-limit-string
5297                              (gnus-group-decoded-name gnus-newsgroup-name)
5298                              35)
5299                             (if initial "max" "default")
5300                             number)
5301                            (if initial
5302                                (cons (number-to-string initial)
5303                                      0)))))
5304                     (if (string-match "^[ \t]*$" input) number input)))
5305                  ((and (> scored marked) (< scored number)
5306                        (> (- scored number) 20))
5307                   (let ((input
5308                          (read-string
5309                           (format "%s %s (%d scored, %d total): "
5310                                   "How many articles from"
5311                                   (gnus-group-decoded-name group)
5312                                   scored number))))
5313                     (if (string-match "^[ \t]*$" input)
5314                         number input)))
5315                  (t number))
5316               (quit
5317                (message "Quit getting the articles to read")
5318                nil))))))
5319     (setq select (if (stringp select) (string-to-number select) select))
5320     (if (or (null select) (zerop select))
5321         select
5322       (if (and (not (zerop scored)) (<= (abs select) scored))
5323           (progn
5324             (setq articles (sort scored-list '<))
5325             (setq number (length articles)))
5326         (setq articles (copy-sequence articles)))
5327
5328       (when (< (abs select) number)
5329         (if (< select 0)
5330             ;; Select the N oldest articles.
5331             (setcdr (nthcdr (1- (abs select)) articles) nil)
5332           ;; Select the N most recent articles.
5333           (setq articles (nthcdr (- number select) articles))))
5334       (setq gnus-newsgroup-unselected
5335             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5336       (when gnus-alter-articles-to-read-function
5337         (setq articles
5338               (sort
5339                (funcall gnus-alter-articles-to-read-function
5340                         gnus-newsgroup-name articles)
5341                '<)))
5342       articles)))
5343
5344 (defun gnus-killed-articles (killed articles)
5345   (let (out)
5346     (while articles
5347       (when (inline (gnus-member-of-range (car articles) killed))
5348         (push (car articles) out))
5349       (setq articles (cdr articles)))
5350     out))
5351
5352 (defun gnus-uncompress-marks (marks)
5353   "Uncompress the mark ranges in MARKS."
5354   (let ((uncompressed '(score bookmark))
5355         out)
5356     (while marks
5357       (if (memq (caar marks) uncompressed)
5358           (push (car marks) out)
5359         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5360       (setq marks (cdr marks)))
5361     out))
5362
5363 (defun gnus-article-mark-to-type (mark)
5364   "Return the type of MARK."
5365   (or (cadr (assq mark gnus-article-special-mark-lists))
5366       'list))
5367
5368 (defun gnus-article-unpropagatable-p (mark)
5369   "Return whether MARK should be propagated to back end."
5370   (memq mark gnus-article-unpropagated-mark-lists))
5371
5372 (defun gnus-adjust-marked-articles (info)
5373   "Set all article lists and remove all marks that are no longer valid."
5374   (let* ((marked-lists (gnus-info-marks info))
5375          (active (gnus-active (gnus-info-group info)))
5376          (min (car active))
5377          (max (cdr active))
5378          (types gnus-article-mark-lists)
5379          marks var articles article mark mark-type
5380          bgn end)
5381
5382     (dolist (marks marked-lists)
5383       (setq mark (car marks)
5384             mark-type (gnus-article-mark-to-type mark)
5385             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5386
5387       ;; We set the variable according to the type of the marks list,
5388       ;; and then adjust the marks to a subset of the active articles.
5389       (cond
5390        ;; Adjust "simple" lists - compressed yet unsorted
5391        ((eq mark-type 'list)
5392         ;; Simultaneously uncompress and clip to active range
5393         ;; See gnus-uncompress-range for a description of possible marks
5394         (let (l lh)
5395           (if (not (cadr marks))
5396               (set var nil)
5397             (setq articles (if (numberp (cddr marks))
5398                                (list (cdr marks))
5399                              (cdr marks))
5400                   lh (cons nil nil)
5401                   l lh)
5402
5403             (while (setq article (pop articles))
5404               (cond ((consp article)
5405                      (setq bgn (max (car article) min)
5406                            end (min (cdr article) max))
5407                      (while (<= bgn end)
5408                        (setq l (setcdr l (cons bgn nil))
5409                              bgn (1+ bgn))))
5410                     ((and (<= min article)
5411                           (>= max article))
5412                      (setq l (setcdr l (cons article nil))))))
5413             (set var (cdr lh)))))
5414        ;; Adjust assocs.
5415        ((eq mark-type 'tuple)
5416         (set var (setq articles (cdr marks)))
5417         (when (not (listp (cdr (symbol-value var))))
5418           (set var (list (symbol-value var))))
5419         (when (not (listp (cdr articles)))
5420           (setq articles (list articles)))
5421         (while articles
5422           (when (or (not (consp (setq article (pop articles))))
5423                     (< (car article) min)
5424                     (> (car article) max))
5425             (set var (delq article (symbol-value var))))))
5426        ;; Adjust ranges (sloppily).
5427        ((eq mark-type 'range)
5428         (cond
5429          ((eq mark 'seen)
5430           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5431           ;; It should be (seen (NUM1 . NUM2)).
5432           (when (numberp (cddr marks))
5433             (setcdr marks (list (cdr marks))))
5434           (setq articles (cdr marks))
5435           (while (and articles
5436                       (or (and (consp (car articles))
5437                                (> min (cdar articles)))
5438                           (and (numberp (car articles))
5439                                (> min (car articles)))))
5440             (pop articles))
5441           (set var articles))))))))
5442
5443 (defun gnus-update-missing-marks (missing)
5444   "Go through the list of MISSING articles and remove them from the mark lists."
5445   (when missing
5446     (let (var m)
5447       ;; Go through all types.
5448       (dolist (elem gnus-article-mark-lists)
5449         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5450           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5451           (when (symbol-value var)
5452             ;; This list has articles.  So we delete all missing
5453             ;; articles from it.
5454             (setq m missing)
5455             (while m
5456               (set var (delq (pop m) (symbol-value var))))))))))
5457
5458 (defun gnus-update-marks ()
5459   "Enter the various lists of marked articles into the newsgroup info list."
5460   (let ((types gnus-article-mark-lists)
5461         (info (gnus-get-info gnus-newsgroup-name))
5462         type list newmarked symbol delta-marks)
5463     (when info
5464       ;; Add all marks lists to the list of marks lists.
5465       (while (setq type (pop types))
5466         (setq list (symbol-value
5467                     (setq symbol
5468                           (intern (format "gnus-newsgroup-%s" (car type))))))
5469
5470         (when list
5471           ;; Get rid of the entries of the articles that have the
5472           ;; default score.
5473           (when (and (eq (cdr type) 'score)
5474                      gnus-save-score
5475                      list)
5476             (let* ((arts list)
5477                    (prev (cons nil list))
5478                    (all prev))
5479               (while arts
5480                 (if (or (not (consp (car arts)))
5481                         (= (cdar arts) gnus-summary-default-score))
5482                     (setcdr prev (cdr arts))
5483                   (setq prev arts))
5484                 (setq arts (cdr arts)))
5485               (setq list (cdr all)))))
5486
5487         (when (eq (cdr type) 'seen)
5488           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5489
5490         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5491           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5492
5493         (when (and (gnus-check-backend-function
5494                     'request-set-mark gnus-newsgroup-name)
5495                    (not (gnus-article-unpropagatable-p (cdr type))))
5496           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5497                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5498                  (add (gnus-remove-from-range
5499                        (gnus-copy-sequence list) old)))
5500             (when add
5501               (push (list add 'add (list (cdr type))) delta-marks))
5502             (when del
5503               (push (list del 'del (list (cdr type))) delta-marks))))
5504
5505         (when list
5506           (push (cons (cdr type) list) newmarked)))
5507
5508       (when delta-marks
5509         (unless (gnus-check-group gnus-newsgroup-name)
5510           (error "Can't open server for %s" gnus-newsgroup-name))
5511         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5512
5513       ;; Enter these new marks into the info of the group.
5514       (if (nthcdr 3 info)
5515           (setcar (nthcdr 3 info) newmarked)
5516         ;; Add the marks lists to the end of the info.
5517         (when newmarked
5518           (setcdr (nthcdr 2 info) (list newmarked))))
5519
5520       ;; Cut off the end of the info if there's nothing else there.
5521       (let ((i 5))
5522         (while (and (> i 2)
5523                     (not (nth i info)))
5524           (when (nthcdr (decf i) info)
5525             (setcdr (nthcdr i info) nil)))))))
5526
5527 (defun gnus-set-mode-line (where)
5528   "Set the mode line of the article or summary buffers.
5529 If WHERE is `summary', the summary mode line format will be used."
5530   ;; Is this mode line one we keep updated?
5531   (when (and (memq where gnus-updated-mode-lines)
5532              (symbol-value
5533               (intern (format "gnus-%s-mode-line-format-spec" where))))
5534     (let (mode-string)
5535       (save-excursion
5536         ;; We evaluate this in the summary buffer since these
5537         ;; variables are buffer-local to that buffer.
5538         (set-buffer gnus-summary-buffer)
5539        ;; We bind all these variables that are used in the `eval' form
5540         ;; below.
5541         (let* ((mformat (symbol-value
5542                          (intern
5543                           (format "gnus-%s-mode-line-format-spec" where))))
5544                (gnus-tmp-group-name (gnus-group-decoded-name
5545                                      gnus-newsgroup-name))
5546                (gnus-tmp-article-number (or gnus-current-article 0))
5547                (gnus-tmp-unread gnus-newsgroup-unreads)
5548                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5549                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5550                (gnus-tmp-unread-and-unselected
5551                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5552                             (zerop gnus-tmp-unselected))
5553                        "")
5554                       ((zerop gnus-tmp-unselected)
5555                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5556                       (t (format "{%d(+%d) more}"
5557                                  gnus-tmp-unread-and-unticked
5558                                  gnus-tmp-unselected))))
5559                (gnus-tmp-subject
5560                 (if (and gnus-current-headers
5561                          (vectorp gnus-current-headers))
5562                     (gnus-mode-string-quote
5563                      (mail-header-subject gnus-current-headers))
5564                   ""))
5565                bufname-length max-len
5566                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5567           (setq mode-string (eval mformat))
5568           (setq bufname-length (if (string-match "%b" mode-string)
5569                                    (- (length
5570                                        (buffer-name
5571                                         (if (eq where 'summary)
5572                                             nil
5573                                           (get-buffer gnus-article-buffer))))
5574                                       2)
5575                                  0))
5576           (setq max-len (max 4 (if gnus-mode-non-string-length
5577                                    (- (window-width)
5578                                       gnus-mode-non-string-length
5579                                       bufname-length)
5580                                  (length mode-string))))
5581           ;; We might have to chop a bit of the string off...
5582           (when (> (length mode-string) max-len)
5583             (setq mode-string
5584                   (concat (truncate-string-to-width mode-string (- max-len 3))
5585                           "...")))
5586           ;; Pad the mode string a bit.
5587           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5588       ;; Update the mode line.
5589       (setq mode-line-buffer-identification
5590             (gnus-mode-line-buffer-identification (list mode-string)))
5591       (set-buffer-modified-p t))))
5592
5593 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5594   "Go through the HEADERS list and add all Xrefs to a hash table.
5595 The resulting hash table is returned, or nil if no Xrefs were found."
5596   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5597          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5598          (xref-hashtb (gnus-make-hashtable))
5599          start group entry number xrefs header)
5600     (while headers
5601       (setq header (pop headers))
5602       (when (and (setq xrefs (mail-header-xref header))
5603                  (not (memq (setq number (mail-header-number header))
5604                             unreads)))
5605         (setq start 0)
5606         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5607           (setq start (match-end 0))
5608           (setq group (if prefix
5609                           (concat prefix (substring xrefs (match-beginning 1)
5610                                                     (match-end 1)))
5611                         (substring xrefs (match-beginning 1) (match-end 1))))
5612           (setq number
5613                 (string-to-int (substring xrefs (match-beginning 2)
5614                                           (match-end 2))))
5615           (if (setq entry (gnus-gethash group xref-hashtb))
5616               (setcdr entry (cons number (cdr entry)))
5617             (gnus-sethash group (cons number nil) xref-hashtb)))))
5618     (and start xref-hashtb)))
5619
5620 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5621   "Look through all the headers and mark the Xrefs as read."
5622   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5623         name info xref-hashtb idlist method nth4)
5624     (save-excursion
5625       (set-buffer gnus-group-buffer)
5626       (when (setq xref-hashtb
5627                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5628         (mapatoms
5629          (lambda (group)
5630            (unless (string= from-newsgroup (setq name (symbol-name group)))
5631              (setq idlist (symbol-value group))
5632              ;; Dead groups are not updated.
5633              (and (prog1
5634                       (setq info (gnus-get-info name))
5635                     (when (stringp (setq nth4 (gnus-info-method info)))
5636                       (setq nth4 (gnus-server-to-method nth4))))
5637                   ;; Only do the xrefs if the group has the same
5638                   ;; select method as the group we have just read.
5639                   (or (gnus-methods-equal-p
5640                        nth4 (gnus-find-method-for-group from-newsgroup))
5641                       virtual
5642                       (equal nth4 (setq method (gnus-find-method-for-group
5643                                                 from-newsgroup)))
5644                       (and (equal (car nth4) (car method))
5645                            (equal (nth 1 nth4) (nth 1 method))))
5646                   gnus-use-cross-reference
5647                   (or (not (eq gnus-use-cross-reference t))
5648                       virtual
5649                       ;; Only do cross-references on subscribed
5650                       ;; groups, if that is what is wanted.
5651                       (<= (gnus-info-level info) gnus-level-subscribed))
5652                   (gnus-group-make-articles-read name idlist))))
5653          xref-hashtb)))))
5654
5655 (defun gnus-compute-read-articles (group articles)
5656   (let* ((entry (gnus-group-entry group))
5657          (info (nth 2 entry))
5658          (active (gnus-active group))
5659          ninfo)
5660     (when entry
5661       ;; First peel off all invalid article numbers.
5662       (when active
5663         (let ((ids articles)
5664               id first)
5665           (while (setq id (pop ids))
5666             (when (and first (> id (cdr active)))
5667               ;; We'll end up in this situation in one particular
5668               ;; obscure situation.  If you re-scan a group and get
5669               ;; a new article that is cross-posted to a different
5670               ;; group that has not been re-scanned, you might get
5671               ;; crossposted article that has a higher number than
5672               ;; Gnus believes possible.  So we re-activate this
5673               ;; group as well.  This might mean doing the
5674               ;; crossposting thingy will *increase* the number
5675               ;; of articles in some groups.  Tsk, tsk.
5676               (setq active (or (gnus-activate-group group) active)))
5677             (when (or (> id (cdr active))
5678                       (< id (car active)))
5679               (setq articles (delq id articles))))))
5680       ;; If the read list is nil, we init it.
5681       (if (and active
5682                (null (gnus-info-read info))
5683                (> (car active) 1))
5684           (setq ninfo (cons 1 (1- (car active))))
5685         (setq ninfo (gnus-info-read info)))
5686       ;; Then we add the read articles to the range.
5687       (gnus-add-to-range
5688        ninfo (setq articles (sort articles '<))))))
5689
5690 (defun gnus-group-make-articles-read (group articles)
5691   "Update the info of GROUP to say that ARTICLES are read."
5692   (let* ((num 0)
5693          (entry (gnus-group-entry group))
5694          (info (nth 2 entry))
5695          (active (gnus-active group))
5696          range)
5697     (when entry
5698       (setq range (gnus-compute-read-articles group articles))
5699       (with-current-buffer gnus-group-buffer
5700         (gnus-undo-register
5701           `(progn
5702              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5703              (gnus-info-set-read ',info ',(gnus-info-read info))
5704              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5705              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5706              (gnus-group-update-group ,group t))))
5707       ;; Add the read articles to the range.
5708       (gnus-info-set-read info range)
5709       (gnus-request-set-mark group (list (list range 'add '(read))))
5710       ;; Then we have to re-compute how many unread
5711       ;; articles there are in this group.
5712       (when active
5713         (cond
5714          ((not range)
5715           (setq num (- (1+ (cdr active)) (car active))))
5716          ((not (listp (cdr range)))
5717           (setq num (- (cdr active) (- (1+ (cdr range))
5718                                        (car range)))))
5719          (t
5720           (while range
5721             (if (numberp (car range))
5722                 (setq num (1+ num))
5723               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5724             (setq range (cdr range)))
5725           (setq num (- (cdr active) num))))
5726         ;; Update the number of unread articles.
5727         (setcar entry num)
5728         ;; Update the group buffer.
5729         (unless (gnus-ephemeral-group-p group)
5730           (gnus-group-update-group group t))))))
5731
5732 (defvar gnus-newsgroup-none-id 0)
5733
5734 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5735   (let ((cur nntp-server-buffer)
5736         (dependencies
5737          (or dependencies
5738              (save-excursion (set-buffer gnus-summary-buffer)
5739                              gnus-newsgroup-dependencies)))
5740         headers id end ref
5741         (mail-parse-charset gnus-newsgroup-charset)
5742         (mail-parse-ignored-charsets
5743          (save-excursion (condition-case nil
5744                              (set-buffer gnus-summary-buffer)
5745                            (error))
5746                          gnus-newsgroup-ignored-charsets)))
5747     (save-excursion
5748       (set-buffer nntp-server-buffer)
5749       ;; Translate all TAB characters into SPACE characters.
5750       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5751       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5752       (ietf-drums-unfold-fws)
5753       (gnus-run-hooks 'gnus-parse-headers-hook)
5754       (let ((case-fold-search t)
5755             in-reply-to header p lines chars)
5756         (goto-char (point-min))
5757         ;; Search to the beginning of the next header.  Error messages
5758         ;; do not begin with 2 or 3.
5759         (while (re-search-forward "^[23][0-9]+ " nil t)
5760           (setq id nil
5761                 ref nil)
5762           ;; This implementation of this function, with nine
5763           ;; search-forwards instead of the one re-search-forward and
5764           ;; a case (which basically was the old function) is actually
5765           ;; about twice as fast, even though it looks messier.  You
5766           ;; can't have everything, I guess.  Speed and elegance
5767           ;; doesn't always go hand in hand.
5768           (setq
5769            header
5770            (vector
5771             ;; Number.
5772             (prog1
5773                 (read cur)
5774               (end-of-line)
5775               (setq p (point))
5776               (narrow-to-region (point)
5777                                 (or (and (search-forward "\n.\n" nil t)
5778                                          (- (point) 2))
5779                                     (point))))
5780             ;; Subject.
5781             (progn
5782               (goto-char p)
5783               (if (search-forward "\nsubject:" nil t)
5784                   (funcall gnus-decode-encoded-word-function
5785                            (nnheader-header-value))
5786                 "(none)"))
5787             ;; From.
5788             (progn
5789               (goto-char p)
5790               (if (search-forward "\nfrom:" nil t)
5791                   (funcall gnus-decode-encoded-word-function
5792                            (nnheader-header-value))
5793                 "(nobody)"))
5794             ;; Date.
5795             (progn
5796               (goto-char p)
5797               (if (search-forward "\ndate:" nil t)
5798                   (nnheader-header-value) ""))
5799             ;; Message-ID.
5800             (progn
5801               (goto-char p)
5802               (setq id (if (re-search-forward
5803                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5804                            ;; We do it this way to make sure the Message-ID
5805                            ;; is (somewhat) syntactically valid.
5806                            (buffer-substring (match-beginning 1)
5807                                              (match-end 1))
5808                          ;; If there was no message-id, we just fake one
5809                          ;; to make subsequent routines simpler.
5810                          (nnheader-generate-fake-message-id))))
5811             ;; References.
5812             (progn
5813               (goto-char p)
5814               (if (search-forward "\nreferences:" nil t)
5815                   (progn
5816                     (setq end (point))
5817                     (prog1
5818                         (nnheader-header-value)
5819                       (setq ref
5820                             (buffer-substring
5821                              (progn
5822                                (end-of-line)
5823                                (search-backward ">" end t)
5824                                (1+ (point)))
5825                              (progn
5826                                (search-backward "<" end t)
5827                                (point))))))
5828                 ;; Get the references from the in-reply-to header if there
5829                 ;; were no references and the in-reply-to header looks
5830                 ;; promising.
5831                 (if (and (search-forward "\nin-reply-to:" nil t)
5832                          (setq in-reply-to (nnheader-header-value))
5833                          (string-match "<[^>]+>" in-reply-to))
5834                     (let (ref2)
5835                       (setq ref (substring in-reply-to (match-beginning 0)
5836                                            (match-end 0)))
5837                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5838                         (setq ref2 (substring in-reply-to (match-beginning 0)
5839                                               (match-end 0)))
5840                         (when (> (length ref2) (length ref))
5841                           (setq ref ref2)))
5842                       ref)
5843                   (setq ref nil))))
5844             ;; Chars.
5845             (progn
5846               (goto-char p)
5847               (if (search-forward "\nchars: " nil t)
5848                   (if (numberp (setq chars (ignore-errors (read cur))))
5849                       chars -1)
5850                 -1))
5851             ;; Lines.
5852             (progn
5853               (goto-char p)
5854               (if (search-forward "\nlines: " nil t)
5855                   (if (numberp (setq lines (ignore-errors (read cur))))
5856                       lines -1)
5857                 -1))
5858             ;; Xref.
5859             (progn
5860               (goto-char p)
5861               (and (search-forward "\nxref:" nil t)
5862                    (nnheader-header-value)))
5863             ;; Extra.
5864             (when gnus-extra-headers
5865               (let ((extra gnus-extra-headers)
5866                     out)
5867                 (while extra
5868                   (goto-char p)
5869                   (when (search-forward
5870                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5871                     (push (cons (car extra) (nnheader-header-value))
5872                           out))
5873                   (pop extra))
5874                 out))))
5875           (when (equal id ref)
5876             (setq ref nil))
5877
5878           (when gnus-alter-header-function
5879             (funcall gnus-alter-header-function header)
5880             (setq id (mail-header-id header)
5881                   ref (gnus-parent-id (mail-header-references header))))
5882
5883           (when (setq header
5884                       (gnus-dependencies-add-header
5885                        header dependencies force-new))
5886             (push header headers))
5887           (goto-char (point-max))
5888           (widen))
5889         (nreverse headers)))))
5890
5891 ;; Goes through the xover lines and returns a list of vectors
5892 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5893                                                   force-new dependencies
5894                                                   group also-fetch-heads)
5895   "Parse the news overview data in the server buffer.
5896 Return a list of headers that match SEQUENCE (see
5897 `nntp-retrieve-headers')."
5898   ;; Get the Xref when the users reads the articles since most/some
5899   ;; NNTP servers do not include Xrefs when using XOVER.
5900   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5901   (let ((mail-parse-charset gnus-newsgroup-charset)
5902         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5903         (cur nntp-server-buffer)
5904         (dependencies (or dependencies gnus-newsgroup-dependencies))
5905         (allp (cond
5906                ((eq gnus-read-all-available-headers t)
5907                 t)
5908                ((stringp gnus-read-all-available-headers)
5909                 (string-match gnus-read-all-available-headers group))
5910                (t
5911                 nil)))
5912         number headers header)
5913     (save-excursion
5914       (set-buffer nntp-server-buffer)
5915       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5916       ;; Allow the user to mangle the headers before parsing them.
5917       (gnus-run-hooks 'gnus-parse-headers-hook)
5918       (goto-char (point-min))
5919       (gnus-parse-without-error
5920         (while (and (or sequence allp)
5921                     (not (eobp)))
5922           (setq number (read cur))
5923           (when (not allp)
5924             (while (and sequence
5925                         (< (car sequence) number))
5926               (setq sequence (cdr sequence))))
5927           (when (and (or allp
5928                          (and sequence
5929                               (eq number (car sequence))))
5930                      (progn
5931                        (setq sequence (cdr sequence))
5932                        (setq header (inline
5933                                       (gnus-nov-parse-line
5934                                        number dependencies force-new)))))
5935             (push header headers))
5936           (forward-line 1)))
5937       ;; A common bug in inn is that if you have posted an article and
5938       ;; then retrieves the active file, it will answer correctly --
5939       ;; the new article is included.  However, a NOV entry for the
5940       ;; article may not have been generated yet, so this may fail.
5941       ;; We work around this problem by retrieving the last few
5942       ;; headers using HEAD.
5943       (if (or (not also-fetch-heads)
5944               (not sequence))
5945           ;; We (probably) got all the headers.
5946           (nreverse headers)
5947         (let ((gnus-nov-is-evil t))
5948           (nconc
5949            (nreverse headers)
5950            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5951              (gnus-get-newsgroup-headers))))))))
5952
5953 (defun gnus-article-get-xrefs ()
5954   "Fill in the Xref value in `gnus-current-headers', if necessary.
5955 This is meant to be called in `gnus-article-internal-prepare-hook'."
5956   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5957                                  gnus-current-headers)))
5958     (or (not gnus-use-cross-reference)
5959         (not headers)
5960         (and (mail-header-xref headers)
5961              (not (string= (mail-header-xref headers) "")))
5962         (let ((case-fold-search t)
5963               xref)
5964           (save-restriction
5965             (nnheader-narrow-to-headers)
5966             (goto-char (point-min))
5967             (when (or (and (not (eobp))
5968                            (eq (downcase (char-after)) ?x)
5969                            (looking-at "Xref:"))
5970                       (search-forward "\nXref:" nil t))
5971               (goto-char (1+ (match-end 0)))
5972               (setq xref (buffer-substring (point) (point-at-eol)))
5973               (mail-header-set-xref headers xref)))))))
5974
5975 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5976   "Find article ID and insert the summary line for that article.
5977 OLD-HEADER can either be a header or a line number to insert
5978 the subject line on."
5979   (let* ((line (and (numberp old-header) old-header))
5980          (old-header (and (vectorp old-header) old-header))
5981          (header (cond ((and old-header use-old-header)
5982                         old-header)
5983                        ((and (numberp id)
5984                              (gnus-number-to-header id))
5985                         (gnus-number-to-header id))
5986                        (t
5987                         (gnus-read-header id))))
5988          (number (and (numberp id) id))
5989          d)
5990     (when header
5991       ;; Rebuild the thread that this article is part of and go to the
5992       ;; article we have fetched.
5993       (when (and (not gnus-show-threads)
5994                  old-header)
5995         (when (and number
5996                    (setq d (gnus-data-find (mail-header-number old-header))))
5997           (goto-char (gnus-data-pos d))
5998           (gnus-data-remove
5999            number
6000            (- (point-at-bol)
6001               (prog1
6002                   (1+ (point-at-eol))
6003                 (gnus-delete-line))))))
6004       (when old-header
6005         (mail-header-set-number header (mail-header-number old-header)))
6006       (setq gnus-newsgroup-sparse
6007             (delq (setq number (mail-header-number header))
6008                   gnus-newsgroup-sparse))
6009       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6010       (push number gnus-newsgroup-limit)
6011       (gnus-rebuild-thread (mail-header-id header) line)
6012       (gnus-summary-goto-subject number nil t))
6013     (when (and (numberp number)
6014                (> number 0))
6015       ;; We have to update the boundaries even if we can't fetch the
6016       ;; article if ID is a number -- so that the next `P' or `N'
6017       ;; command will fetch the previous (or next) article even
6018       ;; if the one we tried to fetch this time has been canceled.
6019       (when (> number gnus-newsgroup-end)
6020         (setq gnus-newsgroup-end number))
6021       (when (< number gnus-newsgroup-begin)
6022         (setq gnus-newsgroup-begin number))
6023       (setq gnus-newsgroup-unselected
6024             (delq number gnus-newsgroup-unselected)))
6025     ;; Report back a success?
6026     (and header (mail-header-number header))))
6027
6028 ;;; Process/prefix in the summary buffer
6029
6030 (defun gnus-summary-work-articles (n)
6031   "Return a list of articles to be worked upon.
6032 The prefix argument, the list of process marked articles, and the
6033 current article will be taken into consideration."
6034   (save-excursion
6035     (set-buffer gnus-summary-buffer)
6036     (cond
6037      (n
6038       ;; A numerical prefix has been given.
6039       (setq n (prefix-numeric-value n))
6040       (let ((backward (< n 0))
6041             (n (abs (prefix-numeric-value n)))
6042             articles article)
6043         (save-excursion
6044           (while
6045               (and (> n 0)
6046                    (push (setq article (gnus-summary-article-number))
6047                          articles)
6048                    (if backward
6049                        (gnus-summary-find-prev nil article)
6050                      (gnus-summary-find-next nil article)))
6051             (decf n)))
6052         (nreverse articles)))
6053      ((and (gnus-region-active-p) (mark))
6054       (message "region active")
6055       ;; Work on the region between point and mark.
6056       (let ((max (max (point) (mark)))
6057             articles article)
6058         (save-excursion
6059           (goto-char (min (min (point) (mark))))
6060           (while
6061               (and
6062                (push (setq article (gnus-summary-article-number)) articles)
6063                (gnus-summary-find-next nil article)
6064                (< (point) max)))
6065           (nreverse articles))))
6066      (gnus-newsgroup-processable
6067       ;; There are process-marked articles present.
6068       ;; Save current state.
6069       (gnus-summary-save-process-mark)
6070       ;; Return the list.
6071       (reverse gnus-newsgroup-processable))
6072      (t
6073       ;; Just return the current article.
6074       (list (gnus-summary-article-number))))))
6075
6076 (defmacro gnus-summary-iterate (arg &rest forms)
6077   "Iterate over the process/prefixed articles and do FORMS.
6078 ARG is the interactive prefix given to the command.  FORMS will be
6079 executed with point over the summary line of the articles."
6080   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6081     `(let ((,articles (gnus-summary-work-articles ,arg)))
6082        (while ,articles
6083          (gnus-summary-goto-subject (car ,articles))
6084          ,@forms
6085          (pop ,articles)))))
6086
6087 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6088 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6089
6090 (defun gnus-summary-save-process-mark ()
6091   "Push the current set of process marked articles on the stack."
6092   (interactive)
6093   (push (copy-sequence gnus-newsgroup-processable)
6094         gnus-newsgroup-process-stack))
6095
6096 (defun gnus-summary-kill-process-mark ()
6097   "Push the current set of process marked articles on the stack and unmark."
6098   (interactive)
6099   (gnus-summary-save-process-mark)
6100   (gnus-summary-unmark-all-processable))
6101
6102 (defun gnus-summary-yank-process-mark ()
6103   "Pop the last process mark state off the stack and restore it."
6104   (interactive)
6105   (unless gnus-newsgroup-process-stack
6106     (error "Empty mark stack"))
6107   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6108
6109 (defun gnus-summary-process-mark-set (set)
6110   "Make SET into the current process marked articles."
6111   (gnus-summary-unmark-all-processable)
6112   (while set
6113     (gnus-summary-set-process-mark (pop set))))
6114
6115 ;;; Searching and stuff
6116
6117 (defun gnus-summary-search-group (&optional backward use-level)
6118   "Search for next unread newsgroup.
6119 If optional argument BACKWARD is non-nil, search backward instead."
6120   (save-excursion
6121     (set-buffer gnus-group-buffer)
6122     (when (gnus-group-search-forward
6123            backward nil (if use-level (gnus-group-group-level) nil))
6124       (gnus-group-group-name))))
6125
6126 (defun gnus-summary-best-group (&optional exclude-group)
6127   "Find the name of the best unread group.
6128 If EXCLUDE-GROUP, do not go to this group."
6129   (save-excursion
6130     (set-buffer gnus-group-buffer)
6131     (save-excursion
6132       (gnus-group-best-unread-group exclude-group))))
6133
6134 (defun gnus-summary-find-next (&optional unread article backward)
6135   (if backward
6136       (gnus-summary-find-prev unread article)
6137     (let* ((dummy (gnus-summary-article-intangible-p))
6138            (article (or article (gnus-summary-article-number)))
6139            (data (gnus-data-find-list article))
6140            result)
6141       (when (and (not dummy)
6142                  (or (not gnus-summary-check-current)
6143                      (not unread)
6144                      (not (gnus-data-unread-p (car data)))))
6145         (setq data (cdr data)))
6146       (when (setq result
6147                   (if unread
6148                       (progn
6149                         (while data
6150                           (unless (memq (gnus-data-number (car data))
6151                                         (cond
6152                                          ((eq gnus-auto-goto-ignores
6153                                               'always-undownloaded)
6154                                           gnus-newsgroup-undownloaded)
6155                                          (gnus-plugged
6156                                           nil)
6157                                          ((eq gnus-auto-goto-ignores
6158                                               'unfetched)
6159                                           gnus-newsgroup-unfetched)
6160                                          ((eq gnus-auto-goto-ignores
6161                                               'undownloaded)
6162                                           gnus-newsgroup-undownloaded)))
6163                             (when (gnus-data-unread-p (car data))
6164                               (setq result (car data)
6165                                     data nil)))
6166                           (setq data (cdr data)))
6167                         result)
6168                     (car data)))
6169         (goto-char (gnus-data-pos result))
6170         (gnus-data-number result)))))
6171
6172 (defun gnus-summary-find-prev (&optional unread article)
6173   (let* ((eobp (eobp))
6174          (article (or article (gnus-summary-article-number)))
6175          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6176          result)
6177     (when (and (not eobp)
6178                (or (not gnus-summary-check-current)
6179                    (not unread)
6180                    (not (gnus-data-unread-p (car data)))))
6181       (setq data (cdr data)))
6182     (when (setq result
6183                 (if unread
6184                     (progn
6185                       (while data
6186                         (unless (memq (gnus-data-number (car data))
6187                                       (cond
6188                                        ((eq gnus-auto-goto-ignores
6189                                             'always-undownloaded)
6190                                         gnus-newsgroup-undownloaded)
6191                                        (gnus-plugged
6192                                         nil)
6193                                        ((eq gnus-auto-goto-ignores
6194                                             'unfetched)
6195                                         gnus-newsgroup-unfetched)
6196                                        ((eq gnus-auto-goto-ignores
6197                                             'undownloaded)
6198                                         gnus-newsgroup-undownloaded)))
6199                           (when (gnus-data-unread-p (car data))
6200                             (setq result (car data)
6201                                   data nil)))
6202                         (setq data (cdr data)))
6203                       result)
6204                   (car data)))
6205       (goto-char (gnus-data-pos result))
6206       (gnus-data-number result))))
6207
6208 (defun gnus-summary-find-subject (subject &optional unread backward article)
6209   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6210          (article (or article (gnus-summary-article-number)))
6211          (articles (gnus-data-list backward))
6212          (arts (gnus-data-find-list article articles))
6213          result)
6214     (when (or (not gnus-summary-check-current)
6215               (not unread)
6216               (not (gnus-data-unread-p (car arts))))
6217       (setq arts (cdr arts)))
6218     (while arts
6219       (and (or (not unread)
6220                (gnus-data-unread-p (car arts)))
6221            (vectorp (gnus-data-header (car arts)))
6222            (gnus-subject-equal
6223             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6224            (setq result (car arts)
6225                  arts nil))
6226       (setq arts (cdr arts)))
6227     (and result
6228          (goto-char (gnus-data-pos result))
6229          (gnus-data-number result))))
6230
6231 (defun gnus-summary-search-forward (&optional unread subject backward)
6232   "Search forward for an article.
6233 If UNREAD, look for unread articles.  If SUBJECT, look for
6234 articles with that subject.  If BACKWARD, search backward instead."
6235   (cond (subject (gnus-summary-find-subject subject unread backward))
6236         (backward (gnus-summary-find-prev unread))
6237         (t (gnus-summary-find-next unread))))
6238
6239 (defun gnus-recenter (&optional n)
6240   "Center point in window and redisplay frame.
6241 Also do horizontal recentering."
6242   (interactive "P")
6243   (when (and gnus-auto-center-summary
6244              (not (eq gnus-auto-center-summary 'vertical)))
6245     (gnus-horizontal-recenter))
6246   (recenter n))
6247
6248 (defun gnus-summary-recenter ()
6249   "Center point in the summary window.
6250 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6251 displayed, no centering will be performed."
6252   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6253   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6254   (interactive)
6255   ;; The user has to want it.
6256   (when gnus-auto-center-summary
6257     (let* ((top (cond ((< (window-height) 4) 0)
6258                       ((< (window-height) 7) 1)
6259                       (t (if (numberp gnus-auto-center-summary)
6260                              gnus-auto-center-summary
6261                            2))))
6262            (height (1- (window-height)))
6263            (bottom (save-excursion (goto-char (point-max))
6264                                    (forward-line (- height))
6265                                    (point)))
6266            (window (get-buffer-window (current-buffer))))
6267       (when (get-buffer-window gnus-article-buffer)
6268         ;; Only do recentering when the article buffer is displayed,
6269         ;; Set the window start to either `bottom', which is the biggest
6270         ;; possible valid number, or the second line from the top,
6271         ;; whichever is the least.
6272         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6273           (if (> bottom top-pos)
6274               ;; Keep the second line from the top visible
6275               (set-window-start window top-pos t)
6276             ;; Try to keep the bottom line visible; if it's partially
6277             ;; obscured, either scroll one more line to make it fully
6278             ;; visible, or revert to using TOP-POS.
6279             (save-excursion
6280               (goto-char (point-max))
6281               (forward-line -1)
6282               (let ((last-line-start (point)))
6283                 (goto-char bottom)
6284                 (set-window-start window (point) t)
6285                 (when (not (pos-visible-in-window-p last-line-start window))
6286                   (forward-line 1)
6287                   (set-window-start window (min (point) top-pos) t)))))))
6288       ;; Do horizontal recentering while we're at it.
6289       (when (and (get-buffer-window (current-buffer) t)
6290                  (not (eq gnus-auto-center-summary 'vertical)))
6291         (let ((selected (selected-window)))
6292           (select-window (get-buffer-window (current-buffer) t))
6293           (gnus-summary-position-point)
6294           (gnus-horizontal-recenter)
6295           (select-window selected))))))
6296
6297 (defun gnus-summary-jump-to-group (newsgroup)
6298   "Move point to NEWSGROUP in group mode buffer."
6299   ;; Keep update point of group mode buffer if visible.
6300   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6301       (save-window-excursion
6302         ;; Take care of tree window mode.
6303         (when (get-buffer-window gnus-group-buffer)
6304           (pop-to-buffer gnus-group-buffer))
6305         (gnus-group-jump-to-group newsgroup))
6306     (save-excursion
6307       ;; Take care of tree window mode.
6308       (if (get-buffer-window gnus-group-buffer)
6309           (pop-to-buffer gnus-group-buffer)
6310         (set-buffer gnus-group-buffer))
6311       (gnus-group-jump-to-group newsgroup))))
6312
6313 ;; This function returns a list of article numbers based on the
6314 ;; difference between the ranges of read articles in this group and
6315 ;; the range of active articles.
6316 (defun gnus-list-of-unread-articles (group)
6317   (let* ((read (gnus-info-read (gnus-get-info group)))
6318          (active (or (gnus-active group) (gnus-activate-group group)))
6319          (last (cdr active))
6320          first nlast unread)
6321     ;; If none are read, then all are unread.
6322     (if (not read)
6323         (setq first (car active))
6324       ;; If the range of read articles is a single range, then the
6325       ;; first unread article is the article after the last read
6326       ;; article.  Sounds logical, doesn't it?
6327       (if (and (not (listp (cdr read)))
6328                (or (< (car read) (car active))
6329                    (progn (setq read (list read))
6330                           nil)))
6331           (setq first (max (car active) (1+ (cdr read))))
6332         ;; `read' is a list of ranges.
6333         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6334                                   (caar read)))
6335                   1)
6336           (setq first (car active)))
6337         (while read
6338           (when first
6339             (while (< first nlast)
6340               (setq unread (cons first unread)
6341                     first (1+ first))))
6342           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6343           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6344           (setq read (cdr read)))))
6345     ;; And add the last unread articles.
6346     (while (<= first last)
6347       (setq unread (cons first unread)
6348             first (1+ first)))
6349     ;; Return the list of unread articles.
6350     (delq 0 (nreverse unread))))
6351
6352 (defun gnus-list-of-read-articles (group)
6353   "Return a list of unread, unticked and non-dormant articles."
6354   (let* ((info (gnus-get-info group))
6355          (marked (gnus-info-marks info))
6356          (active (gnus-active group)))
6357     (and info active
6358          (gnus-list-range-difference
6359           (gnus-list-range-difference
6360            (gnus-sorted-complement
6361             (gnus-uncompress-range active)
6362             (gnus-list-of-unread-articles group))
6363            (cdr (assq 'dormant marked)))
6364           (cdr (assq 'tick marked))))))
6365
6366 ;; This function returns a sequence of article numbers based on the
6367 ;; difference between the ranges of read articles in this group and
6368 ;; the range of active articles.
6369 (defun gnus-sequence-of-unread-articles (group)
6370   (let* ((read (gnus-info-read (gnus-get-info group)))
6371          (active (or (gnus-active group) (gnus-activate-group group)))
6372          (last (cdr active))
6373          first nlast unread)
6374     ;; If none are read, then all are unread.
6375     (if (not read)
6376         (setq first (car active))
6377       ;; If the range of read articles is a single range, then the
6378       ;; first unread article is the article after the last read
6379       ;; article.  Sounds logical, doesn't it?
6380       (if (and (not (listp (cdr read)))
6381                (or (< (car read) (car active))
6382                    (progn (setq read (list read))
6383                           nil)))
6384           (setq first (max (car active) (1+ (cdr read))))
6385         ;; `read' is a list of ranges.
6386         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6387                                   (caar read)))
6388                   1)
6389           (setq first (car active)))
6390         (while read
6391           (when first
6392             (push (cons first nlast) unread))
6393           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6394           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6395           (setq read (cdr read)))))
6396     ;; And add the last unread articles.
6397     (cond ((< first last)
6398            (push (cons first last) unread))
6399           ((= first last)
6400            (push first unread)))
6401     ;; Return the sequence of unread articles.
6402     (delq 0 (nreverse unread))))
6403
6404 ;; Various summary commands
6405
6406 (defun gnus-summary-select-article-buffer ()
6407   "Reconfigure windows to show article buffer."
6408   (interactive)
6409   (if (not (gnus-buffer-live-p gnus-article-buffer))
6410       (error "There is no article buffer for this summary buffer")
6411     (gnus-configure-windows 'article)
6412     (select-window (get-buffer-window gnus-article-buffer))))
6413
6414 (defun gnus-summary-universal-argument (arg)
6415   "Perform any operation on all articles that are process/prefixed."
6416   (interactive "P")
6417   (let ((articles (gnus-summary-work-articles arg))
6418         func article)
6419     (if (eq
6420          (setq
6421           func
6422           (key-binding
6423            (read-key-sequence
6424             (substitute-command-keys
6425              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6426          'undefined)
6427         (gnus-error 1 "Undefined key")
6428       (save-excursion
6429         (while articles
6430           (gnus-summary-goto-subject (setq article (pop articles)))
6431           (let (gnus-newsgroup-processable)
6432             (command-execute func))
6433           (gnus-summary-remove-process-mark article)))))
6434   (gnus-summary-position-point))
6435
6436 (defun gnus-summary-toggle-truncation (&optional arg)
6437   "Toggle truncation of summary lines.
6438 With ARG, turn line truncation on if ARG is positive."
6439   (interactive "P")
6440   (setq truncate-lines
6441         (if (null arg) (not truncate-lines)
6442           (> (prefix-numeric-value arg) 0)))
6443   (redraw-display))
6444
6445 (defun gnus-summary-find-for-reselect ()
6446   "Return the number of an article to stay on across a reselect.
6447 The current article is considered, then following articles, then previous
6448 articles.  An article is sought which is not cancelled and isn't a temporary
6449 insertion from another group.  If there's no such then return a dummy 0."
6450   (let (found)
6451     (dolist (rev '(nil t))
6452       (unless found      ; don't demand the reverse list if we don't need it
6453         (let ((data (gnus-data-find-list
6454                      (gnus-summary-article-number) (gnus-data-list rev))))
6455           (while (and data (not found))
6456             (if (and (< 0 (gnus-data-number (car data)))
6457                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6458                 (setq found (gnus-data-number (car data))))
6459             (setq data (cdr data))))))
6460     (or found 0)))
6461
6462 (defun gnus-summary-reselect-current-group (&optional all rescan)
6463   "Exit and then reselect the current newsgroup.
6464 The prefix argument ALL means to select all articles."
6465   (interactive "P")
6466   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6467     (error "Ephemeral groups can't be reselected"))
6468   (let ((current-subject (gnus-summary-find-for-reselect))
6469         (group gnus-newsgroup-name))
6470     (setq gnus-newsgroup-begin nil)
6471     (gnus-summary-exit nil 'leave-hidden)
6472     ;; We have to adjust the point of group mode buffer because
6473     ;; point was moved to the next unread newsgroup by exiting.
6474     (gnus-summary-jump-to-group group)
6475     (when rescan
6476       (save-excursion
6477         (gnus-group-get-new-news-this-group 1)))
6478     (gnus-group-read-group all t)
6479     (gnus-summary-goto-subject current-subject nil t)))
6480
6481 (defun gnus-summary-rescan-group (&optional all)
6482   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6483   (interactive "P")
6484   (gnus-summary-reselect-current-group all t))
6485
6486 (defun gnus-summary-update-info (&optional non-destructive)
6487   (save-excursion
6488     (let ((group gnus-newsgroup-name))
6489       (when group
6490         (when gnus-newsgroup-kill-headers
6491           (setq gnus-newsgroup-killed
6492                 (gnus-compress-sequence
6493                  (gnus-sorted-union
6494                   (gnus-list-range-intersection
6495                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6496                   gnus-newsgroup-unreads)
6497                  t)))
6498         (unless (listp (cdr gnus-newsgroup-killed))
6499           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6500         (let ((headers gnus-newsgroup-headers))
6501           ;; Set the new ranges of read articles.
6502           (save-excursion
6503             (set-buffer gnus-group-buffer)
6504             (gnus-undo-force-boundary))
6505           (gnus-update-read-articles
6506            group (gnus-sorted-union
6507                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6508           ;; Set the current article marks.
6509           (let ((gnus-newsgroup-scored
6510                  (if (and (not gnus-save-score)
6511                           (not non-destructive))
6512                      nil
6513                    gnus-newsgroup-scored)))
6514             (save-excursion
6515               (gnus-update-marks)))
6516           ;; Do the cross-ref thing.
6517           (when gnus-use-cross-reference
6518             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6519           ;; Do not switch windows but change the buffer to work.
6520           (set-buffer gnus-group-buffer)
6521           (unless (gnus-ephemeral-group-p group)
6522             (gnus-group-update-group group)))))))
6523
6524 (defun gnus-summary-save-newsrc (&optional force)
6525   "Save the current number of read/marked articles in the dribble buffer.
6526 The dribble buffer will then be saved.
6527 If FORCE (the prefix), also save the .newsrc file(s)."
6528   (interactive "P")
6529   (gnus-summary-update-info t)
6530   (if force
6531       (gnus-save-newsrc-file)
6532     (gnus-dribble-save)))
6533
6534 (defun gnus-summary-exit (&optional temporary leave-hidden)
6535   "Exit reading current newsgroup, and then return to group selection mode.
6536 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6537   (interactive)
6538   (gnus-set-global-variables)
6539   (when (gnus-buffer-live-p gnus-article-buffer)
6540     (save-excursion
6541       (set-buffer gnus-article-buffer)
6542       (mm-destroy-parts gnus-article-mime-handles)
6543       ;; Set it to nil for safety reason.
6544       (setq gnus-article-mime-handle-alist nil)
6545       (setq gnus-article-mime-handles nil)))
6546   (gnus-kill-save-kill-buffer)
6547   (gnus-async-halt-prefetch)
6548   (let* ((group gnus-newsgroup-name)
6549          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6550          (gnus-group-is-exiting-p t)
6551          (mode major-mode)
6552          (group-point nil)
6553          (buf (current-buffer)))
6554     (unless quit-config
6555       ;; Do adaptive scoring, and possibly save score files.
6556       (when gnus-newsgroup-adaptive
6557         (gnus-score-adaptive))
6558       (when gnus-use-scoring
6559         (gnus-score-save)))
6560     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6561     ;; If we have several article buffers, we kill them at exit.
6562     (unless gnus-single-article-buffer
6563       (gnus-kill-buffer gnus-original-article-buffer)
6564       (setq gnus-article-current nil))
6565     (when gnus-use-cache
6566       (gnus-cache-possibly-remove-articles)
6567       (gnus-cache-save-buffers))
6568     (gnus-async-prefetch-remove-group group)
6569     (when gnus-suppress-duplicates
6570       (gnus-dup-enter-articles))
6571     (when gnus-use-trees
6572       (gnus-tree-close group))
6573     (when gnus-use-cache
6574       (gnus-cache-write-active))
6575     ;; Remove entries for this group.
6576     (nnmail-purge-split-history (gnus-group-real-name group))
6577     ;; Make all changes in this group permanent.
6578     (unless quit-config
6579       (gnus-run-hooks 'gnus-exit-group-hook)
6580       (gnus-summary-update-info))
6581     (gnus-close-group group)
6582     ;; Make sure where we were, and go to next newsgroup.
6583     (set-buffer gnus-group-buffer)
6584     (unless quit-config
6585       (gnus-group-jump-to-group group))
6586     (gnus-run-hooks 'gnus-summary-exit-hook)
6587     (unless (or quit-config
6588                 ;; If this group has disappeared from the summary
6589                 ;; buffer, don't skip forwards.
6590                 (not (string= group (gnus-group-group-name))))
6591       (gnus-group-next-unread-group 1))
6592     (setq group-point (point))
6593     (if temporary
6594         nil                             ;Nothing to do.
6595       ;; If we have several article buffers, we kill them at exit.
6596       (unless gnus-single-article-buffer
6597         (gnus-kill-buffer gnus-article-buffer)
6598         (gnus-kill-buffer gnus-original-article-buffer)
6599         (setq gnus-article-current nil))
6600       (set-buffer buf)
6601       (if (not gnus-kill-summary-on-exit)
6602           (progn
6603             (gnus-deaden-summary)
6604             (setq mode nil))
6605         ;; We set all buffer-local variables to nil.  It is unclear why
6606         ;; this is needed, but if we don't, buffer-local variables are
6607         ;; not garbage-collected, it seems.  This would the lead to en
6608         ;; ever-growing Emacs.
6609         (gnus-summary-clear-local-variables)
6610         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6611           (gnus-summary-clear-local-variables))
6612         (when (get-buffer gnus-article-buffer)
6613           (bury-buffer gnus-article-buffer))
6614         ;; We clear the global counterparts of the buffer-local
6615         ;; variables as well, just to be on the safe side.
6616         (set-buffer gnus-group-buffer)
6617         (gnus-summary-clear-local-variables)
6618         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6619           (gnus-summary-clear-local-variables))
6620         ;; Return to group mode buffer.
6621         (when (eq mode 'gnus-summary-mode)
6622           (gnus-kill-buffer buf)))
6623       (setq gnus-current-select-method gnus-select-method)
6624       (if leave-hidden
6625           (set-buffer gnus-group-buffer)
6626         (pop-to-buffer gnus-group-buffer))
6627       (if (not quit-config)
6628           (progn
6629             (goto-char group-point)
6630             (unless leave-hidden
6631               (gnus-configure-windows 'group 'force)))
6632         (gnus-handle-ephemeral-exit quit-config))
6633       ;; Clear the current group name.
6634       (unless quit-config
6635         (setq gnus-newsgroup-name nil)))))
6636
6637 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6638 (defun gnus-summary-exit-no-update (&optional no-questions)
6639   "Quit reading current newsgroup without updating read article info."
6640   (interactive)
6641   (let* ((group gnus-newsgroup-name)
6642          (gnus-group-is-exiting-p t)
6643          (gnus-group-is-exiting-without-update-p t)
6644          (quit-config (gnus-group-quit-config group)))
6645     (when (or no-questions
6646               gnus-expert-user
6647               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6648       (gnus-async-halt-prefetch)
6649       (run-hooks 'gnus-summary-prepare-exit-hook)
6650       (when (gnus-buffer-live-p gnus-article-buffer)
6651         (save-excursion
6652           (set-buffer gnus-article-buffer)
6653           (mm-destroy-parts gnus-article-mime-handles)
6654           ;; Set it to nil for safety reason.
6655           (setq gnus-article-mime-handle-alist nil)
6656           (setq gnus-article-mime-handles nil)))
6657       ;; If we have several article buffers, we kill them at exit.
6658       (unless gnus-single-article-buffer
6659         (gnus-kill-buffer gnus-article-buffer)
6660         (gnus-kill-buffer gnus-original-article-buffer)
6661         (setq gnus-article-current nil))
6662       (if (not gnus-kill-summary-on-exit)
6663           (gnus-deaden-summary)
6664         (gnus-close-group group)
6665         (gnus-summary-clear-local-variables)
6666         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6667           (gnus-summary-clear-local-variables))
6668         (set-buffer gnus-group-buffer)
6669         (gnus-summary-clear-local-variables)
6670         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6671           (gnus-summary-clear-local-variables))
6672         (gnus-kill-buffer gnus-summary-buffer))
6673       (unless gnus-single-article-buffer
6674         (setq gnus-article-current nil))
6675       (when gnus-use-trees
6676         (gnus-tree-close group))
6677       (gnus-async-prefetch-remove-group group)
6678       (when (get-buffer gnus-article-buffer)
6679         (bury-buffer gnus-article-buffer))
6680       ;; Return to the group buffer.
6681       (gnus-configure-windows 'group 'force)
6682       ;; Clear the current group name.
6683       (setq gnus-newsgroup-name nil)
6684       (unless (gnus-ephemeral-group-p group)
6685         (gnus-group-update-group group))
6686       (when (equal (gnus-group-group-name) group)
6687         (gnus-group-next-unread-group 1))
6688       (when quit-config
6689         (gnus-handle-ephemeral-exit quit-config)))))
6690
6691 (defun gnus-handle-ephemeral-exit (quit-config)
6692   "Handle movement when leaving an ephemeral group.
6693 The state which existed when entering the ephemeral is reset."
6694   (if (not (buffer-name (car quit-config)))
6695       (gnus-configure-windows 'group 'force)
6696     (set-buffer (car quit-config))
6697     (cond ((eq major-mode 'gnus-summary-mode)
6698            (gnus-set-global-variables))
6699           ((eq major-mode 'gnus-article-mode)
6700            (save-excursion
6701              ;; The `gnus-summary-buffer' variable may point
6702              ;; to the old summary buffer when using a single
6703              ;; article buffer.
6704              (unless (gnus-buffer-live-p gnus-summary-buffer)
6705                (set-buffer gnus-group-buffer))
6706              (set-buffer gnus-summary-buffer)
6707              (gnus-set-global-variables))))
6708     (if (or (eq (cdr quit-config) 'article)
6709             (eq (cdr quit-config) 'pick))
6710         (progn
6711           ;; The current article may be from the ephemeral group
6712           ;; thus it is best that we reload this article
6713           ;;
6714           ;; If we're exiting from a large digest, this can be
6715           ;; extremely slow.  So, it's better not to reload it. -- jh.
6716           ;;(gnus-summary-show-article)
6717           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6718               (gnus-configure-windows 'pick 'force)
6719             (gnus-configure-windows (cdr quit-config) 'force)))
6720       (gnus-configure-windows (cdr quit-config) 'force))
6721     (when (eq major-mode 'gnus-summary-mode)
6722       (gnus-summary-next-subject 1 nil t)
6723       (gnus-summary-recenter)
6724       (gnus-summary-position-point))))
6725
6726 ;;; Dead summaries.
6727
6728 (defvar gnus-dead-summary-mode-map nil)
6729
6730 (unless gnus-dead-summary-mode-map
6731   (setq gnus-dead-summary-mode-map (make-keymap))
6732   (suppress-keymap gnus-dead-summary-mode-map)
6733   (substitute-key-definition
6734    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6735   (dolist (key '("\C-d" "\r" "\177" [delete]))
6736     (define-key gnus-dead-summary-mode-map
6737       key 'gnus-summary-wake-up-the-dead))
6738   (dolist (key '("q" "Q"))
6739     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6740
6741 (defvar gnus-dead-summary-mode nil
6742   "Minor mode for Gnus summary buffers.")
6743
6744 (defun gnus-dead-summary-mode (&optional arg)
6745   "Minor mode for Gnus summary buffers."
6746   (interactive "P")
6747   (when (eq major-mode 'gnus-summary-mode)
6748     (make-local-variable 'gnus-dead-summary-mode)
6749     (setq gnus-dead-summary-mode
6750           (if (null arg) (not gnus-dead-summary-mode)
6751             (> (prefix-numeric-value arg) 0)))
6752     (when gnus-dead-summary-mode
6753       (add-minor-mode
6754        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6755
6756 (defun gnus-deaden-summary ()
6757   "Make the current summary buffer into a dead summary buffer."
6758   ;; Kill any previous dead summary buffer.
6759   (when (and gnus-dead-summary
6760              (buffer-name gnus-dead-summary))
6761     (save-excursion
6762       (set-buffer gnus-dead-summary)
6763       (when gnus-dead-summary-mode
6764         (kill-buffer (current-buffer)))))
6765   ;; Make this the current dead summary.
6766   (setq gnus-dead-summary (current-buffer))
6767   (gnus-dead-summary-mode 1)
6768   (let ((name (buffer-name)))
6769     (when (string-match "Summary" name)
6770       (rename-buffer
6771        (concat (substring name 0 (match-beginning 0)) "Dead "
6772                (substring name (match-beginning 0)))
6773        t)
6774       (bury-buffer))))
6775
6776 (defun gnus-kill-or-deaden-summary (buffer)
6777   "Kill or deaden the summary BUFFER."
6778   (save-excursion
6779     (when (and (buffer-name buffer)
6780                (not gnus-single-article-buffer))
6781       (save-excursion
6782         (set-buffer buffer)
6783         (gnus-kill-buffer gnus-article-buffer)
6784         (gnus-kill-buffer gnus-original-article-buffer)))
6785     (cond
6786      ;; Kill the buffer.
6787      (gnus-kill-summary-on-exit
6788       (when (and gnus-use-trees
6789                  (gnus-buffer-exists-p buffer))
6790         (save-excursion
6791           (set-buffer buffer)
6792           (gnus-tree-close gnus-newsgroup-name)))
6793       (gnus-kill-buffer buffer))
6794      ;; Deaden the buffer.
6795      ((gnus-buffer-exists-p buffer)
6796       (save-excursion
6797         (set-buffer buffer)
6798         (gnus-deaden-summary))))))
6799
6800 (defun gnus-summary-wake-up-the-dead (&rest args)
6801   "Wake up the dead summary buffer."
6802   (interactive)
6803   (gnus-dead-summary-mode -1)
6804   (let ((name (buffer-name)))
6805     (when (string-match "Dead " name)
6806       (rename-buffer
6807        (concat (substring name 0 (match-beginning 0))
6808                (substring name (match-end 0)))
6809        t)))
6810   (gnus-message 3 "This dead summary is now alive again"))
6811
6812 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6813 (defun gnus-summary-fetch-faq (&optional faq-dir)
6814   "Fetch the FAQ for the current group.
6815 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6816 in."
6817   (interactive
6818    (list
6819     (when current-prefix-arg
6820       (completing-read
6821        "FAQ dir: " (and (listp gnus-group-faq-directory)
6822                         (mapcar (lambda (file) (list file))
6823                                 gnus-group-faq-directory))))))
6824   (let (gnus-faq-buffer)
6825     (when (setq gnus-faq-buffer
6826                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6827       (gnus-configure-windows 'summary-faq))))
6828
6829 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6830 (defun gnus-summary-describe-group (&optional force)
6831   "Describe the current newsgroup."
6832   (interactive "P")
6833   (gnus-group-describe-group force gnus-newsgroup-name))
6834
6835 (defun gnus-summary-describe-briefly ()
6836   "Describe summary mode commands briefly."
6837   (interactive)
6838   (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")))
6839
6840 ;; Walking around group mode buffer from summary mode.
6841
6842 (defun gnus-summary-next-group (&optional no-article target-group backward)
6843   "Exit current newsgroup and then select next unread newsgroup.
6844 If prefix argument NO-ARTICLE is non-nil, no article is selected
6845 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6846 previous group instead."
6847   (interactive "P")
6848   ;; Stop pre-fetching.
6849   (gnus-async-halt-prefetch)
6850   (let ((current-group gnus-newsgroup-name)
6851         (current-buffer (current-buffer))
6852         entered)
6853     ;; First we semi-exit this group to update Xrefs and all variables.
6854     ;; We can't do a real exit, because the window conf must remain
6855     ;; the same in case the user is prompted for info, and we don't
6856     ;; want the window conf to change before that...
6857     (gnus-summary-exit t)
6858     (while (not entered)
6859       ;; Then we find what group we are supposed to enter.
6860       (set-buffer gnus-group-buffer)
6861       (gnus-group-jump-to-group current-group)
6862       (setq target-group
6863             (or target-group
6864                 (if (eq gnus-keep-same-level 'best)
6865                     (gnus-summary-best-group gnus-newsgroup-name)
6866                   (gnus-summary-search-group backward gnus-keep-same-level))))
6867       (if (not target-group)
6868           ;; There are no further groups, so we return to the group
6869           ;; buffer.
6870           (progn
6871             (gnus-message 5 "Returning to the group buffer")
6872             (setq entered t)
6873             (when (gnus-buffer-live-p current-buffer)
6874               (set-buffer current-buffer)
6875               (gnus-summary-exit))
6876             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6877         ;; We try to enter the target group.
6878         (gnus-group-jump-to-group target-group)
6879         (let ((unreads (gnus-group-group-unread)))
6880           (if (and (or (eq t unreads)
6881                        (and unreads (not (zerop unreads))))
6882                    (gnus-summary-read-group
6883                     target-group nil no-article
6884                     (and (buffer-name current-buffer) current-buffer)
6885                     nil backward))
6886               (setq entered t)
6887             (setq current-group target-group
6888                   target-group nil)))))))
6889
6890 (defun gnus-summary-prev-group (&optional no-article)
6891   "Exit current newsgroup and then select previous unread newsgroup.
6892 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6893   (interactive "P")
6894   (gnus-summary-next-group no-article nil t))
6895
6896 ;; Walking around summary lines.
6897
6898 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6899   "Go to the first subject satisfying any non-nil constraint.
6900 If UNREAD is non-nil, the article should be unread.
6901 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6902 If UNSEEN is non-nil, the article should be unseen.
6903 Returns the article selected or nil if there are no matching articles."
6904   (interactive "P")
6905   (cond
6906    ;; Empty summary.
6907    ((null gnus-newsgroup-data)
6908     (gnus-message 3 "No articles in the group")
6909     nil)
6910    ;; Pick the first article.
6911    ((not (or unread undownloaded unseen))
6912     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6913     (gnus-data-number (car gnus-newsgroup-data)))
6914    ;; Find the first unread article.
6915    (t
6916     (let ((data gnus-newsgroup-data))
6917       (while (and data
6918                   (let ((num (gnus-data-number (car data))))
6919                     (or (memq num gnus-newsgroup-unfetched)
6920                         (not (or (and unread
6921                                       (memq num gnus-newsgroup-unreads))
6922                                  (and undownloaded
6923                                       (memq num gnus-newsgroup-undownloaded))
6924                                  (and unseen
6925                                       (memq num gnus-newsgroup-unseen)))))))
6926         (setq data (cdr data)))
6927       (prog1
6928           (if data
6929               (progn
6930                 (goto-char (gnus-data-pos (car data)))
6931                 (gnus-data-number (car data)))
6932             (gnus-message 3 "No more%s articles"
6933                           (let* ((r (when unread " unread"))
6934                                  (d (when undownloaded " undownloaded"))
6935                                  (s (when unseen " unseen"))
6936                                  (l (delq nil (list r d s))))
6937                             (cond ((= 3 (length l))
6938                                    (concat r "," d ", or" s))
6939                                   ((= 2 (length l))
6940                                    (concat (car l) ", or" (cadr l)))
6941                                   ((= 1 (length l))
6942                                    (car l))
6943                                   (t
6944                                    ""))))
6945             nil
6946             )
6947         (gnus-summary-position-point))))))
6948
6949 (defun gnus-summary-next-subject (n &optional unread dont-display)
6950   "Go to next N'th summary line.
6951 If N is negative, go to the previous N'th subject line.
6952 If UNREAD is non-nil, only unread articles are selected.
6953 The difference between N and the actual number of steps taken is
6954 returned."
6955   (interactive "p")
6956   (let ((backward (< n 0))
6957         (n (abs n)))
6958     (while (and (> n 0)
6959                 (if backward
6960                     (gnus-summary-find-prev unread)
6961                   (gnus-summary-find-next unread)))
6962       (unless (zerop (setq n (1- n)))
6963         (gnus-summary-show-thread)))
6964     (when (/= 0 n)
6965       (gnus-message 7 "No more%s articles"
6966                     (if unread " unread" "")))
6967     (unless dont-display
6968       (gnus-summary-recenter)
6969       (gnus-summary-position-point))
6970     n))
6971
6972 (defun gnus-summary-next-unread-subject (n)
6973   "Go to next N'th unread summary line."
6974   (interactive "p")
6975   (gnus-summary-next-subject n t))
6976
6977 (defun gnus-summary-prev-subject (n &optional unread)
6978   "Go to previous N'th summary line.
6979 If optional argument UNREAD is non-nil, only unread article is selected."
6980   (interactive "p")
6981   (gnus-summary-next-subject (- n) unread))
6982
6983 (defun gnus-summary-prev-unread-subject (n)
6984   "Go to previous N'th unread summary line."
6985   (interactive "p")
6986   (gnus-summary-next-subject (- n) t))
6987
6988 (defun gnus-summary-goto-subjects (articles)
6989   "Insert the subject header for ARTICLES in the current buffer."
6990   (save-excursion
6991     (dolist (article articles)
6992       (gnus-summary-goto-subject article t)))
6993   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6994   (gnus-summary-position-point))
6995  
6996 (defun gnus-summary-goto-subject (article &optional force silent)
6997   "Go the subject line of ARTICLE.
6998 If FORCE, also allow jumping to articles not currently shown."
6999   (interactive "nArticle number: ")
7000   (unless (numberp article)
7001     (error "Article %s is not a number" article))
7002   (let ((b (point))
7003         (data (gnus-data-find article)))
7004     ;; We read in the article if we have to.
7005     (and (not data)
7006          force
7007          (gnus-summary-insert-subject
7008           article
7009           (if (or (numberp force) (vectorp force)) force)
7010           t)
7011          (setq data (gnus-data-find article)))
7012     (goto-char b)
7013     (if (not data)
7014         (progn
7015           (unless silent
7016             (gnus-message 3 "Can't find article %d" article))
7017           nil)
7018       (let ((pt (gnus-data-pos data)))
7019         (goto-char pt)
7020         (gnus-summary-set-article-display-arrow pt))
7021       (gnus-summary-position-point)
7022       article)))
7023
7024 ;; Walking around summary lines with displaying articles.
7025
7026 (defun gnus-summary-expand-window (&optional arg)
7027   "Make the summary buffer take up the entire Emacs frame.
7028 Given a prefix, will force an `article' buffer configuration."
7029   (interactive "P")
7030   (if arg
7031       (gnus-configure-windows 'article 'force)
7032     (gnus-configure-windows 'summary 'force)))
7033
7034 (defun gnus-summary-display-article (article &optional all-header)
7035   "Display ARTICLE in article buffer."
7036   (when (gnus-buffer-live-p gnus-article-buffer)
7037     (with-current-buffer gnus-article-buffer
7038       (mm-enable-multibyte)))
7039   (gnus-set-global-variables)
7040   (when (gnus-buffer-live-p gnus-article-buffer)
7041     (with-current-buffer gnus-article-buffer
7042       (setq gnus-article-charset gnus-newsgroup-charset)
7043       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7044       (mm-enable-multibyte)))
7045   (if (null article)
7046       nil
7047     (prog1
7048         (if gnus-summary-display-article-function
7049             (funcall gnus-summary-display-article-function article all-header)
7050           (gnus-article-prepare article all-header))
7051       (gnus-run-hooks 'gnus-select-article-hook)
7052       (when (and gnus-current-article
7053                  (not (zerop gnus-current-article)))
7054         (gnus-summary-goto-subject gnus-current-article))
7055       (gnus-summary-recenter)
7056       (when (and gnus-use-trees gnus-show-threads)
7057         (gnus-possibly-generate-tree article)
7058         (gnus-highlight-selected-tree article))
7059       ;; Successfully display article.
7060       (gnus-article-set-window-start
7061        (cdr (assq article gnus-newsgroup-bookmarks))))))
7062
7063 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7064   "Select the current article.
7065 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7066 non-nil, the article will be re-fetched even if it already present in
7067 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7068 be displayed."
7069   ;; Make sure we are in the summary buffer to work around bbdb bug.
7070   (unless (eq major-mode 'gnus-summary-mode)
7071     (set-buffer gnus-summary-buffer))
7072   (let ((article (or article (gnus-summary-article-number)))
7073         (all-headers (not (not all-headers))) ;Must be t or nil.
7074         gnus-summary-display-article-function)
7075     (and (not pseudo)
7076          (gnus-summary-article-pseudo-p article)
7077          (error "This is a pseudo-article"))
7078     (save-excursion
7079       (set-buffer gnus-summary-buffer)
7080       (if (or (and gnus-single-article-buffer
7081                    (or (null gnus-current-article)
7082                        (null gnus-article-current)
7083                        (null (get-buffer gnus-article-buffer))
7084                        (not (eq article (cdr gnus-article-current)))
7085                        (not (equal (car gnus-article-current)
7086                                    gnus-newsgroup-name))))
7087               (and (not gnus-single-article-buffer)
7088                    (or (null gnus-current-article)
7089                        (not (eq gnus-current-article article))))
7090               force)
7091           ;; The requested article is different from the current article.
7092           (progn
7093             (gnus-summary-display-article article all-headers)
7094             (when (gnus-buffer-live-p gnus-article-buffer)
7095               (with-current-buffer gnus-article-buffer
7096                 (if (not gnus-article-decoded-p) ;; a local variable
7097                     (mm-disable-multibyte))))
7098             (gnus-article-set-window-start
7099              (cdr (assq article gnus-newsgroup-bookmarks)))
7100             article)
7101         'old))))
7102
7103 (defun gnus-summary-force-verify-and-decrypt ()
7104   "Display buttons for signed/encrypted parts and verify/decrypt them."
7105   (interactive)
7106   (let ((mm-verify-option 'known)
7107         (mm-decrypt-option 'known)
7108         (gnus-article-emulate-mime t)
7109         (gnus-buttonized-mime-types (append (list "multipart/signed"
7110                                                   "multipart/encrypted")
7111                                             gnus-buttonized-mime-types)))
7112     (gnus-summary-select-article nil 'force)))
7113
7114 (defun gnus-summary-set-current-mark (&optional current-mark)
7115   "Obsolete function."
7116   nil)
7117
7118 (defun gnus-summary-next-article (&optional unread subject backward push)
7119   "Select the next article.
7120 If UNREAD, only unread articles are selected.
7121 If SUBJECT, only articles with SUBJECT are selected.
7122 If BACKWARD, the previous article is selected instead of the next."
7123   (interactive "P")
7124   (cond
7125    ;; Is there such an article?
7126    ((and (gnus-summary-search-forward unread subject backward)
7127          (or (gnus-summary-display-article (gnus-summary-article-number))
7128              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7129     (gnus-summary-position-point))
7130    ;; If not, we try the first unread, if that is wanted.
7131    ((and subject
7132          gnus-auto-select-same
7133          (gnus-summary-first-unread-article))
7134     (gnus-summary-position-point)
7135     (gnus-message 6 "Wrapped"))
7136    ;; Try to get next/previous article not displayed in this group.
7137    ((and gnus-auto-extend-newsgroup
7138          (not unread) (not subject))
7139     (gnus-summary-goto-article
7140      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7141      nil (count-lines (point-min) (point))))
7142    ;; Go to next/previous group.
7143    (t
7144     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7145       (gnus-summary-jump-to-group gnus-newsgroup-name))
7146     (let ((cmd last-command-char)
7147           (point
7148            (save-excursion
7149              (set-buffer gnus-group-buffer)
7150              (point)))
7151           (group
7152            (if (eq gnus-keep-same-level 'best)
7153                (gnus-summary-best-group gnus-newsgroup-name)
7154              (gnus-summary-search-group backward gnus-keep-same-level))))
7155       ;; For some reason, the group window gets selected.  We change
7156       ;; it back.
7157       (select-window (get-buffer-window (current-buffer)))
7158       ;; Select next unread newsgroup automagically.
7159       (cond
7160        ((or (not gnus-auto-select-next)
7161             (not cmd))
7162         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7163        ((or (eq gnus-auto-select-next 'quietly)
7164             (and (eq gnus-auto-select-next 'slightly-quietly)
7165                  push)
7166             (and (eq gnus-auto-select-next 'almost-quietly)
7167                  (gnus-summary-last-article-p)))
7168         ;; Select quietly.
7169         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7170             (gnus-summary-exit)
7171           (gnus-message 7 "No more%s articles (%s)..."
7172                         (if unread " unread" "")
7173                         (if group (concat "selecting " group)
7174                           "exiting"))
7175           (gnus-summary-next-group nil group backward)))
7176        (t
7177         (when (gnus-key-press-event-p last-input-event)
7178           (gnus-summary-walk-group-buffer
7179            gnus-newsgroup-name cmd unread backward point))))))))
7180
7181 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7182   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7183                       (?\C-p (gnus-group-prev-unread-group 1))))
7184         (cursor-in-echo-area t)
7185         keve key group ended prompt)
7186     (save-excursion
7187       (set-buffer gnus-group-buffer)
7188       (goto-char start)
7189       (setq group
7190             (if (eq gnus-keep-same-level 'best)
7191                 (gnus-summary-best-group gnus-newsgroup-name)
7192               (gnus-summary-search-group backward gnus-keep-same-level))))
7193     (while (not ended)
7194       (setq prompt
7195             (format
7196              "No more%s articles%s " (if unread " unread" "")
7197              (if (and group
7198                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7199                  (format " (Type %s for %s [%s])"
7200                          (single-key-description cmd) group
7201                          (gnus-group-unread group))
7202                (format " (Type %s to exit %s)"
7203                        (single-key-description cmd)
7204                        gnus-newsgroup-name))))
7205       ;; Confirm auto selection.
7206       (setq key (car (setq keve (gnus-read-event-char prompt)))
7207             ended t)
7208       (cond
7209        ((assq key keystrokes)
7210         (let ((obuf (current-buffer)))
7211           (switch-to-buffer gnus-group-buffer)
7212           (when group
7213             (gnus-group-jump-to-group group))
7214           (eval (cadr (assq key keystrokes)))
7215           (setq group (gnus-group-group-name))
7216           (switch-to-buffer obuf))
7217         (setq ended nil))
7218        ((equal key cmd)
7219         (if (or (not group)
7220                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7221             (gnus-summary-exit)
7222           (gnus-summary-next-group nil group backward)))
7223        (t
7224         (push (cdr keve) unread-command-events))))))
7225
7226 (defun gnus-summary-next-unread-article ()
7227   "Select unread article after current one."
7228   (interactive)
7229   (gnus-summary-next-article
7230    (or (not (eq gnus-summary-goto-unread 'never))
7231        (gnus-summary-last-article-p (gnus-summary-article-number)))
7232    (and gnus-auto-select-same
7233         (gnus-summary-article-subject))))
7234
7235 (defun gnus-summary-prev-article (&optional unread subject)
7236   "Select the article after the current one.
7237 If UNREAD is non-nil, only unread articles are selected."
7238   (interactive "P")
7239   (gnus-summary-next-article unread subject t))
7240
7241 (defun gnus-summary-prev-unread-article ()
7242   "Select unread article before current one."
7243   (interactive)
7244   (gnus-summary-prev-article
7245    (or (not (eq gnus-summary-goto-unread 'never))
7246        (gnus-summary-first-article-p (gnus-summary-article-number)))
7247    (and gnus-auto-select-same
7248         (gnus-summary-article-subject))))
7249
7250 (defun gnus-summary-next-page (&optional lines circular stop)
7251   "Show next page of the selected article.
7252 If at the end of the current article, select the next article.
7253 LINES says how many lines should be scrolled up.
7254
7255 If CIRCULAR is non-nil, go to the start of the article instead of
7256 selecting the next article when reaching the end of the current
7257 article.
7258
7259 If STOP is non-nil, just stop when reaching the end of the message.
7260
7261 Also see the variable `gnus-article-skip-boring'."
7262   (interactive "P")
7263   (setq gnus-summary-buffer (current-buffer))
7264   (gnus-set-global-variables)
7265   (let ((article (gnus-summary-article-number))
7266         (article-window (get-buffer-window gnus-article-buffer t))
7267         endp)
7268     ;; If the buffer is empty, we have no article.
7269     (unless article
7270       (error "No article to select"))
7271     (gnus-configure-windows 'article)
7272     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7273         (if (and (eq gnus-summary-goto-unread 'never)
7274                  (not (gnus-summary-last-article-p article)))
7275             (gnus-summary-next-article)
7276           (gnus-summary-next-unread-article))
7277       (if (or (null gnus-current-article)
7278               (null gnus-article-current)
7279               (/= article (cdr gnus-article-current))
7280               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7281           ;; Selected subject is different from current article's.
7282           (gnus-summary-display-article article)
7283         (when article-window
7284           (gnus-eval-in-buffer-window gnus-article-buffer
7285             (setq endp (or (gnus-article-next-page lines)
7286                            (gnus-article-only-boring-p))))
7287           (when endp
7288             (cond (stop
7289                    (gnus-message 3 "End of message"))
7290                   (circular
7291                    (gnus-summary-beginning-of-article))
7292                   (lines
7293                    (gnus-message 3 "End of message"))
7294                   ((null lines)
7295                    (if (and (eq gnus-summary-goto-unread 'never)
7296                             (not (gnus-summary-last-article-p article)))
7297                        (gnus-summary-next-article)
7298                      (gnus-summary-next-unread-article))))))))
7299     (gnus-summary-recenter)
7300     (gnus-summary-position-point)))
7301
7302 (defun gnus-summary-prev-page (&optional lines move)
7303   "Show previous page of selected article.
7304 Argument LINES specifies lines to be scrolled down.
7305 If MOVE, move to the previous unread article if point is at
7306 the beginning of the buffer."
7307   (interactive "P")
7308   (let ((article (gnus-summary-article-number))
7309         (article-window (get-buffer-window gnus-article-buffer t))
7310         endp)
7311     (gnus-configure-windows 'article)
7312     (if (or (null gnus-current-article)
7313             (null gnus-article-current)
7314             (/= article (cdr gnus-article-current))
7315             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7316         ;; Selected subject is different from current article's.
7317         (gnus-summary-display-article article)
7318       (gnus-summary-recenter)
7319       (when article-window
7320         (gnus-eval-in-buffer-window gnus-article-buffer
7321           (setq endp (gnus-article-prev-page lines)))
7322         (when (and move endp)
7323           (cond (lines
7324                  (gnus-message 3 "Beginning of message"))
7325                 ((null lines)
7326                  (if (and (eq gnus-summary-goto-unread 'never)
7327                           (not (gnus-summary-first-article-p article)))
7328                      (gnus-summary-prev-article)
7329                    (gnus-summary-prev-unread-article))))))))
7330   (gnus-summary-position-point))
7331
7332 (defun gnus-summary-prev-page-or-article (&optional lines)
7333   "Show previous page of selected article.
7334 Argument LINES specifies lines to be scrolled down.
7335 If at the beginning of the article, go to the next article."
7336   (interactive "P")
7337   (gnus-summary-prev-page lines t))
7338
7339 (defun gnus-summary-scroll-up (lines)
7340   "Scroll up (or down) one line current article.
7341 Argument LINES specifies lines to be scrolled up (or down if negative)."
7342   (interactive "p")
7343   (gnus-configure-windows 'article)
7344   (gnus-summary-show-thread)
7345   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7346     (gnus-eval-in-buffer-window gnus-article-buffer
7347       (cond ((> lines 0)
7348              (when (gnus-article-next-page lines)
7349                (gnus-message 3 "End of message")))
7350             ((< lines 0)
7351              (gnus-article-prev-page (- lines))))))
7352   (gnus-summary-recenter)
7353   (gnus-summary-position-point))
7354
7355 (defun gnus-summary-scroll-down (lines)
7356   "Scroll down (or up) one line current article.
7357 Argument LINES specifies lines to be scrolled down (or up if negative)."
7358   (interactive "p")
7359   (gnus-summary-scroll-up (- lines)))
7360
7361 (defun gnus-summary-next-same-subject ()
7362   "Select next article which has the same subject as current one."
7363   (interactive)
7364   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7365
7366 (defun gnus-summary-prev-same-subject ()
7367   "Select previous article which has the same subject as current one."
7368   (interactive)
7369   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7370
7371 (defun gnus-summary-next-unread-same-subject ()
7372   "Select next unread article which has the same subject as current one."
7373   (interactive)
7374   (gnus-summary-next-article t (gnus-summary-article-subject)))
7375
7376 (defun gnus-summary-prev-unread-same-subject ()
7377   "Select previous unread article which has the same subject as current one."
7378   (interactive)
7379   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7380
7381 (defun gnus-summary-first-unread-article ()
7382   "Select the first unread article.
7383 Return nil if there are no unread articles."
7384   (interactive)
7385   (prog1
7386       (when (gnus-summary-first-subject t)
7387         (gnus-summary-show-thread)
7388         (gnus-summary-first-subject t)
7389         (gnus-summary-display-article (gnus-summary-article-number)))
7390     (gnus-summary-position-point)))
7391
7392 (defun gnus-summary-first-unread-subject ()
7393   "Place the point on the subject line of the first unread article.
7394 Return nil if there are no unread articles."
7395   (interactive)
7396   (prog1
7397       (when (gnus-summary-first-subject t)
7398         (gnus-summary-show-thread)
7399         (gnus-summary-first-subject t))
7400     (gnus-summary-position-point)))
7401
7402 (defun gnus-summary-first-unseen-subject ()
7403   "Place the point on the subject line of the first unseen article.
7404 Return nil if there are no unseen articles."
7405   (interactive)
7406   (prog1
7407       (when (gnus-summary-first-subject nil nil t)
7408         (gnus-summary-show-thread)
7409         (gnus-summary-first-subject nil nil t))
7410     (gnus-summary-position-point)))
7411
7412 (defun gnus-summary-first-unseen-or-unread-subject ()
7413   "Place the point on the subject line of the first unseen article or,
7414 if all article have been seen, on the subject line of the first unread
7415 article."
7416   (interactive)
7417   (prog1
7418       (unless (when (gnus-summary-first-subject nil nil t)
7419                 (gnus-summary-show-thread)
7420                 (gnus-summary-first-subject nil nil t))
7421         (when (gnus-summary-first-subject t)
7422           (gnus-summary-show-thread)
7423           (gnus-summary-first-subject t)))
7424     (gnus-summary-position-point)))
7425
7426 (defun gnus-summary-first-article ()
7427   "Select the first article.
7428 Return nil if there are no articles."
7429   (interactive)
7430   (prog1
7431       (when (gnus-summary-first-subject)
7432         (gnus-summary-show-thread)
7433         (gnus-summary-first-subject)
7434         (gnus-summary-display-article (gnus-summary-article-number)))
7435     (gnus-summary-position-point)))
7436
7437 (defun gnus-summary-best-unread-article (&optional arg)
7438   "Select the unread article with the highest score.
7439 If given a prefix argument, select the next unread article that has a
7440 score higher than the default score."
7441   (interactive "P")
7442   (let ((article (if arg
7443                      (gnus-summary-better-unread-subject)
7444                    (gnus-summary-best-unread-subject))))
7445     (if article
7446         (gnus-summary-goto-article article)
7447       (error "No unread articles"))))
7448
7449 (defun gnus-summary-best-unread-subject ()
7450   "Select the unread subject with the highest score."
7451   (interactive)
7452   (let ((best -1000000)
7453         (data gnus-newsgroup-data)
7454         article score)
7455     (while data
7456       (and (gnus-data-unread-p (car data))
7457            (> (setq score
7458                     (gnus-summary-article-score (gnus-data-number (car data))))
7459               best)
7460            (setq best score
7461                  article (gnus-data-number (car data))))
7462       (setq data (cdr data)))
7463     (when article
7464       (gnus-summary-goto-subject article))
7465     (gnus-summary-position-point)
7466     article))
7467
7468 (defun gnus-summary-better-unread-subject ()
7469   "Select the first unread subject that has a score over the default score."
7470   (interactive)
7471   (let ((data gnus-newsgroup-data)
7472         article score)
7473     (while (and (setq article (gnus-data-number (car data)))
7474                 (or (gnus-data-read-p (car data))
7475                     (not (> (gnus-summary-article-score article)
7476                             gnus-summary-default-score))))
7477       (setq data (cdr data)))
7478     (when article
7479       (gnus-summary-goto-subject article))
7480     (gnus-summary-position-point)
7481     article))
7482
7483 (defun gnus-summary-last-subject ()
7484   "Go to the last displayed subject line in the group."
7485   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7486     (when article
7487       (gnus-summary-goto-subject article))))
7488
7489 (defun gnus-summary-goto-article (article &optional all-headers force)
7490   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7491 If ALL-HEADERS is non-nil, no header lines are hidden.
7492 If FORCE, go to the article even if it isn't displayed.  If FORCE
7493 is a number, it is the line the article is to be displayed on."
7494   (interactive
7495    (list
7496     (completing-read
7497      "Article number or Message-ID: "
7498      (mapcar (lambda (number) (list (int-to-string number)))
7499              gnus-newsgroup-limit))
7500     current-prefix-arg
7501     t))
7502   (prog1
7503       (if (and (stringp article)
7504                (string-match "@\\|%40" article))
7505           (gnus-summary-refer-article article)
7506         (when (stringp article)
7507           (setq article (string-to-number article)))
7508         (if (gnus-summary-goto-subject article force)
7509             (gnus-summary-display-article article all-headers)
7510           (gnus-message 4 "Couldn't go to article %s" article) nil))
7511     (gnus-summary-position-point)))
7512
7513 (defun gnus-summary-goto-last-article ()
7514   "Go to the previously read article."
7515   (interactive)
7516   (prog1
7517       (when gnus-last-article
7518         (gnus-summary-goto-article gnus-last-article nil t))
7519     (gnus-summary-position-point)))
7520
7521 (defun gnus-summary-pop-article (number)
7522   "Pop one article off the history and go to the previous.
7523 NUMBER articles will be popped off."
7524   (interactive "p")
7525   (let (to)
7526     (setq gnus-newsgroup-history
7527           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7528     (if to
7529         (gnus-summary-goto-article (car to) nil t)
7530       (error "Article history empty")))
7531   (gnus-summary-position-point))
7532
7533 ;; Summary commands and functions for limiting the summary buffer.
7534
7535 (defun gnus-summary-limit-to-articles (n)
7536   "Limit the summary buffer to the next N articles.
7537 If not given a prefix, use the process marked articles instead."
7538   (interactive "P")
7539   (prog1
7540       (let ((articles (gnus-summary-work-articles n)))
7541         (setq gnus-newsgroup-processable nil)
7542         (gnus-summary-limit articles))
7543     (gnus-summary-position-point)))
7544
7545 (defun gnus-summary-pop-limit (&optional total)
7546   "Restore the previous limit.
7547 If given a prefix, remove all limits."
7548   (interactive "P")
7549   (when total
7550     (setq gnus-newsgroup-limits
7551           (list (mapcar (lambda (h) (mail-header-number h))
7552                         gnus-newsgroup-headers))))
7553   (unless gnus-newsgroup-limits
7554     (error "No limit to pop"))
7555   (prog1
7556       (gnus-summary-limit nil 'pop)
7557     (gnus-summary-position-point)))
7558
7559 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7560   "Limit the summary buffer to articles that have subjects that match a regexp.
7561 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7562   (interactive
7563    (list (read-string (if current-prefix-arg
7564                           "Exclude subject (regexp): "
7565                         "Limit to subject (regexp): "))
7566          nil current-prefix-arg))
7567   (unless header
7568     (setq header "subject"))
7569   (when (not (equal "" subject))
7570     (prog1
7571         (let ((articles (gnus-summary-find-matching
7572                          (or header "subject") subject 'all nil nil
7573                          not-matching)))
7574           (unless articles
7575             (error "Found no matches for \"%s\"" subject))
7576           (gnus-summary-limit articles))
7577       (gnus-summary-position-point))))
7578
7579 (defun gnus-summary-limit-to-author (from &optional not-matching)
7580   "Limit the summary buffer to articles that have authors that match a regexp.
7581 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7582   (interactive
7583    (list (read-string (if current-prefix-arg
7584                           "Exclude author (regexp): "
7585                         "Limit to author (regexp): "))
7586          current-prefix-arg))
7587   (gnus-summary-limit-to-subject from "from" not-matching))
7588
7589 (defun gnus-summary-limit-to-age (age &optional younger-p)
7590   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7591 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7592 articles that are younger than AGE days."
7593   (interactive
7594    (let ((younger current-prefix-arg)
7595          (days-got nil)
7596          days)
7597      (while (not days-got)
7598        (setq days (if younger
7599                       (read-string "Limit to articles younger than (in days, older when negative): ")
7600                     (read-string
7601                      "Limit to articles older than (in days, younger when negative): ")))
7602        (when (> (length days) 0)
7603          (setq days (read days)))
7604        (if (numberp days)
7605            (progn
7606              (setq days-got t)
7607              (when (< days 0)
7608                (setq younger (not younger))
7609                (setq days (* days -1))))
7610          (message "Please enter a number.")
7611          (sleep-for 1)))
7612      (list days younger)))
7613   (prog1
7614       (let ((data gnus-newsgroup-data)
7615             (cutoff (days-to-time age))
7616             articles d date is-younger)
7617         (while (setq d (pop data))
7618           (when (and (vectorp (gnus-data-header d))
7619                      (setq date (mail-header-date (gnus-data-header d))))
7620             (setq is-younger (time-less-p
7621                               (time-since (condition-case ()
7622                                               (date-to-time date)
7623                                             (error '(0 0))))
7624                               cutoff))
7625             (when (if younger-p
7626                       is-younger
7627                     (not is-younger))
7628               (push (gnus-data-number d) articles))))
7629         (gnus-summary-limit (nreverse articles)))
7630     (gnus-summary-position-point)))
7631
7632 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7633   "Limit the summary buffer to articles that match an 'extra' header."
7634   (interactive
7635    (let ((header
7636           (intern
7637            (gnus-completing-read-with-default
7638             (symbol-name (car gnus-extra-headers))
7639             (if current-prefix-arg
7640                 "Exclude extra header:"
7641               "Limit extra header:")
7642             (mapcar (lambda (x)
7643                       (cons (symbol-name x) x))
7644                     gnus-extra-headers)
7645             nil
7646             t))))
7647      (list header
7648            (read-string (format "%s header %s (regexp): "
7649                                 (if current-prefix-arg "Exclude" "Limit to")
7650                                 header))
7651            current-prefix-arg)))
7652   (when (not (equal "" regexp))
7653     (prog1
7654         (let ((articles (gnus-summary-find-matching
7655                          (cons 'extra header) regexp 'all nil nil
7656                          not-matching)))
7657           (unless articles
7658             (error "Found no matches for \"%s\"" regexp))
7659           (gnus-summary-limit articles))
7660       (gnus-summary-position-point))))
7661
7662 (defun gnus-summary-limit-to-display-predicate ()
7663   "Limit the summary buffer to the predicated in the `display' group parameter."
7664   (interactive)
7665   (unless gnus-newsgroup-display
7666     (error "There is no `display' group parameter"))
7667   (let (articles)
7668     (dolist (number gnus-newsgroup-articles)
7669       (when (funcall gnus-newsgroup-display)
7670         (push number articles)))
7671     (gnus-summary-limit articles))
7672   (gnus-summary-position-point))
7673
7674 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7675 (make-obsolete
7676  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7677
7678 (defun gnus-summary-limit-to-unread (&optional all)
7679   "Limit the summary buffer to articles that are not marked as read.
7680 If ALL is non-nil, limit strictly to unread articles."
7681   (interactive "P")
7682   (if all
7683       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7684     (gnus-summary-limit-to-marks
7685      ;; Concat all the marks that say that an article is read and have
7686      ;; those removed.
7687      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7688            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7689            gnus-low-score-mark gnus-expirable-mark
7690            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7691            gnus-duplicate-mark gnus-souped-mark)
7692      'reverse)))
7693
7694 (defun gnus-summary-limit-to-replied (&optional unreplied)
7695   "Limit the summary buffer to replied articles.
7696 If UNREPLIED (the prefix), limit to unreplied articles."
7697   (interactive "P")
7698   (if unreplied
7699       (gnus-summary-limit
7700        (gnus-set-difference gnus-newsgroup-articles
7701         gnus-newsgroup-replied))
7702     (gnus-summary-limit gnus-newsgroup-replied))
7703   (gnus-summary-position-point))
7704
7705 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7706 (make-obsolete 'gnus-summary-delete-marked-with
7707                'gnus-summary-limit-exclude-marks)
7708
7709 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7710   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7711 If REVERSE, limit the summary buffer to articles that are marked
7712 with MARKS.  MARKS can either be a string of marks or a list of marks.
7713 Returns how many articles were removed."
7714   (interactive "sMarks: ")
7715   (gnus-summary-limit-to-marks marks t))
7716
7717 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7718   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7719 If REVERSE (the prefix), limit the summary buffer to articles that are
7720 not marked with MARKS.  MARKS can either be a string of marks or a
7721 list of marks.
7722 Returns how many articles were removed."
7723   (interactive "sMarks: \nP")
7724   (prog1
7725       (let ((data gnus-newsgroup-data)
7726             (marks (if (listp marks) marks
7727                      (append marks nil))) ; Transform to list.
7728             articles)
7729         (while data
7730           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7731                   (memq (gnus-data-mark (car data)) marks))
7732             (push (gnus-data-number (car data)) articles))
7733           (setq data (cdr data)))
7734         (gnus-summary-limit articles))
7735     (gnus-summary-position-point)))
7736
7737 (defun gnus-summary-limit-to-score (score)
7738   "Limit to articles with score at or above SCORE."
7739   (interactive "NLimit to articles with score of at least: ")
7740   (let ((data gnus-newsgroup-data)
7741         articles)
7742     (while data
7743       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7744                 score)
7745         (push (gnus-data-number (car data)) articles))
7746       (setq data (cdr data)))
7747     (prog1
7748         (gnus-summary-limit articles)
7749       (gnus-summary-position-point))))
7750
7751 (defun gnus-summary-limit-to-unseen ()
7752   "Limit to unseen articles."
7753   (interactive)
7754   (prog1
7755       (gnus-summary-limit gnus-newsgroup-unseen)
7756     (gnus-summary-position-point)))
7757
7758 (defun gnus-summary-limit-include-thread (id)
7759   "Display all the hidden articles that is in the thread with ID in it.
7760 When called interactively, ID is the Message-ID of the current
7761 article."
7762   (interactive (list (mail-header-id (gnus-summary-article-header))))
7763   (let ((articles (gnus-articles-in-thread
7764                    (gnus-id-to-thread (gnus-root-id id)))))
7765     (prog1
7766         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7767       (gnus-summary-limit-include-matching-articles
7768        "subject"
7769        (regexp-quote (gnus-simplify-subject-re
7770                       (mail-header-subject (gnus-id-to-header id)))))
7771       (gnus-summary-position-point))))
7772
7773 (defun gnus-summary-limit-include-matching-articles (header regexp)
7774   "Display all the hidden articles that have HEADERs that match REGEXP."
7775   (interactive (list (read-string "Match on header: ")
7776                      (read-string "Regexp: ")))
7777   (let ((articles (gnus-find-matching-articles header regexp)))
7778     (prog1
7779         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7780       (gnus-summary-position-point))))
7781
7782 (defun gnus-summary-insert-dormant-articles ()
7783   "Insert all the dormant articles for this group into the current buffer."
7784   (interactive)
7785   (let ((gnus-verbose (max 6 gnus-verbose)))
7786     (if (not gnus-newsgroup-dormant)
7787         (gnus-message 3 "No cached articles for this group")
7788       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7789
7790 (defun gnus-summary-limit-include-dormant ()
7791   "Display all the hidden articles that are marked as dormant.
7792 Note that this command only works on a subset of the articles currently
7793 fetched for this group."
7794   (interactive)
7795   (unless gnus-newsgroup-dormant
7796     (error "There are no dormant articles in this group"))
7797   (prog1
7798       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7799     (gnus-summary-position-point)))
7800
7801 (defun gnus-summary-limit-exclude-dormant ()
7802   "Hide all dormant articles."
7803   (interactive)
7804   (prog1
7805       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7806     (gnus-summary-position-point)))
7807
7808 (defun gnus-summary-limit-exclude-childless-dormant ()
7809   "Hide all dormant articles that have no children."
7810   (interactive)
7811   (let ((data (gnus-data-list t))
7812         articles d children)
7813     ;; Find all articles that are either not dormant or have
7814     ;; children.
7815     (while (setq d (pop data))
7816       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7817                 (and (setq children
7818                            (gnus-article-children (gnus-data-number d)))
7819                      (let (found)
7820                        (while children
7821                          (when (memq (car children) articles)
7822                            (setq children nil
7823                                  found t))
7824                          (pop children))
7825                        found)))
7826         (push (gnus-data-number d) articles)))
7827     ;; Do the limiting.
7828     (prog1
7829         (gnus-summary-limit articles)
7830       (gnus-summary-position-point))))
7831
7832 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7833   "Mark all unread excluded articles as read.
7834 If ALL, mark even excluded ticked and dormants as read."
7835   (interactive "P")
7836   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7837   (let ((articles (gnus-sorted-ndifference
7838                    (sort
7839                     (mapcar (lambda (h) (mail-header-number h))
7840                             gnus-newsgroup-headers)
7841                     '<)
7842                    gnus-newsgroup-limit))
7843         article)
7844     (setq gnus-newsgroup-unreads
7845           (gnus-sorted-intersection gnus-newsgroup-unreads
7846                                     gnus-newsgroup-limit))
7847     (if all
7848         (setq gnus-newsgroup-dormant nil
7849               gnus-newsgroup-marked nil
7850               gnus-newsgroup-reads
7851               (nconc
7852                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7853                gnus-newsgroup-reads))
7854       (while (setq article (pop articles))
7855         (unless (or (memq article gnus-newsgroup-dormant)
7856                     (memq article gnus-newsgroup-marked))
7857           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7858
7859 (defun gnus-summary-limit (articles &optional pop)
7860   (if pop
7861       ;; We pop the previous limit off the stack and use that.
7862       (setq articles (car gnus-newsgroup-limits)
7863             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7864     ;; We use the new limit, so we push the old limit on the stack.
7865     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7866   ;; Set the limit.
7867   (setq gnus-newsgroup-limit articles)
7868   (let ((total (length gnus-newsgroup-data))
7869         (data (gnus-data-find-list (gnus-summary-article-number)))
7870         (gnus-summary-mark-below nil)   ; Inhibit this.
7871         found)
7872     ;; This will do all the work of generating the new summary buffer
7873     ;; according to the new limit.
7874     (gnus-summary-prepare)
7875     ;; Hide any threads, possibly.
7876     (gnus-summary-maybe-hide-threads)
7877     ;; Try to return to the article you were at, or one in the
7878     ;; neighborhood.
7879     (when data
7880       ;; We try to find some article after the current one.
7881       (while data
7882         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7883           (setq data nil
7884                 found t))
7885         (setq data (cdr data))))
7886     (unless found
7887       ;; If there is no data, that means that we were after the last
7888       ;; article.  The same goes when we can't find any articles
7889       ;; after the current one.
7890       (goto-char (point-max))
7891       (gnus-summary-find-prev))
7892     (gnus-set-mode-line 'summary)
7893     ;; We return how many articles were removed from the summary
7894     ;; buffer as a result of the new limit.
7895     (- total (length gnus-newsgroup-data))))
7896
7897 (defsubst gnus-invisible-cut-children (threads)
7898   (let ((num 0))
7899     (while threads
7900       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7901         (incf num))
7902       (pop threads))
7903     (< num 2)))
7904
7905 (defsubst gnus-cut-thread (thread)
7906   "Go forwards in the thread until we find an article that we want to display."
7907   (when (or (eq gnus-fetch-old-headers 'some)
7908             (eq gnus-fetch-old-headers 'invisible)
7909             (numberp gnus-fetch-old-headers)
7910             (eq gnus-build-sparse-threads 'some)
7911             (eq gnus-build-sparse-threads 'more))
7912     ;; Deal with old-fetched headers and sparse threads.
7913     (while (and
7914             thread
7915             (or
7916              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7917              (gnus-summary-article-ancient-p
7918               (mail-header-number (car thread))))
7919             (if (or (<= (length (cdr thread)) 1)
7920                     (eq gnus-fetch-old-headers 'invisible))
7921                 (setq gnus-newsgroup-limit
7922                       (delq (mail-header-number (car thread))
7923                             gnus-newsgroup-limit)
7924                       thread (cadr thread))
7925               (when (gnus-invisible-cut-children (cdr thread))
7926                 (let ((th (cdr thread)))
7927                   (while th
7928                     (if (memq (mail-header-number (caar th))
7929                               gnus-newsgroup-limit)
7930                         (setq thread (car th)
7931                               th nil)
7932                       (setq th (cdr th))))))))))
7933   thread)
7934
7935 (defun gnus-cut-threads (threads)
7936   "Cut off all uninteresting articles from the beginning of THREADS."
7937   (when (or (eq gnus-fetch-old-headers 'some)
7938             (eq gnus-fetch-old-headers 'invisible)
7939             (numberp gnus-fetch-old-headers)
7940             (eq gnus-build-sparse-threads 'some)
7941             (eq gnus-build-sparse-threads 'more))
7942     (let ((th threads))
7943       (while th
7944         (setcar th (gnus-cut-thread (car th)))
7945         (setq th (cdr th)))))
7946   ;; Remove nixed out threads.
7947   (delq nil threads))
7948
7949 (defun gnus-summary-initial-limit (&optional show-if-empty)
7950   "Figure out what the initial limit is supposed to be on group entry.
7951 This entails weeding out unwanted dormants, low-scored articles,
7952 fetch-old-headers verbiage, and so on."
7953   ;; Most groups have nothing to remove.
7954   (if (or gnus-inhibit-limiting
7955           (and (null gnus-newsgroup-dormant)
7956                (eq gnus-newsgroup-display 'gnus-not-ignore)
7957                (not (eq gnus-fetch-old-headers 'some))
7958                (not (numberp gnus-fetch-old-headers))
7959                (not (eq gnus-fetch-old-headers 'invisible))
7960                (null gnus-summary-expunge-below)
7961                (not (eq gnus-build-sparse-threads 'some))
7962                (not (eq gnus-build-sparse-threads 'more))
7963                (null gnus-thread-expunge-below)
7964                (not gnus-use-nocem)))
7965       ()                                ; Do nothing.
7966     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7967     (setq gnus-newsgroup-limit nil)
7968     (mapatoms
7969      (lambda (node)
7970        (unless (car (symbol-value node))
7971          ;; These threads have no parents -- they are roots.
7972          (let ((nodes (cdr (symbol-value node)))
7973                thread)
7974            (while nodes
7975              (if (and gnus-thread-expunge-below
7976                       (< (gnus-thread-total-score (car nodes))
7977                          gnus-thread-expunge-below))
7978                  (gnus-expunge-thread (pop nodes))
7979                (setq thread (pop nodes))
7980                (gnus-summary-limit-children thread))))))
7981      gnus-newsgroup-dependencies)
7982     ;; If this limitation resulted in an empty group, we might
7983     ;; pop the previous limit and use it instead.
7984     (when (and (not gnus-newsgroup-limit)
7985                show-if-empty)
7986       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7987     gnus-newsgroup-limit))
7988
7989 (defun gnus-summary-limit-children (thread)
7990   "Return 1 if this subthread is visible and 0 if it is not."
7991   ;; First we get the number of visible children to this thread.  This
7992   ;; is done by recursing down the thread using this function, so this
7993   ;; will really go down to a leaf article first, before slowly
7994   ;; working its way up towards the root.
7995   (when thread
7996     (let* ((max-lisp-eval-depth 5000)
7997            (children
7998            (if (cdr thread)
7999                (apply '+ (mapcar 'gnus-summary-limit-children
8000                                  (cdr thread)))
8001              0))
8002           (number (mail-header-number (car thread)))
8003           score)
8004       (if (and
8005            (not (memq number gnus-newsgroup-marked))
8006            (or
8007             ;; If this article is dormant and has absolutely no visible
8008             ;; children, then this article isn't visible.
8009             (and (memq number gnus-newsgroup-dormant)
8010                  (zerop children))
8011             ;; If this is "fetch-old-headered" and there is no
8012             ;; visible children, then we don't want this article.
8013             (and (or (eq gnus-fetch-old-headers 'some)
8014                      (numberp gnus-fetch-old-headers))
8015                  (gnus-summary-article-ancient-p number)
8016                  (zerop children))
8017             ;; If this is "fetch-old-headered" and `invisible', then
8018             ;; we don't want this article.
8019             (and (eq gnus-fetch-old-headers 'invisible)
8020                  (gnus-summary-article-ancient-p number))
8021             ;; If this is a sparsely inserted article with no children,
8022             ;; we don't want it.
8023             (and (eq gnus-build-sparse-threads 'some)
8024                  (gnus-summary-article-sparse-p number)
8025                  (zerop children))
8026             ;; If we use expunging, and this article is really
8027             ;; low-scored, then we don't want this article.
8028             (when (and gnus-summary-expunge-below
8029                        (< (setq score
8030                                 (or (cdr (assq number gnus-newsgroup-scored))
8031                                     gnus-summary-default-score))
8032                           gnus-summary-expunge-below))
8033               ;; We increase the expunge-tally here, but that has
8034               ;; nothing to do with the limits, really.
8035               (incf gnus-newsgroup-expunged-tally)
8036               ;; We also mark as read here, if that's wanted.
8037               (when (and gnus-summary-mark-below
8038                          (< score gnus-summary-mark-below))
8039                 (setq gnus-newsgroup-unreads
8040                       (delq number gnus-newsgroup-unreads))
8041                 (if gnus-newsgroup-auto-expire
8042                     (push number gnus-newsgroup-expirable)
8043                   (push (cons number gnus-low-score-mark)
8044                         gnus-newsgroup-reads)))
8045               t)
8046             ;; Do the `display' group parameter.
8047             (and gnus-newsgroup-display
8048                  (not (funcall gnus-newsgroup-display)))
8049             ;; Check NoCeM things.
8050             (when (and gnus-use-nocem
8051                        (gnus-nocem-unwanted-article-p
8052                         (mail-header-id (car thread))))
8053               (setq gnus-newsgroup-unreads
8054                     (delq number gnus-newsgroup-unreads))
8055               t)))
8056           ;; Nope, invisible article.
8057           0
8058         ;; Ok, this article is to be visible, so we add it to the limit
8059         ;; and return 1.
8060         (push number gnus-newsgroup-limit)
8061         1))))
8062
8063 (defun gnus-expunge-thread (thread)
8064   "Mark all articles in THREAD as read."
8065   (let* ((number (mail-header-number (car thread))))
8066     (incf gnus-newsgroup-expunged-tally)
8067     ;; We also mark as read here, if that's wanted.
8068     (setq gnus-newsgroup-unreads
8069           (delq number gnus-newsgroup-unreads))
8070     (if gnus-newsgroup-auto-expire
8071         (push number gnus-newsgroup-expirable)
8072       (push (cons number gnus-low-score-mark)
8073             gnus-newsgroup-reads)))
8074   ;; Go recursively through all subthreads.
8075   (mapcar 'gnus-expunge-thread (cdr thread)))
8076
8077 ;; Summary article oriented commands
8078
8079 (defun gnus-summary-refer-parent-article (n)
8080   "Refer parent article N times.
8081 If N is negative, go to ancestor -N instead.
8082 The difference between N and the number of articles fetched is returned."
8083   (interactive "p")
8084   (let ((skip 1)
8085         error header ref)
8086     (when (not (natnump n))
8087       (setq skip (abs n)
8088             n 1))
8089     (while (and (> n 0)
8090                 (not error))
8091       (setq header (gnus-summary-article-header))
8092       (if (and (eq (mail-header-number header)
8093                    (cdr gnus-article-current))
8094                (equal gnus-newsgroup-name
8095                       (car gnus-article-current)))
8096           ;; If we try to find the parent of the currently
8097           ;; displayed article, then we take a look at the actual
8098           ;; References header, since this is slightly more
8099           ;; reliable than the References field we got from the
8100           ;; server.
8101           (save-excursion
8102             (set-buffer gnus-original-article-buffer)
8103             (nnheader-narrow-to-headers)
8104             (unless (setq ref (message-fetch-field "references"))
8105               (when (setq ref (message-fetch-field "in-reply-to"))
8106                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8107             (widen))
8108         (setq ref
8109               ;; It's not the current article, so we take a bet on
8110               ;; the value we got from the server.
8111               (mail-header-references header)))
8112       (if (and ref
8113                (not (equal ref "")))
8114           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8115             (gnus-message 1 "Couldn't find parent"))
8116         (gnus-message 1 "No references in article %d"
8117                       (gnus-summary-article-number))
8118         (setq error t))
8119       (decf n))
8120     (gnus-summary-position-point)
8121     n))
8122
8123 (defun gnus-summary-refer-references ()
8124   "Fetch all articles mentioned in the References header.
8125 Return the number of articles fetched."
8126   (interactive)
8127   (let ((ref (mail-header-references (gnus-summary-article-header)))
8128         (current (gnus-summary-article-number))
8129         (n 0))
8130     (if (or (not ref)
8131             (equal ref ""))
8132         (error "No References in the current article")
8133       ;; For each Message-ID in the References header...
8134       (while (string-match "<[^>]*>" ref)
8135         (incf n)
8136         ;; ... fetch that article.
8137         (gnus-summary-refer-article
8138          (prog1 (match-string 0 ref)
8139            (setq ref (substring ref (match-end 0))))))
8140       (gnus-summary-goto-subject current)
8141       (gnus-summary-position-point)
8142       n)))
8143
8144 (defun gnus-summary-refer-thread (&optional limit)
8145   "Fetch all articles in the current thread.
8146 If LIMIT (the numerical prefix), fetch that many old headers instead
8147 of what's specified by the `gnus-refer-thread-limit' variable."
8148   (interactive "P")
8149   (let ((id (mail-header-id (gnus-summary-article-header)))
8150         (limit (if limit (prefix-numeric-value limit)
8151                  gnus-refer-thread-limit)))
8152     (unless (eq gnus-fetch-old-headers 'invisible)
8153       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8154       ;; Retrieve the headers and read them in.
8155       (if (eq (if (numberp limit)
8156                   (gnus-retrieve-headers
8157                    (list (min
8158                           (+ (mail-header-number
8159                               (gnus-summary-article-header))
8160                              limit)
8161                           gnus-newsgroup-end))
8162                    gnus-newsgroup-name (* limit 2))
8163                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8164                 ;; headers.
8165                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8166                                        gnus-newsgroup-name limit))
8167               'nov)
8168           (gnus-build-all-threads)
8169         (error "Can't fetch thread from back ends that don't support NOV"))
8170       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8171     (gnus-summary-limit-include-thread id)))
8172
8173 (defun gnus-summary-refer-article (message-id)
8174   "Fetch an article specified by MESSAGE-ID."
8175   (interactive "sMessage-ID: ")
8176   (when (and (stringp message-id)
8177              (not (zerop (length message-id))))
8178     (setq message-id (gnus-replace-in-string message-id " " ""))
8179     ;; Construct the correct Message-ID if necessary.
8180     ;; Suggested by tale@pawl.rpi.edu.
8181     (unless (string-match "^<" message-id)
8182       (setq message-id (concat "<" message-id)))
8183     (unless (string-match ">$" message-id)
8184       (setq message-id (concat message-id ">")))
8185     ;; People often post MIDs from URLs, so unhex it:
8186     (unless (string-match "@" message-id)
8187       (setq message-id (gnus-url-unhex-string message-id)))
8188     (let* ((header (gnus-id-to-header message-id))
8189            (sparse (and header
8190                         (gnus-summary-article-sparse-p
8191                          (mail-header-number header))
8192                         (memq (mail-header-number header)
8193                               gnus-newsgroup-limit)))
8194            number)
8195       (cond
8196        ;; If the article is present in the buffer we just go to it.
8197        ((and header
8198              (or (not (gnus-summary-article-sparse-p
8199                        (mail-header-number header)))
8200                  sparse))
8201         (prog1
8202             (gnus-summary-goto-article
8203              (mail-header-number header) nil t)
8204           (when sparse
8205             (gnus-summary-update-article (mail-header-number header)))))
8206        (t
8207         ;; We fetch the article.
8208         (catch 'found
8209           (dolist (gnus-override-method (gnus-refer-article-methods))
8210             (when (and (gnus-check-server gnus-override-method)
8211                        ;; Fetch the header,
8212                        (setq number (gnus-summary-insert-subject message-id)))
8213               ;; and display the article.
8214               (gnus-summary-select-article nil nil nil number)
8215               (throw 'found t)))
8216           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8217
8218 (defun gnus-refer-article-methods ()
8219   "Return a list of referable methods."
8220   (cond
8221    ;; No method, so we default to current and native.
8222    ((null gnus-refer-article-method)
8223     (list gnus-current-select-method gnus-select-method))
8224    ;; Current.
8225    ((eq 'current gnus-refer-article-method)
8226     (list gnus-current-select-method))
8227    ;; List of select methods.
8228    ((not (and (symbolp (car gnus-refer-article-method))
8229               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8230     (let (out)
8231       (dolist (method gnus-refer-article-method)
8232         (push (if (eq 'current method)
8233                   gnus-current-select-method
8234                 method)
8235               out))
8236       (nreverse out)))
8237    ;; One single select method.
8238    (t
8239     (list gnus-refer-article-method))))
8240
8241 (defun gnus-summary-edit-parameters ()
8242   "Edit the group parameters of the current group."
8243   (interactive)
8244   (gnus-group-edit-group gnus-newsgroup-name 'params))
8245
8246 (defun gnus-summary-customize-parameters ()
8247   "Customize the group parameters of the current group."
8248   (interactive)
8249   (gnus-group-customize gnus-newsgroup-name))
8250
8251 (defun gnus-summary-enter-digest-group (&optional force)
8252   "Enter an nndoc group based on the current article.
8253 If FORCE, force a digest interpretation.  If not, try
8254 to guess what the document format is."
8255   (interactive "P")
8256   (let ((conf gnus-current-window-configuration))
8257     (save-window-excursion
8258       (save-excursion
8259         (let (gnus-article-prepare-hook
8260               gnus-display-mime-function
8261               gnus-break-pages)
8262           (gnus-summary-select-article))))
8263     (setq gnus-current-window-configuration conf)
8264     (let* ((name (format "%s-%d"
8265                          (gnus-group-prefixed-name
8266                           gnus-newsgroup-name (list 'nndoc ""))
8267                          (save-excursion
8268                            (set-buffer gnus-summary-buffer)
8269                            gnus-current-article)))
8270            (ogroup gnus-newsgroup-name)
8271            (params (append (gnus-info-params (gnus-get-info ogroup))
8272                            (list (cons 'to-group ogroup))
8273                            (list (cons 'parent-group ogroup))
8274                            (list (cons 'save-article-group ogroup))))
8275            (case-fold-search t)
8276            (buf (current-buffer))
8277            dig to-address)
8278       (save-excursion
8279         (set-buffer gnus-original-article-buffer)
8280         ;; Have the digest group inherit the main mail address of
8281         ;; the parent article.
8282         (when (setq to-address (or (gnus-fetch-field "reply-to")
8283                                    (gnus-fetch-field "from")))
8284           (setq params (append
8285                         (list (cons 'to-address
8286                                     (funcall gnus-decode-encoded-word-function
8287                                              to-address))))))
8288         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8289         (insert-buffer-substring gnus-original-article-buffer)
8290         ;; Remove lines that may lead nndoc to misinterpret the
8291         ;; document type.
8292         (narrow-to-region
8293          (goto-char (point-min))
8294          (or (search-forward "\n\n" nil t) (point)))
8295         (goto-char (point-min))
8296         (delete-matching-lines "^Path:\\|^From ")
8297         (widen))
8298       (unwind-protect
8299           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8300                     (gnus-newsgroup-ephemeral-ignored-charsets
8301                      gnus-newsgroup-ignored-charsets))
8302                 (gnus-group-read-ephemeral-group
8303                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8304                               (nndoc-article-type
8305                                ,(if force 'mbox 'guess)))
8306                  t nil nil nil
8307                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8308                                                         "ADAPT")))))
8309               ;; Make all postings to this group go to the parent group.
8310               (nconc (gnus-info-params (gnus-get-info name))
8311                      params)
8312             ;; Couldn't select this doc group.
8313             (switch-to-buffer buf)
8314             (gnus-set-global-variables)
8315             (gnus-configure-windows 'summary)
8316             (gnus-message 3 "Article couldn't be entered?"))
8317         (kill-buffer dig)))))
8318
8319 (defun gnus-summary-read-document (n)
8320   "Open a new group based on the current article(s).
8321 This will allow you to read digests and other similar
8322 documents as newsgroups.
8323 Obeys the standard process/prefix convention."
8324   (interactive "P")
8325   (let* ((articles (gnus-summary-work-articles n))
8326          (ogroup gnus-newsgroup-name)
8327          (params (append (gnus-info-params (gnus-get-info ogroup))
8328                          (list (cons 'to-group ogroup))))
8329          article group egroup groups vgroup)
8330     (while (setq article (pop articles))
8331       (setq group (format "%s-%d" gnus-newsgroup-name article))
8332       (gnus-summary-remove-process-mark article)
8333       (when (gnus-summary-display-article article)
8334         (save-excursion
8335           (with-temp-buffer
8336             (insert-buffer-substring gnus-original-article-buffer)
8337             ;; Remove some headers that may lead nndoc to make
8338             ;; the wrong guess.
8339             (message-narrow-to-head)
8340             (goto-char (point-min))
8341             (delete-matching-lines "^Path:\\|^From ")
8342             (widen)
8343             (if (setq egroup
8344                       (gnus-group-read-ephemeral-group
8345                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8346                                      (nndoc-article-type guess))
8347                        t nil t))
8348                 (progn
8349             ;; Make all postings to this group go to the parent group.
8350                   (nconc (gnus-info-params (gnus-get-info egroup))
8351                          params)
8352                   (push egroup groups))
8353               ;; Couldn't select this doc group.
8354               (gnus-error 3 "Article couldn't be entered"))))))
8355     ;; Now we have selected all the documents.
8356     (cond
8357      ((not groups)
8358       (error "None of the articles could be interpreted as documents"))
8359      ((gnus-group-read-ephemeral-group
8360        (setq vgroup (format
8361                      "nnvirtual:%s-%s" gnus-newsgroup-name
8362                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8363        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8364        t
8365        (cons (current-buffer) 'summary)))
8366      (t
8367       (error "Couldn't select virtual nndoc group")))))
8368
8369 (defun gnus-summary-isearch-article (&optional regexp-p)
8370   "Do incremental search forward on the current article.
8371 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8372   (interactive "P")
8373   (gnus-summary-select-article)
8374   (gnus-configure-windows 'article)
8375   (gnus-eval-in-buffer-window gnus-article-buffer
8376     (save-restriction
8377       (widen)
8378       (isearch-forward regexp-p))))
8379
8380 (defun gnus-summary-search-article-forward (regexp &optional backward)
8381   "Search for an article containing REGEXP forward.
8382 If BACKWARD, search backward instead."
8383   (interactive
8384    (list (read-string
8385           (format "Search article %s (regexp%s): "
8386                   (if current-prefix-arg "backward" "forward")
8387                   (if gnus-last-search-regexp
8388                       (concat ", default " gnus-last-search-regexp)
8389                     "")))
8390          current-prefix-arg))
8391   (if (string-equal regexp "")
8392       (setq regexp (or gnus-last-search-regexp ""))
8393     (setq gnus-last-search-regexp regexp)
8394     (setq gnus-article-before-search gnus-current-article))
8395   ;; Intentionally set gnus-last-article.
8396   (setq gnus-last-article gnus-article-before-search)
8397   (let ((gnus-last-article gnus-last-article))
8398     (if (gnus-summary-search-article regexp backward)
8399         (gnus-summary-show-thread)
8400       (error "Search failed: \"%s\"" regexp))))
8401
8402 (defun gnus-summary-search-article-backward (regexp)
8403   "Search for an article containing REGEXP backward."
8404   (interactive
8405    (list (read-string
8406           (format "Search article backward (regexp%s): "
8407                   (if gnus-last-search-regexp
8408                       (concat ", default " gnus-last-search-regexp)
8409                     "")))))
8410   (gnus-summary-search-article-forward regexp 'backward))
8411
8412 (defun gnus-summary-search-article (regexp &optional backward)
8413   "Search for an article containing REGEXP.
8414 Optional argument BACKWARD means do search for backward.
8415 `gnus-select-article-hook' is not called during the search."
8416   ;; We have to require this here to make sure that the following
8417   ;; dynamic binding isn't shadowed by autoloading.
8418   (require 'gnus-async)
8419   (require 'gnus-art)
8420   (let ((gnus-select-article-hook nil)  ;Disable hook.
8421         (gnus-article-prepare-hook nil)
8422         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8423         (gnus-use-article-prefetch nil)
8424         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8425         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8426         (gnus-visual nil)
8427         (gnus-keep-backlog nil)
8428         (gnus-break-pages nil)
8429         (gnus-summary-display-arrow nil)
8430         (gnus-updated-mode-lines nil)
8431         (gnus-auto-center-summary nil)
8432         (sum (current-buffer))
8433         (gnus-display-mime-function nil)
8434         (found nil)
8435         point)
8436     (gnus-save-hidden-threads
8437       (gnus-summary-select-article)
8438       (set-buffer gnus-article-buffer)
8439       (goto-char (window-point (get-buffer-window (current-buffer))))
8440       (when backward
8441         (forward-line -1))
8442       (while (not found)
8443         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8444         (if (if backward
8445                 (re-search-backward regexp nil t)
8446               (re-search-forward regexp nil t))
8447             ;; We found the regexp.
8448             (progn
8449               (setq found 'found)
8450               (beginning-of-line)
8451               (set-window-start
8452                (get-buffer-window (current-buffer))
8453                (point))
8454               (forward-line 1)
8455               (set-window-point
8456                (get-buffer-window (current-buffer))
8457                (point))
8458               (set-buffer sum)
8459               (setq point (point)))
8460           ;; We didn't find it, so we go to the next article.
8461           (set-buffer sum)
8462           (setq found 'not)
8463           (while (eq found 'not)
8464             (if (not (if backward (gnus-summary-find-prev)
8465                        (gnus-summary-find-next)))
8466                 ;; No more articles.
8467                 (setq found t)
8468               ;; Select the next article and adjust point.
8469               (unless (gnus-summary-article-sparse-p
8470                        (gnus-summary-article-number))
8471                 (setq found nil)
8472                 (gnus-summary-select-article)
8473                 (set-buffer gnus-article-buffer)
8474                 (widen)
8475                 (goto-char (if backward (point-max) (point-min))))))))
8476       (gnus-message 7 ""))
8477     ;; Return whether we found the regexp.
8478     (when (eq found 'found)
8479       (goto-char point)
8480       (gnus-summary-show-thread)
8481       (gnus-summary-goto-subject gnus-current-article)
8482       (gnus-summary-position-point)
8483       t)))
8484
8485 (defun gnus-find-matching-articles (header regexp)
8486   "Return a list of all articles that match REGEXP on HEADER.
8487 This search includes all articles in the current group that Gnus has
8488 fetched headers for, whether they are displayed or not."
8489   (let ((articles nil)
8490         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8491         (case-fold-search t))
8492     (dolist (header gnus-newsgroup-headers)
8493       (when (string-match regexp (funcall func header))
8494         (push (mail-header-number header) articles)))
8495     (nreverse articles)))
8496
8497 (defun gnus-summary-find-matching (header regexp &optional backward unread
8498                                           not-case-fold not-matching)
8499   "Return a list of all articles that match REGEXP on HEADER.
8500 The search stars on the current article and goes forwards unless
8501 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8502 If UNREAD is non-nil, only unread articles will
8503 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8504 in the comparisons. If NOT-MATCHING, return a list of all articles that
8505 not match REGEXP on HEADER."
8506   (let ((case-fold-search (not not-case-fold))
8507         articles d func)
8508     (if (consp header)
8509         (if (eq (car header) 'extra)
8510             (setq func
8511                   `(lambda (h)
8512                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8513                          "")))
8514           (error "%s is an invalid header" header))
8515       (unless (fboundp (intern (concat "mail-header-" header)))
8516         (error "%s is not a valid header" header))
8517       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8518     (dolist (d (if (eq backward 'all)
8519                    gnus-newsgroup-data
8520                  (gnus-data-find-list
8521                   (gnus-summary-article-number)
8522                   (gnus-data-list backward))))
8523       (when (and (or (not unread)       ; We want all articles...
8524                      (gnus-data-unread-p d)) ; Or just unreads.
8525                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8526                  (if not-matching
8527                      (not (string-match
8528                            regexp
8529                            (funcall func (gnus-data-header d))))
8530                    (string-match regexp
8531                                  (funcall func (gnus-data-header d)))))
8532         (push (gnus-data-number d) articles))) ; Success!
8533     (nreverse articles)))
8534
8535 (defun gnus-summary-execute-command (header regexp command &optional backward)
8536   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8537 If HEADER is an empty string (or nil), the match is done on the entire
8538 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8539   (interactive
8540    (list (let ((completion-ignore-case t))
8541            (completing-read
8542             "Header name: "
8543             (mapcar (lambda (header) (list (format "%s" header)))
8544                     (append
8545                      '("Number" "Subject" "From" "Lines" "Date"
8546                        "Message-ID" "Xref" "References" "Body")
8547                      gnus-extra-headers))
8548             nil 'require-match))
8549          (read-string "Regexp: ")
8550          (read-key-sequence "Command: ")
8551          current-prefix-arg))
8552   (when (equal header "Body")
8553     (setq header ""))
8554   ;; Hidden thread subtrees must be searched as well.
8555   (gnus-summary-show-all-threads)
8556   ;; We don't want to change current point nor window configuration.
8557   (save-excursion
8558     (save-window-excursion
8559       (let (gnus-visual
8560             gnus-treat-strip-trailing-blank-lines
8561             gnus-treat-strip-leading-blank-lines
8562             gnus-treat-strip-multiple-blank-lines
8563             gnus-treat-hide-boring-headers
8564             gnus-treat-fold-newsgroups
8565             gnus-article-prepare-hook)
8566         (gnus-message 6 "Executing %s..." (key-description command))
8567         ;; We'd like to execute COMMAND interactively so as to give arguments.
8568         (gnus-execute header regexp
8569                       `(call-interactively ',(key-binding command))
8570                       backward)
8571         (gnus-message 6 "Executing %s...done" (key-description command))))))
8572
8573 (defun gnus-summary-beginning-of-article ()
8574   "Scroll the article back to the beginning."
8575   (interactive)
8576   (gnus-summary-select-article)
8577   (gnus-configure-windows 'article)
8578   (gnus-eval-in-buffer-window gnus-article-buffer
8579     (widen)
8580     (goto-char (point-min))
8581     (when gnus-break-pages
8582       (gnus-narrow-to-page))))
8583
8584 (defun gnus-summary-end-of-article ()
8585   "Scroll to the end of the article."
8586   (interactive)
8587   (gnus-summary-select-article)
8588   (gnus-configure-windows 'article)
8589   (gnus-eval-in-buffer-window gnus-article-buffer
8590     (widen)
8591     (goto-char (point-max))
8592     (recenter -3)
8593     (when gnus-break-pages
8594       (when (re-search-backward page-delimiter nil t)
8595         (narrow-to-region (match-end 0) (point-max)))
8596       (gnus-narrow-to-page))))
8597
8598 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8599   "Truncate to LEN and quote all \"(\"'s in STRING."
8600   (gnus-replace-in-string (if (and len (> (length string) len))
8601                               (substring string 0 len)
8602                             string)
8603                           "[()]" "\\\\\\&"))
8604
8605 (defun gnus-summary-print-article (&optional filename n)
8606   "Generate and print a PostScript image of the process-marked (mail) articles.
8607
8608 If used interactively, print the current article if none are
8609 process-marked.  With prefix arg, prompt the user for the name of the
8610 file to save in.
8611
8612 When used from Lisp, accept two optional args FILENAME and N.  N means
8613 to print the next N articles.  If N is negative, print the N previous
8614 articles.  If N is nil and articles have been marked with the process
8615 mark, print these instead.
8616
8617 If the optional first argument FILENAME is nil, send the image to the
8618 printer.  If FILENAME is a string, save the PostScript image in a file with
8619 that name.  If FILENAME is a number, prompt the user for the name of the file
8620 to save in."
8621   (interactive (list (ps-print-preprint current-prefix-arg)))
8622   (dolist (article (gnus-summary-work-articles n))
8623     (gnus-summary-select-article nil nil 'pseudo article)
8624     (gnus-eval-in-buffer-window gnus-article-buffer
8625       (gnus-print-buffer))
8626     (gnus-summary-remove-process-mark article))
8627   (ps-despool filename))
8628
8629 (defun gnus-print-buffer ()
8630   (let ((buffer (generate-new-buffer " *print*")))
8631     (unwind-protect
8632         (progn
8633           (copy-to-buffer buffer (point-min) (point-max))
8634           (set-buffer buffer)
8635           (gnus-remove-text-with-property 'gnus-decoration)
8636           (when (gnus-visual-p 'article-highlight 'highlight)
8637             ;; Copy-to-buffer doesn't copy overlay.  So redo
8638             ;; highlight.
8639             (let ((gnus-article-buffer buffer))
8640               (gnus-article-highlight-citation t)
8641               (gnus-article-highlight-signature)
8642               (gnus-article-emphasize)
8643               (gnus-article-delete-invisible-text)))
8644           (let ((ps-left-header
8645                  (list
8646                   (concat "("
8647                           (gnus-summary-print-truncate-and-quote
8648                            (mail-header-subject gnus-current-headers)
8649                            66) ")")
8650                   (concat "("
8651                           (gnus-summary-print-truncate-and-quote
8652                            (mail-header-from gnus-current-headers)
8653                            45) ")")))
8654                 (ps-right-header
8655                  (list
8656                   "/pagenumberstring load"
8657                   (concat "("
8658                           (mail-header-date gnus-current-headers) ")"))))
8659             (gnus-run-hooks 'gnus-ps-print-hook)
8660             (save-excursion
8661               (if window-system
8662                   (ps-spool-buffer-with-faces)
8663                 (ps-spool-buffer)))))
8664       (kill-buffer buffer))))
8665
8666 (defun gnus-summary-show-article (&optional arg)
8667   "Force redisplaying of the current article.
8668 If ARG (the prefix) is a number, show the article with the charset
8669 defined in `gnus-summary-show-article-charset-alist', or the charset
8670 input.
8671 If ARG (the prefix) is non-nil and not a number, show the raw article
8672 without any article massaging functions being run.  Normally, the key
8673 strokes are `C-u g'."
8674   (interactive "P")
8675   (cond
8676    ((numberp arg)
8677     (gnus-summary-show-article t)
8678     (let ((gnus-newsgroup-charset
8679            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8680                (mm-read-coding-system
8681                 "View as charset: " ;; actually it is coding system.
8682                 (save-excursion
8683                   (set-buffer gnus-article-buffer)
8684                   (mm-detect-coding-region (point) (point-max))))))
8685           (gnus-newsgroup-ignored-charsets 'gnus-all))
8686       (gnus-summary-select-article nil 'force)
8687       (let ((deps gnus-newsgroup-dependencies)
8688             head header lines)
8689         (save-excursion
8690           (set-buffer gnus-original-article-buffer)
8691           (save-restriction
8692             (message-narrow-to-head)
8693             (setq head (buffer-string))
8694             (goto-char (point-min))
8695             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8696               (goto-char (point-max))
8697               (widen)
8698               (setq lines (1- (count-lines (point) (point-max))))))
8699           (with-temp-buffer
8700             (insert (format "211 %d Article retrieved.\n"
8701                             (cdr gnus-article-current)))
8702             (insert head)
8703             (if lines (insert (format "Lines: %d\n" lines)))
8704             (insert ".\n")
8705             (let ((nntp-server-buffer (current-buffer)))
8706               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8707         (gnus-data-set-header
8708          (gnus-data-find (cdr gnus-article-current))
8709          header)
8710         (gnus-summary-update-article-line
8711          (cdr gnus-article-current) header)
8712         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8713           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8714    ((not arg)
8715     ;; Select the article the normal way.
8716     (gnus-summary-select-article nil 'force))
8717    (t
8718     ;; We have to require this here to make sure that the following
8719     ;; dynamic binding isn't shadowed by autoloading.
8720     (require 'gnus-async)
8721     (require 'gnus-art)
8722     ;; Bind the article treatment functions to nil.
8723     (let ((gnus-have-all-headers t)
8724           gnus-article-prepare-hook
8725           gnus-article-decode-hook
8726           gnus-display-mime-function
8727           gnus-break-pages)
8728       ;; Destroy any MIME parts.
8729       (when (gnus-buffer-live-p gnus-article-buffer)
8730         (save-excursion
8731           (set-buffer gnus-article-buffer)
8732           (mm-destroy-parts gnus-article-mime-handles)
8733           ;; Set it to nil for safety reason.
8734           (setq gnus-article-mime-handle-alist nil)
8735           (setq gnus-article-mime-handles nil)))
8736       (gnus-summary-select-article nil 'force))))
8737   (gnus-summary-goto-subject gnus-current-article)
8738   (gnus-summary-position-point))
8739
8740 (defun gnus-summary-show-raw-article ()
8741   "Show the raw article without any article massaging functions being run."
8742   (interactive)
8743   (gnus-summary-show-article t))
8744
8745 (defun gnus-summary-verbose-headers (&optional arg)
8746   "Toggle permanent full header display.
8747 If ARG is a positive number, turn header display on.
8748 If ARG is a negative number, turn header display off."
8749   (interactive "P")
8750   (setq gnus-show-all-headers
8751         (cond ((or (not (numberp arg))
8752                    (zerop arg))
8753                (not gnus-show-all-headers))
8754               ((natnump arg)
8755                t)))
8756   (gnus-summary-show-article))
8757
8758 (defun gnus-summary-toggle-header (&optional arg)
8759   "Show the headers if they are hidden, or hide them if they are shown.
8760 If ARG is a positive number, show the entire header.
8761 If ARG is a negative number, hide the unwanted header lines."
8762   (interactive "P")
8763   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8764                      (get-buffer-window gnus-article-buffer t))))
8765     (with-current-buffer gnus-article-buffer
8766       (widen)
8767       (article-narrow-to-head)
8768       (let* ((buffer-read-only nil)
8769              (inhibit-point-motion-hooks t)
8770              (hidden (if (numberp arg)
8771                          (>= arg 0)
8772                        (gnus-article-hidden-text-p 'headers)))
8773              s e)
8774         (delete-region (point-min) (point-max))
8775         (with-current-buffer gnus-original-article-buffer
8776           (goto-char (setq s (point-min)))
8777           (setq e (if (search-forward "\n\n" nil t)
8778                       (1- (point))
8779                     (point-max))))
8780         (insert-buffer-substring gnus-original-article-buffer s e)
8781         (run-hooks 'gnus-article-decode-hook)
8782         (if hidden
8783             (let ((gnus-treat-hide-headers nil)
8784                   (gnus-treat-hide-boring-headers nil))
8785               (gnus-delete-wash-type 'headers)
8786               (gnus-treat-article 'head))
8787           (gnus-treat-article 'head))
8788         (widen)
8789         (if window
8790             (set-window-start window (goto-char (point-min))))
8791         (if gnus-break-pages
8792             (gnus-narrow-to-page)
8793           (when (gnus-visual-p 'page-marker)
8794             (let ((buffer-read-only nil))
8795               (gnus-remove-text-with-property 'gnus-prev)
8796               (gnus-remove-text-with-property 'gnus-next))))
8797         (gnus-set-mode-line 'article)))))
8798
8799 (defun gnus-summary-show-all-headers ()
8800   "Make all header lines visible."
8801   (interactive)
8802   (gnus-summary-toggle-header 1))
8803
8804 (defun gnus-summary-caesar-message (&optional arg)
8805   "Caesar rotate the current article by 13.
8806 The numerical prefix specifies how many places to rotate each letter
8807 forward."
8808   (interactive "P")
8809   (gnus-summary-select-article)
8810   (let ((mail-header-separator ""))
8811     (gnus-eval-in-buffer-window gnus-article-buffer
8812       (save-restriction
8813         (widen)
8814         (let ((start (window-start))
8815               buffer-read-only)
8816           (message-caesar-buffer-body arg)
8817           (set-window-start (get-buffer-window (current-buffer)) start))))))
8818
8819 (autoload 'unmorse-region "morse"
8820   "Convert morse coded text in region to ordinary ASCII text."
8821   t)
8822
8823 (defun gnus-summary-morse-message (&optional arg)
8824   "Morse decode the current article."
8825   (interactive "P")
8826   (gnus-summary-select-article)
8827   (let ((mail-header-separator ""))
8828     (gnus-eval-in-buffer-window gnus-article-buffer
8829       (save-excursion
8830         (save-restriction
8831           (widen)
8832           (let ((pos (window-start))
8833                 buffer-read-only)
8834             (goto-char (point-min))
8835             (when (message-goto-body)
8836               (gnus-narrow-to-body))
8837             (goto-char (point-min))
8838             (while (re-search-forward "·" (point-max) t)
8839               (replace-match "."))
8840             (unmorse-region (point-min) (point-max))
8841             (widen)
8842             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8843
8844 (defun gnus-summary-stop-page-breaking ()
8845   "Stop page breaking in the current article."
8846   (interactive)
8847   (gnus-summary-select-article)
8848   (gnus-eval-in-buffer-window gnus-article-buffer
8849     (widen)
8850     (when (gnus-visual-p 'page-marker)
8851       (let ((buffer-read-only nil))
8852         (gnus-remove-text-with-property 'gnus-prev)
8853         (gnus-remove-text-with-property 'gnus-next))
8854       (setq gnus-page-broken nil))))
8855
8856 (defun gnus-summary-move-article (&optional n to-newsgroup
8857                                             select-method action)
8858   "Move the current article to a different newsgroup.
8859 If N is a positive number, move the N next articles.
8860 If N is a negative number, move the N previous articles.
8861 If N is nil and any articles have been marked with the process mark,
8862 move those articles instead.
8863 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8864 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8865 re-spool using this method.
8866
8867 When called interactively with TO-NEWSGROUP being nil, the value of
8868 the variable `gnus-move-split-methods' is used for finding a default
8869 for the target newsgroup.
8870
8871 For this function to work, both the current newsgroup and the
8872 newsgroup that you want to move to have to support the `request-move'
8873 and `request-accept' functions.
8874
8875 ACTION can be either `move' (the default), `crosspost' or `copy'."
8876   (interactive "P")
8877   (unless action
8878     (setq action 'move))
8879   ;; Check whether the source group supports the required functions.
8880   (cond ((and (eq action 'move)
8881               (not (gnus-check-backend-function
8882                     'request-move-article gnus-newsgroup-name)))
8883          (error "The current group does not support article moving"))
8884         ((and (eq action 'crosspost)
8885               (not (gnus-check-backend-function
8886                     'request-replace-article gnus-newsgroup-name)))
8887          (error "The current group does not support article editing")))
8888   (let ((articles (gnus-summary-work-articles n))
8889         (prefix (if (gnus-check-backend-function
8890                      'request-move-article gnus-newsgroup-name)
8891                     (funcall gnus-move-group-prefix-function
8892                              gnus-newsgroup-name)
8893                   ""))
8894         (names '((move "Move" "Moving")
8895                  (copy "Copy" "Copying")
8896                  (crosspost "Crosspost" "Crossposting")))
8897         (copy-buf (save-excursion
8898                     (nnheader-set-temp-buffer " *copy article*")))
8899         art-group to-method new-xref article to-groups)
8900     (unless (assq action names)
8901       (error "Unknown action %s" action))
8902     ;; Read the newsgroup name.
8903     (when (and (not to-newsgroup)
8904                (not select-method))
8905       (if (and gnus-move-split-methods
8906                (not
8907                 (and (memq gnus-current-article articles)
8908                      (gnus-buffer-live-p gnus-original-article-buffer))))
8909           ;; When `gnus-move-split-methods' is non-nil, we have to
8910           ;; select an article to give `gnus-read-move-group-name' an
8911           ;; opportunity to suggest an appropriate default.  However,
8912           ;; we needn't render or mark the article.
8913           (let ((gnus-display-mime-function nil)
8914                 (gnus-article-prepare-hook nil)
8915                 (gnus-mark-article-hook nil))
8916             (gnus-summary-select-article nil nil nil (car articles))))
8917       (setq to-newsgroup
8918             (gnus-read-move-group-name
8919              (cadr (assq action names))
8920              (symbol-value (intern (format "gnus-current-%s-group" action)))
8921              articles prefix))
8922       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8923     (setq to-method (or select-method
8924                         (gnus-server-to-method
8925                          (gnus-group-method to-newsgroup))))
8926     ;; Check the method we are to move this article to...
8927     (unless (gnus-check-backend-function
8928              'request-accept-article (car to-method))
8929       (error "%s does not support article copying" (car to-method)))
8930     (unless (gnus-check-server to-method)
8931       (error "Can't open server %s" (car to-method)))
8932     (gnus-message 6 "%s to %s: %s..."
8933                   (caddr (assq action names))
8934                   (or (car select-method) to-newsgroup) articles)
8935     (while articles
8936       (setq article (pop articles))
8937       (setq
8938        art-group
8939        (cond
8940         ;; Move the article.
8941         ((eq action 'move)
8942          ;; Remove this article from future suppression.
8943          (gnus-dup-unsuppress-article article)
8944          (gnus-request-move-article
8945           article                       ; Article to move
8946           gnus-newsgroup-name           ; From newsgroup
8947           (nth 1 (gnus-find-method-for-group
8948                   gnus-newsgroup-name)) ; Server
8949           (list 'gnus-request-accept-article
8950                 to-newsgroup (list 'quote select-method)
8951                 (not articles) t)       ; Accept form
8952           (not articles)))              ; Only save nov last time
8953         ;; Copy the article.
8954         ((eq action 'copy)
8955          (save-excursion
8956            (set-buffer copy-buf)
8957            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8958              (gnus-request-accept-article
8959               to-newsgroup select-method (not articles) t))))
8960         ;; Crosspost the article.
8961         ((eq action 'crosspost)
8962          (let ((xref (message-tokenize-header
8963                       (mail-header-xref (gnus-summary-article-header article))
8964                       " ")))
8965            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8966                                   ":" (number-to-string article)))
8967            (unless xref
8968              (setq xref (list (system-name))))
8969            (setq new-xref
8970                  (concat
8971                   (mapconcat 'identity
8972                              (delete "Xref:" (delete new-xref xref))
8973                              " ")
8974                   " " new-xref))
8975            (save-excursion
8976              (set-buffer copy-buf)
8977              ;; First put the article in the destination group.
8978              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8979              (when (consp (setq art-group
8980                                 (gnus-request-accept-article
8981                                  to-newsgroup select-method (not articles))))
8982                (setq new-xref (concat new-xref " " (car art-group)
8983                                       ":"
8984                                       (number-to-string (cdr art-group))))
8985                ;; Now we have the new Xrefs header, so we insert
8986                ;; it and replace the new article.
8987                (nnheader-replace-header "Xref" new-xref)
8988                (gnus-request-replace-article
8989                 (cdr art-group) to-newsgroup (current-buffer))
8990                art-group))))))
8991       (cond
8992        ((not art-group)
8993         (gnus-message 1 "Couldn't %s article %s: %s"
8994                       (cadr (assq action names)) article
8995                       (nnheader-get-report (car to-method))))
8996        ((eq art-group 'junk)
8997         (when (eq action 'move)
8998           (gnus-summary-mark-article article gnus-canceled-mark)
8999           (gnus-message 4 "Deleted article %s" article)
9000           ;; run the delete hook
9001           (run-hook-with-args 'gnus-summary-article-delete-hook
9002                               action
9003                               (gnus-data-header
9004                                (assoc article (gnus-data-list nil)))
9005                               gnus-newsgroup-name nil
9006                               select-method)))
9007        (t
9008         (let* ((pto-group (gnus-group-prefixed-name
9009                            (car art-group) to-method))
9010                (info (gnus-get-info pto-group))
9011                (to-group (gnus-info-group info))
9012                to-marks)
9013           ;; Update the group that has been moved to.
9014           (when (and info
9015                      (memq action '(move copy)))
9016             (unless (member to-group to-groups)
9017               (push to-group to-groups))
9018
9019             (unless (memq article gnus-newsgroup-unreads)
9020               (push 'read to-marks)
9021               (gnus-info-set-read
9022                info (gnus-add-to-range (gnus-info-read info)
9023                                        (list (cdr art-group)))))
9024
9025             ;; See whether the article is to be put in the cache.
9026             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9027                              gnus-article-mark-lists
9028                            (delete '(expirable . expire)
9029                                    (copy-sequence gnus-article-mark-lists))))
9030                   (to-article (cdr art-group)))
9031
9032               ;; Enter the article into the cache in the new group,
9033               ;; if that is required.
9034               (when gnus-use-cache
9035                 (gnus-cache-possibly-enter-article
9036                  to-group to-article
9037                  (memq article gnus-newsgroup-marked)
9038                  (memq article gnus-newsgroup-dormant)
9039                  (memq article gnus-newsgroup-unreads)))
9040
9041               (when gnus-preserve-marks
9042                 ;; Copy any marks over to the new group.
9043                 (when (and (equal to-group gnus-newsgroup-name)
9044                            (not (memq article gnus-newsgroup-unreads)))
9045                   ;; Mark this article as read in this group.
9046                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9047                   (setcdr (gnus-active to-group) to-article)
9048                   (setcdr gnus-newsgroup-active to-article))
9049
9050                 (while marks
9051                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9052                     (when (memq article (symbol-value
9053                                          (intern (format "gnus-newsgroup-%s"
9054                                                          (caar marks)))))
9055                       (push (cdar marks) to-marks)
9056                       ;; If the other group is the same as this group,
9057                       ;; then we have to add the mark to the list.
9058                       (when (equal to-group gnus-newsgroup-name)
9059                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9060                              (cons to-article
9061                                    (symbol-value
9062                                     (intern (format "gnus-newsgroup-%s"
9063                                                     (caar marks)))))))
9064                       ;; Copy the marks to other group.
9065                       (gnus-add-marked-articles
9066                        to-group (cdar marks) (list to-article) info)))
9067                   (setq marks (cdr marks)))
9068
9069                 (gnus-request-set-mark
9070                  to-group (list (list (list to-article) 'add to-marks))))
9071
9072               (gnus-dribble-enter
9073                (concat "(gnus-group-set-info '"
9074                        (gnus-prin1-to-string (gnus-get-info to-group))
9075                        ")"))))
9076
9077           ;; Update the Xref header in this article to point to
9078           ;; the new crossposted article we have just created.
9079           (when (eq action 'crosspost)
9080             (save-excursion
9081               (set-buffer copy-buf)
9082               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9083               (nnheader-replace-header "Xref" new-xref)
9084               (gnus-request-replace-article
9085                article gnus-newsgroup-name (current-buffer))))
9086
9087           ;; run the move/copy/crosspost/respool hook
9088           (run-hook-with-args 'gnus-summary-article-move-hook
9089                               action
9090                               (gnus-data-header
9091                                (assoc article (gnus-data-list nil)))
9092                               gnus-newsgroup-name
9093                               to-newsgroup
9094                               select-method))
9095
9096         ;;;!!!Why is this necessary?
9097         (set-buffer gnus-summary-buffer)
9098         
9099         (gnus-summary-goto-subject article)
9100         (when (eq action 'move)
9101           (gnus-summary-mark-article article gnus-canceled-mark))))
9102       (gnus-summary-remove-process-mark article))
9103     ;; Re-activate all groups that have been moved to.
9104     (save-excursion
9105       (set-buffer gnus-group-buffer)
9106       (let ((gnus-group-marked to-groups))
9107         (gnus-group-get-new-news-this-group nil t)))
9108
9109     (gnus-kill-buffer copy-buf)
9110     (gnus-summary-position-point)
9111     (gnus-set-mode-line 'summary)))
9112
9113 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9114   "Move the current article to a different newsgroup.
9115 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9116 When called interactively, if TO-NEWSGROUP is nil, use the value of
9117 the variable `gnus-move-split-methods' for finding a default target
9118 newsgroup.
9119 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9120 re-spool using this method."
9121   (interactive "P")
9122   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9123
9124 (defun gnus-summary-crosspost-article (&optional n)
9125   "Crosspost the current article to some other group."
9126   (interactive "P")
9127   (gnus-summary-move-article n nil nil 'crosspost))
9128
9129 (defcustom gnus-summary-respool-default-method nil
9130   "Default method type for respooling an article.
9131 If nil, use to the current newsgroup method."
9132   :type 'symbol
9133   :group 'gnus-summary-mail)
9134
9135 (defcustom gnus-summary-display-while-building nil
9136   "If non-nil, show and update the summary buffer as it's being built.
9137 If the value is t, update the buffer after every line is inserted.  If
9138 the value is an integer (N), update the display every N lines."
9139   :group 'gnus-thread
9140   :type '(choice (const :tag "off" nil)
9141                  number
9142                  (const :tag "frequently" t)))
9143
9144 (defun gnus-summary-respool-article (&optional n method)
9145   "Respool the current article.
9146 The article will be squeezed through the mail spooling process again,
9147 which means that it will be put in some mail newsgroup or other
9148 depending on `nnmail-split-methods'.
9149 If N is a positive number, respool the N next articles.
9150 If N is a negative number, respool the N previous articles.
9151 If N is nil and any articles have been marked with the process mark,
9152 respool those articles instead.
9153
9154 Respooling can be done both from mail groups and \"real\" newsgroups.
9155 In the former case, the articles in question will be moved from the
9156 current group into whatever groups they are destined to.  In the
9157 latter case, they will be copied into the relevant groups."
9158   (interactive
9159    (list current-prefix-arg
9160          (let* ((methods (gnus-methods-using 'respool))
9161                 (methname
9162                  (symbol-name (or gnus-summary-respool-default-method
9163                                   (car (gnus-find-method-for-group
9164                                         gnus-newsgroup-name)))))
9165                 (method
9166                  (gnus-completing-read-with-default
9167                   methname "What backend do you want to use when respooling?"
9168                   methods nil t nil 'gnus-mail-method-history))
9169                 ms)
9170            (cond
9171             ((zerop (length (setq ms (gnus-servers-using-backend
9172                                       (intern method)))))
9173              (list (intern method) ""))
9174             ((= 1 (length ms))
9175              (car ms))
9176             (t
9177              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9178                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9179                            ms-alist))))))))
9180   (unless method
9181     (error "No method given for respooling"))
9182   (if (assoc (symbol-name
9183               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9184              (gnus-methods-using 'respool))
9185       (gnus-summary-move-article n nil method)
9186     (gnus-summary-copy-article n nil method)))
9187
9188 (defun gnus-summary-import-article (file &optional edit)
9189   "Import an arbitrary file into a mail newsgroup."
9190   (interactive "fImport file: \nP")
9191   (let ((group gnus-newsgroup-name)
9192         (now (current-time))
9193         atts lines group-art)
9194     (unless (gnus-check-backend-function 'request-accept-article group)
9195       (error "%s does not support article importing" group))
9196     (or (file-readable-p file)
9197         (not (file-regular-p file))
9198         (error "Can't read %s" file))
9199     (save-excursion
9200       (set-buffer (gnus-get-buffer-create " *import file*"))
9201       (erase-buffer)
9202       (nnheader-insert-file-contents file)
9203       (goto-char (point-min))
9204       (if (nnheader-article-p)
9205           (save-restriction
9206             (goto-char (point-min))
9207             (search-forward "\n\n" nil t)
9208             (narrow-to-region (point-min) (1- (point)))
9209             (goto-char (point-min))
9210             (unless (re-search-forward "^date:" nil t)
9211               (goto-char (point-max))
9212               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9213        ;; This doesn't look like an article, so we fudge some headers.
9214         (setq atts (file-attributes file)
9215               lines (count-lines (point-min) (point-max)))
9216         (insert "From: " (read-string "From: ") "\n"
9217                 "Subject: " (read-string "Subject: ") "\n"
9218                 "Date: " (message-make-date (nth 5 atts)) "\n"
9219                 "Message-ID: " (message-make-message-id) "\n"
9220                 "Lines: " (int-to-string lines) "\n"
9221                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9222       (setq group-art (gnus-request-accept-article group nil t))
9223       (kill-buffer (current-buffer)))
9224     (setq gnus-newsgroup-active (gnus-activate-group group))
9225     (forward-line 1)
9226     (gnus-summary-goto-article (cdr group-art) nil t)
9227     (when edit
9228       (gnus-summary-edit-article))))
9229
9230 (defun gnus-summary-create-article ()
9231   "Create an article in a mail newsgroup."
9232   (interactive)
9233   (let ((group gnus-newsgroup-name)
9234         (now (current-time))
9235         group-art)
9236     (unless (gnus-check-backend-function 'request-accept-article group)
9237       (error "%s does not support article importing" group))
9238     (save-excursion
9239       (set-buffer (gnus-get-buffer-create " *import file*"))
9240       (erase-buffer)
9241       (goto-char (point-min))
9242       ;; This doesn't look like an article, so we fudge some headers.
9243       (insert "From: " (read-string "From: ") "\n"
9244               "Subject: " (read-string "Subject: ") "\n"
9245               "Date: " (message-make-date now) "\n"
9246               "Message-ID: " (message-make-message-id) "\n")
9247       (setq group-art (gnus-request-accept-article group nil t))
9248       (kill-buffer (current-buffer)))
9249     (setq gnus-newsgroup-active (gnus-activate-group group))
9250     (forward-line 1)
9251     (gnus-summary-goto-article (cdr group-art) nil t)
9252     (gnus-summary-edit-article)))
9253
9254 (defun gnus-summary-article-posted-p ()
9255   "Say whether the current (mail) article is available from news as well.
9256 This will be the case if the article has both been mailed and posted."
9257   (interactive)
9258   (let ((id (mail-header-references (gnus-summary-article-header)))
9259         (gnus-override-method (car (gnus-refer-article-methods))))
9260     (if (gnus-request-head id "")
9261         (gnus-message 2 "The current message was found on %s"
9262                       gnus-override-method)
9263       (gnus-message 2 "The current message couldn't be found on %s"
9264                     gnus-override-method)
9265       nil)))
9266
9267 (defun gnus-summary-expire-articles (&optional now)
9268   "Expire all articles that are marked as expirable in the current group."
9269   (interactive)
9270   (when (and (not gnus-group-is-exiting-without-update-p)
9271              (gnus-check-backend-function
9272               'request-expire-articles gnus-newsgroup-name))
9273     ;; This backend supports expiry.
9274     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9275            (expirable (if total
9276                           (progn
9277                             ;; We need to update the info for
9278                             ;; this group for `gnus-list-of-read-articles'
9279                             ;; to give us the right answer.
9280                             (gnus-run-hooks 'gnus-exit-group-hook)
9281                             (gnus-summary-update-info)
9282                             (gnus-list-of-read-articles gnus-newsgroup-name))
9283                         (setq gnus-newsgroup-expirable
9284                               (sort gnus-newsgroup-expirable '<))))
9285            (expiry-wait (if now 'immediate
9286                           (gnus-group-find-parameter
9287                            gnus-newsgroup-name 'expiry-wait)))
9288            (nnmail-expiry-target
9289             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9290                 nnmail-expiry-target))
9291            es)
9292       (when expirable
9293         ;; There are expirable articles in this group, so we run them
9294         ;; through the expiry process.
9295         (gnus-message 6 "Expiring articles...")
9296         (unless (gnus-check-group gnus-newsgroup-name)
9297           (error "Can't open server for %s" gnus-newsgroup-name))
9298         ;; The list of articles that weren't expired is returned.
9299         (save-excursion
9300           (if expiry-wait
9301               (let ((nnmail-expiry-wait-function nil)
9302                     (nnmail-expiry-wait expiry-wait))
9303                 (setq es (gnus-request-expire-articles
9304                           expirable gnus-newsgroup-name)))
9305             (setq es (gnus-request-expire-articles
9306                       expirable gnus-newsgroup-name)))
9307           (unless total
9308             (setq gnus-newsgroup-expirable es))
9309           ;; We go through the old list of expirable, and mark all
9310           ;; really expired articles as nonexistent.
9311           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9312             (let ((gnus-use-cache nil))
9313               (dolist (article expirable)
9314                 (when (and (not (memq article es))
9315                            (gnus-data-find article))
9316                   (gnus-summary-mark-article article gnus-canceled-mark)
9317                   (run-hook-with-args 'gnus-summary-article-expire-hook
9318                                       'delete
9319                                       (gnus-data-header
9320                                        (assoc article (gnus-data-list nil)))
9321                                       gnus-newsgroup-name
9322                                       nil
9323                                       nil))))))
9324         (gnus-message 6 "Expiring articles...done")))))
9325
9326 (defun gnus-summary-expire-articles-now ()
9327   "Expunge all expirable articles in the current group.
9328 This means that *all* articles that are marked as expirable will be
9329 deleted forever, right now."
9330   (interactive)
9331   (or gnus-expert-user
9332       (gnus-yes-or-no-p
9333        "Are you really, really, really sure you want to delete all these messages? ")
9334       (error "Phew!"))
9335   (gnus-summary-expire-articles t))
9336
9337 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9338 (defun gnus-summary-delete-article (&optional n)
9339   "Delete the N next (mail) articles.
9340 This command actually deletes articles.  This is not a marking
9341 command.  The article will disappear forever from your life, never to
9342 return.
9343
9344 If N is negative, delete backwards.
9345 If N is nil and articles have been marked with the process mark,
9346 delete these instead.
9347
9348 If `gnus-novice-user' is non-nil you will be asked for
9349 confirmation before the articles are deleted."
9350   (interactive "P")
9351   (unless (gnus-check-backend-function 'request-expire-articles
9352                                        gnus-newsgroup-name)
9353     (error "The current newsgroup does not support article deletion"))
9354   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9355     (error "Couldn't open server"))
9356   ;; Compute the list of articles to delete.
9357   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9358         (nnmail-expiry-target 'delete)
9359         not-deleted)
9360     (if (and gnus-novice-user
9361              (not (gnus-yes-or-no-p
9362                    (format "Do you really want to delete %s forever? "
9363                            (if (> (length articles) 1)
9364                                (format "these %s articles" (length articles))
9365                              "this article")))))
9366         ()
9367       ;; Delete the articles.
9368       (setq not-deleted (gnus-request-expire-articles
9369                          articles gnus-newsgroup-name 'force))
9370       (while articles
9371         (gnus-summary-remove-process-mark (car articles))
9372         ;; The backend might not have been able to delete the article
9373         ;; after all.
9374         (unless (memq (car articles) not-deleted)
9375           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9376         (let* ((article (car articles))
9377                (id (mail-header-id (gnus-data-header
9378                                     (assoc article (gnus-data-list nil))))))
9379           (run-hook-with-args 'gnus-summary-article-delete-hook
9380                               'delete id gnus-newsgroup-name nil
9381                               nil))
9382         (setq articles (cdr articles)))
9383       (when not-deleted
9384         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9385     (gnus-summary-position-point)
9386     (gnus-set-mode-line 'summary)
9387     not-deleted))
9388
9389 (defun gnus-summary-edit-article (&optional arg)
9390   "Edit the current article.
9391 This will have permanent effect only in mail groups.
9392 If ARG is nil, edit the decoded articles.
9393 If ARG is 1, edit the raw articles.
9394 If ARG is 2, edit the raw articles even in read-only groups.
9395 If ARG is 3, edit the articles with the current handles.
9396 Otherwise, allow editing of articles even in read-only
9397 groups."
9398   (interactive "P")
9399   (let (force raw current-handles)
9400     (cond
9401      ((null arg))
9402      ((eq arg 1)
9403       (setq raw t))
9404      ((eq arg 2)
9405       (setq raw t
9406             force t))
9407      ((eq arg 3)
9408       (setq current-handles
9409             (and (gnus-buffer-live-p gnus-article-buffer)
9410                  (with-current-buffer gnus-article-buffer
9411                    (prog1
9412                        gnus-article-mime-handles
9413                      (setq gnus-article-mime-handles nil))))))
9414      (t
9415       (setq force t)))
9416     (when (and raw (not force)
9417                (member gnus-newsgroup-name '("nndraft:delayed"
9418                                              "nndraft:drafts"
9419                                              "nndraft:queue")))
9420       (error "Can't edit the raw article in group %s"
9421              gnus-newsgroup-name))
9422     (save-excursion
9423       (set-buffer gnus-summary-buffer)
9424       (let ((mail-parse-charset gnus-newsgroup-charset)
9425             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9426         (gnus-set-global-variables)
9427         (when (and (not force)
9428                    (gnus-group-read-only-p))
9429           (error "The current newsgroup does not support article editing"))
9430         (gnus-summary-show-article t)
9431         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9432           (with-current-buffer gnus-article-buffer
9433             (mm-enable-multibyte)))
9434         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9435             (setq raw t))
9436         (gnus-article-edit-article
9437          (if raw 'ignore
9438            `(lambda ()
9439               (let ((mbl mml-buffer-list))
9440                 (setq mml-buffer-list nil)
9441                 (mime-to-mml ,'current-handles)
9442                 (let ((mbl1 mml-buffer-list))
9443                   (setq mml-buffer-list mbl)
9444                   (set (make-local-variable 'mml-buffer-list) mbl1))
9445                 (gnus-make-local-hook 'kill-buffer-hook)
9446                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9447          `(lambda (no-highlight)
9448             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9449                   (message-options message-options)
9450                   (message-options-set-recipient)
9451                   (mail-parse-ignored-charsets
9452                    ',gnus-newsgroup-ignored-charsets))
9453               ,(if (not raw) '(progn
9454                                 (mml-to-mime)
9455                                 (mml-destroy-buffers)
9456                                 (remove-hook 'kill-buffer-hook
9457                                              'mml-destroy-buffers t)
9458                                 (kill-local-variable 'mml-buffer-list)))
9459               (gnus-summary-edit-article-done
9460                ,(or (mail-header-references gnus-current-headers) "")
9461                ,(gnus-group-read-only-p)
9462                ,gnus-summary-buffer no-highlight))))))))
9463
9464 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9465
9466 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9467                                                  no-highlight)
9468   "Make edits to the current article permanent."
9469   (interactive)
9470   (save-excursion
9471    ;; The buffer restriction contains the entire article if it exists.
9472     (when (article-goto-body)
9473       (let ((lines (count-lines (point) (point-max)))
9474             (length (- (point-max) (point)))
9475             (case-fold-search t)
9476             (body (copy-marker (point))))
9477         (goto-char (point-min))
9478         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9479           (delete-region (match-beginning 1) (match-end 1))
9480           (insert (number-to-string length)))
9481         (goto-char (point-min))
9482         (when (re-search-forward
9483                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9484           (delete-region (match-beginning 1) (match-end 1))
9485           (insert (number-to-string length)))
9486         (goto-char (point-min))
9487         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9488           (delete-region (match-beginning 1) (match-end 1))
9489           (insert (number-to-string lines))))))
9490   ;; Replace the article.
9491   (let ((buf (current-buffer)))
9492     (with-temp-buffer
9493       (insert-buffer-substring buf)
9494
9495       (if (and (not read-only)
9496                (not (gnus-request-replace-article
9497                      (cdr gnus-article-current) (car gnus-article-current)
9498                      (current-buffer) t)))
9499           (error "Couldn't replace article")
9500         ;; Update the summary buffer.
9501         (if (and references
9502                  (equal (message-tokenize-header references " ")
9503                         (message-tokenize-header
9504                          (or (message-fetch-field "references") "") " ")))
9505             ;; We only have to update this line.
9506             (save-excursion
9507               (save-restriction
9508                 (message-narrow-to-head)
9509                 (let ((head (buffer-string))
9510                       header)
9511                   (with-temp-buffer
9512                     (insert (format "211 %d Article retrieved.\n"
9513                                     (cdr gnus-article-current)))
9514                     (insert head)
9515                     (insert ".\n")
9516                     (let ((nntp-server-buffer (current-buffer)))
9517                       (setq header (car (gnus-get-newsgroup-headers
9518                                          nil t))))
9519                     (save-excursion
9520                       (set-buffer gnus-summary-buffer)
9521                       (gnus-data-set-header
9522                        (gnus-data-find (cdr gnus-article-current))
9523                        header)
9524                       (gnus-summary-update-article-line
9525                        (cdr gnus-article-current) header)
9526                       (if (gnus-summary-goto-subject
9527                            (cdr gnus-article-current) nil t)
9528                           (gnus-summary-update-secondary-mark
9529                            (cdr gnus-article-current))))))))
9530           ;; Update threads.
9531           (set-buffer (or buffer gnus-summary-buffer))
9532           (gnus-summary-update-article (cdr gnus-article-current))
9533           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9534               (gnus-summary-update-secondary-mark
9535                (cdr gnus-article-current))))
9536         ;; Prettify the article buffer again.
9537         (unless no-highlight
9538           (save-excursion
9539             (set-buffer gnus-article-buffer)
9540             ;;;!!! Fix this -- article should be rehighlighted.
9541             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9542             (set-buffer gnus-original-article-buffer)
9543             (gnus-request-article
9544              (cdr gnus-article-current)
9545              (car gnus-article-current) (current-buffer))))
9546         ;; Prettify the summary buffer line.
9547         (when (gnus-visual-p 'summary-highlight 'highlight)
9548           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9549
9550 (defun gnus-summary-edit-wash (key)
9551   "Perform editing command KEY in the article buffer."
9552   (interactive
9553    (list
9554     (progn
9555       (message "%s" (concat (this-command-keys) "- "))
9556       (read-char))))
9557   (message "")
9558   (gnus-summary-edit-article)
9559   (execute-kbd-macro (concat (this-command-keys) key))
9560   (gnus-article-edit-done))
9561
9562 ;;; Respooling
9563
9564 (defun gnus-summary-respool-query (&optional silent trace)
9565   "Query where the respool algorithm would put this article."
9566   (interactive)
9567   (let (gnus-mark-article-hook)
9568     (gnus-summary-select-article)
9569     (save-excursion
9570       (set-buffer gnus-original-article-buffer)
9571       (let ((groups (nnmail-article-group 'identity trace)))
9572         (unless silent
9573           (if groups
9574               (message "This message would go to %s"
9575                        (mapconcat 'car groups ", "))
9576             (message "This message would go to no groups"))
9577           groups)))))
9578
9579 (defun gnus-summary-respool-trace ()
9580   "Trace where the respool algorithm would put this article.
9581 Display a buffer showing all fancy splitting patterns which matched."
9582   (interactive)
9583   (gnus-summary-respool-query nil t))
9584
9585 ;; Summary marking commands.
9586
9587 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9588   "Mark articles which has the same subject as read, and then select the next.
9589 If UNMARK is positive, remove any kind of mark.
9590 If UNMARK is negative, tick articles."
9591   (interactive "P")
9592   (when unmark
9593     (setq unmark (prefix-numeric-value unmark)))
9594   (let ((count
9595          (gnus-summary-mark-same-subject
9596           (gnus-summary-article-subject) unmark)))
9597     ;; Select next unread article.  If auto-select-same mode, should
9598     ;; select the first unread article.
9599     (gnus-summary-next-article t (and gnus-auto-select-same
9600                                       (gnus-summary-article-subject)))
9601     (gnus-message 7 "%d article%s marked as %s"
9602                   count (if (= count 1) " is" "s are")
9603                   (if unmark "unread" "read"))))
9604
9605 (defun gnus-summary-kill-same-subject (&optional unmark)
9606   "Mark articles which has the same subject as read.
9607 If UNMARK is positive, remove any kind of mark.
9608 If UNMARK is negative, tick articles."
9609   (interactive "P")
9610   (when unmark
9611     (setq unmark (prefix-numeric-value unmark)))
9612   (let ((count
9613          (gnus-summary-mark-same-subject
9614           (gnus-summary-article-subject) unmark)))
9615     ;; If marked as read, go to next unread subject.
9616     (when (null unmark)
9617       ;; Go to next unread subject.
9618       (gnus-summary-next-subject 1 t))
9619     (gnus-message 7 "%d articles are marked as %s"
9620                   count (if unmark "unread" "read"))))
9621
9622 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9623   "Mark articles with same SUBJECT as read, and return marked number.
9624 If optional argument UNMARK is positive, remove any kinds of marks.
9625 If optional argument UNMARK is negative, mark articles as unread instead."
9626   (let ((count 1))
9627     (save-excursion
9628       (cond
9629        ((null unmark)                   ; Mark as read.
9630         (while (and
9631                 (progn
9632                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9633                   (gnus-summary-show-thread) t)
9634                 (gnus-summary-find-subject subject))
9635           (setq count (1+ count))))
9636        ((> unmark 0)                    ; Tick.
9637         (while (and
9638                 (progn
9639                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9640                   (gnus-summary-show-thread) t)
9641                 (gnus-summary-find-subject subject))
9642           (setq count (1+ count))))
9643        (t                               ; Mark as unread.
9644         (while (and
9645                 (progn
9646                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9647                   (gnus-summary-show-thread) t)
9648                 (gnus-summary-find-subject subject))
9649           (setq count (1+ count)))))
9650       (gnus-set-mode-line 'summary)
9651       ;; Return the number of marked articles.
9652       count)))
9653
9654 (defun gnus-summary-mark-as-processable (n &optional unmark)
9655   "Set the process mark on the next N articles.
9656 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9657 the process mark instead.  The difference between N and the actual
9658 number of articles marked is returned."
9659   (interactive "P")
9660   (if (and (null n) (gnus-region-active-p))
9661       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9662     (setq n (prefix-numeric-value n))
9663     (let ((backward (< n 0))
9664           (n (abs n)))
9665       (while (and
9666               (> n 0)
9667               (if unmark
9668                   (gnus-summary-remove-process-mark
9669                    (gnus-summary-article-number))
9670                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9671               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9672         (setq n (1- n)))
9673       (when (/= 0 n)
9674         (gnus-message 7 "No more articles"))
9675       (gnus-summary-recenter)
9676       (gnus-summary-position-point)
9677       n)))
9678
9679 (defun gnus-summary-unmark-as-processable (n)
9680   "Remove the process mark from the next N articles.
9681 If N is negative, unmark backward instead.  The difference between N and
9682 the actual number of articles unmarked is returned."
9683   (interactive "P")
9684   (gnus-summary-mark-as-processable n t))
9685
9686 (defun gnus-summary-unmark-all-processable ()
9687   "Remove the process mark from all articles."
9688   (interactive)
9689   (save-excursion
9690     (while gnus-newsgroup-processable
9691       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9692   (gnus-summary-position-point))
9693
9694 (defun gnus-summary-add-mark (article type)
9695   "Mark ARTICLE with a mark of TYPE."
9696   (let ((vtype (car (assq type gnus-article-mark-lists)))
9697         var)
9698     (if (not vtype)
9699         (error "No such mark type: %s" type)
9700       (setq var (intern (format "gnus-newsgroup-%s" type)))
9701       (set var (cons article (symbol-value var)))
9702       (if (memq type '(processable cached replied forwarded recent saved))
9703           (gnus-summary-update-secondary-mark article)
9704         ;;; !!! This is bogus.  We should find out what primary
9705         ;;; !!! mark we want to set.
9706         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9707
9708 (defun gnus-summary-mark-as-expirable (n)
9709   "Mark N articles forward as expirable.
9710 If N is negative, mark backward instead.  The difference between N and
9711 the actual number of articles marked is returned."
9712   (interactive "p")
9713   (gnus-summary-mark-forward n gnus-expirable-mark))
9714
9715 (defun gnus-summary-mark-as-spam (n)
9716   "Mark N articles forward as spam.
9717 If N is negative, mark backward instead.  The difference between N and
9718 the actual number of articles marked is returned."
9719   (interactive "p")
9720   (gnus-summary-mark-forward n gnus-spam-mark))
9721
9722 (defun gnus-summary-mark-article-as-replied (article)
9723   "Mark ARTICLE as replied to and update the summary line.
9724 ARTICLE can also be a list of articles."
9725   (interactive (list (gnus-summary-article-number)))
9726   (let ((articles (if (listp article) article (list article))))
9727     (dolist (article articles)
9728       (unless (numberp article)
9729         (error "%s is not a number" article))
9730       (push article gnus-newsgroup-replied)
9731       (let ((buffer-read-only nil))
9732         (when (gnus-summary-goto-subject article nil t)
9733           (gnus-summary-update-secondary-mark article))))))
9734
9735 (defun gnus-summary-mark-article-as-forwarded (article)
9736   "Mark ARTICLE as forwarded and update the summary line.
9737 ARTICLE can also be a list of articles."
9738   (let ((articles (if (listp article) article (list article))))
9739     (dolist (article articles)
9740       (push article gnus-newsgroup-forwarded)
9741       (let ((buffer-read-only nil))
9742         (when (gnus-summary-goto-subject article nil t)
9743           (gnus-summary-update-secondary-mark article))))))
9744
9745 (defun gnus-summary-set-bookmark (article)
9746   "Set a bookmark in current article."
9747   (interactive (list (gnus-summary-article-number)))
9748   (when (or (not (get-buffer gnus-article-buffer))
9749             (not gnus-current-article)
9750             (not gnus-article-current)
9751             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9752     (error "No current article selected"))
9753   ;; Remove old bookmark, if one exists.
9754   (gnus-pull article gnus-newsgroup-bookmarks)
9755   ;; Set the new bookmark, which is on the form
9756   ;; (article-number . line-number-in-body).
9757   (push
9758    (cons article
9759          (save-excursion
9760            (set-buffer gnus-article-buffer)
9761            (count-lines
9762             (min (point)
9763                  (save-excursion
9764                    (article-goto-body)
9765                    (point)))
9766             (point))))
9767    gnus-newsgroup-bookmarks)
9768   (gnus-message 6 "A bookmark has been added to the current article."))
9769
9770 (defun gnus-summary-remove-bookmark (article)
9771   "Remove the bookmark from the current article."
9772   (interactive (list (gnus-summary-article-number)))
9773   ;; Remove old bookmark, if one exists.
9774   (if (not (assq article gnus-newsgroup-bookmarks))
9775       (gnus-message 6 "No bookmark in current article.")
9776     (gnus-pull article gnus-newsgroup-bookmarks)
9777     (gnus-message 6 "Removed bookmark.")))
9778
9779 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9780 (defun gnus-summary-mark-as-dormant (n)
9781   "Mark N articles forward as dormant.
9782 If N is negative, mark backward instead.  The difference between N and
9783 the actual number of articles marked is returned."
9784   (interactive "p")
9785   (gnus-summary-mark-forward n gnus-dormant-mark))
9786
9787 (defun gnus-summary-set-process-mark (article)
9788   "Set the process mark on ARTICLE and update the summary line."
9789   (setq gnus-newsgroup-processable
9790         (cons article
9791               (delq article gnus-newsgroup-processable)))
9792   (when (gnus-summary-goto-subject article)
9793     (gnus-summary-show-thread)
9794     (gnus-summary-goto-subject article)
9795     (gnus-summary-update-secondary-mark article)))
9796
9797 (defun gnus-summary-remove-process-mark (article)
9798   "Remove the process mark from ARTICLE and update the summary line."
9799   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9800   (when (gnus-summary-goto-subject article)
9801     (gnus-summary-show-thread)
9802     (gnus-summary-goto-subject article)
9803     (gnus-summary-update-secondary-mark article)))
9804
9805 (defun gnus-summary-set-saved-mark (article)
9806   "Set the process mark on ARTICLE and update the summary line."
9807   (push article gnus-newsgroup-saved)
9808   (when (gnus-summary-goto-subject article)
9809     (gnus-summary-update-secondary-mark article)))
9810
9811 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9812   "Mark N articles as read forwards.
9813 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9814 The difference between N and the actual number of articles marked is
9815 returned.
9816 If NO-EXPIRE, auto-expiry will be inhibited."
9817   (interactive "p")
9818   (gnus-summary-show-thread)
9819   (let ((backward (< n 0))
9820         (gnus-summary-goto-unread
9821          (and gnus-summary-goto-unread
9822               (not (eq gnus-summary-goto-unread 'never))
9823               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9824                                     gnus-ticked-mark gnus-dormant-mark)))))
9825         (n (abs n))
9826         (mark (or mark gnus-del-mark)))
9827     (while (and (> n 0)
9828                 (gnus-summary-mark-article nil mark no-expire)
9829                 (zerop (gnus-summary-next-subject
9830                         (if backward -1 1)
9831                         (and gnus-summary-goto-unread
9832                              (not (eq gnus-summary-goto-unread 'never)))
9833                         t)))
9834       (setq n (1- n)))
9835     (when (/= 0 n)
9836       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9837     (gnus-summary-recenter)
9838     (gnus-summary-position-point)
9839     (gnus-set-mode-line 'summary)
9840     n))
9841
9842 (defun gnus-summary-mark-article-as-read (mark)
9843   "Mark the current article quickly as read with MARK."
9844   (let ((article (gnus-summary-article-number)))
9845     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9846     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9847     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9848     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9849     (push (cons article mark) gnus-newsgroup-reads)
9850     ;; Possibly remove from cache, if that is used.
9851     (when gnus-use-cache
9852       (gnus-cache-enter-remove-article article))
9853     ;; Allow the backend to change the mark.
9854     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9855     ;; Check for auto-expiry.
9856     (when (and gnus-newsgroup-auto-expire
9857                (memq mark gnus-auto-expirable-marks))
9858       (setq mark gnus-expirable-mark)
9859       ;; Let the backend know about the mark change.
9860       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9861       (push article gnus-newsgroup-expirable))
9862     ;; Set the mark in the buffer.
9863     (gnus-summary-update-mark mark 'unread)
9864     t))
9865
9866 (defun gnus-summary-mark-article-as-unread (mark)
9867   "Mark the current article quickly as unread with MARK."
9868   (let* ((article (gnus-summary-article-number))
9869          (old-mark (gnus-summary-article-mark article)))
9870     ;; Allow the backend to change the mark.
9871     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9872     (if (eq mark old-mark)
9873         t
9874       (if (<= article 0)
9875           (progn
9876             (gnus-error 1 "Can't mark negative article numbers")
9877             nil)
9878         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9879         (setq gnus-newsgroup-spam-marked
9880               (delq article gnus-newsgroup-spam-marked))
9881         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9882         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9883         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9884         (cond ((= mark gnus-ticked-mark)
9885                (setq gnus-newsgroup-marked
9886                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9887                                               article)))
9888               ((= mark gnus-spam-mark)
9889                (setq gnus-newsgroup-spam-marked
9890                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9891                                               article)))
9892               ((= mark gnus-dormant-mark)
9893                (setq gnus-newsgroup-dormant
9894                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9895                                               article)))
9896               (t
9897                (setq gnus-newsgroup-unreads
9898                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9899                                               article))))
9900         (gnus-pull article gnus-newsgroup-reads)
9901
9902         ;; See whether the article is to be put in the cache.
9903         (and gnus-use-cache
9904              (vectorp (gnus-summary-article-header article))
9905              (save-excursion
9906                (gnus-cache-possibly-enter-article
9907                 gnus-newsgroup-name article
9908                 (= mark gnus-ticked-mark)
9909                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9910
9911         ;; Fix the mark.
9912         (gnus-summary-update-mark mark 'unread)
9913         t))))
9914
9915 (defun gnus-summary-mark-article (&optional article mark no-expire)
9916   "Mark ARTICLE with MARK.  MARK can be any character.
9917 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9918 `??' (dormant) and `?E' (expirable).
9919 If MARK is nil, then the default character `?r' is used.
9920 If ARTICLE is nil, then the article on the current line will be
9921 marked.
9922 If NO-EXPIRE, auto-expiry will be inhibited."
9923   ;; The mark might be a string.
9924   (when (stringp mark)
9925     (setq mark (aref mark 0)))
9926   ;; If no mark is given, then we check auto-expiring.
9927   (when (null mark)
9928     (setq mark gnus-del-mark))
9929   (when (and (not no-expire)
9930              gnus-newsgroup-auto-expire
9931              (memq mark gnus-auto-expirable-marks))
9932     (setq mark gnus-expirable-mark))
9933   (let ((article (or article (gnus-summary-article-number)))
9934         (old-mark (gnus-summary-article-mark article)))
9935     ;; Allow the backend to change the mark.
9936     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9937     (if (eq mark old-mark)
9938         t
9939       (unless article
9940         (error "No article on current line"))
9941       (if (not (if (or (= mark gnus-unread-mark)
9942                        (= mark gnus-ticked-mark)
9943                        (= mark gnus-spam-mark)
9944                        (= mark gnus-dormant-mark))
9945                    (gnus-mark-article-as-unread article mark)
9946                  (gnus-mark-article-as-read article mark)))
9947           t
9948         ;; See whether the article is to be put in the cache.
9949         (and gnus-use-cache
9950              (not (= mark gnus-canceled-mark))
9951              (vectorp (gnus-summary-article-header article))
9952              (save-excursion
9953                (gnus-cache-possibly-enter-article
9954                 gnus-newsgroup-name article
9955                 (= mark gnus-ticked-mark)
9956                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9957
9958         (when (gnus-summary-goto-subject article nil t)
9959           (let ((buffer-read-only nil))
9960             (gnus-summary-show-thread)
9961             ;; Fix the mark.
9962             (gnus-summary-update-mark mark 'unread)
9963             t))))))
9964
9965 (defun gnus-summary-update-secondary-mark (article)
9966   "Update the secondary (read, process, cache) mark."
9967   (gnus-summary-update-mark
9968    (cond ((memq article gnus-newsgroup-processable)
9969           gnus-process-mark)
9970          ((memq article gnus-newsgroup-cached)
9971           gnus-cached-mark)
9972          ((memq article gnus-newsgroup-replied)
9973           gnus-replied-mark)
9974          ((memq article gnus-newsgroup-forwarded)
9975           gnus-forwarded-mark)
9976          ((memq article gnus-newsgroup-saved)
9977           gnus-saved-mark)
9978          ((memq article gnus-newsgroup-recent)
9979           gnus-recent-mark)
9980          ((memq article gnus-newsgroup-unseen)
9981           gnus-unseen-mark)
9982          (t gnus-no-mark))
9983    'replied)
9984   (when (gnus-visual-p 'summary-highlight 'highlight)
9985     (gnus-run-hooks 'gnus-summary-update-hook))
9986   t)
9987
9988 (defun gnus-summary-update-download-mark (article)
9989   "Update the download mark."
9990   (gnus-summary-update-mark
9991    (cond ((memq article gnus-newsgroup-undownloaded)
9992           gnus-undownloaded-mark)
9993          (gnus-newsgroup-agentized
9994           gnus-downloaded-mark)
9995          (t
9996           gnus-no-mark))
9997    'download)
9998   (gnus-summary-update-line t)
9999   t)
10000
10001 (defun gnus-summary-update-mark (mark type)
10002   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10003         (buffer-read-only nil))
10004     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10005     (when forward
10006       (when (looking-at "\r")
10007         (incf forward))
10008       (when (<= (+ forward (point)) (point-max))
10009         ;; Go to the right position on the line.
10010         (goto-char (+ forward (point)))
10011         ;; Replace the old mark with the new mark.
10012         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10013         ;; Optionally update the marks by some user rule.
10014         (when (eq type 'unread)
10015           (gnus-data-set-mark
10016            (gnus-data-find (gnus-summary-article-number)) mark)
10017           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10018
10019 (defun gnus-mark-article-as-read (article &optional mark)
10020   "Enter ARTICLE in the pertinent lists and remove it from others."
10021   ;; Make the article expirable.
10022   (let ((mark (or mark gnus-del-mark)))
10023     (setq gnus-newsgroup-expirable
10024           (if (= mark gnus-expirable-mark)
10025               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10026             (delq article gnus-newsgroup-expirable)))
10027     ;; Remove from unread and marked lists.
10028     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10029     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10030     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10031     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10032     (push (cons article mark) gnus-newsgroup-reads)
10033     ;; Possibly remove from cache, if that is used.
10034     (when gnus-use-cache
10035       (gnus-cache-enter-remove-article article))
10036     t))
10037
10038 (defun gnus-mark-article-as-unread (article &optional mark)
10039   "Enter ARTICLE in the pertinent lists and remove it from others."
10040   (let ((mark (or mark gnus-ticked-mark)))
10041     (if (<= article 0)
10042         (progn
10043           (gnus-error 1 "Can't mark negative article numbers")
10044           nil)
10045       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10046             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10047             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10048             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10049             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10050
10051       ;; Unsuppress duplicates?
10052       (when gnus-suppress-duplicates
10053         (gnus-dup-unsuppress-article article))
10054
10055       (cond ((= mark gnus-ticked-mark)
10056              (setq gnus-newsgroup-marked
10057                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10058             ((= mark gnus-spam-mark)
10059              (setq gnus-newsgroup-spam-marked
10060                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10061                                             article)))
10062             ((= mark gnus-dormant-mark)
10063              (setq gnus-newsgroup-dormant
10064                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10065             (t
10066              (setq gnus-newsgroup-unreads
10067                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10068       (gnus-pull article gnus-newsgroup-reads)
10069       t)))
10070
10071 (defalias 'gnus-summary-mark-as-unread-forward
10072   'gnus-summary-tick-article-forward)
10073 (make-obsolete 'gnus-summary-mark-as-unread-forward
10074                'gnus-summary-tick-article-forward)
10075 (defun gnus-summary-tick-article-forward (n)
10076   "Tick N articles forwards.
10077 If N is negative, tick backwards instead.
10078 The difference between N and the number of articles ticked is returned."
10079   (interactive "p")
10080   (gnus-summary-mark-forward n gnus-ticked-mark))
10081
10082 (defalias 'gnus-summary-mark-as-unread-backward
10083   'gnus-summary-tick-article-backward)
10084 (make-obsolete 'gnus-summary-mark-as-unread-backward
10085                'gnus-summary-tick-article-backward)
10086 (defun gnus-summary-tick-article-backward (n)
10087   "Tick N articles backwards.
10088 The difference between N and the number of articles ticked is returned."
10089   (interactive "p")
10090   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10091
10092 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10093 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10094 (defun gnus-summary-tick-article (&optional article clear-mark)
10095   "Mark current article as unread.
10096 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10097 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10098   (interactive)
10099   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10100                                        gnus-ticked-mark)))
10101
10102 (defun gnus-summary-mark-as-read-forward (n)
10103   "Mark N articles as read forwards.
10104 If N is negative, mark backwards instead.
10105 The difference between N and the actual number of articles marked is
10106 returned."
10107   (interactive "p")
10108   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10109
10110 (defun gnus-summary-mark-as-read-backward (n)
10111   "Mark the N articles as read backwards.
10112 The difference between N and the actual number of articles marked is
10113 returned."
10114   (interactive "p")
10115   (gnus-summary-mark-forward
10116    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10117
10118 (defun gnus-summary-mark-as-read (&optional article mark)
10119   "Mark current article as read.
10120 ARTICLE specifies the article to be marked as read.
10121 MARK specifies a string to be inserted at the beginning of the line."
10122   (gnus-summary-mark-article article mark))
10123
10124 (defun gnus-summary-clear-mark-forward (n)
10125   "Clear marks from N articles forward.
10126 If N is negative, clear backward instead.
10127 The difference between N and the number of marks cleared is returned."
10128   (interactive "p")
10129   (gnus-summary-mark-forward n gnus-unread-mark))
10130
10131 (defun gnus-summary-clear-mark-backward (n)
10132   "Clear marks from N articles backward.
10133 The difference between N and the number of marks cleared is returned."
10134   (interactive "p")
10135   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10136
10137 (defun gnus-summary-mark-unread-as-read ()
10138   "Intended to be used by `gnus-summary-mark-article-hook'."
10139   (when (memq gnus-current-article gnus-newsgroup-unreads)
10140     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10141
10142 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10143   "Intended to be used by `gnus-summary-mark-article-hook'."
10144   (let ((mark (gnus-summary-article-mark)))
10145     (when (or (gnus-unread-mark-p mark)
10146               (gnus-read-mark-p mark))
10147       (gnus-summary-mark-article gnus-current-article
10148                                  (or new-mark gnus-read-mark)))))
10149
10150 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10151   "Intended to be used by `gnus-summary-mark-article-hook'."
10152   (let ((mark (gnus-summary-article-mark)))
10153     (when (or (gnus-unread-mark-p mark)
10154               (gnus-read-mark-p mark))
10155       (gnus-summary-mark-article (gnus-summary-article-number)
10156                                  (or new-mark gnus-read-mark)))))
10157
10158 (defun gnus-summary-mark-unread-as-ticked ()
10159   "Intended to be used by `gnus-summary-mark-article-hook'."
10160   (when (memq gnus-current-article gnus-newsgroup-unreads)
10161     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10162
10163 (defun gnus-summary-mark-region-as-read (point mark all)
10164   "Mark all unread articles between point and mark as read.
10165 If given a prefix, mark all articles between point and mark as read,
10166 even ticked and dormant ones."
10167   (interactive "r\nP")
10168   (save-excursion
10169     (let (article)
10170       (goto-char point)
10171       (beginning-of-line)
10172       (while (and
10173               (< (point) mark)
10174               (progn
10175                 (when (or all
10176                           (memq (setq article (gnus-summary-article-number))
10177                                 gnus-newsgroup-unreads))
10178                   (gnus-summary-mark-article article gnus-del-mark))
10179                 t)
10180               (gnus-summary-find-next))))))
10181
10182 (defun gnus-summary-mark-below (score mark)
10183   "Mark articles with score less than SCORE with MARK."
10184   (interactive "P\ncMark: ")
10185   (setq score (if score
10186                   (prefix-numeric-value score)
10187                 (or gnus-summary-default-score 0)))
10188   (save-excursion
10189     (set-buffer gnus-summary-buffer)
10190     (goto-char (point-min))
10191     (while
10192         (progn
10193           (and (< (gnus-summary-article-score) score)
10194                (gnus-summary-mark-article nil mark))
10195           (gnus-summary-find-next)))))
10196
10197 (defun gnus-summary-kill-below (&optional score)
10198   "Mark articles with score below SCORE as read."
10199   (interactive "P")
10200   (gnus-summary-mark-below score gnus-killed-mark))
10201
10202 (defun gnus-summary-clear-above (&optional score)
10203   "Clear all marks from articles with score above SCORE."
10204   (interactive "P")
10205   (gnus-summary-mark-above score gnus-unread-mark))
10206
10207 (defun gnus-summary-tick-above (&optional score)
10208   "Tick all articles with score above SCORE."
10209   (interactive "P")
10210   (gnus-summary-mark-above score gnus-ticked-mark))
10211
10212 (defun gnus-summary-mark-above (score mark)
10213   "Mark articles with score over SCORE with MARK."
10214   (interactive "P\ncMark: ")
10215   (setq score (if score
10216                   (prefix-numeric-value score)
10217                 (or gnus-summary-default-score 0)))
10218   (save-excursion
10219     (set-buffer gnus-summary-buffer)
10220     (goto-char (point-min))
10221     (while (and (progn
10222                   (when (> (gnus-summary-article-score) score)
10223                     (gnus-summary-mark-article nil mark))
10224                   t)
10225                 (gnus-summary-find-next)))))
10226
10227 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10228 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10229 (defun gnus-summary-limit-include-expunged (&optional no-error)
10230   "Display all the hidden articles that were expunged for low scores."
10231   (interactive)
10232   (let ((buffer-read-only nil))
10233     (let ((scored gnus-newsgroup-scored)
10234           headers h)
10235       (while scored
10236         (unless (gnus-summary-article-header (caar scored))
10237           (and (setq h (gnus-number-to-header (caar scored)))
10238                (< (cdar scored) gnus-summary-expunge-below)
10239                (push h headers)))
10240         (setq scored (cdr scored)))
10241       (if (not headers)
10242           (when (not no-error)
10243             (error "No expunged articles hidden"))
10244         (goto-char (point-min))
10245         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10246         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10247         (mapcar (lambda (x) (push (mail-header-number x)
10248                                   gnus-newsgroup-limit))
10249                 headers)
10250         (gnus-summary-prepare-unthreaded (nreverse headers))
10251         (goto-char (point-min))
10252         (gnus-summary-position-point)
10253         t))))
10254
10255 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10256   "Mark all unread articles in this newsgroup as read.
10257 If prefix argument ALL is non-nil, ticked and dormant articles will
10258 also be marked as read.
10259 If QUIETLY is non-nil, no questions will be asked.
10260
10261 If TO-HERE is non-nil, it should be a point in the buffer.  All
10262 articles before (after, if REVERSE is set) this point will be marked
10263 as read.
10264
10265 Note that this function will only catch up the unread article
10266 in the current summary buffer limitation.
10267
10268 The number of articles marked as read is returned."
10269   (interactive "P")
10270   (prog1
10271       (save-excursion
10272         (when (or quietly
10273                   (not gnus-interactive-catchup) ;Without confirmation?
10274                   gnus-expert-user
10275                   (gnus-y-or-n-p
10276                    (if all
10277                        "Mark absolutely all articles as read? "
10278                      "Mark all unread articles as read? ")))
10279           (if (and not-mark
10280                    (not gnus-newsgroup-adaptive)
10281                    (not gnus-newsgroup-auto-expire)
10282                    (not gnus-suppress-duplicates)
10283                    (or (not gnus-use-cache)
10284                        (eq gnus-use-cache 'passive)))
10285               (progn
10286                 (when all
10287                   (setq gnus-newsgroup-marked nil
10288                         gnus-newsgroup-spam-marked nil
10289                         gnus-newsgroup-dormant nil))
10290                 (setq gnus-newsgroup-unreads
10291                       (gnus-sorted-nunion
10292                        (gnus-intersection gnus-newsgroup-unreads
10293                                           gnus-newsgroup-downloadable)
10294                        gnus-newsgroup-unfetched)))
10295             ;; We actually mark all articles as canceled, which we
10296             ;; have to do when using auto-expiry or adaptive scoring.
10297             (gnus-summary-show-all-threads)
10298             (if (and to-here reverse)
10299                 (progn
10300                   (goto-char to-here)
10301                   (gnus-summary-mark-current-read-and-unread-as-read
10302                    gnus-catchup-mark)
10303                   (while (gnus-summary-find-next (not all))
10304                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10305               (when (gnus-summary-first-subject (not all))
10306                 (while (and
10307                         (if to-here (< (point) to-here) t)
10308                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10309                         (gnus-summary-find-next (not all))))))
10310             (gnus-set-mode-line 'summary))
10311           t))
10312     (gnus-summary-position-point)))
10313
10314 (defun gnus-summary-catchup-to-here (&optional all)
10315   "Mark all unticked articles before the current one as read.
10316 If ALL is non-nil, also mark ticked and dormant articles as read."
10317   (interactive "P")
10318   (save-excursion
10319     (gnus-save-hidden-threads
10320       (let ((beg (point)))
10321         ;; We check that there are unread articles.
10322         (when (or all (gnus-summary-find-prev))
10323           (gnus-summary-catchup all t beg)))))
10324   (gnus-summary-position-point))
10325
10326 (defun gnus-summary-catchup-from-here (&optional all)
10327   "Mark all unticked articles after (and including) the current one as read.
10328 If ALL is non-nil, also mark ticked and dormant articles as read."
10329   (interactive "P")
10330   (save-excursion
10331     (gnus-save-hidden-threads
10332       (let ((beg (point)))
10333         ;; We check that there are unread articles.
10334         (when (or all (gnus-summary-find-next))
10335           (gnus-summary-catchup all t beg nil t)))))
10336   (gnus-summary-position-point))
10337
10338 (defun gnus-summary-catchup-all (&optional quietly)
10339   "Mark all articles in this newsgroup as read.
10340 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10341 instead, which marks only unread articles as read."
10342   (interactive "P")
10343   (gnus-summary-catchup t quietly))
10344
10345 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10346   "Mark all unread articles in this group as read, then exit.
10347 If prefix argument ALL is non-nil, all articles are marked as read.
10348 If QUIETLY is non-nil, no questions will be asked."
10349   (interactive "P")
10350   (when (gnus-summary-catchup all quietly nil 'fast)
10351     ;; Select next newsgroup or exit.
10352     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10353              (eq gnus-auto-select-next 'quietly))
10354         (gnus-summary-next-group nil)
10355       (gnus-summary-exit))))
10356
10357 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10358   "Mark all articles in this newsgroup as read, and then exit.
10359 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10360 instead, which marks only unread articles as read."
10361   (interactive "P")
10362   (gnus-summary-catchup-and-exit t quietly))
10363
10364 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10365   "Mark all articles in this group as read and select the next group.
10366 If given a prefix, mark all articles, unread as well as ticked, as
10367 read."
10368   (interactive "P")
10369   (save-excursion
10370     (gnus-summary-catchup all))
10371   (gnus-summary-next-group))
10372
10373 ;;;
10374 ;;; with article
10375 ;;;
10376
10377 (defmacro gnus-with-article (article &rest forms)
10378   "Select ARTICLE and perform FORMS in the original article buffer.
10379 Then replace the article with the result."
10380   `(progn
10381      ;; We don't want the article to be marked as read.
10382      (let (gnus-mark-article-hook)
10383        (gnus-summary-select-article t t nil ,article))
10384      (set-buffer gnus-original-article-buffer)
10385      ,@forms
10386      (if (not (gnus-check-backend-function
10387                'request-replace-article (car gnus-article-current)))
10388          (gnus-message 5 "Read-only group; not replacing")
10389        (unless (gnus-request-replace-article
10390                 ,article (car gnus-article-current)
10391                 (current-buffer) t)
10392          (error "Couldn't replace article")))
10393      ;; The cache and backlog have to be flushed somewhat.
10394      (when gnus-keep-backlog
10395        (gnus-backlog-remove-article
10396         (car gnus-article-current) (cdr gnus-article-current)))
10397      (when gnus-use-cache
10398        (gnus-cache-update-article
10399         (car gnus-article-current) (cdr gnus-article-current)))))
10400
10401 (put 'gnus-with-article 'lisp-indent-function 1)
10402 (put 'gnus-with-article 'edebug-form-spec '(form body))
10403
10404 ;; Thread-based commands.
10405
10406 (defun gnus-summary-articles-in-thread (&optional article)
10407   "Return a list of all articles in the current thread.
10408 If ARTICLE is non-nil, return all articles in the thread that starts
10409 with that article."
10410   (let* ((article (or article (gnus-summary-article-number)))
10411          (data (gnus-data-find-list article))
10412          (top-level (gnus-data-level (car data)))
10413          (top-subject
10414           (cond ((null gnus-thread-operation-ignore-subject)
10415                  (gnus-simplify-subject-re
10416                   (mail-header-subject (gnus-data-header (car data)))))
10417                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10418                  (gnus-simplify-subject-fuzzy
10419                   (mail-header-subject (gnus-data-header (car data)))))
10420                 (t nil)))
10421          (end-point (save-excursion
10422                       (if (gnus-summary-go-to-next-thread)
10423                           (point) (point-max))))
10424          articles)
10425     (while (and data
10426                 (< (gnus-data-pos (car data)) end-point))
10427       (when (or (not top-subject)
10428                 (string= top-subject
10429                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10430                              (gnus-simplify-subject-fuzzy
10431                               (mail-header-subject
10432                                (gnus-data-header (car data))))
10433                            (gnus-simplify-subject-re
10434                             (mail-header-subject
10435                              (gnus-data-header (car data)))))))
10436         (push (gnus-data-number (car data)) articles))
10437       (unless (and (setq data (cdr data))
10438                    (> (gnus-data-level (car data)) top-level))
10439         (setq data nil)))
10440     ;; Return the list of articles.
10441     (nreverse articles)))
10442
10443 (defun gnus-summary-rethread-current ()
10444   "Rethread the thread the current article is part of."
10445   (interactive)
10446   (let* ((gnus-show-threads t)
10447          (article (gnus-summary-article-number))
10448          (id (mail-header-id (gnus-summary-article-header)))
10449          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10450     (unless id
10451       (error "No article on the current line"))
10452     (gnus-rebuild-thread id)
10453     (gnus-summary-goto-subject article)))
10454
10455 (defun gnus-summary-reparent-thread ()
10456   "Make the current article child of the marked (or previous) article.
10457
10458 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10459 is non-nil or the Subject: of both articles are the same."
10460   (interactive)
10461   (unless (not (gnus-group-read-only-p))
10462     (error "The current newsgroup does not support article editing"))
10463   (unless (<= (length gnus-newsgroup-processable) 1)
10464     (error "No more than one article may be marked"))
10465   (save-window-excursion
10466     (let ((gnus-article-buffer " *reparent*")
10467           (current-article (gnus-summary-article-number))
10468           ;; First grab the marked article, otherwise one line up.
10469           (parent-article (if (not (null gnus-newsgroup-processable))
10470                               (car gnus-newsgroup-processable)
10471                             (save-excursion
10472                               (if (eq (forward-line -1) 0)
10473                                   (gnus-summary-article-number)
10474                                 (error "Beginning of summary buffer"))))))
10475       (unless (not (eq current-article parent-article))
10476         (error "An article may not be self-referential"))
10477       (let ((message-id (mail-header-id
10478                          (gnus-summary-article-header parent-article))))
10479         (unless (and message-id (not (equal message-id "")))
10480           (error "No message-id in desired parent"))
10481         (gnus-with-article current-article
10482           (save-restriction
10483             (goto-char (point-min))
10484             (message-narrow-to-head)
10485             (if (re-search-forward "^References: " nil t)
10486                 (progn
10487                   (re-search-forward "^[^ \t]" nil t)
10488                   (forward-line -1)
10489                   (end-of-line)
10490                   (insert " " message-id))
10491               (insert "References: " message-id "\n"))))
10492         (set-buffer gnus-summary-buffer)
10493         (gnus-summary-unmark-all-processable)
10494         (gnus-summary-update-article current-article)
10495         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10496             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10497         (gnus-summary-rethread-current)
10498         (gnus-message 3 "Article %d is now the child of article %d"
10499                       current-article parent-article)))))
10500
10501 (defun gnus-summary-toggle-threads (&optional arg)
10502   "Toggle showing conversation threads.
10503 If ARG is positive number, turn showing conversation threads on."
10504   (interactive "P")
10505   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10506     (setq gnus-show-threads
10507           (if (null arg) (not gnus-show-threads)
10508             (> (prefix-numeric-value arg) 0)))
10509     (gnus-summary-prepare)
10510     (gnus-summary-goto-subject current)
10511     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10512     (gnus-summary-position-point)))
10513
10514 (defun gnus-summary-show-all-threads ()
10515   "Show all threads."
10516   (interactive)
10517   (save-excursion
10518     (let ((buffer-read-only nil))
10519       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10520   (gnus-summary-position-point))
10521
10522 (defun gnus-summary-show-thread ()
10523   "Show thread subtrees.
10524 Returns nil if no thread was there to be shown."
10525   (interactive)
10526   (let ((buffer-read-only nil)
10527         (orig (point))
10528         (end (point-at-eol))
10529         ;; Leave point at bol
10530         (beg (progn (beginning-of-line) (point))))
10531     (prog1
10532         ;; Any hidden lines here?
10533         (search-forward "\r" end t)
10534       (subst-char-in-region beg end ?\^M ?\n t)
10535       (goto-char orig)
10536       (gnus-summary-position-point))))
10537
10538 (defun gnus-summary-maybe-hide-threads ()
10539   "If requested, hide the threads that should be hidden."
10540   (when (and gnus-show-threads
10541              gnus-thread-hide-subtree)
10542     (gnus-summary-hide-all-threads
10543      (if (or (consp gnus-thread-hide-subtree)
10544              (functionp gnus-thread-hide-subtree))
10545          (gnus-make-predicate gnus-thread-hide-subtree)
10546        nil))))
10547
10548 ;;; Hiding predicates.
10549
10550 (defun gnus-article-unread-p (header)
10551   (memq (mail-header-number header) gnus-newsgroup-unreads))
10552
10553 (defun gnus-article-unseen-p (header)
10554   (memq (mail-header-number header) gnus-newsgroup-unseen))
10555
10556 (defun gnus-map-articles (predicate articles)
10557   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10558   (apply 'gnus-or (mapcar predicate
10559                           (mapcar 'gnus-summary-article-header articles))))
10560
10561 (defun gnus-summary-hide-all-threads (&optional predicate)
10562   "Hide all thread subtrees.
10563 If PREDICATE is supplied, threads that satisfy this predicate
10564 will not be hidden."
10565   (interactive)
10566   (save-excursion
10567     (goto-char (point-min))
10568     (let ((end nil))
10569       (while (not end)
10570         (when (or (not predicate)
10571                   (gnus-map-articles
10572                    predicate (gnus-summary-article-children)))
10573             (gnus-summary-hide-thread))
10574         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10575   (gnus-summary-position-point))
10576
10577 (defun gnus-summary-hide-thread ()
10578   "Hide thread subtrees.
10579 If PREDICATE is supplied, threads that satisfy this predicate
10580 will not be hidden.
10581 Returns nil if no threads were there to be hidden."
10582   (interactive)
10583   (let ((buffer-read-only nil)
10584         (start (point))
10585         (article (gnus-summary-article-number)))
10586     (goto-char start)
10587     ;; Go forward until either the buffer ends or the subthread
10588     ;; ends.
10589     (when (and (not (eobp))
10590                (or (zerop (gnus-summary-next-thread 1 t))
10591                    (goto-char (point-max))))
10592       (prog1
10593           (if (and (> (point) start)
10594                    (search-backward "\n" start t))
10595               (progn
10596                 (subst-char-in-region start (point) ?\n ?\^M)
10597                 (gnus-summary-goto-subject article))
10598             (goto-char start)
10599             nil)))))
10600
10601 (defun gnus-summary-go-to-next-thread (&optional previous)
10602   "Go to the same level (or less) next thread.
10603 If PREVIOUS is non-nil, go to previous thread instead.
10604 Return the article number moved to, or nil if moving was impossible."
10605   (let ((level (gnus-summary-thread-level))
10606         (way (if previous -1 1))
10607         (beg (point)))
10608     (forward-line way)
10609     (while (and (not (eobp))
10610                 (< level (gnus-summary-thread-level)))
10611       (forward-line way))
10612     (if (eobp)
10613         (progn
10614           (goto-char beg)
10615           nil)
10616       (setq beg (point))
10617       (prog1
10618           (gnus-summary-article-number)
10619         (goto-char beg)))))
10620
10621 (defun gnus-summary-next-thread (n &optional silent)
10622   "Go to the same level next N'th thread.
10623 If N is negative, search backward instead.
10624 Returns the difference between N and the number of skips actually
10625 done.
10626
10627 If SILENT, don't output messages."
10628   (interactive "p")
10629   (let ((backward (< n 0))
10630         (n (abs n)))
10631     (while (and (> n 0)
10632                 (gnus-summary-go-to-next-thread backward))
10633       (decf n))
10634     (unless silent
10635       (gnus-summary-position-point))
10636     (when (and (not silent) (/= 0 n))
10637       (gnus-message 7 "No more threads"))
10638     n))
10639
10640 (defun gnus-summary-prev-thread (n)
10641   "Go to the same level previous N'th thread.
10642 Returns the difference between N and the number of skips actually
10643 done."
10644   (interactive "p")
10645   (gnus-summary-next-thread (- n)))
10646
10647 (defun gnus-summary-go-down-thread ()
10648   "Go down one level in the current thread."
10649   (let ((children (gnus-summary-article-children)))
10650     (when children
10651       (gnus-summary-goto-subject (car children)))))
10652
10653 (defun gnus-summary-go-up-thread ()
10654   "Go up one level in the current thread."
10655   (let ((parent (gnus-summary-article-parent)))
10656     (when parent
10657       (gnus-summary-goto-subject parent))))
10658
10659 (defun gnus-summary-down-thread (n)
10660   "Go down thread N steps.
10661 If N is negative, go up instead.
10662 Returns the difference between N and how many steps down that were
10663 taken."
10664   (interactive "p")
10665   (let ((up (< n 0))
10666         (n (abs n)))
10667     (while (and (> n 0)
10668                 (if up (gnus-summary-go-up-thread)
10669                   (gnus-summary-go-down-thread)))
10670       (setq n (1- n)))
10671     (gnus-summary-position-point)
10672     (when (/= 0 n)
10673       (gnus-message 7 "Can't go further"))
10674     n))
10675
10676 (defun gnus-summary-up-thread (n)
10677   "Go up thread N steps.
10678 If N is negative, go down instead.
10679 Returns the difference between N and how many steps down that were
10680 taken."
10681   (interactive "p")
10682   (gnus-summary-down-thread (- n)))
10683
10684 (defun gnus-summary-top-thread ()
10685   "Go to the top of the thread."
10686   (interactive)
10687   (while (gnus-summary-go-up-thread))
10688   (gnus-summary-article-number))
10689
10690 (defun gnus-summary-kill-thread (&optional unmark)
10691   "Mark articles under current thread as read.
10692 If the prefix argument is positive, remove any kinds of marks.
10693 If the prefix argument is negative, tick articles instead."
10694   (interactive "P")
10695   (when unmark
10696     (setq unmark (prefix-numeric-value unmark)))
10697   (let ((articles (gnus-summary-articles-in-thread)))
10698     (save-excursion
10699       ;; Expand the thread.
10700       (gnus-summary-show-thread)
10701       ;; Mark all the articles.
10702       (while articles
10703         (gnus-summary-goto-subject (car articles))
10704         (cond ((null unmark)
10705                (gnus-summary-mark-article-as-read gnus-killed-mark))
10706               ((> unmark 0)
10707                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10708               (t
10709                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10710         (setq articles (cdr articles))))
10711     ;; Hide killed subtrees.
10712     (and (null unmark)
10713          gnus-thread-hide-killed
10714          (gnus-summary-hide-thread))
10715     ;; If marked as read, go to next unread subject.
10716     (when (null unmark)
10717       ;; Go to next unread subject.
10718       (gnus-summary-next-subject 1 t)))
10719   (gnus-set-mode-line 'summary))
10720
10721 ;; Summary sorting commands
10722
10723 (defun gnus-summary-sort-by-number (&optional reverse)
10724   "Sort the summary buffer by article number.
10725 Argument REVERSE means reverse order."
10726   (interactive "P")
10727   (gnus-summary-sort 'number reverse))
10728
10729 (defun gnus-summary-sort-by-random (&optional reverse)
10730   "Randomize the order in the summary buffer.
10731 Argument REVERSE means to randomize in reverse order."
10732   (interactive "P")
10733   (gnus-summary-sort 'random reverse))
10734
10735 (defun gnus-summary-sort-by-author (&optional reverse)
10736   "Sort the summary buffer by author name alphabetically.
10737 If `case-fold-search' is non-nil, case of letters is ignored.
10738 Argument REVERSE means reverse order."
10739   (interactive "P")
10740   (gnus-summary-sort 'author reverse))
10741
10742 (defun gnus-summary-sort-by-subject (&optional reverse)
10743   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10744 If `case-fold-search' is non-nil, case of letters is ignored.
10745 Argument REVERSE means reverse order."
10746   (interactive "P")
10747   (gnus-summary-sort 'subject reverse))
10748
10749 (defun gnus-summary-sort-by-date (&optional reverse)
10750   "Sort the summary buffer by date.
10751 Argument REVERSE means reverse order."
10752   (interactive "P")
10753   (gnus-summary-sort 'date reverse))
10754
10755 (defun gnus-summary-sort-by-score (&optional reverse)
10756   "Sort the summary buffer by score.
10757 Argument REVERSE means reverse order."
10758   (interactive "P")
10759   (gnus-summary-sort 'score reverse))
10760
10761 (defun gnus-summary-sort-by-lines (&optional reverse)
10762   "Sort the summary buffer by the number of lines.
10763 Argument REVERSE means reverse order."
10764   (interactive "P")
10765   (gnus-summary-sort 'lines reverse))
10766
10767 (defun gnus-summary-sort-by-chars (&optional reverse)
10768   "Sort the summary buffer by article length.
10769 Argument REVERSE means reverse order."
10770   (interactive "P")
10771   (gnus-summary-sort 'chars reverse))
10772
10773 (defun gnus-summary-sort-by-original (&optional reverse)
10774   "Sort the summary buffer using the default sorting method.
10775 Argument REVERSE means reverse order."
10776   (interactive "P")
10777   (let* ((buffer-read-only)
10778          (gnus-summary-prepare-hook nil))
10779     ;; We do the sorting by regenerating the threads.
10780     (gnus-summary-prepare)
10781     ;; Hide subthreads if needed.
10782     (gnus-summary-maybe-hide-threads)))
10783
10784 (defun gnus-summary-sort (predicate reverse)
10785   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10786   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10787          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10788          (gnus-thread-sort-functions
10789           (if (not reverse)
10790               thread
10791             `(lambda (t1 t2)
10792                (,thread t2 t1))))
10793          (gnus-sort-gathered-threads-function
10794           gnus-thread-sort-functions)
10795          (gnus-article-sort-functions
10796           (if (not reverse)
10797               article
10798             `(lambda (t1 t2)
10799                (,article t2 t1))))
10800          (buffer-read-only)
10801          (gnus-summary-prepare-hook nil))
10802     ;; We do the sorting by regenerating the threads.
10803     (gnus-summary-prepare)
10804     ;; Hide subthreads if needed.
10805     (gnus-summary-maybe-hide-threads)))
10806
10807 ;; Summary saving commands.
10808
10809 (defun gnus-summary-save-article (&optional n not-saved)
10810   "Save the current article using the default saver function.
10811 If N is a positive number, save the N next articles.
10812 If N is a negative number, save the N previous articles.
10813 If N is nil and any articles have been marked with the process mark,
10814 save those articles instead.
10815 The variable `gnus-default-article-saver' specifies the saver function."
10816   (interactive "P")
10817   (let* ((articles (gnus-summary-work-articles n))
10818          (save-buffer (save-excursion
10819                         (nnheader-set-temp-buffer " *Gnus Save*")))
10820          (num (length articles))
10821          header file)
10822     (dolist (article articles)
10823       (setq header (gnus-summary-article-header article))
10824       (if (not (vectorp header))
10825           ;; This is a pseudo-article.
10826           (if (assq 'name header)
10827               (gnus-copy-file (cdr (assq 'name header)))
10828             (gnus-message 1 "Article %d is unsaveable" article))
10829         ;; This is a real article.
10830         (save-window-excursion
10831           (let ((gnus-display-mime-function nil)
10832                 (gnus-article-prepare-hook nil))
10833             (gnus-summary-select-article t nil nil article)))
10834         (save-excursion
10835           (set-buffer save-buffer)
10836           (erase-buffer)
10837           (insert-buffer-substring gnus-original-article-buffer))
10838         (setq file (gnus-article-save save-buffer file num))
10839         (gnus-summary-remove-process-mark article)
10840         (unless not-saved
10841           (gnus-summary-set-saved-mark article))))
10842     (gnus-kill-buffer save-buffer)
10843     (gnus-summary-position-point)
10844     (gnus-set-mode-line 'summary)
10845     n))
10846
10847 (defun gnus-summary-pipe-output (&optional arg headers)
10848   "Pipe the current article to a subprocess.
10849 If N is a positive number, pipe the N next articles.
10850 If N is a negative number, pipe the N previous articles.
10851 If N is nil and any articles have been marked with the process mark,
10852 pipe those articles instead.
10853 If HEADERS (the symbolic prefix), include the headers, too."
10854   (interactive (gnus-interactive "P\ny"))
10855   (require 'gnus-art)
10856   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10857         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10858     (gnus-summary-save-article arg t))
10859   (let ((buffer (get-buffer "*Shell Command Output*")))
10860     (when (and buffer
10861                (not (zerop (buffer-size buffer))))
10862       (gnus-configure-windows 'pipe))))
10863
10864 (defun gnus-summary-save-article-mail (&optional arg)
10865   "Append the current article to an mail file.
10866 If N is a positive number, save the N next articles.
10867 If N is a negative number, save the N previous articles.
10868 If N is nil and any articles have been marked with the process mark,
10869 save those articles instead."
10870   (interactive "P")
10871   (require 'gnus-art)
10872   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10873     (gnus-summary-save-article arg)))
10874
10875 (defun gnus-summary-save-article-rmail (&optional arg)
10876   "Append the current article to an rmail file.
10877 If N is a positive number, save the N next articles.
10878 If N is a negative number, save the N previous articles.
10879 If N is nil and any articles have been marked with the process mark,
10880 save those articles instead."
10881   (interactive "P")
10882   (require 'gnus-art)
10883   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10884     (gnus-summary-save-article arg)))
10885
10886 (defun gnus-summary-save-article-file (&optional arg)
10887   "Append the current article to a file.
10888 If N is a positive number, save the N next articles.
10889 If N is a negative number, save the N previous articles.
10890 If N is nil and any articles have been marked with the process mark,
10891 save those articles instead."
10892   (interactive "P")
10893   (require 'gnus-art)
10894   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10895     (gnus-summary-save-article arg)))
10896
10897 (defun gnus-summary-write-article-file (&optional arg)
10898   "Write the current article to a file, deleting the previous file.
10899 If N is a positive number, save the N next articles.
10900 If N is a negative number, save the N previous articles.
10901 If N is nil and any articles have been marked with the process mark,
10902 save those articles instead."
10903   (interactive "P")
10904   (require 'gnus-art)
10905   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10906     (gnus-summary-save-article arg)))
10907
10908 (defun gnus-summary-save-article-body-file (&optional arg)
10909   "Append the current article body to a file.
10910 If N is a positive number, save the N next articles.
10911 If N is a negative number, save the N previous articles.
10912 If N is nil and any articles have been marked with the process mark,
10913 save those articles instead."
10914   (interactive "P")
10915   (require 'gnus-art)
10916   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10917     (gnus-summary-save-article arg)))
10918
10919 (defun gnus-summary-muttprint (&optional arg)
10920   "Print the current article using Muttprint.
10921 If N is a positive number, save the N next articles.
10922 If N is a negative number, save the N previous articles.
10923 If N is nil and any articles have been marked with the process mark,
10924 save those articles instead."
10925   (interactive "P")
10926   (require 'gnus-art)
10927   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10928     (gnus-summary-save-article arg t)))
10929
10930 (defun gnus-summary-pipe-message (program)
10931   "Pipe the current article through PROGRAM."
10932   (interactive "sProgram: ")
10933   (gnus-summary-select-article)
10934   (let ((mail-header-separator ""))
10935     (gnus-eval-in-buffer-window gnus-article-buffer
10936       (save-restriction
10937         (widen)
10938         (let ((start (window-start))
10939               buffer-read-only)
10940           (message-pipe-buffer-body program)
10941           (set-window-start (get-buffer-window (current-buffer)) start))))))
10942
10943 (defun gnus-get-split-value (methods)
10944   "Return a value based on the split METHODS."
10945   (let (split-name method result match)
10946     (when methods
10947       (save-excursion
10948         (set-buffer gnus-original-article-buffer)
10949         (save-restriction
10950           (nnheader-narrow-to-headers)
10951           (while (and methods (not split-name))
10952             (goto-char (point-min))
10953             (setq method (pop methods))
10954             (setq match (car method))
10955             (when (cond
10956                    ((stringp match)
10957                     ;; Regular expression.
10958                     (ignore-errors
10959                       (re-search-forward match nil t)))
10960                    ((functionp match)
10961                     ;; Function.
10962                     (save-restriction
10963                       (widen)
10964                       (setq result (funcall match gnus-newsgroup-name))))
10965                    ((consp match)
10966                     ;; Form.
10967                     (save-restriction
10968                       (widen)
10969                       (setq result (eval match)))))
10970               (setq split-name (cdr method))
10971               (cond ((stringp result)
10972                      (push (expand-file-name
10973                             result gnus-article-save-directory)
10974                            split-name))
10975                     ((consp result)
10976                      (setq split-name (append result split-name)))))))))
10977     (nreverse split-name)))
10978
10979 (defun gnus-valid-move-group-p (group)
10980   (and (boundp group)
10981        (symbol-name group)
10982        (symbol-value group)
10983        (gnus-get-function (gnus-find-method-for-group
10984                            (symbol-name group)) 'request-accept-article t)))
10985
10986 (defun gnus-read-move-group-name (prompt default articles prefix)
10987   "Read a group name."
10988   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10989          (minibuffer-confirm-incomplete nil) ; XEmacs
10990          (prom
10991           (format "%s %s to:"
10992                   prompt
10993                   (if (> (length articles) 1)
10994                       (format "these %d articles" (length articles))
10995                     "this article")))
10996          (to-newsgroup
10997           (cond
10998            ((null split-name)
10999             (gnus-completing-read-with-default
11000              default prom
11001              gnus-active-hashtb
11002              'gnus-valid-move-group-p
11003              nil prefix
11004              'gnus-group-history))
11005            ((= 1 (length split-name))
11006             (gnus-completing-read-with-default
11007              (car split-name) prom
11008              gnus-active-hashtb
11009              'gnus-valid-move-group-p
11010              nil nil
11011              'gnus-group-history))
11012            (t
11013             (gnus-completing-read-with-default
11014              nil prom
11015              (mapcar (lambda (el) (list el))
11016                      (nreverse split-name))
11017              nil nil nil
11018              'gnus-group-history))))
11019          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11020     (when to-newsgroup
11021       (if (or (string= to-newsgroup "")
11022               (string= to-newsgroup prefix))
11023           (setq to-newsgroup default))
11024       (unless to-newsgroup
11025         (error "No group name entered"))
11026       (or (gnus-active to-newsgroup)
11027           (gnus-activate-group to-newsgroup nil nil to-method)
11028           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11029                                      to-newsgroup))
11030               (or (and (gnus-request-create-group to-newsgroup to-method)
11031                        (gnus-activate-group
11032                         to-newsgroup nil nil to-method)
11033                        (gnus-subscribe-group to-newsgroup))
11034                   (error "Couldn't create group %s" to-newsgroup)))
11035           (error "No such group: %s" to-newsgroup)))
11036     to-newsgroup))
11037
11038 (defun gnus-summary-save-parts (type dir n &optional reverse)
11039   "Save parts matching TYPE to DIR.
11040 If REVERSE, save parts that do not match TYPE."
11041   (interactive
11042    (list (read-string "Save parts of type: "
11043                       (or (car gnus-summary-save-parts-type-history)
11044                           gnus-summary-save-parts-default-mime)
11045                       'gnus-summary-save-parts-type-history)
11046          (setq gnus-summary-save-parts-last-directory
11047                (read-file-name "Save to directory: "
11048                                gnus-summary-save-parts-last-directory
11049                                nil t))
11050          current-prefix-arg))
11051   (gnus-summary-iterate n
11052     (let ((gnus-display-mime-function nil)
11053           (gnus-inhibit-treatment t))
11054       (gnus-summary-select-article))
11055     (save-excursion
11056       (set-buffer gnus-article-buffer)
11057       (let ((handles (or gnus-article-mime-handles
11058                          (mm-dissect-buffer nil gnus-article-loose-mime)
11059                          (and gnus-article-emulate-mime
11060                               (mm-uu-dissect)))))
11061         (when handles
11062           (gnus-summary-save-parts-1 type dir handles reverse)
11063           (unless gnus-article-mime-handles ;; Don't destroy this case.
11064             (mm-destroy-parts handles)))))))
11065
11066 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11067   (if (stringp (car handle))
11068       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11069               (cdr handle))
11070     (when (if reverse
11071               (not (string-match type (mm-handle-media-type handle)))
11072             (string-match type (mm-handle-media-type handle)))
11073       (let ((file (expand-file-name
11074                    (gnus-map-function
11075                     mm-file-name-rewrite-functions
11076                     (file-name-nondirectory
11077                      (or
11078                       (mail-content-type-get
11079                        (mm-handle-disposition handle) 'filename)
11080                       (mail-content-type-get
11081                        (mm-handle-type handle) 'name)
11082                       (concat gnus-newsgroup-name
11083                               "." (number-to-string
11084                                    (cdr gnus-article-current))))))
11085                    dir)))
11086         (unless (file-exists-p file)
11087           (mm-save-part-to-file handle file))))))
11088
11089 ;; Summary extract commands
11090
11091 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11092   (let ((buffer-read-only nil)
11093         (article (gnus-summary-article-number))
11094         after-article b e)
11095     (unless (gnus-summary-goto-subject article)
11096       (error "No such article: %d" article))
11097     (gnus-summary-position-point)
11098     ;; If all commands are to be bunched up on one line, we collect
11099     ;; them here.
11100     (unless gnus-view-pseudos-separately
11101       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11102             files action)
11103         (while ps
11104           (setq action (cdr (assq 'action (car ps))))
11105           (setq files (list (cdr (assq 'name (car ps)))))
11106           (while (and ps (cdr ps)
11107                       (string= (or action "1")
11108                                (or (cdr (assq 'action (cadr ps))) "2")))
11109             (push (cdr (assq 'name (cadr ps))) files)
11110             (setcdr ps (cddr ps)))
11111           (when files
11112             (when (not (string-match "%s" action))
11113               (push " " files))
11114             (push " " files)
11115             (when (assq 'execute (car ps))
11116               (setcdr (assq 'execute (car ps))
11117                       (funcall (if (string-match "%s" action)
11118                                    'format 'concat)
11119                                action
11120                                (mapconcat
11121                                 (lambda (f)
11122                                   (if (equal f " ")
11123                                       f
11124                                     (shell-quote-argument f)))
11125                                 files " ")))))
11126           (setq ps (cdr ps)))))
11127     (if (and gnus-view-pseudos (not not-view))
11128         (while pslist
11129           (when (assq 'execute (car pslist))
11130             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11131                                   (eq gnus-view-pseudos 'not-confirm)))
11132           (setq pslist (cdr pslist)))
11133       (save-excursion
11134         (while pslist
11135           (setq after-article (or (cdr (assq 'article (car pslist)))
11136                                   (gnus-summary-article-number)))
11137           (gnus-summary-goto-subject after-article)
11138           (forward-line 1)
11139           (setq b (point))
11140           (insert "    " (file-name-nondirectory
11141                           (cdr (assq 'name (car pslist))))
11142                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11143           (setq e (point))
11144           (forward-line -1)             ; back to `b'
11145           (gnus-add-text-properties
11146            b (1- e) (list 'gnus-number gnus-reffed-article-number
11147                           gnus-mouse-face-prop gnus-mouse-face))
11148           (gnus-data-enter
11149            after-article gnus-reffed-article-number
11150            gnus-unread-mark b (car pslist) 0 (- e b))
11151           (setq gnus-newsgroup-unreads
11152                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11153                                          gnus-reffed-article-number))
11154           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11155           (setq pslist (cdr pslist)))))))
11156
11157 (defun gnus-pseudos< (p1 p2)
11158   (let ((c1 (cdr (assq 'action p1)))
11159         (c2 (cdr (assq 'action p2))))
11160     (and c1 c2 (string< c1 c2))))
11161
11162 (defun gnus-request-pseudo-article (props)
11163   (cond ((assq 'execute props)
11164          (gnus-execute-command (cdr (assq 'execute props)))))
11165   (let ((gnus-current-article (gnus-summary-article-number)))
11166     (gnus-run-hooks 'gnus-mark-article-hook)))
11167
11168 (defun gnus-execute-command (command &optional automatic)
11169   (save-excursion
11170     (gnus-article-setup-buffer)
11171     (set-buffer gnus-article-buffer)
11172     (setq buffer-read-only nil)
11173     (let ((command (if automatic command
11174                      (read-string "Command: " (cons command 0)))))
11175       (erase-buffer)
11176       (insert "$ " command "\n\n")
11177       (if gnus-view-pseudo-asynchronously
11178           (start-process "gnus-execute" (current-buffer) shell-file-name
11179                          shell-command-switch command)
11180         (call-process shell-file-name nil t nil
11181                       shell-command-switch command)))))
11182
11183 ;; Summary kill commands.
11184
11185 (defun gnus-summary-edit-global-kill (article)
11186   "Edit the \"global\" kill file."
11187   (interactive (list (gnus-summary-article-number)))
11188   (gnus-group-edit-global-kill article))
11189
11190 (defun gnus-summary-edit-local-kill ()
11191   "Edit a local kill file applied to the current newsgroup."
11192   (interactive)
11193   (setq gnus-current-headers (gnus-summary-article-header))
11194   (gnus-group-edit-local-kill
11195    (gnus-summary-article-number) gnus-newsgroup-name))
11196
11197 ;;; Header reading.
11198
11199 (defun gnus-read-header (id &optional header)
11200   "Read the headers of article ID and enter them into the Gnus system."
11201   (let ((group gnus-newsgroup-name)
11202         (gnus-override-method
11203          (or
11204           gnus-override-method
11205           (and (gnus-news-group-p gnus-newsgroup-name)
11206                (car (gnus-refer-article-methods)))))
11207         where)
11208     ;; First we check to see whether the header in question is already
11209     ;; fetched.
11210     (if (stringp id)
11211         ;; This is a Message-ID.
11212         (setq header (or header (gnus-id-to-header id)))
11213       ;; This is an article number.
11214       (setq header (or header (gnus-summary-article-header id))))
11215     (if (and header
11216              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11217         ;; We have found the header.
11218         header
11219       ;; If this is a sparse article, we have to nix out its
11220       ;; previous entry in the thread hashtb.
11221       (when (and header
11222                  (gnus-summary-article-sparse-p (mail-header-number header)))
11223         (let* ((parent (gnus-parent-id (mail-header-references header)))
11224                (thread (and parent (gnus-id-to-thread parent))))
11225           (when thread
11226             (delq (assq header thread) thread))))
11227       ;; We have to really fetch the header to this article.
11228       (save-excursion
11229         (set-buffer nntp-server-buffer)
11230         (when (setq where (gnus-request-head id group))
11231           (nnheader-fold-continuation-lines)
11232           (goto-char (point-max))
11233           (insert ".\n")
11234           (goto-char (point-min))
11235           (insert "211 ")
11236           (princ (cond
11237                   ((numberp id) id)
11238                   ((cdr where) (cdr where))
11239                   (header (mail-header-number header))
11240                   (t gnus-reffed-article-number))
11241                  (current-buffer))
11242           (insert " Article retrieved.\n"))
11243         (if (or (not where)
11244                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11245             ()                          ; Malformed head.
11246           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11247             (when (and (stringp id)
11248                        (not (string= (gnus-group-real-name group)
11249                                      (car where))))
11250               ;; If we fetched by Message-ID and the article came
11251               ;; from a different group, we fudge some bogus article
11252               ;; numbers for this article.
11253               (mail-header-set-number header gnus-reffed-article-number))
11254             (save-excursion
11255               (set-buffer gnus-summary-buffer)
11256               (decf gnus-reffed-article-number)
11257               (gnus-remove-header (mail-header-number header))
11258               (push header gnus-newsgroup-headers)
11259               (setq gnus-current-headers header)
11260               (push (mail-header-number header) gnus-newsgroup-limit)))
11261           header)))))
11262
11263 (defun gnus-remove-header (number)
11264   "Remove header NUMBER from `gnus-newsgroup-headers'."
11265   (if (and gnus-newsgroup-headers
11266            (= number (mail-header-number (car gnus-newsgroup-headers))))
11267       (pop gnus-newsgroup-headers)
11268     (let ((headers gnus-newsgroup-headers))
11269       (while (and (cdr headers)
11270                   (not (= number (mail-header-number (cadr headers)))))
11271         (pop headers))
11272       (when (cdr headers)
11273         (setcdr headers (cddr headers))))))
11274
11275 ;;;
11276 ;;; summary highlights
11277 ;;;
11278
11279 (defun gnus-highlight-selected-summary ()
11280   "Highlight selected article in summary buffer."
11281   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11282   (when gnus-summary-selected-face
11283     (save-excursion
11284       (let* ((beg (point-at-bol))
11285              (end (point-at-eol))
11286              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11287              (from (if (get-text-property beg gnus-mouse-face-prop)
11288                        beg
11289                      (or (next-single-property-change
11290                           beg gnus-mouse-face-prop nil end)
11291                          beg)))
11292              (to
11293               (if (= from end)
11294                   (- from 2)
11295                 (or (next-single-property-change
11296                      from gnus-mouse-face-prop nil end)
11297                     end))))
11298         ;; If no mouse-face prop on line we will have to = from = end,
11299         ;; so we highlight the entire line instead.
11300         (when (= (+ to 2) from)
11301           (setq from beg)
11302           (setq to end))
11303         (if gnus-newsgroup-selected-overlay
11304             ;; Move old overlay.
11305             (gnus-move-overlay
11306              gnus-newsgroup-selected-overlay from to (current-buffer))
11307           ;; Create new overlay.
11308           (gnus-overlay-put
11309            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11310            'face gnus-summary-selected-face))))))
11311
11312 (defvar gnus-summary-highlight-line-cached nil)
11313 (defvar gnus-summary-highlight-line-trigger nil)
11314
11315 (defun gnus-summary-highlight-line-0 ()
11316   (if (and (eq gnus-summary-highlight-line-trigger
11317                gnus-summary-highlight)
11318            gnus-summary-highlight-line-cached)
11319       gnus-summary-highlight-line-cached
11320     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11321           gnus-summary-highlight-line-cached
11322           (let* ((cond (list 'cond))
11323                  (c cond)
11324                  (list gnus-summary-highlight))
11325             (while list
11326               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11327                               nil))
11328               (setq c (cdr c)
11329                     list (cdr list)))
11330             (gnus-byte-compile (list 'lambda nil cond))))))
11331
11332 (defun gnus-summary-highlight-line ()
11333   "Highlight current line according to `gnus-summary-highlight'."
11334   (let* ((beg (point-at-bol))
11335          (article (or (gnus-summary-article-number) gnus-current-article))
11336          (score (or (cdr (assq article
11337                                gnus-newsgroup-scored))
11338                     gnus-summary-default-score 0))
11339          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11340          (inhibit-read-only t)
11341          (default gnus-summary-default-score)
11342          (default-high gnus-summary-default-high-score)
11343          (default-low gnus-summary-default-low-score)
11344          (uncached (and gnus-summary-use-undownloaded-faces
11345                         (memq article gnus-newsgroup-undownloaded)
11346                         (not (memq article gnus-newsgroup-cached)))))
11347     (let ((face (funcall (gnus-summary-highlight-line-0))))
11348       (unless (eq face (get-text-property beg 'face))
11349         (gnus-put-text-property-excluding-characters-with-faces
11350          beg (point-at-eol) 'face
11351          (setq face (if (boundp face) (symbol-value face) face)))
11352         (when gnus-summary-highlight-line-function
11353           (funcall gnus-summary-highlight-line-function article face))))))
11354
11355 (defun gnus-update-read-articles (group unread &optional compute)
11356   "Update the list of read articles in GROUP.
11357 UNREAD is a sorted list."
11358   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11359         (info (gnus-get-info group))
11360         (prev 1)
11361         read)
11362     (if (or (not info) (not active))
11363         ;; There is no info on this group if it was, in fact,
11364         ;; killed.  Gnus stores no information on killed groups, so
11365         ;; there's nothing to be done.
11366         ;; One could store the information somewhere temporarily,
11367         ;; perhaps...  Hmmm...
11368         ()
11369       ;; Remove any negative articles numbers.
11370       (while (and unread (< (car unread) 0))
11371         (setq unread (cdr unread)))
11372       ;; Remove any expired article numbers
11373       (while (and unread (< (car unread) (car active)))
11374         (setq unread (cdr unread)))
11375       ;; Compute the ranges of read articles by looking at the list of
11376       ;; unread articles.
11377       (while unread
11378         (when (/= (car unread) prev)
11379           (push (if (= prev (1- (car unread))) prev
11380                   (cons prev (1- (car unread))))
11381                 read))
11382         (setq prev (1+ (car unread)))
11383         (setq unread (cdr unread)))
11384       (when (<= prev (cdr active))
11385         (push (cons prev (cdr active)) read))
11386       (setq read (if (> (length read) 1) (nreverse read) read))
11387       (if compute
11388           read
11389         (save-excursion
11390           (let (setmarkundo)
11391             ;; Propagate the read marks to the backend.
11392             (when (gnus-check-backend-function 'request-set-mark group)
11393               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11394                     (add (gnus-remove-from-range read (gnus-info-read info))))
11395                 (when (or add del)
11396                   (unless (gnus-check-group group)
11397                     (error "Can't open server for %s" group))
11398                   (gnus-request-set-mark
11399                    group (delq nil (list (if add (list add 'add '(read)))
11400                                          (if del (list del 'del '(read))))))
11401                   (setq setmarkundo
11402                         `(gnus-request-set-mark
11403                           ,group
11404                           ',(delq nil (list
11405                                        (if del (list del 'add '(read)))
11406                                        (if add (list add 'del '(read))))))))))
11407             (set-buffer gnus-group-buffer)
11408             (gnus-undo-register
11409               `(progn
11410                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11411                  (gnus-info-set-read ',info ',(gnus-info-read info))
11412                  (gnus-get-unread-articles-in-group ',info
11413                                                     (gnus-active ,group))
11414                  (gnus-group-update-group ,group t)
11415                  ,setmarkundo))))
11416         ;; Enter this list into the group info.
11417         (gnus-info-set-read info read)
11418         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11419         (gnus-get-unread-articles-in-group info (gnus-active group))
11420         t))))
11421
11422 (defun gnus-offer-save-summaries ()
11423   "Offer to save all active summary buffers."
11424   (let (buffers)
11425     ;; Go through all buffers and find all summaries.
11426     (dolist (buffer (buffer-list))
11427       (when (and (setq buffer (buffer-name buffer))
11428                  (string-match "Summary" buffer)
11429                  (save-excursion
11430                    (set-buffer buffer)
11431                    ;; We check that this is, indeed, a summary buffer.
11432                    (and (eq major-mode 'gnus-summary-mode)
11433                         ;; Also make sure this isn't bogus.
11434                         gnus-newsgroup-prepared
11435                         ;; Also make sure that this isn't a
11436                         ;; dead summary buffer.
11437                         (not gnus-dead-summary-mode))))
11438         (push buffer buffers)))
11439     ;; Go through all these summary buffers and offer to save them.
11440     (when buffers
11441       (save-excursion
11442         (map-y-or-n-p
11443          "Update summary buffer %s? "
11444          (lambda (buf)
11445            (switch-to-buffer buf)
11446            (gnus-summary-exit))
11447          buffers)))))
11448
11449 (defun gnus-summary-setup-default-charset ()
11450   "Setup newsgroup default charset."
11451   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11452       (setq gnus-newsgroup-charset nil)
11453     (let* ((ignored-charsets
11454             (or gnus-newsgroup-ephemeral-ignored-charsets
11455                 (append
11456                  (and gnus-newsgroup-name
11457                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11458                  gnus-newsgroup-ignored-charsets))))
11459       (setq gnus-newsgroup-charset
11460             (or gnus-newsgroup-ephemeral-charset
11461                 (and gnus-newsgroup-name
11462                      (gnus-parameter-charset gnus-newsgroup-name))
11463                 gnus-default-charset))
11464       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11465            ignored-charsets))))
11466
11467 ;;;
11468 ;;; Mime Commands
11469 ;;;
11470
11471 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11472   "Display the current article buffer fully MIME-buttonized.
11473 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11474 treated as multipart/mixed."
11475   (interactive "P")
11476   (require 'gnus-art)
11477   (let ((gnus-unbuttonized-mime-types nil)
11478         (gnus-mime-display-multipart-as-mixed show-all-parts))
11479     (gnus-summary-show-article)))
11480
11481 (defun gnus-summary-repair-multipart (article)
11482   "Add a Content-Type header to a multipart article without one."
11483   (interactive (list (gnus-summary-article-number)))
11484   (gnus-with-article article
11485     (message-narrow-to-head)
11486     (message-remove-header "Mime-Version")
11487     (goto-char (point-max))
11488     (insert "Mime-Version: 1.0\n")
11489     (widen)
11490     (when (search-forward "\n--" nil t)
11491       (let ((separator (buffer-substring (point) (point-at-eol))))
11492         (message-narrow-to-head)
11493         (message-remove-header "Content-Type")
11494         (goto-char (point-max))
11495         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11496                         separator))
11497         (widen))))
11498   (let (gnus-mark-article-hook)
11499     (gnus-summary-select-article t t nil article)))
11500
11501 (defun gnus-summary-toggle-display-buttonized ()
11502   "Toggle the buttonizing of the article buffer."
11503   (interactive)
11504   (require 'gnus-art)
11505   (if (setq gnus-inhibit-mime-unbuttonizing
11506             (not gnus-inhibit-mime-unbuttonizing))
11507       (let ((gnus-unbuttonized-mime-types nil))
11508         (gnus-summary-show-article))
11509     (gnus-summary-show-article)))
11510
11511 ;;;
11512 ;;; Generic summary marking commands
11513 ;;;
11514
11515 (defvar gnus-summary-marking-alist
11516   '((read gnus-del-mark "d")
11517     (unread gnus-unread-mark "u")
11518     (ticked gnus-ticked-mark "!")
11519     (dormant gnus-dormant-mark "?")
11520     (expirable gnus-expirable-mark "e"))
11521   "An alist of names/marks/keystrokes.")
11522
11523 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11524 (defvar gnus-summary-mark-map)
11525
11526 (defun gnus-summary-make-all-marking-commands ()
11527   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11528   (dolist (elem gnus-summary-marking-alist)
11529     (apply 'gnus-summary-make-marking-command elem)))
11530
11531 (defun gnus-summary-make-marking-command (name mark keystroke)
11532   (let ((map (make-sparse-keymap)))
11533     (define-key gnus-summary-generic-mark-map keystroke map)
11534     (dolist (lway `((next "next" next nil "n")
11535                     (next-unread "next unread" next t "N")
11536                     (prev "previous" prev nil "p")
11537                     (prev-unread "previous unread" prev t "P")
11538                     (nomove "" nil nil ,keystroke)))
11539       (let ((func (gnus-summary-make-marking-command-1
11540                    mark (car lway) lway name)))
11541         (setq func (eval func))
11542         (define-key map (nth 4 lway) func)))))
11543
11544 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11545   `(defun ,(intern
11546             (format "gnus-summary-put-mark-as-%s%s"
11547                     name (if (eq way 'nomove)
11548                              ""
11549                            (concat "-" (symbol-name way)))))
11550      (n)
11551      ,(format
11552        "Mark the current article as %s%s.
11553 If N, the prefix, then repeat N times.
11554 If N is negative, move in reverse order.
11555 The difference between N and the actual number of articles marked is
11556 returned."
11557        name (cadr lway))
11558      (interactive "p")
11559      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11560
11561 (defun gnus-summary-generic-mark (n mark move unread)
11562   "Mark N articles with MARK."
11563   (unless (eq major-mode 'gnus-summary-mode)
11564     (error "This command can only be used in the summary buffer"))
11565   (gnus-summary-show-thread)
11566   (let ((nummove
11567          (cond
11568           ((eq move 'next) 1)
11569           ((eq move 'prev) -1)
11570           (t 0))))
11571     (if (zerop nummove)
11572         (setq n 1)
11573       (when (< n 0)
11574         (setq n (abs n)
11575               nummove (* -1 nummove))))
11576     (while (and (> n 0)
11577                 (gnus-summary-mark-article nil mark)
11578                 (zerop (gnus-summary-next-subject nummove unread t)))
11579       (setq n (1- n)))
11580     (when (/= 0 n)
11581       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11582     (gnus-summary-recenter)
11583     (gnus-summary-position-point)
11584     (gnus-set-mode-line 'summary)
11585     n))
11586
11587 (defun gnus-summary-insert-articles (articles)
11588   (when (setq articles
11589               (gnus-sorted-difference articles
11590                                       (mapcar (lambda (h)
11591                                                 (mail-header-number h))
11592                                               gnus-newsgroup-headers)))
11593     (setq gnus-newsgroup-headers
11594           (gnus-merge 'list
11595                       gnus-newsgroup-headers
11596                       (gnus-fetch-headers articles)
11597                       'gnus-article-sort-by-number))
11598     ;; Suppress duplicates?
11599     (when gnus-suppress-duplicates
11600       (gnus-dup-suppress-articles))
11601
11602     ;; We might want to build some more threads first.
11603     (when (and gnus-fetch-old-headers
11604                (eq gnus-headers-retrieved-by 'nov))
11605       (if (eq gnus-fetch-old-headers 'invisible)
11606           (gnus-build-all-threads)
11607         (gnus-build-old-threads)))
11608     ;; Let the Gnus agent mark articles as read.
11609     (when gnus-agent
11610       (gnus-agent-get-undownloaded-list))
11611     ;; Remove list identifiers from subject
11612     (when gnus-list-identifiers
11613       (gnus-summary-remove-list-identifiers))
11614     ;; First and last article in this newsgroup.
11615     (when gnus-newsgroup-headers
11616       (setq gnus-newsgroup-begin
11617             (mail-header-number (car gnus-newsgroup-headers))
11618             gnus-newsgroup-end
11619             (mail-header-number
11620              (gnus-last-element gnus-newsgroup-headers))))
11621     (when gnus-use-scoring
11622       (gnus-possibly-score-headers))))
11623
11624 (defun gnus-summary-insert-old-articles (&optional all)
11625   "Insert all old articles in this group.
11626 If ALL is non-nil, already read articles become readable.
11627 If ALL is a number, fetch this number of articles."
11628   (interactive "P")
11629   (prog1
11630       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11631             older len)
11632         (setq older
11633               ;; Some nntp servers lie about their active range.  When
11634               ;; this happens, the active range can be in the millions.
11635               ;; Use a compressed range to avoid creating a huge list.
11636               (gnus-range-difference (list gnus-newsgroup-active) old))
11637         (setq len (gnus-range-length older))
11638         (cond
11639          ((null older) nil)
11640          ((numberp all)
11641           (if (< all len)
11642               (let ((older-range (nreverse older)))
11643                 (setq older nil)
11644
11645                 (while (> all 0)
11646                   (let* ((r (pop older-range))
11647                          (min (if (numberp r) r (car r)))
11648                          (max (if (numberp r) r (cdr r))))
11649                     (while (and (<= min max)
11650                                 (> all 0))
11651                       (push max older)
11652                       (setq all (1- all)
11653                             max (1- max))))))
11654             (setq older (gnus-uncompress-range older))))
11655          (all
11656           (setq older (gnus-uncompress-range older)))
11657          (t
11658           (when (and (numberp gnus-large-newsgroup)
11659                    (> len gnus-large-newsgroup))
11660               (let* ((cursor-in-echo-area nil)
11661                      (initial (gnus-parameter-large-newsgroup-initial
11662                                gnus-newsgroup-name))
11663                      (input
11664                       (read-string
11665                        (format
11666                         "How many articles from %s (%s %d): "
11667                         (gnus-limit-string
11668                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11669                         (if initial "max" "default")
11670                         len)
11671                        (if initial
11672                            (cons (number-to-string initial)
11673                                  0)))))
11674                 (unless (string-match "^[ \t]*$" input)
11675                   (setq all (string-to-number input))
11676                   (if (< all len)
11677                       (let ((older-range (nreverse older)))
11678                         (setq older nil)
11679
11680                         (while (> all 0)
11681                           (let* ((r (pop older-range))
11682                                  (min (if (numberp r) r (car r)))
11683                                  (max (if (numberp r) r (cdr r))))
11684                             (while (and (<= min max)
11685                                         (> all 0))
11686                               (push max older)
11687                               (setq all (1- all)
11688                                     max (1- max))))))))))
11689           (setq older (gnus-uncompress-range older))))
11690         (if (not older)
11691             (message "No old news.")
11692           (gnus-summary-insert-articles older)
11693           (gnus-summary-limit (gnus-sorted-nunion old older))))
11694     (gnus-summary-position-point)))
11695
11696 (defun gnus-summary-insert-new-articles ()
11697   "Insert all new articles in this group."
11698   (interactive)
11699   (prog1
11700       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11701             (old-active gnus-newsgroup-active)
11702             (nnmail-fetched-sources (list t))
11703             i new)
11704         (setq gnus-newsgroup-active
11705               (gnus-activate-group gnus-newsgroup-name 'scan))
11706         (setq i (cdr gnus-newsgroup-active))
11707         (while (> i (cdr old-active))
11708           (push i new)
11709           (decf i))
11710         (if (not new)
11711             (message "No gnus is bad news")
11712           (gnus-summary-insert-articles new)
11713           (setq gnus-newsgroup-unreads
11714                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11715           (gnus-summary-limit (gnus-sorted-nunion old new))))
11716     (gnus-summary-position-point)))
11717
11718 (gnus-summary-make-all-marking-commands)
11719
11720 (gnus-ems-redefine)
11721
11722 (provide 'gnus-sum)
11723
11724 (run-hooks 'gnus-sum-load-hook)
11725
11726 ;; Local Variables:
11727 ;; coding: iso-8859-1
11728 ;; End:
11729
11730 ;;; gnus-sum.el ends here