(gnus-get-newsgroup-headers): Unfold headers.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
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 'mm-uu-dissect "mm-uu")
48 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
49   "Deuglify broken Outlook (Express) articles and redisplay."
50   t)
51 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
52 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
53 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
54
55 (defcustom gnus-kill-summary-on-exit t
56   "*If non-nil, kill the summary buffer when you exit from it.
57 If nil, the summary will become a \"*Dead Summary*\" buffer, and
58 it will be killed sometime later."
59   :group 'gnus-summary-exit
60   :type 'boolean)
61
62 (defcustom gnus-fetch-old-headers nil
63   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
64 If an unread article in the group refers to an older, already read (or
65 just marked as read) article, the old article will not normally be
66 displayed in the Summary buffer.  If this variable is non-nil, Gnus
67 will attempt to grab the headers to the old articles, and thereby
68 build complete threads.  If it has the value `some', only enough
69 headers to connect otherwise loose threads will be displayed.  This
70 variable can also be a number.  In that case, no more than that number
71 of old headers will be fetched.  If it has the value `invisible', all
72 old headers will be fetched, but none will be displayed.
73
74 The server has to support NOV for any of this to work."
75   :group 'gnus-thread
76   :type '(choice (const :tag "off" nil)
77                  (const some)
78                  number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-refer-thread-limit 200
82   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
83 If t, fetch all the available old headers."
84   :group 'gnus-thread
85   :type '(choice number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-summary-make-false-root 'adopt
89   "*nil means that Gnus won't gather loose threads.
90 If the root of a thread has expired or been read in a previous
91 session, the information necessary to build a complete thread has been
92 lost.  Instead of having many small sub-threads from this original thread
93 scattered all over the summary buffer, Gnus can gather them.
94
95 If non-nil, Gnus will try to gather all loose sub-threads from an
96 original thread into one large thread.
97
98 If this variable is non-nil, it should be one of `none', `adopt',
99 `dummy' or `empty'.
100
101 If this variable is `none', Gnus will not make a false root, but just
102 present the sub-threads after another.
103 If this variable is `dummy', Gnus will create a dummy root that will
104 have all the sub-threads as children.
105 If this variable is `adopt', Gnus will make one of the \"children\"
106 the parent and mark all the step-children as such.
107 If this variable is `empty', the \"children\" are printed with empty
108 subject fields.  (Or rather, they will be printed with a string
109 given by the `gnus-summary-same-subject' variable.)"
110   :group 'gnus-thread
111   :type '(choice (const :tag "off" nil)
112                  (const none)
113                  (const dummy)
114                  (const adopt)
115                  (const empty)))
116
117 (defcustom gnus-summary-make-false-root-always nil
118   "Always make a false dummy root."
119   :group 'gnus-thread
120   :type 'boolean)
121
122 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
123   "*A regexp to match subjects to be excluded from loose thread gathering.
124 As loose thread gathering is done on subjects only, that means that
125 there can be many false gatherings performed.  By rooting out certain
126 common subjects, gathering might become saner."
127   :group 'gnus-thread
128   :type 'regexp)
129
130 (defcustom gnus-summary-gather-subject-limit nil
131   "*Maximum length of subject comparisons when gathering loose threads.
132 Use nil to compare full subjects.  Setting this variable to a low
133 number will help gather threads that have been corrupted by
134 newsreaders chopping off subject lines, but it might also mean that
135 unrelated articles that have subject that happen to begin with the
136 same few characters will be incorrectly gathered.
137
138 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
139 comparing subjects."
140   :group 'gnus-thread
141   :type '(choice (const :tag "off" nil)
142                  (const fuzzy)
143                  (sexp :menu-tag "on" t)))
144
145 (defcustom gnus-simplify-subject-functions nil
146   "List of functions taking a string argument that simplify subjects.
147 The functions are applied recursively.
148
149 Useful functions to put in this list include:
150 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
151 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 \(they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-default-high-score 0
216   "*Default threshold for a high scored article.
217 An article will be highlighted as high scored if its score is greater
218 than this score."
219   :group 'gnus-score-default
220   :type 'integer)
221
222 (defcustom gnus-summary-default-low-score 0
223   "*Default threshold for a low scored article.
224 An article will be highlighted as low scored if its score is smaller
225 than this score."
226   :group 'gnus-score-default
227   :type 'integer)
228
229 (defcustom gnus-summary-zcore-fuzz 0
230   "*Fuzziness factor for the zcore in the summary buffer.
231 Articles with scores closer than this to `gnus-summary-default-score'
232 will not be marked."
233   :group 'gnus-summary-format
234   :type 'integer)
235
236 (defcustom gnus-simplify-subject-fuzzy-regexp nil
237   "*Strings to be removed when doing fuzzy matches.
238 This can either be a regular expression or list of regular expressions
239 that will be removed from subject strings if fuzzy subject
240 simplification is selected."
241   :group 'gnus-thread
242   :type '(repeat regexp))
243
244 (defcustom gnus-show-threads t
245   "*If non-nil, display threads in summary mode."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-hide-subtree nil
250   "*If non-nil, hide all threads initially.
251 This can be a predicate specifier which says which threads to hide.
252 If threads are hidden, you have to run the command
253 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
254 to expose hidden threads."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-hide-killed t
259   "*If non-nil, hide killed threads automatically."
260   :group 'gnus-thread
261   :type 'boolean)
262
263 (defcustom gnus-thread-ignore-subject t
264   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
265 If nil, articles that have different subjects from their parents will
266 start separate threads."
267   :group 'gnus-thread
268   :type 'boolean)
269
270 (defcustom gnus-thread-operation-ignore-subject t
271   "*If non-nil, subjects will be ignored when doing thread commands.
272 This affects commands like `gnus-summary-kill-thread' and
273 `gnus-summary-lower-thread'.
274
275 If this variable is nil, articles in the same thread with different
276 subjects will not be included in the operation in question.  If this
277 variable is `fuzzy', only articles that have subjects that are fuzzily
278 equal will be included."
279   :group 'gnus-thread
280   :type '(choice (const :tag "off" nil)
281                  (const fuzzy)
282                  (sexp :tag "on" t)))
283
284 (defcustom gnus-thread-indent-level 4
285   "*Number that says how much each sub-thread should be indented."
286   :group 'gnus-thread
287   :type 'integer)
288
289 (defcustom gnus-auto-extend-newsgroup t
290   "*If non-nil, extend newsgroup forward and backward when requested."
291   :group 'gnus-summary-choose
292   :type 'boolean)
293
294 (defcustom gnus-auto-select-first t
295   "*If non-nil, select the article under point.
296 Which article this is is controlled by the `gnus-auto-select-subject'
297 variable.
298
299 If you want to prevent automatic selection of articles in some
300 newsgroups, set the variable to nil in `gnus-select-group-hook'."
301   :group 'gnus-group-select
302   :type '(choice (const :tag "none" nil)
303                  (sexp :menu-tag "first" t)))
304
305 (defcustom gnus-auto-select-subject 'unread
306   "*Says what subject to place under point when entering a group.
307
308 This variable can either be the symbols `first' (place point on the
309 first subject), `unread' (place point on the subject line of the first
310 unread article), `best' (place point on the subject line of the
311 higest-scored article), `unseen' (place point on the subject line of
312 the first unseen article), 'unseen-or-unread' (place point on the subject
313 line of the first unseen article or, if all article have been seen, on the
314 subject line of the first unread article), or a function to be called to
315 place point on some subject line."
316   :group 'gnus-group-select
317   :type '(choice (const best)
318                  (const unread)
319                  (const first)
320                  (const unseen)
321                  (const unseen-or-unread)))
322
323 (defcustom gnus-auto-select-next t
324   "*If non-nil, offer to go to the next group from the end of the previous.
325 If the value is t and the next newsgroup is empty, Gnus will exit
326 summary mode and go back to group mode.  If the value is neither nil
327 nor t, Gnus will select the following unread newsgroup.  In
328 particular, if the value is the symbol `quietly', the next unread
329 newsgroup will be selected without any confirmation, and if it is
330 `almost-quietly', the next group will be selected without any
331 confirmation if you are located on the last article in the group.
332 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
333 will go to the next group without confirmation."
334   :group 'gnus-summary-maneuvering
335   :type '(choice (const :tag "off" nil)
336                  (const quietly)
337                  (const almost-quietly)
338                  (const slightly-quietly)
339                  (sexp :menu-tag "on" t)))
340
341 (defcustom gnus-auto-select-same nil
342   "*If non-nil, select the next article with the same subject.
343 If there are no more articles with the same subject, go to
344 the first unread article."
345   :group 'gnus-summary-maneuvering
346   :type 'boolean)
347
348 (defcustom gnus-auto-goto-ignores 'unfetched
349   "*Says how to handle unfetched articles when maneuvering.
350
351 This variable can either be the symbols nil (maneuver to any
352 article), `undownloaded' (maneuvering while unplugged ignores articles
353 that have not been fetched), `always-undownloaded' (maneuvering always
354 ignores articles that have not been fetched), `unfetched' (maneuvering
355 ignores articles whose headers have not been fetched).
356
357 NOTE: The list of unfetched articles will always be nil when plugged
358 and, when unplugged, a subset of the undownloaded article list."
359   :group 'gnus-summary-maneuvering
360   :type '(choice (const :tag "None" nil)
361                  (const :tag "Undownloaded when unplugged" undownloaded)
362                  (const :tag "Undownloaded" always-undownloaded)
363                  (const :tag "Unfetched" unfetched)))
364
365 (defcustom gnus-summary-check-current nil
366   "*If non-nil, consider the current article when moving.
367 The \"unread\" movement commands will stay on the same line if the
368 current article is unread."
369   :group 'gnus-summary-maneuvering
370   :type 'boolean)
371
372 (defcustom gnus-auto-center-summary t
373   "*If non-nil, always center the current summary buffer.
374 In particular, if `vertical' do only vertical recentering.  If non-nil
375 and non-`vertical', do both horizontal and vertical recentering."
376   :group 'gnus-summary-maneuvering
377   :type '(choice (const :tag "none" nil)
378                  (const vertical)
379                  (integer :tag "height")
380                  (sexp :menu-tag "both" t)))
381
382 (defvar gnus-auto-center-group t
383   "*If non-nil, always center the group buffer.")
384
385 (defcustom gnus-show-all-headers nil
386   "*If non-nil, don't hide any headers."
387   :group 'gnus-article-hiding
388   :group 'gnus-article-headers
389   :type 'boolean)
390
391 (defcustom gnus-summary-ignore-duplicates nil
392   "*If non-nil, ignore articles with identical Message-ID headers."
393   :group 'gnus-summary
394   :type 'boolean)
395
396 (defcustom gnus-single-article-buffer t
397   "*If non-nil, display all articles in the same buffer.
398 If nil, each group will get its own article buffer."
399   :group 'gnus-article-various
400   :type 'boolean)
401
402 (defcustom gnus-break-pages t
403   "*If non-nil, do page breaking on articles.
404 The page delimiter is specified by the `gnus-page-delimiter'
405 variable."
406   :group 'gnus-article-various
407   :type 'boolean)
408
409 (defcustom gnus-move-split-methods nil
410   "*Variable used to suggest where articles are to be moved to.
411 It uses the same syntax as the `gnus-split-methods' variable.
412 However, whereas `gnus-split-methods' specifies file names as targets,
413 this variable specifies group names."
414   :group 'gnus-summary-mail
415   :type '(repeat (choice (list :value (fun) function)
416                          (cons :value ("" "") regexp (repeat string))
417                          (sexp :value nil))))
418
419 (defcustom gnus-unread-mark ?           ;Whitespace
420   "*Mark used for unread articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-ticked-mark ?!
425   "*Mark used for ticked articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-dormant-mark ??
430   "*Mark used for dormant articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-del-mark ?r
435   "*Mark used for del'd articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-read-mark ?R
440   "*Mark used for read articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-expirable-mark ?E
445   "*Mark used for expirable articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-killed-mark ?K
450   "*Mark used for killed articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-spam-mark ?$
455   "*Mark used for spam articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-souped-mark ?F
460   "*Mark used for souped articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-kill-file-mark ?X
465   "*Mark used for articles killed by kill files."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-low-score-mark ?Y
470   "*Mark used for articles with a low score."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-catchup-mark ?C
475   "*Mark used for articles that are caught up."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-replied-mark ?A
480   "*Mark used for articles that have been replied to."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-forwarded-mark ?F
485   "*Mark used for articles that have been forwarded."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-recent-mark ?N
490   "*Mark used for articles that are recent."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-cached-mark ?*
495   "*Mark used for articles that are in the cache."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-saved-mark ?S
500   "*Mark used for articles that have been saved."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-unseen-mark ?.
505   "*Mark used for articles that haven't been seen."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-no-mark ?               ;Whitespace
510   "*Mark used for articles that have no other secondary mark."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-ancient-mark ?O
515   "*Mark used for ancient articles."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-sparse-mark ?Q
520   "*Mark used for sparsely reffed articles."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-canceled-mark ?G
525   "*Mark used for canceled articles."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-duplicate-mark ?M
530   "*Mark used for duplicate articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-undownloaded-mark ?-
535   "*Mark used for articles that weren't downloaded."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-downloaded-mark ?+
540   "*Mark used for articles that were downloaded."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-downloadable-mark ?%
545   "*Mark used for articles that are to be downloaded."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-unsendable-mark ?=
550   "*Mark used for articles that won't be sent."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-score-over-mark ?+
555   "*Score mark used for articles with high scores."
556   :group 'gnus-summary-marks
557   :type 'character)
558
559 (defcustom gnus-score-below-mark ?-
560   "*Score mark used for articles with low scores."
561   :group 'gnus-summary-marks
562   :type 'character)
563
564 (defcustom gnus-empty-thread-mark ?     ;Whitespace
565   "*There is no thread under the article."
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-not-empty-thread-mark ?=
570   "*There is a thread under the article."
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-view-pseudo-asynchronously nil
575   "*If non-nil, Gnus will view pseudo-articles asynchronously."
576   :group 'gnus-extract-view
577   :type 'boolean)
578
579 (defcustom gnus-auto-expirable-marks
580   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
581         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
582         gnus-souped-mark gnus-duplicate-mark)
583   "*The list of marks converted into expiration if a group is auto-expirable."
584   :version "21.1"
585   :group 'gnus-summary
586   :type '(repeat character))
587
588 (defcustom gnus-inhibit-user-auto-expire t
589   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
590   :version "21.1"
591   :group 'gnus-summary
592   :type 'boolean)
593
594 (defcustom gnus-view-pseudos nil
595   "*If `automatic', pseudo-articles will be viewed automatically.
596 If `not-confirm', pseudos will be viewed automatically, and the user
597 will not be asked to confirm the command."
598   :group 'gnus-extract-view
599   :type '(choice (const :tag "off" nil)
600                  (const automatic)
601                  (const not-confirm)))
602
603 (defcustom gnus-view-pseudos-separately t
604   "*If non-nil, one pseudo-article will be created for each file to be viewed.
605 If nil, all files that use the same viewing command will be given as a
606 list of parameters to that command."
607   :group 'gnus-extract-view
608   :type 'boolean)
609
610 (defcustom gnus-insert-pseudo-articles t
611   "*If non-nil, insert pseudo-articles when decoding articles."
612   :group 'gnus-extract-view
613   :type 'boolean)
614
615 (defcustom gnus-summary-dummy-line-format
616   "   %(:                             :%) %S\n"
617   "*The format specification for the dummy roots in the summary buffer.
618 It works along the same lines as a normal formatting string,
619 with some simple extensions.
620
621 %S  The subject
622
623 General format specifiers can also be used.
624 See `(gnus)Formatting Variables'."
625   :link '(custom-manual "(gnus)Formatting Variables")
626   :group 'gnus-threading
627   :type 'string)
628
629 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
630   "*The format specification for the summary mode line.
631 It works along the same lines as a normal formatting string,
632 with some simple extensions:
633
634 %G  Group name
635 %p  Unprefixed group name
636 %A  Current article number
637 %z  Current article score
638 %V  Gnus version
639 %U  Number of unread articles in the group
640 %e  Number of unselected articles in the group
641 %Z  A string with unread/unselected article counts
642 %g  Shortish group name
643 %S  Subject of the current article
644 %u  User-defined spec
645 %s  Current score file name
646 %d  Number of dormant articles
647 %r  Number of articles that have been marked as read in this session
648 %E  Number of articles expunged by the score files"
649   :group 'gnus-summary-format
650   :type 'string)
651
652 (defcustom gnus-list-identifiers nil
653   "Regexp that matches list identifiers to be removed from subject.
654 This can also be a list of regexps."
655   :version "21.1"
656   :group 'gnus-summary-format
657   :group 'gnus-article-hiding
658   :type '(choice (const :tag "none" nil)
659                  (regexp :value ".*")
660                  (repeat :value (".*") regexp)))
661
662 (defcustom gnus-summary-mark-below 0
663   "*Mark all articles with a score below this variable as read.
664 This variable is local to each summary buffer and usually set by the
665 score file."
666   :group 'gnus-score-default
667   :type 'integer)
668
669 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
670   "*List of functions used for sorting articles in the summary buffer.
671
672 Each function takes two articles and returns non-nil if the first
673 article should be sorted before the other.  If you use more than one
674 function, the primary sort function should be the last.  You should
675 probably always include `gnus-article-sort-by-number' in the list of
676 sorting functions -- preferably first.  Also note that sorting by date
677 is often much slower than sorting by number, and the sorting order is
678 very similar.  (Sorting by date means sorting by the time the message
679 was sent, sorting by number means sorting by arrival time.)
680
681 Ready-made functions include `gnus-article-sort-by-number',
682 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
683 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
684 and `gnus-article-sort-by-score'.
685
686 When threading is turned on, the variable `gnus-thread-sort-functions'
687 controls how articles are sorted."
688   :group 'gnus-summary-sort
689   :type '(repeat (choice (function-item gnus-article-sort-by-number)
690                          (function-item gnus-article-sort-by-author)
691                          (function-item gnus-article-sort-by-subject)
692                          (function-item gnus-article-sort-by-date)
693                          (function-item gnus-article-sort-by-score)
694                          (function-item gnus-article-sort-by-random)
695                          (function :tag "other"))))
696
697 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
698   "*List of functions used for sorting threads in the summary buffer.
699 By default, threads are sorted by article number.
700
701 Each function takes two threads and returns non-nil if the first
702 thread should be sorted before the other.  If you use more than one
703 function, the primary sort function should be the last.  You should
704 probably always include `gnus-thread-sort-by-number' in the list of
705 sorting functions -- preferably first.  Also note that sorting by date
706 is often much slower than sorting by number, and the sorting order is
707 very similar.  (Sorting by date means sorting by the time the message
708 was sent, sorting by number means sorting by arrival time.)
709
710 Ready-made functions include `gnus-thread-sort-by-number',
711 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
712 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
713 `gnus-thread-sort-by-most-recent-number',
714 `gnus-thread-sort-by-most-recent-date',
715 `gnus-thread-sort-by-random', and
716 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
717
718 When threading is turned off, the variable
719 `gnus-article-sort-functions' controls how articles are sorted."
720   :group 'gnus-summary-sort
721   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
722                          (function-item gnus-thread-sort-by-author)
723                          (function-item gnus-thread-sort-by-subject)
724                          (function-item gnus-thread-sort-by-date)
725                          (function-item gnus-thread-sort-by-score)
726                          (function-item gnus-thread-sort-by-total-score)
727                          (function-item gnus-thread-sort-by-random)
728                          (function :tag "other"))))
729
730 (defcustom gnus-thread-score-function '+
731   "*Function used for calculating the total score of a thread.
732
733 The function is called with the scores of the article and each
734 subthread and should then return the score of the thread.
735
736 Some functions you can use are `+', `max', or `min'."
737   :group 'gnus-summary-sort
738   :type 'function)
739
740 (defcustom gnus-summary-expunge-below nil
741   "All articles that have a score less than this variable will be expunged.
742 This variable is local to the summary buffers."
743   :group 'gnus-score-default
744   :type '(choice (const :tag "off" nil)
745                  integer))
746
747 (defcustom gnus-thread-expunge-below nil
748   "All threads that have a total score less than this variable will be expunged.
749 See `gnus-thread-score-function' for en explanation of what a
750 \"thread score\" is.
751
752 This variable is local to the summary buffers."
753   :group 'gnus-threading
754   :group 'gnus-score-default
755   :type '(choice (const :tag "off" nil)
756                  integer))
757
758 (defcustom gnus-summary-mode-hook nil
759   "*A hook for Gnus summary mode.
760 This hook is run before any variables are set in the summary buffer."
761   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
762   :group 'gnus-summary-various
763   :type 'hook)
764
765 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
766 (when (featurep 'xemacs)
767   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
768   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
769   (add-hook 'gnus-summary-mode-hook
770             'gnus-xmas-switch-horizontal-scrollbar-off))
771
772 (defcustom gnus-summary-menu-hook nil
773   "*Hook run after the creation of the summary mode menu."
774   :group 'gnus-summary-visual
775   :type 'hook)
776
777 (defcustom gnus-summary-exit-hook nil
778   "*A hook called on exit from the summary buffer.
779 It will be called with point in the group buffer."
780   :group 'gnus-summary-exit
781   :type 'hook)
782
783 (defcustom gnus-summary-prepare-hook nil
784   "*A hook called after the summary buffer has been generated.
785 If you want to modify the summary buffer, you can use this hook."
786   :group 'gnus-summary-various
787   :type 'hook)
788
789 (defcustom gnus-summary-prepared-hook nil
790   "*A hook called as the last thing after the summary buffer has been generated."
791   :group 'gnus-summary-various
792   :type 'hook)
793
794 (defcustom gnus-summary-generate-hook nil
795   "*A hook run just before generating the summary buffer.
796 This hook is commonly used to customize threading variables and the
797 like."
798   :group 'gnus-summary-various
799   :type 'hook)
800
801 (defcustom gnus-select-group-hook nil
802   "*A hook called when a newsgroup is selected.
803
804 If you'd like to simplify subjects like the
805 `gnus-summary-next-same-subject' command does, you can use the
806 following hook:
807
808  (add-hook gnus-select-group-hook
809            (lambda ()
810              (mapcar (lambda (header)
811                        (mail-header-set-subject
812                         header
813                         (gnus-simplify-subject
814                          (mail-header-subject header) 're-only)))
815                      gnus-newsgroup-headers)))"
816   :group 'gnus-group-select
817   :type 'hook)
818
819 (defcustom gnus-select-article-hook nil
820   "*A hook called when an article is selected."
821   :group 'gnus-summary-choose
822   :options '(gnus-agent-fetch-selected-article)
823   :type 'hook)
824
825 (defcustom gnus-visual-mark-article-hook
826   (list 'gnus-highlight-selected-summary)
827   "*Hook run after selecting an article in the summary buffer.
828 It is meant to be used for highlighting the article in some way.  It
829 is not run if `gnus-visual' is nil."
830   :group 'gnus-summary-visual
831   :type 'hook)
832
833 (defcustom gnus-parse-headers-hook nil
834   "*A hook called before parsing the headers."
835   :group 'gnus-various
836   :type 'hook)
837
838 (defcustom gnus-exit-group-hook nil
839   "*A hook called when exiting summary mode.
840 This hook is not called from the non-updating exit commands like `Q'."
841   :group 'gnus-various
842   :type 'hook)
843
844 (defcustom gnus-summary-update-hook
845   (list 'gnus-summary-highlight-line)
846   "*A hook called when a summary line is changed.
847 The hook will not be called if `gnus-visual' is nil.
848
849 The default function `gnus-summary-highlight-line' will
850 highlight the line according to the `gnus-summary-highlight'
851 variable."
852   :group 'gnus-summary-visual
853   :type 'hook)
854
855 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
856   "*A hook called when an article is selected for the first time.
857 The hook is intended to mark an article as read (or unread)
858 automatically when it is selected."
859   :group 'gnus-summary-choose
860   :type 'hook)
861
862 (defcustom gnus-group-no-more-groups-hook nil
863   "*A hook run when returning to group mode having no more (unread) groups."
864   :group 'gnus-group-select
865   :type 'hook)
866
867 (defcustom gnus-ps-print-hook nil
868   "*A hook run before ps-printing something from Gnus."
869   :group 'gnus-summary
870   :type 'hook)
871
872 (defcustom gnus-summary-article-move-hook nil
873   "*A hook called after an article is moved, copied, respooled, or crossposted."
874   :group 'gnus-summary
875   :type 'hook)
876
877 (defcustom gnus-summary-article-delete-hook nil
878   "*A hook called after an article is deleted."
879   :group 'gnus-summary
880   :type 'hook)
881
882 (defcustom gnus-summary-article-expire-hook nil
883   "*A hook called after an article is expired."
884   :group 'gnus-summary
885   :type 'hook)
886
887 (defcustom gnus-summary-display-arrow
888   (and (fboundp 'display-graphic-p)
889        (display-graphic-p))
890   "*If non-nil, display an arrow highlighting the current article."
891   :version "21.1"
892   :group 'gnus-summary
893   :type 'boolean)
894
895 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
896   "Face used for highlighting the current article in the summary buffer."
897   :group 'gnus-summary-visual
898   :type 'face)
899
900 (defvar gnus-tmp-downloaded nil)
901
902 (defcustom gnus-summary-highlight
903   '(((eq mark gnus-canceled-mark)
904      . gnus-summary-cancelled-face)
905     ((and uncached (> score default-high))
906      . gnus-summary-high-undownloaded-face)
907     ((and uncached (< score default-low))
908      . gnus-summary-low-undownloaded-face)
909     (uncached
910      . gnus-summary-normal-undownloaded-face)
911     ((and (> score default-high)
912           (or (eq mark gnus-dormant-mark)
913               (eq mark gnus-ticked-mark)))
914      . gnus-summary-high-ticked-face)
915     ((and (< score default-low)
916           (or (eq mark gnus-dormant-mark)
917               (eq mark gnus-ticked-mark)))
918      . gnus-summary-low-ticked-face)
919     ((or (eq mark gnus-dormant-mark)
920          (eq mark gnus-ticked-mark))
921      . gnus-summary-normal-ticked-face)
922     ((and (> score default-high) (eq mark gnus-ancient-mark))
923      . gnus-summary-high-ancient-face)
924     ((and (< score default-low) (eq mark gnus-ancient-mark))
925      . gnus-summary-low-ancient-face)
926     ((eq mark gnus-ancient-mark)
927      . gnus-summary-normal-ancient-face)
928     ((and (> score default-high) (eq mark gnus-unread-mark))
929      . gnus-summary-high-unread-face)
930     ((and (< score default-low) (eq mark gnus-unread-mark))
931      . gnus-summary-low-unread-face)
932     ((eq mark gnus-unread-mark)
933      . gnus-summary-normal-unread-face)
934     ((> score default-high)
935      . gnus-summary-high-read-face)
936     ((< score default-low)
937      . gnus-summary-low-read-face)
938     (t
939      . gnus-summary-normal-read-face))
940   "*Controls the highlighting of summary buffer lines.
941
942 A list of (FORM . FACE) pairs.  When deciding how a a particular
943 summary line should be displayed, each form is evaluated.  The content
944 of the face field after the first true form is used.  You can change
945 how those summary lines are displayed, by editing the face field.
946
947 You can use the following variables in the FORM field.
948
949 score:        The article's score
950 default:      The default article score.
951 default-high: The default score for high scored articles.
952 default-low:  The default score for low scored articles.
953 below:        The score below which articles are automatically marked as read.
954 mark:         The articles mark."
955   :group 'gnus-summary-visual
956   :type '(repeat (cons (sexp :tag "Form" nil)
957                        face)))
958
959 (defcustom gnus-alter-header-function nil
960   "Function called to allow alteration of article header structures.
961 The function is called with one parameter, the article header vector,
962 which it may alter in any way."
963   :type '(choice (const :tag "None" nil)
964                  function)
965   :group 'gnus-summary)
966
967 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
968   "Variable that says which function should be used to decode a string with encoded words.")
969
970 (defcustom gnus-extra-headers '(To Newsgroups)
971   "*Extra headers to parse."
972   :version "21.1"
973   :group 'gnus-summary
974   :type '(repeat symbol))
975
976 (defcustom gnus-ignored-from-addresses
977   (and user-mail-address (regexp-quote user-mail-address))
978   "*Regexp of From headers that may be suppressed in favor of To headers."
979   :version "21.1"
980   :group 'gnus-summary
981   :type 'regexp)
982
983 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
984   "List of charsets that should be ignored.
985 When these charsets are used in the \"charset\" parameter, the
986 default charset will be used instead."
987   :version "21.1"
988   :type '(repeat symbol)
989   :group 'gnus-charset)
990
991 (gnus-define-group-parameter
992  ignored-charsets
993  :type list
994  :function-document
995  "Return the ignored charsets of GROUP."
996  :variable gnus-group-ignored-charsets-alist
997  :variable-default
998  '(("alt\\.chinese\\.text" iso-8859-1))
999  :variable-document
1000  "Alist of regexps (to match group names) and charsets that should be ignored.
1001 When these charsets are used in the \"charset\" parameter, the
1002 default charset will be used instead."
1003  :variable-group gnus-charset
1004  :variable-type '(repeat (cons (regexp :tag "Group")
1005                                (repeat symbol)))
1006  :parameter-type '(choice :tag "Ignored charsets"
1007                           :value nil
1008                           (repeat (symbol)))
1009  :parameter-document       "\
1010 List of charsets that should be ignored.
1011
1012 When these charsets are used in the \"charset\" parameter, the
1013 default charset will be used instead.")
1014
1015 (defcustom gnus-group-highlight-words-alist nil
1016   "Alist of group regexps and highlight regexps.
1017 This variable uses the same syntax as `gnus-emphasis-alist'."
1018   :version "21.1"
1019   :type '(repeat (cons (regexp :tag "Group")
1020                        (repeat (list (regexp :tag "Highlight regexp")
1021                                      (number :tag "Group for entire word" 0)
1022                                      (number :tag "Group for displayed part" 0)
1023                                      (symbol :tag "Face"
1024                                              gnus-emphasis-highlight-words)))))
1025   :group 'gnus-summary-visual)
1026
1027 (defcustom gnus-summary-show-article-charset-alist
1028   nil
1029   "Alist of number and charset.
1030 The article will be shown with the charset corresponding to the
1031 numbered argument.
1032 For example: ((1 . cn-gb-2312) (2 . big5))."
1033   :version "21.1"
1034   :type '(repeat (cons (number :tag "Argument" 1)
1035                        (symbol :tag "Charset")))
1036   :group 'gnus-charset)
1037
1038 (defcustom gnus-preserve-marks t
1039   "Whether marks are preserved when moving, copying and respooling messages."
1040   :version "21.1"
1041   :type 'boolean
1042   :group 'gnus-summary-marks)
1043
1044 (defcustom gnus-alter-articles-to-read-function nil
1045   "Function to be called to alter the list of articles to be selected."
1046   :type '(choice (const nil) function)
1047   :group 'gnus-summary)
1048
1049 (defcustom gnus-orphan-score nil
1050   "*All orphans get this score added.  Set in the score file."
1051   :group 'gnus-score-default
1052   :type '(choice (const nil)
1053                  integer))
1054
1055 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1056   "*A regexp to match MIME parts when saving multiple parts of a
1057 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1058 This regexp will be used by default when prompting the user for which
1059 type of files to save."
1060   :group 'gnus-summary
1061   :type 'regexp)
1062
1063 (defcustom gnus-read-all-available-headers nil
1064   "Whether Gnus should parse all headers made available to it.
1065 This is mostly relevant for slow backends where the user may
1066 wish to widen the summary buffer to include all headers
1067 that were fetched.  Say, for nnultimate groups."
1068   :group 'gnus-summary
1069   :type '(choice boolean regexp))
1070
1071 (defcustom gnus-summary-muttprint-program "muttprint"
1072   "Command (and optional arguments) used to run Muttprint."
1073   :version "21.3"
1074   :group 'gnus-summary
1075   :type 'string)
1076
1077 (defcustom gnus-article-loose-mime nil
1078   "If non-nil, don't require MIME-Version header.
1079 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1080 supply the MIME-Version header or deliberately strip it From the mail.
1081 Set it to non-nil, Gnus will treat some articles as MIME even if
1082 the MIME-Version header is missed."
1083   :version "21.3"
1084   :type 'boolean
1085   :group 'gnus-article-mime)
1086
1087 (defcustom gnus-article-emulate-mime t
1088   "If non-nil, use MIME emulation for uuencode and the like.
1089 This means that Gnus will search message bodies for text that look
1090 like uuencoded bits, yEncoded bits, and so on, and present that using
1091 the normal Gnus MIME machinery."
1092   :type 'boolean
1093   :group 'gnus-article-mime)
1094
1095 ;;; Internal variables
1096
1097 (defvar gnus-summary-display-cache nil)
1098 (defvar gnus-article-mime-handles nil)
1099 (defvar gnus-article-decoded-p nil)
1100 (defvar gnus-article-charset nil)
1101 (defvar gnus-article-ignored-charsets nil)
1102 (defvar gnus-scores-exclude-files nil)
1103 (defvar gnus-page-broken nil)
1104
1105 (defvar gnus-original-article nil)
1106 (defvar gnus-article-internal-prepare-hook nil)
1107 (defvar gnus-newsgroup-process-stack nil)
1108
1109 (defvar gnus-thread-indent-array nil)
1110 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1111 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1112   "Function called to sort the articles within a thread after it has been gathered together.")
1113
1114 (defvar gnus-summary-save-parts-type-history nil)
1115 (defvar gnus-summary-save-parts-last-directory nil)
1116
1117 ;; Avoid highlighting in kill files.
1118 (defvar gnus-summary-inhibit-highlight nil)
1119 (defvar gnus-newsgroup-selected-overlay nil)
1120 (defvar gnus-inhibit-limiting nil)
1121 (defvar gnus-newsgroup-adaptive-score-file nil)
1122 (defvar gnus-current-score-file nil)
1123 (defvar gnus-current-move-group nil)
1124 (defvar gnus-current-copy-group nil)
1125 (defvar gnus-current-crosspost-group nil)
1126 (defvar gnus-newsgroup-display nil)
1127
1128 (defvar gnus-newsgroup-dependencies nil)
1129 (defvar gnus-newsgroup-adaptive nil)
1130 (defvar gnus-summary-display-article-function nil)
1131 (defvar gnus-summary-highlight-line-function nil
1132   "Function called after highlighting a summary line.")
1133
1134 (defvar gnus-summary-line-format-alist
1135   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1136     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1137     (?s gnus-tmp-subject-or-nil ?s)
1138     (?n gnus-tmp-name ?s)
1139     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1140         ?s)
1141     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1142             gnus-tmp-from) ?s)
1143     (?F gnus-tmp-from ?s)
1144     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1145     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1146     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1147     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1148     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1149     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1150     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1151     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1152     (?L gnus-tmp-lines ?s)
1153     (?O gnus-tmp-downloaded ?c)
1154     (?I gnus-tmp-indentation ?s)
1155     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1156     (?R gnus-tmp-replied ?c)
1157     (?\[ gnus-tmp-opening-bracket ?c)
1158     (?\] gnus-tmp-closing-bracket ?c)
1159     (?\> (make-string gnus-tmp-level ? ) ?s)
1160     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1161     (?i gnus-tmp-score ?d)
1162     (?z gnus-tmp-score-char ?c)
1163     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1164     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1165     (?U gnus-tmp-unread ?c)
1166     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1167         ?s)
1168     (?t (gnus-summary-number-of-articles-in-thread
1169          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1170         ?d)
1171     (?e (gnus-summary-number-of-articles-in-thread
1172          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1173         ?c)
1174     (?u gnus-tmp-user-defined ?s)
1175     (?P (gnus-pick-line-number) ?d)
1176     (?B gnus-tmp-thread-tree-header-string ?s)
1177     (user-date (gnus-user-date
1178                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1179   "An alist of format specifications that can appear in summary lines.
1180 These are paired with what variables they correspond with, along with
1181 the type of the variable (string, integer, character, etc).")
1182
1183 (defvar gnus-summary-dummy-line-format-alist
1184   `((?S gnus-tmp-subject ?s)
1185     (?N gnus-tmp-number ?d)
1186     (?u gnus-tmp-user-defined ?s)))
1187
1188 (defvar gnus-summary-mode-line-format-alist
1189   `((?G gnus-tmp-group-name ?s)
1190     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1191     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1192     (?A gnus-tmp-article-number ?d)
1193     (?Z gnus-tmp-unread-and-unselected ?s)
1194     (?V gnus-version ?s)
1195     (?U gnus-tmp-unread-and-unticked ?d)
1196     (?S gnus-tmp-subject ?s)
1197     (?e gnus-tmp-unselected ?d)
1198     (?u gnus-tmp-user-defined ?s)
1199     (?d (length gnus-newsgroup-dormant) ?d)
1200     (?t (length gnus-newsgroup-marked) ?d)
1201     (?h (length gnus-newsgroup-spam-marked) ?d)
1202     (?r (length gnus-newsgroup-reads) ?d)
1203     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1204     (?E gnus-newsgroup-expunged-tally ?d)
1205     (?s (gnus-current-score-file-nondirectory) ?s)))
1206
1207 (defvar gnus-last-search-regexp nil
1208   "Default regexp for article search command.")
1209
1210 (defvar gnus-last-shell-command nil
1211   "Default shell command on article.")
1212
1213 (defvar gnus-newsgroup-agentized nil
1214   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1215 (defvar gnus-newsgroup-begin nil)
1216 (defvar gnus-newsgroup-end nil)
1217 (defvar gnus-newsgroup-last-rmail nil)
1218 (defvar gnus-newsgroup-last-mail nil)
1219 (defvar gnus-newsgroup-last-folder nil)
1220 (defvar gnus-newsgroup-last-file nil)
1221 (defvar gnus-newsgroup-auto-expire nil)
1222 (defvar gnus-newsgroup-active nil)
1223
1224 (defvar gnus-newsgroup-data nil)
1225 (defvar gnus-newsgroup-data-reverse nil)
1226 (defvar gnus-newsgroup-limit nil)
1227 (defvar gnus-newsgroup-limits nil)
1228 (defvar gnus-summary-use-undownloaded-faces nil)
1229
1230 (defvar gnus-newsgroup-unreads nil
1231   "Sorted list of unread articles in the current newsgroup.")
1232
1233 (defvar gnus-newsgroup-unselected nil
1234   "Sorted list of unselected unread articles in the current newsgroup.")
1235
1236 (defvar gnus-newsgroup-reads nil
1237   "Alist of read articles and article marks in the current newsgroup.")
1238
1239 (defvar gnus-newsgroup-expunged-tally nil)
1240
1241 (defvar gnus-newsgroup-marked nil
1242   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1243
1244 (defvar gnus-newsgroup-spam-marked nil
1245   "List of ranges of articles that have been marked as spam.")
1246
1247 (defvar gnus-newsgroup-killed nil
1248   "List of ranges of articles that have been through the scoring process.")
1249
1250 (defvar gnus-newsgroup-cached nil
1251   "Sorted list of articles that come from the article cache.")
1252
1253 (defvar gnus-newsgroup-saved nil
1254   "List of articles that have been saved.")
1255
1256 (defvar gnus-newsgroup-kill-headers nil)
1257
1258 (defvar gnus-newsgroup-replied nil
1259   "List of articles that have been replied to in the current newsgroup.")
1260
1261 (defvar gnus-newsgroup-forwarded nil
1262   "List of articles that have been forwarded in the current newsgroup.")
1263
1264 (defvar gnus-newsgroup-recent nil
1265   "List of articles that have are recent in the current newsgroup.")
1266
1267 (defvar gnus-newsgroup-expirable nil
1268   "Sorted list of articles in the current newsgroup that can be expired.")
1269
1270 (defvar gnus-newsgroup-processable nil
1271   "List of articles in the current newsgroup that can be processed.")
1272
1273 (defvar gnus-newsgroup-downloadable nil
1274   "Sorted list of articles in the current newsgroup that can be processed.")
1275
1276 (defvar gnus-newsgroup-unfetched nil
1277   "Sorted list of articles in the current newsgroup whose headers have
1278 not been fetched into the agent.
1279
1280 This list will always be a subset of gnus-newsgroup-undownloaded.")
1281
1282 (defvar gnus-newsgroup-undownloaded nil
1283   "List of articles in the current newsgroup that haven't been downloaded.")
1284
1285 (defvar gnus-newsgroup-unsendable nil
1286   "List of articles in the current newsgroup that won't be sent.")
1287
1288 (defvar gnus-newsgroup-bookmarks nil
1289   "List of articles in the current newsgroup that have bookmarks.")
1290
1291 (defvar gnus-newsgroup-dormant nil
1292   "Sorted list of dormant articles in the current newsgroup.")
1293
1294 (defvar gnus-newsgroup-unseen nil
1295   "List of unseen articles in the current newsgroup.")
1296
1297 (defvar gnus-newsgroup-seen nil
1298   "Range of seen articles in the current newsgroup.")
1299
1300 (defvar gnus-newsgroup-articles nil
1301   "List of articles in the current newsgroup.")
1302
1303 (defvar gnus-newsgroup-scored nil
1304   "List of scored articles in the current newsgroup.")
1305
1306 (defvar gnus-newsgroup-headers nil
1307   "List of article headers in the current newsgroup.")
1308
1309 (defvar gnus-newsgroup-threads nil)
1310
1311 (defvar gnus-newsgroup-prepared nil
1312   "Whether the current group has been prepared properly.")
1313
1314 (defvar gnus-newsgroup-ancient nil
1315   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1316
1317 (defvar gnus-newsgroup-sparse nil)
1318
1319 (defvar gnus-current-article nil)
1320 (defvar gnus-article-current nil)
1321 (defvar gnus-current-headers nil)
1322 (defvar gnus-have-all-headers nil)
1323 (defvar gnus-last-article nil)
1324 (defvar gnus-newsgroup-history nil)
1325 (defvar gnus-newsgroup-charset nil)
1326 (defvar gnus-newsgroup-ephemeral-charset nil)
1327 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1328
1329 (defvar gnus-article-before-search nil)
1330
1331 (defvar gnus-summary-local-variables
1332   '(gnus-newsgroup-name
1333     gnus-newsgroup-begin gnus-newsgroup-end
1334     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1335     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1336     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1337     gnus-newsgroup-unselected gnus-newsgroup-marked
1338     gnus-newsgroup-spam-marked
1339     gnus-newsgroup-reads gnus-newsgroup-saved
1340     gnus-newsgroup-replied gnus-newsgroup-forwarded
1341     gnus-newsgroup-recent
1342     gnus-newsgroup-expirable
1343     gnus-newsgroup-processable gnus-newsgroup-killed
1344     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1345     gnus-newsgroup-unfetched
1346     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1347     gnus-newsgroup-seen gnus-newsgroup-articles
1348     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1349     gnus-newsgroup-headers gnus-newsgroup-threads
1350     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1351     gnus-current-article gnus-current-headers gnus-have-all-headers
1352     gnus-last-article gnus-article-internal-prepare-hook
1353     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1354     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1355     gnus-thread-expunge-below
1356     gnus-score-alist gnus-current-score-file
1357     (gnus-summary-expunge-below . global)
1358     (gnus-summary-mark-below . global)
1359     (gnus-orphan-score . global)
1360     gnus-newsgroup-active gnus-scores-exclude-files
1361     gnus-newsgroup-history gnus-newsgroup-ancient
1362     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1363     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1364     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1365     (gnus-newsgroup-expunged-tally . 0)
1366     gnus-cache-removable-articles gnus-newsgroup-cached
1367     gnus-newsgroup-data gnus-newsgroup-data-reverse
1368     gnus-newsgroup-limit gnus-newsgroup-limits
1369     gnus-newsgroup-charset gnus-newsgroup-display
1370     gnus-summary-use-undownloaded-faces)
1371   "Variables that are buffer-local to the summary buffers.")
1372
1373 (defvar gnus-newsgroup-variables nil
1374   "A list of variables that have separate values in different newsgroups.
1375 A list of newsgroup (summary buffer) local variables, or cons of
1376 variables and their default values (when the default values are not
1377 nil), that should be made global while the summary buffer is active.
1378 These variables can be used to set variables in the group parameters
1379 while still allowing them to affect operations done in other
1380 buffers. For example:
1381
1382 \(setq gnus-newsgroup-variables
1383      '(message-use-followup-to
1384        (gnus-visible-headers .
1385          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1386 ")
1387
1388 ;; Byte-compiler warning.
1389 (eval-when-compile
1390   ;; Bind features so that require will believe that gnus-sum has
1391   ;; already been loaded (avoids infinite recursion)
1392   (let ((features (cons 'gnus-sum features)))
1393     ;; Several of the declarations in gnus-sum are needed to load the
1394     ;; following files. Right now, these definitions have been
1395     ;; compiled but not defined (evaluated).  We could either do a
1396     ;; eval-and-compile about all of the declarations or evaluate the
1397     ;; source file.
1398     (if (boundp 'gnus-newsgroup-variables)
1399         nil
1400       (load "gnus-sum.el" t t t))
1401     (require 'gnus)
1402     (require 'gnus-agent)
1403     (require 'gnus-art)))
1404
1405 ;; MIME stuff.
1406
1407 (defvar gnus-decode-encoded-word-methods
1408   '(mail-decode-encoded-word-string)
1409   "List of methods used to decode encoded words.
1410
1411 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1412 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1413 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1414 whose names match REGEXP.
1415
1416 For example:
1417 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1418  mail-decode-encoded-word-string
1419  (\"chinese\" . rfc1843-decode-string))")
1420
1421 (defvar gnus-decode-encoded-word-methods-cache nil)
1422
1423 (defun gnus-multi-decode-encoded-word-string (string)
1424   "Apply the functions from `gnus-encoded-word-methods' that match."
1425   (unless (and gnus-decode-encoded-word-methods-cache
1426                (eq gnus-newsgroup-name
1427                    (car gnus-decode-encoded-word-methods-cache)))
1428     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1429     (mapcar (lambda (x)
1430               (if (symbolp x)
1431                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1432                 (if (and gnus-newsgroup-name
1433                          (string-match (car x) gnus-newsgroup-name))
1434                     (nconc gnus-decode-encoded-word-methods-cache
1435                            (list (cdr x))))))
1436             gnus-decode-encoded-word-methods))
1437   (let ((xlist gnus-decode-encoded-word-methods-cache))
1438     (pop xlist)
1439     (while xlist
1440       (setq string (funcall (pop xlist) string))))
1441   string)
1442
1443 ;; Subject simplification.
1444
1445 (defun gnus-simplify-whitespace (str)
1446   "Remove excessive whitespace from STR."
1447   ;; Multiple spaces.
1448   (while (string-match "[ \t][ \t]+" str)
1449     (setq str (concat (substring str 0 (match-beginning 0))
1450                         " "
1451                         (substring str (match-end 0)))))
1452   ;; Leading spaces.
1453   (when (string-match "^[ \t]+" str)
1454     (setq str (substring str (match-end 0))))
1455   ;; Trailing spaces.
1456   (when (string-match "[ \t]+$" str)
1457     (setq str (substring str 0 (match-beginning 0))))
1458   str)
1459
1460 (defun gnus-simplify-all-whitespace (str)
1461   "Remove all whitespace from STR."
1462   (while (string-match "[ \t\n]+" str)
1463     (setq str (replace-match "" nil nil str)))
1464   str)
1465
1466 (defsubst gnus-simplify-subject-re (subject)
1467   "Remove \"Re:\" from subject lines."
1468   (if (string-match message-subject-re-regexp subject)
1469       (substring subject (match-end 0))
1470     subject))
1471
1472 (defun gnus-simplify-subject (subject &optional re-only)
1473   "Remove `Re:' and words in parentheses.
1474 If RE-ONLY is non-nil, strip leading `Re:'s only."
1475   (let ((case-fold-search t))           ;Ignore case.
1476     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1477     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1478       (setq subject (substring subject (match-end 0))))
1479     ;; Remove uninteresting prefixes.
1480     (when (and (not re-only)
1481                gnus-simplify-ignored-prefixes
1482                (string-match gnus-simplify-ignored-prefixes subject))
1483       (setq subject (substring subject (match-end 0))))
1484     ;; Remove words in parentheses from end.
1485     (unless re-only
1486       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1487         (setq subject (substring subject 0 (match-beginning 0)))))
1488     ;; Return subject string.
1489     subject))
1490
1491 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1492 ;; all whitespace.
1493 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1494   (goto-char (point-min))
1495   (while (re-search-forward regexp nil t)
1496     (replace-match (or newtext ""))))
1497
1498 (defun gnus-simplify-buffer-fuzzy ()
1499   "Simplify string in the buffer fuzzily.
1500 The string in the accessible portion of the current buffer is simplified.
1501 It is assumed to be a single-line subject.
1502 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1503 matter is removed.  Additional things can be deleted by setting
1504 `gnus-simplify-subject-fuzzy-regexp'."
1505   (let ((case-fold-search t)
1506         (modified-tick))
1507     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1508
1509     (while (not (eq modified-tick (buffer-modified-tick)))
1510       (setq modified-tick (buffer-modified-tick))
1511       (cond
1512        ((listp gnus-simplify-subject-fuzzy-regexp)
1513         (mapcar 'gnus-simplify-buffer-fuzzy-step
1514                 gnus-simplify-subject-fuzzy-regexp))
1515        (gnus-simplify-subject-fuzzy-regexp
1516         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1517       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1518       (gnus-simplify-buffer-fuzzy-step
1519        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1520       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1521
1522     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1523     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1524     (gnus-simplify-buffer-fuzzy-step " $")
1525     (gnus-simplify-buffer-fuzzy-step "^ +")))
1526
1527 (defun gnus-simplify-subject-fuzzy (subject)
1528   "Simplify a subject string fuzzily.
1529 See `gnus-simplify-buffer-fuzzy' for details."
1530   (save-excursion
1531     (gnus-set-work-buffer)
1532     (let ((case-fold-search t))
1533       ;; Remove uninteresting prefixes.
1534       (when (and gnus-simplify-ignored-prefixes
1535                  (string-match gnus-simplify-ignored-prefixes subject))
1536         (setq subject (substring subject (match-end 0))))
1537       (insert subject)
1538       (inline (gnus-simplify-buffer-fuzzy))
1539       (buffer-string))))
1540
1541 (defsubst gnus-simplify-subject-fully (subject)
1542   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1543   (cond
1544    (gnus-simplify-subject-functions
1545     (gnus-map-function gnus-simplify-subject-functions subject))
1546    ((null gnus-summary-gather-subject-limit)
1547     (gnus-simplify-subject-re subject))
1548    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1549     (gnus-simplify-subject-fuzzy subject))
1550    ((numberp gnus-summary-gather-subject-limit)
1551     (gnus-limit-string (gnus-simplify-subject-re subject)
1552                        gnus-summary-gather-subject-limit))
1553    (t
1554     subject)))
1555
1556 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1557   "Check whether two subjects are equal.
1558 If optional argument SIMPLE-FIRST is t, first argument is already
1559 simplified."
1560   (cond
1561    ((null simple-first)
1562     (equal (gnus-simplify-subject-fully s1)
1563            (gnus-simplify-subject-fully s2)))
1564    (t
1565     (equal s1
1566            (gnus-simplify-subject-fully s2)))))
1567
1568 (defun gnus-summary-bubble-group ()
1569   "Increase the score of the current group.
1570 This is a handy function to add to `gnus-summary-exit-hook' to
1571 increase the score of each group you read."
1572   (gnus-group-add-score gnus-newsgroup-name))
1573
1574 \f
1575 ;;;
1576 ;;; Gnus summary mode
1577 ;;;
1578
1579 (put 'gnus-summary-mode 'mode-class 'special)
1580
1581 (defvar gnus-article-commands-menu)
1582
1583 ;; Non-orthogonal keys
1584
1585 (gnus-define-keys gnus-summary-mode-map
1586   " " gnus-summary-next-page
1587   "\177" gnus-summary-prev-page
1588   [delete] gnus-summary-prev-page
1589   [backspace] gnus-summary-prev-page
1590   "\r" gnus-summary-scroll-up
1591   "\M-\r" gnus-summary-scroll-down
1592   "n" gnus-summary-next-unread-article
1593   "p" gnus-summary-prev-unread-article
1594   "N" gnus-summary-next-article
1595   "P" gnus-summary-prev-article
1596   "\M-\C-n" gnus-summary-next-same-subject
1597   "\M-\C-p" gnus-summary-prev-same-subject
1598   "\M-n" gnus-summary-next-unread-subject
1599   "\M-p" gnus-summary-prev-unread-subject
1600   "." gnus-summary-first-unread-article
1601   "," gnus-summary-best-unread-article
1602   "\M-s" gnus-summary-search-article-forward
1603   "\M-r" gnus-summary-search-article-backward
1604   "<" gnus-summary-beginning-of-article
1605   ">" gnus-summary-end-of-article
1606   "j" gnus-summary-goto-article
1607   "^" gnus-summary-refer-parent-article
1608   "\M-^" gnus-summary-refer-article
1609   "u" gnus-summary-tick-article-forward
1610   "!" gnus-summary-tick-article-forward
1611   "U" gnus-summary-tick-article-backward
1612   "d" gnus-summary-mark-as-read-forward
1613   "D" gnus-summary-mark-as-read-backward
1614   "E" gnus-summary-mark-as-expirable
1615   "\M-u" gnus-summary-clear-mark-forward
1616   "\M-U" gnus-summary-clear-mark-backward
1617   "k" gnus-summary-kill-same-subject-and-select
1618   "\C-k" gnus-summary-kill-same-subject
1619   "\M-\C-k" gnus-summary-kill-thread
1620   "\M-\C-l" gnus-summary-lower-thread
1621   "e" gnus-summary-edit-article
1622   "#" gnus-summary-mark-as-processable
1623   "\M-#" gnus-summary-unmark-as-processable
1624   "\M-\C-t" gnus-summary-toggle-threads
1625   "\M-\C-s" gnus-summary-show-thread
1626   "\M-\C-h" gnus-summary-hide-thread
1627   "\M-\C-f" gnus-summary-next-thread
1628   "\M-\C-b" gnus-summary-prev-thread
1629   [(meta down)] gnus-summary-next-thread
1630   [(meta up)] gnus-summary-prev-thread
1631   "\M-\C-u" gnus-summary-up-thread
1632   "\M-\C-d" gnus-summary-down-thread
1633   "&" gnus-summary-execute-command
1634   "c" gnus-summary-catchup-and-exit
1635   "\C-w" gnus-summary-mark-region-as-read
1636   "\C-t" gnus-summary-toggle-truncation
1637   "?" gnus-summary-mark-as-dormant
1638   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1639   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1640   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1641   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1642   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1643   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1644   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1645   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1646   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1647   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1648   "=" gnus-summary-expand-window
1649   "\C-x\C-s" gnus-summary-reselect-current-group
1650   "\M-g" gnus-summary-rescan-group
1651   "w" gnus-summary-stop-page-breaking
1652   "\C-c\C-r" gnus-summary-caesar-message
1653   "f" gnus-summary-followup
1654   "F" gnus-summary-followup-with-original
1655   "C" gnus-summary-cancel-article
1656   "r" gnus-summary-reply
1657   "R" gnus-summary-reply-with-original
1658   "\C-c\C-f" gnus-summary-mail-forward
1659   "o" gnus-summary-save-article
1660   "\C-o" gnus-summary-save-article-mail
1661   "|" gnus-summary-pipe-output
1662   "\M-k" gnus-summary-edit-local-kill
1663   "\M-K" gnus-summary-edit-global-kill
1664   ;; "V" gnus-version
1665   "\C-c\C-d" gnus-summary-describe-group
1666   "q" gnus-summary-exit
1667   "Q" gnus-summary-exit-no-update
1668   "\C-c\C-i" gnus-info-find-node
1669   gnus-mouse-2 gnus-mouse-pick-article
1670   "m" gnus-summary-mail-other-window
1671   "a" gnus-summary-post-news
1672   "i" gnus-summary-news-other-window
1673   "x" gnus-summary-limit-to-unread
1674   "s" gnus-summary-isearch-article
1675   "t" gnus-summary-toggle-header
1676   "g" gnus-summary-show-article
1677   "l" gnus-summary-goto-last-article
1678   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1679   "\C-d" gnus-summary-enter-digest-group
1680   "\M-\C-d" gnus-summary-read-document
1681   "\M-\C-e" gnus-summary-edit-parameters
1682   "\M-\C-a" gnus-summary-customize-parameters
1683   "\C-c\C-b" gnus-bug
1684   "*" gnus-cache-enter-article
1685   "\M-*" gnus-cache-remove-article
1686   "\M-&" gnus-summary-universal-argument
1687   "\C-l" gnus-recenter
1688   "I" gnus-summary-increase-score
1689   "L" gnus-summary-lower-score
1690   "\M-i" gnus-symbolic-argument
1691   "h" gnus-summary-select-article-buffer
1692
1693   "b" gnus-article-view-part
1694   "\M-t" gnus-summary-toggle-display-buttonized
1695
1696   "V" gnus-summary-score-map
1697   "X" gnus-uu-extract-map
1698   "S" gnus-summary-send-map)
1699
1700 ;; Sort of orthogonal keymap
1701 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1702   "t" gnus-summary-tick-article-forward
1703   "!" gnus-summary-tick-article-forward
1704   "d" gnus-summary-mark-as-read-forward
1705   "r" gnus-summary-mark-as-read-forward
1706   "c" gnus-summary-clear-mark-forward
1707   " " gnus-summary-clear-mark-forward
1708   "e" gnus-summary-mark-as-expirable
1709   "x" gnus-summary-mark-as-expirable
1710   "?" gnus-summary-mark-as-dormant
1711   "b" gnus-summary-set-bookmark
1712   "B" gnus-summary-remove-bookmark
1713   "#" gnus-summary-mark-as-processable
1714   "\M-#" gnus-summary-unmark-as-processable
1715   "S" gnus-summary-limit-include-expunged
1716   "C" gnus-summary-catchup
1717   "H" gnus-summary-catchup-to-here
1718   "h" gnus-summary-catchup-from-here
1719   "\C-c" gnus-summary-catchup-all
1720   "k" gnus-summary-kill-same-subject-and-select
1721   "K" gnus-summary-kill-same-subject
1722   "P" gnus-uu-mark-map)
1723
1724 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1725   "c" gnus-summary-clear-above
1726   "u" gnus-summary-tick-above
1727   "m" gnus-summary-mark-above
1728   "k" gnus-summary-kill-below)
1729
1730 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1731   "/" gnus-summary-limit-to-subject
1732   "n" gnus-summary-limit-to-articles
1733   "w" gnus-summary-pop-limit
1734   "s" gnus-summary-limit-to-subject
1735   "a" gnus-summary-limit-to-author
1736   "u" gnus-summary-limit-to-unread
1737   "m" gnus-summary-limit-to-marks
1738   "M" gnus-summary-limit-exclude-marks
1739   "v" gnus-summary-limit-to-score
1740   "*" gnus-summary-limit-include-cached
1741   "D" gnus-summary-limit-include-dormant
1742   "T" gnus-summary-limit-include-thread
1743   "d" gnus-summary-limit-exclude-dormant
1744   "t" gnus-summary-limit-to-age
1745   "." gnus-summary-limit-to-unseen
1746   "x" gnus-summary-limit-to-extra
1747   "p" gnus-summary-limit-to-display-predicate
1748   "E" gnus-summary-limit-include-expunged
1749   "c" gnus-summary-limit-exclude-childless-dormant
1750   "C" gnus-summary-limit-mark-excluded-as-read
1751   "o" gnus-summary-insert-old-articles
1752   "N" gnus-summary-insert-new-articles)
1753
1754 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1755   "n" gnus-summary-next-unread-article
1756   "p" gnus-summary-prev-unread-article
1757   "N" gnus-summary-next-article
1758   "P" gnus-summary-prev-article
1759   "\C-n" gnus-summary-next-same-subject
1760   "\C-p" gnus-summary-prev-same-subject
1761   "\M-n" gnus-summary-next-unread-subject
1762   "\M-p" gnus-summary-prev-unread-subject
1763   "f" gnus-summary-first-unread-article
1764   "b" gnus-summary-best-unread-article
1765   "j" gnus-summary-goto-article
1766   "g" gnus-summary-goto-subject
1767   "l" gnus-summary-goto-last-article
1768   "o" gnus-summary-pop-article)
1769
1770 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1771   "k" gnus-summary-kill-thread
1772   "l" gnus-summary-lower-thread
1773   "i" gnus-summary-raise-thread
1774   "T" gnus-summary-toggle-threads
1775   "t" gnus-summary-rethread-current
1776   "^" gnus-summary-reparent-thread
1777   "s" gnus-summary-show-thread
1778   "S" gnus-summary-show-all-threads
1779   "h" gnus-summary-hide-thread
1780   "H" gnus-summary-hide-all-threads
1781   "n" gnus-summary-next-thread
1782   "p" gnus-summary-prev-thread
1783   "u" gnus-summary-up-thread
1784   "o" gnus-summary-top-thread
1785   "d" gnus-summary-down-thread
1786   "#" gnus-uu-mark-thread
1787   "\M-#" gnus-uu-unmark-thread)
1788
1789 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1790   "g" gnus-summary-prepare
1791   "c" gnus-summary-insert-cached-articles
1792   "d" gnus-summary-insert-dormant-articles)
1793
1794 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1795   "c" gnus-summary-catchup-and-exit
1796   "C" gnus-summary-catchup-all-and-exit
1797   "E" gnus-summary-exit-no-update
1798   "Q" gnus-summary-exit
1799   "Z" gnus-summary-exit
1800   "n" gnus-summary-catchup-and-goto-next-group
1801   "R" gnus-summary-reselect-current-group
1802   "G" gnus-summary-rescan-group
1803   "N" gnus-summary-next-group
1804   "s" gnus-summary-save-newsrc
1805   "P" gnus-summary-prev-group)
1806
1807 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1808   " " gnus-summary-next-page
1809   "n" gnus-summary-next-page
1810   "\177" gnus-summary-prev-page
1811   [delete] gnus-summary-prev-page
1812   "p" gnus-summary-prev-page
1813   "\r" gnus-summary-scroll-up
1814   "\M-\r" gnus-summary-scroll-down
1815   "<" gnus-summary-beginning-of-article
1816   ">" gnus-summary-end-of-article
1817   "b" gnus-summary-beginning-of-article
1818   "e" gnus-summary-end-of-article
1819   "^" gnus-summary-refer-parent-article
1820   "r" gnus-summary-refer-parent-article
1821   "D" gnus-summary-enter-digest-group
1822   "R" gnus-summary-refer-references
1823   "T" gnus-summary-refer-thread
1824   "g" gnus-summary-show-article
1825   "s" gnus-summary-isearch-article
1826   "P" gnus-summary-print-article
1827   "M" gnus-mailing-list-insinuate
1828   "t" gnus-article-babel)
1829
1830 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1831   "b" gnus-article-add-buttons
1832   "B" gnus-article-add-buttons-to-head
1833   "o" gnus-article-treat-overstrike
1834   "e" gnus-article-emphasize
1835   "w" gnus-article-fill-cited-article
1836   "Q" gnus-article-fill-long-lines
1837   "C" gnus-article-capitalize-sentences
1838   "c" gnus-article-remove-cr
1839   "q" gnus-article-de-quoted-unreadable
1840   "6" gnus-article-de-base64-unreadable
1841   "Z" gnus-article-decode-HZ
1842   "h" gnus-article-wash-html
1843   "u" gnus-article-unsplit-urls
1844   "s" gnus-summary-force-verify-and-decrypt
1845   "f" gnus-article-display-x-face
1846   "l" gnus-summary-stop-page-breaking
1847   "r" gnus-summary-caesar-message
1848   "m" gnus-summary-morse-message
1849   "t" gnus-summary-toggle-header
1850   "g" gnus-treat-smiley
1851   "v" gnus-summary-verbose-headers
1852   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1853   "p" gnus-article-verify-x-pgp-sig
1854   "d" gnus-article-treat-dumbquotes)
1855
1856 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1857   ;; mnemonic: deuglif*Y*
1858   "u" gnus-article-outlook-unwrap-lines
1859   "a" gnus-article-outlook-repair-attribution
1860   "c" gnus-article-outlook-rearrange-citation
1861   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1862
1863 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1864   "a" gnus-article-hide
1865   "h" gnus-article-hide-headers
1866   "b" gnus-article-hide-boring-headers
1867   "s" gnus-article-hide-signature
1868   "c" gnus-article-hide-citation
1869   "C" gnus-article-hide-citation-in-followups
1870   "l" gnus-article-hide-list-identifiers
1871   "B" gnus-article-strip-banner
1872   "P" gnus-article-hide-pem
1873   "\C-c" gnus-article-hide-citation-maybe)
1874
1875 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1876   "a" gnus-article-highlight
1877   "h" gnus-article-highlight-headers
1878   "c" gnus-article-highlight-citation
1879   "s" gnus-article-highlight-signature)
1880
1881 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1882   "f" gnus-article-treat-fold-headers
1883   "u" gnus-article-treat-unfold-headers
1884   "n" gnus-article-treat-fold-newsgroups)
1885
1886 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1887   "x" gnus-article-display-x-face
1888   "d" gnus-article-display-face
1889   "s" gnus-treat-smiley
1890   "D" gnus-article-remove-images
1891   "f" gnus-treat-from-picon
1892   "m" gnus-treat-mail-picon
1893   "n" gnus-treat-newsgroups-picon)
1894
1895 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1896   "w" gnus-article-decode-mime-words
1897   "c" gnus-article-decode-charset
1898   "v" gnus-mime-view-all-parts
1899   "b" gnus-article-view-part)
1900
1901 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1902   "z" gnus-article-date-ut
1903   "u" gnus-article-date-ut
1904   "l" gnus-article-date-local
1905   "p" gnus-article-date-english
1906   "e" gnus-article-date-lapsed
1907   "o" gnus-article-date-original
1908   "i" gnus-article-date-iso8601
1909   "s" gnus-article-date-user)
1910
1911 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1912   "t" gnus-article-remove-trailing-blank-lines
1913   "l" gnus-article-strip-leading-blank-lines
1914   "m" gnus-article-strip-multiple-blank-lines
1915   "a" gnus-article-strip-blank-lines
1916   "A" gnus-article-strip-all-blank-lines
1917   "s" gnus-article-strip-leading-space
1918   "e" gnus-article-strip-trailing-space
1919   "w" gnus-article-remove-leading-whitespace)
1920
1921 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1922   "v" gnus-version
1923   "f" gnus-summary-fetch-faq
1924   "d" gnus-summary-describe-group
1925   "h" gnus-summary-describe-briefly
1926   "i" gnus-info-find-node
1927   "c" gnus-group-fetch-charter
1928   "C" gnus-group-fetch-control)
1929
1930 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1931   "e" gnus-summary-expire-articles
1932   "\M-\C-e" gnus-summary-expire-articles-now
1933   "\177" gnus-summary-delete-article
1934   [delete] gnus-summary-delete-article
1935   [backspace] gnus-summary-delete-article
1936   "m" gnus-summary-move-article
1937   "r" gnus-summary-respool-article
1938   "w" gnus-summary-edit-article
1939   "c" gnus-summary-copy-article
1940   "B" gnus-summary-crosspost-article
1941   "q" gnus-summary-respool-query
1942   "t" gnus-summary-respool-trace
1943   "i" gnus-summary-import-article
1944   "I" gnus-summary-create-article
1945   "p" gnus-summary-article-posted-p)
1946
1947 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1948   "o" gnus-summary-save-article
1949   "m" gnus-summary-save-article-mail
1950   "F" gnus-summary-write-article-file
1951   "r" gnus-summary-save-article-rmail
1952   "f" gnus-summary-save-article-file
1953   "b" gnus-summary-save-article-body-file
1954   "h" gnus-summary-save-article-folder
1955   "v" gnus-summary-save-article-vm
1956   "p" gnus-summary-pipe-output
1957   "P" gnus-summary-muttprint
1958   "s" gnus-soup-add-article)
1959
1960 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1961   "b" gnus-summary-display-buttonized
1962   "m" gnus-summary-repair-multipart
1963   "v" gnus-article-view-part
1964   "o" gnus-article-save-part
1965   "c" gnus-article-copy-part
1966   "C" gnus-article-view-part-as-charset
1967   "e" gnus-article-view-part-externally
1968   "E" gnus-article-encrypt-body
1969   "i" gnus-article-inline-part
1970   "|" gnus-article-pipe-part)
1971
1972 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1973   "p" gnus-summary-mark-as-processable
1974   "u" gnus-summary-unmark-as-processable
1975   "U" gnus-summary-unmark-all-processable
1976   "v" gnus-uu-mark-over
1977   "s" gnus-uu-mark-series
1978   "r" gnus-uu-mark-region
1979   "g" gnus-uu-unmark-region
1980   "R" gnus-uu-mark-by-regexp
1981   "G" gnus-uu-unmark-by-regexp
1982   "t" gnus-uu-mark-thread
1983   "T" gnus-uu-unmark-thread
1984   "a" gnus-uu-mark-all
1985   "b" gnus-uu-mark-buffer
1986   "S" gnus-uu-mark-sparse
1987   "k" gnus-summary-kill-process-mark
1988   "y" gnus-summary-yank-process-mark
1989   "w" gnus-summary-save-process-mark
1990   "i" gnus-uu-invert-processable)
1991
1992 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1993   ;;"x" gnus-uu-extract-any
1994   "m" gnus-summary-save-parts
1995   "u" gnus-uu-decode-uu
1996   "U" gnus-uu-decode-uu-and-save
1997   "s" gnus-uu-decode-unshar
1998   "S" gnus-uu-decode-unshar-and-save
1999   "o" gnus-uu-decode-save
2000   "O" gnus-uu-decode-save
2001   "b" gnus-uu-decode-binhex
2002   "B" gnus-uu-decode-binhex
2003   "p" gnus-uu-decode-postscript
2004   "P" gnus-uu-decode-postscript-and-save)
2005
2006 (gnus-define-keys
2007     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2008   "u" gnus-uu-decode-uu-view
2009   "U" gnus-uu-decode-uu-and-save-view
2010   "s" gnus-uu-decode-unshar-view
2011   "S" gnus-uu-decode-unshar-and-save-view
2012   "o" gnus-uu-decode-save-view
2013   "O" gnus-uu-decode-save-view
2014   "b" gnus-uu-decode-binhex-view
2015   "B" gnus-uu-decode-binhex-view
2016   "p" gnus-uu-decode-postscript-view
2017   "P" gnus-uu-decode-postscript-and-save-view)
2018
2019 (defvar gnus-article-post-menu nil)
2020
2021 (defconst gnus-summary-menu-maxlen 20)
2022
2023 (defun gnus-summary-menu-split (menu)
2024   ;; If we have lots of elements, divide them into groups of 20
2025   ;; and make a pane (or submenu) for each one.
2026   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2027       (let ((menu menu) sublists next
2028             (i 1))
2029         (while menu
2030           ;; Pull off the next gnus-summary-menu-maxlen elements
2031           ;; and make them the next element of sublist.
2032           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2033           (if next
2034               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2035                       nil))
2036           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2037                                              (aref (car (last menu)) 0)) menu)
2038                                sublists))
2039           (setq i (1+ i))
2040           (setq menu next))
2041         (nreverse sublists))
2042     ;; Few elements--put them all in one pane.
2043     menu))
2044
2045 (defun gnus-summary-make-menu-bar ()
2046   (gnus-turn-off-edit-menu 'summary)
2047
2048   (unless (boundp 'gnus-summary-misc-menu)
2049
2050     (easy-menu-define
2051       gnus-summary-kill-menu gnus-summary-mode-map ""
2052       (cons
2053        "Score"
2054        (nconc
2055         (list
2056          ["Customize" gnus-score-customize t])
2057         (gnus-make-score-map 'increase)
2058         (gnus-make-score-map 'lower)
2059         '(("Mark"
2060            ["Kill below" gnus-summary-kill-below t]
2061            ["Mark above" gnus-summary-mark-above t]
2062            ["Tick above" gnus-summary-tick-above t]
2063            ["Clear above" gnus-summary-clear-above t])
2064           ["Current score" gnus-summary-current-score t]
2065           ["Set score" gnus-summary-set-score t]
2066           ["Switch current score file..." gnus-score-change-score-file t]
2067           ["Set mark below..." gnus-score-set-mark-below t]
2068           ["Set expunge below..." gnus-score-set-expunge-below t]
2069           ["Edit current score file" gnus-score-edit-current-scores t]
2070           ["Edit score file" gnus-score-edit-file t]
2071           ["Trace score" gnus-score-find-trace t]
2072           ["Find words" gnus-score-find-favourite-words t]
2073           ["Rescore buffer" gnus-summary-rescore t]
2074           ["Increase score..." gnus-summary-increase-score t]
2075           ["Lower score..." gnus-summary-lower-score t]))))
2076
2077     ;; Define both the Article menu in the summary buffer and the
2078     ;; equivalent Commands menu in the article buffer here for
2079     ;; consistency.
2080     (let ((innards
2081            `(("Hide"
2082               ["All" gnus-article-hide t]
2083               ["Headers" gnus-article-hide-headers t]
2084               ["Signature" gnus-article-hide-signature t]
2085               ["Citation" gnus-article-hide-citation t]
2086               ["List identifiers" gnus-article-hide-list-identifiers t]
2087               ["Banner" gnus-article-strip-banner t]
2088               ["Boring headers" gnus-article-hide-boring-headers t])
2089              ("Highlight"
2090               ["All" gnus-article-highlight t]
2091               ["Headers" gnus-article-highlight-headers t]
2092               ["Signature" gnus-article-highlight-signature t]
2093               ["Citation" gnus-article-highlight-citation t])
2094              ("MIME"
2095               ["Words" gnus-article-decode-mime-words t]
2096               ["Charset" gnus-article-decode-charset t]
2097               ["QP" gnus-article-de-quoted-unreadable t]
2098               ["Base64" gnus-article-de-base64-unreadable t]
2099               ["View MIME buttons" gnus-summary-display-buttonized t]
2100               ["View all" gnus-mime-view-all-parts t]
2101               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2102               ["Encrypt body" gnus-article-encrypt-body
2103                :active (not (gnus-group-read-only-p))
2104                ,@(if (featurep 'xemacs) nil
2105                    '(:help "Encrypt the message body on disk"))]
2106               ["Extract all parts" gnus-summary-save-parts t]
2107               ("Multipart"
2108                ["Repair multipart" gnus-summary-repair-multipart t]
2109                ["Add buttons" gnus-summary-display-buttonized t]
2110                ["Pipe part" gnus-article-pipe-part t]
2111                ["Inline part" gnus-article-inline-part t]
2112                ["Encrypt body" gnus-article-encrypt-body
2113                 :active (not (gnus-group-read-only-p))
2114                ,@(if (featurep 'xemacs) nil
2115                    '(:help "Encrypt the message body on disk"))]
2116                ["View part externally" gnus-article-view-part-externally t]
2117                ["View part with charset" gnus-article-view-part-as-charset t]
2118                ["Copy part" gnus-article-copy-part t]
2119                ["Save part" gnus-article-save-part t]
2120                ["View part" gnus-article-view-part t]))
2121              ("Date"
2122               ["Local" gnus-article-date-local t]
2123               ["ISO8601" gnus-article-date-iso8601 t]
2124               ["UT" gnus-article-date-ut t]
2125               ["Original" gnus-article-date-original t]
2126               ["Lapsed" gnus-article-date-lapsed t]
2127               ["User-defined" gnus-article-date-user t])
2128              ("Display"
2129               ["Remove images" gnus-article-remove-images t]
2130               ["Toggle smiley" gnus-treat-smiley t]
2131               ["Show X-Face" gnus-article-display-x-face t]
2132               ["Show picons in From" gnus-treat-from-picon t]
2133               ["Show picons in mail headers" gnus-treat-mail-picon t]
2134               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2135               ("View as different encoding"
2136                ,@(gnus-summary-menu-split
2137                   (mapcar
2138                    (lambda (cs)
2139                      ;; Since easymenu under Emacs doesn't allow
2140                      ;; lambda forms for menu commands, we should
2141                      ;; provide intern'ed function symbols.
2142                      (let ((command (intern (format "\
2143 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2144                        (fset command
2145                              `(lambda ()
2146                                 (interactive)
2147                                 (let ((gnus-summary-show-article-charset-alist
2148                                        '((1 . ,cs))))
2149                                   (gnus-summary-show-article 1))))
2150                        `[,(symbol-name cs) ,command t]))
2151                    (sort (if (fboundp 'coding-system-list)
2152                              (coding-system-list)
2153                            (mapcar 'car mm-mime-mule-charset-alist))
2154                          'string<)))))
2155              ("Washing"
2156               ("Remove Blanks"
2157                ["Leading" gnus-article-strip-leading-blank-lines t]
2158                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2159                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2160                ["All of the above" gnus-article-strip-blank-lines t]
2161                ["All" gnus-article-strip-all-blank-lines t]
2162                ["Leading space" gnus-article-strip-leading-space t]
2163                ["Trailing space" gnus-article-strip-trailing-space t]
2164                ["Leading space in headers"
2165                 gnus-article-remove-leading-whitespace t])
2166               ["Overstrike" gnus-article-treat-overstrike t]
2167               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2168               ["Emphasis" gnus-article-emphasize t]
2169               ["Word wrap" gnus-article-fill-cited-article t]
2170               ["Fill long lines" gnus-article-fill-long-lines t]
2171               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2172               ["CR" gnus-article-remove-cr t]
2173               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2174               ["Base64" gnus-article-de-base64-unreadable t]
2175               ["Rot 13" gnus-summary-caesar-message
2176                ,@(if (featurep 'xemacs) '(t)
2177                    '(:help "\"Caesar rotate\" article by 13"))]
2178               ["Morse decode" gnus-summary-morse-message t]
2179               ["Unix pipe..." gnus-summary-pipe-message t]
2180               ["Add buttons" gnus-article-add-buttons t]
2181               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2182               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2183               ["Verbose header" gnus-summary-verbose-headers t]
2184               ["Toggle header" gnus-summary-toggle-header t]
2185               ["Unfold headers" gnus-article-treat-unfold-headers t]
2186               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2187               ["Html" gnus-article-wash-html t]
2188               ["URLs" gnus-article-unsplit-urls t]
2189               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2190               ["HZ" gnus-article-decode-HZ t]
2191               ("(Outlook) Deuglify"
2192                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2193                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2194                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2195                ["Full (Outlook) deuglify"
2196                 gnus-article-outlook-deuglify-article t])
2197               )
2198              ("Output"
2199               ["Save in default format" gnus-summary-save-article
2200                ,@(if (featurep 'xemacs) '(t)
2201                    '(:help "Save article using default method"))]
2202               ["Save in file" gnus-summary-save-article-file
2203                ,@(if (featurep 'xemacs) '(t)
2204                    '(:help "Save article in file"))]
2205               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2206               ["Save in MH folder" gnus-summary-save-article-folder t]
2207               ["Save in VM folder" gnus-summary-save-article-vm t]
2208               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2209               ["Save body in file" gnus-summary-save-article-body-file t]
2210               ["Pipe through a filter" gnus-summary-pipe-output t]
2211               ["Add to SOUP packet" gnus-soup-add-article t]
2212               ["Print with Muttprint" gnus-summary-muttprint t]
2213               ["Print" gnus-summary-print-article t])
2214              ("Backend"
2215               ["Respool article..." gnus-summary-respool-article t]
2216               ["Move article..." gnus-summary-move-article
2217                (gnus-check-backend-function
2218                 'request-move-article gnus-newsgroup-name)]
2219               ["Copy article..." gnus-summary-copy-article t]
2220               ["Crosspost article..." gnus-summary-crosspost-article
2221                (gnus-check-backend-function
2222                 'request-replace-article gnus-newsgroup-name)]
2223               ["Import file..." gnus-summary-import-article
2224                (gnus-check-backend-function
2225                 'request-accept-article gnus-newsgroup-name)]
2226               ["Create article..." gnus-summary-create-article
2227                (gnus-check-backend-function
2228                 'request-accept-article gnus-newsgroup-name)]
2229               ["Check if posted" gnus-summary-article-posted-p t]
2230               ["Edit article" gnus-summary-edit-article
2231                (not (gnus-group-read-only-p))]
2232               ["Delete article" gnus-summary-delete-article
2233                (gnus-check-backend-function
2234                 'request-expire-articles gnus-newsgroup-name)]
2235               ["Query respool" gnus-summary-respool-query t]
2236               ["Trace respool" gnus-summary-respool-trace t]
2237               ["Delete expirable articles" gnus-summary-expire-articles-now
2238                (gnus-check-backend-function
2239                 'request-expire-articles gnus-newsgroup-name)])
2240              ("Extract"
2241               ["Uudecode" gnus-uu-decode-uu
2242                ,@(if (featurep 'xemacs) '(t)
2243                    '(:help "Decode uuencoded article(s)"))]
2244               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2245               ["Unshar" gnus-uu-decode-unshar t]
2246               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2247               ["Save" gnus-uu-decode-save t]
2248               ["Binhex" gnus-uu-decode-binhex t]
2249               ["Postscript" gnus-uu-decode-postscript t]
2250               ["All MIME parts" gnus-summary-save-parts t])
2251              ("Cache"
2252               ["Enter article" gnus-cache-enter-article t]
2253               ["Remove article" gnus-cache-remove-article t])
2254              ["Translate" gnus-article-babel t]
2255              ["Select article buffer" gnus-summary-select-article-buffer t]
2256              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2257              ["Isearch article..." gnus-summary-isearch-article t]
2258              ["Beginning of the article" gnus-summary-beginning-of-article t]
2259              ["End of the article" gnus-summary-end-of-article t]
2260              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2261              ["Fetch referenced articles" gnus-summary-refer-references t]
2262              ["Fetch current thread" gnus-summary-refer-thread t]
2263              ["Fetch article with id..." gnus-summary-refer-article t]
2264              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2265              ["Redisplay" gnus-summary-show-article t]
2266              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2267       (easy-menu-define
2268         gnus-summary-article-menu gnus-summary-mode-map ""
2269         (cons "Article" innards))
2270
2271       (if (not (keymapp gnus-summary-article-menu))
2272           (easy-menu-define
2273             gnus-article-commands-menu gnus-article-mode-map ""
2274             (cons "Commands" innards))
2275         ;; in Emacs, don't share menu.
2276         (setq gnus-article-commands-menu
2277               (copy-keymap gnus-summary-article-menu))
2278         (define-key gnus-article-mode-map [menu-bar commands]
2279           (cons "Commands" gnus-article-commands-menu))))
2280
2281     (easy-menu-define
2282       gnus-summary-thread-menu gnus-summary-mode-map ""
2283       '("Threads"
2284         ["Find all messages in thread" gnus-summary-refer-thread t]
2285         ["Toggle threading" gnus-summary-toggle-threads t]
2286         ["Hide threads" gnus-summary-hide-all-threads t]
2287         ["Show threads" gnus-summary-show-all-threads t]
2288         ["Hide thread" gnus-summary-hide-thread t]
2289         ["Show thread" gnus-summary-show-thread t]
2290         ["Go to next thread" gnus-summary-next-thread t]
2291         ["Go to previous thread" gnus-summary-prev-thread t]
2292         ["Go down thread" gnus-summary-down-thread t]
2293         ["Go up thread" gnus-summary-up-thread t]
2294         ["Top of thread" gnus-summary-top-thread t]
2295         ["Mark thread as read" gnus-summary-kill-thread t]
2296         ["Lower thread score" gnus-summary-lower-thread t]
2297         ["Raise thread score" gnus-summary-raise-thread t]
2298         ["Rethread current" gnus-summary-rethread-current t]))
2299
2300     (easy-menu-define
2301       gnus-summary-post-menu gnus-summary-mode-map ""
2302       `("Post"
2303         ["Send a message (mail or news)" gnus-summary-post-news
2304          ,@(if (featurep 'xemacs) '(t)
2305              '(:help "Post an article"))]
2306         ["Followup" gnus-summary-followup
2307          ,@(if (featurep 'xemacs) '(t)
2308              '(:help "Post followup to this article"))]
2309         ["Followup and yank" gnus-summary-followup-with-original
2310          ,@(if (featurep 'xemacs) '(t)
2311              '(:help "Post followup to this article, quoting its contents"))]
2312         ["Supersede article" gnus-summary-supersede-article t]
2313         ["Cancel article" gnus-summary-cancel-article
2314          ,@(if (featurep 'xemacs) '(t)
2315              '(:help "Cancel an article you posted"))]
2316         ["Reply" gnus-summary-reply t]
2317         ["Reply and yank" gnus-summary-reply-with-original t]
2318         ["Wide reply" gnus-summary-wide-reply t]
2319         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2320          ,@(if (featurep 'xemacs) '(t)
2321              '(:help "Mail a reply, quoting this article"))]
2322         ["Very wide reply" gnus-summary-very-wide-reply t]
2323         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2324          ,@(if (featurep 'xemacs) '(t)
2325              '(:help "Mail a very wide reply, quoting this article"))]
2326         ["Mail forward" gnus-summary-mail-forward t]
2327         ["Post forward" gnus-summary-post-forward t]
2328         ["Digest and mail" gnus-uu-digest-mail-forward t]
2329         ["Digest and post" gnus-uu-digest-post-forward t]
2330         ["Resend message" gnus-summary-resend-message t]
2331         ["Resend message edit" gnus-summary-resend-message-edit t]
2332         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2333         ["Send a mail" gnus-summary-mail-other-window t]
2334         ["Create a local message" gnus-summary-news-other-window t]
2335         ["Uuencode and post" gnus-uu-post-news
2336          ,@(if (featurep 'xemacs) '(t)
2337              '(:help "Post a uuencoded article"))]
2338         ["Followup via news" gnus-summary-followup-to-mail t]
2339         ["Followup via news and yank"
2340          gnus-summary-followup-to-mail-with-original t]
2341         ;;("Draft"
2342         ;;["Send" gnus-summary-send-draft t]
2343         ;;["Send bounced" gnus-resend-bounced-mail t])
2344         ))
2345
2346     (cond
2347      ((not (keymapp gnus-summary-post-menu))
2348       (setq gnus-article-post-menu gnus-summary-post-menu))
2349      ((not gnus-article-post-menu)
2350       ;; Don't share post menu.
2351       (setq gnus-article-post-menu
2352             (copy-keymap gnus-summary-post-menu))))
2353     (define-key gnus-article-mode-map [menu-bar post]
2354       (cons "Post" gnus-article-post-menu))
2355
2356     (easy-menu-define
2357       gnus-summary-misc-menu gnus-summary-mode-map ""
2358       `("Gnus"
2359         ("Mark Read"
2360          ["Mark as read" gnus-summary-mark-as-read-forward t]
2361          ["Mark same subject and select"
2362           gnus-summary-kill-same-subject-and-select t]
2363          ["Mark same subject" gnus-summary-kill-same-subject t]
2364          ["Catchup" gnus-summary-catchup
2365           ,@(if (featurep 'xemacs) '(t)
2366               '(:help "Mark unread articles in this group as read"))]
2367          ["Catchup all" gnus-summary-catchup-all t]
2368          ["Catchup to here" gnus-summary-catchup-to-here t]
2369          ["Catchup from here" gnus-summary-catchup-from-here t]
2370          ["Catchup region" gnus-summary-mark-region-as-read 
2371           (gnus-mark-active-p)]
2372          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2373         ("Mark Various"
2374          ["Tick" gnus-summary-tick-article-forward t]
2375          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2376          ["Remove marks" gnus-summary-clear-mark-forward t]
2377          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2378          ["Set bookmark" gnus-summary-set-bookmark t]
2379          ["Remove bookmark" gnus-summary-remove-bookmark t])
2380         ("Limit to"
2381          ["Marks..." gnus-summary-limit-to-marks t]
2382          ["Subject..." gnus-summary-limit-to-subject t]
2383          ["Author..." gnus-summary-limit-to-author t]
2384          ["Age..." gnus-summary-limit-to-age t]
2385          ["Extra..." gnus-summary-limit-to-extra t]
2386          ["Score..." gnus-summary-limit-to-score t]
2387          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2388          ["Unread" gnus-summary-limit-to-unread t]
2389          ["Unseen" gnus-summary-limit-to-unseen t]
2390          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2391          ["Next articles" gnus-summary-limit-to-articles t]
2392          ["Pop limit" gnus-summary-pop-limit t]
2393          ["Show dormant" gnus-summary-limit-include-dormant t]
2394          ["Hide childless dormant"
2395           gnus-summary-limit-exclude-childless-dormant t]
2396          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2397          ["Hide marked" gnus-summary-limit-exclude-marks t]
2398          ["Show expunged" gnus-summary-limit-include-expunged t])
2399         ("Process Mark"
2400          ["Set mark" gnus-summary-mark-as-processable t]
2401          ["Remove mark" gnus-summary-unmark-as-processable t]
2402          ["Remove all marks" gnus-summary-unmark-all-processable t]
2403          ["Mark above" gnus-uu-mark-over t]
2404          ["Mark series" gnus-uu-mark-series t]
2405          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2406          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2407          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2408          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2409          ["Mark all" gnus-uu-mark-all t]
2410          ["Mark buffer" gnus-uu-mark-buffer t]
2411          ["Mark sparse" gnus-uu-mark-sparse t]
2412          ["Mark thread" gnus-uu-mark-thread t]
2413          ["Unmark thread" gnus-uu-unmark-thread t]
2414          ("Process Mark Sets"
2415           ["Kill" gnus-summary-kill-process-mark t]
2416           ["Yank" gnus-summary-yank-process-mark
2417            gnus-newsgroup-process-stack]
2418           ["Save" gnus-summary-save-process-mark t]
2419           ["Run command on marked..." gnus-summary-universal-argument t]))
2420         ("Scroll article"
2421          ["Page forward" gnus-summary-next-page
2422           ,@(if (featurep 'xemacs) '(t)
2423               '(:help "Show next page of article"))]
2424          ["Page backward" gnus-summary-prev-page
2425           ,@(if (featurep 'xemacs) '(t)
2426               '(:help "Show previous page of article"))]
2427          ["Line forward" gnus-summary-scroll-up t])
2428         ("Move"
2429          ["Next unread article" gnus-summary-next-unread-article t]
2430          ["Previous unread article" gnus-summary-prev-unread-article t]
2431          ["Next article" gnus-summary-next-article t]
2432          ["Previous article" gnus-summary-prev-article t]
2433          ["Next unread subject" gnus-summary-next-unread-subject t]
2434          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2435          ["Next article same subject" gnus-summary-next-same-subject t]
2436          ["Previous article same subject" gnus-summary-prev-same-subject t]
2437          ["First unread article" gnus-summary-first-unread-article t]
2438          ["Best unread article" gnus-summary-best-unread-article t]
2439          ["Go to subject number..." gnus-summary-goto-subject t]
2440          ["Go to article number..." gnus-summary-goto-article t]
2441          ["Go to the last article" gnus-summary-goto-last-article t]
2442          ["Pop article off history" gnus-summary-pop-article t])
2443         ("Sort"
2444          ["Sort by number" gnus-summary-sort-by-number t]
2445          ["Sort by author" gnus-summary-sort-by-author t]
2446          ["Sort by subject" gnus-summary-sort-by-subject t]
2447          ["Sort by date" gnus-summary-sort-by-date t]
2448          ["Sort by score" gnus-summary-sort-by-score t]
2449          ["Sort by lines" gnus-summary-sort-by-lines t]
2450          ["Sort by characters" gnus-summary-sort-by-chars t]
2451          ["Randomize" gnus-summary-sort-by-random t]
2452          ["Original sort" gnus-summary-sort-by-original t])
2453         ("Help"
2454          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2455          ["Describe group" gnus-summary-describe-group t]
2456          ["Fetch charter" gnus-group-fetch-charter
2457           ,@(if (featurep 'xemacs) nil
2458               '(:help "Display the charter of the current group"))]
2459          ["Fetch control message" gnus-group-fetch-control
2460           ,@(if (featurep 'xemacs) nil
2461               '(:help "Display the archived control message for the current group"))]
2462          ["Read manual" gnus-info-find-node t])
2463         ("Modes"
2464          ["Pick and read" gnus-pick-mode t]
2465          ["Binary" gnus-binary-mode t])
2466         ("Regeneration"
2467          ["Regenerate" gnus-summary-prepare t]
2468          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2469          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2470          ["Toggle threading" gnus-summary-toggle-threads t])
2471         ["See old articles" gnus-summary-insert-old-articles t]
2472         ["See new articles" gnus-summary-insert-new-articles t]
2473         ["Filter articles..." gnus-summary-execute-command t]
2474         ["Run command on articles..." gnus-summary-universal-argument t]
2475         ["Search articles forward..." gnus-summary-search-article-forward t]
2476         ["Search articles backward..." gnus-summary-search-article-backward t]
2477         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2478         ["Expand window" gnus-summary-expand-window t]
2479         ["Expire expirable articles" gnus-summary-expire-articles
2480          (gnus-check-backend-function
2481           'request-expire-articles gnus-newsgroup-name)]
2482         ["Edit local kill file" gnus-summary-edit-local-kill t]
2483         ["Edit main kill file" gnus-summary-edit-global-kill t]
2484         ["Edit group parameters" gnus-summary-edit-parameters t]
2485         ["Customize group parameters" gnus-summary-customize-parameters t]
2486         ["Send a bug report" gnus-bug t]
2487         ("Exit"
2488          ["Catchup and exit" gnus-summary-catchup-and-exit
2489           ,@(if (featurep 'xemacs) '(t)
2490               '(:help "Mark unread articles in this group as read, then exit"))]
2491          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2492          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2493          ["Exit group" gnus-summary-exit
2494           ,@(if (featurep 'xemacs) '(t)
2495               '(:help "Exit current group, return to group selection mode"))]
2496          ["Exit group without updating" gnus-summary-exit-no-update t]
2497          ["Exit and goto next group" gnus-summary-next-group t]
2498          ["Exit and goto prev group" gnus-summary-prev-group t]
2499          ["Reselect group" gnus-summary-reselect-current-group t]
2500          ["Rescan group" gnus-summary-rescan-group t]
2501          ["Update dribble" gnus-summary-save-newsrc t])))
2502
2503     (gnus-run-hooks 'gnus-summary-menu-hook)))
2504
2505 (defvar gnus-summary-tool-bar-map nil)
2506
2507 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2508 (defun gnus-summary-make-tool-bar ()
2509   (if (and (fboundp 'tool-bar-add-item-from-menu)
2510            (default-value 'tool-bar-mode)
2511            (not gnus-summary-tool-bar-map))
2512       (setq gnus-summary-tool-bar-map
2513             (let ((tool-bar-map (make-sparse-keymap))
2514                   (load-path (mm-image-load-path)))
2515               (tool-bar-add-item-from-menu
2516                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2517               (tool-bar-add-item-from-menu
2518                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2519               (tool-bar-add-item-from-menu
2520                'gnus-summary-post-news "post" gnus-summary-mode-map)
2521               (tool-bar-add-item-from-menu
2522                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2523               (tool-bar-add-item-from-menu
2524                'gnus-summary-followup "followup" gnus-summary-mode-map)
2525               (tool-bar-add-item-from-menu
2526                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2527               (tool-bar-add-item-from-menu
2528                'gnus-summary-reply "reply" gnus-summary-mode-map)
2529               (tool-bar-add-item-from-menu
2530                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2531               (tool-bar-add-item-from-menu
2532                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2533               (tool-bar-add-item-from-menu
2534                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2535               (tool-bar-add-item-from-menu
2536                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2537               (tool-bar-add-item-from-menu
2538                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2539               (tool-bar-add-item-from-menu
2540                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2541               (tool-bar-add-item-from-menu
2542                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2543               (tool-bar-add-item-from-menu
2544                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2545               tool-bar-map)))
2546   (if gnus-summary-tool-bar-map
2547       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2548
2549 (defun gnus-score-set-default (var value)
2550   "A version of set that updates the GNU Emacs menu-bar."
2551   (set var value)
2552   ;; It is the message that forces the active status to be updated.
2553   (message ""))
2554
2555 (defun gnus-make-score-map (type)
2556   "Make a summary score map of type TYPE."
2557   (if t
2558       nil
2559     (let ((headers '(("author" "from" string)
2560                      ("subject" "subject" string)
2561                      ("article body" "body" string)
2562                      ("article head" "head" string)
2563                      ("xref" "xref" string)
2564                      ("extra header" "extra" string)
2565                      ("lines" "lines" number)
2566                      ("followups to author" "followup" string)))
2567           (types '((number ("less than" <)
2568                            ("greater than" >)
2569                            ("equal" =))
2570                    (string ("substring" s)
2571                            ("exact string" e)
2572                            ("fuzzy string" f)
2573                            ("regexp" r))))
2574           (perms '(("temporary" (current-time-string))
2575                    ("permanent" nil)
2576                    ("immediate" now)))
2577           header)
2578       (list
2579        (apply
2580         'nconc
2581         (list
2582          (if (eq type 'lower)
2583              "Lower score"
2584            "Increase score"))
2585         (let (outh)
2586           (while headers
2587             (setq header (car headers))
2588             (setq outh
2589                   (cons
2590                    (apply
2591                     'nconc
2592                     (list (car header))
2593                     (let ((ts (cdr (assoc (nth 2 header) types)))
2594                           outt)
2595                       (while ts
2596                         (setq outt
2597                               (cons
2598                                (apply
2599                                 'nconc
2600                                 (list (caar ts))
2601                                 (let ((ps perms)
2602                                       outp)
2603                                   (while ps
2604                                     (setq outp
2605                                           (cons
2606                                            (vector
2607                                             (caar ps)
2608                                             (list
2609                                              'gnus-summary-score-entry
2610                                              (nth 1 header)
2611                                              (if (or (string= (nth 1 header)
2612                                                               "head")
2613                                                      (string= (nth 1 header)
2614                                                               "body"))
2615                                                  ""
2616                                                (list 'gnus-summary-header
2617                                                      (nth 1 header)))
2618                                              (list 'quote (nth 1 (car ts)))
2619                                              (list 'gnus-score-delta-default
2620                                                    nil)
2621                                              (nth 1 (car ps))
2622                                              t)
2623                                             t)
2624                                            outp))
2625                                     (setq ps (cdr ps)))
2626                                   (list (nreverse outp))))
2627                                outt))
2628                         (setq ts (cdr ts)))
2629                       (list (nreverse outt))))
2630                    outh))
2631             (setq headers (cdr headers)))
2632           (list (nreverse outh))))))))
2633
2634 \f
2635
2636 (defun gnus-summary-mode (&optional group)
2637   "Major mode for reading articles.
2638
2639 All normal editing commands are switched off.
2640 \\<gnus-summary-mode-map>
2641 Each line in this buffer represents one article.  To read an
2642 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2643 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2644 respectively.
2645
2646 You can also post articles and send mail from this buffer.  To
2647 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2648 of an article, type `\\[gnus-summary-reply]'.
2649
2650 There are approx. one gazillion commands you can execute in this
2651 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2652
2653 The following commands are available:
2654
2655 \\{gnus-summary-mode-map}"
2656   (interactive)
2657   (kill-all-local-variables)
2658   (when (gnus-visual-p 'summary-menu 'menu)
2659     (gnus-summary-make-menu-bar)
2660     (gnus-summary-make-tool-bar))
2661   (gnus-summary-make-local-variables)
2662   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2663     (gnus-summary-make-local-variables))
2664   (gnus-make-thread-indent-array)
2665   (gnus-simplify-mode-line)
2666   (setq major-mode 'gnus-summary-mode)
2667   (setq mode-name "Summary")
2668   (make-local-variable 'minor-mode-alist)
2669   (use-local-map gnus-summary-mode-map)
2670   (buffer-disable-undo)
2671   (setq buffer-read-only t)             ;Disable modification
2672   (setq truncate-lines t)
2673   (setq selective-display t)
2674   (setq selective-display-ellipses t)   ;Display `...'
2675   (gnus-summary-set-display-table)
2676   (gnus-set-default-directory)
2677   (setq gnus-newsgroup-name group)
2678   (make-local-variable 'gnus-summary-line-format)
2679   (make-local-variable 'gnus-summary-line-format-spec)
2680   (make-local-variable 'gnus-summary-dummy-line-format)
2681   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2682   (make-local-variable 'gnus-summary-mark-positions)
2683   (gnus-make-local-hook 'pre-command-hook)
2684   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2685   (gnus-run-hooks 'gnus-summary-mode-hook)
2686   (turn-on-gnus-mailing-list-mode)
2687   (mm-enable-multibyte)
2688   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2689   (gnus-update-summary-mark-positions))
2690
2691 (defun gnus-summary-make-local-variables ()
2692   "Make all the local summary buffer variables."
2693   (let (global)
2694     (dolist (local gnus-summary-local-variables)
2695       (if (consp local)
2696           (progn
2697             (if (eq (cdr local) 'global)
2698                 ;; Copy the global value of the variable.
2699                 (setq global (symbol-value (car local)))
2700               ;; Use the value from the list.
2701               (setq global (eval (cdr local))))
2702             (set (make-local-variable (car local)) global))
2703         ;; Simple nil-valued local variable.
2704         (set (make-local-variable local) nil)))))
2705
2706 (defun gnus-summary-clear-local-variables ()
2707   (let ((locals gnus-summary-local-variables))
2708     (while locals
2709       (if (consp (car locals))
2710           (and (vectorp (caar locals))
2711                (set (caar locals) nil))
2712         (and (vectorp (car locals))
2713              (set (car locals) nil)))
2714       (setq locals (cdr locals)))))
2715
2716 ;; Summary data functions.
2717
2718 (defmacro gnus-data-number (data)
2719   `(car ,data))
2720
2721 (defmacro gnus-data-set-number (data number)
2722   `(setcar ,data ,number))
2723
2724 (defmacro gnus-data-mark (data)
2725   `(nth 1 ,data))
2726
2727 (defmacro gnus-data-set-mark (data mark)
2728   `(setcar (nthcdr 1 ,data) ,mark))
2729
2730 (defmacro gnus-data-pos (data)
2731   `(nth 2 ,data))
2732
2733 (defmacro gnus-data-set-pos (data pos)
2734   `(setcar (nthcdr 2 ,data) ,pos))
2735
2736 (defmacro gnus-data-header (data)
2737   `(nth 3 ,data))
2738
2739 (defmacro gnus-data-set-header (data header)
2740   `(setf (nth 3 ,data) ,header))
2741
2742 (defmacro gnus-data-level (data)
2743   `(nth 4 ,data))
2744
2745 (defmacro gnus-data-unread-p (data)
2746   `(= (nth 1 ,data) gnus-unread-mark))
2747
2748 (defmacro gnus-data-read-p (data)
2749   `(/= (nth 1 ,data) gnus-unread-mark))
2750
2751 (defmacro gnus-data-pseudo-p (data)
2752   `(consp (nth 3 ,data)))
2753
2754 (defmacro gnus-data-find (number)
2755   `(assq ,number gnus-newsgroup-data))
2756
2757 (defmacro gnus-data-find-list (number &optional data)
2758   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2759      (memq (assq ,number bdata)
2760            bdata)))
2761
2762 (defmacro gnus-data-make (number mark pos header level)
2763   `(list ,number ,mark ,pos ,header ,level))
2764
2765 (defun gnus-data-enter (after-article number mark pos header level offset)
2766   (let ((data (gnus-data-find-list after-article)))
2767     (unless data
2768       (error "No such article: %d" after-article))
2769     (setcdr data (cons (gnus-data-make number mark pos header level)
2770                        (cdr data)))
2771     (setq gnus-newsgroup-data-reverse nil)
2772     (gnus-data-update-list (cddr data) offset)))
2773
2774 (defun gnus-data-enter-list (after-article list &optional offset)
2775   (when list
2776     (let ((data (and after-article (gnus-data-find-list after-article)))
2777           (ilist list))
2778       (if (not (or data
2779                    after-article))
2780           (let ((odata gnus-newsgroup-data))
2781             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2782             (when offset
2783               (gnus-data-update-list odata offset)))
2784       ;; Find the last element in the list to be spliced into the main
2785         ;; list.
2786         (while (cdr list)
2787           (setq list (cdr list)))
2788         (if (not data)
2789             (progn
2790               (setcdr list gnus-newsgroup-data)
2791               (setq gnus-newsgroup-data ilist)
2792               (when offset
2793                 (gnus-data-update-list (cdr list) offset)))
2794           (setcdr list (cdr data))
2795           (setcdr data ilist)
2796           (when offset
2797             (gnus-data-update-list (cdr list) offset))))
2798       (setq gnus-newsgroup-data-reverse nil))))
2799
2800 (defun gnus-data-remove (article &optional offset)
2801   (let ((data gnus-newsgroup-data))
2802     (if (= (gnus-data-number (car data)) article)
2803         (progn
2804           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2805                 gnus-newsgroup-data-reverse nil)
2806           (when offset
2807             (gnus-data-update-list gnus-newsgroup-data offset)))
2808       (while (cdr data)
2809         (when (= (gnus-data-number (cadr data)) article)
2810           (setcdr data (cddr data))
2811           (when offset
2812             (gnus-data-update-list (cdr data) offset))
2813           (setq data nil
2814                 gnus-newsgroup-data-reverse nil))
2815         (setq data (cdr data))))))
2816
2817 (defmacro gnus-data-list (backward)
2818   `(if ,backward
2819        (or gnus-newsgroup-data-reverse
2820            (setq gnus-newsgroup-data-reverse
2821                  (reverse gnus-newsgroup-data)))
2822      gnus-newsgroup-data))
2823
2824 (defun gnus-data-update-list (data offset)
2825   "Add OFFSET to the POS of all data entries in DATA."
2826   (setq gnus-newsgroup-data-reverse nil)
2827   (while data
2828     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2829     (setq data (cdr data))))
2830
2831 (defun gnus-summary-article-pseudo-p (article)
2832   "Say whether this article is a pseudo article or not."
2833   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2834
2835 (defmacro gnus-summary-article-sparse-p (article)
2836   "Say whether this article is a sparse article or not."
2837   `(memq ,article gnus-newsgroup-sparse))
2838
2839 (defmacro gnus-summary-article-ancient-p (article)
2840   "Say whether this article is a sparse article or not."
2841   `(memq ,article gnus-newsgroup-ancient))
2842
2843 (defun gnus-article-parent-p (number)
2844   "Say whether this article is a parent or not."
2845   (let ((data (gnus-data-find-list number)))
2846     (and (cdr data)              ; There has to be an article after...
2847          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2848             (gnus-data-level (nth 1 data))))))
2849
2850 (defun gnus-article-children (number)
2851   "Return a list of all children to NUMBER."
2852   (let* ((data (gnus-data-find-list number))
2853          (level (gnus-data-level (car data)))
2854          children)
2855     (setq data (cdr data))
2856     (while (and data
2857                 (= (gnus-data-level (car data)) (1+ level)))
2858       (push (gnus-data-number (car data)) children)
2859       (setq data (cdr data)))
2860     children))
2861
2862 (defmacro gnus-summary-skip-intangible ()
2863   "If the current article is intangible, then jump to a different article."
2864   '(let ((to (get-text-property (point) 'gnus-intangible)))
2865      (and to (gnus-summary-goto-subject to))))
2866
2867 (defmacro gnus-summary-article-intangible-p ()
2868   "Say whether this article is intangible or not."
2869   '(get-text-property (point) 'gnus-intangible))
2870
2871 (defun gnus-article-read-p (article)
2872   "Say whether ARTICLE is read or not."
2873   (not (or (memq article gnus-newsgroup-marked)
2874            (memq article gnus-newsgroup-spam-marked)
2875            (memq article gnus-newsgroup-unreads)
2876            (memq article gnus-newsgroup-unselected)
2877            (memq article gnus-newsgroup-dormant))))
2878
2879 ;; Some summary mode macros.
2880
2881 (defmacro gnus-summary-article-number ()
2882   "The article number of the article on the current line.
2883 If there isn't an article number here, then we return the current
2884 article number."
2885   '(progn
2886      (gnus-summary-skip-intangible)
2887      (or (get-text-property (point) 'gnus-number)
2888          (gnus-summary-last-subject))))
2889
2890 (defmacro gnus-summary-article-header (&optional number)
2891   "Return the header of article NUMBER."
2892   `(gnus-data-header (gnus-data-find
2893                       ,(or number '(gnus-summary-article-number)))))
2894
2895 (defmacro gnus-summary-thread-level (&optional number)
2896   "Return the level of thread that starts with article NUMBER."
2897   `(if (and (eq gnus-summary-make-false-root 'dummy)
2898             (get-text-property (point) 'gnus-intangible))
2899        0
2900      (gnus-data-level (gnus-data-find
2901                        ,(or number '(gnus-summary-article-number))))))
2902
2903 (defmacro gnus-summary-article-mark (&optional number)
2904   "Return the mark of article NUMBER."
2905   `(gnus-data-mark (gnus-data-find
2906                     ,(or number '(gnus-summary-article-number)))))
2907
2908 (defmacro gnus-summary-article-pos (&optional number)
2909   "Return the position of the line of article NUMBER."
2910   `(gnus-data-pos (gnus-data-find
2911                    ,(or number '(gnus-summary-article-number)))))
2912
2913 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2914 (defmacro gnus-summary-article-subject (&optional number)
2915   "Return current subject string or nil if nothing."
2916   `(let ((headers
2917           ,(if number
2918                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2919              '(gnus-data-header (assq (gnus-summary-article-number)
2920                                       gnus-newsgroup-data)))))
2921      (and headers
2922           (vectorp headers)
2923           (mail-header-subject headers))))
2924
2925 (defmacro gnus-summary-article-score (&optional number)
2926   "Return current article score."
2927   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2928                   gnus-newsgroup-scored))
2929        gnus-summary-default-score 0))
2930
2931 (defun gnus-summary-article-children (&optional number)
2932   "Return a list of article numbers that are children of article NUMBER."
2933   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2934          (level (gnus-data-level (car data)))
2935          l children)
2936     (while (and (setq data (cdr data))
2937                 (> (setq l (gnus-data-level (car data))) level))
2938       (and (= (1+ level) l)
2939            (push (gnus-data-number (car data))
2940                  children)))
2941     (nreverse children)))
2942
2943 (defun gnus-summary-article-parent (&optional number)
2944   "Return the article number of the parent of article NUMBER."
2945   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2946                                     (gnus-data-list t)))
2947          (level (gnus-data-level (car data))))
2948     (if (zerop level)
2949         ()                              ; This is a root.
2950       ;; We search until we find an article with a level less than
2951       ;; this one.  That function has to be the parent.
2952       (while (and (setq data (cdr data))
2953                   (not (< (gnus-data-level (car data)) level))))
2954       (and data (gnus-data-number (car data))))))
2955
2956 (defun gnus-unread-mark-p (mark)
2957   "Say whether MARK is the unread mark."
2958   (= mark gnus-unread-mark))
2959
2960 (defun gnus-read-mark-p (mark)
2961   "Say whether MARK is one of the marks that mark as read.
2962 This is all marks except unread, ticked, dormant, and expirable."
2963   (not (or (= mark gnus-unread-mark)
2964            (= mark gnus-ticked-mark)
2965            (= mark gnus-spam-mark)
2966            (= mark gnus-dormant-mark)
2967            (= mark gnus-expirable-mark))))
2968
2969 (defmacro gnus-article-mark (number)
2970   "Return the MARK of article NUMBER.
2971 This macro should only be used when computing the mark the \"first\"
2972 time; i.e., when generating the summary lines.  After that,
2973 `gnus-summary-article-mark' should be used to examine the
2974 marks of articles."
2975   `(cond
2976     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2977     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2978     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2979     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2980     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2981     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2982     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2983     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2984            gnus-ancient-mark))))
2985
2986 ;; Saving hidden threads.
2987
2988 (defmacro gnus-save-hidden-threads (&rest forms)
2989   "Save hidden threads, eval FORMS, and restore the hidden threads."
2990   (let ((config (make-symbol "config")))
2991     `(let ((,config (gnus-hidden-threads-configuration)))
2992        (unwind-protect
2993            (save-excursion
2994              ,@forms)
2995          (gnus-restore-hidden-threads-configuration ,config)))))
2996 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2997 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2998
2999 (defun gnus-data-compute-positions ()
3000   "Compute the positions of all articles."
3001   (setq gnus-newsgroup-data-reverse nil)
3002   (let ((data gnus-newsgroup-data))
3003     (save-excursion
3004       (gnus-save-hidden-threads
3005         (gnus-summary-show-all-threads)
3006         (goto-char (point-min))
3007         (while data
3008           (while (get-text-property (point) 'gnus-intangible)
3009             (forward-line 1))
3010           (gnus-data-set-pos (car data) (+ (point) 3))
3011           (setq data (cdr data))
3012           (forward-line 1))))))
3013
3014 (defun gnus-hidden-threads-configuration ()
3015   "Return the current hidden threads configuration."
3016   (save-excursion
3017     (let (config)
3018       (goto-char (point-min))
3019       (while (search-forward "\r" nil t)
3020         (push (1- (point)) config))
3021       config)))
3022
3023 (defun gnus-restore-hidden-threads-configuration (config)
3024   "Restore hidden threads configuration from CONFIG."
3025   (save-excursion
3026     (let (point buffer-read-only)
3027       (while (setq point (pop config))
3028         (when (and (< point (point-max))
3029                    (goto-char point)
3030                    (eq (char-after) ?\n))
3031           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3032
3033 ;; Various summary mode internalish functions.
3034
3035 (defun gnus-mouse-pick-article (e)
3036   (interactive "e")
3037   (mouse-set-point e)
3038   (gnus-summary-next-page nil t))
3039
3040 (defun gnus-summary-set-display-table ()
3041   "Change the display table.
3042 Odd characters have a tendency to mess
3043 up nicely formatted displays - we make all possible glyphs
3044 display only a single character."
3045
3046   ;; We start from the standard display table, if any.
3047   (let ((table (or (copy-sequence standard-display-table)
3048                    (make-display-table)))
3049         (i 32))
3050     ;; Nix out all the control chars...
3051     (while (>= (setq i (1- i)) 0)
3052       (aset table i [??]))
3053    ;; ... but not newline and cr, of course.  (cr is necessary for the
3054     ;; selective display).
3055     (aset table ?\n nil)
3056     (aset table ?\r nil)
3057     ;; We keep TAB as well.
3058     (aset table ?\t nil)
3059     ;; We nix out any glyphs over 126 that are not set already.
3060     (let ((i 256))
3061       (while (>= (setq i (1- i)) 127)
3062         ;; Only modify if the entry is nil.
3063         (unless (aref table i)
3064           (aset table i [??]))))
3065     (setq buffer-display-table table)))
3066
3067 (defun gnus-summary-set-article-display-arrow (pos)
3068   "Update the overlay arrow to point to line at position POS."
3069   (when (and gnus-summary-display-arrow
3070              (boundp 'overlay-arrow-position)
3071              (boundp 'overlay-arrow-string))
3072     (save-excursion
3073       (goto-char pos)
3074       (beginning-of-line)
3075       (unless overlay-arrow-position
3076         (setq overlay-arrow-position (make-marker)))
3077       (setq overlay-arrow-string "=>"
3078             overlay-arrow-position (set-marker overlay-arrow-position
3079                                                (point)
3080                                                (current-buffer))))))
3081
3082 (defun gnus-summary-setup-buffer (group)
3083   "Initialize summary buffer."
3084   (let ((buffer (gnus-summary-buffer-name group))
3085         (dead-name (concat "*Dead Summary "
3086                            (gnus-group-decoded-name group) "*")))
3087     ;; If a dead summary buffer exists, we kill it.
3088     (when (gnus-buffer-live-p dead-name)
3089       (gnus-kill-buffer dead-name))
3090     (if (get-buffer buffer)
3091         (progn
3092           (set-buffer buffer)
3093           (setq gnus-summary-buffer (current-buffer))
3094           (not gnus-newsgroup-prepared))
3095       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3096       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3097       (gnus-summary-mode group)
3098       (when gnus-carpal
3099         (gnus-carpal-setup-buffer 'summary))
3100       (unless gnus-single-article-buffer
3101         (make-local-variable 'gnus-article-buffer)
3102         (make-local-variable 'gnus-article-current)
3103         (make-local-variable 'gnus-original-article-buffer))
3104       (setq gnus-newsgroup-name group)
3105       ;; Set any local variables in the group parameters.
3106       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3107       t)))
3108
3109 (defun gnus-set-global-variables ()
3110   "Set the global equivalents of the buffer-local variables.
3111 They are set to the latest values they had.  These reflect the summary
3112 buffer that was in action when the last article was fetched."
3113   (when (eq major-mode 'gnus-summary-mode)
3114     (setq gnus-summary-buffer (current-buffer))
3115     (let ((name gnus-newsgroup-name)
3116           (marked gnus-newsgroup-marked)
3117           (spam gnus-newsgroup-spam-marked)
3118           (unread gnus-newsgroup-unreads)
3119           (headers gnus-current-headers)
3120           (data gnus-newsgroup-data)
3121           (summary gnus-summary-buffer)
3122           (article-buffer gnus-article-buffer)
3123           (original gnus-original-article-buffer)
3124           (gac gnus-article-current)
3125           (reffed gnus-reffed-article-number)
3126           (score-file gnus-current-score-file)
3127           (default-charset gnus-newsgroup-charset)
3128           vlist)
3129       (let ((locals gnus-newsgroup-variables))
3130         (while locals
3131           (if (consp (car locals))
3132               (push (eval (caar locals)) vlist)
3133             (push (eval (car locals)) vlist))
3134           (setq locals (cdr locals)))
3135         (setq vlist (nreverse vlist)))
3136       (save-excursion
3137         (set-buffer gnus-group-buffer)
3138         (setq gnus-newsgroup-name name
3139               gnus-newsgroup-marked marked
3140               gnus-newsgroup-spam-marked spam
3141               gnus-newsgroup-unreads unread
3142               gnus-current-headers headers
3143               gnus-newsgroup-data data
3144               gnus-article-current gac
3145               gnus-summary-buffer summary
3146               gnus-article-buffer article-buffer
3147               gnus-original-article-buffer original
3148               gnus-reffed-article-number reffed
3149               gnus-current-score-file score-file
3150               gnus-newsgroup-charset default-charset)
3151         (let ((locals gnus-newsgroup-variables))
3152           (while locals
3153             (if (consp (car locals))
3154                 (set (caar locals) (pop vlist))
3155               (set (car locals) (pop vlist)))
3156             (setq locals (cdr locals))))
3157         ;; The article buffer also has local variables.
3158         (when (gnus-buffer-live-p gnus-article-buffer)
3159           (set-buffer gnus-article-buffer)
3160           (setq gnus-summary-buffer summary))))))
3161
3162 (defun gnus-summary-article-unread-p (article)
3163   "Say whether ARTICLE is unread or not."
3164   (memq article gnus-newsgroup-unreads))
3165
3166 (defun gnus-summary-first-article-p (&optional article)
3167   "Return whether ARTICLE is the first article in the buffer."
3168   (if (not (setq article (or article (gnus-summary-article-number))))
3169       nil
3170     (eq article (caar gnus-newsgroup-data))))
3171
3172 (defun gnus-summary-last-article-p (&optional article)
3173   "Return whether ARTICLE is the last article in the buffer."
3174   (if (not (setq article (or article (gnus-summary-article-number))))
3175       ;; All non-existent numbers are the last article.  :-)
3176       t
3177     (not (cdr (gnus-data-find-list article)))))
3178
3179 (defun gnus-make-thread-indent-array ()
3180   (let ((n 200))
3181     (unless (and gnus-thread-indent-array
3182                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3183       (setq gnus-thread-indent-array (make-vector 201 "")
3184             gnus-thread-indent-array-level gnus-thread-indent-level)
3185       (while (>= n 0)
3186         (aset gnus-thread-indent-array n
3187               (make-string (* n gnus-thread-indent-level) ? ))
3188         (setq n (1- n))))))
3189
3190 (defun gnus-update-summary-mark-positions ()
3191   "Compute where the summary marks are to go."
3192   (save-excursion
3193     (when (gnus-buffer-exists-p gnus-summary-buffer)
3194       (set-buffer gnus-summary-buffer))
3195     (let ((gnus-replied-mark 129)
3196           (gnus-score-below-mark 130)
3197           (gnus-score-over-mark 130)
3198           (gnus-undownloaded-mark 131)
3199           (spec gnus-summary-line-format-spec)
3200           gnus-visual pos)
3201       (save-excursion
3202         (gnus-set-work-buffer)
3203         (let ((gnus-summary-line-format-spec spec)
3204               (gnus-newsgroup-downloadable '(0)))
3205           (gnus-summary-insert-line
3206            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3207            0 nil t 128 t nil "" nil 1)
3208           (goto-char (point-min))
3209           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3210                                              (- (point) (point-min) 1)))))
3211           (goto-char (point-min))
3212           (push (cons 'replied (and (search-forward "\201" nil t)
3213                                     (- (point) (point-min) 1)))
3214                 pos)
3215           (goto-char (point-min))
3216           (push (cons 'score (and (search-forward "\202" nil t)
3217                                   (- (point) (point-min) 1)))
3218                 pos)
3219           (goto-char (point-min))
3220           (push (cons 'download
3221                       (and (search-forward "\203" nil t)
3222                            (- (point) (point-min) 1)))
3223                 pos)))
3224       (setq gnus-summary-mark-positions pos))))
3225
3226 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3227   "Insert a dummy root in the summary buffer."
3228   (beginning-of-line)
3229   (gnus-add-text-properties
3230    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3231    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3232
3233 (defun gnus-summary-extract-address-component (from)
3234   (or (car (funcall gnus-extract-address-components from))
3235       from))
3236
3237 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3238   (let ((mail-parse-charset gnus-newsgroup-charset)
3239         ; Is it really necessary to do this next part for each summary line?
3240         ; Luckily, doesn't seem to slow things down much.
3241         (mail-parse-ignored-charsets
3242          (save-excursion (set-buffer gnus-summary-buffer)
3243                          gnus-newsgroup-ignored-charsets)))
3244     (or
3245      (and gnus-ignored-from-addresses
3246           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3247           (let ((extra-headers (mail-header-extra header))
3248                 to
3249                 newsgroups)
3250             (cond
3251              ((setq to (cdr (assq 'To extra-headers)))
3252               (concat "-> "
3253                       (inline
3254                         (gnus-summary-extract-address-component
3255                          (funcall gnus-decode-encoded-word-function to)))))
3256              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3257               (concat "=> " newsgroups)))))
3258      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3259
3260 (defun gnus-summary-insert-line (gnus-tmp-header
3261                                  gnus-tmp-level gnus-tmp-current
3262                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3263                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3264                                  &optional gnus-tmp-dummy gnus-tmp-score
3265                                  gnus-tmp-process)
3266   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3267          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3268          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3269          (gnus-tmp-score-char
3270           (if (or (null gnus-summary-default-score)
3271                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3272                       gnus-summary-zcore-fuzz))
3273               ?                         ;Whitespace
3274             (if (< gnus-tmp-score gnus-summary-default-score)
3275                 gnus-score-below-mark gnus-score-over-mark)))
3276          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3277          (gnus-tmp-replied
3278           (cond (gnus-tmp-process gnus-process-mark)
3279                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3280                  gnus-cached-mark)
3281                 (gnus-tmp-replied gnus-replied-mark)
3282                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3283                  gnus-forwarded-mark)
3284                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3285                  gnus-saved-mark)
3286                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3287                  gnus-recent-mark)
3288                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3289                  gnus-unseen-mark)
3290                 (t gnus-no-mark)))
3291          (gnus-tmp-downloaded
3292           (cond (undownloaded 
3293                  gnus-undownloaded-mark)
3294                 (gnus-newsgroup-agentized
3295                  gnus-downloaded-mark)
3296                 (t
3297                  gnus-no-mark)))
3298          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3299          (gnus-tmp-name
3300           (cond
3301            ((string-match "<[^>]+> *$" gnus-tmp-from)
3302             (let ((beg (match-beginning 0)))
3303               (or (and (string-match "^\".+\"" gnus-tmp-from)
3304                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3305                   (substring gnus-tmp-from 0 beg))))
3306            ((string-match "(.+)" gnus-tmp-from)
3307             (substring gnus-tmp-from
3308                        (1+ (match-beginning 0)) (1- (match-end 0))))
3309            (t gnus-tmp-from)))
3310          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3311          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3312          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3313          (buffer-read-only nil))
3314     (when (string= gnus-tmp-name "")
3315       (setq gnus-tmp-name gnus-tmp-from))
3316     (unless (numberp gnus-tmp-lines)
3317       (setq gnus-tmp-lines -1))
3318     (if (= gnus-tmp-lines -1)
3319         (setq gnus-tmp-lines "?")
3320       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines))) 
3321       (gnus-put-text-property
3322      (point)
3323      (progn (eval gnus-summary-line-format-spec) (point))
3324        'gnus-number gnus-tmp-number)
3325     (when (gnus-visual-p 'summary-highlight 'highlight)
3326       (forward-line -1)
3327       (gnus-run-hooks 'gnus-summary-update-hook)
3328       (forward-line 1))))
3329
3330 (defun gnus-summary-update-line (&optional dont-update)
3331   "Update summary line after change."
3332   (when (and gnus-summary-default-score
3333              (not gnus-summary-inhibit-highlight))
3334     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3335            (article (gnus-summary-article-number))
3336            (score (gnus-summary-article-score article)))
3337       (unless dont-update
3338         (if (and gnus-summary-mark-below
3339                  (< (gnus-summary-article-score)
3340                     gnus-summary-mark-below))
3341             ;; This article has a low score, so we mark it as read.
3342             (when (memq article gnus-newsgroup-unreads)
3343               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3344           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3345             ;; This article was previously marked as read on account
3346             ;; of a low score, but now it has risen, so we mark it as
3347             ;; unread.
3348             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3349         (gnus-summary-update-mark
3350          (if (or (null gnus-summary-default-score)
3351                  (<= (abs (- score gnus-summary-default-score))
3352                      gnus-summary-zcore-fuzz))
3353              ?                          ;Whitespace
3354            (if (< score gnus-summary-default-score)
3355                gnus-score-below-mark gnus-score-over-mark))
3356          'score))
3357       ;; Do visual highlighting.
3358       (when (gnus-visual-p 'summary-highlight 'highlight)
3359         (gnus-run-hooks 'gnus-summary-update-hook)))))
3360
3361 (defvar gnus-tmp-new-adopts nil)
3362
3363 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3364   "Return the number of articles in THREAD.
3365 This may be 0 in some cases -- if none of the articles in
3366 the thread are to be displayed."
3367   (let* ((number
3368          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3369           (cond
3370            ((not (listp thread))
3371             1)
3372            ((and (consp thread) (cdr thread))
3373             (apply
3374              '+ 1 (mapcar
3375                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3376            ((null thread)
3377             1)
3378            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3379             1)
3380            (t 0))))
3381     (when (and level (zerop level) gnus-tmp-new-adopts)
3382       (incf number
3383             (apply '+ (mapcar
3384                        'gnus-summary-number-of-articles-in-thread
3385                        gnus-tmp-new-adopts))))
3386     (if char
3387         (if (> number 1) gnus-not-empty-thread-mark
3388           gnus-empty-thread-mark)
3389       number)))
3390
3391 (defsubst gnus-summary-line-message-size (head)
3392   "Return pretty-printed version of message size.
3393 This function is intended to be used in
3394 `gnus-summary-line-format-alist'."
3395   (let ((c (or (mail-header-chars head) -1)))
3396     (cond ((< c 0) "n/a")               ; chars not available
3397           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3398           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3399           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3400           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3401
3402
3403 (defun gnus-summary-set-local-parameters (group)
3404   "Go through the local params of GROUP and set all variable specs in that list."
3405   (let ((params (gnus-group-find-parameter group))
3406         (vars '(quit-config))           ; Ignore quit-config.
3407         elem)
3408     (while params
3409       (setq elem (car params)
3410             params (cdr params))
3411       (and (consp elem)                 ; Has to be a cons.
3412            (consp (cdr elem))           ; The cdr has to be a list.
3413            (symbolp (car elem))         ; Has to be a symbol in there.
3414            (not (memq (car elem) vars))
3415            (ignore-errors               ; So we set it.
3416              (push (car elem) vars)
3417              (make-local-variable (car elem))
3418              (set (car elem) (eval (nth 1 elem))))))))
3419
3420 (defun gnus-summary-read-group (group &optional show-all no-article
3421                                       kill-buffer no-display backward
3422                                       select-articles)
3423   "Start reading news in newsgroup GROUP.
3424 If SHOW-ALL is non-nil, already read articles are also listed.
3425 If NO-ARTICLE is non-nil, no article is selected initially.
3426 If NO-DISPLAY, don't generate a summary buffer."
3427   (let (result)
3428     (while (and group
3429                 (null (setq result
3430                             (let ((gnus-auto-select-next nil))
3431                               (or (gnus-summary-read-group-1
3432                                    group show-all no-article
3433                                    kill-buffer no-display
3434                                    select-articles)
3435                                   (setq show-all nil
3436                                         select-articles nil)))))
3437                 (eq gnus-auto-select-next 'quietly))
3438       (set-buffer gnus-group-buffer)
3439       ;; The entry function called above goes to the next
3440       ;; group automatically, so we go two groups back
3441       ;; if we are searching for the previous group.
3442       (when backward
3443         (gnus-group-prev-unread-group 2))
3444       (if (not (equal group (gnus-group-group-name)))
3445           (setq group (gnus-group-group-name))
3446         (setq group nil)))
3447     result))
3448
3449 (defun gnus-summary-read-group-1 (group show-all no-article
3450                                         kill-buffer no-display
3451                                         &optional select-articles)
3452   ;; Killed foreign groups can't be entered.
3453   ;;  (when (and (not (gnus-group-native-p group))
3454   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3455   ;;    (error "Dead non-native groups can't be entered"))
3456   (gnus-message 5 "Retrieving newsgroup: %s..."
3457                 (gnus-group-decoded-name group))
3458   (let* ((new-group (gnus-summary-setup-buffer group))
3459          (quit-config (gnus-group-quit-config group))
3460          (did-select (and new-group (gnus-select-newsgroup
3461                                      group show-all select-articles))))
3462     (cond
3463      ;; This summary buffer exists already, so we just select it.
3464      ((not new-group)
3465       (gnus-set-global-variables)
3466       (when kill-buffer
3467         (gnus-kill-or-deaden-summary kill-buffer))
3468       (gnus-configure-windows 'summary 'force)
3469       (gnus-set-mode-line 'summary)
3470       (gnus-summary-position-point)
3471       (message "")
3472       t)
3473      ;; We couldn't select this group.
3474      ((null did-select)
3475       (when (and (eq major-mode 'gnus-summary-mode)
3476                  (not (equal (current-buffer) kill-buffer)))
3477         (kill-buffer (current-buffer))
3478         (if (not quit-config)
3479             (progn
3480               ;; Update the info -- marks might need to be removed,
3481               ;; for instance.
3482               (gnus-summary-update-info)
3483               (set-buffer gnus-group-buffer)
3484               (gnus-group-jump-to-group group)
3485               (gnus-group-next-unread-group 1))
3486           (gnus-handle-ephemeral-exit quit-config)))
3487       (let ((grpinfo (gnus-get-info group)))
3488         (if (null (gnus-info-read grpinfo))
3489             (gnus-message 3 "Group %s contains no messages"
3490                           (gnus-group-decoded-name group))
3491           (gnus-message 3 "Can't select group")))
3492       nil)
3493      ;; The user did a `C-g' while prompting for number of articles,
3494      ;; so we exit this group.
3495      ((eq did-select 'quit)
3496       (and (eq major-mode 'gnus-summary-mode)
3497            (not (equal (current-buffer) kill-buffer))
3498            (kill-buffer (current-buffer)))
3499       (when kill-buffer
3500         (gnus-kill-or-deaden-summary kill-buffer))
3501       (if (not quit-config)
3502           (progn
3503             (set-buffer gnus-group-buffer)
3504             (gnus-group-jump-to-group group)
3505             (gnus-group-next-unread-group 1)
3506             (gnus-configure-windows 'group 'force))
3507         (gnus-handle-ephemeral-exit quit-config))
3508       ;; Finally signal the quit.
3509       (signal 'quit nil))
3510      ;; The group was successfully selected.
3511      (t
3512       (gnus-set-global-variables)
3513       ;; Save the active value in effect when the group was entered.
3514       (setq gnus-newsgroup-active
3515             (gnus-copy-sequence
3516              (gnus-active gnus-newsgroup-name)))
3517       ;; You can change the summary buffer in some way with this hook.
3518       (gnus-run-hooks 'gnus-select-group-hook)
3519       (gnus-update-format-specifications
3520        nil 'summary 'summary-mode 'summary-dummy)
3521       (gnus-update-summary-mark-positions)
3522       ;; Do score processing.
3523       (when gnus-use-scoring
3524         (gnus-possibly-score-headers))
3525       ;; Check whether to fill in the gaps in the threads.
3526       (when gnus-build-sparse-threads
3527         (gnus-build-sparse-threads))
3528       ;; Find the initial limit.
3529       (if gnus-show-threads
3530           (if show-all
3531               (let ((gnus-newsgroup-dormant nil))
3532                 (gnus-summary-initial-limit show-all))
3533             (gnus-summary-initial-limit show-all))
3534         ;; When unthreaded, all articles are always shown.
3535         (setq gnus-newsgroup-limit
3536               (mapcar
3537                (lambda (header) (mail-header-number header))
3538                gnus-newsgroup-headers)))
3539       ;; Generate the summary buffer.
3540       (unless no-display
3541         (gnus-summary-prepare))
3542       (when gnus-use-trees
3543         (gnus-tree-open group)
3544         (setq gnus-summary-highlight-line-function
3545               'gnus-tree-highlight-article))
3546       ;; If the summary buffer is empty, but there are some low-scored
3547       ;; articles or some excluded dormants, we include these in the
3548       ;; buffer.
3549       (when (and (zerop (buffer-size))
3550                  (not no-display))
3551         (cond (gnus-newsgroup-dormant
3552                (gnus-summary-limit-include-dormant))
3553               ((and gnus-newsgroup-scored show-all)
3554                (gnus-summary-limit-include-expunged t))))
3555       ;; Function `gnus-apply-kill-file' must be called in this hook.
3556       (gnus-run-hooks 'gnus-apply-kill-hook)
3557       (if (and (zerop (buffer-size))
3558                (not no-display))
3559           (progn
3560             ;; This newsgroup is empty.
3561             (gnus-summary-catchup-and-exit nil t)
3562             (gnus-message 6 "No unread news")
3563             (when kill-buffer
3564               (gnus-kill-or-deaden-summary kill-buffer))
3565             ;; Return nil from this function.
3566             nil)
3567         ;; Hide conversation thread subtrees.  We cannot do this in
3568         ;; gnus-summary-prepare-hook since kill processing may not
3569         ;; work with hidden articles.
3570         (gnus-summary-maybe-hide-threads)
3571         (when kill-buffer
3572           (gnus-kill-or-deaden-summary kill-buffer))
3573         (gnus-summary-auto-select-subject)
3574         ;; Show first unread article if requested.
3575         (if (and (not no-article)
3576                  (not no-display)
3577                  gnus-newsgroup-unreads
3578                  gnus-auto-select-first)
3579             (progn
3580               (gnus-configure-windows 'summary)
3581               (let ((art (gnus-summary-article-number)))
3582                 (unless (and (not gnus-plugged)
3583                              (or (memq art gnus-newsgroup-undownloaded)
3584                                  (memq art gnus-newsgroup-downloadable)))
3585                   (gnus-summary-goto-article art))))
3586           ;; Don't select any articles.
3587           (gnus-summary-position-point)
3588           (gnus-configure-windows 'summary 'force)
3589           (gnus-set-mode-line 'summary))
3590         (when (and gnus-auto-center-group
3591                    (get-buffer-window gnus-group-buffer t))
3592           ;; Gotta use windows, because recenter does weird stuff if
3593           ;; the current buffer ain't the displayed window.
3594           (let ((owin (selected-window)))
3595             (select-window (get-buffer-window gnus-group-buffer t))
3596             (when (gnus-group-goto-group group)
3597               (recenter))
3598             (select-window owin)))
3599         ;; Mark this buffer as "prepared".
3600         (setq gnus-newsgroup-prepared t)
3601         (gnus-run-hooks 'gnus-summary-prepared-hook)
3602         (unless (gnus-ephemeral-group-p group)
3603           (gnus-group-update-group group))
3604         t)))))
3605
3606 (defun gnus-summary-auto-select-subject ()
3607   "Select the subject line on initial group entry."
3608   (goto-char (point-min))
3609   (cond
3610    ((eq gnus-auto-select-subject 'best)
3611     (gnus-summary-best-unread-subject))
3612    ((eq gnus-auto-select-subject 'unread)
3613     (gnus-summary-first-unread-subject))
3614    ((eq gnus-auto-select-subject 'unseen)
3615     (gnus-summary-first-unseen-subject))
3616    ((eq gnus-auto-select-subject 'unseen-or-unread)
3617     (gnus-summary-first-unseen-or-unread-subject))
3618    ((eq gnus-auto-select-subject 'first)
3619     ;; Do nothing.
3620     )
3621    ((functionp gnus-auto-select-subject)
3622     (funcall gnus-auto-select-subject))))
3623
3624 (defun gnus-summary-prepare ()
3625   "Generate the summary buffer."
3626   (interactive)
3627   (let ((buffer-read-only nil))
3628     (erase-buffer)
3629     (setq gnus-newsgroup-data nil
3630           gnus-newsgroup-data-reverse nil)
3631     (gnus-run-hooks 'gnus-summary-generate-hook)
3632     ;; Generate the buffer, either with threads or without.
3633     (when gnus-newsgroup-headers
3634       (gnus-summary-prepare-threads
3635        (if gnus-show-threads
3636            (gnus-sort-gathered-threads
3637             (funcall gnus-summary-thread-gathering-function
3638                      (gnus-sort-threads
3639                       (gnus-cut-threads (gnus-make-threads)))))
3640          ;; Unthreaded display.
3641          (gnus-sort-articles gnus-newsgroup-headers))))
3642     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3643     ;; Call hooks for modifying summary buffer.
3644     (goto-char (point-min))
3645     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3646
3647 (defsubst gnus-general-simplify-subject (subject)
3648   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3649   (setq subject
3650         (cond
3651          ;; Truncate the subject.
3652          (gnus-simplify-subject-functions
3653           (gnus-map-function gnus-simplify-subject-functions subject))
3654          ((numberp gnus-summary-gather-subject-limit)
3655           (setq subject (gnus-simplify-subject-re subject))
3656           (if (> (length subject) gnus-summary-gather-subject-limit)
3657               (substring subject 0 gnus-summary-gather-subject-limit)
3658             subject))
3659          ;; Fuzzily simplify it.
3660          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3661           (gnus-simplify-subject-fuzzy subject))
3662          ;; Just remove the leading "Re:".
3663          (t
3664           (gnus-simplify-subject-re subject))))
3665
3666   (if (and gnus-summary-gather-exclude-subject
3667            (string-match gnus-summary-gather-exclude-subject subject))
3668       nil                         ; This article shouldn't be gathered
3669     subject))
3670
3671 (defun gnus-summary-simplify-subject-query ()
3672   "Query where the respool algorithm would put this article."
3673   (interactive)
3674   (gnus-summary-select-article)
3675   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3676
3677 (defun gnus-gather-threads-by-subject (threads)
3678   "Gather threads by looking at Subject headers."
3679   (if (not gnus-summary-make-false-root)
3680       threads
3681     (let ((hashtb (gnus-make-hashtable 1024))
3682           (prev threads)
3683           (result threads)
3684           subject hthread whole-subject)
3685       (while threads
3686         (setq subject (gnus-general-simplify-subject
3687                        (setq whole-subject (mail-header-subject
3688                                             (caar threads)))))
3689         (when subject
3690           (if (setq hthread (gnus-gethash subject hashtb))
3691               (progn
3692                 ;; We enter a dummy root into the thread, if we
3693                 ;; haven't done that already.
3694                 (unless (stringp (caar hthread))
3695                   (setcar hthread (list whole-subject (car hthread))))
3696                 ;; We add this new gathered thread to this gathered
3697                 ;; thread.
3698                 (setcdr (car hthread)
3699                         (nconc (cdar hthread) (list (car threads))))
3700                 ;; Remove it from the list of threads.
3701                 (setcdr prev (cdr threads))
3702                 (setq threads prev))
3703             ;; Enter this thread into the hash table.
3704             (gnus-sethash subject
3705                           (if gnus-summary-make-false-root-always
3706                               (progn
3707                                 ;; If you want a dummy root above all
3708                                 ;; threads...
3709                                 (setcar threads (list whole-subject
3710                                                       (car threads)))
3711                                 threads)
3712                             threads)
3713                           hashtb)))
3714         (setq prev threads)
3715         (setq threads (cdr threads)))
3716       result)))
3717
3718 (defun gnus-gather-threads-by-references (threads)
3719   "Gather threads by looking at References headers."
3720   (let ((idhashtb (gnus-make-hashtable 1024))
3721         (thhashtb (gnus-make-hashtable 1024))
3722         (prev threads)
3723         (result threads)
3724         ids references id gthread gid entered ref)
3725     (while threads
3726       (when (setq references (mail-header-references (caar threads)))
3727         (setq id (mail-header-id (caar threads))
3728               ids (inline (gnus-split-references references))
3729               entered nil)
3730         (while (setq ref (pop ids))
3731           (setq ids (delete ref ids))
3732           (if (not (setq gid (gnus-gethash ref idhashtb)))
3733               (progn
3734                 (gnus-sethash ref id idhashtb)
3735                 (gnus-sethash id threads thhashtb))
3736             (setq gthread (gnus-gethash gid thhashtb))
3737             (unless entered
3738               ;; We enter a dummy root into the thread, if we
3739               ;; haven't done that already.
3740               (unless (stringp (caar gthread))
3741                 (setcar gthread (list (mail-header-subject (caar gthread))
3742                                       (car gthread))))
3743               ;; We add this new gathered thread to this gathered
3744               ;; thread.
3745               (setcdr (car gthread)
3746                       (nconc (cdar gthread) (list (car threads)))))
3747             ;; Add it into the thread hash table.
3748             (gnus-sethash id gthread thhashtb)
3749             (setq entered t)
3750             ;; Remove it from the list of threads.
3751             (setcdr prev (cdr threads))
3752             (setq threads prev))))
3753       (setq prev threads)
3754       (setq threads (cdr threads)))
3755     result))
3756
3757 (defun gnus-sort-gathered-threads (threads)
3758   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3759   (let ((result threads))
3760     (while threads
3761       (when (stringp (caar threads))
3762         (setcdr (car threads)
3763                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3764       (setq threads (cdr threads)))
3765     result))
3766
3767 (defun gnus-thread-loop-p (root thread)
3768   "Say whether ROOT is in THREAD."
3769   (let ((stack (list thread))
3770         (infloop 0)
3771         th)
3772     (while (setq thread (pop stack))
3773       (setq th (cdr thread))
3774       (while (and th
3775                   (not (eq (caar th) root)))
3776         (pop th))
3777       (if th
3778           ;; We have found a loop.
3779           (let (ref-dep)
3780             (setcdr thread (delq (car th) (cdr thread)))
3781             (if (boundp (setq ref-dep (intern "none"
3782                                               gnus-newsgroup-dependencies)))
3783                 (setcdr (symbol-value ref-dep)
3784                         (nconc (cdr (symbol-value ref-dep))
3785                                (list (car th))))
3786               (set ref-dep (list nil (car th))))
3787             (setq infloop 1
3788                   stack nil))
3789         ;; Push all the subthreads onto the stack.
3790         (push (cdr thread) stack)))
3791     infloop))
3792
3793 (defun gnus-make-threads ()
3794   "Go through the dependency hashtb and find the roots.  Return all threads."
3795   (let (threads)
3796     (while (catch 'infloop
3797              (mapatoms
3798               (lambda (refs)
3799                 ;; Deal with self-referencing References loops.
3800                 (when (and (car (symbol-value refs))
3801                            (not (zerop
3802                                  (apply
3803                                   '+
3804                                   (mapcar
3805                                    (lambda (thread)
3806                                      (gnus-thread-loop-p
3807                                       (car (symbol-value refs)) thread))
3808                                    (cdr (symbol-value refs)))))))
3809                   (setq threads nil)
3810                   (throw 'infloop t))
3811                 (unless (car (symbol-value refs))
3812                   ;; These threads do not refer back to any other
3813                   ;; articles, so they're roots.
3814                   (setq threads (append (cdr (symbol-value refs)) threads))))
3815               gnus-newsgroup-dependencies)))
3816     threads))
3817
3818 ;; Build the thread tree.
3819 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3820   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3821
3822 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3823 if it was already present.
3824
3825 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3826 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3827 Message-IDs will be renamed to a unique Message-ID before being
3828 entered.
3829
3830 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3831   (let* ((id (mail-header-id header))
3832          (id-dep (and id (intern id dependencies)))
3833          parent-id ref ref-dep ref-header replaced)
3834     ;; Enter this `header' in the `dependencies' table.
3835     (cond
3836      ((not id-dep)
3837       (setq header nil))
3838      ;; The first two cases do the normal part: enter a new `header'
3839      ;; in the `dependencies' table.
3840      ((not (boundp id-dep))
3841       (set id-dep (list header)))
3842      ((null (car (symbol-value id-dep)))
3843       (setcar (symbol-value id-dep) header))
3844
3845      ;; From here the `header' was already present in the
3846      ;; `dependencies' table.
3847      (force-new
3848       ;; Overrides an existing entry;
3849       ;; just set the header part of the entry.
3850       (setcar (symbol-value id-dep) header)
3851       (setq replaced t))
3852
3853      ;; Renames the existing `header' to a unique Message-ID.
3854      ((not gnus-summary-ignore-duplicates)
3855       ;; An article with this Message-ID has already been seen.
3856       ;; We rename the Message-ID.
3857       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3858            (list header))
3859       (mail-header-set-id header id))
3860
3861      ;; The last case ignores an existing entry, except it adds any
3862      ;; additional Xrefs (in case the two articles came from different
3863      ;; servers.
3864      ;; Also sets `header' to `nil' meaning that the `dependencies'
3865      ;; table was *not* modified.
3866      (t
3867       (mail-header-set-xref
3868        (car (symbol-value id-dep))
3869        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3870                    "")
3871                (or (mail-header-xref header) "")))
3872       (setq header nil)))
3873
3874     (when (and header (not replaced))
3875       ;; First check that we are not creating a References loop.
3876       (setq parent-id (gnus-parent-id (mail-header-references header)))
3877       (setq ref parent-id)
3878       (while (and ref
3879                   (setq ref-dep (intern-soft ref dependencies))
3880                   (boundp ref-dep)
3881                   (setq ref-header (car (symbol-value ref-dep))))
3882         (if (string= id ref)
3883             ;; Yuk!  This is a reference loop.  Make the article be a
3884             ;; root article.
3885             (progn
3886               (mail-header-set-references (car (symbol-value id-dep)) "none")
3887               (setq ref nil)
3888               (setq parent-id nil))
3889           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3890       (setq ref-dep (intern (or parent-id "none") dependencies))
3891       (if (boundp ref-dep)
3892           (setcdr (symbol-value ref-dep)
3893                   (nconc (cdr (symbol-value ref-dep))
3894                          (list (symbol-value id-dep))))
3895         (set ref-dep (list nil (symbol-value id-dep)))))
3896     header))
3897
3898 (defun gnus-extract-message-id-from-in-reply-to (string)
3899   (if (string-match "<[^>]+>" string)
3900       (substring string (match-beginning 0) (match-end 0))
3901     nil))
3902
3903 (defun gnus-build-sparse-threads ()
3904   (let ((headers gnus-newsgroup-headers)
3905         (mail-parse-charset gnus-newsgroup-charset)
3906         (gnus-summary-ignore-duplicates t)
3907         header references generation relations
3908         subject child end new-child date)
3909     ;; First we create an alist of generations/relations, where
3910     ;; generations is how much we trust the relation, and the relation
3911     ;; is parent/child.
3912     (gnus-message 7 "Making sparse threads...")
3913     (save-excursion
3914       (nnheader-set-temp-buffer " *gnus sparse threads*")
3915       (while (setq header (pop headers))
3916         (when (and (setq references (mail-header-references header))
3917                    (not (string= references "")))
3918           (insert references)
3919           (setq child (mail-header-id header)
3920                 subject (mail-header-subject header)
3921                 date (mail-header-date header)
3922                 generation 0)
3923           (while (search-backward ">" nil t)
3924             (setq end (1+ (point)))
3925             (when (search-backward "<" nil t)
3926               (setq new-child (buffer-substring (point) end))
3927               (push (list (incf generation)
3928                           child (setq child new-child)
3929                           subject date)
3930                     relations)))
3931           (when child
3932             (push (list (1+ generation) child nil subject) relations))
3933           (erase-buffer)))
3934       (kill-buffer (current-buffer)))
3935     ;; Sort over trustworthiness.
3936     (mapcar
3937      (lambda (relation)
3938        (when (gnus-dependencies-add-header
3939               (make-full-mail-header
3940                gnus-reffed-article-number
3941                (nth 3 relation) "" (or (nth 4 relation) "")
3942                (nth 1 relation)
3943                (or (nth 2 relation) "") 0 0 "")
3944               gnus-newsgroup-dependencies nil)
3945          (push gnus-reffed-article-number gnus-newsgroup-limit)
3946          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3947          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3948                gnus-newsgroup-reads)
3949          (decf gnus-reffed-article-number)))
3950      (sort relations 'car-less-than-car))
3951     (gnus-message 7 "Making sparse threads...done")))
3952
3953 (defun gnus-build-old-threads ()
3954   ;; Look at all the articles that refer back to old articles, and
3955   ;; fetch the headers for the articles that aren't there.  This will
3956   ;; build complete threads - if the roots haven't been expired by the
3957   ;; server, that is.
3958   (let ((mail-parse-charset gnus-newsgroup-charset)
3959         id heads)
3960     (mapatoms
3961      (lambda (refs)
3962        (when (not (car (symbol-value refs)))
3963          (setq heads (cdr (symbol-value refs)))
3964          (while heads
3965            (if (memq (mail-header-number (caar heads))
3966                      gnus-newsgroup-dormant)
3967                (setq heads (cdr heads))
3968              (setq id (symbol-name refs))
3969              (while (and (setq id (gnus-build-get-header id))
3970                          (not (car (gnus-id-to-thread id)))))
3971              (setq heads nil)))))
3972      gnus-newsgroup-dependencies)))
3973
3974 ;; This function has to be called with point after the article number
3975 ;; on the beginning of the line.
3976 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3977   (let ((eol (gnus-point-at-eol))
3978         (buffer (current-buffer))
3979         header references in-reply-to)
3980
3981     ;; overview: [num subject from date id refs chars lines misc]
3982     (unwind-protect
3983         (let (x)
3984           (narrow-to-region (point) eol)
3985           (unless (eobp)
3986             (forward-char))
3987
3988           (setq header
3989                 (make-full-mail-header
3990                  number                 ; number
3991                  (condition-case ()     ; subject
3992                      (funcall gnus-decode-encoded-word-function
3993                               (setq x (nnheader-nov-field)))
3994                    (error x))
3995                  (condition-case ()     ; from
3996                      (funcall gnus-decode-encoded-word-function
3997                               (setq x (nnheader-nov-field)))
3998                    (error x))
3999                  (nnheader-nov-field)   ; date
4000                  (nnheader-nov-read-message-id) ; id
4001                  (setq references (nnheader-nov-field)) ; refs
4002                  (nnheader-nov-read-integer) ; chars
4003                  (nnheader-nov-read-integer) ; lines
4004                  (unless (eobp)
4005                    (if (looking-at "Xref: ")
4006                        (goto-char (match-end 0)))
4007                    (nnheader-nov-field)) ; Xref
4008                  (nnheader-nov-parse-extra)))) ; extra
4009
4010       (widen))
4011
4012     (when (and (string= references "")
4013                (setq in-reply-to (mail-header-extra header))
4014                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4015       (mail-header-set-references
4016        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4017
4018     (when gnus-alter-header-function
4019       (funcall gnus-alter-header-function header))
4020     (gnus-dependencies-add-header header dependencies force-new)))
4021
4022 (defun gnus-build-get-header (id)
4023   "Look through the buffer of NOV lines and find the header to ID.
4024 Enter this line into the dependencies hash table, and return
4025 the id of the parent article (if any)."
4026   (let ((deps gnus-newsgroup-dependencies)
4027         found header)
4028     (prog1
4029         (save-excursion
4030           (set-buffer nntp-server-buffer)
4031           (let ((case-fold-search nil))
4032             (goto-char (point-min))
4033             (while (and (not found)
4034                         (search-forward id nil t))
4035               (beginning-of-line)
4036               (setq found (looking-at
4037                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4038                                    (regexp-quote id))))
4039               (or found (beginning-of-line 2)))
4040             (when found
4041               (beginning-of-line)
4042               (and
4043                (setq header (gnus-nov-parse-line
4044                              (read (current-buffer)) deps))
4045                (gnus-parent-id (mail-header-references header))))))
4046       (when header
4047         (let ((number (mail-header-number header)))
4048           (push number gnus-newsgroup-limit)
4049           (push header gnus-newsgroup-headers)
4050           (if (memq number gnus-newsgroup-unselected)
4051               (progn
4052                 (setq gnus-newsgroup-unreads
4053                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4054                                                number))
4055                 (setq gnus-newsgroup-unselected
4056                       (delq number gnus-newsgroup-unselected)))
4057             (push number gnus-newsgroup-ancient)))))))
4058
4059 (defun gnus-build-all-threads ()
4060   "Read all the headers."
4061   (let ((gnus-summary-ignore-duplicates t)
4062         (mail-parse-charset gnus-newsgroup-charset)
4063         (dependencies gnus-newsgroup-dependencies)
4064         header article)
4065     (save-excursion
4066       (set-buffer nntp-server-buffer)
4067       (let ((case-fold-search nil))
4068         (goto-char (point-min))
4069         (while (not (eobp))
4070           (ignore-errors
4071             (setq article (read (current-buffer))
4072                   header (gnus-nov-parse-line article dependencies)))
4073           (when header
4074             (save-excursion
4075               (set-buffer gnus-summary-buffer)
4076               (push header gnus-newsgroup-headers)
4077               (if (memq (setq article (mail-header-number header))
4078                         gnus-newsgroup-unselected)
4079                   (progn
4080                     (setq gnus-newsgroup-unreads
4081                           (gnus-add-to-sorted-list
4082                            gnus-newsgroup-unreads article))
4083                     (setq gnus-newsgroup-unselected
4084                           (delq article gnus-newsgroup-unselected)))
4085                 (push article gnus-newsgroup-ancient)))
4086             (forward-line 1)))))))
4087
4088 (defun gnus-summary-update-article-line (article header)
4089   "Update the line for ARTICLE using HEADERS."
4090   (let* ((id (mail-header-id header))
4091          (thread (gnus-id-to-thread id)))
4092     (unless thread
4093       (error "Article in no thread"))
4094     ;; Update the thread.
4095     (setcar thread header)
4096     (gnus-summary-goto-subject article)
4097     (let* ((datal (gnus-data-find-list article))
4098            (data (car datal))
4099            (buffer-read-only nil)
4100            (level (gnus-summary-thread-level)))
4101       (gnus-delete-line)
4102       (let ((inserted (- (point)
4103                          (progn
4104                            (gnus-summary-insert-line
4105                             header level nil 
4106                             (memq article gnus-newsgroup-undownloaded)
4107                             (gnus-article-mark article)
4108                             (memq article gnus-newsgroup-replied)
4109                             (memq article gnus-newsgroup-expirable)
4110                             ;; Only insert the Subject string when it's different
4111                             ;; from the previous Subject string.
4112                             (if (and
4113                                  gnus-show-threads
4114                                  (gnus-subject-equal
4115                                   (condition-case ()
4116                                       (mail-header-subject
4117                                        (gnus-data-header
4118                                         (cadr
4119                                          (gnus-data-find-list
4120                                           article
4121                                           (gnus-data-list t)))))
4122                                     ;; Error on the side of excessive subjects.
4123                                     (error ""))
4124                                   (mail-header-subject header)))
4125                                 ""
4126                               (mail-header-subject header))
4127                             nil (cdr (assq article gnus-newsgroup-scored))
4128                             (memq article gnus-newsgroup-processable))
4129                            (point)))))
4130         (when (cdr datal)
4131           (gnus-data-update-list
4132            (cdr datal) 
4133            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4134
4135 (defun gnus-summary-update-article (article &optional iheader)
4136   "Update ARTICLE in the summary buffer."
4137   (set-buffer gnus-summary-buffer)
4138   (let* ((header (gnus-summary-article-header article))
4139          (id (mail-header-id header))
4140          (data (gnus-data-find article))
4141          (thread (gnus-id-to-thread id))
4142          (references (mail-header-references header))
4143          (parent
4144           (gnus-id-to-thread
4145            (or (gnus-parent-id
4146                 (when (and references
4147                            (not (equal "" references)))
4148                   references))
4149                "none")))
4150          (buffer-read-only nil)
4151          (old (car thread)))
4152     (when thread
4153       (unless iheader
4154         (setcar thread nil)
4155         (when parent
4156           (delq thread parent)))
4157       (if (gnus-summary-insert-subject id header)
4158           ;; Set the (possibly) new article number in the data structure.
4159           (gnus-data-set-number data (gnus-id-to-article id))
4160         (setcar thread old)
4161         nil))))
4162
4163 (defun gnus-rebuild-thread (id &optional line)
4164   "Rebuild the thread containing ID.
4165 If LINE, insert the rebuilt thread starting on line LINE."
4166   (let ((buffer-read-only nil)
4167         old-pos current thread data)
4168     (if (not gnus-show-threads)
4169         (setq thread (list (car (gnus-id-to-thread id))))
4170       ;; Get the thread this article is part of.
4171       (setq thread (gnus-remove-thread id)))
4172     (setq old-pos (gnus-point-at-bol))
4173     (setq current (save-excursion
4174                     (and (re-search-backward "[\r\n]" nil t)
4175                          (gnus-summary-article-number))))
4176     ;; If this is a gathered thread, we have to go some re-gathering.
4177     (when (stringp (car thread))
4178       (let ((subject (car thread))
4179             roots thr)
4180         (setq thread (cdr thread))
4181         (while thread
4182           (unless (memq (setq thr (gnus-id-to-thread
4183                                    (gnus-root-id
4184                                     (mail-header-id (caar thread)))))
4185                         roots)
4186             (push thr roots))
4187           (setq thread (cdr thread)))
4188         ;; We now have all (unique) roots.
4189         (if (= (length roots) 1)
4190             ;; All the loose roots are now one solid root.
4191             (setq thread (car roots))
4192           (setq thread (cons subject (gnus-sort-threads roots))))))
4193     (let (threads)
4194       ;; We then insert this thread into the summary buffer.
4195       (when line
4196         (goto-char (point-min))
4197         (forward-line (1- line)))
4198       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4199         (if gnus-show-threads
4200             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4201           (gnus-summary-prepare-unthreaded thread))
4202         (setq data (nreverse gnus-newsgroup-data))
4203         (setq threads gnus-newsgroup-threads))
4204       ;; We splice the new data into the data structure.
4205       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4206       ;;!!! then we want to insert at the beginning of the buffer.
4207       ;;!!! That happens to be true with Gnus now, but that may
4208       ;;!!! change in the future.  Perhaps.
4209       (gnus-data-enter-list
4210        (if line nil current) data (- (point) old-pos))
4211       (setq gnus-newsgroup-threads
4212             (nconc threads gnus-newsgroup-threads))
4213       (gnus-data-compute-positions))))
4214
4215 (defun gnus-number-to-header (number)
4216   "Return the header for article NUMBER."
4217   (let ((headers gnus-newsgroup-headers))
4218     (while (and headers
4219                 (not (= number (mail-header-number (car headers)))))
4220       (pop headers))
4221     (when headers
4222       (car headers))))
4223
4224 (defun gnus-parent-headers (in-headers &optional generation)
4225   "Return the headers of the GENERATIONeth parent of HEADERS."
4226   (unless generation
4227     (setq generation 1))
4228   (let ((parent t)
4229         (headers in-headers)
4230         references)
4231     (while (and parent
4232                 (not (zerop generation))
4233                 (setq references (mail-header-references headers)))
4234       (setq headers (if (and references
4235                              (setq parent (gnus-parent-id references)))
4236                         (car (gnus-id-to-thread parent))
4237                       nil))
4238       (decf generation))
4239     (and (not (eq headers in-headers))
4240          headers)))
4241
4242 (defun gnus-id-to-thread (id)
4243   "Return the (sub-)thread where ID appears."
4244   (gnus-gethash id gnus-newsgroup-dependencies))
4245
4246 (defun gnus-id-to-article (id)
4247   "Return the article number of ID."
4248   (let ((thread (gnus-id-to-thread id)))
4249     (when (and thread
4250                (car thread))
4251       (mail-header-number (car thread)))))
4252
4253 (defun gnus-id-to-header (id)
4254   "Return the article headers of ID."
4255   (car (gnus-id-to-thread id)))
4256
4257 (defun gnus-article-displayed-root-p (article)
4258   "Say whether ARTICLE is a root(ish) article."
4259   (let ((level (gnus-summary-thread-level article))
4260         (refs (mail-header-references  (gnus-summary-article-header article)))
4261         particle)
4262     (cond
4263      ((null level) nil)
4264      ((zerop level) t)
4265      ((null refs) t)
4266      ((null (gnus-parent-id refs)) t)
4267      ((and (= 1 level)
4268            (null (setq particle (gnus-id-to-article
4269                                  (gnus-parent-id refs))))
4270            (null (gnus-summary-thread-level particle)))))))
4271
4272 (defun gnus-root-id (id)
4273   "Return the id of the root of the thread where ID appears."
4274   (let (last-id prev)
4275     (while (and id (setq prev (car (gnus-id-to-thread id))))
4276       (setq last-id id
4277             id (gnus-parent-id (mail-header-references prev))))
4278     last-id))
4279
4280 (defun gnus-articles-in-thread (thread)
4281   "Return the list of articles in THREAD."
4282   (cons (mail-header-number (car thread))
4283         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4284
4285 (defun gnus-remove-thread (id &optional dont-remove)
4286   "Remove the thread that has ID in it."
4287   (let (headers thread last-id)
4288     ;; First go up in this thread until we find the root.
4289     (setq last-id (gnus-root-id id)
4290           headers (message-flatten-list (gnus-id-to-thread last-id)))
4291     ;; We have now found the real root of this thread.  It might have
4292     ;; been gathered into some loose thread, so we have to search
4293     ;; through the threads to find the thread we wanted.
4294     (let ((threads gnus-newsgroup-threads)
4295           sub)
4296       (while threads
4297         (setq sub (car threads))
4298         (if (stringp (car sub))
4299             ;; This is a gathered thread, so we look at the roots
4300             ;; below it to find whether this article is in this
4301             ;; gathered root.
4302             (progn
4303               (setq sub (cdr sub))
4304               (while sub
4305                 (when (member (caar sub) headers)
4306                   (setq thread (car threads)
4307                         threads nil
4308                         sub nil))
4309                 (setq sub (cdr sub))))
4310           ;; It's an ordinary thread, so we check it.
4311           (when (eq (car sub) (car headers))
4312             (setq thread sub
4313                   threads nil)))
4314         (setq threads (cdr threads)))
4315       ;; If this article is in no thread, then it's a root.
4316       (if thread
4317           (unless dont-remove
4318             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4319         (setq thread (gnus-id-to-thread last-id)))
4320       (when thread
4321         (prog1
4322             thread                      ; We return this thread.
4323           (unless dont-remove
4324             (if (stringp (car thread))
4325                 (progn
4326                   ;; If we use dummy roots, then we have to remove the
4327                   ;; dummy root as well.
4328                   (when (eq gnus-summary-make-false-root 'dummy)
4329                     ;; We go to the dummy root by going to
4330                     ;; the first sub-"thread", and then one line up.
4331                     (gnus-summary-goto-article
4332                      (mail-header-number (caadr thread)))
4333                     (forward-line -1)
4334                     (gnus-delete-line)
4335                     (gnus-data-compute-positions))
4336                   (setq thread (cdr thread))
4337                   (while thread
4338                     (gnus-remove-thread-1 (car thread))
4339                     (setq thread (cdr thread))))
4340               (gnus-remove-thread-1 thread))))))))
4341
4342 (defun gnus-remove-thread-1 (thread)
4343   "Remove the thread THREAD recursively."
4344   (let ((number (mail-header-number (pop thread)))
4345         d)
4346     (setq thread (reverse thread))
4347     (while thread
4348       (gnus-remove-thread-1 (pop thread)))
4349     (when (setq d (gnus-data-find number))
4350       (goto-char (gnus-data-pos d))
4351       (gnus-summary-show-thread)
4352       (gnus-data-remove
4353        number
4354        (- (gnus-point-at-bol)
4355           (prog1
4356               (1+ (gnus-point-at-eol))
4357             (gnus-delete-line)))))))
4358
4359 (defun gnus-sort-threads-1 (threads func)
4360   (sort (mapcar (lambda (thread)
4361                   (cons (car thread)
4362                         (and (cdr thread)
4363                              (gnus-sort-threads-1 (cdr thread) func))))
4364                 threads) func))
4365
4366 (defun gnus-sort-threads (threads)
4367   "Sort THREADS."
4368   (if (not gnus-thread-sort-functions)
4369       threads
4370     (gnus-message 8 "Sorting threads...")
4371     (let ((max-lisp-eval-depth 5000))
4372       (prog1 (gnus-sort-threads-1
4373          threads
4374          (gnus-make-sort-function gnus-thread-sort-functions))
4375         (gnus-message 8 "Sorting threads...done")))))
4376
4377 (defun gnus-sort-articles (articles)
4378   "Sort ARTICLES."
4379   (when gnus-article-sort-functions
4380     (gnus-message 7 "Sorting articles...")
4381     (prog1
4382         (setq gnus-newsgroup-headers
4383               (sort articles (gnus-make-sort-function
4384                               gnus-article-sort-functions)))
4385       (gnus-message 7 "Sorting articles...done"))))
4386
4387 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4388 (defmacro gnus-thread-header (thread)
4389   "Return header of first article in THREAD.
4390 Note that THREAD must never, ever be anything else than a variable -
4391 using some other form will lead to serious barfage."
4392   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4393   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4394   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4395         (vector thread) 2))
4396
4397 (defsubst gnus-article-sort-by-number (h1 h2)
4398   "Sort articles by article number."
4399   (< (mail-header-number h1)
4400      (mail-header-number h2)))
4401
4402 (defun gnus-thread-sort-by-number (h1 h2)
4403   "Sort threads by root article number."
4404   (gnus-article-sort-by-number
4405    (gnus-thread-header h1) (gnus-thread-header h2)))
4406
4407 (defsubst gnus-article-sort-by-random (h1 h2)
4408   "Sort articles by article number."
4409   (zerop (random 2)))
4410
4411 (defun gnus-thread-sort-by-random (h1 h2)
4412   "Sort threads by root article number."
4413   (gnus-article-sort-by-random
4414    (gnus-thread-header h1) (gnus-thread-header h2)))
4415
4416 (defsubst gnus-article-sort-by-lines (h1 h2)
4417   "Sort articles by article Lines header."
4418   (< (mail-header-lines h1)
4419      (mail-header-lines h2)))
4420
4421 (defun gnus-thread-sort-by-lines (h1 h2)
4422   "Sort threads by root article Lines header."
4423   (gnus-article-sort-by-lines
4424    (gnus-thread-header h1) (gnus-thread-header h2)))
4425
4426 (defsubst gnus-article-sort-by-chars (h1 h2)
4427   "Sort articles by octet length."
4428   (< (mail-header-chars h1)
4429      (mail-header-chars h2)))
4430
4431 (defun gnus-thread-sort-by-chars (h1 h2)
4432   "Sort threads by root article octet length."
4433   (gnus-article-sort-by-chars
4434    (gnus-thread-header h1) (gnus-thread-header h2)))
4435
4436 (defsubst gnus-article-sort-by-author (h1 h2)
4437   "Sort articles by root author."
4438   (string-lessp
4439    (let ((extract (funcall
4440                    gnus-extract-address-components
4441                    (mail-header-from h1))))
4442      (or (car extract) (cadr extract) ""))
4443    (let ((extract (funcall
4444                    gnus-extract-address-components
4445                    (mail-header-from h2))))
4446      (or (car extract) (cadr extract) ""))))
4447
4448 (defun gnus-thread-sort-by-author (h1 h2)
4449   "Sort threads by root author."
4450   (gnus-article-sort-by-author
4451    (gnus-thread-header h1)  (gnus-thread-header h2)))
4452
4453 (defsubst gnus-article-sort-by-subject (h1 h2)
4454   "Sort articles by root subject."
4455   (string-lessp
4456    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4457    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4458
4459 (defun gnus-thread-sort-by-subject (h1 h2)
4460   "Sort threads by root subject."
4461   (gnus-article-sort-by-subject
4462    (gnus-thread-header h1) (gnus-thread-header h2)))
4463
4464 (defsubst gnus-article-sort-by-date (h1 h2)
4465   "Sort articles by root article date."
4466   (time-less-p
4467    (gnus-date-get-time (mail-header-date h1))
4468    (gnus-date-get-time (mail-header-date h2))))
4469
4470 (defun gnus-thread-sort-by-date (h1 h2)
4471   "Sort threads by root article date."
4472   (gnus-article-sort-by-date
4473    (gnus-thread-header h1) (gnus-thread-header h2)))
4474
4475 (defsubst gnus-article-sort-by-score (h1 h2)
4476   "Sort articles by root article score.
4477 Unscored articles will be counted as having a score of zero."
4478   (> (or (cdr (assq (mail-header-number h1)
4479                     gnus-newsgroup-scored))
4480          gnus-summary-default-score 0)
4481      (or (cdr (assq (mail-header-number h2)
4482                     gnus-newsgroup-scored))
4483          gnus-summary-default-score 0)))
4484
4485 (defun gnus-thread-sort-by-score (h1 h2)
4486   "Sort threads by root article score."
4487   (gnus-article-sort-by-score
4488    (gnus-thread-header h1) (gnus-thread-header h2)))
4489
4490 (defun gnus-thread-sort-by-total-score (h1 h2)
4491   "Sort threads by the sum of all scores in the thread.
4492 Unscored articles will be counted as having a score of zero."
4493   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4494
4495 (defun gnus-thread-total-score (thread)
4496   ;; This function find the total score of THREAD.
4497   (cond
4498    ((null thread)
4499     0)
4500    ((consp thread)
4501     (if (stringp (car thread))
4502         (apply gnus-thread-score-function 0
4503                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4504       (gnus-thread-total-score-1 thread)))
4505    (t
4506     (gnus-thread-total-score-1 (list thread)))))
4507
4508 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4509   "Sort threads such that the thread with the most recently arrived article comes first."
4510   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4511
4512 (defun gnus-thread-highest-number (thread)
4513   "Return the highest article number in THREAD."
4514   (apply 'max (mapcar (lambda (header)
4515                         (mail-header-number header))
4516                       (message-flatten-list thread))))
4517
4518 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4519   "Sort threads such that the thread with the most recently dated article comes first."
4520   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4521
4522 (defun gnus-thread-latest-date (thread)
4523   "Return the highest article date in THREAD."
4524   (let ((previous-time 0))
4525     (apply 'max
4526            (mapcar
4527             (lambda (header)
4528               (setq previous-time
4529                     (time-to-seconds
4530                      (condition-case ()
4531                          (mail-header-parse-date (mail-header-date header))
4532                        (error previous-time)))))
4533             (sort
4534              (message-flatten-list thread)
4535              (lambda (h1 h2)
4536                (< (mail-header-number h1)
4537                   (mail-header-number h2))))))))
4538
4539 (defun gnus-thread-total-score-1 (root)
4540   ;; This function find the total score of the thread below ROOT.
4541   (setq root (car root))
4542   (apply gnus-thread-score-function
4543          (or (append
4544               (mapcar 'gnus-thread-total-score
4545                       (cdr (gnus-id-to-thread (mail-header-id root))))
4546               (when (> (mail-header-number root) 0)
4547                 (list (or (cdr (assq (mail-header-number root)
4548                                      gnus-newsgroup-scored))
4549                           gnus-summary-default-score 0))))
4550              (list gnus-summary-default-score)
4551              '(0))))
4552
4553 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4554 (defvar gnus-tmp-prev-subject nil)
4555 (defvar gnus-tmp-false-parent nil)
4556 (defvar gnus-tmp-root-expunged nil)
4557 (defvar gnus-tmp-dummy-line nil)
4558
4559 (eval-when-compile (defvar gnus-tmp-header))
4560 (defun gnus-extra-header (type &optional header)
4561   "Return the extra header of TYPE."
4562   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4563       ""))
4564
4565 (defvar gnus-tmp-thread-tree-header-string "")
4566
4567 (defcustom gnus-sum-thread-tree-root "> "
4568   "With %B spec, used for the root of a thread.
4569 If nil, use subject instead."
4570   :type 'string
4571   :group 'gnus-thread)
4572 (defcustom gnus-sum-thread-tree-false-root "> "
4573   "With %B spec, used for a false root of a thread.
4574 If nil, use subject instead."
4575   :type 'string
4576   :group 'gnus-thread)
4577 (defcustom gnus-sum-thread-tree-single-indent ""
4578   "With %B spec, used for a thread with just one message.
4579 If nil, use subject instead."
4580   :type 'string
4581   :group 'gnus-thread)
4582 (defcustom gnus-sum-thread-tree-vertical "| "
4583   "With %B spec, used for drawing a vertical line."
4584   :type 'string
4585   :group 'gnus-thread)
4586 (defcustom gnus-sum-thread-tree-indent "  "
4587   "With %B spec, used for indenting."
4588   :type 'string
4589   :group 'gnus-thread)
4590 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4591   "With %B spec, used for a leaf with brothers."
4592   :type 'string
4593   :group 'gnus-thread)
4594 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4595   "With %B spec, used for a leaf without brothers."
4596   :type 'string
4597   :group 'gnus-thread)
4598
4599 (defun gnus-summary-prepare-threads (threads)
4600   "Prepare summary buffer from THREADS and indentation LEVEL.
4601 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4602 or a straight list of headers."
4603   (gnus-message 7 "Generating summary...")
4604
4605   (setq gnus-newsgroup-threads threads)
4606   (beginning-of-line)
4607
4608   (let ((gnus-tmp-level 0)
4609         (default-score (or gnus-summary-default-score 0))
4610         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4611         (building-line-count gnus-summary-display-while-building)
4612         (building-count (integerp gnus-summary-display-while-building))
4613         thread number subject stack state gnus-tmp-gathered beg-match
4614         new-roots gnus-tmp-new-adopts thread-end simp-subject
4615         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4616         gnus-tmp-replied gnus-tmp-subject-or-nil
4617         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4618         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4619         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4620         tree-stack)
4621
4622     (setq gnus-tmp-prev-subject nil
4623           gnus-tmp-thread-tree-header-string "")
4624
4625     (if (vectorp (car threads))
4626         ;; If this is a straight (sic) list of headers, then a
4627         ;; threaded summary display isn't required, so we just create
4628         ;; an unthreaded one.
4629         (gnus-summary-prepare-unthreaded threads)
4630
4631       ;; Do the threaded display.
4632
4633       (if gnus-summary-display-while-building
4634           (switch-to-buffer (buffer-name)))
4635       (while (or threads stack gnus-tmp-new-adopts new-roots)
4636
4637         (if (and (= gnus-tmp-level 0)
4638                  (or (not stack)
4639                      (= (caar stack) 0))
4640                  (not gnus-tmp-false-parent)
4641                  (or gnus-tmp-new-adopts new-roots))
4642             (if gnus-tmp-new-adopts
4643                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4644                       thread (list (car gnus-tmp-new-adopts))
4645                       gnus-tmp-header (caar thread)
4646                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4647               (when new-roots
4648                 (setq thread (list (car new-roots))
4649                       gnus-tmp-header (caar thread)
4650                       new-roots (cdr new-roots))))
4651
4652           (if threads
4653               ;; If there are some threads, we do them before the
4654               ;; threads on the stack.
4655               (setq thread threads
4656                     gnus-tmp-header (caar thread))
4657             ;; There were no current threads, so we pop something off
4658             ;; the stack.
4659             (setq state (car stack)
4660                   gnus-tmp-level (car state)
4661                   tree-stack (cadr state)
4662                   thread (caddr state)
4663                   stack (cdr stack)
4664                   gnus-tmp-header (caar thread))))
4665
4666         (setq gnus-tmp-false-parent nil)
4667         (setq gnus-tmp-root-expunged nil)
4668         (setq thread-end nil)
4669
4670         (if (stringp gnus-tmp-header)
4671             ;; The header is a dummy root.
4672             (cond
4673              ((eq gnus-summary-make-false-root 'adopt)
4674               ;; We let the first article adopt the rest.
4675               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4676                                                (cddar thread)))
4677               (setq gnus-tmp-gathered
4678                     (nconc (mapcar
4679                             (lambda (h) (mail-header-number (car h)))
4680                             (cddar thread))
4681                            gnus-tmp-gathered))
4682               (setq thread (cons (list (caar thread)
4683                                        (cadar thread))
4684                                  (cdr thread)))
4685               (setq gnus-tmp-level -1
4686                     gnus-tmp-false-parent t))
4687              ((eq gnus-summary-make-false-root 'empty)
4688               ;; We print adopted articles with empty subject fields.
4689               (setq gnus-tmp-gathered
4690                     (nconc (mapcar
4691                             (lambda (h) (mail-header-number (car h)))
4692                             (cddar thread))
4693                            gnus-tmp-gathered))
4694               (setq gnus-tmp-level -1))
4695              ((eq gnus-summary-make-false-root 'dummy)
4696               ;; We remember that we probably want to output a dummy
4697               ;; root.
4698               (setq gnus-tmp-dummy-line gnus-tmp-header)
4699               (setq gnus-tmp-prev-subject gnus-tmp-header))
4700              (t
4701               ;; We do not make a root for the gathered
4702               ;; sub-threads at all.
4703               (setq gnus-tmp-level -1)))
4704
4705           (setq number (mail-header-number gnus-tmp-header)
4706                 subject (mail-header-subject gnus-tmp-header)
4707                 simp-subject (gnus-simplify-subject-fully subject))
4708
4709           (cond
4710            ;; If the thread has changed subject, we might want to make
4711            ;; this subthread into a root.
4712            ((and (null gnus-thread-ignore-subject)
4713                  (not (zerop gnus-tmp-level))
4714                  gnus-tmp-prev-subject
4715                  (not (string= gnus-tmp-prev-subject simp-subject)))
4716             (setq new-roots (nconc new-roots (list (car thread)))
4717                   thread-end t
4718                   gnus-tmp-header nil))
4719            ;; If the article lies outside the current limit,
4720            ;; then we do not display it.
4721            ((not (memq number gnus-newsgroup-limit))
4722             (setq gnus-tmp-gathered
4723                   (nconc (mapcar
4724                           (lambda (h) (mail-header-number (car h)))
4725                           (cdar thread))
4726                          gnus-tmp-gathered))
4727             (setq gnus-tmp-new-adopts (if (cdar thread)
4728                                           (append gnus-tmp-new-adopts
4729                                                   (cdar thread))
4730                                         gnus-tmp-new-adopts)
4731                   thread-end t
4732                   gnus-tmp-header nil)
4733             (when (zerop gnus-tmp-level)
4734               (setq gnus-tmp-root-expunged t)))
4735            ;; Perhaps this article is to be marked as read?
4736            ((and gnus-summary-mark-below
4737                  (< (or (cdr (assq number gnus-newsgroup-scored))
4738                         default-score)
4739                     gnus-summary-mark-below)
4740                  ;; Don't touch sparse articles.
4741                  (not (gnus-summary-article-sparse-p number))
4742                  (not (gnus-summary-article-ancient-p number)))
4743             (setq gnus-newsgroup-unreads
4744                   (delq number gnus-newsgroup-unreads))
4745             (if gnus-newsgroup-auto-expire
4746                 (setq gnus-newsgroup-expirable
4747                       (gnus-add-to-sorted-list
4748                        gnus-newsgroup-expirable number))
4749               (push (cons number gnus-low-score-mark)
4750                     gnus-newsgroup-reads))))
4751
4752           (when gnus-tmp-header
4753             ;; We may have an old dummy line to output before this
4754             ;; article.
4755             (when (and gnus-tmp-dummy-line
4756                        (gnus-subject-equal
4757                         gnus-tmp-dummy-line
4758                         (mail-header-subject gnus-tmp-header)))
4759               (gnus-summary-insert-dummy-line
4760                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4761               (setq gnus-tmp-dummy-line nil))
4762
4763             ;; Compute the mark.
4764             (setq gnus-tmp-unread (gnus-article-mark number))
4765
4766             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4767                                   gnus-tmp-header gnus-tmp-level)
4768                   gnus-newsgroup-data)
4769
4770             ;; Actually insert the line.
4771             (setq
4772              gnus-tmp-subject-or-nil
4773              (cond
4774               ((and gnus-thread-ignore-subject
4775                     gnus-tmp-prev-subject
4776                     (not (string= gnus-tmp-prev-subject simp-subject)))
4777                subject)
4778               ((zerop gnus-tmp-level)
4779                (if (and (eq gnus-summary-make-false-root 'empty)
4780                         (memq number gnus-tmp-gathered)
4781                         gnus-tmp-prev-subject
4782                         (string= gnus-tmp-prev-subject simp-subject))
4783                    gnus-summary-same-subject
4784                  subject))
4785               (t gnus-summary-same-subject)))
4786             (if (and (eq gnus-summary-make-false-root 'adopt)
4787                      (= gnus-tmp-level 1)
4788                      (memq number gnus-tmp-gathered))
4789                 (setq gnus-tmp-opening-bracket ?\<
4790                       gnus-tmp-closing-bracket ?\>)
4791               (setq gnus-tmp-opening-bracket ?\[
4792                     gnus-tmp-closing-bracket ?\]))
4793             (setq
4794              gnus-tmp-indentation
4795              (aref gnus-thread-indent-array gnus-tmp-level)
4796              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4797              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4798                                 gnus-summary-default-score 0)
4799              gnus-tmp-score-char
4800              (if (or (null gnus-summary-default-score)
4801                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4802                          gnus-summary-zcore-fuzz))
4803                  ?                      ;Whitespace
4804                (if (< gnus-tmp-score gnus-summary-default-score)
4805                    gnus-score-below-mark gnus-score-over-mark))
4806              gnus-tmp-replied
4807              (cond ((memq number gnus-newsgroup-processable)
4808                     gnus-process-mark)
4809                    ((memq number gnus-newsgroup-cached)
4810                     gnus-cached-mark)
4811                    ((memq number gnus-newsgroup-replied)
4812                     gnus-replied-mark)
4813                    ((memq number gnus-newsgroup-forwarded)
4814                     gnus-forwarded-mark)
4815                    ((memq number gnus-newsgroup-saved)
4816                     gnus-saved-mark)
4817                    ((memq number gnus-newsgroup-recent)
4818                     gnus-recent-mark)
4819                    ((memq number gnus-newsgroup-unseen)
4820                     gnus-unseen-mark)
4821                    (t gnus-no-mark))
4822              gnus-tmp-downloaded
4823              (cond ((memq number gnus-newsgroup-undownloaded) 
4824                     gnus-undownloaded-mark)
4825                    (gnus-newsgroup-agentized
4826                     gnus-downloaded-mark)
4827                    (t
4828                     gnus-no-mark))
4829              gnus-tmp-from (mail-header-from gnus-tmp-header)
4830              gnus-tmp-name
4831              (cond
4832               ((string-match "<[^>]+> *$" gnus-tmp-from)
4833                (setq beg-match (match-beginning 0))
4834                (or (and (string-match "^\".+\"" gnus-tmp-from)
4835                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4836                    (substring gnus-tmp-from 0 beg-match)))
4837               ((string-match "(.+)" gnus-tmp-from)
4838                (substring gnus-tmp-from
4839                           (1+ (match-beginning 0)) (1- (match-end 0))))
4840               (t gnus-tmp-from))
4841
4842              ;; Do the %B string
4843              gnus-tmp-thread-tree-header-string
4844              (cond
4845               ((not gnus-show-threads) "")
4846               ((zerop gnus-tmp-level)
4847                (cond ((cdar thread)
4848                       (or gnus-sum-thread-tree-root subject))
4849                      (gnus-tmp-new-adopts
4850                       (or gnus-sum-thread-tree-false-root subject))
4851                      (t
4852                       (or gnus-sum-thread-tree-single-indent subject))))
4853               (t
4854                (concat (apply 'concat
4855                               (mapcar (lambda (item)
4856                                         (if (= item 1)
4857                                             gnus-sum-thread-tree-vertical
4858                                           gnus-sum-thread-tree-indent))
4859                                       (cdr (reverse tree-stack))))
4860                        (if (nth 1 thread)
4861                            gnus-sum-thread-tree-leaf-with-other
4862                          gnus-sum-thread-tree-single-leaf)))))
4863             (when (string= gnus-tmp-name "")
4864               (setq gnus-tmp-name gnus-tmp-from))
4865             (unless (numberp gnus-tmp-lines)
4866               (setq gnus-tmp-lines -1))
4867             (if (= gnus-tmp-lines -1)
4868                 (setq gnus-tmp-lines "?")
4869               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4870               (gnus-put-text-property
4871              (point)
4872              (progn (eval gnus-summary-line-format-spec) (point))
4873                'gnus-number number)
4874             (when gnus-visual-p
4875               (forward-line -1)
4876               (gnus-run-hooks 'gnus-summary-update-hook)
4877               (forward-line 1))
4878
4879             (setq gnus-tmp-prev-subject simp-subject)))
4880
4881         (when (nth 1 thread)
4882           (push (list (max 0 gnus-tmp-level)
4883                       (copy-sequence tree-stack)
4884                       (nthcdr 1 thread))
4885                 stack))
4886         (push (if (nth 1 thread) 1 0) tree-stack)
4887         (incf gnus-tmp-level)
4888         (setq threads (if thread-end nil (cdar thread)))
4889         (if gnus-summary-display-while-building
4890             (if building-count
4891                 (progn
4892                   ;; use a set frequency
4893                   (setq building-line-count (1- building-line-count))
4894                   (when (= building-line-count 0)
4895                     (sit-for 0)
4896                     (setq building-line-count
4897                           gnus-summary-display-while-building)))
4898               ;; always
4899               (sit-for 0)))
4900         (unless threads
4901           (setq gnus-tmp-level 0)))))
4902   (gnus-message 7 "Generating summary...done"))
4903
4904 (defun gnus-summary-prepare-unthreaded (headers)
4905   "Generate an unthreaded summary buffer based on HEADERS."
4906   (let (header number mark)
4907
4908     (beginning-of-line)
4909
4910     (while headers
4911       ;; We may have to root out some bad articles...
4912       (when (memq (setq number (mail-header-number
4913                                 (setq header (pop headers))))
4914                   gnus-newsgroup-limit)
4915         ;; Mark article as read when it has a low score.
4916         (when (and gnus-summary-mark-below
4917                    (< (or (cdr (assq number gnus-newsgroup-scored))
4918                           gnus-summary-default-score 0)
4919                       gnus-summary-mark-below)
4920                    (not (gnus-summary-article-ancient-p number)))
4921           (setq gnus-newsgroup-unreads
4922                 (delq number gnus-newsgroup-unreads))
4923           (if gnus-newsgroup-auto-expire
4924               (push number gnus-newsgroup-expirable)
4925             (push (cons number gnus-low-score-mark)
4926                   gnus-newsgroup-reads)))
4927
4928         (setq mark (gnus-article-mark number))
4929         (push (gnus-data-make number mark (1+ (point)) header 0)
4930               gnus-newsgroup-data)
4931         (gnus-summary-insert-line
4932          header 0 number
4933          (memq number gnus-newsgroup-undownloaded)
4934          mark (memq number gnus-newsgroup-replied)
4935          (memq number gnus-newsgroup-expirable)
4936          (mail-header-subject header) nil
4937          (cdr (assq number gnus-newsgroup-scored))
4938          (memq number gnus-newsgroup-processable))))))
4939
4940 (defun gnus-summary-remove-list-identifiers ()
4941   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4942   (let ((regexp (if (consp gnus-list-identifiers)
4943                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4944                   gnus-list-identifiers))
4945         changed subject)
4946     (when regexp
4947       (dolist (header gnus-newsgroup-headers)
4948         (setq subject (mail-header-subject header)
4949               changed nil)
4950         (while (string-match
4951                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4952                 subject)
4953           (setq subject
4954                 (concat (substring subject 0 (match-beginning 2))
4955                         (substring subject (match-end 0)))
4956                 changed t))
4957         (when (and changed
4958                    (string-match
4959                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4960           (setq subject
4961                 (concat (substring subject 0 (match-beginning 1))
4962                         (substring subject (match-end 1)))))
4963         (when changed
4964           (mail-header-set-subject header subject))))))
4965
4966 (defun gnus-fetch-headers (articles)
4967   "Fetch headers of ARTICLES."
4968   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4969     (gnus-message 5 "Fetching headers for %s..." name)
4970     (prog1
4971         (if (eq 'nov
4972                 (setq gnus-headers-retrieved-by
4973                       (gnus-retrieve-headers
4974                        articles gnus-newsgroup-name
4975                        ;; We might want to fetch old headers, but
4976                        ;; not if there is only 1 article.
4977                        (and (or (and
4978                                  (not (eq gnus-fetch-old-headers 'some))
4979                                  (not (numberp gnus-fetch-old-headers)))
4980                                 (> (length articles) 1))
4981                             gnus-fetch-old-headers))))
4982             (gnus-get-newsgroup-headers-xover
4983              articles nil nil gnus-newsgroup-name t)
4984           (gnus-get-newsgroup-headers))
4985       (gnus-message 5 "Fetching headers for %s...done" name))))
4986
4987 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4988   "Select newsgroup GROUP.
4989 If READ-ALL is non-nil, all articles in the group are selected.
4990 If SELECT-ARTICLES, only select those articles from GROUP."
4991   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4992          ;;!!! Dirty hack; should be removed.
4993          (gnus-summary-ignore-duplicates
4994           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4995               t
4996             gnus-summary-ignore-duplicates))
4997          (info (nth 2 entry))
4998          articles fetched-articles cached)
4999
5000     (unless (gnus-check-server
5001              (set (make-local-variable 'gnus-current-select-method)
5002                   (gnus-find-method-for-group group)))
5003       (error "Couldn't open server"))
5004
5005     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5006         (gnus-activate-group group)     ; Or we can activate it...
5007         (progn                          ; Or we bug out.
5008           (when (equal major-mode 'gnus-summary-mode)
5009             (gnus-kill-buffer (current-buffer)))
5010           (error "Couldn't activate group %s: %s"
5011                  group (gnus-status-message group))))
5012
5013     (unless (gnus-request-group group t)
5014       (when (equal major-mode 'gnus-summary-mode)
5015         (gnus-kill-buffer (current-buffer)))
5016       (error "Couldn't request group %s: %s"
5017              group (gnus-status-message group)))
5018
5019     (when gnus-agent
5020       ;; The agent may be storing articles that are no longer in the
5021       ;; server's active range.  If that is the case, the active range
5022       ;; needs to be expanded such that the agent's articles can be
5023       ;; included in the summary.
5024       (let* ((gnus-command-method (gnus-find-method-for-group group))
5025              (alist (gnus-agent-load-alist group))
5026              (active (gnus-active group)))
5027         (if (and (car alist)
5028                  (< (caar alist) (car active)))
5029             (gnus-set-active group (cons (caar alist) (cdr active)))))
5030
5031       (setq gnus-summary-use-undownloaded-faces
5032             (not (gnus-agent-find-parameter 
5033                   group
5034                   'agent-disable-undownloaded-faces))))
5035
5036     (setq gnus-newsgroup-name group
5037           gnus-newsgroup-unselected nil
5038           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5039
5040     (let ((display (gnus-group-find-parameter group 'display)))
5041       (setq gnus-newsgroup-display
5042             (cond
5043              ((not (zerop (or (car-safe read-all) 0)))
5044               ;; The user entered the group with C-u SPC/RET, let's show
5045               ;; all articles.
5046               'gnus-not-ignore)
5047              ((eq display 'all)
5048               'gnus-not-ignore)
5049              ((arrayp display)
5050               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5051              ((numberp display)
5052               ;; The following is probably the "correct" solution, but
5053               ;; it makes Gnus fetch all headers and then limit the
5054               ;; articles (which is slow), so instead we hack the
5055               ;; select-articles parameter instead. -- Simon Josefsson
5056               ;; <jas@kth.se>
5057               ;;
5058               ;; (gnus-byte-compile
5059               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5060               ;;                         display)))))
5061               (setq select-articles
5062                     (gnus-uncompress-range
5063                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5064                              (if (> tmp 0)
5065                                  tmp
5066                                1))
5067                            (cdr (gnus-active group)))))
5068               nil)
5069              (t
5070               nil))))
5071
5072     (gnus-summary-setup-default-charset)
5073
5074     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5075     (when (gnus-virtual-group-p group)
5076       (setq cached gnus-newsgroup-cached))
5077
5078     (setq gnus-newsgroup-unreads
5079           (gnus-sorted-ndifference
5080            (gnus-sorted-ndifference gnus-newsgroup-unreads
5081                                     gnus-newsgroup-marked)
5082            gnus-newsgroup-dormant))
5083
5084     (setq gnus-newsgroup-processable nil)
5085
5086     (gnus-update-read-articles group gnus-newsgroup-unreads)
5087
5088     ;; Adjust and set lists of article marks.
5089     (when info
5090       (gnus-adjust-marked-articles info))
5091     (if (setq articles select-articles)
5092         (setq gnus-newsgroup-unselected
5093               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5094       (setq articles (gnus-articles-to-read group read-all)))
5095
5096     (cond
5097      ((null articles)
5098       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5099       'quit)
5100      ((eq articles 0) nil)
5101      (t
5102       ;; Init the dependencies hash table.
5103       (setq gnus-newsgroup-dependencies
5104             (gnus-make-hashtable (length articles)))
5105       (gnus-set-global-variables)
5106       ;; Retrieve the headers and read them in.
5107
5108       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5109
5110       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5111       (when cached
5112         (setq gnus-newsgroup-cached cached))
5113
5114       ;; Suppress duplicates?
5115       (when gnus-suppress-duplicates
5116         (gnus-dup-suppress-articles))
5117
5118       ;; Set the initial limit.
5119       (setq gnus-newsgroup-limit (copy-sequence articles))
5120       ;; Remove canceled articles from the list of unread articles.
5121       (setq fetched-articles
5122             (mapcar (lambda (headers) (mail-header-number headers))
5123                     gnus-newsgroup-headers))
5124       (setq gnus-newsgroup-articles fetched-articles)
5125       (setq gnus-newsgroup-unreads
5126             (gnus-sorted-nintersection
5127              gnus-newsgroup-unreads fetched-articles))
5128       (gnus-compute-unseen-list)
5129
5130       ;; Removed marked articles that do not exist.
5131       (gnus-update-missing-marks
5132        (gnus-sorted-difference articles fetched-articles))
5133       ;; We might want to build some more threads first.
5134       (when (and gnus-fetch-old-headers
5135                  (eq gnus-headers-retrieved-by 'nov))
5136         (if (eq gnus-fetch-old-headers 'invisible)
5137             (gnus-build-all-threads)
5138           (gnus-build-old-threads)))
5139       ;; Let the Gnus agent mark articles as read.
5140       (when gnus-agent
5141         (gnus-agent-get-undownloaded-list))
5142       ;; Remove list identifiers from subject
5143       (when gnus-list-identifiers
5144         (gnus-summary-remove-list-identifiers))
5145       ;; Check whether auto-expire is to be done in this group.
5146       (setq gnus-newsgroup-auto-expire
5147             (gnus-group-auto-expirable-p group))
5148       ;; Set up the article buffer now, if necessary.
5149       (unless gnus-single-article-buffer
5150         (gnus-article-setup-buffer))
5151       ;; First and last article in this newsgroup.
5152       (when gnus-newsgroup-headers
5153         (setq gnus-newsgroup-begin
5154               (mail-header-number (car gnus-newsgroup-headers))
5155               gnus-newsgroup-end
5156               (mail-header-number
5157                (gnus-last-element gnus-newsgroup-headers))))
5158       ;; GROUP is successfully selected.
5159       (or gnus-newsgroup-headers t)))))
5160
5161 (defun gnus-compute-unseen-list ()
5162   ;; The `seen' marks are treated specially.
5163   (if (not gnus-newsgroup-seen)
5164       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5165     (setq gnus-newsgroup-unseen
5166           (gnus-inverse-list-range-intersection
5167            gnus-newsgroup-articles gnus-newsgroup-seen))))
5168
5169 (defun gnus-summary-display-make-predicate (display)
5170   (require 'gnus-agent)
5171   (when (= (length display) 1)
5172     (setq display (car display)))
5173   (unless gnus-summary-display-cache
5174     (dolist (elem (append '((unread . unread)
5175                             (read . read)
5176                             (unseen . unseen))
5177                           gnus-article-mark-lists))
5178       (push (cons (cdr elem)
5179                   (gnus-byte-compile
5180                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5181             gnus-summary-display-cache)))
5182   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5183         (gnus-category-predicate-cache gnus-summary-display-cache))
5184     (gnus-get-predicate display)))
5185
5186 ;; Uses the dynamically bound `number' variable.
5187 (eval-when-compile
5188   (defvar number))
5189 (defun gnus-article-marked-p (type &optional article)
5190   (let ((article (or article number)))
5191     (cond
5192      ((eq type 'tick)
5193       (memq article gnus-newsgroup-marked))
5194      ((eq type 'spam)
5195       (memq article gnus-newsgroup-spam-marked))
5196      ((eq type 'unsend)
5197       (memq article gnus-newsgroup-unsendable))
5198      ((eq type 'undownload)
5199       (memq article gnus-newsgroup-undownloaded))
5200      ((eq type 'download)
5201       (memq article gnus-newsgroup-downloadable))
5202      ((eq type 'unread)
5203       (memq article gnus-newsgroup-unreads))
5204      ((eq type 'read)
5205       (memq article gnus-newsgroup-reads))
5206      ((eq type 'dormant)
5207       (memq article gnus-newsgroup-dormant) )
5208      ((eq type 'expire)
5209       (memq article gnus-newsgroup-expirable))
5210      ((eq type 'reply)
5211       (memq article gnus-newsgroup-replied))
5212      ((eq type 'killed)
5213       (memq article gnus-newsgroup-killed))
5214      ((eq type 'bookmark)
5215       (assq article gnus-newsgroup-bookmarks))
5216      ((eq type 'score)
5217       (assq article gnus-newsgroup-scored))
5218      ((eq type 'save)
5219       (memq article gnus-newsgroup-saved))
5220      ((eq type 'cache)
5221       (memq article gnus-newsgroup-cached))
5222      ((eq type 'forward)
5223       (memq article gnus-newsgroup-forwarded))
5224      ((eq type 'seen)
5225       (not (memq article gnus-newsgroup-unseen)))
5226      ((eq type 'recent)
5227       (memq article gnus-newsgroup-recent))
5228      (t t))))
5229
5230 (defun gnus-articles-to-read (group &optional read-all)
5231   "Find out what articles the user wants to read."
5232   (let* ((display (gnus-group-find-parameter group 'display))
5233          (articles
5234           ;; Select all articles if `read-all' is non-nil, or if there
5235           ;; are no unread articles.
5236           (if (or read-all
5237                   (and (zerop (length gnus-newsgroup-marked))
5238                        (zerop (length gnus-newsgroup-unreads)))
5239                   ;; Fetch all if the predicate is non-nil.
5240                   gnus-newsgroup-display)
5241               ;; We want to select the headers for all the articles in
5242               ;; the group, so we select either all the active
5243               ;; articles in the group, or (if that's nil), the
5244               ;; articles in the cache.
5245               (or
5246                (gnus-uncompress-range (gnus-active group))
5247                (gnus-cache-articles-in-group group))
5248             ;; Select only the "normal" subset of articles.
5249             (gnus-sorted-nunion
5250              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5251              gnus-newsgroup-unreads)))
5252          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5253          (scored (length scored-list))
5254          (number (length articles))
5255          (marked (+ (length gnus-newsgroup-marked)
5256                     (length gnus-newsgroup-dormant)))
5257          (select
5258           (cond
5259            ((numberp read-all)
5260             read-all)
5261            ((numberp gnus-newsgroup-display)
5262             gnus-newsgroup-display)
5263            (t
5264             (condition-case ()
5265                 (cond
5266                  ((and (or (<= scored marked) (= scored number))
5267                        (numberp gnus-large-newsgroup)
5268                        (> number gnus-large-newsgroup))
5269                   (let* ((cursor-in-echo-area nil)
5270                          (initial (gnus-parameter-large-newsgroup-initial
5271                                    gnus-newsgroup-name))
5272                          (input
5273                           (read-string
5274                            (format
5275                             "How many articles from %s (%s %d): "
5276                             (gnus-limit-string
5277                              (gnus-group-decoded-name gnus-newsgroup-name)
5278                              35)
5279                             (if initial "max" "default")
5280                             number)
5281                            (if initial
5282                                (cons (number-to-string initial)
5283                                      0)))))
5284                     (if (string-match "^[ \t]*$" input) number input)))
5285                  ((and (> scored marked) (< scored number)
5286                        (> (- scored number) 20))
5287                   (let ((input
5288                          (read-string
5289                           (format "%s %s (%d scored, %d total): "
5290                                   "How many articles from"
5291                                   (gnus-group-decoded-name group)
5292                                   scored number))))
5293                     (if (string-match "^[ \t]*$" input)
5294                         number input)))
5295                  (t number))
5296               (quit
5297                (message "Quit getting the articles to read")
5298                nil))))))
5299     (setq select (if (stringp select) (string-to-number select) select))
5300     (if (or (null select) (zerop select))
5301         select
5302       (if (and (not (zerop scored)) (<= (abs select) scored))
5303           (progn
5304             (setq articles (sort scored-list '<))
5305             (setq number (length articles)))
5306         (setq articles (copy-sequence articles)))
5307
5308       (when (< (abs select) number)
5309         (if (< select 0)
5310             ;; Select the N oldest articles.
5311             (setcdr (nthcdr (1- (abs select)) articles) nil)
5312           ;; Select the N most recent articles.
5313           (setq articles (nthcdr (- number select) articles))))
5314       (setq gnus-newsgroup-unselected
5315             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5316       (when gnus-alter-articles-to-read-function
5317         (setq articles
5318               (sort
5319                (funcall gnus-alter-articles-to-read-function
5320                         gnus-newsgroup-name articles)
5321                '<)))
5322       articles)))
5323
5324 (defun gnus-killed-articles (killed articles)
5325   (let (out)
5326     (while articles
5327       (when (inline (gnus-member-of-range (car articles) killed))
5328         (push (car articles) out))
5329       (setq articles (cdr articles)))
5330     out))
5331
5332 (defun gnus-uncompress-marks (marks)
5333   "Uncompress the mark ranges in MARKS."
5334   (let ((uncompressed '(score bookmark))
5335         out)
5336     (while marks
5337       (if (memq (caar marks) uncompressed)
5338           (push (car marks) out)
5339         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5340       (setq marks (cdr marks)))
5341     out))
5342
5343 (defun gnus-article-mark-to-type (mark)
5344   "Return the type of MARK."
5345   (or (cadr (assq mark gnus-article-special-mark-lists))
5346       'list))
5347
5348 (defun gnus-article-unpropagatable-p (mark)
5349   "Return whether MARK should be propagated to backend."
5350   (memq mark gnus-article-unpropagated-mark-lists))
5351
5352 (defun gnus-adjust-marked-articles (info)
5353   "Set all article lists and remove all marks that are no longer valid."
5354   (let* ((marked-lists (gnus-info-marks info))
5355          (active (gnus-active (gnus-info-group info)))
5356          (min (car active))
5357          (max (cdr active))
5358          (types gnus-article-mark-lists)
5359          marks var articles article mark mark-type)
5360
5361     (dolist (marks marked-lists)
5362       (setq mark (car marks)
5363             mark-type (gnus-article-mark-to-type mark)
5364             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5365
5366       ;; We set the variable according to the type of the marks list,
5367       ;; and then adjust the marks to a subset of the active articles.
5368       (cond
5369        ;; Adjust "simple" lists.
5370        ((eq mark-type 'list)
5371         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5372         (when (memq mark '(tick dormant expire reply save))
5373           (while articles
5374             (when (or (< (setq article (pop articles)) min) (> article max))
5375               (set var (delq article (symbol-value var)))))))
5376        ;; Adjust assocs.
5377        ((eq mark-type 'tuple)
5378         (set var (setq articles (cdr marks)))
5379         (when (not (listp (cdr (symbol-value var))))
5380           (set var (list (symbol-value var))))
5381         (when (not (listp (cdr articles)))
5382           (setq articles (list articles)))
5383         (while articles
5384           (when (or (not (consp (setq article (pop articles))))
5385                     (< (car article) min)
5386                     (> (car article) max))
5387             (set var (delq article (symbol-value var))))))
5388        ;; Adjust ranges (sloppily).
5389        ((eq mark-type 'range)
5390         (cond
5391          ((eq mark 'seen)
5392           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5393           ;; It should be (seen (NUM1 . NUM2)).
5394           (when (numberp (cddr marks))
5395             (setcdr marks (list (cdr marks))))
5396           (setq articles (cdr marks))
5397           (while (and articles
5398                       (or (and (consp (car articles))
5399                                (> min (cdar articles)))
5400                           (and (numberp (car articles))
5401                                (> min (car articles)))))
5402             (pop articles))
5403           (set var articles))))))))
5404
5405 (defun gnus-update-missing-marks (missing)
5406   "Go through the list of MISSING articles and remove them from the mark lists."
5407   (when missing
5408     (let (var m)
5409       ;; Go through all types.
5410       (dolist (elem gnus-article-mark-lists)
5411         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5412           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5413           (when (symbol-value var)
5414             ;; This list has articles.  So we delete all missing
5415             ;; articles from it.
5416             (setq m missing)
5417             (while m
5418               (set var (delq (pop m) (symbol-value var))))))))))
5419
5420 (defun gnus-update-marks ()
5421   "Enter the various lists of marked articles into the newsgroup info list."
5422   (let ((types gnus-article-mark-lists)
5423         (info (gnus-get-info gnus-newsgroup-name))
5424         type list newmarked symbol delta-marks)
5425     (when info
5426       ;; Add all marks lists to the list of marks lists.
5427       (while (setq type (pop types))
5428         (setq list (symbol-value
5429                     (setq symbol
5430                           (intern (format "gnus-newsgroup-%s" (car type))))))
5431
5432         (when list
5433           ;; Get rid of the entries of the articles that have the
5434           ;; default score.
5435           (when (and (eq (cdr type) 'score)
5436                      gnus-save-score
5437                      list)
5438             (let* ((arts list)
5439                    (prev (cons nil list))
5440                    (all prev))
5441               (while arts
5442                 (if (or (not (consp (car arts)))
5443                         (= (cdar arts) gnus-summary-default-score))
5444                     (setcdr prev (cdr arts))
5445                   (setq prev arts))
5446                 (setq arts (cdr arts)))
5447               (setq list (cdr all)))))
5448
5449         (when (eq (cdr type) 'seen)
5450           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5451
5452         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5453           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5454
5455         (when (and (gnus-check-backend-function
5456                     'request-set-mark gnus-newsgroup-name)
5457                    (not (gnus-article-unpropagatable-p (cdr type))))
5458           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5459                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5460                  (add (gnus-remove-from-range
5461                        (gnus-copy-sequence list) old)))
5462             (when add
5463               (push (list add 'add (list (cdr type))) delta-marks))
5464             (when del
5465               (push (list del 'del (list (cdr type))) delta-marks))))
5466
5467         (when list
5468           (push (cons (cdr type) list) newmarked)))
5469
5470       (when delta-marks
5471         (unless (gnus-check-group gnus-newsgroup-name)
5472           (error "Can't open server for %s" gnus-newsgroup-name))
5473         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5474
5475       ;; Enter these new marks into the info of the group.
5476       (if (nthcdr 3 info)
5477           (setcar (nthcdr 3 info) newmarked)
5478         ;; Add the marks lists to the end of the info.
5479         (when newmarked
5480           (setcdr (nthcdr 2 info) (list newmarked))))
5481
5482       ;; Cut off the end of the info if there's nothing else there.
5483       (let ((i 5))
5484         (while (and (> i 2)
5485                     (not (nth i info)))
5486           (when (nthcdr (decf i) info)
5487             (setcdr (nthcdr i info) nil)))))))
5488
5489 (defun gnus-set-mode-line (where)
5490   "Set the mode line of the article or summary buffers.
5491 If WHERE is `summary', the summary mode line format will be used."
5492   ;; Is this mode line one we keep updated?
5493   (when (and (memq where gnus-updated-mode-lines)
5494              (symbol-value
5495               (intern (format "gnus-%s-mode-line-format-spec" where))))
5496     (let (mode-string)
5497       (save-excursion
5498         ;; We evaluate this in the summary buffer since these
5499         ;; variables are buffer-local to that buffer.
5500         (set-buffer gnus-summary-buffer)
5501        ;; We bind all these variables that are used in the `eval' form
5502         ;; below.
5503         (let* ((mformat (symbol-value
5504                          (intern
5505                           (format "gnus-%s-mode-line-format-spec" where))))
5506                (gnus-tmp-group-name (gnus-group-decoded-name
5507                                      gnus-newsgroup-name))
5508                (gnus-tmp-article-number (or gnus-current-article 0))
5509                (gnus-tmp-unread gnus-newsgroup-unreads)
5510                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5511                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5512                (gnus-tmp-unread-and-unselected
5513                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5514                             (zerop gnus-tmp-unselected))
5515                        "")
5516                       ((zerop gnus-tmp-unselected)
5517                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5518                       (t (format "{%d(+%d) more}"
5519                                  gnus-tmp-unread-and-unticked
5520                                  gnus-tmp-unselected))))
5521                (gnus-tmp-subject
5522                 (if (and gnus-current-headers
5523                          (vectorp gnus-current-headers))
5524                     (gnus-mode-string-quote
5525                      (mail-header-subject gnus-current-headers))
5526                   ""))
5527                bufname-length max-len
5528                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5529           (setq mode-string (eval mformat))
5530           (setq bufname-length (if (string-match "%b" mode-string)
5531                                    (- (length
5532                                        (buffer-name
5533                                         (if (eq where 'summary)
5534                                             nil
5535                                           (get-buffer gnus-article-buffer))))
5536                                       2)
5537                                  0))
5538           (setq max-len (max 4 (if gnus-mode-non-string-length
5539                                    (- (window-width)
5540                                       gnus-mode-non-string-length
5541                                       bufname-length)
5542                                  (length mode-string))))
5543           ;; We might have to chop a bit of the string off...
5544           (when (> (length mode-string) max-len)
5545             (setq mode-string
5546                   (concat (truncate-string-to-width mode-string (- max-len 3))
5547                           "...")))
5548           ;; Pad the mode string a bit.
5549           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5550       ;; Update the mode line.
5551       (setq mode-line-buffer-identification
5552             (gnus-mode-line-buffer-identification (list mode-string)))
5553       (set-buffer-modified-p t))))
5554
5555 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5556   "Go through the HEADERS list and add all Xrefs to a hash table.
5557 The resulting hash table is returned, or nil if no Xrefs were found."
5558   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5559          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5560          (xref-hashtb (gnus-make-hashtable))
5561          start group entry number xrefs header)
5562     (while headers
5563       (setq header (pop headers))
5564       (when (and (setq xrefs (mail-header-xref header))
5565                  (not (memq (setq number (mail-header-number header))
5566                             unreads)))
5567         (setq start 0)
5568         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5569           (setq start (match-end 0))
5570           (setq group (if prefix
5571                           (concat prefix (substring xrefs (match-beginning 1)
5572                                                     (match-end 1)))
5573                         (substring xrefs (match-beginning 1) (match-end 1))))
5574           (setq number
5575                 (string-to-int (substring xrefs (match-beginning 2)
5576                                           (match-end 2))))
5577           (if (setq entry (gnus-gethash group xref-hashtb))
5578               (setcdr entry (cons number (cdr entry)))
5579             (gnus-sethash group (cons number nil) xref-hashtb)))))
5580     (and start xref-hashtb)))
5581
5582 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5583   "Look through all the headers and mark the Xrefs as read."
5584   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5585         name entry info xref-hashtb idlist method nth4)
5586     (save-excursion
5587       (set-buffer gnus-group-buffer)
5588       (when (setq xref-hashtb
5589                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5590         (mapatoms
5591          (lambda (group)
5592            (unless (string= from-newsgroup (setq name (symbol-name group)))
5593              (setq idlist (symbol-value group))
5594              ;; Dead groups are not updated.
5595              (and (prog1
5596                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5597                             info (nth 2 entry))
5598                     (when (stringp (setq nth4 (gnus-info-method info)))
5599                       (setq nth4 (gnus-server-to-method nth4))))
5600                   ;; Only do the xrefs if the group has the same
5601                   ;; select method as the group we have just read.
5602                   (or (gnus-methods-equal-p
5603                        nth4 (gnus-find-method-for-group from-newsgroup))
5604                       virtual
5605                       (equal nth4 (setq method (gnus-find-method-for-group
5606                                                 from-newsgroup)))
5607                       (and (equal (car nth4) (car method))
5608                            (equal (nth 1 nth4) (nth 1 method))))
5609                   gnus-use-cross-reference
5610                   (or (not (eq gnus-use-cross-reference t))
5611                       virtual
5612                       ;; Only do cross-references on subscribed
5613                       ;; groups, if that is what is wanted.
5614                       (<= (gnus-info-level info) gnus-level-subscribed))
5615                   (gnus-group-make-articles-read name idlist))))
5616          xref-hashtb)))))
5617
5618 (defun gnus-compute-read-articles (group articles)
5619   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5620          (info (nth 2 entry))
5621          (active (gnus-active group))
5622          ninfo)
5623     (when entry
5624       ;; First peel off all invalid article numbers.
5625       (when active
5626         (let ((ids articles)
5627               id first)
5628           (while (setq id (pop ids))
5629             (when (and first (> id (cdr active)))
5630               ;; We'll end up in this situation in one particular
5631               ;; obscure situation.  If you re-scan a group and get
5632               ;; a new article that is cross-posted to a different
5633               ;; group that has not been re-scanned, you might get
5634               ;; crossposted article that has a higher number than
5635               ;; Gnus believes possible.  So we re-activate this
5636               ;; group as well.  This might mean doing the
5637               ;; crossposting thingy will *increase* the number
5638               ;; of articles in some groups.  Tsk, tsk.
5639               (setq active (or (gnus-activate-group group) active)))
5640             (when (or (> id (cdr active))
5641                       (< id (car active)))
5642               (setq articles (delq id articles))))))
5643       ;; If the read list is nil, we init it.
5644       (if (and active
5645                (null (gnus-info-read info))
5646                (> (car active) 1))
5647           (setq ninfo (cons 1 (1- (car active))))
5648         (setq ninfo (gnus-info-read info)))
5649       ;; Then we add the read articles to the range.
5650       (gnus-add-to-range
5651        ninfo (setq articles (sort articles '<))))))
5652
5653 (defun gnus-group-make-articles-read (group articles)
5654   "Update the info of GROUP to say that ARTICLES are read."
5655   (let* ((num 0)
5656          (entry (gnus-gethash group gnus-newsrc-hashtb))
5657          (info (nth 2 entry))
5658          (active (gnus-active group))
5659          range)
5660     (when entry
5661       (setq range (gnus-compute-read-articles group articles))
5662       (save-excursion
5663         (set-buffer gnus-group-buffer)
5664         (gnus-undo-register
5665           `(progn
5666              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5667              (gnus-info-set-read ',info ',(gnus-info-read info))
5668              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5669              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5670              (gnus-group-update-group ,group t))))
5671       ;; Add the read articles to the range.
5672       (gnus-info-set-read info range)
5673       (gnus-request-set-mark group (list (list range 'add '(read))))
5674       ;; Then we have to re-compute how many unread
5675       ;; articles there are in this group.
5676       (when active
5677         (cond
5678          ((not range)
5679           (setq num (- (1+ (cdr active)) (car active))))
5680          ((not (listp (cdr range)))
5681           (setq num (- (cdr active) (- (1+ (cdr range))
5682                                        (car range)))))
5683          (t
5684           (while range
5685             (if (numberp (car range))
5686                 (setq num (1+ num))
5687               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5688             (setq range (cdr range)))
5689           (setq num (- (cdr active) num))))
5690         ;; Update the number of unread articles.
5691         (setcar entry num)
5692         ;; Update the group buffer.
5693         (unless (gnus-ephemeral-group-p group)
5694           (gnus-group-update-group group t))))))
5695
5696 (defvar gnus-newsgroup-none-id 0)
5697
5698 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5699   (let ((cur nntp-server-buffer)
5700         (dependencies
5701          (or dependencies
5702              (save-excursion (set-buffer gnus-summary-buffer)
5703                              gnus-newsgroup-dependencies)))
5704         headers id end ref
5705         (mail-parse-charset gnus-newsgroup-charset)
5706         (mail-parse-ignored-charsets
5707          (save-excursion (condition-case nil
5708                              (set-buffer gnus-summary-buffer)
5709                            (error))
5710                          gnus-newsgroup-ignored-charsets)))
5711     (save-excursion
5712       (set-buffer nntp-server-buffer)
5713       ;; Translate all TAB characters into SPACE characters.
5714       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5715       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5716       (ietf-drums-unfold-fws)
5717       (gnus-run-hooks 'gnus-parse-headers-hook)
5718       (let ((case-fold-search t)
5719             in-reply-to header p lines chars)
5720         (goto-char (point-min))
5721         ;; Search to the beginning of the next header.  Error messages
5722         ;; do not begin with 2 or 3.
5723         (while (re-search-forward "^[23][0-9]+ " nil t)
5724           (setq id nil
5725                 ref nil)
5726           ;; This implementation of this function, with nine
5727           ;; search-forwards instead of the one re-search-forward and
5728           ;; a case (which basically was the old function) is actually
5729           ;; about twice as fast, even though it looks messier.  You
5730           ;; can't have everything, I guess.  Speed and elegance
5731           ;; doesn't always go hand in hand.
5732           (setq
5733            header
5734            (vector
5735             ;; Number.
5736             (prog1
5737                 (read cur)
5738               (end-of-line)
5739               (setq p (point))
5740               (narrow-to-region (point)
5741                                 (or (and (search-forward "\n.\n" nil t)
5742                                          (- (point) 2))
5743                                     (point))))
5744             ;; Subject.
5745             (progn
5746               (goto-char p)
5747               (if (search-forward "\nsubject:" nil t)
5748                   (funcall gnus-decode-encoded-word-function
5749                            (nnheader-header-value))
5750                 "(none)"))
5751             ;; From.
5752             (progn
5753               (goto-char p)
5754               (if (search-forward "\nfrom:" nil t)
5755                   (funcall gnus-decode-encoded-word-function
5756                            (nnheader-header-value))
5757                 "(nobody)"))
5758             ;; Date.
5759             (progn
5760               (goto-char p)
5761               (if (search-forward "\ndate:" nil t)
5762                   (nnheader-header-value) ""))
5763             ;; Message-ID.
5764             (progn
5765               (goto-char p)
5766               (setq id (if (re-search-forward
5767                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5768                            ;; We do it this way to make sure the Message-ID
5769                            ;; is (somewhat) syntactically valid.
5770                            (buffer-substring (match-beginning 1)
5771                                              (match-end 1))
5772                          ;; If there was no message-id, we just fake one
5773                          ;; to make subsequent routines simpler.
5774                          (nnheader-generate-fake-message-id))))
5775             ;; References.
5776             (progn
5777               (goto-char p)
5778               (if (search-forward "\nreferences:" nil t)
5779                   (progn
5780                     (setq end (point))
5781                     (prog1
5782                         (nnheader-header-value)
5783                       (setq ref
5784                             (buffer-substring
5785                              (progn
5786                                (end-of-line)
5787                                (search-backward ">" end t)
5788                                (1+ (point)))
5789                              (progn
5790                                (search-backward "<" end t)
5791                                (point))))))
5792                 ;; Get the references from the in-reply-to header if there
5793                 ;; were no references and the in-reply-to header looks
5794                 ;; promising.
5795                 (if (and (search-forward "\nin-reply-to:" nil t)
5796                          (setq in-reply-to (nnheader-header-value))
5797                          (string-match "<[^>]+>" in-reply-to))
5798                     (let (ref2)
5799                       (setq ref (substring in-reply-to (match-beginning 0)
5800                                            (match-end 0)))
5801                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5802                         (setq ref2 (substring in-reply-to (match-beginning 0)
5803                                               (match-end 0)))
5804                         (when (> (length ref2) (length ref))
5805                           (setq ref ref2)))
5806                       ref)
5807                   (setq ref nil))))
5808             ;; Chars.
5809             (progn
5810               (goto-char p)
5811               (if (search-forward "\nchars: " nil t)
5812                   (if (numberp (setq chars (ignore-errors (read cur))))
5813                       chars -1)
5814                 -1))
5815             ;; Lines.
5816             (progn
5817               (goto-char p)
5818               (if (search-forward "\nlines: " nil t)
5819                   (if (numberp (setq lines (ignore-errors (read cur))))
5820                       lines -1)
5821                 -1))
5822             ;; Xref.
5823             (progn
5824               (goto-char p)
5825               (and (search-forward "\nxref:" nil t)
5826                    (nnheader-header-value)))
5827             ;; Extra.
5828             (when gnus-extra-headers
5829               (let ((extra gnus-extra-headers)
5830                     out)
5831                 (while extra
5832                   (goto-char p)
5833                   (when (search-forward
5834                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5835                     (push (cons (car extra) (nnheader-header-value))
5836                           out))
5837                   (pop extra))
5838                 out))))
5839           (when (equal id ref)
5840             (setq ref nil))
5841
5842           (when gnus-alter-header-function
5843             (funcall gnus-alter-header-function header)
5844             (setq id (mail-header-id header)
5845                   ref (gnus-parent-id (mail-header-references header))))
5846
5847           (when (setq header
5848                       (gnus-dependencies-add-header
5849                        header dependencies force-new))
5850             (push header headers))
5851           (goto-char (point-max))
5852           (widen))
5853         (nreverse headers)))))
5854
5855 ;; Goes through the xover lines and returns a list of vectors
5856 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5857                                                   force-new dependencies
5858                                                   group also-fetch-heads)
5859   "Parse the news overview data in the server buffer.
5860 Return a list of headers that match SEQUENCE (see
5861 `nntp-retrieve-headers')."
5862   ;; Get the Xref when the users reads the articles since most/some
5863   ;; NNTP servers do not include Xrefs when using XOVER.
5864   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5865   (let ((mail-parse-charset gnus-newsgroup-charset)
5866         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5867         (cur nntp-server-buffer)
5868         (dependencies (or dependencies gnus-newsgroup-dependencies))
5869         (allp (cond
5870                ((eq gnus-read-all-available-headers t)
5871                 t)
5872                ((stringp gnus-read-all-available-headers)
5873                 (string-match gnus-read-all-available-headers group))
5874                (t
5875                 nil)))
5876         number headers header)
5877     (save-excursion
5878       (set-buffer nntp-server-buffer)
5879       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5880       ;; Allow the user to mangle the headers before parsing them.
5881       (gnus-run-hooks 'gnus-parse-headers-hook)
5882       (goto-char (point-min))
5883       (gnus-parse-without-error
5884         (while (and (or sequence allp)
5885                     (not (eobp)))
5886           (setq number (read cur))
5887           (when (not allp)
5888             (while (and sequence
5889                         (< (car sequence) number))
5890               (setq sequence (cdr sequence))))
5891           (when (and (or allp
5892                          (and sequence
5893                               (eq number (car sequence))))
5894                      (progn
5895                        (setq sequence (cdr sequence))
5896                        (setq header (inline
5897                                       (gnus-nov-parse-line
5898                                        number dependencies force-new)))))
5899             (push header headers))
5900           (forward-line 1)))
5901       ;; A common bug in inn is that if you have posted an article and
5902       ;; then retrieves the active file, it will answer correctly --
5903       ;; the new article is included.  However, a NOV entry for the
5904       ;; article may not have been generated yet, so this may fail.
5905       ;; We work around this problem by retrieving the last few
5906       ;; headers using HEAD.
5907       (if (or (not also-fetch-heads)
5908               (not sequence))
5909           ;; We (probably) got all the headers.
5910           (nreverse headers)
5911         (let ((gnus-nov-is-evil t))
5912           (nconc
5913            (nreverse headers)
5914            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5915              (gnus-get-newsgroup-headers))))))))
5916
5917 (defun gnus-article-get-xrefs ()
5918   "Fill in the Xref value in `gnus-current-headers', if necessary.
5919 This is meant to be called in `gnus-article-internal-prepare-hook'."
5920   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5921                                  gnus-current-headers)))
5922     (or (not gnus-use-cross-reference)
5923         (not headers)
5924         (and (mail-header-xref headers)
5925              (not (string= (mail-header-xref headers) "")))
5926         (let ((case-fold-search t)
5927               xref)
5928           (save-restriction
5929             (nnheader-narrow-to-headers)
5930             (goto-char (point-min))
5931             (when (or (and (not (eobp))
5932                            (eq (downcase (char-after)) ?x)
5933                            (looking-at "Xref:"))
5934                       (search-forward "\nXref:" nil t))
5935               (goto-char (1+ (match-end 0)))
5936               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5937               (mail-header-set-xref headers xref)))))))
5938
5939 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5940   "Find article ID and insert the summary line for that article.
5941 OLD-HEADER can either be a header or a line number to insert
5942 the subject line on."
5943   (let* ((line (and (numberp old-header) old-header))
5944          (old-header (and (vectorp old-header) old-header))
5945          (header (cond ((and old-header use-old-header)
5946                         old-header)
5947                        ((and (numberp id)
5948                              (gnus-number-to-header id))
5949                         (gnus-number-to-header id))
5950                        (t
5951                         (gnus-read-header id))))
5952          (number (and (numberp id) id))
5953          d)
5954     (when header
5955       ;; Rebuild the thread that this article is part of and go to the
5956       ;; article we have fetched.
5957       (when (and (not gnus-show-threads)
5958                  old-header)
5959         (when (and number
5960                    (setq d (gnus-data-find (mail-header-number old-header))))
5961           (goto-char (gnus-data-pos d))
5962           (gnus-data-remove
5963            number
5964            (- (gnus-point-at-bol)
5965               (prog1
5966                   (1+ (gnus-point-at-eol))
5967                 (gnus-delete-line))))))
5968       (when old-header
5969         (mail-header-set-number header (mail-header-number old-header)))
5970       (setq gnus-newsgroup-sparse
5971             (delq (setq number (mail-header-number header))
5972                   gnus-newsgroup-sparse))
5973       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5974       (push number gnus-newsgroup-limit)
5975       (gnus-rebuild-thread (mail-header-id header) line)
5976       (gnus-summary-goto-subject number nil t))
5977     (when (and (numberp number)
5978                (> number 0))
5979       ;; We have to update the boundaries even if we can't fetch the
5980       ;; article if ID is a number -- so that the next `P' or `N'
5981       ;; command will fetch the previous (or next) article even
5982       ;; if the one we tried to fetch this time has been canceled.
5983       (when (> number gnus-newsgroup-end)
5984         (setq gnus-newsgroup-end number))
5985       (when (< number gnus-newsgroup-begin)
5986         (setq gnus-newsgroup-begin number))
5987       (setq gnus-newsgroup-unselected
5988             (delq number gnus-newsgroup-unselected)))
5989     ;; Report back a success?
5990     (and header (mail-header-number header))))
5991
5992 ;;; Process/prefix in the summary buffer
5993
5994 (defun gnus-summary-work-articles (n)
5995   "Return a list of articles to be worked upon.
5996 The prefix argument, the list of process marked articles, and the
5997 current article will be taken into consideration."
5998   (save-excursion
5999     (set-buffer gnus-summary-buffer)
6000     (cond
6001      (n
6002       ;; A numerical prefix has been given.
6003       (setq n (prefix-numeric-value n))
6004       (let ((backward (< n 0))
6005             (n (abs (prefix-numeric-value n)))
6006             articles article)
6007         (save-excursion
6008           (while
6009               (and (> n 0)
6010                    (push (setq article (gnus-summary-article-number))
6011                          articles)
6012                    (if backward
6013                        (gnus-summary-find-prev nil article)
6014                      (gnus-summary-find-next nil article)))
6015             (decf n)))
6016         (nreverse articles)))
6017      ((and (gnus-region-active-p) (mark))
6018       (message "region active")
6019       ;; Work on the region between point and mark.
6020       (let ((max (max (point) (mark)))
6021             articles article)
6022         (save-excursion
6023           (goto-char (min (min (point) (mark))))
6024           (while
6025               (and
6026                (push (setq article (gnus-summary-article-number)) articles)
6027                (gnus-summary-find-next nil article)
6028                (< (point) max)))
6029           (nreverse articles))))
6030      (gnus-newsgroup-processable
6031       ;; There are process-marked articles present.
6032       ;; Save current state.
6033       (gnus-summary-save-process-mark)
6034       ;; Return the list.
6035       (reverse gnus-newsgroup-processable))
6036      (t
6037       ;; Just return the current article.
6038       (list (gnus-summary-article-number))))))
6039
6040 (defmacro gnus-summary-iterate (arg &rest forms)
6041   "Iterate over the process/prefixed articles and do FORMS.
6042 ARG is the interactive prefix given to the command.  FORMS will be
6043 executed with point over the summary line of the articles."
6044   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6045     `(let ((,articles (gnus-summary-work-articles ,arg)))
6046        (while ,articles
6047          (gnus-summary-goto-subject (car ,articles))
6048          ,@forms
6049          (pop ,articles)))))
6050
6051 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6052 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6053
6054 (defun gnus-summary-save-process-mark ()
6055   "Push the current set of process marked articles on the stack."
6056   (interactive)
6057   (push (copy-sequence gnus-newsgroup-processable)
6058         gnus-newsgroup-process-stack))
6059
6060 (defun gnus-summary-kill-process-mark ()
6061   "Push the current set of process marked articles on the stack and unmark."
6062   (interactive)
6063   (gnus-summary-save-process-mark)
6064   (gnus-summary-unmark-all-processable))
6065
6066 (defun gnus-summary-yank-process-mark ()
6067   "Pop the last process mark state off the stack and restore it."
6068   (interactive)
6069   (unless gnus-newsgroup-process-stack
6070     (error "Empty mark stack"))
6071   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6072
6073 (defun gnus-summary-process-mark-set (set)
6074   "Make SET into the current process marked articles."
6075   (gnus-summary-unmark-all-processable)
6076   (while set
6077     (gnus-summary-set-process-mark (pop set))))
6078
6079 ;;; Searching and stuff
6080
6081 (defun gnus-summary-search-group (&optional backward use-level)
6082   "Search for next unread newsgroup.
6083 If optional argument BACKWARD is non-nil, search backward instead."
6084   (save-excursion
6085     (set-buffer gnus-group-buffer)
6086     (when (gnus-group-search-forward
6087            backward nil (if use-level (gnus-group-group-level) nil))
6088       (gnus-group-group-name))))
6089
6090 (defun gnus-summary-best-group (&optional exclude-group)
6091   "Find the name of the best unread group.
6092 If EXCLUDE-GROUP, do not go to this group."
6093   (save-excursion
6094     (set-buffer gnus-group-buffer)
6095     (save-excursion
6096       (gnus-group-best-unread-group exclude-group))))
6097
6098 (defun gnus-summary-find-next (&optional unread article backward)
6099   (if backward
6100       (gnus-summary-find-prev unread article)
6101     (let* ((dummy (gnus-summary-article-intangible-p))
6102            (article (or article (gnus-summary-article-number)))
6103            (data (gnus-data-find-list article))
6104            result)
6105       (when (and (not dummy)
6106                  (or (not gnus-summary-check-current)
6107                      (not unread)
6108                      (not (gnus-data-unread-p (car data)))))
6109         (setq data (cdr data)))
6110       (when (setq result
6111                   (if unread
6112                       (progn
6113                         (while data
6114                           (unless (memq (gnus-data-number (car data)) 
6115                                         (cond
6116                                          ((eq gnus-auto-goto-ignores
6117                                               'always-undownloaded)
6118                                           gnus-newsgroup-undownloaded)
6119                                          (gnus-plugged
6120                                           nil)
6121                                          ((eq gnus-auto-goto-ignores
6122                                               'unfetched)
6123                                           gnus-newsgroup-unfetched)
6124                                          ((eq gnus-auto-goto-ignores
6125                                               'undownloaded)
6126                                           gnus-newsgroup-undownloaded)))
6127                             (when (gnus-data-unread-p (car data))
6128                               (setq result (car data)
6129                                     data nil)))
6130                           (setq data (cdr data)))
6131                         result)
6132                     (car data)))
6133         (goto-char (gnus-data-pos result))
6134         (gnus-data-number result)))))
6135
6136 (defun gnus-summary-find-prev (&optional unread article)
6137   (let* ((eobp (eobp))
6138          (article (or article (gnus-summary-article-number)))
6139          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6140          result)
6141     (when (and (not eobp)
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-subject (subject &optional unread backward article)
6173   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6174          (article (or article (gnus-summary-article-number)))
6175          (articles (gnus-data-list backward))
6176          (arts (gnus-data-find-list article articles))
6177          result)
6178     (when (or (not gnus-summary-check-current)
6179               (not unread)
6180               (not (gnus-data-unread-p (car arts))))
6181       (setq arts (cdr arts)))
6182     (while arts
6183       (and (or (not unread)
6184                (gnus-data-unread-p (car arts)))
6185            (vectorp (gnus-data-header (car arts)))
6186            (gnus-subject-equal
6187             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6188            (setq result (car arts)
6189                  arts nil))
6190       (setq arts (cdr arts)))
6191     (and result
6192          (goto-char (gnus-data-pos result))
6193          (gnus-data-number result))))
6194
6195 (defun gnus-summary-search-forward (&optional unread subject backward)
6196   "Search forward for an article.
6197 If UNREAD, look for unread articles.  If SUBJECT, look for
6198 articles with that subject.  If BACKWARD, search backward instead."
6199   (cond (subject (gnus-summary-find-subject subject unread backward))
6200         (backward (gnus-summary-find-prev unread))
6201         (t (gnus-summary-find-next unread))))
6202
6203 (defun gnus-recenter (&optional n)
6204   "Center point in window and redisplay frame.
6205 Also do horizontal recentering."
6206   (interactive "P")
6207   (when (and gnus-auto-center-summary
6208              (not (eq gnus-auto-center-summary 'vertical)))
6209     (gnus-horizontal-recenter))
6210   (recenter n))
6211
6212 (defun gnus-summary-recenter ()
6213   "Center point in the summary window.
6214 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6215 displayed, no centering will be performed."
6216   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6217   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6218   (interactive)
6219   ;; The user has to want it.
6220   (when gnus-auto-center-summary
6221     (let* ((top (cond ((< (window-height) 4) 0)
6222                       ((< (window-height) 7) 1)
6223                       (t (if (numberp gnus-auto-center-summary)
6224                              gnus-auto-center-summary
6225                            2))))
6226            (height (1- (window-height)))
6227            (bottom (save-excursion (goto-char (point-max))
6228                                    (forward-line (- height))
6229                                    (point)))
6230            (window (get-buffer-window (current-buffer))))
6231       (when (get-buffer-window gnus-article-buffer)
6232         ;; Only do recentering when the article buffer is displayed,
6233         ;; Set the window start to either `bottom', which is the biggest
6234         ;; possible valid number, or the second line from the top,
6235         ;; whichever is the least.
6236         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6237           (if (> bottom top-pos)
6238               ;; Keep the second line from the top visible
6239               (set-window-start window top-pos t)
6240             ;; Try to keep the bottom line visible; if it's partially
6241             ;; obscured, either scroll one more line to make it fully
6242             ;; visible, or revert to using TOP-POS.
6243             (save-excursion
6244               (goto-char (point-max))
6245               (forward-line -1)
6246               (let ((last-line-start (point)))
6247                 (goto-char bottom)
6248                 (set-window-start window (point) t)
6249                 (when (not (pos-visible-in-window-p last-line-start window))
6250                   (forward-line 1)
6251                   (set-window-start window (min (point) top-pos) t)))))))
6252       ;; Do horizontal recentering while we're at it.
6253       (when (and (get-buffer-window (current-buffer) t)
6254                  (not (eq gnus-auto-center-summary 'vertical)))
6255         (let ((selected (selected-window)))
6256           (select-window (get-buffer-window (current-buffer) t))
6257           (gnus-summary-position-point)
6258           (gnus-horizontal-recenter)
6259           (select-window selected))))))
6260
6261 (defun gnus-summary-jump-to-group (newsgroup)
6262   "Move point to NEWSGROUP in group mode buffer."
6263   ;; Keep update point of group mode buffer if visible.
6264   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6265       (save-window-excursion
6266         ;; Take care of tree window mode.
6267         (when (get-buffer-window gnus-group-buffer)
6268           (pop-to-buffer gnus-group-buffer))
6269         (gnus-group-jump-to-group newsgroup))
6270     (save-excursion
6271       ;; Take care of tree window mode.
6272       (if (get-buffer-window gnus-group-buffer)
6273           (pop-to-buffer gnus-group-buffer)
6274         (set-buffer gnus-group-buffer))
6275       (gnus-group-jump-to-group newsgroup))))
6276
6277 ;; This function returns a list of article numbers based on the
6278 ;; difference between the ranges of read articles in this group and
6279 ;; the range of active articles.
6280 (defun gnus-list-of-unread-articles (group)
6281   (let* ((read (gnus-info-read (gnus-get-info group)))
6282          (active (or (gnus-active group) (gnus-activate-group group)))
6283          (last (cdr active))
6284          first nlast unread)
6285     ;; If none are read, then all are unread.
6286     (if (not read)
6287         (setq first (car active))
6288       ;; If the range of read articles is a single range, then the
6289       ;; first unread article is the article after the last read
6290       ;; article.  Sounds logical, doesn't it?
6291       (if (and (not (listp (cdr read)))
6292                (or (< (car read) (car active))
6293                    (progn (setq read (list read))
6294                           nil)))
6295           (setq first (max (car active) (1+ (cdr read))))
6296         ;; `read' is a list of ranges.
6297         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6298                                   (caar read)))
6299                   1)
6300           (setq first (car active)))
6301         (while read
6302           (when first
6303             (while (< first nlast)
6304               (push first unread)
6305               (setq first (1+ first))))
6306           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6307           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6308           (setq read (cdr read)))))
6309     ;; And add the last unread articles.
6310     (while (<= first last)
6311       (push first unread)
6312       (setq first (1+ first)))
6313     ;; Return the list of unread articles.
6314     (delq 0 (nreverse unread))))
6315
6316 (defun gnus-list-of-read-articles (group)
6317   "Return a list of unread, unticked and non-dormant articles."
6318   (let* ((info (gnus-get-info group))
6319          (marked (gnus-info-marks info))
6320          (active (gnus-active group)))
6321     (and info active
6322          (gnus-list-range-difference
6323           (gnus-list-range-difference
6324            (gnus-sorted-complement
6325             (gnus-uncompress-range active)
6326             (gnus-list-of-unread-articles group))
6327            (cdr (assq 'dormant marked)))
6328           (cdr (assq 'tick marked))))))
6329
6330 ;; Various summary commands
6331
6332 (defun gnus-summary-select-article-buffer ()
6333   "Reconfigure windows to show article buffer."
6334   (interactive)
6335   (if (not (gnus-buffer-live-p gnus-article-buffer))
6336       (error "There is no article buffer for this summary buffer")
6337     (gnus-configure-windows 'article)
6338     (select-window (get-buffer-window gnus-article-buffer))))
6339
6340 (defun gnus-summary-universal-argument (arg)
6341   "Perform any operation on all articles that are process/prefixed."
6342   (interactive "P")
6343   (let ((articles (gnus-summary-work-articles arg))
6344         func article)
6345     (if (eq
6346          (setq
6347           func
6348           (key-binding
6349            (read-key-sequence
6350             (substitute-command-keys
6351              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6352          'undefined)
6353         (gnus-error 1 "Undefined key")
6354       (save-excursion
6355         (while articles
6356           (gnus-summary-goto-subject (setq article (pop articles)))
6357           (let (gnus-newsgroup-processable)
6358             (command-execute func))
6359           (gnus-summary-remove-process-mark article)))))
6360   (gnus-summary-position-point))
6361
6362 (defun gnus-summary-toggle-truncation (&optional arg)
6363   "Toggle truncation of summary lines.
6364 With arg, turn line truncation on if arg is positive."
6365   (interactive "P")
6366   (setq truncate-lines
6367         (if (null arg) (not truncate-lines)
6368           (> (prefix-numeric-value arg) 0)))
6369   (redraw-display))
6370
6371 (defun gnus-summary-find-for-reselect ()
6372   "Return the number of an article to stay on across a reselect.
6373 The current article is considered, then following articles, then previous
6374 articles.  An article is sought which is not cancelled and isn't a temporary
6375 insertion from another group.  If there's no such then return a dummy 0."
6376   (let (found)
6377     (dolist (rev '(nil t))
6378       (unless found      ; don't demand the reverse list if we don't need it
6379         (let ((data (gnus-data-find-list
6380                      (gnus-summary-article-number) (gnus-data-list rev))))
6381           (while (and data (not found))
6382             (if (and (< 0 (gnus-data-number (car data)))
6383                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6384                 (setq found (gnus-data-number (car data))))
6385             (setq data (cdr data))))))
6386     (or found 0)))
6387
6388 (defun gnus-summary-reselect-current-group (&optional all rescan)
6389   "Exit and then reselect the current newsgroup.
6390 The prefix argument ALL means to select all articles."
6391   (interactive "P")
6392   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6393     (error "Ephemeral groups can't be reselected"))
6394   (let ((current-subject (gnus-summary-find-for-reselect))
6395         (group gnus-newsgroup-name))
6396     (setq gnus-newsgroup-begin nil)
6397     (gnus-summary-exit nil 'leave-hidden)
6398     ;; We have to adjust the point of group mode buffer because
6399     ;; point was moved to the next unread newsgroup by exiting.
6400     (gnus-summary-jump-to-group group)
6401     (when rescan
6402       (save-excursion
6403         (gnus-group-get-new-news-this-group 1)))
6404     (gnus-group-read-group all t)
6405     (gnus-summary-goto-subject current-subject nil t)))
6406
6407 (defun gnus-summary-rescan-group (&optional all)
6408   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6409   (interactive "P")
6410   (gnus-summary-reselect-current-group all t))
6411
6412 (defun gnus-summary-update-info (&optional non-destructive)
6413   (save-excursion
6414     (let ((group gnus-newsgroup-name))
6415       (when group
6416         (when gnus-newsgroup-kill-headers
6417           (setq gnus-newsgroup-killed
6418                 (gnus-compress-sequence
6419                  (gnus-sorted-union
6420                   (gnus-list-range-intersection
6421                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6422                   gnus-newsgroup-unreads)
6423                  t)))
6424         (unless (listp (cdr gnus-newsgroup-killed))
6425           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6426         (let ((headers gnus-newsgroup-headers))
6427           ;; Set the new ranges of read articles.
6428           (save-excursion
6429             (set-buffer gnus-group-buffer)
6430             (gnus-undo-force-boundary))
6431           (gnus-update-read-articles
6432            group (gnus-sorted-union
6433                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6434           ;; Set the current article marks.
6435           (let ((gnus-newsgroup-scored
6436                  (if (and (not gnus-save-score)
6437                           (not non-destructive))
6438                      nil
6439                    gnus-newsgroup-scored)))
6440             (save-excursion
6441               (gnus-update-marks)))
6442           ;; Do the cross-ref thing.
6443           (when gnus-use-cross-reference
6444             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6445           ;; Do not switch windows but change the buffer to work.
6446           (set-buffer gnus-group-buffer)
6447           (unless (gnus-ephemeral-group-p group)
6448             (gnus-group-update-group group)))))))
6449
6450 (defun gnus-summary-save-newsrc (&optional force)
6451   "Save the current number of read/marked articles in the dribble buffer.
6452 The dribble buffer will then be saved.
6453 If FORCE (the prefix), also save the .newsrc file(s)."
6454   (interactive "P")
6455   (gnus-summary-update-info t)
6456   (if force
6457       (gnus-save-newsrc-file)
6458     (gnus-dribble-save)))
6459
6460 (defun gnus-summary-exit (&optional temporary leave-hidden)
6461   "Exit reading current newsgroup, and then return to group selection mode.
6462 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6463   (interactive)
6464   (gnus-set-global-variables)
6465   (when (gnus-buffer-live-p gnus-article-buffer)
6466     (save-excursion
6467       (set-buffer gnus-article-buffer)
6468       (mm-destroy-parts gnus-article-mime-handles)
6469       ;; Set it to nil for safety reason.
6470       (setq gnus-article-mime-handle-alist nil)
6471       (setq gnus-article-mime-handles nil)))
6472   (gnus-kill-save-kill-buffer)
6473   (gnus-async-halt-prefetch)
6474   (let* ((group gnus-newsgroup-name)
6475          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6476          (gnus-group-is-exiting-p t)
6477          (mode major-mode)
6478          (group-point nil)
6479          (buf (current-buffer)))
6480     (unless quit-config
6481       ;; Do adaptive scoring, and possibly save score files.
6482       (when gnus-newsgroup-adaptive
6483         (gnus-score-adaptive))
6484       (when gnus-use-scoring
6485         (gnus-score-save)))
6486     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6487     ;; If we have several article buffers, we kill them at exit.
6488     (unless gnus-single-article-buffer
6489       (gnus-kill-buffer gnus-original-article-buffer)
6490       (setq gnus-article-current nil))
6491     (when gnus-use-cache
6492       (gnus-cache-possibly-remove-articles)
6493       (gnus-cache-save-buffers))
6494     (gnus-async-prefetch-remove-group group)
6495     (when gnus-suppress-duplicates
6496       (gnus-dup-enter-articles))
6497     (when gnus-use-trees
6498       (gnus-tree-close group))
6499     (when gnus-use-cache
6500       (gnus-cache-write-active))
6501     ;; Remove entries for this group.
6502     (nnmail-purge-split-history (gnus-group-real-name group))
6503     ;; Make all changes in this group permanent.
6504     (unless quit-config
6505       (gnus-run-hooks 'gnus-exit-group-hook)
6506       (gnus-summary-update-info))
6507     (gnus-close-group group)
6508     ;; Make sure where we were, and go to next newsgroup.
6509     (set-buffer gnus-group-buffer)
6510     (unless quit-config
6511       (gnus-group-jump-to-group group))
6512     (gnus-run-hooks 'gnus-summary-exit-hook)
6513     (unless (or quit-config
6514                 ;; If this group has disappeared from the summary
6515                 ;; buffer, don't skip forwards.
6516                 (not (string= group (gnus-group-group-name))))
6517       (gnus-group-next-unread-group 1))
6518     (setq group-point (point))
6519     (if temporary
6520         nil                             ;Nothing to do.
6521       ;; If we have several article buffers, we kill them at exit.
6522       (unless gnus-single-article-buffer
6523         (gnus-kill-buffer gnus-article-buffer)
6524         (gnus-kill-buffer gnus-original-article-buffer)
6525         (setq gnus-article-current nil))
6526       (set-buffer buf)
6527       (if (not gnus-kill-summary-on-exit)
6528           (progn
6529             (gnus-deaden-summary)
6530             (setq mode nil))
6531         ;; We set all buffer-local variables to nil.  It is unclear why
6532         ;; this is needed, but if we don't, buffer-local variables are
6533         ;; not garbage-collected, it seems.  This would the lead to en
6534         ;; ever-growing Emacs.
6535         (gnus-summary-clear-local-variables)
6536         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6537           (gnus-summary-clear-local-variables))
6538         (when (get-buffer gnus-article-buffer)
6539           (bury-buffer gnus-article-buffer))
6540         ;; We clear the global counterparts of the buffer-local
6541         ;; variables as well, just to be on the safe side.
6542         (set-buffer gnus-group-buffer)
6543         (gnus-summary-clear-local-variables)
6544         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6545           (gnus-summary-clear-local-variables))
6546         ;; Return to group mode buffer.
6547         (when (eq mode 'gnus-summary-mode)
6548           (gnus-kill-buffer buf)))
6549       (setq gnus-current-select-method gnus-select-method)
6550       (if leave-hidden
6551           (set-buffer gnus-group-buffer)
6552         (pop-to-buffer gnus-group-buffer))
6553       (if (not quit-config)
6554           (progn
6555             (goto-char group-point)
6556             (unless leave-hidden
6557               (gnus-configure-windows 'group 'force)))
6558         (gnus-handle-ephemeral-exit quit-config))
6559       ;; Clear the current group name.
6560       (unless quit-config
6561         (setq gnus-newsgroup-name nil)))))
6562
6563 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6564 (defun gnus-summary-exit-no-update (&optional no-questions)
6565   "Quit reading current newsgroup without updating read article info."
6566   (interactive)
6567   (let* ((group gnus-newsgroup-name)
6568          (gnus-group-is-exiting-p t)
6569          (gnus-group-is-exiting-without-update-p t)
6570          (quit-config (gnus-group-quit-config group)))
6571     (when (or no-questions
6572               gnus-expert-user
6573               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6574       (gnus-async-halt-prefetch)
6575       (run-hooks 'gnus-summary-prepare-exit-hook)
6576       (when (gnus-buffer-live-p gnus-article-buffer)
6577         (save-excursion
6578           (set-buffer gnus-article-buffer)
6579           (mm-destroy-parts gnus-article-mime-handles)
6580           ;; Set it to nil for safety reason.
6581           (setq gnus-article-mime-handle-alist nil)
6582           (setq gnus-article-mime-handles nil)))
6583       ;; If we have several article buffers, we kill them at exit.
6584       (unless gnus-single-article-buffer
6585         (gnus-kill-buffer gnus-article-buffer)
6586         (gnus-kill-buffer gnus-original-article-buffer)
6587         (setq gnus-article-current nil))
6588       (if (not gnus-kill-summary-on-exit)
6589           (gnus-deaden-summary)
6590         (gnus-close-group group)
6591         (gnus-summary-clear-local-variables)
6592         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6593           (gnus-summary-clear-local-variables))
6594         (set-buffer gnus-group-buffer)
6595         (gnus-summary-clear-local-variables)
6596         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6597           (gnus-summary-clear-local-variables))
6598         (gnus-kill-buffer gnus-summary-buffer))
6599       (unless gnus-single-article-buffer
6600         (setq gnus-article-current nil))
6601       (when gnus-use-trees
6602         (gnus-tree-close group))
6603       (gnus-async-prefetch-remove-group group)
6604       (when (get-buffer gnus-article-buffer)
6605         (bury-buffer gnus-article-buffer))
6606       ;; Return to the group buffer.
6607       (gnus-configure-windows 'group 'force)
6608       ;; Clear the current group name.
6609       (setq gnus-newsgroup-name nil)
6610       (unless (gnus-ephemeral-group-p group)
6611         (gnus-group-update-group group))
6612       (when (equal (gnus-group-group-name) group)
6613         (gnus-group-next-unread-group 1))
6614       (when quit-config
6615         (gnus-handle-ephemeral-exit quit-config)))))
6616
6617 (defun gnus-handle-ephemeral-exit (quit-config)
6618   "Handle movement when leaving an ephemeral group.
6619 The state which existed when entering the ephemeral is reset."
6620   (if (not (buffer-name (car quit-config)))
6621       (gnus-configure-windows 'group 'force)
6622     (set-buffer (car quit-config))
6623     (cond ((eq major-mode 'gnus-summary-mode)
6624            (gnus-set-global-variables))
6625           ((eq major-mode 'gnus-article-mode)
6626            (save-excursion
6627              ;; The `gnus-summary-buffer' variable may point
6628              ;; to the old summary buffer when using a single
6629              ;; article buffer.
6630              (unless (gnus-buffer-live-p gnus-summary-buffer)
6631                (set-buffer gnus-group-buffer))
6632              (set-buffer gnus-summary-buffer)
6633              (gnus-set-global-variables))))
6634     (if (or (eq (cdr quit-config) 'article)
6635             (eq (cdr quit-config) 'pick))
6636         (progn
6637           ;; The current article may be from the ephemeral group
6638           ;; thus it is best that we reload this article
6639           ;;
6640           ;; If we're exiting from a large digest, this can be
6641           ;; extremely slow.  So, it's better not to reload it. -- jh.
6642           ;;(gnus-summary-show-article)
6643           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6644               (gnus-configure-windows 'pick 'force)
6645             (gnus-configure-windows (cdr quit-config) 'force)))
6646       (gnus-configure-windows (cdr quit-config) 'force))
6647     (when (eq major-mode 'gnus-summary-mode)
6648       (gnus-summary-next-subject 1 nil t)
6649       (gnus-summary-recenter)
6650       (gnus-summary-position-point))))
6651
6652 ;;; Dead summaries.
6653
6654 (defvar gnus-dead-summary-mode-map nil)
6655
6656 (unless gnus-dead-summary-mode-map
6657   (setq gnus-dead-summary-mode-map (make-keymap))
6658   (suppress-keymap gnus-dead-summary-mode-map)
6659   (substitute-key-definition
6660    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6661   (dolist (key '("\C-d" "\r" "\177" [delete]))
6662     (define-key gnus-dead-summary-mode-map
6663       key 'gnus-summary-wake-up-the-dead))
6664   (dolist (key '("q" "Q"))
6665     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6666
6667 (defvar gnus-dead-summary-mode nil
6668   "Minor mode for Gnus summary buffers.")
6669
6670 (defun gnus-dead-summary-mode (&optional arg)
6671   "Minor mode for Gnus summary buffers."
6672   (interactive "P")
6673   (when (eq major-mode 'gnus-summary-mode)
6674     (make-local-variable 'gnus-dead-summary-mode)
6675     (setq gnus-dead-summary-mode
6676           (if (null arg) (not gnus-dead-summary-mode)
6677             (> (prefix-numeric-value arg) 0)))
6678     (when gnus-dead-summary-mode
6679       (gnus-add-minor-mode
6680        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6681
6682 (defun gnus-deaden-summary ()
6683   "Make the current summary buffer into a dead summary buffer."
6684   ;; Kill any previous dead summary buffer.
6685   (when (and gnus-dead-summary
6686              (buffer-name gnus-dead-summary))
6687     (save-excursion
6688       (set-buffer gnus-dead-summary)
6689       (when gnus-dead-summary-mode
6690         (kill-buffer (current-buffer)))))
6691   ;; Make this the current dead summary.
6692   (setq gnus-dead-summary (current-buffer))
6693   (gnus-dead-summary-mode 1)
6694   (let ((name (buffer-name)))
6695     (when (string-match "Summary" name)
6696       (rename-buffer
6697        (concat (substring name 0 (match-beginning 0)) "Dead "
6698                (substring name (match-beginning 0)))
6699        t)
6700       (bury-buffer))))
6701
6702 (defun gnus-kill-or-deaden-summary (buffer)
6703   "Kill or deaden the summary BUFFER."
6704   (save-excursion
6705     (when (and (buffer-name buffer)
6706                (not gnus-single-article-buffer))
6707       (save-excursion
6708         (set-buffer buffer)
6709         (gnus-kill-buffer gnus-article-buffer)
6710         (gnus-kill-buffer gnus-original-article-buffer)))
6711     (cond
6712      ;; Kill the buffer.
6713      (gnus-kill-summary-on-exit
6714       (when (and gnus-use-trees
6715                  (gnus-buffer-exists-p buffer))
6716         (save-excursion
6717           (set-buffer buffer)
6718           (gnus-tree-close gnus-newsgroup-name)))
6719       (gnus-kill-buffer buffer))
6720      ;; Deaden the buffer.
6721      ((gnus-buffer-exists-p buffer)
6722       (save-excursion
6723         (set-buffer buffer)
6724         (gnus-deaden-summary))))))
6725
6726 (defun gnus-summary-wake-up-the-dead (&rest args)
6727   "Wake up the dead summary buffer."
6728   (interactive)
6729   (gnus-dead-summary-mode -1)
6730   (let ((name (buffer-name)))
6731     (when (string-match "Dead " name)
6732       (rename-buffer
6733        (concat (substring name 0 (match-beginning 0))
6734                (substring name (match-end 0)))
6735        t)))
6736   (gnus-message 3 "This dead summary is now alive again"))
6737
6738 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6739 (defun gnus-summary-fetch-faq (&optional faq-dir)
6740   "Fetch the FAQ for the current group.
6741 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6742 in."
6743   (interactive
6744    (list
6745     (when current-prefix-arg
6746       (completing-read
6747        "FAQ dir: " (and (listp gnus-group-faq-directory)
6748                         (mapcar (lambda (file) (list file))
6749                                 gnus-group-faq-directory))))))
6750   (let (gnus-faq-buffer)
6751     (when (setq gnus-faq-buffer
6752                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6753       (gnus-configure-windows 'summary-faq))))
6754
6755 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6756 (defun gnus-summary-describe-group (&optional force)
6757   "Describe the current newsgroup."
6758   (interactive "P")
6759   (gnus-group-describe-group force gnus-newsgroup-name))
6760
6761 (defun gnus-summary-describe-briefly ()
6762   "Describe summary mode commands briefly."
6763   (interactive)
6764   (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")))
6765
6766 ;; Walking around group mode buffer from summary mode.
6767
6768 (defun gnus-summary-next-group (&optional no-article target-group backward)
6769   "Exit current newsgroup and then select next unread newsgroup.
6770 If prefix argument NO-ARTICLE is non-nil, no article is selected
6771 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6772 previous group instead."
6773   (interactive "P")
6774   ;; Stop pre-fetching.
6775   (gnus-async-halt-prefetch)
6776   (let ((current-group gnus-newsgroup-name)
6777         (current-buffer (current-buffer))
6778         entered)
6779     ;; First we semi-exit this group to update Xrefs and all variables.
6780     ;; We can't do a real exit, because the window conf must remain
6781     ;; the same in case the user is prompted for info, and we don't
6782     ;; want the window conf to change before that...
6783     (gnus-summary-exit t)
6784     (while (not entered)
6785       ;; Then we find what group we are supposed to enter.
6786       (set-buffer gnus-group-buffer)
6787       (gnus-group-jump-to-group current-group)
6788       (setq target-group
6789             (or target-group
6790                 (if (eq gnus-keep-same-level 'best)
6791                     (gnus-summary-best-group gnus-newsgroup-name)
6792                   (gnus-summary-search-group backward gnus-keep-same-level))))
6793       (if (not target-group)
6794           ;; There are no further groups, so we return to the group
6795           ;; buffer.
6796           (progn
6797             (gnus-message 5 "Returning to the group buffer")
6798             (setq entered t)
6799             (when (gnus-buffer-live-p current-buffer)
6800               (set-buffer current-buffer)
6801               (gnus-summary-exit))
6802             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6803         ;; We try to enter the target group.
6804         (gnus-group-jump-to-group target-group)
6805         (let ((unreads (gnus-group-group-unread)))
6806           (if (and (or (eq t unreads)
6807                        (and unreads (not (zerop unreads))))
6808                    (gnus-summary-read-group
6809                     target-group nil no-article
6810                     (and (buffer-name current-buffer) current-buffer)
6811                     nil backward))
6812               (setq entered t)
6813             (setq current-group target-group
6814                   target-group nil)))))))
6815
6816 (defun gnus-summary-prev-group (&optional no-article)
6817   "Exit current newsgroup and then select previous unread newsgroup.
6818 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6819   (interactive "P")
6820   (gnus-summary-next-group no-article nil t))
6821
6822 ;; Walking around summary lines.
6823
6824 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6825   "Go to the first subject satisfying any non-nil constraint.
6826 If UNREAD is non-nil, the article should be unread.
6827 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6828 If UNSEED is non-nil, the article should be unseen.
6829 Returns the article selected or nil if there are no matching articles."
6830   (interactive "P")
6831   (cond
6832    ;; Empty summary.
6833    ((null gnus-newsgroup-data)
6834     (gnus-message 3 "No articles in the group")
6835     nil)
6836    ;; Pick the first article.
6837    ((not (or unread undownloaded unseen))
6838     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6839     (gnus-data-number (car gnus-newsgroup-data)))
6840    ;; Find the first unread article.
6841    (t
6842     (let ((data gnus-newsgroup-data))
6843       (while (and data
6844                   (let ((num (gnus-data-number (car data))))
6845                     (or (memq num gnus-newsgroup-unfetched)
6846                         (not (or (and unread
6847                                       (memq num gnus-newsgroup-unreads))
6848                                  (and undownloaded
6849                                       (memq num gnus-newsgroup-undownloaded))
6850                                  (and unseen
6851                                       (memq num gnus-newsgroup-unseen)))))))
6852         (setq data (cdr data)))
6853       (prog1 
6854           (if data
6855               (progn
6856                 (goto-char (gnus-data-pos (car data)))
6857                 (gnus-data-number (car data)))
6858             (gnus-message 3 "No more%s articles"
6859                           (let* ((r (when unread " unread"))
6860                                  (d (when undownloaded " undownloaded"))
6861                                  (s (when unseen " unseen"))
6862                                  (l (delq nil (list r d s))))
6863                             (cond ((= 3 (length l))
6864                                    (concat r "," d ", or" s))
6865                                   ((= 2 (length l))
6866                                    (concat (car l) ", or" (cadr l)))
6867                                   ((= 1 (length l))
6868                                    (car l))
6869                                   (t
6870                                    ""))))
6871             nil
6872             )
6873         (gnus-summary-position-point))))))
6874
6875 (defun gnus-summary-next-subject (n &optional unread dont-display)
6876   "Go to next N'th summary line.
6877 If N is negative, go to the previous N'th subject line.
6878 If UNREAD is non-nil, only unread articles are selected.
6879 The difference between N and the actual number of steps taken is
6880 returned."
6881   (interactive "p")
6882   (let ((backward (< n 0))
6883         (n (abs n)))
6884     (while (and (> n 0)
6885                 (if backward
6886                     (gnus-summary-find-prev unread)
6887                   (gnus-summary-find-next unread)))
6888       (unless (zerop (setq n (1- n)))
6889         (gnus-summary-show-thread)))
6890     (when (/= 0 n)
6891       (gnus-message 7 "No more%s articles"
6892                     (if unread " unread" "")))
6893     (unless dont-display
6894       (gnus-summary-recenter)
6895       (gnus-summary-position-point))
6896     n))
6897
6898 (defun gnus-summary-next-unread-subject (n)
6899   "Go to next N'th unread summary line."
6900   (interactive "p")
6901   (gnus-summary-next-subject n t))
6902
6903 (defun gnus-summary-prev-subject (n &optional unread)
6904   "Go to previous N'th summary line.
6905 If optional argument UNREAD is non-nil, only unread article is selected."
6906   (interactive "p")
6907   (gnus-summary-next-subject (- n) unread))
6908
6909 (defun gnus-summary-prev-unread-subject (n)
6910   "Go to previous N'th unread summary line."
6911   (interactive "p")
6912   (gnus-summary-next-subject (- n) t))
6913
6914 (defun gnus-summary-goto-subjects (articles)
6915   "Insert the subject header for ARTICLES in the current buffer."
6916   (save-excursion
6917     (dolist (article articles)
6918       (gnus-summary-goto-subject article t)))
6919   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6920   (gnus-summary-position-point))
6921  
6922 (defun gnus-summary-goto-subject (article &optional force silent)
6923   "Go the subject line of ARTICLE.
6924 If FORCE, also allow jumping to articles not currently shown."
6925   (interactive "nArticle number: ")
6926   (unless (numberp article)
6927     (error "Article %s is not a number" article))
6928   (let ((b (point))
6929         (data (gnus-data-find article)))
6930     ;; We read in the article if we have to.
6931     (and (not data)
6932          force
6933          (gnus-summary-insert-subject
6934           article
6935           (if (or (numberp force) (vectorp force)) force)
6936           t)
6937          (setq data (gnus-data-find article)))
6938     (goto-char b)
6939     (if (not data)
6940         (progn
6941           (unless silent
6942             (gnus-message 3 "Can't find article %d" article))
6943           nil)
6944       (let ((pt (gnus-data-pos data)))
6945         (goto-char pt)
6946         (gnus-summary-set-article-display-arrow pt))
6947       (gnus-summary-position-point)
6948       article)))
6949
6950 ;; Walking around summary lines with displaying articles.
6951
6952 (defun gnus-summary-expand-window (&optional arg)
6953   "Make the summary buffer take up the entire Emacs frame.
6954 Given a prefix, will force an `article' buffer configuration."
6955   (interactive "P")
6956   (if arg
6957       (gnus-configure-windows 'article 'force)
6958     (gnus-configure-windows 'summary 'force)))
6959
6960 (defun gnus-summary-display-article (article &optional all-header)
6961   "Display ARTICLE in article buffer."
6962   (when (gnus-buffer-live-p gnus-article-buffer)
6963     (with-current-buffer gnus-article-buffer
6964       (mm-enable-multibyte)))
6965   (gnus-set-global-variables)
6966   (when (gnus-buffer-live-p gnus-article-buffer)
6967     (with-current-buffer gnus-article-buffer
6968       (setq gnus-article-charset gnus-newsgroup-charset)
6969       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6970       (mm-enable-multibyte)))
6971   (if (null article)
6972       nil
6973     (prog1
6974         (if gnus-summary-display-article-function
6975             (funcall gnus-summary-display-article-function article all-header)
6976           (gnus-article-prepare article all-header))
6977       (gnus-run-hooks 'gnus-select-article-hook)
6978       (when (and gnus-current-article
6979                  (not (zerop gnus-current-article)))
6980         (gnus-summary-goto-subject gnus-current-article))
6981       (gnus-summary-recenter)
6982       (when (and gnus-use-trees gnus-show-threads)
6983         (gnus-possibly-generate-tree article)
6984         (gnus-highlight-selected-tree article))
6985       ;; Successfully display article.
6986       (gnus-article-set-window-start
6987        (cdr (assq article gnus-newsgroup-bookmarks))))))
6988
6989 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6990   "Select the current article.
6991 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6992 non-nil, the article will be re-fetched even if it already present in
6993 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6994 be displayed."
6995   ;; Make sure we are in the summary buffer to work around bbdb bug.
6996   (unless (eq major-mode 'gnus-summary-mode)
6997     (set-buffer gnus-summary-buffer))
6998   (let ((article (or article (gnus-summary-article-number)))
6999         (all-headers (not (not all-headers))) ;Must be t or nil.
7000         gnus-summary-display-article-function)
7001     (and (not pseudo)
7002          (gnus-summary-article-pseudo-p article)
7003          (error "This is a pseudo-article"))
7004     (save-excursion
7005       (set-buffer gnus-summary-buffer)
7006       (if (or (and gnus-single-article-buffer
7007                    (or (null gnus-current-article)
7008                        (null gnus-article-current)
7009                        (null (get-buffer gnus-article-buffer))
7010                        (not (eq article (cdr gnus-article-current)))
7011                        (not (equal (car gnus-article-current)
7012                                    gnus-newsgroup-name))))
7013               (and (not gnus-single-article-buffer)
7014                    (or (null gnus-current-article)
7015                        (not (eq gnus-current-article article))))
7016               force)
7017           ;; The requested article is different from the current article.
7018           (progn
7019             (gnus-summary-display-article article all-headers)
7020             (when (gnus-buffer-live-p gnus-article-buffer)
7021               (with-current-buffer gnus-article-buffer
7022                 (if (not gnus-article-decoded-p) ;; a local variable
7023                     (mm-disable-multibyte))))
7024             (gnus-article-set-window-start
7025              (cdr (assq article gnus-newsgroup-bookmarks)))
7026             article)
7027         'old))))
7028
7029 (defun gnus-summary-force-verify-and-decrypt ()
7030   "Display buttons for signed/encrypted parts and verify/decrypt them."
7031   (interactive)
7032   (let ((mm-verify-option 'known)
7033         (mm-decrypt-option 'known)
7034         (gnus-buttonized-mime-types (append (list "multipart/signed"
7035                                                   "multipart/encrypted")
7036                                             gnus-buttonized-mime-types)))
7037     (gnus-summary-select-article nil 'force)))
7038
7039 (defun gnus-summary-set-current-mark (&optional current-mark)
7040   "Obsolete function."
7041   nil)
7042
7043 (defun gnus-summary-next-article (&optional unread subject backward push)
7044   "Select the next article.
7045 If UNREAD, only unread articles are selected.
7046 If SUBJECT, only articles with SUBJECT are selected.
7047 If BACKWARD, the previous article is selected instead of the next."
7048   (interactive "P")
7049   (cond
7050    ;; Is there such an article?
7051    ((and (gnus-summary-search-forward unread subject backward)
7052          (or (gnus-summary-display-article (gnus-summary-article-number))
7053              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7054     (gnus-summary-position-point))
7055    ;; If not, we try the first unread, if that is wanted.
7056    ((and subject
7057          gnus-auto-select-same
7058          (gnus-summary-first-unread-article))
7059     (gnus-summary-position-point)
7060     (gnus-message 6 "Wrapped"))
7061    ;; Try to get next/previous article not displayed in this group.
7062    ((and gnus-auto-extend-newsgroup
7063          (not unread) (not subject))
7064     (gnus-summary-goto-article
7065      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7066      nil (count-lines (point-min) (point))))
7067    ;; Go to next/previous group.
7068    (t
7069     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7070       (gnus-summary-jump-to-group gnus-newsgroup-name))
7071     (let ((cmd last-command-char)
7072           (point
7073            (save-excursion
7074              (set-buffer gnus-group-buffer)
7075              (point)))
7076           (group
7077            (if (eq gnus-keep-same-level 'best)
7078                (gnus-summary-best-group gnus-newsgroup-name)
7079              (gnus-summary-search-group backward gnus-keep-same-level))))
7080       ;; For some reason, the group window gets selected.  We change
7081       ;; it back.
7082       (select-window (get-buffer-window (current-buffer)))
7083       ;; Select next unread newsgroup automagically.
7084       (cond
7085        ((or (not gnus-auto-select-next)
7086             (not cmd))
7087         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7088        ((or (eq gnus-auto-select-next 'quietly)
7089             (and (eq gnus-auto-select-next 'slightly-quietly)
7090                  push)
7091             (and (eq gnus-auto-select-next 'almost-quietly)
7092                  (gnus-summary-last-article-p)))
7093         ;; Select quietly.
7094         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7095             (gnus-summary-exit)
7096           (gnus-message 7 "No more%s articles (%s)..."
7097                         (if unread " unread" "")
7098                         (if group (concat "selecting " group)
7099                           "exiting"))
7100           (gnus-summary-next-group nil group backward)))
7101        (t
7102         (when (gnus-key-press-event-p last-input-event)
7103           (gnus-summary-walk-group-buffer
7104            gnus-newsgroup-name cmd unread backward point))))))))
7105
7106 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7107   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7108                       (?\C-p (gnus-group-prev-unread-group 1))))
7109         (cursor-in-echo-area t)
7110         keve key group ended prompt)
7111     (save-excursion
7112       (set-buffer gnus-group-buffer)
7113       (goto-char start)
7114       (setq group
7115             (if (eq gnus-keep-same-level 'best)
7116                 (gnus-summary-best-group gnus-newsgroup-name)
7117               (gnus-summary-search-group backward gnus-keep-same-level))))
7118     (while (not ended)
7119       (setq prompt
7120             (format
7121              "No more%s articles%s " (if unread " unread" "")
7122              (if (and group
7123                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7124                  (format " (Type %s for %s [%s])"
7125                          (single-key-description cmd) group
7126                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7127                (format " (Type %s to exit %s)"
7128                        (single-key-description cmd)
7129                        gnus-newsgroup-name))))
7130       ;; Confirm auto selection.
7131       (setq key (car (setq keve (gnus-read-event-char prompt)))
7132             ended t)
7133       (cond
7134        ((assq key keystrokes)
7135         (let ((obuf (current-buffer)))
7136           (switch-to-buffer gnus-group-buffer)
7137           (when group
7138             (gnus-group-jump-to-group group))
7139           (eval (cadr (assq key keystrokes)))
7140           (setq group (gnus-group-group-name))
7141           (switch-to-buffer obuf))
7142         (setq ended nil))
7143        ((equal key cmd)
7144         (if (or (not group)
7145                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7146             (gnus-summary-exit)
7147           (gnus-summary-next-group nil group backward)))
7148        (t
7149         (push (cdr keve) unread-command-events))))))
7150
7151 (defun gnus-summary-next-unread-article ()
7152   "Select unread article after current one."
7153   (interactive)
7154   (gnus-summary-next-article
7155    (or (not (eq gnus-summary-goto-unread 'never))
7156        (gnus-summary-last-article-p (gnus-summary-article-number)))
7157    (and gnus-auto-select-same
7158         (gnus-summary-article-subject))))
7159
7160 (defun gnus-summary-prev-article (&optional unread subject)
7161   "Select the article after the current one.
7162 If UNREAD is non-nil, only unread articles are selected."
7163   (interactive "P")
7164   (gnus-summary-next-article unread subject t))
7165
7166 (defun gnus-summary-prev-unread-article ()
7167   "Select unread article before current one."
7168   (interactive)
7169   (gnus-summary-prev-article
7170    (or (not (eq gnus-summary-goto-unread 'never))
7171        (gnus-summary-first-article-p (gnus-summary-article-number)))
7172    (and gnus-auto-select-same
7173         (gnus-summary-article-subject))))
7174
7175 (defun gnus-summary-next-page (&optional lines circular stop)
7176   "Show next page of the selected article.
7177 If at the end of the current article, select the next article.
7178 LINES says how many lines should be scrolled up.
7179
7180 If CIRCULAR is non-nil, go to the start of the article instead of
7181 selecting the next article when reaching the end of the current
7182 article.
7183
7184 If STOP is non-nil, just stop when reaching the end of the message.
7185
7186 Also see the variable `gnus-article-skip-boring'."
7187   (interactive "P")
7188   (setq gnus-summary-buffer (current-buffer))
7189   (gnus-set-global-variables)
7190   (let ((article (gnus-summary-article-number))
7191         (article-window (get-buffer-window gnus-article-buffer t))
7192         endp)
7193     ;; If the buffer is empty, we have no article.
7194     (unless article
7195       (error "No article to select"))
7196     (gnus-configure-windows 'article)
7197     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7198         (if (and (eq gnus-summary-goto-unread 'never)
7199                  (not (gnus-summary-last-article-p article)))
7200             (gnus-summary-next-article)
7201           (gnus-summary-next-unread-article))
7202       (if (or (null gnus-current-article)
7203               (null gnus-article-current)
7204               (/= article (cdr gnus-article-current))
7205               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7206           ;; Selected subject is different from current article's.
7207           (gnus-summary-display-article article)
7208         (when article-window
7209           (gnus-eval-in-buffer-window gnus-article-buffer
7210             (setq endp (or (gnus-article-next-page lines)
7211                            (gnus-article-only-boring-p))))
7212           (when endp
7213             (cond (stop
7214                    (gnus-message 3 "End of message"))
7215                   (circular
7216                    (gnus-summary-beginning-of-article))
7217                   (lines
7218                    (gnus-message 3 "End of message"))
7219                   ((null lines)
7220                    (if (and (eq gnus-summary-goto-unread 'never)
7221                             (not (gnus-summary-last-article-p article)))
7222                        (gnus-summary-next-article)
7223                      (gnus-summary-next-unread-article))))))))
7224     (gnus-summary-recenter)
7225     (gnus-summary-position-point)))
7226
7227 (defun gnus-summary-prev-page (&optional lines move)
7228   "Show previous page of selected article.
7229 Argument LINES specifies lines to be scrolled down.
7230 If MOVE, move to the previous unread article if point is at
7231 the beginning of the buffer."
7232   (interactive "P")
7233   (let ((article (gnus-summary-article-number))
7234         (article-window (get-buffer-window gnus-article-buffer t))
7235         endp)
7236     (gnus-configure-windows 'article)
7237     (if (or (null gnus-current-article)
7238             (null gnus-article-current)
7239             (/= article (cdr gnus-article-current))
7240             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7241         ;; Selected subject is different from current article's.
7242         (gnus-summary-display-article article)
7243       (gnus-summary-recenter)
7244       (when article-window
7245         (gnus-eval-in-buffer-window gnus-article-buffer
7246           (setq endp (gnus-article-prev-page lines)))
7247         (when (and move endp)
7248           (cond (lines
7249                  (gnus-message 3 "Beginning of message"))
7250                 ((null lines)
7251                  (if (and (eq gnus-summary-goto-unread 'never)
7252                           (not (gnus-summary-first-article-p article)))
7253                      (gnus-summary-prev-article)
7254                    (gnus-summary-prev-unread-article))))))))
7255   (gnus-summary-position-point))
7256
7257 (defun gnus-summary-prev-page-or-article (&optional lines)
7258   "Show previous page of selected article.
7259 Argument LINES specifies lines to be scrolled down.
7260 If at the beginning of the article, go to the next article."
7261   (interactive "P")
7262   (gnus-summary-prev-page lines t))
7263
7264 (defun gnus-summary-scroll-up (lines)
7265   "Scroll up (or down) one line current article.
7266 Argument LINES specifies lines to be scrolled up (or down if negative)."
7267   (interactive "p")
7268   (gnus-configure-windows 'article)
7269   (gnus-summary-show-thread)
7270   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7271     (gnus-eval-in-buffer-window gnus-article-buffer
7272       (cond ((> lines 0)
7273              (when (gnus-article-next-page lines)
7274                (gnus-message 3 "End of message")))
7275             ((< lines 0)
7276              (gnus-article-prev-page (- lines))))))
7277   (gnus-summary-recenter)
7278   (gnus-summary-position-point))
7279
7280 (defun gnus-summary-scroll-down (lines)
7281   "Scroll down (or up) one line current article.
7282 Argument LINES specifies lines to be scrolled down (or up if negative)."
7283   (interactive "p")
7284   (gnus-summary-scroll-up (- lines)))
7285
7286 (defun gnus-summary-next-same-subject ()
7287   "Select next article which has the same subject as current one."
7288   (interactive)
7289   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7290
7291 (defun gnus-summary-prev-same-subject ()
7292   "Select previous article which has the same subject as current one."
7293   (interactive)
7294   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7295
7296 (defun gnus-summary-next-unread-same-subject ()
7297   "Select next unread article which has the same subject as current one."
7298   (interactive)
7299   (gnus-summary-next-article t (gnus-summary-article-subject)))
7300
7301 (defun gnus-summary-prev-unread-same-subject ()
7302   "Select previous unread article which has the same subject as current one."
7303   (interactive)
7304   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7305
7306 (defun gnus-summary-first-unread-article ()
7307   "Select the first unread article.
7308 Return nil if there are no unread articles."
7309   (interactive)
7310   (prog1
7311       (when (gnus-summary-first-subject t)
7312         (gnus-summary-show-thread)
7313         (gnus-summary-first-subject t)
7314         (gnus-summary-display-article (gnus-summary-article-number)))
7315     (gnus-summary-position-point)))
7316
7317 (defun gnus-summary-first-unread-subject ()
7318   "Place the point on the subject line of the first unread article.
7319 Return nil if there are no unread articles."
7320   (interactive)
7321   (prog1
7322       (when (gnus-summary-first-subject t)
7323         (gnus-summary-show-thread)
7324         (gnus-summary-first-subject t))
7325     (gnus-summary-position-point)))
7326
7327 (defun gnus-summary-first-unseen-subject ()
7328   "Place the point on the subject line of the first unseen article.
7329 Return nil if there are no unseen articles."
7330   (interactive)
7331   (prog1
7332       (when (gnus-summary-first-subject nil nil t)
7333         (gnus-summary-show-thread)
7334         (gnus-summary-first-subject nil nil t))
7335     (gnus-summary-position-point)))
7336
7337 (defun gnus-summary-first-unseen-or-unread-subject ()
7338   "Place the point on the subject line of the first unseen article or,
7339 if all article have been seen, on the subject line of the first unread
7340 article."
7341   (interactive)
7342   (prog1
7343       (unless (when (gnus-summary-first-subject nil nil t)
7344                 (gnus-summary-show-thread)
7345                 (gnus-summary-first-subject nil nil t))
7346         (when (gnus-summary-first-subject t)
7347           (gnus-summary-show-thread)
7348           (gnus-summary-first-subject t)))
7349     (gnus-summary-position-point)))
7350
7351 (defun gnus-summary-first-article ()
7352   "Select the first article.
7353 Return nil if there are no articles."
7354   (interactive)
7355   (prog1
7356       (when (gnus-summary-first-subject)
7357         (gnus-summary-show-thread)
7358         (gnus-summary-first-subject)
7359         (gnus-summary-display-article (gnus-summary-article-number)))
7360     (gnus-summary-position-point)))
7361
7362 (defun gnus-summary-best-unread-article (&optional arg)
7363   "Select the unread article with the highest score.
7364 If given a prefix argument, select the next unread article that has a
7365 score higher than the default score."
7366   (interactive "P")
7367   (let ((article (if arg
7368                      (gnus-summary-better-unread-subject)
7369                    (gnus-summary-best-unread-subject))))
7370     (if article
7371         (gnus-summary-goto-article article)
7372       (error "No unread articles"))))
7373
7374 (defun gnus-summary-best-unread-subject ()
7375   "Select the unread subject with the highest score."
7376   (interactive)
7377   (let ((best -1000000)
7378         (data gnus-newsgroup-data)
7379         article score)
7380     (while data
7381       (and (gnus-data-unread-p (car data))
7382            (> (setq score
7383                     (gnus-summary-article-score (gnus-data-number (car data))))
7384               best)
7385            (setq best score
7386                  article (gnus-data-number (car data))))
7387       (setq data (cdr data)))
7388     (when article
7389       (gnus-summary-goto-subject article))
7390     (gnus-summary-position-point)
7391     article))
7392
7393 (defun gnus-summary-better-unread-subject ()
7394   "Select the first unread subject that has a score over the default score."
7395   (interactive)
7396   (let ((data gnus-newsgroup-data)
7397         article score)
7398     (while (and (setq article (gnus-data-number (car data)))
7399                 (or (gnus-data-read-p (car data))
7400                     (not (> (gnus-summary-article-score article)
7401                             gnus-summary-default-score))))
7402       (setq data (cdr data)))
7403     (when article
7404       (gnus-summary-goto-subject article))
7405     (gnus-summary-position-point)
7406     article))
7407
7408 (defun gnus-summary-last-subject ()
7409   "Go to the last displayed subject line in the group."
7410   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7411     (when article
7412       (gnus-summary-goto-subject article))))
7413
7414 (defun gnus-summary-goto-article (article &optional all-headers force)
7415   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7416 If ALL-HEADERS is non-nil, no header lines are hidden.
7417 If FORCE, go to the article even if it isn't displayed.  If FORCE
7418 is a number, it is the line the article is to be displayed on."
7419   (interactive
7420    (list
7421     (completing-read
7422      "Article number or Message-ID: "
7423      (mapcar (lambda (number) (list (int-to-string number)))
7424              gnus-newsgroup-limit))
7425     current-prefix-arg
7426     t))
7427   (prog1
7428       (if (and (stringp article)
7429                (string-match "@" article))
7430           (gnus-summary-refer-article article)
7431         (when (stringp article)
7432           (setq article (string-to-number article)))
7433         (if (gnus-summary-goto-subject article force)
7434             (gnus-summary-display-article article all-headers)
7435           (gnus-message 4 "Couldn't go to article %s" article) nil))
7436     (gnus-summary-position-point)))
7437
7438 (defun gnus-summary-goto-last-article ()
7439   "Go to the previously read article."
7440   (interactive)
7441   (prog1
7442       (when gnus-last-article
7443         (gnus-summary-goto-article gnus-last-article nil t))
7444     (gnus-summary-position-point)))
7445
7446 (defun gnus-summary-pop-article (number)
7447   "Pop one article off the history and go to the previous.
7448 NUMBER articles will be popped off."
7449   (interactive "p")
7450   (let (to)
7451     (setq gnus-newsgroup-history
7452           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7453     (if to
7454         (gnus-summary-goto-article (car to) nil t)
7455       (error "Article history empty")))
7456   (gnus-summary-position-point))
7457
7458 ;; Summary commands and functions for limiting the summary buffer.
7459
7460 (defun gnus-summary-limit-to-articles (n)
7461   "Limit the summary buffer to the next N articles.
7462 If not given a prefix, use the process marked articles instead."
7463   (interactive "P")
7464   (prog1
7465       (let ((articles (gnus-summary-work-articles n)))
7466         (setq gnus-newsgroup-processable nil)
7467         (gnus-summary-limit articles))
7468     (gnus-summary-position-point)))
7469
7470 (defun gnus-summary-pop-limit (&optional total)
7471   "Restore the previous limit.
7472 If given a prefix, remove all limits."
7473   (interactive "P")
7474   (when total
7475     (setq gnus-newsgroup-limits
7476           (list (mapcar (lambda (h) (mail-header-number h))
7477                         gnus-newsgroup-headers))))
7478   (unless gnus-newsgroup-limits
7479     (error "No limit to pop"))
7480   (prog1
7481       (gnus-summary-limit nil 'pop)
7482     (gnus-summary-position-point)))
7483
7484 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7485   "Limit the summary buffer to articles that have subjects that match a regexp.
7486 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7487   (interactive
7488    (list (read-string (if current-prefix-arg
7489                           "Exclude subject (regexp): "
7490                         "Limit to subject (regexp): "))
7491          nil current-prefix-arg))
7492   (unless header
7493     (setq header "subject"))
7494   (when (not (equal "" subject))
7495     (prog1
7496         (let ((articles (gnus-summary-find-matching
7497                          (or header "subject") subject 'all nil nil
7498                          not-matching)))
7499           (unless articles
7500             (error "Found no matches for \"%s\"" subject))
7501           (gnus-summary-limit articles))
7502       (gnus-summary-position-point))))
7503
7504 (defun gnus-summary-limit-to-author (from &optional not-matching)
7505   "Limit the summary buffer to articles that have authors that match a regexp.
7506 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7507   (interactive
7508    (list (read-string (if current-prefix-arg
7509                           "Exclude author (regexp): "
7510                         "Limit to author (regexp): "))
7511          current-prefix-arg))
7512   (gnus-summary-limit-to-subject from "from" not-matching))
7513
7514 (defun gnus-summary-limit-to-age (age &optional younger-p)
7515   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7516 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7517 articles that are younger than AGE days."
7518   (interactive
7519    (let ((younger current-prefix-arg)
7520          (days-got nil)
7521          days)
7522      (while (not days-got)
7523        (setq days (if younger
7524                       (read-string "Limit to articles younger than (in days, older when negative): ")
7525                     (read-string
7526                      "Limit to articles older than (in days, younger when negative): ")))
7527        (when (> (length days) 0)
7528          (setq days (read days)))
7529        (if (numberp days)
7530            (progn
7531              (setq days-got t)
7532              (if (< days 0)
7533                  (progn
7534                    (setq younger (not younger))
7535                    (setq days (* days -1)))))
7536          (message "Please enter a number.")
7537          (sleep-for 1)))
7538      (list days younger)))
7539   (prog1
7540       (let ((data gnus-newsgroup-data)
7541             (cutoff (days-to-time age))
7542             articles d date is-younger)
7543         (while (setq d (pop data))
7544           (when (and (vectorp (gnus-data-header d))
7545                      (setq date (mail-header-date (gnus-data-header d))))
7546             (setq is-younger (time-less-p
7547                               (time-since (condition-case ()
7548                                               (date-to-time date)
7549                                             (error '(0 0))))
7550                               cutoff))
7551             (when (if younger-p
7552                       is-younger
7553                     (not is-younger))
7554               (push (gnus-data-number d) articles))))
7555         (gnus-summary-limit (nreverse articles)))
7556     (gnus-summary-position-point)))
7557
7558 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7559   "Limit the summary buffer to articles that match an 'extra' header."
7560   (interactive
7561    (let ((header
7562           (intern
7563            (gnus-completing-read-with-default
7564             (symbol-name (car gnus-extra-headers))
7565             (if current-prefix-arg
7566                 "Exclude extra header:"
7567               "Limit extra header:")
7568             (mapcar (lambda (x)
7569                       (cons (symbol-name x) x))
7570                     gnus-extra-headers)
7571             nil
7572             t))))
7573      (list header
7574            (read-string (format "%s header %s (regexp): "
7575                                 (if current-prefix-arg "Exclude" "Limit to")
7576                                 header))
7577            current-prefix-arg)))
7578   (when (not (equal "" regexp))
7579     (prog1
7580         (let ((articles (gnus-summary-find-matching
7581                          (cons 'extra header) regexp 'all nil nil
7582                          not-matching)))
7583           (unless articles
7584             (error "Found no matches for \"%s\"" regexp))
7585           (gnus-summary-limit articles))
7586       (gnus-summary-position-point))))
7587
7588 (defun gnus-summary-limit-to-display-predicate ()
7589   "Limit the summary buffer to the predicated in the `display' group parameter."
7590   (interactive)
7591   (unless gnus-newsgroup-display
7592     (error "There is no `display' group parameter"))
7593   (let (articles)
7594     (dolist (number gnus-newsgroup-articles)
7595       (when (funcall gnus-newsgroup-display)
7596         (push number articles)))
7597     (gnus-summary-limit articles))
7598   (gnus-summary-position-point))
7599
7600 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7601 (make-obsolete
7602  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7603
7604 (defun gnus-summary-limit-to-unread (&optional all)
7605   "Limit the summary buffer to articles that are not marked as read.
7606 If ALL is non-nil, limit strictly to unread articles."
7607   (interactive "P")
7608   (if all
7609       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7610     (gnus-summary-limit-to-marks
7611      ;; Concat all the marks that say that an article is read and have
7612      ;; those removed.
7613      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7614            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7615            gnus-low-score-mark gnus-expirable-mark
7616            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7617            gnus-duplicate-mark gnus-souped-mark)
7618      'reverse)))
7619
7620 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7621 (make-obsolete 'gnus-summary-delete-marked-with
7622                'gnus-summary-limit-exclude-marks)
7623
7624 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7625   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7626 If REVERSE, limit the summary buffer to articles that are marked
7627 with MARKS.  MARKS can either be a string of marks or a list of marks.
7628 Returns how many articles were removed."
7629   (interactive "sMarks: ")
7630   (gnus-summary-limit-to-marks marks t))
7631
7632 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7633   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7634 If REVERSE (the prefix), limit the summary buffer to articles that are
7635 not marked with MARKS.  MARKS can either be a string of marks or a
7636 list of marks.
7637 Returns how many articles were removed."
7638   (interactive "sMarks: \nP")
7639   (prog1
7640       (let ((data gnus-newsgroup-data)
7641             (marks (if (listp marks) marks
7642                      (append marks nil))) ; Transform to list.
7643             articles)
7644         (while data
7645           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7646                   (memq (gnus-data-mark (car data)) marks))
7647             (push (gnus-data-number (car data)) articles))
7648           (setq data (cdr data)))
7649         (gnus-summary-limit articles))
7650     (gnus-summary-position-point)))
7651
7652 (defun gnus-summary-limit-to-score (score)
7653   "Limit to articles with score at or above SCORE."
7654   (interactive "NLimit to articles with score of at least: ")
7655   (let ((data gnus-newsgroup-data)
7656         articles)
7657     (while data
7658       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7659                 score)
7660         (push (gnus-data-number (car data)) articles))
7661       (setq data (cdr data)))
7662     (prog1
7663         (gnus-summary-limit articles)
7664       (gnus-summary-position-point))))
7665
7666 (defun gnus-summary-limit-to-unseen ()
7667   "Limit to unseen articles."
7668   (interactive)
7669   (prog1
7670       (gnus-summary-limit gnus-newsgroup-unseen)
7671     (gnus-summary-position-point)))
7672
7673 (defun gnus-summary-limit-include-thread (id)
7674   "Display all the hidden articles that is in the thread with ID in it.
7675 When called interactively, ID is the Message-ID of the current
7676 article."
7677   (interactive (list (mail-header-id (gnus-summary-article-header))))
7678   (let ((articles (gnus-articles-in-thread
7679                    (gnus-id-to-thread (gnus-root-id id)))))
7680     (prog1
7681         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7682       (gnus-summary-limit-include-matching-articles
7683        "subject"
7684        (regexp-quote (gnus-simplify-subject-re
7685                       (mail-header-subject (gnus-id-to-header id)))))
7686       (gnus-summary-position-point))))
7687
7688 (defun gnus-summary-limit-include-matching-articles (header regexp)
7689   "Display all the hidden articles that have HEADERs that match REGEXP."
7690   (interactive (list (read-string "Match on header: ")
7691                      (read-string "Regexp: ")))
7692   (let ((articles (gnus-find-matching-articles header regexp)))
7693     (prog1
7694         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7695       (gnus-summary-position-point))))
7696
7697 (defun gnus-summary-insert-dormant-articles ()
7698   "Insert all the dormat articles for this group into the current buffer."
7699   (interactive)
7700   (let ((gnus-verbose (max 6 gnus-verbose)))
7701     (if (not gnus-newsgroup-dormant)
7702         (gnus-message 3 "No cached articles for this group")
7703       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7704
7705 (defun gnus-summary-limit-include-dormant ()
7706   "Display all the hidden articles that are marked as dormant.
7707 Note that this command only works on a subset of the articles currently
7708 fetched for this group."
7709   (interactive)
7710   (unless gnus-newsgroup-dormant
7711     (error "There are no dormant articles in this group"))
7712   (prog1
7713       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7714     (gnus-summary-position-point)))
7715
7716 (defun gnus-summary-limit-exclude-dormant ()
7717   "Hide all dormant articles."
7718   (interactive)
7719   (prog1
7720       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7721     (gnus-summary-position-point)))
7722
7723 (defun gnus-summary-limit-exclude-childless-dormant ()
7724   "Hide all dormant articles that have no children."
7725   (interactive)
7726   (let ((data (gnus-data-list t))
7727         articles d children)
7728     ;; Find all articles that are either not dormant or have
7729     ;; children.
7730     (while (setq d (pop data))
7731       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7732                 (and (setq children
7733                            (gnus-article-children (gnus-data-number d)))
7734                      (let (found)
7735                        (while children
7736                          (when (memq (car children) articles)
7737                            (setq children nil
7738                                  found t))
7739                          (pop children))
7740                        found)))
7741         (push (gnus-data-number d) articles)))
7742     ;; Do the limiting.
7743     (prog1
7744         (gnus-summary-limit articles)
7745       (gnus-summary-position-point))))
7746
7747 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7748   "Mark all unread excluded articles as read.
7749 If ALL, mark even excluded ticked and dormants as read."
7750   (interactive "P")
7751   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7752   (let ((articles (gnus-sorted-ndifference
7753                    (sort
7754                     (mapcar (lambda (h) (mail-header-number h))
7755                             gnus-newsgroup-headers)
7756                     '<)
7757                    gnus-newsgroup-limit))
7758         article)
7759     (setq gnus-newsgroup-unreads
7760           (gnus-sorted-intersection gnus-newsgroup-unreads
7761                                     gnus-newsgroup-limit))
7762     (if all
7763         (setq gnus-newsgroup-dormant nil
7764               gnus-newsgroup-marked nil
7765               gnus-newsgroup-reads
7766               (nconc
7767                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7768                gnus-newsgroup-reads))
7769       (while (setq article (pop articles))
7770         (unless (or (memq article gnus-newsgroup-dormant)
7771                     (memq article gnus-newsgroup-marked))
7772           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7773
7774 (defun gnus-summary-limit (articles &optional pop)
7775   (if pop
7776       ;; We pop the previous limit off the stack and use that.
7777       (setq articles (car gnus-newsgroup-limits)
7778             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7779     ;; We use the new limit, so we push the old limit on the stack.
7780     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7781   ;; Set the limit.
7782   (setq gnus-newsgroup-limit articles)
7783   (let ((total (length gnus-newsgroup-data))
7784         (data (gnus-data-find-list (gnus-summary-article-number)))
7785         (gnus-summary-mark-below nil)   ; Inhibit this.
7786         found)
7787     ;; This will do all the work of generating the new summary buffer
7788     ;; according to the new limit.
7789     (gnus-summary-prepare)
7790     ;; Hide any threads, possibly.
7791     (gnus-summary-maybe-hide-threads)
7792     ;; Try to return to the article you were at, or one in the
7793     ;; neighborhood.
7794     (when data
7795       ;; We try to find some article after the current one.
7796       (while data
7797         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7798           (setq data nil
7799                 found t))
7800         (setq data (cdr data))))
7801     (unless found
7802       ;; If there is no data, that means that we were after the last
7803       ;; article.  The same goes when we can't find any articles
7804       ;; after the current one.
7805       (goto-char (point-max))
7806       (gnus-summary-find-prev))
7807     (gnus-set-mode-line 'summary)
7808     ;; We return how many articles were removed from the summary
7809     ;; buffer as a result of the new limit.
7810     (- total (length gnus-newsgroup-data))))
7811
7812 (defsubst gnus-invisible-cut-children (threads)
7813   (let ((num 0))
7814     (while threads
7815       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7816         (incf num))
7817       (pop threads))
7818     (< num 2)))
7819
7820 (defsubst gnus-cut-thread (thread)
7821   "Go forwards in the thread until we find an article that we want to display."
7822   (when (or (eq gnus-fetch-old-headers 'some)
7823             (eq gnus-fetch-old-headers 'invisible)
7824             (numberp gnus-fetch-old-headers)
7825             (eq gnus-build-sparse-threads 'some)
7826             (eq gnus-build-sparse-threads 'more))
7827     ;; Deal with old-fetched headers and sparse threads.
7828     (while (and
7829             thread
7830             (or
7831              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7832              (gnus-summary-article-ancient-p
7833               (mail-header-number (car thread))))
7834             (if (or (<= (length (cdr thread)) 1)
7835                     (eq gnus-fetch-old-headers 'invisible))
7836                 (setq gnus-newsgroup-limit
7837                       (delq (mail-header-number (car thread))
7838                             gnus-newsgroup-limit)
7839                       thread (cadr thread))
7840               (when (gnus-invisible-cut-children (cdr thread))
7841                 (let ((th (cdr thread)))
7842                   (while th
7843                     (if (memq (mail-header-number (caar th))
7844                               gnus-newsgroup-limit)
7845                         (setq thread (car th)
7846                               th nil)
7847                       (setq th (cdr th))))))))))
7848   thread)
7849
7850 (defun gnus-cut-threads (threads)
7851   "Cut off all uninteresting articles from the beginning of threads."
7852   (when (or (eq gnus-fetch-old-headers 'some)
7853             (eq gnus-fetch-old-headers 'invisible)
7854             (numberp gnus-fetch-old-headers)
7855             (eq gnus-build-sparse-threads 'some)
7856             (eq gnus-build-sparse-threads 'more))
7857     (let ((th threads))
7858       (while th
7859         (setcar th (gnus-cut-thread (car th)))
7860         (setq th (cdr th)))))
7861   ;; Remove nixed out threads.
7862   (delq nil threads))
7863
7864 (defun gnus-summary-initial-limit (&optional show-if-empty)
7865   "Figure out what the initial limit is supposed to be on group entry.
7866 This entails weeding out unwanted dormants, low-scored articles,
7867 fetch-old-headers verbiage, and so on."
7868   ;; Most groups have nothing to remove.
7869   (if (or gnus-inhibit-limiting
7870           (and (null gnus-newsgroup-dormant)
7871                (eq gnus-newsgroup-display 'gnus-not-ignore)
7872                (not (eq gnus-fetch-old-headers 'some))
7873                (not (numberp gnus-fetch-old-headers))
7874                (not (eq gnus-fetch-old-headers 'invisible))
7875                (null gnus-summary-expunge-below)
7876                (not (eq gnus-build-sparse-threads 'some))
7877                (not (eq gnus-build-sparse-threads 'more))
7878                (null gnus-thread-expunge-below)
7879                (not gnus-use-nocem)))
7880       ()                                ; Do nothing.
7881     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7882     (setq gnus-newsgroup-limit nil)
7883     (mapatoms
7884      (lambda (node)
7885        (unless (car (symbol-value node))
7886          ;; These threads have no parents -- they are roots.
7887          (let ((nodes (cdr (symbol-value node)))
7888                thread)
7889            (while nodes
7890              (if (and gnus-thread-expunge-below
7891                       (< (gnus-thread-total-score (car nodes))
7892                          gnus-thread-expunge-below))
7893                  (gnus-expunge-thread (pop nodes))
7894                (setq thread (pop nodes))
7895                (gnus-summary-limit-children thread))))))
7896      gnus-newsgroup-dependencies)
7897     ;; If this limitation resulted in an empty group, we might
7898     ;; pop the previous limit and use it instead.
7899     (when (and (not gnus-newsgroup-limit)
7900                show-if-empty)
7901       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7902     gnus-newsgroup-limit))
7903
7904 (defun gnus-summary-limit-children (thread)
7905   "Return 1 if this subthread is visible and 0 if it is not."
7906   ;; First we get the number of visible children to this thread.  This
7907   ;; is done by recursing down the thread using this function, so this
7908   ;; will really go down to a leaf article first, before slowly
7909   ;; working its way up towards the root.
7910   (when thread
7911     (let* ((max-lisp-eval-depth 5000)
7912            (children
7913            (if (cdr thread)
7914                (apply '+ (mapcar 'gnus-summary-limit-children
7915                                  (cdr thread)))
7916              0))
7917           (number (mail-header-number (car thread)))
7918           score)
7919       (if (and
7920            (not (memq number gnus-newsgroup-marked))
7921            (or
7922             ;; If this article is dormant and has absolutely no visible
7923             ;; children, then this article isn't visible.
7924             (and (memq number gnus-newsgroup-dormant)
7925                  (zerop children))
7926             ;; If this is "fetch-old-headered" and there is no
7927             ;; visible children, then we don't want this article.
7928             (and (or (eq gnus-fetch-old-headers 'some)
7929                      (numberp gnus-fetch-old-headers))
7930                  (gnus-summary-article-ancient-p number)
7931                  (zerop children))
7932             ;; If this is "fetch-old-headered" and `invisible', then
7933             ;; we don't want this article.
7934             (and (eq gnus-fetch-old-headers 'invisible)
7935                  (gnus-summary-article-ancient-p number))
7936             ;; If this is a sparsely inserted article with no children,
7937             ;; we don't want it.
7938             (and (eq gnus-build-sparse-threads 'some)
7939                  (gnus-summary-article-sparse-p number)
7940                  (zerop children))
7941             ;; If we use expunging, and this article is really
7942             ;; low-scored, then we don't want this article.
7943             (when (and gnus-summary-expunge-below
7944                        (< (setq score
7945                                 (or (cdr (assq number gnus-newsgroup-scored))
7946                                     gnus-summary-default-score))
7947                           gnus-summary-expunge-below))
7948               ;; We increase the expunge-tally here, but that has
7949               ;; nothing to do with the limits, really.
7950               (incf gnus-newsgroup-expunged-tally)
7951               ;; We also mark as read here, if that's wanted.
7952               (when (and gnus-summary-mark-below
7953                          (< score gnus-summary-mark-below))
7954                 (setq gnus-newsgroup-unreads
7955                       (delq number gnus-newsgroup-unreads))
7956                 (if gnus-newsgroup-auto-expire
7957                     (push number gnus-newsgroup-expirable)
7958                   (push (cons number gnus-low-score-mark)
7959                         gnus-newsgroup-reads)))
7960               t)
7961             ;; Do the `display' group parameter.
7962             (and gnus-newsgroup-display
7963                  (not (funcall gnus-newsgroup-display)))
7964             ;; Check NoCeM things.
7965             (if (and gnus-use-nocem
7966                      (gnus-nocem-unwanted-article-p
7967                       (mail-header-id (car thread))))
7968                 (progn
7969                   (setq gnus-newsgroup-unreads
7970                         (delq number gnus-newsgroup-unreads))
7971                   t))))
7972           ;; Nope, invisible article.
7973           0
7974         ;; Ok, this article is to be visible, so we add it to the limit
7975         ;; and return 1.
7976         (push number gnus-newsgroup-limit)
7977         1))))
7978
7979 (defun gnus-expunge-thread (thread)
7980   "Mark all articles in THREAD as read."
7981   (let* ((number (mail-header-number (car thread))))
7982     (incf gnus-newsgroup-expunged-tally)
7983     ;; We also mark as read here, if that's wanted.
7984     (setq gnus-newsgroup-unreads
7985           (delq number gnus-newsgroup-unreads))
7986     (if gnus-newsgroup-auto-expire
7987         (push number gnus-newsgroup-expirable)
7988       (push (cons number gnus-low-score-mark)
7989             gnus-newsgroup-reads)))
7990   ;; Go recursively through all subthreads.
7991   (mapcar 'gnus-expunge-thread (cdr thread)))
7992
7993 ;; Summary article oriented commands
7994
7995 (defun gnus-summary-refer-parent-article (n)
7996   "Refer parent article N times.
7997 If N is negative, go to ancestor -N instead.
7998 The difference between N and the number of articles fetched is returned."
7999   (interactive "p")
8000   (let ((skip 1)
8001         error header ref)
8002     (when (not (natnump n))
8003       (setq skip (abs n)
8004             n 1))
8005     (while (and (> n 0)
8006                 (not error))
8007       (setq header (gnus-summary-article-header))
8008       (if (and (eq (mail-header-number header)
8009                    (cdr gnus-article-current))
8010                (equal gnus-newsgroup-name
8011                       (car gnus-article-current)))
8012           ;; If we try to find the parent of the currently
8013           ;; displayed article, then we take a look at the actual
8014           ;; References header, since this is slightly more
8015           ;; reliable than the References field we got from the
8016           ;; server.
8017           (save-excursion
8018             (set-buffer gnus-original-article-buffer)
8019             (nnheader-narrow-to-headers)
8020             (unless (setq ref (message-fetch-field "references"))
8021               (setq ref (message-fetch-field "in-reply-to")))
8022             (widen))
8023         (setq ref
8024               ;; It's not the current article, so we take a bet on
8025               ;; the value we got from the server.
8026               (mail-header-references header)))
8027       (if (and ref
8028                (not (equal ref "")))
8029           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8030             (gnus-message 1 "Couldn't find parent"))
8031         (gnus-message 1 "No references in article %d"
8032                       (gnus-summary-article-number))
8033         (setq error t))
8034       (decf n))
8035     (gnus-summary-position-point)
8036     n))
8037
8038 (defun gnus-summary-refer-references ()
8039   "Fetch all articles mentioned in the References header.
8040 Return the number of articles fetched."
8041   (interactive)
8042   (let ((ref (mail-header-references (gnus-summary-article-header)))
8043         (current (gnus-summary-article-number))
8044         (n 0))
8045     (if (or (not ref)
8046             (equal ref ""))
8047         (error "No References in the current article")
8048       ;; For each Message-ID in the References header...
8049       (while (string-match "<[^>]*>" ref)
8050         (incf n)
8051         ;; ... fetch that article.
8052         (gnus-summary-refer-article
8053          (prog1 (match-string 0 ref)
8054            (setq ref (substring ref (match-end 0))))))
8055       (gnus-summary-goto-subject current)
8056       (gnus-summary-position-point)
8057       n)))
8058
8059 (defun gnus-summary-refer-thread (&optional limit)
8060   "Fetch all articles in the current thread.
8061 If LIMIT (the numerical prefix), fetch that many old headers instead
8062 of what's specified by the `gnus-refer-thread-limit' variable."
8063   (interactive "P")
8064   (let ((id (mail-header-id (gnus-summary-article-header)))
8065         (limit (if limit (prefix-numeric-value limit)
8066                  gnus-refer-thread-limit)))
8067     (unless (eq gnus-fetch-old-headers 'invisible)
8068       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8069       ;; Retrieve the headers and read them in.
8070       (if (eq (if (numberp limit)
8071                   (gnus-retrieve-headers
8072                    (list (min
8073                           (+ (mail-header-number
8074                               (gnus-summary-article-header))
8075                              limit)
8076                           gnus-newsgroup-end))
8077                    gnus-newsgroup-name (* limit 2))
8078                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8079                 ;; headers.
8080                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8081                                        gnus-newsgroup-name limit))
8082               'nov)
8083           (gnus-build-all-threads)
8084         (error "Can't fetch thread from backends that don't support NOV"))
8085       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8086     (gnus-summary-limit-include-thread id)))
8087
8088 (defun gnus-summary-refer-article (message-id)
8089   "Fetch an article specified by MESSAGE-ID."
8090   (interactive "sMessage-ID: ")
8091   (when (and (stringp message-id)
8092              (not (zerop (length message-id))))
8093     ;; Construct the correct Message-ID if necessary.
8094     ;; Suggested by tale@pawl.rpi.edu.
8095     (unless (string-match "^<" message-id)
8096       (setq message-id (concat "<" message-id)))
8097     (unless (string-match ">$" message-id)
8098       (setq message-id (concat message-id ">")))
8099     (let* ((header (gnus-id-to-header message-id))
8100            (sparse (and header
8101                         (gnus-summary-article-sparse-p
8102                          (mail-header-number header))
8103                         (memq (mail-header-number header)
8104                               gnus-newsgroup-limit)))
8105            number)
8106       (cond
8107        ;; If the article is present in the buffer we just go to it.
8108        ((and header
8109              (or (not (gnus-summary-article-sparse-p
8110                        (mail-header-number header)))
8111                  sparse))
8112         (prog1
8113             (gnus-summary-goto-article
8114              (mail-header-number header) nil t)
8115           (when sparse
8116             (gnus-summary-update-article (mail-header-number header)))))
8117        (t
8118         ;; We fetch the article.
8119         (catch 'found
8120           (dolist (gnus-override-method (gnus-refer-article-methods))
8121             (when (and (gnus-check-server gnus-override-method)
8122                        ;; Fetch the header,
8123                        (setq number (gnus-summary-insert-subject message-id)))
8124               ;; and display the article.
8125               (gnus-summary-select-article nil nil nil number)
8126               (throw 'found t)))
8127           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8128
8129 (defun gnus-refer-article-methods ()
8130   "Return a list of referable methods."
8131   (cond
8132    ;; No method, so we default to current and native.
8133    ((null gnus-refer-article-method)
8134     (list gnus-current-select-method gnus-select-method))
8135    ;; Current.
8136    ((eq 'current gnus-refer-article-method)
8137     (list gnus-current-select-method))
8138    ;; List of select methods.
8139    ((not (and (symbolp (car gnus-refer-article-method))
8140               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8141     (let (out)
8142       (dolist (method gnus-refer-article-method)
8143         (push (if (eq 'current method)
8144                   gnus-current-select-method
8145                 method)
8146               out))
8147       (nreverse out)))
8148    ;; One single select method.
8149    (t
8150     (list gnus-refer-article-method))))
8151
8152 (defun gnus-summary-edit-parameters ()
8153   "Edit the group parameters of the current group."
8154   (interactive)
8155   (gnus-group-edit-group gnus-newsgroup-name 'params))
8156
8157 (defun gnus-summary-customize-parameters ()
8158   "Customize the group parameters of the current group."
8159   (interactive)
8160   (gnus-group-customize gnus-newsgroup-name))
8161
8162 (defun gnus-summary-enter-digest-group (&optional force)
8163   "Enter an nndoc group based on the current article.
8164 If FORCE, force a digest interpretation.  If not, try
8165 to guess what the document format is."
8166   (interactive "P")
8167   (let ((conf gnus-current-window-configuration))
8168     (save-window-excursion
8169       (save-excursion
8170         (let (gnus-article-prepare-hook
8171               gnus-display-mime-function
8172               gnus-break-pages)
8173           (gnus-summary-select-article))))
8174     (setq gnus-current-window-configuration conf)
8175     (let* ((name (format "%s-%d"
8176                          (gnus-group-prefixed-name
8177                           gnus-newsgroup-name (list 'nndoc ""))
8178                          (save-excursion
8179                            (set-buffer gnus-summary-buffer)
8180                            gnus-current-article)))
8181            (ogroup gnus-newsgroup-name)
8182            (params (append (gnus-info-params (gnus-get-info ogroup))
8183                            (list (cons 'to-group ogroup))
8184                            (list (cons 'save-article-group ogroup))))
8185            (case-fold-search t)
8186            (buf (current-buffer))
8187            dig to-address)
8188       (save-excursion
8189         (set-buffer gnus-original-article-buffer)
8190         ;; Have the digest group inherit the main mail address of
8191         ;; the parent article.
8192         (when (setq to-address (or (gnus-fetch-field "reply-to")
8193                                    (gnus-fetch-field "from")))
8194           (setq params (append
8195                         (list (cons 'to-address
8196                                     (funcall gnus-decode-encoded-word-function
8197                                              to-address))))))
8198         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8199         (insert-buffer-substring gnus-original-article-buffer)
8200         ;; Remove lines that may lead nndoc to misinterpret the
8201         ;; document type.
8202         (narrow-to-region
8203          (goto-char (point-min))
8204          (or (search-forward "\n\n" nil t) (point)))
8205         (goto-char (point-min))
8206         (delete-matching-lines "^Path:\\|^From ")
8207         (widen))
8208       (unwind-protect
8209           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8210                     (gnus-newsgroup-ephemeral-ignored-charsets
8211                      gnus-newsgroup-ignored-charsets))
8212                 (gnus-group-read-ephemeral-group
8213                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8214                               (nndoc-article-type
8215                                ,(if force 'mbox 'guess)))
8216                  t nil nil nil
8217                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8218                                                         "ADAPT")))))
8219               ;; Make all postings to this group go to the parent group.
8220               (nconc (gnus-info-params (gnus-get-info name))
8221                      params)
8222             ;; Couldn't select this doc group.
8223             (switch-to-buffer buf)
8224             (gnus-set-global-variables)
8225             (gnus-configure-windows 'summary)
8226             (gnus-message 3 "Article couldn't be entered?"))
8227         (kill-buffer dig)))))
8228
8229 (defun gnus-summary-read-document (n)
8230   "Open a new group based on the current article(s).
8231 This will allow you to read digests and other similar
8232 documents as newsgroups.
8233 Obeys the standard process/prefix convention."
8234   (interactive "P")
8235   (let* ((articles (gnus-summary-work-articles n))
8236          (ogroup gnus-newsgroup-name)
8237          (params (append (gnus-info-params (gnus-get-info ogroup))
8238                          (list (cons 'to-group ogroup))))
8239          article group egroup groups vgroup)
8240     (while (setq article (pop articles))
8241       (setq group (format "%s-%d" gnus-newsgroup-name article))
8242       (gnus-summary-remove-process-mark article)
8243       (when (gnus-summary-display-article article)
8244         (save-excursion
8245           (with-temp-buffer
8246             (insert-buffer-substring gnus-original-article-buffer)
8247             ;; Remove some headers that may lead nndoc to make
8248             ;; the wrong guess.
8249             (message-narrow-to-head)
8250             (goto-char (point-min))
8251             (delete-matching-lines "^\\(Path\\):\\|^From ")
8252             (widen)
8253             (if (setq egroup
8254                       (gnus-group-read-ephemeral-group
8255                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8256                                      (nndoc-article-type guess))
8257                        t nil t))
8258                 (progn
8259             ;; Make all postings to this group go to the parent group.
8260                   (nconc (gnus-info-params (gnus-get-info egroup))
8261                          params)
8262                   (push egroup groups))
8263               ;; Couldn't select this doc group.
8264               (gnus-error 3 "Article couldn't be entered"))))))
8265     ;; Now we have selected all the documents.
8266     (cond
8267      ((not groups)
8268       (error "None of the articles could be interpreted as documents"))
8269      ((gnus-group-read-ephemeral-group
8270        (setq vgroup (format
8271                      "nnvirtual:%s-%s" gnus-newsgroup-name
8272                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8273        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8274        t
8275        (cons (current-buffer) 'summary)))
8276      (t
8277       (error "Couldn't select virtual nndoc group")))))
8278
8279 (defun gnus-summary-isearch-article (&optional regexp-p)
8280   "Do incremental search forward on the current article.
8281 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8282   (interactive "P")
8283   (gnus-summary-select-article)
8284   (gnus-configure-windows 'article)
8285   (gnus-eval-in-buffer-window gnus-article-buffer
8286     (save-restriction
8287       (widen)
8288       (isearch-forward regexp-p))))
8289
8290 (defun gnus-summary-search-article-forward (regexp &optional backward)
8291   "Search for an article containing REGEXP forward.
8292 If BACKWARD, search backward instead."
8293   (interactive
8294    (list (read-string
8295           (format "Search article %s (regexp%s): "
8296                   (if current-prefix-arg "backward" "forward")
8297                   (if gnus-last-search-regexp
8298                       (concat ", default " gnus-last-search-regexp)
8299                     "")))
8300          current-prefix-arg))
8301   (if (string-equal regexp "")
8302       (setq regexp (or gnus-last-search-regexp ""))
8303     (setq gnus-last-search-regexp regexp)
8304     (setq gnus-article-before-search gnus-current-article))
8305   ;; Intentionally set gnus-last-article.
8306   (setq gnus-last-article gnus-article-before-search)
8307   (let ((gnus-last-article gnus-last-article))
8308     (if (gnus-summary-search-article regexp backward)
8309         (gnus-summary-show-thread)
8310       (error "Search failed: \"%s\"" regexp))))
8311
8312 (defun gnus-summary-search-article-backward (regexp)
8313   "Search for an article containing REGEXP backward."
8314   (interactive
8315    (list (read-string
8316           (format "Search article backward (regexp%s): "
8317                   (if gnus-last-search-regexp
8318                       (concat ", default " gnus-last-search-regexp)
8319                     "")))))
8320   (gnus-summary-search-article-forward regexp 'backward))
8321
8322 (defun gnus-summary-search-article (regexp &optional backward)
8323   "Search for an article containing REGEXP.
8324 Optional argument BACKWARD means do search for backward.
8325 `gnus-select-article-hook' is not called during the search."
8326   ;; We have to require this here to make sure that the following
8327   ;; dynamic binding isn't shadowed by autoloading.
8328   (require 'gnus-async)
8329   (require 'gnus-art)
8330   (let ((gnus-select-article-hook nil)  ;Disable hook.
8331         (gnus-article-prepare-hook nil)
8332         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8333         (gnus-use-article-prefetch nil)
8334         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8335         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8336         (gnus-visual nil)
8337         (gnus-keep-backlog nil)
8338         (gnus-break-pages nil)
8339         (gnus-summary-display-arrow nil)
8340         (gnus-updated-mode-lines nil)
8341         (gnus-auto-center-summary nil)
8342         (sum (current-buffer))
8343         (gnus-display-mime-function nil)
8344         (found nil)
8345         point)
8346     (gnus-save-hidden-threads
8347       (gnus-summary-select-article)
8348       (set-buffer gnus-article-buffer)
8349       (goto-char (window-point (get-buffer-window (current-buffer))))
8350       (when backward
8351         (forward-line -1))
8352       (while (not found)
8353         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8354         (if (if backward
8355                 (re-search-backward regexp nil t)
8356               (re-search-forward regexp nil t))
8357             ;; We found the regexp.
8358             (progn
8359               (setq found 'found)
8360               (beginning-of-line)
8361               (set-window-start
8362                (get-buffer-window (current-buffer))
8363                (point))
8364               (forward-line 1)
8365               (set-window-point
8366                (get-buffer-window (current-buffer))
8367                (point))
8368               (set-buffer sum)
8369               (setq point (point)))
8370           ;; We didn't find it, so we go to the next article.
8371           (set-buffer sum)
8372           (setq found 'not)
8373           (while (eq found 'not)
8374             (if (not (if backward (gnus-summary-find-prev)
8375                        (gnus-summary-find-next)))
8376                 ;; No more articles.
8377                 (setq found t)
8378               ;; Select the next article and adjust point.
8379               (unless (gnus-summary-article-sparse-p
8380                        (gnus-summary-article-number))
8381                 (setq found nil)
8382                 (gnus-summary-select-article)
8383                 (set-buffer gnus-article-buffer)
8384                 (widen)
8385                 (goto-char (if backward (point-max) (point-min))))))))
8386       (gnus-message 7 ""))
8387     ;; Return whether we found the regexp.
8388     (when (eq found 'found)
8389       (goto-char point)
8390       (gnus-summary-show-thread)
8391       (gnus-summary-goto-subject gnus-current-article)
8392       (gnus-summary-position-point)
8393       t)))
8394
8395 (defun gnus-find-matching-articles (header regexp)
8396   "Return a list of all articles that match REGEXP on HEADER.
8397 This search includes all articles in the current group that Gnus has
8398 fetched headers for, whether they are displayed or not."
8399   (let ((articles nil)
8400         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8401         (case-fold-search t))
8402     (dolist (header gnus-newsgroup-headers)
8403       (when (string-match regexp (funcall func header))
8404         (push (mail-header-number header) articles)))
8405     (nreverse articles)))
8406
8407 (defun gnus-summary-find-matching (header regexp &optional backward unread
8408                                           not-case-fold not-matching)
8409   "Return a list of all articles that match REGEXP on HEADER.
8410 The search stars on the current article and goes forwards unless
8411 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8412 If UNREAD is non-nil, only unread articles will
8413 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8414 in the comparisons. If NOT-MATCHING, return a list of all articles that
8415 not match REGEXP on HEADER."
8416   (let ((case-fold-search (not not-case-fold))
8417         articles d func)
8418     (if (consp header)
8419         (if (eq (car header) 'extra)
8420             (setq func
8421                   `(lambda (h)
8422                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8423                          "")))
8424           (error "%s is an invalid header" header))
8425       (unless (fboundp (intern (concat "mail-header-" header)))
8426         (error "%s is not a valid header" header))
8427       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8428     (dolist (d (if (eq backward 'all)
8429                    gnus-newsgroup-data
8430                  (gnus-data-find-list
8431                   (gnus-summary-article-number)
8432                   (gnus-data-list backward))))
8433       (when (and (or (not unread)       ; We want all articles...
8434                      (gnus-data-unread-p d)) ; Or just unreads.
8435                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8436                  (if not-matching
8437                      (not (string-match
8438                            regexp
8439                            (funcall func (gnus-data-header d))))
8440                    (string-match regexp
8441                                  (funcall func (gnus-data-header d)))))
8442         (push (gnus-data-number d) articles))) ; Success!
8443     (nreverse articles)))
8444
8445 (defun gnus-summary-execute-command (header regexp command &optional backward)
8446   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8447 If HEADER is an empty string (or nil), the match is done on the entire
8448 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8449   (interactive
8450    (list (let ((completion-ignore-case t))
8451            (completing-read
8452             "Header name: "
8453             (mapcar (lambda (header) (list (format "%s" header)))
8454                     (append
8455                      '("Number" "Subject" "From" "Lines" "Date"
8456                        "Message-ID" "Xref" "References" "Body")
8457                      gnus-extra-headers))
8458             nil 'require-match))
8459          (read-string "Regexp: ")
8460          (read-key-sequence "Command: ")
8461          current-prefix-arg))
8462   (when (equal header "Body")
8463     (setq header ""))
8464   ;; Hidden thread subtrees must be searched as well.
8465   (gnus-summary-show-all-threads)
8466   ;; We don't want to change current point nor window configuration.
8467   (save-excursion
8468     (save-window-excursion
8469       (let (gnus-visual
8470             gnus-treat-strip-trailing-blank-lines
8471             gnus-treat-strip-leading-blank-lines
8472             gnus-treat-strip-multiple-blank-lines
8473             gnus-treat-hide-boring-headers
8474             gnus-treat-fold-newsgroups
8475             gnus-article-prepare-hook)
8476         (gnus-message 6 "Executing %s..." (key-description command))
8477         ;; We'd like to execute COMMAND interactively so as to give arguments.
8478         (gnus-execute header regexp
8479                       `(call-interactively ',(key-binding command))
8480                       backward)
8481         (gnus-message 6 "Executing %s...done" (key-description command))))))
8482
8483 (defun gnus-summary-beginning-of-article ()
8484   "Scroll the article back to the beginning."
8485   (interactive)
8486   (gnus-summary-select-article)
8487   (gnus-configure-windows 'article)
8488   (gnus-eval-in-buffer-window gnus-article-buffer
8489     (widen)
8490     (goto-char (point-min))
8491     (when gnus-page-broken
8492       (gnus-narrow-to-page))))
8493
8494 (defun gnus-summary-end-of-article ()
8495   "Scroll to the end of the article."
8496   (interactive)
8497   (gnus-summary-select-article)
8498   (gnus-configure-windows 'article)
8499   (gnus-eval-in-buffer-window gnus-article-buffer
8500     (widen)
8501     (goto-char (point-max))
8502     (recenter -3)
8503     (when gnus-page-broken
8504       (gnus-narrow-to-page))))
8505
8506 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8507   "Truncate to LEN and quote all \"(\"'s in STRING."
8508   (gnus-replace-in-string (if (and len (> (length string) len))
8509                               (substring string 0 len)
8510                             string)
8511                           "[()]" "\\\\\\&"))
8512
8513 (defun gnus-summary-print-article (&optional filename n)
8514   "Generate and print a PostScript image of the N next (mail) articles.
8515
8516 If N is negative, print the N previous articles.  If N is nil and articles
8517 have been marked with the process mark, print these instead.
8518
8519 If the optional first argument FILENAME is nil, send the image to the
8520 printer.  If FILENAME is a string, save the PostScript image in a file with
8521 that name.  If FILENAME is a number, prompt the user for the name of the file
8522 to save in."
8523   (interactive (list (ps-print-preprint current-prefix-arg)))
8524   (dolist (article (gnus-summary-work-articles n))
8525     (gnus-summary-select-article nil nil 'pseudo article)
8526     (gnus-eval-in-buffer-window gnus-article-buffer
8527       (gnus-print-buffer))
8528     (gnus-summary-remove-process-mark article))
8529   (ps-despool filename))
8530
8531 (defun gnus-print-buffer ()
8532   (let ((buffer (generate-new-buffer " *print*")))
8533     (unwind-protect
8534         (progn
8535           (copy-to-buffer buffer (point-min) (point-max))
8536           (set-buffer buffer)
8537           (gnus-article-delete-invisible-text)
8538           (gnus-remove-text-with-property 'gnus-decoration)
8539           (when (gnus-visual-p 'article-highlight 'highlight)
8540             ;; Copy-to-buffer doesn't copy overlay.  So redo
8541             ;; highlight.
8542             (let ((gnus-article-buffer buffer))
8543               (gnus-article-highlight-citation t)
8544               (gnus-article-highlight-signature)))
8545           (let ((ps-left-header
8546                  (list
8547                   (concat "("
8548                           (gnus-summary-print-truncate-and-quote
8549                            (mail-header-subject gnus-current-headers)
8550                            66) ")")
8551                   (concat "("
8552                           (gnus-summary-print-truncate-and-quote
8553                            (mail-header-from gnus-current-headers)
8554                            45) ")")))
8555                 (ps-right-header
8556                  (list
8557                   "/pagenumberstring load"
8558                   (concat "("
8559                           (mail-header-date gnus-current-headers) ")"))))
8560             (gnus-run-hooks 'gnus-ps-print-hook)
8561             (save-excursion
8562               (if window-system
8563                   (ps-spool-buffer-with-faces)
8564                 (ps-spool-buffer)))))
8565       (kill-buffer buffer))))
8566
8567 (defun gnus-summary-show-article (&optional arg)
8568   "Force redisplaying of the current article.
8569 If ARG (the prefix) is a number, show the article with the charset
8570 defined in `gnus-summary-show-article-charset-alist', or the charset
8571 input.
8572 If ARG (the prefix) is non-nil and not a number, show the raw article
8573 without any article massaging functions being run.  Normally, the key
8574 strokes are `C-u g'."
8575   (interactive "P")
8576   (cond
8577    ((numberp arg)
8578     (gnus-summary-show-article t)
8579     (let ((gnus-newsgroup-charset
8580            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8581                (mm-read-coding-system
8582                 "View as charset: " ;; actually it is coding system.
8583                 (save-excursion
8584                   (set-buffer gnus-article-buffer)
8585                   (mm-detect-coding-region (point) (point-max))))))
8586           (gnus-newsgroup-ignored-charsets 'gnus-all))
8587       (gnus-summary-select-article nil 'force)
8588       (let ((deps gnus-newsgroup-dependencies)
8589             head header lines)
8590         (save-excursion
8591           (set-buffer gnus-original-article-buffer)
8592           (save-restriction
8593             (message-narrow-to-head)
8594             (setq head (buffer-string))
8595             (goto-char (point-min))
8596             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8597               (goto-char (point-max))
8598               (widen)
8599               (setq lines (1- (count-lines (point) (point-max))))))
8600           (with-temp-buffer
8601             (insert (format "211 %d Article retrieved.\n"
8602                             (cdr gnus-article-current)))
8603             (insert head)
8604             (if lines (insert (format "Lines: %d\n" lines)))
8605             (insert ".\n")
8606             (let ((nntp-server-buffer (current-buffer)))
8607               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8608         (gnus-data-set-header
8609          (gnus-data-find (cdr gnus-article-current))
8610          header)
8611         (gnus-summary-update-article-line
8612          (cdr gnus-article-current) header)
8613         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8614           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8615    ((not arg)
8616     ;; Select the article the normal way.
8617     (gnus-summary-select-article nil 'force))
8618    (t
8619     ;; We have to require this here to make sure that the following
8620     ;; dynamic binding isn't shadowed by autoloading.
8621     (require 'gnus-async)
8622     (require 'gnus-art)
8623     ;; Bind the article treatment functions to nil.
8624     (let ((gnus-have-all-headers t)
8625           gnus-article-prepare-hook
8626           gnus-article-decode-hook
8627           gnus-display-mime-function
8628           gnus-break-pages)
8629       ;; Destroy any MIME parts.
8630       (when (gnus-buffer-live-p gnus-article-buffer)
8631         (save-excursion
8632           (set-buffer gnus-article-buffer)
8633           (mm-destroy-parts gnus-article-mime-handles)
8634           ;; Set it to nil for safety reason.
8635           (setq gnus-article-mime-handle-alist nil)
8636           (setq gnus-article-mime-handles nil)))
8637       (gnus-summary-select-article nil 'force))))
8638   (gnus-summary-goto-subject gnus-current-article)
8639   (gnus-summary-position-point))
8640
8641 (defun gnus-summary-show-raw-article ()
8642   "Show the raw article without any article massaging functions being run."
8643   (interactive)
8644   (gnus-summary-show-article t))
8645
8646 (defun gnus-summary-verbose-headers (&optional arg)
8647   "Toggle permanent full header display.
8648 If ARG is a positive number, turn header display on.
8649 If ARG is a negative number, turn header display off."
8650   (interactive "P")
8651   (setq gnus-show-all-headers
8652         (cond ((or (not (numberp arg))
8653                    (zerop arg))
8654                (not gnus-show-all-headers))
8655               ((natnump arg)
8656                t)))
8657   (gnus-summary-show-article))
8658
8659 (defun gnus-summary-toggle-header (&optional arg)
8660   "Show the headers if they are hidden, or hide them if they are shown.
8661 If ARG is a positive number, show the entire header.
8662 If ARG is a negative number, hide the unwanted header lines."
8663   (interactive "P")
8664   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8665                      (get-buffer-window gnus-article-buffer t))))
8666     (with-current-buffer gnus-article-buffer
8667       (widen)
8668       (article-narrow-to-head)
8669       (let* ((buffer-read-only nil)
8670              (inhibit-point-motion-hooks t)
8671              (hidden (if (numberp arg)
8672                          (>= arg 0)
8673                        (gnus-article-hidden-text-p 'headers)))
8674              s e)
8675         (delete-region (point-min) (point-max))
8676         (with-current-buffer gnus-original-article-buffer
8677           (goto-char (setq s (point-min)))
8678           (setq e (if (search-forward "\n\n" nil t)
8679                       (1- (point))
8680                     (point-max))))
8681         (insert-buffer-substring gnus-original-article-buffer s e)
8682         (run-hooks 'gnus-article-decode-hook)
8683         (if hidden
8684             (let ((gnus-treat-hide-headers nil)
8685                   (gnus-treat-hide-boring-headers nil))
8686               (gnus-delete-wash-type 'headers)
8687               (gnus-treat-article 'head))
8688           (gnus-treat-article 'head))
8689         (widen)
8690         (if window
8691             (set-window-start window (goto-char (point-min))))
8692         (setq gnus-page-broken
8693               (when gnus-break-pages
8694                 (gnus-narrow-to-page)
8695                 t))
8696         (gnus-set-mode-line 'article)))))
8697
8698 (defun gnus-summary-show-all-headers ()
8699   "Make all header lines visible."
8700   (interactive)
8701   (gnus-summary-toggle-header 1))
8702
8703 (defun gnus-summary-caesar-message (&optional arg)
8704   "Caesar rotate the current article by 13.
8705 The numerical prefix specifies how many places to rotate each letter
8706 forward."
8707   (interactive "P")
8708   (gnus-summary-select-article)
8709   (let ((mail-header-separator ""))
8710     (gnus-eval-in-buffer-window gnus-article-buffer
8711       (save-restriction
8712         (widen)
8713         (let ((start (window-start))
8714               buffer-read-only)
8715           (message-caesar-buffer-body arg)
8716           (set-window-start (get-buffer-window (current-buffer)) start))))))
8717
8718 (autoload 'unmorse-region "morse"
8719   "Convert morse coded text in region to ordinary ASCII text."
8720   t)
8721
8722 (defun gnus-summary-morse-message (&optional arg)
8723   "Morse decode the current article."
8724   (interactive "P")
8725   (gnus-summary-select-article)
8726   (let ((mail-header-separator ""))
8727     (gnus-eval-in-buffer-window gnus-article-buffer
8728       (save-excursion
8729         (save-restriction
8730           (widen)
8731           (let ((pos (window-start))
8732                 buffer-read-only)
8733             (goto-char (point-min))
8734             (when (message-goto-body)
8735               (gnus-narrow-to-body))
8736             (goto-char (point-min))
8737             (while (re-search-forward "·" (point-max) t)
8738               (replace-match "."))
8739             (unmorse-region (point-min) (point-max))
8740             (widen)
8741             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8742
8743 (defun gnus-summary-stop-page-breaking ()
8744   "Stop page breaking in the current article."
8745   (interactive)
8746   (gnus-summary-select-article)
8747   (gnus-eval-in-buffer-window gnus-article-buffer
8748     (widen)
8749     (when (gnus-visual-p 'page-marker)
8750       (let ((buffer-read-only nil))
8751         (gnus-remove-text-with-property 'gnus-prev)
8752         (gnus-remove-text-with-property 'gnus-next))
8753       (setq gnus-page-broken nil))))
8754
8755 (defun gnus-summary-move-article (&optional n to-newsgroup
8756                                             select-method action)
8757   "Move the current article to a different newsgroup.
8758 If N is a positive number, move the N next articles.
8759 If N is a negative number, move the N previous articles.
8760 If N is nil and any articles have been marked with the process mark,
8761 move those articles instead.
8762 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8763 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8764 re-spool using this method.
8765
8766 When called interactively with TO-NEWSGROUP being nil, the value of
8767 the variable `gnus-move-split-methods' is used for finding a default
8768 for the target newsgroup.
8769
8770 For this function to work, both the current newsgroup and the
8771 newsgroup that you want to move to have to support the `request-move'
8772 and `request-accept' functions.
8773
8774 ACTION can be either `move' (the default), `crosspost' or `copy'."
8775   (interactive "P")
8776   (unless action
8777     (setq action 'move))
8778   ;; Check whether the source group supports the required functions.
8779   (cond ((and (eq action 'move)
8780               (not (gnus-check-backend-function
8781                     'request-move-article gnus-newsgroup-name)))
8782          (error "The current group does not support article moving"))
8783         ((and (eq action 'crosspost)
8784               (not (gnus-check-backend-function
8785                     'request-replace-article gnus-newsgroup-name)))
8786          (error "The current group does not support article editing")))
8787   (let ((articles (gnus-summary-work-articles n))
8788         (prefix (if (gnus-check-backend-function
8789                      'request-move-article gnus-newsgroup-name)
8790                     (gnus-group-real-prefix gnus-newsgroup-name)
8791                   ""))
8792         (names '((move "Move" "Moving")
8793                  (copy "Copy" "Copying")
8794                  (crosspost "Crosspost" "Crossposting")))
8795         (copy-buf (save-excursion
8796                     (nnheader-set-temp-buffer " *copy article*")))
8797         art-group to-method new-xref article to-groups)
8798     (unless (assq action names)
8799       (error "Unknown action %s" action))
8800     ;; Read the newsgroup name.
8801     (when (and (not to-newsgroup)
8802                (not select-method))
8803       (if (and gnus-move-split-methods
8804                (not
8805                 (and (memq gnus-current-article articles)
8806                      (gnus-buffer-live-p gnus-original-article-buffer))))
8807           ;; When `gnus-move-split-methods' is non-nil, we have to
8808           ;; select an article to give `gnus-read-move-group-name' an
8809           ;; opportunity to suggest an appropriate default.  However,
8810           ;; we needn't render or mark the article.
8811           (let ((gnus-display-mime-function nil)
8812                 (gnus-article-prepare-hook nil)
8813                 (gnus-mark-article-hook nil))
8814             (gnus-summary-select-article nil nil nil (car articles))))
8815       (setq to-newsgroup
8816             (gnus-read-move-group-name
8817              (cadr (assq action names))
8818              (symbol-value (intern (format "gnus-current-%s-group" action)))
8819              articles prefix))
8820       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8821     (setq to-method (or select-method
8822                         (gnus-server-to-method
8823                          (gnus-group-method to-newsgroup))))
8824     ;; Check the method we are to move this article to...
8825     (unless (gnus-check-backend-function
8826              'request-accept-article (car to-method))
8827       (error "%s does not support article copying" (car to-method)))
8828     (unless (gnus-check-server to-method)
8829       (error "Can't open server %s" (car to-method)))
8830     (gnus-message 6 "%s to %s: %s..."
8831                   (caddr (assq action names))
8832                   (or (car select-method) to-newsgroup) articles)
8833     (while articles
8834       (setq article (pop articles))
8835       (setq
8836        art-group
8837        (cond
8838         ;; Move the article.
8839         ((eq action 'move)
8840          ;; Remove this article from future suppression.
8841          (gnus-dup-unsuppress-article article)
8842          (gnus-request-move-article
8843           article                       ; Article to move
8844           gnus-newsgroup-name           ; From newsgroup
8845           (nth 1 (gnus-find-method-for-group
8846                   gnus-newsgroup-name)) ; Server
8847           (list 'gnus-request-accept-article
8848                 to-newsgroup (list 'quote select-method)
8849                 (not articles) t)       ; Accept form
8850           (not articles)))              ; Only save nov last time
8851         ;; Copy the article.
8852         ((eq action 'copy)
8853          (save-excursion
8854            (set-buffer copy-buf)
8855            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8856              (gnus-request-accept-article
8857               to-newsgroup select-method (not articles) t))))
8858         ;; Crosspost the article.
8859         ((eq action 'crosspost)
8860          (let ((xref (message-tokenize-header
8861                       (mail-header-xref (gnus-summary-article-header article))
8862                       " ")))
8863            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8864                                   ":" (number-to-string article)))
8865            (unless xref
8866              (setq xref (list (system-name))))
8867            (setq new-xref
8868                  (concat
8869                   (mapconcat 'identity
8870                              (delete "Xref:" (delete new-xref xref))
8871                              " ")
8872                   " " new-xref))
8873            (save-excursion
8874              (set-buffer copy-buf)
8875              ;; First put the article in the destination group.
8876              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8877              (when (consp (setq art-group
8878                                 (gnus-request-accept-article
8879                                  to-newsgroup select-method (not articles))))
8880                (setq new-xref (concat new-xref " " (car art-group)
8881                                       ":"
8882                                       (number-to-string (cdr art-group))))
8883                ;; Now we have the new Xrefs header, so we insert
8884                ;; it and replace the new article.
8885                (nnheader-replace-header "Xref" new-xref)
8886                (gnus-request-replace-article
8887                 (cdr art-group) to-newsgroup (current-buffer))
8888                art-group))))))
8889       (cond
8890        ((not art-group)
8891         (gnus-message 1 "Couldn't %s article %s: %s"
8892                       (cadr (assq action names)) article
8893                       (nnheader-get-report (car to-method))))
8894        ((eq art-group 'junk)
8895         (when (eq action 'move)
8896           (gnus-summary-mark-article article gnus-canceled-mark)
8897           (gnus-message 4 "Deleted article %s" article)
8898           ;; run the delete hook
8899           (run-hook-with-args 'gnus-summary-article-delete-hook
8900                               action
8901                               (gnus-data-header
8902                                (assoc article (gnus-data-list nil)))
8903                               gnus-newsgroup-name nil
8904                               select-method)))
8905        (t
8906         (let* ((pto-group (gnus-group-prefixed-name
8907                            (car art-group) to-method))
8908                (entry
8909                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8910                (info (nth 2 entry))
8911                (to-group (gnus-info-group info))
8912                to-marks)
8913           ;; Update the group that has been moved to.
8914           (when (and info
8915                      (memq action '(move copy)))
8916             (unless (member to-group to-groups)
8917               (push to-group to-groups))
8918
8919             (unless (memq article gnus-newsgroup-unreads)
8920               (push 'read to-marks)
8921               (gnus-info-set-read
8922                info (gnus-add-to-range (gnus-info-read info)
8923                                        (list (cdr art-group)))))
8924
8925             ;; See whether the article is to be put in the cache.
8926             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8927                              gnus-article-mark-lists
8928                            (delete '(expirable . expire)
8929                                    (copy-sequence gnus-article-mark-lists))))
8930                   (to-article (cdr art-group)))
8931
8932               ;; Enter the article into the cache in the new group,
8933               ;; if that is required.
8934               (when gnus-use-cache
8935                 (gnus-cache-possibly-enter-article
8936                  to-group to-article
8937                  (memq article gnus-newsgroup-marked)
8938                  (memq article gnus-newsgroup-dormant)
8939                  (memq article gnus-newsgroup-unreads)))
8940
8941               (when gnus-preserve-marks
8942                 ;; Copy any marks over to the new group.
8943                 (when (and (equal to-group gnus-newsgroup-name)
8944                            (not (memq article gnus-newsgroup-unreads)))
8945                   ;; Mark this article as read in this group.
8946                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8947                   (setcdr (gnus-active to-group) to-article)
8948                   (setcdr gnus-newsgroup-active to-article))
8949
8950                 (while marks
8951                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8952                     (when (memq article (symbol-value
8953                                          (intern (format "gnus-newsgroup-%s"
8954                                                          (caar marks)))))
8955                       (push (cdar marks) to-marks)
8956                       ;; If the other group is the same as this group,
8957                       ;; then we have to add the mark to the list.
8958                       (when (equal to-group gnus-newsgroup-name)
8959                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8960                              (cons to-article
8961                                    (symbol-value
8962                                     (intern (format "gnus-newsgroup-%s"
8963                                                     (caar marks)))))))
8964                       ;; Copy the marks to other group.
8965                       (gnus-add-marked-articles
8966                        to-group (cdar marks) (list to-article) info)))
8967                   (setq marks (cdr marks)))
8968
8969                 (gnus-request-set-mark
8970                  to-group (list (list (list to-article) 'add to-marks))))
8971
8972               (gnus-dribble-enter
8973                (concat "(gnus-group-set-info '"
8974                        (gnus-prin1-to-string (gnus-get-info to-group))
8975                        ")"))))
8976
8977           ;; Update the Xref header in this article to point to
8978           ;; the new crossposted article we have just created.
8979           (when (eq action 'crosspost)
8980             (save-excursion
8981               (set-buffer copy-buf)
8982               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8983               (nnheader-replace-header "Xref" new-xref)
8984               (gnus-request-replace-article
8985                article gnus-newsgroup-name (current-buffer))))
8986
8987           ;; run the move/copy/crosspost/respool hook
8988           (run-hook-with-args 'gnus-summary-article-move-hook 
8989                               action
8990                               (gnus-data-header 
8991                                (assoc article (gnus-data-list nil)))
8992                               gnus-newsgroup-name
8993                               to-newsgroup
8994                               select-method))
8995
8996         ;;;!!!Why is this necessary?
8997         (set-buffer gnus-summary-buffer)
8998         
8999         (gnus-summary-goto-subject article)
9000         (when (eq action 'move)
9001           (gnus-summary-mark-article article gnus-canceled-mark))))
9002       (gnus-summary-remove-process-mark article))
9003     ;; Re-activate all groups that have been moved to.
9004     (save-excursion
9005       (set-buffer gnus-group-buffer)
9006       (let ((gnus-group-marked to-groups))
9007         (gnus-group-get-new-news-this-group nil t)))
9008
9009     (gnus-kill-buffer copy-buf)
9010     (gnus-summary-position-point)
9011     (gnus-set-mode-line 'summary)))
9012
9013 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9014   "Move the current article to a different newsgroup.
9015 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9016 When called interactively, if TO-NEWSGROUP is nil, use the value of
9017 the variable `gnus-move-split-methods' for finding a default target
9018 newsgroup.
9019 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9020 re-spool using this method."
9021   (interactive "P")
9022   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9023
9024 (defun gnus-summary-crosspost-article (&optional n)
9025   "Crosspost the current article to some other group."
9026   (interactive "P")
9027   (gnus-summary-move-article n nil nil 'crosspost))
9028
9029 (defcustom gnus-summary-respool-default-method nil
9030   "Default method type for respooling an article.
9031 If nil, use to the current newsgroup method."
9032   :type 'symbol
9033   :group 'gnus-summary-mail)
9034
9035 (defcustom gnus-summary-display-while-building nil
9036   "If non-nil, show and update the summary buffer as it's being built.
9037 If the value is t, update the buffer after every line is inserted.  If
9038 the value is an integer (N), update the display every N lines."
9039   :group 'gnus-thread
9040   :type '(choice (const :tag "off" nil)
9041                  number
9042                  (const :tag "frequently" t)))
9043
9044 (defun gnus-summary-respool-article (&optional n method)
9045   "Respool the current article.
9046 The article will be squeezed through the mail spooling process again,
9047 which means that it will be put in some mail newsgroup or other
9048 depending on `nnmail-split-methods'.
9049 If N is a positive number, respool the N next articles.
9050 If N is a negative number, respool the N previous articles.
9051 If N is nil and any articles have been marked with the process mark,
9052 respool those articles instead.
9053
9054 Respooling can be done both from mail groups and \"real\" newsgroups.
9055 In the former case, the articles in question will be moved from the
9056 current group into whatever groups they are destined to.  In the
9057 latter case, they will be copied into the relevant groups."
9058   (interactive
9059    (list current-prefix-arg
9060          (let* ((methods (gnus-methods-using 'respool))
9061                 (methname
9062                  (symbol-name (or gnus-summary-respool-default-method
9063                                   (car (gnus-find-method-for-group
9064                                         gnus-newsgroup-name)))))
9065                 (method
9066                  (gnus-completing-read-with-default
9067                   methname "What backend do you want to use when respooling?"
9068                   methods nil t nil 'gnus-mail-method-history))
9069                 ms)
9070            (cond
9071             ((zerop (length (setq ms (gnus-servers-using-backend
9072                                       (intern method)))))
9073              (list (intern method) ""))
9074             ((= 1 (length ms))
9075              (car ms))
9076             (t
9077              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9078                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9079                            ms-alist))))))))
9080   (unless method
9081     (error "No method given for respooling"))
9082   (if (assoc (symbol-name
9083               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9084              (gnus-methods-using 'respool))
9085       (gnus-summary-move-article n nil method)
9086     (gnus-summary-copy-article n nil method)))
9087
9088 (defun gnus-summary-import-article (file &optional edit)
9089   "Import an arbitrary file into a mail newsgroup."
9090   (interactive "fImport file: \nP")
9091   (let ((group gnus-newsgroup-name)
9092         (now (current-time))
9093         atts lines group-art)
9094     (unless (gnus-check-backend-function 'request-accept-article group)
9095       (error "%s does not support article importing" group))
9096     (or (file-readable-p file)
9097         (not (file-regular-p file))
9098         (error "Can't read %s" file))
9099     (save-excursion
9100       (set-buffer (gnus-get-buffer-create " *import file*"))
9101       (erase-buffer)
9102       (nnheader-insert-file-contents file)
9103       (goto-char (point-min))
9104       (if (nnheader-article-p)
9105           (save-restriction
9106             (goto-char (point-min))
9107             (search-forward "\n\n" nil t)
9108             (narrow-to-region (point-min) (1- (point)))
9109             (goto-char (point-min))
9110             (unless (re-search-forward "^date:" nil t)
9111               (goto-char (point-max))
9112               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9113        ;; This doesn't look like an article, so we fudge some headers.
9114         (setq atts (file-attributes file)
9115               lines (count-lines (point-min) (point-max)))
9116         (insert "From: " (read-string "From: ") "\n"
9117                 "Subject: " (read-string "Subject: ") "\n"
9118                 "Date: " (message-make-date (nth 5 atts)) "\n"
9119                 "Message-ID: " (message-make-message-id) "\n"
9120                 "Lines: " (int-to-string lines) "\n"
9121                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9122       (setq group-art (gnus-request-accept-article group nil t))
9123       (kill-buffer (current-buffer)))
9124     (setq gnus-newsgroup-active (gnus-activate-group group))
9125     (forward-line 1)
9126     (gnus-summary-goto-article (cdr group-art) nil t)
9127     (when edit
9128       (gnus-summary-edit-article))))
9129
9130 (defun gnus-summary-create-article ()
9131   "Create an article in a mail newsgroup."
9132   (interactive)
9133   (let ((group gnus-newsgroup-name)
9134         (now (current-time))
9135         group-art)
9136     (unless (gnus-check-backend-function 'request-accept-article group)
9137       (error "%s does not support article importing" group))
9138     (save-excursion
9139       (set-buffer (gnus-get-buffer-create " *import file*"))
9140       (erase-buffer)
9141       (goto-char (point-min))
9142       ;; This doesn't look like an article, so we fudge some headers.
9143       (insert "From: " (read-string "From: ") "\n"
9144               "Subject: " (read-string "Subject: ") "\n"
9145               "Date: " (message-make-date now) "\n"
9146               "Message-ID: " (message-make-message-id) "\n")
9147       (setq group-art (gnus-request-accept-article group nil t))
9148       (kill-buffer (current-buffer)))
9149     (setq gnus-newsgroup-active (gnus-activate-group group))
9150     (forward-line 1)
9151     (gnus-summary-goto-article (cdr group-art) nil t)
9152     (gnus-summary-edit-article)))
9153
9154 (defun gnus-summary-article-posted-p ()
9155   "Say whether the current (mail) article is available from news as well.
9156 This will be the case if the article has both been mailed and posted."
9157   (interactive)
9158   (let ((id (mail-header-references (gnus-summary-article-header)))
9159         (gnus-override-method (car (gnus-refer-article-methods))))
9160     (if (gnus-request-head id "")
9161         (gnus-message 2 "The current message was found on %s"
9162                       gnus-override-method)
9163       (gnus-message 2 "The current message couldn't be found on %s"
9164                     gnus-override-method)
9165       nil)))
9166
9167 (defun gnus-summary-expire-articles (&optional now)
9168   "Expire all articles that are marked as expirable in the current group."
9169   (interactive)
9170   (when (and (not gnus-group-is-exiting-without-update-p)
9171              (gnus-check-backend-function
9172               'request-expire-articles gnus-newsgroup-name))
9173     ;; This backend supports expiry.
9174     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9175            (expirable (if total
9176                           (progn
9177                             ;; We need to update the info for
9178                             ;; this group for `gnus-list-of-read-articles'
9179                             ;; to give us the right answer.
9180                             (gnus-run-hooks 'gnus-exit-group-hook)
9181                             (gnus-summary-update-info)
9182                             (gnus-list-of-read-articles gnus-newsgroup-name))
9183                         (setq gnus-newsgroup-expirable
9184                               (sort gnus-newsgroup-expirable '<))))
9185            (expiry-wait (if now 'immediate
9186                           (gnus-group-find-parameter
9187                            gnus-newsgroup-name 'expiry-wait)))
9188            (nnmail-expiry-target
9189             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9190                 nnmail-expiry-target))
9191            es)
9192       (when expirable
9193         ;; There are expirable articles in this group, so we run them
9194         ;; through the expiry process.
9195         (gnus-message 6 "Expiring articles...")
9196         (unless (gnus-check-group gnus-newsgroup-name)
9197           (error "Can't open server for %s" gnus-newsgroup-name))
9198         ;; The list of articles that weren't expired is returned.
9199         (save-excursion
9200           (if expiry-wait
9201               (let ((nnmail-expiry-wait-function nil)
9202                     (nnmail-expiry-wait expiry-wait))
9203                 (setq es (gnus-request-expire-articles
9204                           expirable gnus-newsgroup-name)))
9205             (setq es (gnus-request-expire-articles
9206                       expirable gnus-newsgroup-name)))
9207           (unless total
9208             (setq gnus-newsgroup-expirable es))
9209           ;; We go through the old list of expirable, and mark all
9210           ;; really expired articles as nonexistent.
9211           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9212             (let ((gnus-use-cache nil))
9213               (dolist (article expirable)
9214                 (when (and (not (memq article es))
9215                            (gnus-data-find article))
9216                   (gnus-summary-mark-article article gnus-canceled-mark)
9217                   (run-hook-with-args 'gnus-summary-article-expire-hook
9218                                       'delete
9219                                       (gnus-data-header
9220                                        (assoc article (gnus-data-list nil)))
9221                                       gnus-newsgroup-name
9222                                       nil
9223                                       nil))))))
9224         (gnus-message 6 "Expiring articles...done")))))
9225
9226 (defun gnus-summary-expire-articles-now ()
9227   "Expunge all expirable articles in the current group.
9228 This means that *all* articles that are marked as expirable will be
9229 deleted forever, right now."
9230   (interactive)
9231   (or gnus-expert-user
9232       (gnus-yes-or-no-p
9233        "Are you really, really, really sure you want to delete all these messages? ")
9234       (error "Phew!"))
9235   (gnus-summary-expire-articles t))
9236
9237 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9238 (defun gnus-summary-delete-article (&optional n)
9239   "Delete the N next (mail) articles.
9240 This command actually deletes articles.  This is not a marking
9241 command.  The article will disappear forever from your life, never to
9242 return.
9243
9244 If N is negative, delete backwards.
9245 If N is nil and articles have been marked with the process mark,
9246 delete these instead.
9247
9248 If `gnus-novice-user' is non-nil you will be asked for
9249 confirmation before the articles are deleted."
9250   (interactive "P")
9251   (unless (gnus-check-backend-function 'request-expire-articles
9252                                        gnus-newsgroup-name)
9253     (error "The current newsgroup does not support article deletion"))
9254   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9255     (error "Couldn't open server"))
9256   ;; Compute the list of articles to delete.
9257   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9258         (nnmail-expiry-target 'delete)
9259         not-deleted)
9260     (if (and gnus-novice-user
9261              (not (gnus-yes-or-no-p
9262                    (format "Do you really want to delete %s forever? "
9263                            (if (> (length articles) 1)
9264                                (format "these %s articles" (length articles))
9265                              "this article")))))
9266         ()
9267       ;; Delete the articles.
9268       (setq not-deleted (gnus-request-expire-articles
9269                          articles gnus-newsgroup-name 'force))
9270       (while articles
9271         (gnus-summary-remove-process-mark (car articles))
9272         ;; The backend might not have been able to delete the article
9273         ;; after all.
9274         (unless (memq (car articles) not-deleted)
9275           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9276         (let* ((article (car articles))
9277                (id (mail-header-id (gnus-data-header 
9278                                     (assoc article (gnus-data-list nil))))))
9279           (run-hook-with-args 'gnus-summary-article-delete-hook
9280                               'delete id gnus-newsgroup-name nil
9281                               nil))
9282         (setq articles (cdr articles)))
9283       (when not-deleted
9284         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9285     (gnus-summary-position-point)
9286     (gnus-set-mode-line 'summary)
9287     not-deleted))
9288
9289 (defun gnus-summary-edit-article (&optional arg)
9290   "Edit the current article.
9291 This will have permanent effect only in mail groups.
9292 If ARG is nil, edit the decoded articles.
9293 If ARG is 1, edit the raw articles.
9294 If ARG is 2, edit the raw articles even in read-only groups.
9295 If ARG is 3, edit the articles with the current handles.
9296 Otherwise, allow editing of articles even in read-only
9297 groups."
9298   (interactive "P")
9299   (let (force raw current-handles)
9300     (cond
9301      ((null arg))
9302      ((eq arg 1)
9303       (setq raw t))
9304      ((eq arg 2)
9305       (setq raw t
9306             force t))
9307      ((eq arg 3)
9308       (setq current-handles
9309             (and (gnus-buffer-live-p gnus-article-buffer)
9310                  (with-current-buffer gnus-article-buffer
9311                    (prog1
9312                        gnus-article-mime-handles
9313                      (setq gnus-article-mime-handles nil))))))
9314      (t
9315       (setq force t)))
9316     (when (and raw (not force)
9317                (member gnus-newsgroup-name '("nndraft:delayed"
9318                                              "nndraft:drafts"
9319                                              "nndraft:queue")))
9320       (error "Can't edit the raw article in group %s"
9321              gnus-newsgroup-name))
9322     (save-excursion
9323       (set-buffer gnus-summary-buffer)
9324       (let ((mail-parse-charset gnus-newsgroup-charset)
9325             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9326         (gnus-set-global-variables)
9327         (when (and (not force)
9328                    (gnus-group-read-only-p))
9329           (error "The current newsgroup does not support article editing"))
9330         (gnus-summary-show-article t)
9331         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9332           (with-current-buffer gnus-article-buffer
9333             (mm-enable-multibyte)))
9334         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9335             (setq raw t))
9336         (gnus-article-edit-article
9337          (if raw 'ignore
9338            `(lambda ()
9339               (let ((mbl mml-buffer-list))
9340                 (setq mml-buffer-list nil)
9341                 (mime-to-mml ,'current-handles)
9342                 (let ((mbl1 mml-buffer-list))
9343                   (setq mml-buffer-list mbl)
9344                   (set (make-local-variable 'mml-buffer-list) mbl1))
9345                 (gnus-make-local-hook 'kill-buffer-hook)
9346                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9347          `(lambda (no-highlight)
9348             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9349                   (message-options message-options)
9350                   (message-options-set-recipient)
9351                   (mail-parse-ignored-charsets
9352                    ',gnus-newsgroup-ignored-charsets))
9353               ,(if (not raw) '(progn
9354                                 (mml-to-mime)
9355                                 (mml-destroy-buffers)
9356                                 (remove-hook 'kill-buffer-hook
9357                                              'mml-destroy-buffers t)
9358                                 (kill-local-variable 'mml-buffer-list)))
9359               (gnus-summary-edit-article-done
9360                ,(or (mail-header-references gnus-current-headers) "")
9361                ,(gnus-group-read-only-p)
9362                ,gnus-summary-buffer no-highlight))))))))
9363
9364 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9365
9366 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9367                                                  no-highlight)
9368   "Make edits to the current article permanent."
9369   (interactive)
9370   (save-excursion
9371    ;; The buffer restriction contains the entire article if it exists.
9372     (when (article-goto-body)
9373       (let ((lines (count-lines (point) (point-max)))
9374             (length (- (point-max) (point)))
9375             (case-fold-search t)
9376             (body (copy-marker (point))))
9377         (goto-char (point-min))
9378         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9379           (delete-region (match-beginning 1) (match-end 1))
9380           (insert (number-to-string length)))
9381         (goto-char (point-min))
9382         (when (re-search-forward
9383                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9384           (delete-region (match-beginning 1) (match-end 1))
9385           (insert (number-to-string length)))
9386         (goto-char (point-min))
9387         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9388           (delete-region (match-beginning 1) (match-end 1))
9389           (insert (number-to-string lines))))))
9390   ;; Replace the article.
9391   (let ((buf (current-buffer)))
9392     (with-temp-buffer
9393       (insert-buffer-substring buf)
9394
9395       (if (and (not read-only)
9396                (not (gnus-request-replace-article
9397                      (cdr gnus-article-current) (car gnus-article-current)
9398                      (current-buffer) t)))
9399           (error "Couldn't replace article")
9400         ;; Update the summary buffer.
9401         (if (and references
9402                  (equal (message-tokenize-header references " ")
9403                         (message-tokenize-header
9404                          (or (message-fetch-field "references") "") " ")))
9405             ;; We only have to update this line.
9406             (save-excursion
9407               (save-restriction
9408                 (message-narrow-to-head)
9409                 (let ((head (buffer-string))
9410                       header)
9411                   (with-temp-buffer
9412                     (insert (format "211 %d Article retrieved.\n"
9413                                     (cdr gnus-article-current)))
9414                     (insert head)
9415                     (insert ".\n")
9416                     (let ((nntp-server-buffer (current-buffer)))
9417                       (setq header (car (gnus-get-newsgroup-headers
9418                                          nil t))))
9419                     (save-excursion
9420                       (set-buffer gnus-summary-buffer)
9421                       (gnus-data-set-header
9422                        (gnus-data-find (cdr gnus-article-current))
9423                        header)
9424                       (gnus-summary-update-article-line
9425                        (cdr gnus-article-current) header)
9426                       (if (gnus-summary-goto-subject
9427                            (cdr gnus-article-current) nil t)
9428                           (gnus-summary-update-secondary-mark
9429                            (cdr gnus-article-current))))))))
9430           ;; Update threads.
9431           (set-buffer (or buffer gnus-summary-buffer))
9432           (gnus-summary-update-article (cdr gnus-article-current))
9433           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9434               (gnus-summary-update-secondary-mark
9435                (cdr gnus-article-current))))
9436         ;; Prettify the article buffer again.
9437         (unless no-highlight
9438           (save-excursion
9439             (set-buffer gnus-article-buffer)
9440             ;;;!!! Fix this -- article should be rehighlighted.
9441             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9442             (set-buffer gnus-original-article-buffer)
9443             (gnus-request-article
9444              (cdr gnus-article-current)
9445              (car gnus-article-current) (current-buffer))))
9446         ;; Prettify the summary buffer line.
9447         (when (gnus-visual-p 'summary-highlight 'highlight)
9448           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9449
9450 (defun gnus-summary-edit-wash (key)
9451   "Perform editing command KEY in the article buffer."
9452   (interactive
9453    (list
9454     (progn
9455       (message "%s" (concat (this-command-keys) "- "))
9456       (read-char))))
9457   (message "")
9458   (gnus-summary-edit-article)
9459   (execute-kbd-macro (concat (this-command-keys) key))
9460   (gnus-article-edit-done))
9461
9462 ;;; Respooling
9463
9464 (defun gnus-summary-respool-query (&optional silent trace)
9465   "Query where the respool algorithm would put this article."
9466   (interactive)
9467   (let (gnus-mark-article-hook)
9468     (gnus-summary-select-article)
9469     (save-excursion
9470       (set-buffer gnus-original-article-buffer)
9471       (save-restriction
9472         (message-narrow-to-head)
9473         (let ((groups (nnmail-article-group 'identity trace)))
9474           (unless silent
9475             (if groups
9476                 (message "This message would go to %s"
9477                          (mapconcat 'car groups ", "))
9478               (message "This message would go to no groups"))
9479             groups))))))
9480
9481 (defun gnus-summary-respool-trace ()
9482   "Trace where the respool algorithm would put this article.
9483 Display a buffer showing all fancy splitting patterns which matched."
9484   (interactive)
9485   (gnus-summary-respool-query nil t))
9486
9487 ;; Summary marking commands.
9488
9489 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9490   "Mark articles which has the same subject as read, and then select the next.
9491 If UNMARK is positive, remove any kind of mark.
9492 If UNMARK is negative, tick articles."
9493   (interactive "P")
9494   (when unmark
9495     (setq unmark (prefix-numeric-value unmark)))
9496   (let ((count
9497          (gnus-summary-mark-same-subject
9498           (gnus-summary-article-subject) unmark)))
9499     ;; Select next unread article.  If auto-select-same mode, should
9500     ;; select the first unread article.
9501     (gnus-summary-next-article t (and gnus-auto-select-same
9502                                       (gnus-summary-article-subject)))
9503     (gnus-message 7 "%d article%s marked as %s"
9504                   count (if (= count 1) " is" "s are")
9505                   (if unmark "unread" "read"))))
9506
9507 (defun gnus-summary-kill-same-subject (&optional unmark)
9508   "Mark articles which has the same subject as read.
9509 If UNMARK is positive, remove any kind of mark.
9510 If UNMARK is negative, tick articles."
9511   (interactive "P")
9512   (when unmark
9513     (setq unmark (prefix-numeric-value unmark)))
9514   (let ((count
9515          (gnus-summary-mark-same-subject
9516           (gnus-summary-article-subject) unmark)))
9517     ;; If marked as read, go to next unread subject.
9518     (when (null unmark)
9519       ;; Go to next unread subject.
9520       (gnus-summary-next-subject 1 t))
9521     (gnus-message 7 "%d articles are marked as %s"
9522                   count (if unmark "unread" "read"))))
9523
9524 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9525   "Mark articles with same SUBJECT as read, and return marked number.
9526 If optional argument UNMARK is positive, remove any kinds of marks.
9527 If optional argument UNMARK is negative, mark articles as unread instead."
9528   (let ((count 1))
9529     (save-excursion
9530       (cond
9531        ((null unmark)                   ; Mark as read.
9532         (while (and
9533                 (progn
9534                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9535                   (gnus-summary-show-thread) t)
9536                 (gnus-summary-find-subject subject))
9537           (setq count (1+ count))))
9538        ((> unmark 0)                    ; Tick.
9539         (while (and
9540                 (progn
9541                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9542                   (gnus-summary-show-thread) t)
9543                 (gnus-summary-find-subject subject))
9544           (setq count (1+ count))))
9545        (t                               ; Mark as unread.
9546         (while (and
9547                 (progn
9548                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9549                   (gnus-summary-show-thread) t)
9550                 (gnus-summary-find-subject subject))
9551           (setq count (1+ count)))))
9552       (gnus-set-mode-line 'summary)
9553       ;; Return the number of marked articles.
9554       count)))
9555
9556 (defun gnus-summary-mark-as-processable (n &optional unmark)
9557   "Set the process mark on the next N articles.
9558 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9559 the process mark instead.  The difference between N and the actual
9560 number of articles marked is returned."
9561   (interactive "P")
9562   (if (and (null n) (gnus-region-active-p))
9563       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9564     (setq n (prefix-numeric-value n))
9565     (let ((backward (< n 0))
9566           (n (abs n)))
9567       (while (and
9568               (> n 0)
9569               (if unmark
9570                   (gnus-summary-remove-process-mark
9571                    (gnus-summary-article-number))
9572                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9573               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9574         (setq n (1- n)))
9575       (when (/= 0 n)
9576         (gnus-message 7 "No more articles"))
9577       (gnus-summary-recenter)
9578       (gnus-summary-position-point)
9579       n)))
9580
9581 (defun gnus-summary-unmark-as-processable (n)
9582   "Remove the process mark from the next N articles.
9583 If N is negative, unmark backward instead.  The difference between N and
9584 the actual number of articles unmarked is returned."
9585   (interactive "P")
9586   (gnus-summary-mark-as-processable n t))
9587
9588 (defun gnus-summary-unmark-all-processable ()
9589   "Remove the process mark from all articles."
9590   (interactive)
9591   (save-excursion
9592     (while gnus-newsgroup-processable
9593       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9594   (gnus-summary-position-point))
9595
9596 (defun gnus-summary-add-mark (article type)
9597   "Mark ARTICLE with a mark of TYPE."
9598   (let ((vtype (car (assq type gnus-article-mark-lists)))
9599         var)
9600     (if (not vtype)
9601         (error "No such mark type: %s" type)
9602       (setq var (intern (format "gnus-newsgroup-%s" type)))
9603       (set var (cons article (symbol-value var)))
9604       (if (memq type '(processable cached replied forwarded recent saved))
9605           (gnus-summary-update-secondary-mark article)
9606         ;;; !!! This is bogus.  We should find out what primary
9607         ;;; !!! mark we want to set.
9608         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9609
9610 (defun gnus-summary-mark-as-expirable (n)
9611   "Mark N articles forward as expirable.
9612 If N is negative, mark backward instead.  The difference between N and
9613 the actual number of articles marked is returned."
9614   (interactive "p")
9615   (gnus-summary-mark-forward n gnus-expirable-mark))
9616
9617 (defun gnus-summary-mark-as-spam (n)
9618   "Mark N articles forward as spam.
9619 If N is negative, mark backward instead.  The difference between N and
9620 the actual number of articles marked is returned."
9621   (interactive "p")
9622   (gnus-summary-mark-forward n gnus-spam-mark))
9623
9624 (defun gnus-summary-mark-article-as-replied (article)
9625   "Mark ARTICLE as replied to and update the summary line.
9626 ARTICLE can also be a list of articles."
9627   (interactive (list (gnus-summary-article-number)))
9628   (let ((articles (if (listp article) article (list article))))
9629     (dolist (article articles)
9630       (unless (numberp article)
9631         (error "%s is not a number" article))
9632       (push article gnus-newsgroup-replied)
9633       (let ((buffer-read-only nil))
9634         (when (gnus-summary-goto-subject article nil t)
9635           (gnus-summary-update-secondary-mark article))))))
9636
9637 (defun gnus-summary-mark-article-as-forwarded (article)
9638   "Mark ARTICLE as forwarded and update the summary line.
9639 ARTICLE can also be a list of articles."
9640   (let ((articles (if (listp article) article (list article))))
9641     (dolist (article articles)
9642       (push article gnus-newsgroup-forwarded)
9643       (let ((buffer-read-only nil))
9644         (when (gnus-summary-goto-subject article nil t)
9645           (gnus-summary-update-secondary-mark article))))))
9646
9647 (defun gnus-summary-set-bookmark (article)
9648   "Set a bookmark in current article."
9649   (interactive (list (gnus-summary-article-number)))
9650   (when (or (not (get-buffer gnus-article-buffer))
9651             (not gnus-current-article)
9652             (not gnus-article-current)
9653             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9654     (error "No current article selected"))
9655   ;; Remove old bookmark, if one exists.
9656   (gnus-pull article gnus-newsgroup-bookmarks)
9657   ;; Set the new bookmark, which is on the form
9658   ;; (article-number . line-number-in-body).
9659   (push
9660    (cons article
9661          (save-excursion
9662            (set-buffer gnus-article-buffer)
9663            (count-lines
9664             (min (point)
9665                  (save-excursion
9666                    (article-goto-body)
9667                    (point)))
9668             (point))))
9669    gnus-newsgroup-bookmarks)
9670   (gnus-message 6 "A bookmark has been added to the current article."))
9671
9672 (defun gnus-summary-remove-bookmark (article)
9673   "Remove the bookmark from the current article."
9674   (interactive (list (gnus-summary-article-number)))
9675   ;; Remove old bookmark, if one exists.
9676   (if (not (assq article gnus-newsgroup-bookmarks))
9677       (gnus-message 6 "No bookmark in current article.")
9678     (gnus-pull article gnus-newsgroup-bookmarks)
9679     (gnus-message 6 "Removed bookmark.")))
9680
9681 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9682 (defun gnus-summary-mark-as-dormant (n)
9683   "Mark N articles forward as dormant.
9684 If N is negative, mark backward instead.  The difference between N and
9685 the actual number of articles marked is returned."
9686   (interactive "p")
9687   (gnus-summary-mark-forward n gnus-dormant-mark))
9688
9689 (defun gnus-summary-set-process-mark (article)
9690   "Set the process mark on ARTICLE and update the summary line."
9691   (setq gnus-newsgroup-processable
9692         (cons article
9693               (delq article gnus-newsgroup-processable)))
9694   (when (gnus-summary-goto-subject article)
9695     (gnus-summary-show-thread)
9696     (gnus-summary-goto-subject article)
9697     (gnus-summary-update-secondary-mark article)))
9698
9699 (defun gnus-summary-remove-process-mark (article)
9700   "Remove the process mark from ARTICLE and update the summary line."
9701   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9702   (when (gnus-summary-goto-subject article)
9703     (gnus-summary-show-thread)
9704     (gnus-summary-goto-subject article)
9705     (gnus-summary-update-secondary-mark article)))
9706
9707 (defun gnus-summary-set-saved-mark (article)
9708   "Set the process mark on ARTICLE and update the summary line."
9709   (push article gnus-newsgroup-saved)
9710   (when (gnus-summary-goto-subject article)
9711     (gnus-summary-update-secondary-mark article)))
9712
9713 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9714   "Mark N articles as read forwards.
9715 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9716 The difference between N and the actual number of articles marked is
9717 returned.
9718 If NO-EXPIRE, auto-expiry will be inhibited."
9719   (interactive "p")
9720   (gnus-summary-show-thread)
9721   (let ((backward (< n 0))
9722         (gnus-summary-goto-unread
9723          (and gnus-summary-goto-unread
9724               (not (eq gnus-summary-goto-unread 'never))
9725               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9726                                     gnus-ticked-mark gnus-dormant-mark)))))
9727         (n (abs n))
9728         (mark (or mark gnus-del-mark)))
9729     (while (and (> n 0)
9730                 (gnus-summary-mark-article nil mark no-expire)
9731                 (zerop (gnus-summary-next-subject
9732                         (if backward -1 1)
9733                         (and gnus-summary-goto-unread
9734                              (not (eq gnus-summary-goto-unread 'never)))
9735                         t)))
9736       (setq n (1- n)))
9737     (when (/= 0 n)
9738       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9739     (gnus-summary-recenter)
9740     (gnus-summary-position-point)
9741     (gnus-set-mode-line 'summary)
9742     n))
9743
9744 (defun gnus-summary-mark-article-as-read (mark)
9745   "Mark the current article quickly as read with MARK."
9746   (let ((article (gnus-summary-article-number)))
9747     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9748     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9749     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9750     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9751     (push (cons article mark) gnus-newsgroup-reads)
9752     ;; Possibly remove from cache, if that is used.
9753     (when gnus-use-cache
9754       (gnus-cache-enter-remove-article article))
9755     ;; Allow the backend to change the mark.
9756     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9757     ;; Check for auto-expiry.
9758     (when (and gnus-newsgroup-auto-expire
9759                (memq mark gnus-auto-expirable-marks))
9760       (setq mark gnus-expirable-mark)
9761       ;; Let the backend know about the mark change.
9762       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9763       (push article gnus-newsgroup-expirable))
9764     ;; Set the mark in the buffer.
9765     (gnus-summary-update-mark mark 'unread)
9766     t))
9767
9768 (defun gnus-summary-mark-article-as-unread (mark)
9769   "Mark the current article quickly as unread with MARK."
9770   (let* ((article (gnus-summary-article-number))
9771          (old-mark (gnus-summary-article-mark article)))
9772     ;; Allow the backend to change the mark.
9773     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9774     (if (eq mark old-mark)
9775         t
9776       (if (<= article 0)
9777           (progn
9778             (gnus-error 1 "Can't mark negative article numbers")
9779             nil)
9780         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9781         (setq gnus-newsgroup-spam-marked
9782               (delq article gnus-newsgroup-spam-marked))
9783         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9784         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9785         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9786         (cond ((= mark gnus-ticked-mark)
9787                (setq gnus-newsgroup-marked
9788                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9789                                               article)))
9790               ((= mark gnus-spam-mark)
9791                (setq gnus-newsgroup-spam-marked
9792                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9793                                               article)))
9794               ((= mark gnus-dormant-mark)
9795                (setq gnus-newsgroup-dormant
9796                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9797                                               article)))
9798               (t
9799                (setq gnus-newsgroup-unreads
9800                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9801                                               article))))
9802         (gnus-pull article gnus-newsgroup-reads)
9803
9804         ;; See whether the article is to be put in the cache.
9805         (and gnus-use-cache
9806              (vectorp (gnus-summary-article-header article))
9807              (save-excursion
9808                (gnus-cache-possibly-enter-article
9809                 gnus-newsgroup-name article
9810                 (= mark gnus-ticked-mark)
9811                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9812
9813         ;; Fix the mark.
9814         (gnus-summary-update-mark mark 'unread)
9815         t))))
9816
9817 (defun gnus-summary-mark-article (&optional article mark no-expire)
9818   "Mark ARTICLE with MARK.  MARK can be any character.
9819 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9820 `??' (dormant) and `?E' (expirable).
9821 If MARK is nil, then the default character `?r' is used.
9822 If ARTICLE is nil, then the article on the current line will be
9823 marked.
9824 If NO-EXPIRE, auto-expiry will be inhibited."
9825   ;; The mark might be a string.
9826   (when (stringp mark)
9827     (setq mark (aref mark 0)))
9828   ;; If no mark is given, then we check auto-expiring.
9829   (when (null mark)
9830     (setq mark gnus-del-mark))
9831   (when (and (not no-expire)
9832              gnus-newsgroup-auto-expire
9833              (memq mark gnus-auto-expirable-marks))
9834     (setq mark gnus-expirable-mark))
9835   (let ((article (or article (gnus-summary-article-number)))
9836         (old-mark (gnus-summary-article-mark article)))
9837     ;; Allow the backend to change the mark.
9838     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9839     (if (eq mark old-mark)
9840         t
9841       (unless article
9842         (error "No article on current line"))
9843       (if (not (if (or (= mark gnus-unread-mark)
9844                        (= mark gnus-ticked-mark)
9845                        (= mark gnus-spam-mark)
9846                        (= mark gnus-dormant-mark))
9847                    (gnus-mark-article-as-unread article mark)
9848                  (gnus-mark-article-as-read article mark)))
9849           t
9850         ;; See whether the article is to be put in the cache.
9851         (and gnus-use-cache
9852              (not (= mark gnus-canceled-mark))
9853              (vectorp (gnus-summary-article-header article))
9854              (save-excursion
9855                (gnus-cache-possibly-enter-article
9856                 gnus-newsgroup-name article
9857                 (= mark gnus-ticked-mark)
9858                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9859
9860         (when (gnus-summary-goto-subject article nil t)
9861           (let ((buffer-read-only nil))
9862             (gnus-summary-show-thread)
9863             ;; Fix the mark.
9864             (gnus-summary-update-mark mark 'unread)
9865             t))))))
9866
9867 (defun gnus-summary-update-secondary-mark (article)
9868   "Update the secondary (read, process, cache) mark."
9869   (gnus-summary-update-mark
9870    (cond ((memq article gnus-newsgroup-processable)
9871           gnus-process-mark)
9872          ((memq article gnus-newsgroup-cached)
9873           gnus-cached-mark)
9874          ((memq article gnus-newsgroup-replied)
9875           gnus-replied-mark)
9876          ((memq article gnus-newsgroup-forwarded)
9877           gnus-forwarded-mark)
9878          ((memq article gnus-newsgroup-saved)
9879           gnus-saved-mark)
9880          ((memq article gnus-newsgroup-recent)
9881           gnus-recent-mark)
9882          ((memq article gnus-newsgroup-unseen)
9883           gnus-unseen-mark)
9884          (t gnus-no-mark))
9885    'replied)
9886   (when (gnus-visual-p 'summary-highlight 'highlight)
9887     (gnus-run-hooks 'gnus-summary-update-hook))
9888   t)
9889
9890 (defun gnus-summary-update-download-mark (article)
9891   "Update the download mark."
9892   (gnus-summary-update-mark
9893    (cond ((memq article gnus-newsgroup-undownloaded) 
9894           gnus-undownloaded-mark)
9895          (gnus-newsgroup-agentized
9896           gnus-downloaded-mark)
9897          (t
9898           gnus-no-mark))
9899    'download)
9900   (gnus-summary-update-line t)
9901   t)
9902
9903 (defun gnus-summary-update-mark (mark type)
9904   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9905         (buffer-read-only nil))
9906     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9907     (when forward
9908       (when (looking-at "\r")
9909         (incf forward))
9910       (when (<= (+ forward (point)) (point-max))
9911         ;; Go to the right position on the line.
9912         (goto-char (+ forward (point)))
9913         ;; Replace the old mark with the new mark.
9914         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9915         ;; Optionally update the marks by some user rule.
9916         (when (eq type 'unread)
9917           (gnus-data-set-mark
9918            (gnus-data-find (gnus-summary-article-number)) mark)
9919           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9920
9921 (defun gnus-mark-article-as-read (article &optional mark)
9922   "Enter ARTICLE in the pertinent lists and remove it from others."
9923   ;; Make the article expirable.
9924   (let ((mark (or mark gnus-del-mark)))
9925     (setq gnus-newsgroup-expirable
9926           (if (= mark gnus-expirable-mark)
9927               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9928             (delq article gnus-newsgroup-expirable)))
9929     ;; Remove from unread and marked lists.
9930     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9931     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9932     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9933     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9934     (push (cons article mark) gnus-newsgroup-reads)
9935     ;; Possibly remove from cache, if that is used.
9936     (when gnus-use-cache
9937       (gnus-cache-enter-remove-article article))
9938     t))
9939
9940 (defun gnus-mark-article-as-unread (article &optional mark)
9941   "Enter ARTICLE in the pertinent lists and remove it from others."
9942   (let ((mark (or mark gnus-ticked-mark)))
9943     (if (<= article 0)
9944         (progn
9945           (gnus-error 1 "Can't mark negative article numbers")
9946           nil)
9947       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9948             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9949             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9950             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9951             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9952
9953       ;; Unsuppress duplicates?
9954       (when gnus-suppress-duplicates
9955         (gnus-dup-unsuppress-article article))
9956
9957       (cond ((= mark gnus-ticked-mark)
9958              (setq gnus-newsgroup-marked
9959                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9960             ((= mark gnus-spam-mark)
9961              (setq gnus-newsgroup-spam-marked
9962                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9963                                             article)))
9964             ((= mark gnus-dormant-mark)
9965              (setq gnus-newsgroup-dormant
9966                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9967             (t
9968              (setq gnus-newsgroup-unreads
9969                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9970       (gnus-pull article gnus-newsgroup-reads)
9971       t)))
9972
9973 (defalias 'gnus-summary-mark-as-unread-forward
9974   'gnus-summary-tick-article-forward)
9975 (make-obsolete 'gnus-summary-mark-as-unread-forward
9976                'gnus-summary-tick-article-forward)
9977 (defun gnus-summary-tick-article-forward (n)
9978   "Tick N articles forwards.
9979 If N is negative, tick backwards instead.
9980 The difference between N and the number of articles ticked is returned."
9981   (interactive "p")
9982   (gnus-summary-mark-forward n gnus-ticked-mark))
9983
9984 (defalias 'gnus-summary-mark-as-unread-backward
9985   'gnus-summary-tick-article-backward)
9986 (make-obsolete 'gnus-summary-mark-as-unread-backward
9987                'gnus-summary-tick-article-backward)
9988 (defun gnus-summary-tick-article-backward (n)
9989   "Tick N articles backwards.
9990 The difference between N and the number of articles ticked is returned."
9991   (interactive "p")
9992   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9993
9994 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9995 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9996 (defun gnus-summary-tick-article (&optional article clear-mark)
9997   "Mark current article as unread.
9998 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9999 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10000   (interactive)
10001   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10002                                        gnus-ticked-mark)))
10003
10004 (defun gnus-summary-mark-as-read-forward (n)
10005   "Mark N articles as read forwards.
10006 If N is negative, mark backwards instead.
10007 The difference between N and the actual number of articles marked is
10008 returned."
10009   (interactive "p")
10010   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10011
10012 (defun gnus-summary-mark-as-read-backward (n)
10013   "Mark the N articles as read backwards.
10014 The difference between N and the actual number of articles marked is
10015 returned."
10016   (interactive "p")
10017   (gnus-summary-mark-forward
10018    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10019
10020 (defun gnus-summary-mark-as-read (&optional article mark)
10021   "Mark current article as read.
10022 ARTICLE specifies the article to be marked as read.
10023 MARK specifies a string to be inserted at the beginning of the line."
10024   (gnus-summary-mark-article article mark))
10025
10026 (defun gnus-summary-clear-mark-forward (n)
10027   "Clear marks from N articles forward.
10028 If N is negative, clear backward instead.
10029 The difference between N and the number of marks cleared is returned."
10030   (interactive "p")
10031   (gnus-summary-mark-forward n gnus-unread-mark))
10032
10033 (defun gnus-summary-clear-mark-backward (n)
10034   "Clear marks from N articles backward.
10035 The difference between N and the number of marks cleared is returned."
10036   (interactive "p")
10037   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10038
10039 (defun gnus-summary-mark-unread-as-read ()
10040   "Intended to be used by `gnus-summary-mark-article-hook'."
10041   (when (memq gnus-current-article gnus-newsgroup-unreads)
10042     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10043
10044 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10045   "Intended to be used by `gnus-summary-mark-article-hook'."
10046   (let ((mark (gnus-summary-article-mark)))
10047     (when (or (gnus-unread-mark-p mark)
10048               (gnus-read-mark-p mark))
10049       (gnus-summary-mark-article gnus-current-article
10050                                  (or new-mark gnus-read-mark)))))
10051
10052 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10053   "Intended to be used by `gnus-summary-mark-article-hook'."
10054   (let ((mark (gnus-summary-article-mark)))
10055     (when (or (gnus-unread-mark-p mark)
10056               (gnus-read-mark-p mark))
10057       (gnus-summary-mark-article (gnus-summary-article-number)
10058                                  (or new-mark gnus-read-mark)))))
10059
10060 (defun gnus-summary-mark-unread-as-ticked ()
10061   "Intended to be used by `gnus-summary-mark-article-hook'."
10062   (when (memq gnus-current-article gnus-newsgroup-unreads)
10063     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10064
10065 (defun gnus-summary-mark-region-as-read (point mark all)
10066   "Mark all unread articles between point and mark as read.
10067 If given a prefix, mark all articles between point and mark as read,
10068 even ticked and dormant ones."
10069   (interactive "r\nP")
10070   (save-excursion
10071     (let (article)
10072       (goto-char point)
10073       (beginning-of-line)
10074       (while (and
10075               (< (point) mark)
10076               (progn
10077                 (when (or all
10078                           (memq (setq article (gnus-summary-article-number))
10079                                 gnus-newsgroup-unreads))
10080                   (gnus-summary-mark-article article gnus-del-mark))
10081                 t)
10082               (gnus-summary-find-next))))))
10083
10084 (defun gnus-summary-mark-below (score mark)
10085   "Mark articles with score less than SCORE with MARK."
10086   (interactive "P\ncMark: ")
10087   (setq score (if score
10088                   (prefix-numeric-value score)
10089                 (or gnus-summary-default-score 0)))
10090   (save-excursion
10091     (set-buffer gnus-summary-buffer)
10092     (goto-char (point-min))
10093     (while
10094         (progn
10095           (and (< (gnus-summary-article-score) score)
10096                (gnus-summary-mark-article nil mark))
10097           (gnus-summary-find-next)))))
10098
10099 (defun gnus-summary-kill-below (&optional score)
10100   "Mark articles with score below SCORE as read."
10101   (interactive "P")
10102   (gnus-summary-mark-below score gnus-killed-mark))
10103
10104 (defun gnus-summary-clear-above (&optional score)
10105   "Clear all marks from articles with score above SCORE."
10106   (interactive "P")
10107   (gnus-summary-mark-above score gnus-unread-mark))
10108
10109 (defun gnus-summary-tick-above (&optional score)
10110   "Tick all articles with score above SCORE."
10111   (interactive "P")
10112   (gnus-summary-mark-above score gnus-ticked-mark))
10113
10114 (defun gnus-summary-mark-above (score mark)
10115   "Mark articles with score over SCORE with MARK."
10116   (interactive "P\ncMark: ")
10117   (setq score (if score
10118                   (prefix-numeric-value score)
10119                 (or gnus-summary-default-score 0)))
10120   (save-excursion
10121     (set-buffer gnus-summary-buffer)
10122     (goto-char (point-min))
10123     (while (and (progn
10124                   (when (> (gnus-summary-article-score) score)
10125                     (gnus-summary-mark-article nil mark))
10126                   t)
10127                 (gnus-summary-find-next)))))
10128
10129 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10130 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10131 (defun gnus-summary-limit-include-expunged (&optional no-error)
10132   "Display all the hidden articles that were expunged for low scores."
10133   (interactive)
10134   (let ((buffer-read-only nil))
10135     (let ((scored gnus-newsgroup-scored)
10136           headers h)
10137       (while scored
10138         (unless (gnus-summary-article-header (caar scored))
10139           (and (setq h (gnus-number-to-header (caar scored)))
10140                (< (cdar scored) gnus-summary-expunge-below)
10141                (push h headers)))
10142         (setq scored (cdr scored)))
10143       (if (not headers)
10144           (when (not no-error)
10145             (error "No expunged articles hidden"))
10146         (goto-char (point-min))
10147         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10148         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10149         (mapcar (lambda (x) (push (mail-header-number x)
10150                                   gnus-newsgroup-limit))
10151                 headers)
10152         (gnus-summary-prepare-unthreaded (nreverse headers))
10153         (goto-char (point-min))
10154         (gnus-summary-position-point)
10155         t))))
10156
10157 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10158   "Mark all unread articles in this newsgroup as read.
10159 If prefix argument ALL is non-nil, ticked and dormant articles will
10160 also be marked as read.
10161 If QUIETLY is non-nil, no questions will be asked.
10162
10163 If TO-HERE is non-nil, it should be a point in the buffer.  All
10164 articles before (after, if REVERSE is set) this point will be marked
10165 as read.
10166
10167 Note that this function will only catch up the unread article
10168 in the current summary buffer limitation.
10169
10170 The number of articles marked as read is returned."
10171   (interactive "P")
10172   (prog1
10173       (save-excursion
10174         (when (or quietly
10175                   (not gnus-interactive-catchup) ;Without confirmation?
10176                   gnus-expert-user
10177                   (gnus-y-or-n-p
10178                    (if all
10179                        "Mark absolutely all articles as read? "
10180                      "Mark all unread articles as read? ")))
10181           (if (and not-mark
10182                    (not gnus-newsgroup-adaptive)
10183                    (not gnus-newsgroup-auto-expire)
10184                    (not gnus-suppress-duplicates)
10185                    (or (not gnus-use-cache)
10186                        (eq gnus-use-cache 'passive)))
10187               (progn
10188                 (when all
10189                   (setq gnus-newsgroup-marked nil
10190                         gnus-newsgroup-spam-marked nil
10191                         gnus-newsgroup-dormant nil))
10192                 (setq gnus-newsgroup-unreads
10193                       (gnus-sorted-nunion
10194                        (gnus-intersection gnus-newsgroup-unreads
10195                                           gnus-newsgroup-downloadable)
10196                        gnus-newsgroup-unfetched)))
10197             ;; We actually mark all articles as canceled, which we
10198             ;; have to do when using auto-expiry or adaptive scoring.
10199             (gnus-summary-show-all-threads)
10200             (if (and to-here reverse)
10201                 (progn
10202                   (goto-char to-here)
10203                   (gnus-summary-mark-current-read-and-unread-as-read
10204                    gnus-catchup-mark)
10205                   (while (gnus-summary-find-next (not all))
10206                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10207               (when (gnus-summary-first-subject (not all))
10208                 (while (and
10209                         (if to-here (< (point) to-here) t)
10210                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10211                         (gnus-summary-find-next (not all))))))
10212             (gnus-set-mode-line 'summary))
10213           t))
10214     (gnus-summary-position-point)))
10215
10216 (defun gnus-summary-catchup-to-here (&optional all)
10217   "Mark all unticked articles before the current one as read.
10218 If ALL is non-nil, also mark ticked and dormant articles as read."
10219   (interactive "P")
10220   (save-excursion
10221     (gnus-save-hidden-threads
10222       (let ((beg (point)))
10223         ;; We check that there are unread articles.
10224         (when (or all (gnus-summary-find-prev))
10225           (gnus-summary-catchup all t beg)))))
10226   (gnus-summary-position-point))
10227
10228 (defun gnus-summary-catchup-from-here (&optional all)
10229   "Mark all unticked articles after (and including) the current one as read.
10230 If ALL is non-nil, also mark ticked and dormant articles as read."
10231   (interactive "P")
10232   (save-excursion
10233     (gnus-save-hidden-threads
10234       (let ((beg (point)))
10235         ;; We check that there are unread articles.
10236         (when (or all (gnus-summary-find-next))
10237           (gnus-summary-catchup all t beg nil t)))))
10238   (gnus-summary-position-point))
10239
10240 (defun gnus-summary-catchup-all (&optional quietly)
10241   "Mark all articles in this newsgroup as read.
10242 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10243 instead, which marks only unread articles as read."
10244   (interactive "P")
10245   (gnus-summary-catchup t quietly))
10246
10247 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10248   "Mark all unread articles in this group as read, then exit.
10249 If prefix argument ALL is non-nil, all articles are marked as read.
10250 If QUIETLY is non-nil, no questions will be asked."
10251   (interactive "P")
10252   (when (gnus-summary-catchup all quietly nil 'fast)
10253     ;; Select next newsgroup or exit.
10254     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10255              (eq gnus-auto-select-next 'quietly))
10256         (gnus-summary-next-group nil)
10257       (gnus-summary-exit))))
10258
10259 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10260   "Mark all articles in this newsgroup as read, and then exit.
10261 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10262 instead, which marks only unread articles as read."
10263   (interactive "P")
10264   (gnus-summary-catchup-and-exit t quietly))
10265
10266 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10267   "Mark all articles in this group as read and select the next group.
10268 If given a prefix, mark all articles, unread as well as ticked, as
10269 read."
10270   (interactive "P")
10271   (save-excursion
10272     (gnus-summary-catchup all))
10273   (gnus-summary-next-group))
10274
10275 ;;;
10276 ;;; with article
10277 ;;;
10278
10279 (defmacro gnus-with-article (article &rest forms)
10280   "Select ARTICLE and perform FORMS in the original article buffer.
10281 Then replace the article with the result."
10282   `(progn
10283      ;; We don't want the article to be marked as read.
10284      (let (gnus-mark-article-hook)
10285        (gnus-summary-select-article t t nil ,article))
10286      (set-buffer gnus-original-article-buffer)
10287      ,@forms
10288      (if (not (gnus-check-backend-function
10289                'request-replace-article (car gnus-article-current)))
10290          (gnus-message 5 "Read-only group; not replacing")
10291        (unless (gnus-request-replace-article
10292                 ,article (car gnus-article-current)
10293                 (current-buffer) t)
10294          (error "Couldn't replace article")))
10295      ;; The cache and backlog have to be flushed somewhat.
10296      (when gnus-keep-backlog
10297        (gnus-backlog-remove-article
10298         (car gnus-article-current) (cdr gnus-article-current)))
10299      (when gnus-use-cache
10300        (gnus-cache-update-article
10301         (car gnus-article-current) (cdr gnus-article-current)))))
10302
10303 (put 'gnus-with-article 'lisp-indent-function 1)
10304 (put 'gnus-with-article 'edebug-form-spec '(form body))
10305
10306 ;; Thread-based commands.
10307
10308 (defun gnus-summary-articles-in-thread (&optional article)
10309   "Return a list of all articles in the current thread.
10310 If ARTICLE is non-nil, return all articles in the thread that starts
10311 with that article."
10312   (let* ((article (or article (gnus-summary-article-number)))
10313          (data (gnus-data-find-list article))
10314          (top-level (gnus-data-level (car data)))
10315          (top-subject
10316           (cond ((null gnus-thread-operation-ignore-subject)
10317                  (gnus-simplify-subject-re
10318                   (mail-header-subject (gnus-data-header (car data)))))
10319                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10320                  (gnus-simplify-subject-fuzzy
10321                   (mail-header-subject (gnus-data-header (car data)))))
10322                 (t nil)))
10323          (end-point (save-excursion
10324                       (if (gnus-summary-go-to-next-thread)
10325                           (point) (point-max))))
10326          articles)
10327     (while (and data
10328                 (< (gnus-data-pos (car data)) end-point))
10329       (when (or (not top-subject)
10330                 (string= top-subject
10331                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10332                              (gnus-simplify-subject-fuzzy
10333                               (mail-header-subject
10334                                (gnus-data-header (car data))))
10335                            (gnus-simplify-subject-re
10336                             (mail-header-subject
10337                              (gnus-data-header (car data)))))))
10338         (push (gnus-data-number (car data)) articles))
10339       (unless (and (setq data (cdr data))
10340                    (> (gnus-data-level (car data)) top-level))
10341         (setq data nil)))
10342     ;; Return the list of articles.
10343     (nreverse articles)))
10344
10345 (defun gnus-summary-rethread-current ()
10346   "Rethread the thread the current article is part of."
10347   (interactive)
10348   (let* ((gnus-show-threads t)
10349          (article (gnus-summary-article-number))
10350          (id (mail-header-id (gnus-summary-article-header)))
10351          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10352     (unless id
10353       (error "No article on the current line"))
10354     (gnus-rebuild-thread id)
10355     (gnus-summary-goto-subject article)))
10356
10357 (defun gnus-summary-reparent-thread ()
10358   "Make the current article child of the marked (or previous) article.
10359
10360 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10361 is non-nil or the Subject: of both articles are the same."
10362   (interactive)
10363   (unless (not (gnus-group-read-only-p))
10364     (error "The current newsgroup does not support article editing"))
10365   (unless (<= (length gnus-newsgroup-processable) 1)
10366     (error "No more than one article may be marked"))
10367   (save-window-excursion
10368     (let ((gnus-article-buffer " *reparent*")
10369           (current-article (gnus-summary-article-number))
10370           ;; First grab the marked article, otherwise one line up.
10371           (parent-article (if (not (null gnus-newsgroup-processable))
10372                               (car gnus-newsgroup-processable)
10373                             (save-excursion
10374                               (if (eq (forward-line -1) 0)
10375                                   (gnus-summary-article-number)
10376                                 (error "Beginning of summary buffer"))))))
10377       (unless (not (eq current-article parent-article))
10378         (error "An article may not be self-referential"))
10379       (let ((message-id (mail-header-id
10380                          (gnus-summary-article-header parent-article))))
10381         (unless (and message-id (not (equal message-id "")))
10382           (error "No message-id in desired parent"))
10383         (gnus-with-article current-article
10384           (save-restriction
10385             (goto-char (point-min))
10386             (message-narrow-to-head)
10387             (if (re-search-forward "^References: " nil t)
10388                 (progn
10389                   (re-search-forward "^[^ \t]" nil t)
10390                   (forward-line -1)
10391                   (end-of-line)
10392                   (insert " " message-id))
10393               (insert "References: " message-id "\n"))))
10394         (set-buffer gnus-summary-buffer)
10395         (gnus-summary-unmark-all-processable)
10396         (gnus-summary-update-article current-article)
10397         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10398             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10399         (gnus-summary-rethread-current)
10400         (gnus-message 3 "Article %d is now the child of article %d"
10401                       current-article parent-article)))))
10402
10403 (defun gnus-summary-toggle-threads (&optional arg)
10404   "Toggle showing conversation threads.
10405 If ARG is positive number, turn showing conversation threads on."
10406   (interactive "P")
10407   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10408     (setq gnus-show-threads
10409           (if (null arg) (not gnus-show-threads)
10410             (> (prefix-numeric-value arg) 0)))
10411     (gnus-summary-prepare)
10412     (gnus-summary-goto-subject current)
10413     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10414     (gnus-summary-position-point)))
10415
10416 (defun gnus-summary-show-all-threads ()
10417   "Show all threads."
10418   (interactive)
10419   (save-excursion
10420     (let ((buffer-read-only nil))
10421       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10422   (gnus-summary-position-point))
10423
10424 (defun gnus-summary-show-thread ()
10425   "Show thread subtrees.
10426 Returns nil if no thread was there to be shown."
10427   (interactive)
10428   (let ((buffer-read-only nil)
10429         (orig (point))
10430         (end (gnus-point-at-eol))
10431         ;; Leave point at bol
10432         (beg (progn (beginning-of-line) (point))))
10433     (prog1
10434         ;; Any hidden lines here?
10435         (search-forward "\r" end t)
10436       (subst-char-in-region beg end ?\^M ?\n t)
10437       (goto-char orig)
10438       (gnus-summary-position-point))))
10439
10440 (defun gnus-summary-maybe-hide-threads ()
10441   "If requested, hide the threads that should be hidden."
10442   (when (and gnus-show-threads
10443              gnus-thread-hide-subtree)
10444     (gnus-summary-hide-all-threads
10445      (if (or (consp gnus-thread-hide-subtree)
10446              (functionp gnus-thread-hide-subtree))
10447          (gnus-make-predicate gnus-thread-hide-subtree)
10448        nil))))
10449
10450 ;;; Hiding predicates.
10451
10452 (defun gnus-article-unread-p (header)
10453   (memq (mail-header-number header) gnus-newsgroup-unreads))
10454
10455 (defun gnus-article-unseen-p (header)
10456   (memq (mail-header-number header) gnus-newsgroup-unseen))
10457
10458 (defun gnus-map-articles (predicate articles)
10459   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10460   (apply 'gnus-or (mapcar predicate
10461                           (mapcar 'gnus-summary-article-header articles))))
10462
10463 (defun gnus-summary-hide-all-threads (&optional predicate)
10464   "Hide all thread subtrees.
10465 If PREDICATE is supplied, threads that satisfy this predicate
10466 will not be hidden."
10467   (interactive)
10468   (save-excursion
10469     (goto-char (point-min))
10470     (let ((end nil))
10471       (while (not end)
10472         (when (or (not predicate)
10473                   (gnus-map-articles
10474                    predicate (gnus-summary-article-children)))
10475             (gnus-summary-hide-thread))
10476         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10477   (gnus-summary-position-point))
10478
10479 (defun gnus-summary-hide-thread ()
10480   "Hide thread subtrees.
10481 If PREDICATE is supplied, threads that satisfy this predicate
10482 will not be hidden.
10483 Returns nil if no threads were there to be hidden."
10484   (interactive)
10485   (let ((buffer-read-only nil)
10486         (start (point))
10487         (article (gnus-summary-article-number)))
10488     (goto-char start)
10489     ;; Go forward until either the buffer ends or the subthread
10490     ;; ends.
10491     (when (and (not (eobp))
10492                (or (zerop (gnus-summary-next-thread 1 t))
10493                    (goto-char (point-max))))
10494       (prog1
10495           (if (and (> (point) start)
10496                    (search-backward "\n" start t))
10497               (progn
10498                 (subst-char-in-region start (point) ?\n ?\^M)
10499                 (gnus-summary-goto-subject article))
10500             (goto-char start)
10501             nil)))))
10502
10503 (defun gnus-summary-go-to-next-thread (&optional previous)
10504   "Go to the same level (or less) next thread.
10505 If PREVIOUS is non-nil, go to previous thread instead.
10506 Return the article number moved to, or nil if moving was impossible."
10507   (let ((level (gnus-summary-thread-level))
10508         (way (if previous -1 1))
10509         (beg (point)))
10510     (forward-line way)
10511     (while (and (not (eobp))
10512                 (< level (gnus-summary-thread-level)))
10513       (forward-line way))
10514     (if (eobp)
10515         (progn
10516           (goto-char beg)
10517           nil)
10518       (setq beg (point))
10519       (prog1
10520           (gnus-summary-article-number)
10521         (goto-char beg)))))
10522
10523 (defun gnus-summary-next-thread (n &optional silent)
10524   "Go to the same level next N'th thread.
10525 If N is negative, search backward instead.
10526 Returns the difference between N and the number of skips actually
10527 done.
10528
10529 If SILENT, don't output messages."
10530   (interactive "p")
10531   (let ((backward (< n 0))
10532         (n (abs n)))
10533     (while (and (> n 0)
10534                 (gnus-summary-go-to-next-thread backward))
10535       (decf n))
10536     (unless silent
10537       (gnus-summary-position-point))
10538     (when (and (not silent) (/= 0 n))
10539       (gnus-message 7 "No more threads"))
10540     n))
10541
10542 (defun gnus-summary-prev-thread (n)
10543   "Go to the same level previous N'th thread.
10544 Returns the difference between N and the number of skips actually
10545 done."
10546   (interactive "p")
10547   (gnus-summary-next-thread (- n)))
10548
10549 (defun gnus-summary-go-down-thread ()
10550   "Go down one level in the current thread."
10551   (let ((children (gnus-summary-article-children)))
10552     (when children
10553       (gnus-summary-goto-subject (car children)))))
10554
10555 (defun gnus-summary-go-up-thread ()
10556   "Go up one level in the current thread."
10557   (let ((parent (gnus-summary-article-parent)))
10558     (when parent
10559       (gnus-summary-goto-subject parent))))
10560
10561 (defun gnus-summary-down-thread (n)
10562   "Go down thread N steps.
10563 If N is negative, go up instead.
10564 Returns the difference between N and how many steps down that were
10565 taken."
10566   (interactive "p")
10567   (let ((up (< n 0))
10568         (n (abs n)))
10569     (while (and (> n 0)
10570                 (if up (gnus-summary-go-up-thread)
10571                   (gnus-summary-go-down-thread)))
10572       (setq n (1- n)))
10573     (gnus-summary-position-point)
10574     (when (/= 0 n)
10575       (gnus-message 7 "Can't go further"))
10576     n))
10577
10578 (defun gnus-summary-up-thread (n)
10579   "Go up thread N steps.
10580 If N is negative, go down instead.
10581 Returns the difference between N and how many steps down that were
10582 taken."
10583   (interactive "p")
10584   (gnus-summary-down-thread (- n)))
10585
10586 (defun gnus-summary-top-thread ()
10587   "Go to the top of the thread."
10588   (interactive)
10589   (while (gnus-summary-go-up-thread))
10590   (gnus-summary-article-number))
10591
10592 (defun gnus-summary-kill-thread (&optional unmark)
10593   "Mark articles under current thread as read.
10594 If the prefix argument is positive, remove any kinds of marks.
10595 If the prefix argument is negative, tick articles instead."
10596   (interactive "P")
10597   (when unmark
10598     (setq unmark (prefix-numeric-value unmark)))
10599   (let ((articles (gnus-summary-articles-in-thread)))
10600     (save-excursion
10601       ;; Expand the thread.
10602       (gnus-summary-show-thread)
10603       ;; Mark all the articles.
10604       (while articles
10605         (gnus-summary-goto-subject (car articles))
10606         (cond ((null unmark)
10607                (gnus-summary-mark-article-as-read gnus-killed-mark))
10608               ((> unmark 0)
10609                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10610               (t
10611                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10612         (setq articles (cdr articles))))
10613     ;; Hide killed subtrees.
10614     (and (null unmark)
10615          gnus-thread-hide-killed
10616          (gnus-summary-hide-thread))
10617     ;; If marked as read, go to next unread subject.
10618     (when (null unmark)
10619       ;; Go to next unread subject.
10620       (gnus-summary-next-subject 1 t)))
10621   (gnus-set-mode-line 'summary))
10622
10623 ;; Summary sorting commands
10624
10625 (defun gnus-summary-sort-by-number (&optional reverse)
10626   "Sort the summary buffer by article number.
10627 Argument REVERSE means reverse order."
10628   (interactive "P")
10629   (gnus-summary-sort 'number reverse))
10630
10631 (defun gnus-summary-sort-by-random (&optional reverse)
10632   "Randomize the order in the summary buffer.
10633 Argument REVERSE means to randomize in reverse order."
10634   (interactive "P")
10635   (gnus-summary-sort 'random reverse))
10636
10637 (defun gnus-summary-sort-by-author (&optional reverse)
10638   "Sort the summary buffer by author name alphabetically.
10639 If `case-fold-search' is non-nil, case of letters is ignored.
10640 Argument REVERSE means reverse order."
10641   (interactive "P")
10642   (gnus-summary-sort 'author reverse))
10643
10644 (defun gnus-summary-sort-by-subject (&optional reverse)
10645   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10646 If `case-fold-search' is non-nil, case of letters is ignored.
10647 Argument REVERSE means reverse order."
10648   (interactive "P")
10649   (gnus-summary-sort 'subject reverse))
10650
10651 (defun gnus-summary-sort-by-date (&optional reverse)
10652   "Sort the summary buffer by date.
10653 Argument REVERSE means reverse order."
10654   (interactive "P")
10655   (gnus-summary-sort 'date reverse))
10656
10657 (defun gnus-summary-sort-by-score (&optional reverse)
10658   "Sort the summary buffer by score.
10659 Argument REVERSE means reverse order."
10660   (interactive "P")
10661   (gnus-summary-sort 'score reverse))
10662
10663 (defun gnus-summary-sort-by-lines (&optional reverse)
10664   "Sort the summary buffer by the number of lines.
10665 Argument REVERSE means reverse order."
10666   (interactive "P")
10667   (gnus-summary-sort 'lines reverse))
10668
10669 (defun gnus-summary-sort-by-chars (&optional reverse)
10670   "Sort the summary buffer by article length.
10671 Argument REVERSE means reverse order."
10672   (interactive "P")
10673   (gnus-summary-sort 'chars reverse))
10674
10675 (defun gnus-summary-sort-by-original (&optional reverse)
10676   "Sort the summary buffer using the default sorting method.
10677 Argument REVERSE means reverse order."
10678   (interactive "P")
10679   (let* ((buffer-read-only)
10680          (gnus-summary-prepare-hook nil))
10681     ;; We do the sorting by regenerating the threads.
10682     (gnus-summary-prepare)
10683     ;; Hide subthreads if needed.
10684     (gnus-summary-maybe-hide-threads)))
10685
10686 (defun gnus-summary-sort (predicate reverse)
10687   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10688   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10689          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10690          (gnus-thread-sort-functions
10691           (if (not reverse)
10692               thread
10693             `(lambda (t1 t2)
10694                (,thread t2 t1))))
10695          (gnus-sort-gathered-threads-function
10696           gnus-thread-sort-functions)
10697          (gnus-article-sort-functions
10698           (if (not reverse)
10699               article
10700             `(lambda (t1 t2)
10701                (,article t2 t1))))
10702          (buffer-read-only)
10703          (gnus-summary-prepare-hook nil))
10704     ;; We do the sorting by regenerating the threads.
10705     (gnus-summary-prepare)
10706     ;; Hide subthreads if needed.
10707     (gnus-summary-maybe-hide-threads)))
10708
10709 ;; Summary saving commands.
10710
10711 (defun gnus-summary-save-article (&optional n not-saved)
10712   "Save the current article using the default saver function.
10713 If N is a positive number, save the N next articles.
10714 If N is a negative number, save the N previous articles.
10715 If N is nil and any articles have been marked with the process mark,
10716 save those articles instead.
10717 The variable `gnus-default-article-saver' specifies the saver function."
10718   (interactive "P")
10719   (let* ((articles (gnus-summary-work-articles n))
10720          (save-buffer (save-excursion
10721                         (nnheader-set-temp-buffer " *Gnus Save*")))
10722          (num (length articles))
10723          header file)
10724     (dolist (article articles)
10725       (setq header (gnus-summary-article-header article))
10726       (if (not (vectorp header))
10727           ;; This is a pseudo-article.
10728           (if (assq 'name header)
10729               (gnus-copy-file (cdr (assq 'name header)))
10730             (gnus-message 1 "Article %d is unsaveable" article))
10731         ;; This is a real article.
10732         (save-window-excursion
10733           (let ((gnus-display-mime-function nil)
10734                 (gnus-article-prepare-hook nil))
10735             (gnus-summary-select-article t nil nil article)))
10736         (save-excursion
10737           (set-buffer save-buffer)
10738           (erase-buffer)
10739           (insert-buffer-substring gnus-original-article-buffer))
10740         (setq file (gnus-article-save save-buffer file num))
10741         (gnus-summary-remove-process-mark article)
10742         (unless not-saved
10743           (gnus-summary-set-saved-mark article))))
10744     (gnus-kill-buffer save-buffer)
10745     (gnus-summary-position-point)
10746     (gnus-set-mode-line 'summary)
10747     n))
10748
10749 (defun gnus-summary-pipe-output (&optional arg headers)
10750   "Pipe the current article to a subprocess.
10751 If N is a positive number, pipe the N next articles.
10752 If N is a negative number, pipe the N previous articles.
10753 If N is nil and any articles have been marked with the process mark,
10754 pipe those articles instead.
10755 If HEADERS (the symbolic prefix), include the headers, too."
10756   (interactive (gnus-interactive "P\ny"))
10757   (require 'gnus-art)
10758   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10759         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10760     (gnus-summary-save-article arg t))
10761   (let ((buffer (get-buffer "*Shell Command Output*")))
10762     (when (and buffer
10763                (not (zerop (buffer-size buffer))))
10764       (gnus-configure-windows 'pipe))))
10765
10766 (defun gnus-summary-save-article-mail (&optional arg)
10767   "Append the current article to an mail file.
10768 If N is a positive number, save the N next articles.
10769 If N is a negative number, save the N previous articles.
10770 If N is nil and any articles have been marked with the process mark,
10771 save those articles instead."
10772   (interactive "P")
10773   (require 'gnus-art)
10774   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10775     (gnus-summary-save-article arg)))
10776
10777 (defun gnus-summary-save-article-rmail (&optional arg)
10778   "Append the current article to an rmail file.
10779 If N is a positive number, save the N next articles.
10780 If N is a negative number, save the N previous articles.
10781 If N is nil and any articles have been marked with the process mark,
10782 save those articles instead."
10783   (interactive "P")
10784   (require 'gnus-art)
10785   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10786     (gnus-summary-save-article arg)))
10787
10788 (defun gnus-summary-save-article-file (&optional arg)
10789   "Append the current article to a file.
10790 If N is a positive number, save the N next articles.
10791 If N is a negative number, save the N previous articles.
10792 If N is nil and any articles have been marked with the process mark,
10793 save those articles instead."
10794   (interactive "P")
10795   (require 'gnus-art)
10796   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10797     (gnus-summary-save-article arg)))
10798
10799 (defun gnus-summary-write-article-file (&optional arg)
10800   "Write the current article to a file, deleting the previous file.
10801 If N is a positive number, save the N next articles.
10802 If N is a negative number, save the N previous articles.
10803 If N is nil and any articles have been marked with the process mark,
10804 save those articles instead."
10805   (interactive "P")
10806   (require 'gnus-art)
10807   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10808     (gnus-summary-save-article arg)))
10809
10810 (defun gnus-summary-save-article-body-file (&optional arg)
10811   "Append the current article body to a file.
10812 If N is a positive number, save the N next articles.
10813 If N is a negative number, save the N previous articles.
10814 If N is nil and any articles have been marked with the process mark,
10815 save those articles instead."
10816   (interactive "P")
10817   (require 'gnus-art)
10818   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10819     (gnus-summary-save-article arg)))
10820
10821 (defun gnus-summary-muttprint (&optional arg)
10822   "Print the current article using Muttprint.
10823 If N is a positive number, save the N next articles.
10824 If N is a negative number, save the N previous articles.
10825 If N is nil and any articles have been marked with the process mark,
10826 save those articles instead."
10827   (interactive "P")
10828   (require 'gnus-art)
10829   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10830     (gnus-summary-save-article arg t)))
10831
10832 (defun gnus-summary-pipe-message (program)
10833   "Pipe the current article through PROGRAM."
10834   (interactive "sProgram: ")
10835   (gnus-summary-select-article)
10836   (let ((mail-header-separator ""))
10837     (gnus-eval-in-buffer-window gnus-article-buffer
10838       (save-restriction
10839         (widen)
10840         (let ((start (window-start))
10841               buffer-read-only)
10842           (message-pipe-buffer-body program)
10843           (set-window-start (get-buffer-window (current-buffer)) start))))))
10844
10845 (defun gnus-get-split-value (methods)
10846   "Return a value based on the split METHODS."
10847   (let (split-name method result match)
10848     (when methods
10849       (save-excursion
10850         (set-buffer gnus-original-article-buffer)
10851         (save-restriction
10852           (nnheader-narrow-to-headers)
10853           (while (and methods (not split-name))
10854             (goto-char (point-min))
10855             (setq method (pop methods))
10856             (setq match (car method))
10857             (when (cond
10858                    ((stringp match)
10859                     ;; Regular expression.
10860                     (ignore-errors
10861                       (re-search-forward match nil t)))
10862                    ((functionp match)
10863                     ;; Function.
10864                     (save-restriction
10865                       (widen)
10866                       (setq result (funcall match gnus-newsgroup-name))))
10867                    ((consp match)
10868                     ;; Form.
10869                     (save-restriction
10870                       (widen)
10871                       (setq result (eval match)))))
10872               (setq split-name (cdr method))
10873               (cond ((stringp result)
10874                      (push (expand-file-name
10875                             result gnus-article-save-directory)
10876                            split-name))
10877                     ((consp result)
10878                      (setq split-name (append result split-name)))))))))
10879     (nreverse split-name)))
10880
10881 (defun gnus-valid-move-group-p (group)
10882   (and (boundp group)
10883        (symbol-name group)
10884        (symbol-value group)
10885        (gnus-get-function (gnus-find-method-for-group
10886                            (symbol-name group)) 'request-accept-article t)))
10887
10888 (defun gnus-read-move-group-name (prompt default articles prefix)
10889   "Read a group name."
10890   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10891          (minibuffer-confirm-incomplete nil) ; XEmacs
10892          (prom
10893           (format "%s %s to:"
10894                   prompt
10895                   (if (> (length articles) 1)
10896                       (format "these %d articles" (length articles))
10897                     "this article")))
10898          (to-newsgroup
10899           (cond
10900            ((null split-name)
10901             (gnus-completing-read-with-default
10902              default prom
10903              gnus-active-hashtb
10904              'gnus-valid-move-group-p
10905              nil prefix
10906              'gnus-group-history))
10907            ((= 1 (length split-name))
10908             (gnus-completing-read-with-default
10909              (car split-name) prom
10910              gnus-active-hashtb
10911              'gnus-valid-move-group-p
10912              nil nil
10913              'gnus-group-history))
10914            (t
10915             (gnus-completing-read-with-default
10916              nil prom
10917              (mapcar (lambda (el) (list el))
10918                      (nreverse split-name))
10919              nil nil nil
10920              'gnus-group-history))))
10921          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10922     (when to-newsgroup
10923       (if (or (string= to-newsgroup "")
10924               (string= to-newsgroup prefix))
10925           (setq to-newsgroup default))
10926       (unless to-newsgroup
10927         (error "No group name entered"))
10928       (or (gnus-active to-newsgroup)
10929           (gnus-activate-group to-newsgroup nil nil to-method)
10930           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10931                                      to-newsgroup))
10932               (or (and (gnus-request-create-group to-newsgroup to-method)
10933                        (gnus-activate-group
10934                         to-newsgroup nil nil to-method)
10935                        (gnus-subscribe-group to-newsgroup))
10936                   (error "Couldn't create group %s" to-newsgroup)))
10937           (error "No such group: %s" to-newsgroup)))
10938     to-newsgroup))
10939
10940 (defun gnus-summary-save-parts (type dir n &optional reverse)
10941   "Save parts matching TYPE to DIR.
10942 If REVERSE, save parts that do not match TYPE."
10943   (interactive
10944    (list (read-string "Save parts of type: "
10945                       (or (car gnus-summary-save-parts-type-history)
10946                           gnus-summary-save-parts-default-mime)
10947                       'gnus-summary-save-parts-type-history)
10948          (setq gnus-summary-save-parts-last-directory
10949                (read-file-name "Save to directory: "
10950                                gnus-summary-save-parts-last-directory
10951                                nil t))
10952          current-prefix-arg))
10953   (gnus-summary-iterate n
10954     (let ((gnus-display-mime-function nil)
10955           (gnus-inhibit-treatment t))
10956       (gnus-summary-select-article))
10957     (save-excursion
10958       (set-buffer gnus-article-buffer)
10959       (let ((handles (or gnus-article-mime-handles
10960                          (mm-dissect-buffer nil gnus-article-loose-mime)
10961                          (and gnus-article-emulate-mime
10962                               (mm-uu-dissect)))))
10963         (when handles
10964           (gnus-summary-save-parts-1 type dir handles reverse)
10965           (unless gnus-article-mime-handles ;; Don't destroy this case.
10966             (mm-destroy-parts handles)))))))
10967
10968 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10969   (if (stringp (car handle))
10970       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10971               (cdr handle))
10972     (when (if reverse
10973               (not (string-match type (mm-handle-media-type handle)))
10974             (string-match type (mm-handle-media-type handle)))
10975       (let ((file (expand-file-name
10976                    (file-name-nondirectory
10977                     (or
10978                      (mail-content-type-get
10979                       (mm-handle-disposition handle) 'filename)
10980                      (concat gnus-newsgroup-name
10981                              "." (number-to-string
10982                                   (cdr gnus-article-current)))))
10983                    dir)))
10984         (unless (file-exists-p file)
10985           (mm-save-part-to-file handle file))))))
10986
10987 ;; Summary extract commands
10988
10989 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10990   (let ((buffer-read-only nil)
10991         (article (gnus-summary-article-number))
10992         after-article b e)
10993     (unless (gnus-summary-goto-subject article)
10994       (error "No such article: %d" article))
10995     (gnus-summary-position-point)
10996     ;; If all commands are to be bunched up on one line, we collect
10997     ;; them here.
10998     (unless gnus-view-pseudos-separately
10999       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11000             files action)
11001         (while ps
11002           (setq action (cdr (assq 'action (car ps))))
11003           (setq files (list (cdr (assq 'name (car ps)))))
11004           (while (and ps (cdr ps)
11005                       (string= (or action "1")
11006                                (or (cdr (assq 'action (cadr ps))) "2")))
11007             (push (cdr (assq 'name (cadr ps))) files)
11008             (setcdr ps (cddr ps)))
11009           (when files
11010             (when (not (string-match "%s" action))
11011               (push " " files))
11012             (push " " files)
11013             (when (assq 'execute (car ps))
11014               (setcdr (assq 'execute (car ps))
11015                       (funcall (if (string-match "%s" action)
11016                                    'format 'concat)
11017                                action
11018                                (mapconcat
11019                                 (lambda (f)
11020                                   (if (equal f " ")
11021                                       f
11022                                     (mm-quote-arg f)))
11023                                 files " ")))))
11024           (setq ps (cdr ps)))))
11025     (if (and gnus-view-pseudos (not not-view))
11026         (while pslist
11027           (when (assq 'execute (car pslist))
11028             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11029                                   (eq gnus-view-pseudos 'not-confirm)))
11030           (setq pslist (cdr pslist)))
11031       (save-excursion
11032         (while pslist
11033           (setq after-article (or (cdr (assq 'article (car pslist)))
11034                                   (gnus-summary-article-number)))
11035           (gnus-summary-goto-subject after-article)
11036           (forward-line 1)
11037           (setq b (point))
11038           (insert "    " (file-name-nondirectory
11039                           (cdr (assq 'name (car pslist))))
11040                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11041           (setq e (point))
11042           (forward-line -1)             ; back to `b'
11043           (gnus-add-text-properties
11044            b (1- e) (list 'gnus-number gnus-reffed-article-number
11045                           gnus-mouse-face-prop gnus-mouse-face))
11046           (gnus-data-enter
11047            after-article gnus-reffed-article-number
11048            gnus-unread-mark b (car pslist) 0 (- e b))
11049           (setq gnus-newsgroup-unreads
11050                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11051                                          gnus-reffed-article-number))
11052           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11053           (setq pslist (cdr pslist)))))))
11054
11055 (defun gnus-pseudos< (p1 p2)
11056   (let ((c1 (cdr (assq 'action p1)))
11057         (c2 (cdr (assq 'action p2))))
11058     (and c1 c2 (string< c1 c2))))
11059
11060 (defun gnus-request-pseudo-article (props)
11061   (cond ((assq 'execute props)
11062          (gnus-execute-command (cdr (assq 'execute props)))))
11063   (let ((gnus-current-article (gnus-summary-article-number)))
11064     (gnus-run-hooks 'gnus-mark-article-hook)))
11065
11066 (defun gnus-execute-command (command &optional automatic)
11067   (save-excursion
11068     (gnus-article-setup-buffer)
11069     (set-buffer gnus-article-buffer)
11070     (setq buffer-read-only nil)
11071     (let ((command (if automatic command
11072                      (read-string "Command: " (cons command 0)))))
11073       (erase-buffer)
11074       (insert "$ " command "\n\n")
11075       (if gnus-view-pseudo-asynchronously
11076           (start-process "gnus-execute" (current-buffer) shell-file-name
11077                          shell-command-switch command)
11078         (call-process shell-file-name nil t nil
11079                       shell-command-switch command)))))
11080
11081 ;; Summary kill commands.
11082
11083 (defun gnus-summary-edit-global-kill (article)
11084   "Edit the \"global\" kill file."
11085   (interactive (list (gnus-summary-article-number)))
11086   (gnus-group-edit-global-kill article))
11087
11088 (defun gnus-summary-edit-local-kill ()
11089   "Edit a local kill file applied to the current newsgroup."
11090   (interactive)
11091   (setq gnus-current-headers (gnus-summary-article-header))
11092   (gnus-group-edit-local-kill
11093    (gnus-summary-article-number) gnus-newsgroup-name))
11094
11095 ;;; Header reading.
11096
11097 (defun gnus-read-header (id &optional header)
11098   "Read the headers of article ID and enter them into the Gnus system."
11099   (let ((group gnus-newsgroup-name)
11100         (gnus-override-method
11101          (or
11102           gnus-override-method
11103           (and (gnus-news-group-p gnus-newsgroup-name)
11104                (car (gnus-refer-article-methods)))))
11105         where)
11106     ;; First we check to see whether the header in question is already
11107     ;; fetched.
11108     (if (stringp id)
11109         ;; This is a Message-ID.
11110         (setq header (or header (gnus-id-to-header id)))
11111       ;; This is an article number.
11112       (setq header (or header (gnus-summary-article-header id))))
11113     (if (and header
11114              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11115         ;; We have found the header.
11116         header
11117       ;; If this is a sparse article, we have to nix out its
11118       ;; previous entry in the thread hashtb.
11119       (when (and header
11120                  (gnus-summary-article-sparse-p (mail-header-number header)))
11121         (let* ((parent (gnus-parent-id (mail-header-references header)))
11122                (thread (and parent (gnus-id-to-thread parent))))
11123           (when thread
11124             (delq (assq header thread) thread))))
11125       ;; We have to really fetch the header to this article.
11126       (save-excursion
11127         (set-buffer nntp-server-buffer)
11128         (when (setq where (gnus-request-head id group))
11129           (nnheader-fold-continuation-lines)
11130           (goto-char (point-max))
11131           (insert ".\n")
11132           (goto-char (point-min))
11133           (insert "211 ")
11134           (princ (cond
11135                   ((numberp id) id)
11136                   ((cdr where) (cdr where))
11137                   (header (mail-header-number header))
11138                   (t gnus-reffed-article-number))
11139                  (current-buffer))
11140           (insert " Article retrieved.\n"))
11141         (if (or (not where)
11142                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11143             ()                          ; Malformed head.
11144           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11145             (when (and (stringp id)
11146                        (not (string= (gnus-group-real-name group)
11147                                      (car where))))
11148               ;; If we fetched by Message-ID and the article came
11149               ;; from a different group, we fudge some bogus article
11150               ;; numbers for this article.
11151               (mail-header-set-number header gnus-reffed-article-number))
11152             (save-excursion
11153               (set-buffer gnus-summary-buffer)
11154               (decf gnus-reffed-article-number)
11155               (gnus-remove-header (mail-header-number header))
11156               (push header gnus-newsgroup-headers)
11157               (setq gnus-current-headers header)
11158               (push (mail-header-number header) gnus-newsgroup-limit)))
11159           header)))))
11160
11161 (defun gnus-remove-header (number)
11162   "Remove header NUMBER from `gnus-newsgroup-headers'."
11163   (if (and gnus-newsgroup-headers
11164            (= number (mail-header-number (car gnus-newsgroup-headers))))
11165       (pop gnus-newsgroup-headers)
11166     (let ((headers gnus-newsgroup-headers))
11167       (while (and (cdr headers)
11168                   (not (= number (mail-header-number (cadr headers)))))
11169         (pop headers))
11170       (when (cdr headers)
11171         (setcdr headers (cddr headers))))))
11172
11173 ;;;
11174 ;;; summary highlights
11175 ;;;
11176
11177 (defun gnus-highlight-selected-summary ()
11178   "Highlight selected article in summary buffer."
11179   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11180   (when gnus-summary-selected-face
11181     (save-excursion
11182       (let* ((beg (gnus-point-at-bol))
11183              (end (gnus-point-at-eol))
11184              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11185              (from (if (get-text-property beg gnus-mouse-face-prop)
11186                        beg
11187                      (or (next-single-property-change
11188                           beg gnus-mouse-face-prop nil end)
11189                          beg)))
11190              (to
11191               (if (= from end)
11192                   (- from 2)
11193                 (or (next-single-property-change
11194                      from gnus-mouse-face-prop nil end)
11195                     end))))
11196         ;; If no mouse-face prop on line we will have to = from = end,
11197         ;; so we highlight the entire line instead.
11198         (when (= (+ to 2) from)
11199           (setq from beg)
11200           (setq to end))
11201         (if gnus-newsgroup-selected-overlay
11202             ;; Move old overlay.
11203             (gnus-move-overlay
11204              gnus-newsgroup-selected-overlay from to (current-buffer))
11205           ;; Create new overlay.
11206           (gnus-overlay-put
11207            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11208            'face gnus-summary-selected-face))))))
11209
11210 (defvar gnus-summary-highlight-line-cached nil)
11211 (defvar gnus-summary-highlight-line-trigger nil)
11212
11213 (defun gnus-summary-highlight-line-0 ()
11214   (if (and (eq gnus-summary-highlight-line-trigger 
11215                gnus-summary-highlight)
11216            gnus-summary-highlight-line-cached)
11217       gnus-summary-highlight-line-cached
11218     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11219           gnus-summary-highlight-line-cached
11220           (let* ((cond (list 'cond))
11221                  (c cond)
11222                  (list gnus-summary-highlight))
11223             (while list
11224               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11225                               nil))
11226               (setq c (cdr c)
11227                     list (cdr list)))
11228             (gnus-byte-compile (list 'lambda nil cond))))))
11229
11230 (defun gnus-summary-highlight-line ()
11231   "Highlight current line according to `gnus-summary-highlight'."
11232   (let* ((beg (gnus-point-at-bol))
11233          (article (or (gnus-summary-article-number) gnus-current-article))
11234          (score (or (cdr (assq article
11235                                gnus-newsgroup-scored))
11236                     gnus-summary-default-score 0))
11237          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11238          (inhibit-read-only t)
11239          (default gnus-summary-default-score)
11240          (default-high gnus-summary-default-high-score)
11241          (default-low gnus-summary-default-low-score)
11242          (uncached (and gnus-summary-use-undownloaded-faces
11243                         (memq article gnus-newsgroup-undownloaded))))
11244     (let ((face (funcall (gnus-summary-highlight-line-0))))
11245       (unless (eq face (get-text-property beg 'face))
11246         (gnus-put-text-property-excluding-characters-with-faces
11247          beg (gnus-point-at-eol) 'face
11248          (setq face (if (boundp face) (symbol-value face) face)))
11249         (when gnus-summary-highlight-line-function
11250           (funcall gnus-summary-highlight-line-function article face))))))
11251
11252 (defun gnus-update-read-articles (group unread &optional compute)
11253   "Update the list of read articles in GROUP.
11254 UNREAD is a sorted list."
11255   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11256          (entry (gnus-gethash group gnus-newsrc-hashtb))
11257          (info (nth 2 entry))
11258          (prev 1)
11259          read)
11260     (if (or (not info) (not active))
11261         ;; There is no info on this group if it was, in fact,
11262         ;; killed.  Gnus stores no information on killed groups, so
11263         ;; there's nothing to be done.
11264         ;; One could store the information somewhere temporarily,
11265         ;; perhaps...  Hmmm...
11266         ()
11267       ;; Remove any negative articles numbers.
11268       (while (and unread (< (car unread) 0))
11269         (setq unread (cdr unread)))
11270       ;; Remove any expired article numbers
11271       (while (and unread (< (car unread) (car active)))
11272         (setq unread (cdr unread)))
11273       ;; Compute the ranges of read articles by looking at the list of
11274       ;; unread articles.
11275       (while unread
11276         (when (/= (car unread) prev)
11277           (push (if (= prev (1- (car unread))) prev
11278                   (cons prev (1- (car unread))))
11279                 read))
11280         (setq prev (1+ (car unread)))
11281         (setq unread (cdr unread)))
11282       (when (<= prev (cdr active))
11283         (push (cons prev (cdr active)) read))
11284       (setq read (if (> (length read) 1) (nreverse read) read))
11285       (if compute
11286           read
11287         (save-excursion
11288           (let (setmarkundo)
11289             ;; Propagate the read marks to the backend.
11290             (when (gnus-check-backend-function 'request-set-mark group)
11291               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11292                     (add (gnus-remove-from-range read (gnus-info-read info))))
11293                 (when (or add del)
11294                   (unless (gnus-check-group group)
11295                     (error "Can't open server for %s" group))
11296                   (gnus-request-set-mark
11297                    group (delq nil (list (if add (list add 'add '(read)))
11298                                          (if del (list del 'del '(read))))))
11299                   (setq setmarkundo
11300                         `(gnus-request-set-mark
11301                           ,group
11302                           ',(delq nil (list
11303                                        (if del (list del 'add '(read)))
11304                                        (if add (list add 'del '(read))))))))))
11305             (set-buffer gnus-group-buffer)
11306             (gnus-undo-register
11307               `(progn
11308                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11309                  (gnus-info-set-read ',info ',(gnus-info-read info))
11310                  (gnus-get-unread-articles-in-group ',info
11311                                                     (gnus-active ,group))
11312                  (gnus-group-update-group ,group t)
11313                  ,setmarkundo))))
11314         ;; Enter this list into the group info.
11315         (gnus-info-set-read info read)
11316         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11317         (gnus-get-unread-articles-in-group info (gnus-active group))
11318         t))))
11319
11320 (defun gnus-offer-save-summaries ()
11321   "Offer to save all active summary buffers."
11322   (let (buffers)
11323     ;; Go through all buffers and find all summaries.
11324     (dolist (buffer (buffer-list))
11325       (when (and (setq buffer (buffer-name buffer))
11326                  (string-match "Summary" buffer)
11327                  (save-excursion
11328                    (set-buffer buffer)
11329                    ;; We check that this is, indeed, a summary buffer.
11330                    (and (eq major-mode 'gnus-summary-mode)
11331                         ;; Also make sure this isn't bogus.
11332                         gnus-newsgroup-prepared
11333                         ;; Also make sure that this isn't a
11334                         ;; dead summary buffer.
11335                         (not gnus-dead-summary-mode))))
11336         (push buffer buffers)))
11337     ;; Go through all these summary buffers and offer to save them.
11338     (when buffers
11339       (save-excursion
11340         (map-y-or-n-p
11341          "Update summary buffer %s? "
11342          (lambda (buf)
11343            (switch-to-buffer buf)
11344            (gnus-summary-exit))
11345          buffers)))))
11346
11347 (defun gnus-summary-setup-default-charset ()
11348   "Setup newsgroup default charset."
11349   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11350       (setq gnus-newsgroup-charset nil)
11351     (let* ((ignored-charsets
11352             (or gnus-newsgroup-ephemeral-ignored-charsets
11353                 (append
11354                  (and gnus-newsgroup-name
11355                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11356                  gnus-newsgroup-ignored-charsets))))
11357       (setq gnus-newsgroup-charset
11358             (or gnus-newsgroup-ephemeral-charset
11359                 (and gnus-newsgroup-name
11360                      (gnus-parameter-charset gnus-newsgroup-name))
11361                 gnus-default-charset))
11362       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11363            ignored-charsets))))
11364
11365 ;;;
11366 ;;; Mime Commands
11367 ;;;
11368
11369 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11370   "Display the current article buffer fully MIME-buttonized.
11371 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11372 treated as multipart/mixed."
11373   (interactive "P")
11374   (require 'gnus-art)
11375   (let ((gnus-unbuttonized-mime-types nil)
11376         (gnus-mime-display-multipart-as-mixed show-all-parts))
11377     (gnus-summary-show-article)))
11378
11379 (defun gnus-summary-repair-multipart (article)
11380   "Add a Content-Type header to a multipart article without one."
11381   (interactive (list (gnus-summary-article-number)))
11382   (gnus-with-article article
11383     (message-narrow-to-head)
11384     (message-remove-header "Mime-Version")
11385     (goto-char (point-max))
11386     (insert "Mime-Version: 1.0\n")
11387     (widen)
11388     (when (search-forward "\n--" nil t)
11389       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11390         (message-narrow-to-head)
11391         (message-remove-header "Content-Type")
11392         (goto-char (point-max))
11393         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11394                         separator))
11395         (widen))))
11396   (let (gnus-mark-article-hook)
11397     (gnus-summary-select-article t t nil article)))
11398
11399 (defun gnus-summary-toggle-display-buttonized ()
11400   "Toggle the buttonizing of the article buffer."
11401   (interactive)
11402   (require 'gnus-art)
11403   (if (setq gnus-inhibit-mime-unbuttonizing
11404             (not gnus-inhibit-mime-unbuttonizing))
11405       (let ((gnus-unbuttonized-mime-types nil))
11406         (gnus-summary-show-article))
11407     (gnus-summary-show-article)))
11408
11409 ;;;
11410 ;;; Generic summary marking commands
11411 ;;;
11412
11413 (defvar gnus-summary-marking-alist
11414   '((read gnus-del-mark "d")
11415     (unread gnus-unread-mark "u")
11416     (ticked gnus-ticked-mark "!")
11417     (dormant gnus-dormant-mark "?")
11418     (expirable gnus-expirable-mark "e"))
11419   "An alist of names/marks/keystrokes.")
11420
11421 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11422 (defvar gnus-summary-mark-map)
11423
11424 (defun gnus-summary-make-all-marking-commands ()
11425   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11426   (dolist (elem gnus-summary-marking-alist)
11427     (apply 'gnus-summary-make-marking-command elem)))
11428
11429 (defun gnus-summary-make-marking-command (name mark keystroke)
11430   (let ((map (make-sparse-keymap)))
11431     (define-key gnus-summary-generic-mark-map keystroke map)
11432     (dolist (lway `((next "next" next nil "n")
11433                     (next-unread "next unread" next t "N")
11434                     (prev "previous" prev nil "p")
11435                     (prev-unread "previous unread" prev t "P")
11436                     (nomove "" nil nil ,keystroke)))
11437       (let ((func (gnus-summary-make-marking-command-1
11438                    mark (car lway) lway name)))
11439         (setq func (eval func))
11440         (define-key map (nth 4 lway) func)))))
11441
11442 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11443   `(defun ,(intern
11444             (format "gnus-summary-put-mark-as-%s%s"
11445                     name (if (eq way 'nomove)
11446                              ""
11447                            (concat "-" (symbol-name way)))))
11448      (n)
11449      ,(format
11450        "Mark the current article as %s%s.
11451 If N, the prefix, then repeat N times.
11452 If N is negative, move in reverse order.
11453 The difference between N and the actual number of articles marked is
11454 returned."
11455        name (cadr lway))
11456      (interactive "p")
11457      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11458
11459 (defun gnus-summary-generic-mark (n mark move unread)
11460   "Mark N articles with MARK."
11461   (unless (eq major-mode 'gnus-summary-mode)
11462     (error "This command can only be used in the summary buffer"))
11463   (gnus-summary-show-thread)
11464   (let ((nummove
11465          (cond
11466           ((eq move 'next) 1)
11467           ((eq move 'prev) -1)
11468           (t 0))))
11469     (if (zerop nummove)
11470         (setq n 1)
11471       (when (< n 0)
11472         (setq n (abs n)
11473               nummove (* -1 nummove))))
11474     (while (and (> n 0)
11475                 (gnus-summary-mark-article nil mark)
11476                 (zerop (gnus-summary-next-subject nummove unread t)))
11477       (setq n (1- n)))
11478     (when (/= 0 n)
11479       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11480     (gnus-summary-recenter)
11481     (gnus-summary-position-point)
11482     (gnus-set-mode-line 'summary)
11483     n))
11484
11485 (defun gnus-summary-insert-articles (articles)
11486   (when (setq articles
11487               (gnus-sorted-difference articles
11488                                       (mapcar (lambda (h)
11489                                                 (mail-header-number h))
11490                                               gnus-newsgroup-headers)))
11491     (setq gnus-newsgroup-headers
11492           (gnus-merge 'list
11493                       gnus-newsgroup-headers
11494                       (gnus-fetch-headers articles)
11495                       'gnus-article-sort-by-number))
11496     ;; Suppress duplicates?
11497     (when gnus-suppress-duplicates
11498       (gnus-dup-suppress-articles))
11499
11500     ;; We might want to build some more threads first.
11501     (when (and gnus-fetch-old-headers
11502                (eq gnus-headers-retrieved-by 'nov))
11503       (if (eq gnus-fetch-old-headers 'invisible)
11504           (gnus-build-all-threads)
11505         (gnus-build-old-threads)))
11506     ;; Let the Gnus agent mark articles as read.
11507     (when gnus-agent
11508       (gnus-agent-get-undownloaded-list))
11509     ;; Remove list identifiers from subject
11510     (when gnus-list-identifiers
11511       (gnus-summary-remove-list-identifiers))
11512     ;; First and last article in this newsgroup.
11513     (when gnus-newsgroup-headers
11514       (setq gnus-newsgroup-begin
11515             (mail-header-number (car gnus-newsgroup-headers))
11516             gnus-newsgroup-end
11517             (mail-header-number
11518              (gnus-last-element gnus-newsgroup-headers))))
11519     (when gnus-use-scoring
11520       (gnus-possibly-score-headers))))
11521
11522 (defun gnus-summary-insert-old-articles (&optional all)
11523   "Insert all old articles in this group.
11524 If ALL is non-nil, already read articles become readable.
11525 If ALL is a number, fetch this number of articles."
11526   (interactive "P")
11527   (prog1
11528       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11529             older len)
11530         (setq older
11531               ;; Some nntp servers lie about their active range.  When
11532               ;; this happens, the active range can be in the millions.
11533               ;; Use a compressed range to avoid creating a huge list.
11534               (gnus-range-difference (list gnus-newsgroup-active) old))
11535         (setq len (gnus-range-length older))
11536         (cond
11537          ((null older) nil)
11538          ((numberp all)
11539           (if (< all len)
11540               (let ((older-range (nreverse older)))
11541                 (setq older nil)
11542
11543                 (while (> all 0)
11544                   (let* ((r (pop older-range))
11545                          (min (if (numberp r) r (car r)))
11546                          (max (if (numberp r) r (cdr r))))
11547                     (while (and (<= min max)
11548                                 (> all 0))
11549                       (push max older)
11550                       (setq all (1- all)
11551                             max (1- max))))))
11552             (setq older (gnus-uncompress-range older))))
11553          (all
11554           (setq older (gnus-uncompress-range older)))
11555          (t
11556           (when (and (numberp gnus-large-newsgroup)
11557                    (> len gnus-large-newsgroup))
11558               (let* ((cursor-in-echo-area nil)
11559                      (initial (gnus-parameter-large-newsgroup-initial
11560                                gnus-newsgroup-name))
11561                      (input
11562                       (read-string
11563                        (format
11564                         "How many articles from %s (%s %d): "
11565                         (gnus-limit-string
11566                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11567                         (if initial "max" "default")
11568                         len)
11569                        (if initial
11570                            (cons (number-to-string initial)
11571                                  0)))))
11572                 (unless (string-match "^[ \t]*$" input)
11573                   (setq all (string-to-number input))
11574                   (if (< all len)
11575                       (let ((older-range (nreverse older)))
11576                         (setq older nil)
11577
11578                         (while (> all 0)
11579                           (let* ((r (pop older-range))
11580                                  (min (if (numberp r) r (car r)))
11581                                  (max (if (numberp r) r (cdr r))))
11582                             (while (and (<= min max)
11583                                         (> all 0))
11584                               (push max older)
11585                               (setq all (1- all)
11586                                     max (1- max))))))))))
11587           (setq older (gnus-uncompress-range older))))
11588         (if (not older)
11589             (message "No old news.")
11590           (gnus-summary-insert-articles older)
11591           (gnus-summary-limit (gnus-sorted-nunion old older))))
11592     (gnus-summary-position-point)))
11593
11594 (defun gnus-summary-insert-new-articles ()
11595   "Insert all new articles in this group."
11596   (interactive)
11597   (prog1
11598       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11599             (old-active gnus-newsgroup-active)
11600             (nnmail-fetched-sources (list t))
11601             i new)
11602         (setq gnus-newsgroup-active
11603               (gnus-activate-group gnus-newsgroup-name 'scan))
11604         (setq i (cdr gnus-newsgroup-active))
11605         (while (> i (cdr old-active))
11606           (push i new)
11607           (decf i))
11608         (if (not new)
11609             (message "No gnus is bad news.")
11610           (gnus-summary-insert-articles new)
11611           (setq gnus-newsgroup-unreads
11612                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11613           (gnus-summary-limit (gnus-sorted-nunion old new))))
11614     (gnus-summary-position-point)))
11615
11616 (gnus-summary-make-all-marking-commands)
11617
11618 (gnus-ems-redefine)
11619
11620 (provide 'gnus-sum)
11621
11622 (run-hooks 'gnus-sum-load-hook)
11623
11624 ;; Local Variables:
11625 ;; coding: iso-8859-1
11626 ;; End:
11627
11628 ;;; gnus-sum.el ends here