(gnus-thread-latest-date): Move condition-case to
[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 back ends 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                ["Pipe part" gnus-article-pipe-part t]
2110                ["Inline part" gnus-article-inline-part t]
2111                ["Encrypt body" gnus-article-encrypt-body
2112                 :active (not (gnus-group-read-only-p))
2113                ,@(if (featurep 'xemacs) nil
2114                    '(:help "Encrypt the message body on disk"))]
2115                ["View part externally" gnus-article-view-part-externally t]
2116                ["View part with charset" gnus-article-view-part-as-charset t]
2117                ["Copy part" gnus-article-copy-part t]
2118                ["Save part" gnus-article-save-part t]
2119                ["View part" gnus-article-view-part t]))
2120              ("Date"
2121               ["Local" gnus-article-date-local t]
2122               ["ISO8601" gnus-article-date-iso8601 t]
2123               ["UT" gnus-article-date-ut t]
2124               ["Original" gnus-article-date-original t]
2125               ["Lapsed" gnus-article-date-lapsed t]
2126               ["User-defined" gnus-article-date-user t])
2127              ("Display"
2128               ["Remove images" gnus-article-remove-images t]
2129               ["Toggle smiley" gnus-treat-smiley t]
2130               ["Show X-Face" gnus-article-display-x-face t]
2131               ["Show picons in From" gnus-treat-from-picon t]
2132               ["Show picons in mail headers" gnus-treat-mail-picon t]
2133               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2134               ("View as different encoding"
2135                ,@(gnus-summary-menu-split
2136                   (mapcar
2137                    (lambda (cs)
2138                      ;; Since easymenu under Emacs doesn't allow
2139                      ;; lambda forms for menu commands, we should
2140                      ;; provide intern'ed function symbols.
2141                      (let ((command (intern (format "\
2142 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2143                        (fset command
2144                              `(lambda ()
2145                                 (interactive)
2146                                 (let ((gnus-summary-show-article-charset-alist
2147                                        '((1 . ,cs))))
2148                                   (gnus-summary-show-article 1))))
2149                        `[,(symbol-name cs) ,command t]))
2150                    (sort (if (fboundp 'coding-system-list)
2151                              (coding-system-list)
2152                            (mapcar 'car mm-mime-mule-charset-alist))
2153                          'string<)))))
2154              ("Washing"
2155               ("Remove Blanks"
2156                ["Leading" gnus-article-strip-leading-blank-lines t]
2157                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2158                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2159                ["All of the above" gnus-article-strip-blank-lines t]
2160                ["All" gnus-article-strip-all-blank-lines t]
2161                ["Leading space" gnus-article-strip-leading-space t]
2162                ["Trailing space" gnus-article-strip-trailing-space t]
2163                ["Leading space in headers"
2164                 gnus-article-remove-leading-whitespace t])
2165               ["Overstrike" gnus-article-treat-overstrike t]
2166               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2167               ["Emphasis" gnus-article-emphasize t]
2168               ["Word wrap" gnus-article-fill-cited-article t]
2169               ["Fill long lines" gnus-article-fill-long-lines t]
2170               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2171               ["CR" gnus-article-remove-cr t]
2172               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2173               ["Base64" gnus-article-de-base64-unreadable t]
2174               ["Rot 13" gnus-summary-caesar-message
2175                ,@(if (featurep 'xemacs) '(t)
2176                    '(:help "\"Caesar rotate\" article by 13"))]
2177               ["Morse decode" gnus-summary-morse-message t]
2178               ["Unix pipe..." gnus-summary-pipe-message t]
2179               ["Add buttons" gnus-article-add-buttons t]
2180               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2181               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2182               ["Verbose header" gnus-summary-verbose-headers t]
2183               ["Toggle header" gnus-summary-toggle-header t]
2184               ["Unfold headers" gnus-article-treat-unfold-headers t]
2185               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2186               ["Html" gnus-article-wash-html t]
2187               ["URLs" gnus-article-unsplit-urls t]
2188               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2189               ["HZ" gnus-article-decode-HZ t]
2190               ("(Outlook) Deuglify"
2191                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2192                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2193                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2194                ["Full (Outlook) deuglify"
2195                 gnus-article-outlook-deuglify-article t])
2196               )
2197              ("Output"
2198               ["Save in default format" gnus-summary-save-article
2199                ,@(if (featurep 'xemacs) '(t)
2200                    '(:help "Save article using default method"))]
2201               ["Save in file" gnus-summary-save-article-file
2202                ,@(if (featurep 'xemacs) '(t)
2203                    '(:help "Save article in file"))]
2204               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2205               ["Save in MH folder" gnus-summary-save-article-folder t]
2206               ["Save in VM folder" gnus-summary-save-article-vm t]
2207               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2208               ["Save body in file" gnus-summary-save-article-body-file t]
2209               ["Pipe through a filter" gnus-summary-pipe-output t]
2210               ["Add to SOUP packet" gnus-soup-add-article t]
2211               ["Print with Muttprint" gnus-summary-muttprint t]
2212               ["Print" gnus-summary-print-article t])
2213              ("Backend"
2214               ["Respool article..." gnus-summary-respool-article t]
2215               ["Move article..." gnus-summary-move-article
2216                (gnus-check-backend-function
2217                 'request-move-article gnus-newsgroup-name)]
2218               ["Copy article..." gnus-summary-copy-article t]
2219               ["Crosspost article..." gnus-summary-crosspost-article
2220                (gnus-check-backend-function
2221                 'request-replace-article gnus-newsgroup-name)]
2222               ["Import file..." gnus-summary-import-article
2223                (gnus-check-backend-function
2224                 'request-accept-article gnus-newsgroup-name)]
2225               ["Create article..." gnus-summary-create-article
2226                (gnus-check-backend-function
2227                 'request-accept-article gnus-newsgroup-name)]
2228               ["Check if posted" gnus-summary-article-posted-p t]
2229               ["Edit article" gnus-summary-edit-article
2230                (not (gnus-group-read-only-p))]
2231               ["Delete article" gnus-summary-delete-article
2232                (gnus-check-backend-function
2233                 'request-expire-articles gnus-newsgroup-name)]
2234               ["Query respool" gnus-summary-respool-query t]
2235               ["Trace respool" gnus-summary-respool-trace t]
2236               ["Delete expirable articles" gnus-summary-expire-articles-now
2237                (gnus-check-backend-function
2238                 'request-expire-articles gnus-newsgroup-name)])
2239              ("Extract"
2240               ["Uudecode" gnus-uu-decode-uu
2241                ,@(if (featurep 'xemacs) '(t)
2242                    '(:help "Decode uuencoded article(s)"))]
2243               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2244               ["Unshar" gnus-uu-decode-unshar t]
2245               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2246               ["Save" gnus-uu-decode-save t]
2247               ["Binhex" gnus-uu-decode-binhex t]
2248               ["Postscript" gnus-uu-decode-postscript t]
2249               ["All MIME parts" gnus-summary-save-parts t])
2250              ("Cache"
2251               ["Enter article" gnus-cache-enter-article t]
2252               ["Remove article" gnus-cache-remove-article t])
2253              ["Translate" gnus-article-babel t]
2254              ["Select article buffer" gnus-summary-select-article-buffer t]
2255              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2256              ["Isearch article..." gnus-summary-isearch-article t]
2257              ["Beginning of the article" gnus-summary-beginning-of-article t]
2258              ["End of the article" gnus-summary-end-of-article t]
2259              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2260              ["Fetch referenced articles" gnus-summary-refer-references t]
2261              ["Fetch current thread" gnus-summary-refer-thread t]
2262              ["Fetch article with id..." gnus-summary-refer-article t]
2263              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2264              ["Redisplay" gnus-summary-show-article t]
2265              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2266       (easy-menu-define
2267         gnus-summary-article-menu gnus-summary-mode-map ""
2268         (cons "Article" innards))
2269
2270       (if (not (keymapp gnus-summary-article-menu))
2271           (easy-menu-define
2272             gnus-article-commands-menu gnus-article-mode-map ""
2273             (cons "Commands" innards))
2274         ;; in Emacs, don't share menu.
2275         (setq gnus-article-commands-menu
2276               (copy-keymap gnus-summary-article-menu))
2277         (define-key gnus-article-mode-map [menu-bar commands]
2278           (cons "Commands" gnus-article-commands-menu))))
2279
2280     (easy-menu-define
2281       gnus-summary-thread-menu gnus-summary-mode-map ""
2282       '("Threads"
2283         ["Find all messages in thread" gnus-summary-refer-thread t]
2284         ["Toggle threading" gnus-summary-toggle-threads t]
2285         ["Hide threads" gnus-summary-hide-all-threads t]
2286         ["Show threads" gnus-summary-show-all-threads t]
2287         ["Hide thread" gnus-summary-hide-thread t]
2288         ["Show thread" gnus-summary-show-thread t]
2289         ["Go to next thread" gnus-summary-next-thread t]
2290         ["Go to previous thread" gnus-summary-prev-thread t]
2291         ["Go down thread" gnus-summary-down-thread t]
2292         ["Go up thread" gnus-summary-up-thread t]
2293         ["Top of thread" gnus-summary-top-thread t]
2294         ["Mark thread as read" gnus-summary-kill-thread t]
2295         ["Lower thread score" gnus-summary-lower-thread t]
2296         ["Raise thread score" gnus-summary-raise-thread t]
2297         ["Rethread current" gnus-summary-rethread-current t]))
2298
2299     (easy-menu-define
2300       gnus-summary-post-menu gnus-summary-mode-map ""
2301       `("Post"
2302         ["Send a message (mail or news)" gnus-summary-post-news
2303          ,@(if (featurep 'xemacs) '(t)
2304              '(:help "Post an article"))]
2305         ["Followup" gnus-summary-followup
2306          ,@(if (featurep 'xemacs) '(t)
2307              '(:help "Post followup to this article"))]
2308         ["Followup and yank" gnus-summary-followup-with-original
2309          ,@(if (featurep 'xemacs) '(t)
2310              '(:help "Post followup to this article, quoting its contents"))]
2311         ["Supersede article" gnus-summary-supersede-article t]
2312         ["Cancel article" gnus-summary-cancel-article
2313          ,@(if (featurep 'xemacs) '(t)
2314              '(:help "Cancel an article you posted"))]
2315         ["Reply" gnus-summary-reply t]
2316         ["Reply and yank" gnus-summary-reply-with-original t]
2317         ["Wide reply" gnus-summary-wide-reply t]
2318         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2319          ,@(if (featurep 'xemacs) '(t)
2320              '(:help "Mail a reply, quoting this article"))]
2321         ["Very wide reply" gnus-summary-very-wide-reply t]
2322         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2323          ,@(if (featurep 'xemacs) '(t)
2324              '(:help "Mail a very wide reply, quoting this article"))]
2325         ["Mail forward" gnus-summary-mail-forward t]
2326         ["Post forward" gnus-summary-post-forward t]
2327         ["Digest and mail" gnus-uu-digest-mail-forward t]
2328         ["Digest and post" gnus-uu-digest-post-forward t]
2329         ["Resend message" gnus-summary-resend-message t]
2330         ["Resend message edit" gnus-summary-resend-message-edit t]
2331         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2332         ["Send a mail" gnus-summary-mail-other-window t]
2333         ["Create a local message" gnus-summary-news-other-window t]
2334         ["Uuencode and post" gnus-uu-post-news
2335          ,@(if (featurep 'xemacs) '(t)
2336              '(:help "Post a uuencoded article"))]
2337         ["Followup via news" gnus-summary-followup-to-mail t]
2338         ["Followup via news and yank"
2339          gnus-summary-followup-to-mail-with-original t]
2340         ;;("Draft"
2341         ;;["Send" gnus-summary-send-draft t]
2342         ;;["Send bounced" gnus-resend-bounced-mail t])
2343         ))
2344
2345     (cond
2346      ((not (keymapp gnus-summary-post-menu))
2347       (setq gnus-article-post-menu gnus-summary-post-menu))
2348      ((not gnus-article-post-menu)
2349       ;; Don't share post menu.
2350       (setq gnus-article-post-menu
2351             (copy-keymap gnus-summary-post-menu))))
2352     (define-key gnus-article-mode-map [menu-bar post]
2353       (cons "Post" gnus-article-post-menu))
2354
2355     (easy-menu-define
2356       gnus-summary-misc-menu gnus-summary-mode-map ""
2357       `("Gnus"
2358         ("Mark Read"
2359          ["Mark as read" gnus-summary-mark-as-read-forward t]
2360          ["Mark same subject and select"
2361           gnus-summary-kill-same-subject-and-select t]
2362          ["Mark same subject" gnus-summary-kill-same-subject t]
2363          ["Catchup" gnus-summary-catchup
2364           ,@(if (featurep 'xemacs) '(t)
2365               '(:help "Mark unread articles in this group as read"))]
2366          ["Catchup all" gnus-summary-catchup-all t]
2367          ["Catchup to here" gnus-summary-catchup-to-here t]
2368          ["Catchup from here" gnus-summary-catchup-from-here t]
2369          ["Catchup region" gnus-summary-mark-region-as-read
2370           (gnus-mark-active-p)]
2371          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2372         ("Mark Various"
2373          ["Tick" gnus-summary-tick-article-forward t]
2374          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2375          ["Remove marks" gnus-summary-clear-mark-forward t]
2376          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2377          ["Set bookmark" gnus-summary-set-bookmark t]
2378          ["Remove bookmark" gnus-summary-remove-bookmark t])
2379         ("Limit to"
2380          ["Marks..." gnus-summary-limit-to-marks t]
2381          ["Subject..." gnus-summary-limit-to-subject t]
2382          ["Author..." gnus-summary-limit-to-author t]
2383          ["Age..." gnus-summary-limit-to-age t]
2384          ["Extra..." gnus-summary-limit-to-extra t]
2385          ["Score..." gnus-summary-limit-to-score t]
2386          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2387          ["Unread" gnus-summary-limit-to-unread t]
2388          ["Unseen" gnus-summary-limit-to-unseen t]
2389          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2390          ["Next articles" gnus-summary-limit-to-articles t]
2391          ["Pop limit" gnus-summary-pop-limit t]
2392          ["Show dormant" gnus-summary-limit-include-dormant t]
2393          ["Hide childless dormant"
2394           gnus-summary-limit-exclude-childless-dormant t]
2395          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2396          ["Hide marked" gnus-summary-limit-exclude-marks t]
2397          ["Show expunged" gnus-summary-limit-include-expunged t])
2398         ("Process Mark"
2399          ["Set mark" gnus-summary-mark-as-processable t]
2400          ["Remove mark" gnus-summary-unmark-as-processable t]
2401          ["Remove all marks" gnus-summary-unmark-all-processable t]
2402          ["Mark above" gnus-uu-mark-over t]
2403          ["Mark series" gnus-uu-mark-series t]
2404          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2405          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2406          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2407          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2408          ["Mark all" gnus-uu-mark-all t]
2409          ["Mark buffer" gnus-uu-mark-buffer t]
2410          ["Mark sparse" gnus-uu-mark-sparse t]
2411          ["Mark thread" gnus-uu-mark-thread t]
2412          ["Unmark thread" gnus-uu-unmark-thread t]
2413          ("Process Mark Sets"
2414           ["Kill" gnus-summary-kill-process-mark t]
2415           ["Yank" gnus-summary-yank-process-mark
2416            gnus-newsgroup-process-stack]
2417           ["Save" gnus-summary-save-process-mark t]
2418           ["Run command on marked..." gnus-summary-universal-argument t]))
2419         ("Scroll article"
2420          ["Page forward" gnus-summary-next-page
2421           ,@(if (featurep 'xemacs) '(t)
2422               '(:help "Show next page of article"))]
2423          ["Page backward" gnus-summary-prev-page
2424           ,@(if (featurep 'xemacs) '(t)
2425               '(:help "Show previous page of article"))]
2426          ["Line forward" gnus-summary-scroll-up t])
2427         ("Move"
2428          ["Next unread article" gnus-summary-next-unread-article t]
2429          ["Previous unread article" gnus-summary-prev-unread-article t]
2430          ["Next article" gnus-summary-next-article t]
2431          ["Previous article" gnus-summary-prev-article t]
2432          ["Next unread subject" gnus-summary-next-unread-subject t]
2433          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2434          ["Next article same subject" gnus-summary-next-same-subject t]
2435          ["Previous article same subject" gnus-summary-prev-same-subject t]
2436          ["First unread article" gnus-summary-first-unread-article t]
2437          ["Best unread article" gnus-summary-best-unread-article t]
2438          ["Go to subject number..." gnus-summary-goto-subject t]
2439          ["Go to article number..." gnus-summary-goto-article t]
2440          ["Go to the last article" gnus-summary-goto-last-article t]
2441          ["Pop article off history" gnus-summary-pop-article t])
2442         ("Sort"
2443          ["Sort by number" gnus-summary-sort-by-number t]
2444          ["Sort by author" gnus-summary-sort-by-author t]
2445          ["Sort by subject" gnus-summary-sort-by-subject t]
2446          ["Sort by date" gnus-summary-sort-by-date t]
2447          ["Sort by score" gnus-summary-sort-by-score t]
2448          ["Sort by lines" gnus-summary-sort-by-lines t]
2449          ["Sort by characters" gnus-summary-sort-by-chars t]
2450          ["Randomize" gnus-summary-sort-by-random t]
2451          ["Original sort" gnus-summary-sort-by-original t])
2452         ("Help"
2453          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2454          ["Describe group" gnus-summary-describe-group t]
2455          ["Fetch charter" gnus-group-fetch-charter
2456           ,@(if (featurep 'xemacs) nil
2457               '(:help "Display the charter of the current group"))]
2458          ["Fetch control message" gnus-group-fetch-control
2459           ,@(if (featurep 'xemacs) nil
2460               '(:help "Display the archived control message for the current group"))]
2461          ["Read manual" gnus-info-find-node t])
2462         ("Modes"
2463          ["Pick and read" gnus-pick-mode t]
2464          ["Binary" gnus-binary-mode t])
2465         ("Regeneration"
2466          ["Regenerate" gnus-summary-prepare t]
2467          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2468          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2469          ["Toggle threading" gnus-summary-toggle-threads t])
2470         ["See old articles" gnus-summary-insert-old-articles t]
2471         ["See new articles" gnus-summary-insert-new-articles t]
2472         ["Filter articles..." gnus-summary-execute-command t]
2473         ["Run command on articles..." gnus-summary-universal-argument t]
2474         ["Search articles forward..." gnus-summary-search-article-forward t]
2475         ["Search articles backward..." gnus-summary-search-article-backward t]
2476         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2477         ["Expand window" gnus-summary-expand-window t]
2478         ["Expire expirable articles" gnus-summary-expire-articles
2479          (gnus-check-backend-function
2480           'request-expire-articles gnus-newsgroup-name)]
2481         ["Edit local kill file" gnus-summary-edit-local-kill t]
2482         ["Edit main kill file" gnus-summary-edit-global-kill t]
2483         ["Edit group parameters" gnus-summary-edit-parameters t]
2484         ["Customize group parameters" gnus-summary-customize-parameters t]
2485         ["Send a bug report" gnus-bug t]
2486         ("Exit"
2487          ["Catchup and exit" gnus-summary-catchup-and-exit
2488           ,@(if (featurep 'xemacs) '(t)
2489               '(:help "Mark unread articles in this group as read, then exit"))]
2490          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2491          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2492          ["Exit group" gnus-summary-exit
2493           ,@(if (featurep 'xemacs) '(t)
2494               '(:help "Exit current group, return to group selection mode"))]
2495          ["Exit group without updating" gnus-summary-exit-no-update t]
2496          ["Exit and goto next group" gnus-summary-next-group t]
2497          ["Exit and goto prev group" gnus-summary-prev-group t]
2498          ["Reselect group" gnus-summary-reselect-current-group t]
2499          ["Rescan group" gnus-summary-rescan-group t]
2500          ["Update dribble" gnus-summary-save-newsrc t])))
2501
2502     (gnus-run-hooks 'gnus-summary-menu-hook)))
2503
2504 (defvar gnus-summary-tool-bar-map nil)
2505
2506 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2507 (defun gnus-summary-make-tool-bar ()
2508   (if (and (fboundp 'tool-bar-add-item-from-menu)
2509            (default-value 'tool-bar-mode)
2510            (not gnus-summary-tool-bar-map))
2511       (setq gnus-summary-tool-bar-map
2512             (let ((tool-bar-map (make-sparse-keymap))
2513                   (load-path (mm-image-load-path)))
2514               (tool-bar-add-item-from-menu
2515                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2516               (tool-bar-add-item-from-menu
2517                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2518               (tool-bar-add-item-from-menu
2519                'gnus-summary-post-news "post" gnus-summary-mode-map)
2520               (tool-bar-add-item-from-menu
2521                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2522               (tool-bar-add-item-from-menu
2523                'gnus-summary-followup "followup" gnus-summary-mode-map)
2524               (tool-bar-add-item-from-menu
2525                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2526               (tool-bar-add-item-from-menu
2527                'gnus-summary-reply "reply" gnus-summary-mode-map)
2528               (tool-bar-add-item-from-menu
2529                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2530               (tool-bar-add-item-from-menu
2531                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2532               (tool-bar-add-item-from-menu
2533                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2534               (tool-bar-add-item-from-menu
2535                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2536               (tool-bar-add-item-from-menu
2537                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2538               (tool-bar-add-item-from-menu
2539                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2540               (tool-bar-add-item-from-menu
2541                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2542               (tool-bar-add-item-from-menu
2543                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2544               tool-bar-map)))
2545   (if gnus-summary-tool-bar-map
2546       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2547
2548 (defun gnus-score-set-default (var value)
2549   "A version of set that updates the GNU Emacs menu-bar."
2550   (set var value)
2551   ;; It is the message that forces the active status to be updated.
2552   (message ""))
2553
2554 (defun gnus-make-score-map (type)
2555   "Make a summary score map of type TYPE."
2556   (if t
2557       nil
2558     (let ((headers '(("author" "from" string)
2559                      ("subject" "subject" string)
2560                      ("article body" "body" string)
2561                      ("article head" "head" string)
2562                      ("xref" "xref" string)
2563                      ("extra header" "extra" string)
2564                      ("lines" "lines" number)
2565                      ("followups to author" "followup" string)))
2566           (types '((number ("less than" <)
2567                            ("greater than" >)
2568                            ("equal" =))
2569                    (string ("substring" s)
2570                            ("exact string" e)
2571                            ("fuzzy string" f)
2572                            ("regexp" r))))
2573           (perms '(("temporary" (current-time-string))
2574                    ("permanent" nil)
2575                    ("immediate" now)))
2576           header)
2577       (list
2578        (apply
2579         'nconc
2580         (list
2581          (if (eq type 'lower)
2582              "Lower score"
2583            "Increase score"))
2584         (let (outh)
2585           (while headers
2586             (setq header (car headers))
2587             (setq outh
2588                   (cons
2589                    (apply
2590                     'nconc
2591                     (list (car header))
2592                     (let ((ts (cdr (assoc (nth 2 header) types)))
2593                           outt)
2594                       (while ts
2595                         (setq outt
2596                               (cons
2597                                (apply
2598                                 'nconc
2599                                 (list (caar ts))
2600                                 (let ((ps perms)
2601                                       outp)
2602                                   (while ps
2603                                     (setq outp
2604                                           (cons
2605                                            (vector
2606                                             (caar ps)
2607                                             (list
2608                                              'gnus-summary-score-entry
2609                                              (nth 1 header)
2610                                              (if (or (string= (nth 1 header)
2611                                                               "head")
2612                                                      (string= (nth 1 header)
2613                                                               "body"))
2614                                                  ""
2615                                                (list 'gnus-summary-header
2616                                                      (nth 1 header)))
2617                                              (list 'quote (nth 1 (car ts)))
2618                                              (list 'gnus-score-delta-default
2619                                                    nil)
2620                                              (nth 1 (car ps))
2621                                              t)
2622                                             t)
2623                                            outp))
2624                                     (setq ps (cdr ps)))
2625                                   (list (nreverse outp))))
2626                                outt))
2627                         (setq ts (cdr ts)))
2628                       (list (nreverse outt))))
2629                    outh))
2630             (setq headers (cdr headers)))
2631           (list (nreverse outh))))))))
2632
2633 \f
2634
2635 (defun gnus-summary-mode (&optional group)
2636   "Major mode for reading articles.
2637
2638 All normal editing commands are switched off.
2639 \\<gnus-summary-mode-map>
2640 Each line in this buffer represents one article.  To read an
2641 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2642 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2643 respectively.
2644
2645 You can also post articles and send mail from this buffer.  To
2646 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2647 of an article, type `\\[gnus-summary-reply]'.
2648
2649 There are approx. one gazillion commands you can execute in this
2650 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2651
2652 The following commands are available:
2653
2654 \\{gnus-summary-mode-map}"
2655   (interactive)
2656   (kill-all-local-variables)
2657   (when (gnus-visual-p 'summary-menu 'menu)
2658     (gnus-summary-make-menu-bar)
2659     (gnus-summary-make-tool-bar))
2660   (gnus-summary-make-local-variables)
2661   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2662     (gnus-summary-make-local-variables))
2663   (gnus-make-thread-indent-array)
2664   (gnus-simplify-mode-line)
2665   (setq major-mode 'gnus-summary-mode)
2666   (setq mode-name "Summary")
2667   (make-local-variable 'minor-mode-alist)
2668   (use-local-map gnus-summary-mode-map)
2669   (buffer-disable-undo)
2670   (setq buffer-read-only t)             ;Disable modification
2671   (setq truncate-lines t)
2672   (setq selective-display t)
2673   (setq selective-display-ellipses t)   ;Display `...'
2674   (gnus-summary-set-display-table)
2675   (gnus-set-default-directory)
2676   (setq gnus-newsgroup-name group)
2677   (make-local-variable 'gnus-summary-line-format)
2678   (make-local-variable 'gnus-summary-line-format-spec)
2679   (make-local-variable 'gnus-summary-dummy-line-format)
2680   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2681   (make-local-variable 'gnus-summary-mark-positions)
2682   (gnus-make-local-hook 'pre-command-hook)
2683   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2684   (gnus-run-hooks 'gnus-summary-mode-hook)
2685   (turn-on-gnus-mailing-list-mode)
2686   (mm-enable-multibyte)
2687   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2688   (gnus-update-summary-mark-positions))
2689
2690 (defun gnus-summary-make-local-variables ()
2691   "Make all the local summary buffer variables."
2692   (let (global)
2693     (dolist (local gnus-summary-local-variables)
2694       (if (consp local)
2695           (progn
2696             (if (eq (cdr local) 'global)
2697                 ;; Copy the global value of the variable.
2698                 (setq global (symbol-value (car local)))
2699               ;; Use the value from the list.
2700               (setq global (eval (cdr local))))
2701             (set (make-local-variable (car local)) global))
2702         ;; Simple nil-valued local variable.
2703         (set (make-local-variable local) nil)))))
2704
2705 (defun gnus-summary-clear-local-variables ()
2706   (let ((locals gnus-summary-local-variables))
2707     (while locals
2708       (if (consp (car locals))
2709           (and (vectorp (caar locals))
2710                (set (caar locals) nil))
2711         (and (vectorp (car locals))
2712              (set (car locals) nil)))
2713       (setq locals (cdr locals)))))
2714
2715 ;; Summary data functions.
2716
2717 (defmacro gnus-data-number (data)
2718   `(car ,data))
2719
2720 (defmacro gnus-data-set-number (data number)
2721   `(setcar ,data ,number))
2722
2723 (defmacro gnus-data-mark (data)
2724   `(nth 1 ,data))
2725
2726 (defmacro gnus-data-set-mark (data mark)
2727   `(setcar (nthcdr 1 ,data) ,mark))
2728
2729 (defmacro gnus-data-pos (data)
2730   `(nth 2 ,data))
2731
2732 (defmacro gnus-data-set-pos (data pos)
2733   `(setcar (nthcdr 2 ,data) ,pos))
2734
2735 (defmacro gnus-data-header (data)
2736   `(nth 3 ,data))
2737
2738 (defmacro gnus-data-set-header (data header)
2739   `(setf (nth 3 ,data) ,header))
2740
2741 (defmacro gnus-data-level (data)
2742   `(nth 4 ,data))
2743
2744 (defmacro gnus-data-unread-p (data)
2745   `(= (nth 1 ,data) gnus-unread-mark))
2746
2747 (defmacro gnus-data-read-p (data)
2748   `(/= (nth 1 ,data) gnus-unread-mark))
2749
2750 (defmacro gnus-data-pseudo-p (data)
2751   `(consp (nth 3 ,data)))
2752
2753 (defmacro gnus-data-find (number)
2754   `(assq ,number gnus-newsgroup-data))
2755
2756 (defmacro gnus-data-find-list (number &optional data)
2757   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2758      (memq (assq ,number bdata)
2759            bdata)))
2760
2761 (defmacro gnus-data-make (number mark pos header level)
2762   `(list ,number ,mark ,pos ,header ,level))
2763
2764 (defun gnus-data-enter (after-article number mark pos header level offset)
2765   (let ((data (gnus-data-find-list after-article)))
2766     (unless data
2767       (error "No such article: %d" after-article))
2768     (setcdr data (cons (gnus-data-make number mark pos header level)
2769                        (cdr data)))
2770     (setq gnus-newsgroup-data-reverse nil)
2771     (gnus-data-update-list (cddr data) offset)))
2772
2773 (defun gnus-data-enter-list (after-article list &optional offset)
2774   (when list
2775     (let ((data (and after-article (gnus-data-find-list after-article)))
2776           (ilist list))
2777       (if (not (or data
2778                    after-article))
2779           (let ((odata gnus-newsgroup-data))
2780             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2781             (when offset
2782               (gnus-data-update-list odata offset)))
2783       ;; Find the last element in the list to be spliced into the main
2784         ;; list.
2785         (while (cdr list)
2786           (setq list (cdr list)))
2787         (if (not data)
2788             (progn
2789               (setcdr list gnus-newsgroup-data)
2790               (setq gnus-newsgroup-data ilist)
2791               (when offset
2792                 (gnus-data-update-list (cdr list) offset)))
2793           (setcdr list (cdr data))
2794           (setcdr data ilist)
2795           (when offset
2796             (gnus-data-update-list (cdr list) offset))))
2797       (setq gnus-newsgroup-data-reverse nil))))
2798
2799 (defun gnus-data-remove (article &optional offset)
2800   (let ((data gnus-newsgroup-data))
2801     (if (= (gnus-data-number (car data)) article)
2802         (progn
2803           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2804                 gnus-newsgroup-data-reverse nil)
2805           (when offset
2806             (gnus-data-update-list gnus-newsgroup-data offset)))
2807       (while (cdr data)
2808         (when (= (gnus-data-number (cadr data)) article)
2809           (setcdr data (cddr data))
2810           (when offset
2811             (gnus-data-update-list (cdr data) offset))
2812           (setq data nil
2813                 gnus-newsgroup-data-reverse nil))
2814         (setq data (cdr data))))))
2815
2816 (defmacro gnus-data-list (backward)
2817   `(if ,backward
2818        (or gnus-newsgroup-data-reverse
2819            (setq gnus-newsgroup-data-reverse
2820                  (reverse gnus-newsgroup-data)))
2821      gnus-newsgroup-data))
2822
2823 (defun gnus-data-update-list (data offset)
2824   "Add OFFSET to the POS of all data entries in DATA."
2825   (setq gnus-newsgroup-data-reverse nil)
2826   (while data
2827     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2828     (setq data (cdr data))))
2829
2830 (defun gnus-summary-article-pseudo-p (article)
2831   "Say whether this article is a pseudo article or not."
2832   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2833
2834 (defmacro gnus-summary-article-sparse-p (article)
2835   "Say whether this article is a sparse article or not."
2836   `(memq ,article gnus-newsgroup-sparse))
2837
2838 (defmacro gnus-summary-article-ancient-p (article)
2839   "Say whether this article is a sparse article or not."
2840   `(memq ,article gnus-newsgroup-ancient))
2841
2842 (defun gnus-article-parent-p (number)
2843   "Say whether this article is a parent or not."
2844   (let ((data (gnus-data-find-list number)))
2845     (and (cdr data)              ; There has to be an article after...
2846          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2847             (gnus-data-level (nth 1 data))))))
2848
2849 (defun gnus-article-children (number)
2850   "Return a list of all children to NUMBER."
2851   (let* ((data (gnus-data-find-list number))
2852          (level (gnus-data-level (car data)))
2853          children)
2854     (setq data (cdr data))
2855     (while (and data
2856                 (= (gnus-data-level (car data)) (1+ level)))
2857       (push (gnus-data-number (car data)) children)
2858       (setq data (cdr data)))
2859     children))
2860
2861 (defmacro gnus-summary-skip-intangible ()
2862   "If the current article is intangible, then jump to a different article."
2863   '(let ((to (get-text-property (point) 'gnus-intangible)))
2864      (and to (gnus-summary-goto-subject to))))
2865
2866 (defmacro gnus-summary-article-intangible-p ()
2867   "Say whether this article is intangible or not."
2868   '(get-text-property (point) 'gnus-intangible))
2869
2870 (defun gnus-article-read-p (article)
2871   "Say whether ARTICLE is read or not."
2872   (not (or (memq article gnus-newsgroup-marked)
2873            (memq article gnus-newsgroup-spam-marked)
2874            (memq article gnus-newsgroup-unreads)
2875            (memq article gnus-newsgroup-unselected)
2876            (memq article gnus-newsgroup-dormant))))
2877
2878 ;; Some summary mode macros.
2879
2880 (defmacro gnus-summary-article-number ()
2881   "The article number of the article on the current line.
2882 If there isn't an article number here, then we return the current
2883 article number."
2884   '(progn
2885      (gnus-summary-skip-intangible)
2886      (or (get-text-property (point) 'gnus-number)
2887          (gnus-summary-last-subject))))
2888
2889 (defmacro gnus-summary-article-header (&optional number)
2890   "Return the header of article NUMBER."
2891   `(gnus-data-header (gnus-data-find
2892                       ,(or number '(gnus-summary-article-number)))))
2893
2894 (defmacro gnus-summary-thread-level (&optional number)
2895   "Return the level of thread that starts with article NUMBER."
2896   `(if (and (eq gnus-summary-make-false-root 'dummy)
2897             (get-text-property (point) 'gnus-intangible))
2898        0
2899      (gnus-data-level (gnus-data-find
2900                        ,(or number '(gnus-summary-article-number))))))
2901
2902 (defmacro gnus-summary-article-mark (&optional number)
2903   "Return the mark of article NUMBER."
2904   `(gnus-data-mark (gnus-data-find
2905                     ,(or number '(gnus-summary-article-number)))))
2906
2907 (defmacro gnus-summary-article-pos (&optional number)
2908   "Return the position of the line of article NUMBER."
2909   `(gnus-data-pos (gnus-data-find
2910                    ,(or number '(gnus-summary-article-number)))))
2911
2912 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2913 (defmacro gnus-summary-article-subject (&optional number)
2914   "Return current subject string or nil if nothing."
2915   `(let ((headers
2916           ,(if number
2917                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2918              '(gnus-data-header (assq (gnus-summary-article-number)
2919                                       gnus-newsgroup-data)))))
2920      (and headers
2921           (vectorp headers)
2922           (mail-header-subject headers))))
2923
2924 (defmacro gnus-summary-article-score (&optional number)
2925   "Return current article score."
2926   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2927                   gnus-newsgroup-scored))
2928        gnus-summary-default-score 0))
2929
2930 (defun gnus-summary-article-children (&optional number)
2931   "Return a list of article numbers that are children of article NUMBER."
2932   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2933          (level (gnus-data-level (car data)))
2934          l children)
2935     (while (and (setq data (cdr data))
2936                 (> (setq l (gnus-data-level (car data))) level))
2937       (and (= (1+ level) l)
2938            (push (gnus-data-number (car data))
2939                  children)))
2940     (nreverse children)))
2941
2942 (defun gnus-summary-article-parent (&optional number)
2943   "Return the article number of the parent of article NUMBER."
2944   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2945                                     (gnus-data-list t)))
2946          (level (gnus-data-level (car data))))
2947     (if (zerop level)
2948         ()                              ; This is a root.
2949       ;; We search until we find an article with a level less than
2950       ;; this one.  That function has to be the parent.
2951       (while (and (setq data (cdr data))
2952                   (not (< (gnus-data-level (car data)) level))))
2953       (and data (gnus-data-number (car data))))))
2954
2955 (defun gnus-unread-mark-p (mark)
2956   "Say whether MARK is the unread mark."
2957   (= mark gnus-unread-mark))
2958
2959 (defun gnus-read-mark-p (mark)
2960   "Say whether MARK is one of the marks that mark as read.
2961 This is all marks except unread, ticked, dormant, and expirable."
2962   (not (or (= mark gnus-unread-mark)
2963            (= mark gnus-ticked-mark)
2964            (= mark gnus-spam-mark)
2965            (= mark gnus-dormant-mark)
2966            (= mark gnus-expirable-mark))))
2967
2968 (defmacro gnus-article-mark (number)
2969   "Return the MARK of article NUMBER.
2970 This macro should only be used when computing the mark the \"first\"
2971 time; i.e., when generating the summary lines.  After that,
2972 `gnus-summary-article-mark' should be used to examine the
2973 marks of articles."
2974   `(cond
2975     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2976     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2977     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2978     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2979     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2980     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2981     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2982     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2983            gnus-ancient-mark))))
2984
2985 ;; Saving hidden threads.
2986
2987 (defmacro gnus-save-hidden-threads (&rest forms)
2988   "Save hidden threads, eval FORMS, and restore the hidden threads."
2989   (let ((config (make-symbol "config")))
2990     `(let ((,config (gnus-hidden-threads-configuration)))
2991        (unwind-protect
2992            (save-excursion
2993              ,@forms)
2994          (gnus-restore-hidden-threads-configuration ,config)))))
2995 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2996 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2997
2998 (defun gnus-data-compute-positions ()
2999   "Compute the positions of all articles."
3000   (setq gnus-newsgroup-data-reverse nil)
3001   (let ((data gnus-newsgroup-data))
3002     (save-excursion
3003       (gnus-save-hidden-threads
3004         (gnus-summary-show-all-threads)
3005         (goto-char (point-min))
3006         (while data
3007           (while (get-text-property (point) 'gnus-intangible)
3008             (forward-line 1))
3009           (gnus-data-set-pos (car data) (+ (point) 3))
3010           (setq data (cdr data))
3011           (forward-line 1))))))
3012
3013 (defun gnus-hidden-threads-configuration ()
3014   "Return the current hidden threads configuration."
3015   (save-excursion
3016     (let (config)
3017       (goto-char (point-min))
3018       (while (search-forward "\r" nil t)
3019         (push (1- (point)) config))
3020       config)))
3021
3022 (defun gnus-restore-hidden-threads-configuration (config)
3023   "Restore hidden threads configuration from CONFIG."
3024   (save-excursion
3025     (let (point buffer-read-only)
3026       (while (setq point (pop config))
3027         (when (and (< point (point-max))
3028                    (goto-char point)
3029                    (eq (char-after) ?\n))
3030           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3031
3032 ;; Various summary mode internalish functions.
3033
3034 (defun gnus-mouse-pick-article (e)
3035   (interactive "e")
3036   (mouse-set-point e)
3037   (gnus-summary-next-page nil t))
3038
3039 (defun gnus-summary-set-display-table ()
3040   "Change the display table.
3041 Odd characters have a tendency to mess
3042 up nicely formatted displays - we make all possible glyphs
3043 display only a single character."
3044
3045   ;; We start from the standard display table, if any.
3046   (let ((table (or (copy-sequence standard-display-table)
3047                    (make-display-table)))
3048         (i 32))
3049     ;; Nix out all the control chars...
3050     (while (>= (setq i (1- i)) 0)
3051       (aset table i [??]))
3052    ;; ... but not newline and cr, of course.  (cr is necessary for the
3053     ;; selective display).
3054     (aset table ?\n nil)
3055     (aset table ?\r nil)
3056     ;; We keep TAB as well.
3057     (aset table ?\t nil)
3058     ;; We nix out any glyphs over 126 that are not set already.
3059     (let ((i 256))
3060       (while (>= (setq i (1- i)) 127)
3061         ;; Only modify if the entry is nil.
3062         (unless (aref table i)
3063           (aset table i [??]))))
3064     (setq buffer-display-table table)))
3065
3066 (defun gnus-summary-set-article-display-arrow (pos)
3067   "Update the overlay arrow to point to line at position POS."
3068   (when (and gnus-summary-display-arrow
3069              (boundp 'overlay-arrow-position)
3070              (boundp 'overlay-arrow-string))
3071     (save-excursion
3072       (goto-char pos)
3073       (beginning-of-line)
3074       (unless overlay-arrow-position
3075         (setq overlay-arrow-position (make-marker)))
3076       (setq overlay-arrow-string "=>"
3077             overlay-arrow-position (set-marker overlay-arrow-position
3078                                                (point)
3079                                                (current-buffer))))))
3080
3081 (defun gnus-summary-setup-buffer (group)
3082   "Initialize summary buffer."
3083   (let ((buffer (gnus-summary-buffer-name group))
3084         (dead-name (concat "*Dead Summary "
3085                            (gnus-group-decoded-name group) "*")))
3086     ;; If a dead summary buffer exists, we kill it.
3087     (when (gnus-buffer-live-p dead-name)
3088       (gnus-kill-buffer dead-name))
3089     (if (get-buffer buffer)
3090         (progn
3091           (set-buffer buffer)
3092           (setq gnus-summary-buffer (current-buffer))
3093           (not gnus-newsgroup-prepared))
3094       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3095       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3096       (gnus-summary-mode group)
3097       (when gnus-carpal
3098         (gnus-carpal-setup-buffer 'summary))
3099       (unless gnus-single-article-buffer
3100         (make-local-variable 'gnus-article-buffer)
3101         (make-local-variable 'gnus-article-current)
3102         (make-local-variable 'gnus-original-article-buffer))
3103       (setq gnus-newsgroup-name group)
3104       ;; Set any local variables in the group parameters.
3105       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3106       t)))
3107
3108 (defun gnus-set-global-variables ()
3109   "Set the global equivalents of the buffer-local variables.
3110 They are set to the latest values they had.  These reflect the summary
3111 buffer that was in action when the last article was fetched."
3112   (when (eq major-mode 'gnus-summary-mode)
3113     (setq gnus-summary-buffer (current-buffer))
3114     (let ((name gnus-newsgroup-name)
3115           (marked gnus-newsgroup-marked)
3116           (spam gnus-newsgroup-spam-marked)
3117           (unread gnus-newsgroup-unreads)
3118           (headers gnus-current-headers)
3119           (data gnus-newsgroup-data)
3120           (summary gnus-summary-buffer)
3121           (article-buffer gnus-article-buffer)
3122           (original gnus-original-article-buffer)
3123           (gac gnus-article-current)
3124           (reffed gnus-reffed-article-number)
3125           (score-file gnus-current-score-file)
3126           (default-charset gnus-newsgroup-charset)
3127           vlist)
3128       (let ((locals gnus-newsgroup-variables))
3129         (while locals
3130           (if (consp (car locals))
3131               (push (eval (caar locals)) vlist)
3132             (push (eval (car locals)) vlist))
3133           (setq locals (cdr locals)))
3134         (setq vlist (nreverse vlist)))
3135       (save-excursion
3136         (set-buffer gnus-group-buffer)
3137         (setq gnus-newsgroup-name name
3138               gnus-newsgroup-marked marked
3139               gnus-newsgroup-spam-marked spam
3140               gnus-newsgroup-unreads unread
3141               gnus-current-headers headers
3142               gnus-newsgroup-data data
3143               gnus-article-current gac
3144               gnus-summary-buffer summary
3145               gnus-article-buffer article-buffer
3146               gnus-original-article-buffer original
3147               gnus-reffed-article-number reffed
3148               gnus-current-score-file score-file
3149               gnus-newsgroup-charset default-charset)
3150         (let ((locals gnus-newsgroup-variables))
3151           (while locals
3152             (if (consp (car locals))
3153                 (set (caar locals) (pop vlist))
3154               (set (car locals) (pop vlist)))
3155             (setq locals (cdr locals))))
3156         ;; The article buffer also has local variables.
3157         (when (gnus-buffer-live-p gnus-article-buffer)
3158           (set-buffer gnus-article-buffer)
3159           (setq gnus-summary-buffer summary))))))
3160
3161 (defun gnus-summary-article-unread-p (article)
3162   "Say whether ARTICLE is unread or not."
3163   (memq article gnus-newsgroup-unreads))
3164
3165 (defun gnus-summary-first-article-p (&optional article)
3166   "Return whether ARTICLE is the first article in the buffer."
3167   (if (not (setq article (or article (gnus-summary-article-number))))
3168       nil
3169     (eq article (caar gnus-newsgroup-data))))
3170
3171 (defun gnus-summary-last-article-p (&optional article)
3172   "Return whether ARTICLE is the last article in the buffer."
3173   (if (not (setq article (or article (gnus-summary-article-number))))
3174       ;; All non-existent numbers are the last article.  :-)
3175       t
3176     (not (cdr (gnus-data-find-list article)))))
3177
3178 (defun gnus-make-thread-indent-array ()
3179   (let ((n 200))
3180     (unless (and gnus-thread-indent-array
3181                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3182       (setq gnus-thread-indent-array (make-vector 201 "")
3183             gnus-thread-indent-array-level gnus-thread-indent-level)
3184       (while (>= n 0)
3185         (aset gnus-thread-indent-array n
3186               (make-string (* n gnus-thread-indent-level) ? ))
3187         (setq n (1- n))))))
3188
3189 (defun gnus-update-summary-mark-positions ()
3190   "Compute where the summary marks are to go."
3191   (save-excursion
3192     (when (gnus-buffer-exists-p gnus-summary-buffer)
3193       (set-buffer gnus-summary-buffer))
3194     (let ((gnus-replied-mark 129)
3195           (gnus-score-below-mark 130)
3196           (gnus-score-over-mark 130)
3197           (gnus-undownloaded-mark 131)
3198           (spec gnus-summary-line-format-spec)
3199           gnus-visual pos)
3200       (save-excursion
3201         (gnus-set-work-buffer)
3202         (let ((gnus-summary-line-format-spec spec)
3203               (gnus-newsgroup-downloadable '(0)))
3204           (gnus-summary-insert-line
3205            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3206            0 nil t 128 t nil "" nil 1)
3207           (goto-char (point-min))
3208           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3209                                              (- (point) (point-min) 1)))))
3210           (goto-char (point-min))
3211           (push (cons 'replied (and (search-forward "\201" nil t)
3212                                     (- (point) (point-min) 1)))
3213                 pos)
3214           (goto-char (point-min))
3215           (push (cons 'score (and (search-forward "\202" nil t)
3216                                   (- (point) (point-min) 1)))
3217                 pos)
3218           (goto-char (point-min))
3219           (push (cons 'download
3220                       (and (search-forward "\203" nil t)
3221                            (- (point) (point-min) 1)))
3222                 pos)))
3223       (setq gnus-summary-mark-positions pos))))
3224
3225 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3226   "Insert a dummy root in the summary buffer."
3227   (beginning-of-line)
3228   (gnus-add-text-properties
3229    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3230    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3231
3232 (defun gnus-summary-extract-address-component (from)
3233   (or (car (funcall gnus-extract-address-components from))
3234       from))
3235
3236 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3237   (let ((mail-parse-charset gnus-newsgroup-charset)
3238         ; Is it really necessary to do this next part for each summary line?
3239         ; Luckily, doesn't seem to slow things down much.
3240         (mail-parse-ignored-charsets
3241          (save-excursion (set-buffer gnus-summary-buffer)
3242                          gnus-newsgroup-ignored-charsets)))
3243     (or
3244      (and gnus-ignored-from-addresses
3245           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3246           (let ((extra-headers (mail-header-extra header))
3247                 to
3248                 newsgroups)
3249             (cond
3250              ((setq to (cdr (assq 'To extra-headers)))
3251               (concat "-> "
3252                       (inline
3253                         (gnus-summary-extract-address-component
3254                          (funcall gnus-decode-encoded-word-function to)))))
3255              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3256               (concat "=> " newsgroups)))))
3257      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3258
3259 (defun gnus-summary-insert-line (gnus-tmp-header
3260                                  gnus-tmp-level gnus-tmp-current
3261                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3262                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3263                                  &optional gnus-tmp-dummy gnus-tmp-score
3264                                  gnus-tmp-process)
3265   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3266          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3267          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3268          (gnus-tmp-score-char
3269           (if (or (null gnus-summary-default-score)
3270                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3271                       gnus-summary-zcore-fuzz))
3272               ?                         ;Whitespace
3273             (if (< gnus-tmp-score gnus-summary-default-score)
3274                 gnus-score-below-mark gnus-score-over-mark)))
3275          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3276          (gnus-tmp-replied
3277           (cond (gnus-tmp-process gnus-process-mark)
3278                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3279                  gnus-cached-mark)
3280                 (gnus-tmp-replied gnus-replied-mark)
3281                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3282                  gnus-forwarded-mark)
3283                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3284                  gnus-saved-mark)
3285                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3286                  gnus-recent-mark)
3287                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3288                  gnus-unseen-mark)
3289                 (t gnus-no-mark)))
3290          (gnus-tmp-downloaded
3291           (cond (undownloaded
3292                  gnus-undownloaded-mark)
3293                 (gnus-newsgroup-agentized
3294                  gnus-downloaded-mark)
3295                 (t
3296                  gnus-no-mark)))
3297          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3298          (gnus-tmp-name
3299           (cond
3300            ((string-match "<[^>]+> *$" gnus-tmp-from)
3301             (let ((beg (match-beginning 0)))
3302               (or (and (string-match "^\".+\"" gnus-tmp-from)
3303                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3304                   (substring gnus-tmp-from 0 beg))))
3305            ((string-match "(.+)" gnus-tmp-from)
3306             (substring gnus-tmp-from
3307                        (1+ (match-beginning 0)) (1- (match-end 0))))
3308            (t gnus-tmp-from)))
3309          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3310          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3311          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3312          (buffer-read-only nil))
3313     (when (string= gnus-tmp-name "")
3314       (setq gnus-tmp-name gnus-tmp-from))
3315     (unless (numberp gnus-tmp-lines)
3316       (setq gnus-tmp-lines -1))
3317     (if (= gnus-tmp-lines -1)
3318         (setq gnus-tmp-lines "?")
3319       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3320       (gnus-put-text-property
3321      (point)
3322      (progn (eval gnus-summary-line-format-spec) (point))
3323        'gnus-number gnus-tmp-number)
3324     (when (gnus-visual-p 'summary-highlight 'highlight)
3325       (forward-line -1)
3326       (gnus-run-hooks 'gnus-summary-update-hook)
3327       (forward-line 1))))
3328
3329 (defun gnus-summary-update-line (&optional dont-update)
3330   "Update summary line after change."
3331   (when (and gnus-summary-default-score
3332              (not gnus-summary-inhibit-highlight))
3333     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3334            (article (gnus-summary-article-number))
3335            (score (gnus-summary-article-score article)))
3336       (unless dont-update
3337         (if (and gnus-summary-mark-below
3338                  (< (gnus-summary-article-score)
3339                     gnus-summary-mark-below))
3340             ;; This article has a low score, so we mark it as read.
3341             (when (memq article gnus-newsgroup-unreads)
3342               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3343           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3344             ;; This article was previously marked as read on account
3345             ;; of a low score, but now it has risen, so we mark it as
3346             ;; unread.
3347             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3348         (gnus-summary-update-mark
3349          (if (or (null gnus-summary-default-score)
3350                  (<= (abs (- score gnus-summary-default-score))
3351                      gnus-summary-zcore-fuzz))
3352              ?                          ;Whitespace
3353            (if (< score gnus-summary-default-score)
3354                gnus-score-below-mark gnus-score-over-mark))
3355          'score))
3356       ;; Do visual highlighting.
3357       (when (gnus-visual-p 'summary-highlight 'highlight)
3358         (gnus-run-hooks 'gnus-summary-update-hook)))))
3359
3360 (defvar gnus-tmp-new-adopts nil)
3361
3362 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3363   "Return the number of articles in THREAD.
3364 This may be 0 in some cases -- if none of the articles in
3365 the thread are to be displayed."
3366   (let* ((number
3367          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3368           (cond
3369            ((not (listp thread))
3370             1)
3371            ((and (consp thread) (cdr thread))
3372             (apply
3373              '+ 1 (mapcar
3374                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3375            ((null thread)
3376             1)
3377            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3378             1)
3379            (t 0))))
3380     (when (and level (zerop level) gnus-tmp-new-adopts)
3381       (incf number
3382             (apply '+ (mapcar
3383                        'gnus-summary-number-of-articles-in-thread
3384                        gnus-tmp-new-adopts))))
3385     (if char
3386         (if (> number 1) gnus-not-empty-thread-mark
3387           gnus-empty-thread-mark)
3388       number)))
3389
3390 (defsubst gnus-summary-line-message-size (head)
3391   "Return pretty-printed version of message size.
3392 This function is intended to be used in
3393 `gnus-summary-line-format-alist'."
3394   (let ((c (or (mail-header-chars head) -1)))
3395     (cond ((< c 0) "n/a")               ; chars not available
3396           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3397           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3398           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3399           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3400
3401
3402 (defun gnus-summary-set-local-parameters (group)
3403   "Go through the local params of GROUP and set all variable specs in that list."
3404   (let ((params (gnus-group-find-parameter group))
3405         (vars '(quit-config))           ; Ignore quit-config.
3406         elem)
3407     (while params
3408       (setq elem (car params)
3409             params (cdr params))
3410       (and (consp elem)                 ; Has to be a cons.
3411            (consp (cdr elem))           ; The cdr has to be a list.
3412            (symbolp (car elem))         ; Has to be a symbol in there.
3413            (not (memq (car elem) vars))
3414            (ignore-errors               ; So we set it.
3415              (push (car elem) vars)
3416              (make-local-variable (car elem))
3417              (set (car elem) (eval (nth 1 elem))))))))
3418
3419 (defun gnus-summary-read-group (group &optional show-all no-article
3420                                       kill-buffer no-display backward
3421                                       select-articles)
3422   "Start reading news in newsgroup GROUP.
3423 If SHOW-ALL is non-nil, already read articles are also listed.
3424 If NO-ARTICLE is non-nil, no article is selected initially.
3425 If NO-DISPLAY, don't generate a summary buffer."
3426   (let (result)
3427     (while (and group
3428                 (null (setq result
3429                             (let ((gnus-auto-select-next nil))
3430                               (or (gnus-summary-read-group-1
3431                                    group show-all no-article
3432                                    kill-buffer no-display
3433                                    select-articles)
3434                                   (setq show-all nil
3435                                         select-articles nil)))))
3436                 (eq gnus-auto-select-next 'quietly))
3437       (set-buffer gnus-group-buffer)
3438       ;; The entry function called above goes to the next
3439       ;; group automatically, so we go two groups back
3440       ;; if we are searching for the previous group.
3441       (when backward
3442         (gnus-group-prev-unread-group 2))
3443       (if (not (equal group (gnus-group-group-name)))
3444           (setq group (gnus-group-group-name))
3445         (setq group nil)))
3446     result))
3447
3448 (defun gnus-summary-read-group-1 (group show-all no-article
3449                                         kill-buffer no-display
3450                                         &optional select-articles)
3451   ;; Killed foreign groups can't be entered.
3452   ;;  (when (and (not (gnus-group-native-p group))
3453   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3454   ;;    (error "Dead non-native groups can't be entered"))
3455   (gnus-message 5 "Retrieving newsgroup: %s..."
3456                 (gnus-group-decoded-name group))
3457   (let* ((new-group (gnus-summary-setup-buffer group))
3458          (quit-config (gnus-group-quit-config group))
3459          (did-select (and new-group (gnus-select-newsgroup
3460                                      group show-all select-articles))))
3461     (cond
3462      ;; This summary buffer exists already, so we just select it.
3463      ((not new-group)
3464       (gnus-set-global-variables)
3465       (when kill-buffer
3466         (gnus-kill-or-deaden-summary kill-buffer))
3467       (gnus-configure-windows 'summary 'force)
3468       (gnus-set-mode-line 'summary)
3469       (gnus-summary-position-point)
3470       (message "")
3471       t)
3472      ;; We couldn't select this group.
3473      ((null did-select)
3474       (when (and (eq major-mode 'gnus-summary-mode)
3475                  (not (equal (current-buffer) kill-buffer)))
3476         (kill-buffer (current-buffer))
3477         (if (not quit-config)
3478             (progn
3479               ;; Update the info -- marks might need to be removed,
3480               ;; for instance.
3481               (gnus-summary-update-info)
3482               (set-buffer gnus-group-buffer)
3483               (gnus-group-jump-to-group group)
3484               (gnus-group-next-unread-group 1))
3485           (gnus-handle-ephemeral-exit quit-config)))
3486       (let ((grpinfo (gnus-get-info group)))
3487         (if (null (gnus-info-read grpinfo))
3488             (gnus-message 3 "Group %s contains no messages"
3489                           (gnus-group-decoded-name group))
3490           (gnus-message 3 "Can't select group")))
3491       nil)
3492      ;; The user did a `C-g' while prompting for number of articles,
3493      ;; so we exit this group.
3494      ((eq did-select 'quit)
3495       (and (eq major-mode 'gnus-summary-mode)
3496            (not (equal (current-buffer) kill-buffer))
3497            (kill-buffer (current-buffer)))
3498       (when kill-buffer
3499         (gnus-kill-or-deaden-summary kill-buffer))
3500       (if (not quit-config)
3501           (progn
3502             (set-buffer gnus-group-buffer)
3503             (gnus-group-jump-to-group group)
3504             (gnus-group-next-unread-group 1)
3505             (gnus-configure-windows 'group 'force))
3506         (gnus-handle-ephemeral-exit quit-config))
3507       ;; Finally signal the quit.
3508       (signal 'quit nil))
3509      ;; The group was successfully selected.
3510      (t
3511       (gnus-set-global-variables)
3512       ;; Save the active value in effect when the group was entered.
3513       (setq gnus-newsgroup-active
3514             (gnus-copy-sequence
3515              (gnus-active gnus-newsgroup-name)))
3516       ;; You can change the summary buffer in some way with this hook.
3517       (gnus-run-hooks 'gnus-select-group-hook)
3518       (gnus-update-format-specifications
3519        nil 'summary 'summary-mode 'summary-dummy)
3520       (gnus-update-summary-mark-positions)
3521       ;; Do score processing.
3522       (when gnus-use-scoring
3523         (gnus-possibly-score-headers))
3524       ;; Check whether to fill in the gaps in the threads.
3525       (when gnus-build-sparse-threads
3526         (gnus-build-sparse-threads))
3527       ;; Find the initial limit.
3528       (if gnus-show-threads
3529           (if show-all
3530               (let ((gnus-newsgroup-dormant nil))
3531                 (gnus-summary-initial-limit show-all))
3532             (gnus-summary-initial-limit show-all))
3533         ;; When unthreaded, all articles are always shown.
3534         (setq gnus-newsgroup-limit
3535               (mapcar
3536                (lambda (header) (mail-header-number header))
3537                gnus-newsgroup-headers)))
3538       ;; Generate the summary buffer.
3539       (unless no-display
3540         (gnus-summary-prepare))
3541       (when gnus-use-trees
3542         (gnus-tree-open group)
3543         (setq gnus-summary-highlight-line-function
3544               'gnus-tree-highlight-article))
3545       ;; If the summary buffer is empty, but there are some low-scored
3546       ;; articles or some excluded dormants, we include these in the
3547       ;; buffer.
3548       (when (and (zerop (buffer-size))
3549                  (not no-display))
3550         (cond (gnus-newsgroup-dormant
3551                (gnus-summary-limit-include-dormant))
3552               ((and gnus-newsgroup-scored show-all)
3553                (gnus-summary-limit-include-expunged t))))
3554       ;; Function `gnus-apply-kill-file' must be called in this hook.
3555       (gnus-run-hooks 'gnus-apply-kill-hook)
3556       (if (and (zerop (buffer-size))
3557                (not no-display))
3558           (progn
3559             ;; This newsgroup is empty.
3560             (gnus-summary-catchup-and-exit nil t)
3561             (gnus-message 6 "No unread news")
3562             (when kill-buffer
3563               (gnus-kill-or-deaden-summary kill-buffer))
3564             ;; Return nil from this function.
3565             nil)
3566         ;; Hide conversation thread subtrees.  We cannot do this in
3567         ;; gnus-summary-prepare-hook since kill processing may not
3568         ;; work with hidden articles.
3569         (gnus-summary-maybe-hide-threads)
3570         (when kill-buffer
3571           (gnus-kill-or-deaden-summary kill-buffer))
3572         (gnus-summary-auto-select-subject)
3573         ;; Show first unread article if requested.
3574         (if (and (not no-article)
3575                  (not no-display)
3576                  gnus-newsgroup-unreads
3577                  gnus-auto-select-first)
3578             (progn
3579               (gnus-configure-windows 'summary)
3580               (let ((art (gnus-summary-article-number)))
3581                 (unless (and (not gnus-plugged)
3582                              (or (memq art gnus-newsgroup-undownloaded)
3583                                  (memq art gnus-newsgroup-downloadable)))
3584                   (gnus-summary-goto-article art))))
3585           ;; Don't select any articles.
3586           (gnus-summary-position-point)
3587           (gnus-configure-windows 'summary 'force)
3588           (gnus-set-mode-line 'summary))
3589         (when (and gnus-auto-center-group
3590                    (get-buffer-window gnus-group-buffer t))
3591           ;; Gotta use windows, because recenter does weird stuff if
3592           ;; the current buffer ain't the displayed window.
3593           (let ((owin (selected-window)))
3594             (select-window (get-buffer-window gnus-group-buffer t))
3595             (when (gnus-group-goto-group group)
3596               (recenter))
3597             (select-window owin)))
3598         ;; Mark this buffer as "prepared".
3599         (setq gnus-newsgroup-prepared t)
3600         (gnus-run-hooks 'gnus-summary-prepared-hook)
3601         (unless (gnus-ephemeral-group-p group)
3602           (gnus-group-update-group group))
3603         t)))))
3604
3605 (defun gnus-summary-auto-select-subject ()
3606   "Select the subject line on initial group entry."
3607   (goto-char (point-min))
3608   (cond
3609    ((eq gnus-auto-select-subject 'best)
3610     (gnus-summary-best-unread-subject))
3611    ((eq gnus-auto-select-subject 'unread)
3612     (gnus-summary-first-unread-subject))
3613    ((eq gnus-auto-select-subject 'unseen)
3614     (gnus-summary-first-unseen-subject))
3615    ((eq gnus-auto-select-subject 'unseen-or-unread)
3616     (gnus-summary-first-unseen-or-unread-subject))
3617    ((eq gnus-auto-select-subject 'first)
3618     ;; Do nothing.
3619     )
3620    ((functionp gnus-auto-select-subject)
3621     (funcall gnus-auto-select-subject))))
3622
3623 (defun gnus-summary-prepare ()
3624   "Generate the summary buffer."
3625   (interactive)
3626   (let ((buffer-read-only nil))
3627     (erase-buffer)
3628     (setq gnus-newsgroup-data nil
3629           gnus-newsgroup-data-reverse nil)
3630     (gnus-run-hooks 'gnus-summary-generate-hook)
3631     ;; Generate the buffer, either with threads or without.
3632     (when gnus-newsgroup-headers
3633       (gnus-summary-prepare-threads
3634        (if gnus-show-threads
3635            (gnus-sort-gathered-threads
3636             (funcall gnus-summary-thread-gathering-function
3637                      (gnus-sort-threads
3638                       (gnus-cut-threads (gnus-make-threads)))))
3639          ;; Unthreaded display.
3640          (gnus-sort-articles gnus-newsgroup-headers))))
3641     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3642     ;; Call hooks for modifying summary buffer.
3643     (goto-char (point-min))
3644     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3645
3646 (defsubst gnus-general-simplify-subject (subject)
3647   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3648   (setq subject
3649         (cond
3650          ;; Truncate the subject.
3651          (gnus-simplify-subject-functions
3652           (gnus-map-function gnus-simplify-subject-functions subject))
3653          ((numberp gnus-summary-gather-subject-limit)
3654           (setq subject (gnus-simplify-subject-re subject))
3655           (if (> (length subject) gnus-summary-gather-subject-limit)
3656               (substring subject 0 gnus-summary-gather-subject-limit)
3657             subject))
3658          ;; Fuzzily simplify it.
3659          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3660           (gnus-simplify-subject-fuzzy subject))
3661          ;; Just remove the leading "Re:".
3662          (t
3663           (gnus-simplify-subject-re subject))))
3664
3665   (if (and gnus-summary-gather-exclude-subject
3666            (string-match gnus-summary-gather-exclude-subject subject))
3667       nil                         ; This article shouldn't be gathered
3668     subject))
3669
3670 (defun gnus-summary-simplify-subject-query ()
3671   "Query where the respool algorithm would put this article."
3672   (interactive)
3673   (gnus-summary-select-article)
3674   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3675
3676 (defun gnus-gather-threads-by-subject (threads)
3677   "Gather threads by looking at Subject headers."
3678   (if (not gnus-summary-make-false-root)
3679       threads
3680     (let ((hashtb (gnus-make-hashtable 1024))
3681           (prev threads)
3682           (result threads)
3683           subject hthread whole-subject)
3684       (while threads
3685         (setq subject (gnus-general-simplify-subject
3686                        (setq whole-subject (mail-header-subject
3687                                             (caar threads)))))
3688         (when subject
3689           (if (setq hthread (gnus-gethash subject hashtb))
3690               (progn
3691                 ;; We enter a dummy root into the thread, if we
3692                 ;; haven't done that already.
3693                 (unless (stringp (caar hthread))
3694                   (setcar hthread (list whole-subject (car hthread))))
3695                 ;; We add this new gathered thread to this gathered
3696                 ;; thread.
3697                 (setcdr (car hthread)
3698                         (nconc (cdar hthread) (list (car threads))))
3699                 ;; Remove it from the list of threads.
3700                 (setcdr prev (cdr threads))
3701                 (setq threads prev))
3702             ;; Enter this thread into the hash table.
3703             (gnus-sethash subject
3704                           (if gnus-summary-make-false-root-always
3705                               (progn
3706                                 ;; If you want a dummy root above all
3707                                 ;; threads...
3708                                 (setcar threads (list whole-subject
3709                                                       (car threads)))
3710                                 threads)
3711                             threads)
3712                           hashtb)))
3713         (setq prev threads)
3714         (setq threads (cdr threads)))
3715       result)))
3716
3717 (defun gnus-gather-threads-by-references (threads)
3718   "Gather threads by looking at References headers."
3719   (let ((idhashtb (gnus-make-hashtable 1024))
3720         (thhashtb (gnus-make-hashtable 1024))
3721         (prev threads)
3722         (result threads)
3723         ids references id gthread gid entered ref)
3724     (while threads
3725       (when (setq references (mail-header-references (caar threads)))
3726         (setq id (mail-header-id (caar threads))
3727               ids (inline (gnus-split-references references))
3728               entered nil)
3729         (while (setq ref (pop ids))
3730           (setq ids (delete ref ids))
3731           (if (not (setq gid (gnus-gethash ref idhashtb)))
3732               (progn
3733                 (gnus-sethash ref id idhashtb)
3734                 (gnus-sethash id threads thhashtb))
3735             (setq gthread (gnus-gethash gid thhashtb))
3736             (unless entered
3737               ;; We enter a dummy root into the thread, if we
3738               ;; haven't done that already.
3739               (unless (stringp (caar gthread))
3740                 (setcar gthread (list (mail-header-subject (caar gthread))
3741                                       (car gthread))))
3742               ;; We add this new gathered thread to this gathered
3743               ;; thread.
3744               (setcdr (car gthread)
3745                       (nconc (cdar gthread) (list (car threads)))))
3746             ;; Add it into the thread hash table.
3747             (gnus-sethash id gthread thhashtb)
3748             (setq entered t)
3749             ;; Remove it from the list of threads.
3750             (setcdr prev (cdr threads))
3751             (setq threads prev))))
3752       (setq prev threads)
3753       (setq threads (cdr threads)))
3754     result))
3755
3756 (defun gnus-sort-gathered-threads (threads)
3757   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3758   (let ((result threads))
3759     (while threads
3760       (when (stringp (caar threads))
3761         (setcdr (car threads)
3762                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3763       (setq threads (cdr threads)))
3764     result))
3765
3766 (defun gnus-thread-loop-p (root thread)
3767   "Say whether ROOT is in THREAD."
3768   (let ((stack (list thread))
3769         (infloop 0)
3770         th)
3771     (while (setq thread (pop stack))
3772       (setq th (cdr thread))
3773       (while (and th
3774                   (not (eq (caar th) root)))
3775         (pop th))
3776       (if th
3777           ;; We have found a loop.
3778           (let (ref-dep)
3779             (setcdr thread (delq (car th) (cdr thread)))
3780             (if (boundp (setq ref-dep (intern "none"
3781                                               gnus-newsgroup-dependencies)))
3782                 (setcdr (symbol-value ref-dep)
3783                         (nconc (cdr (symbol-value ref-dep))
3784                                (list (car th))))
3785               (set ref-dep (list nil (car th))))
3786             (setq infloop 1
3787                   stack nil))
3788         ;; Push all the subthreads onto the stack.
3789         (push (cdr thread) stack)))
3790     infloop))
3791
3792 (defun gnus-make-threads ()
3793   "Go through the dependency hashtb and find the roots.  Return all threads."
3794   (let (threads)
3795     (while (catch 'infloop
3796              (mapatoms
3797               (lambda (refs)
3798                 ;; Deal with self-referencing References loops.
3799                 (when (and (car (symbol-value refs))
3800                            (not (zerop
3801                                  (apply
3802                                   '+
3803                                   (mapcar
3804                                    (lambda (thread)
3805                                      (gnus-thread-loop-p
3806                                       (car (symbol-value refs)) thread))
3807                                    (cdr (symbol-value refs)))))))
3808                   (setq threads nil)
3809                   (throw 'infloop t))
3810                 (unless (car (symbol-value refs))
3811                   ;; These threads do not refer back to any other
3812                   ;; articles, so they're roots.
3813                   (setq threads (append (cdr (symbol-value refs)) threads))))
3814               gnus-newsgroup-dependencies)))
3815     threads))
3816
3817 ;; Build the thread tree.
3818 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3819   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3820
3821 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3822 if it was already present.
3823
3824 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3825 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3826 Message-IDs will be renamed to a unique Message-ID before being
3827 entered.
3828
3829 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3830   (let* ((id (mail-header-id header))
3831          (id-dep (and id (intern id dependencies)))
3832          parent-id ref ref-dep ref-header replaced)
3833     ;; Enter this `header' in the `dependencies' table.
3834     (cond
3835      ((not id-dep)
3836       (setq header nil))
3837      ;; The first two cases do the normal part: enter a new `header'
3838      ;; in the `dependencies' table.
3839      ((not (boundp id-dep))
3840       (set id-dep (list header)))
3841      ((null (car (symbol-value id-dep)))
3842       (setcar (symbol-value id-dep) header))
3843
3844      ;; From here the `header' was already present in the
3845      ;; `dependencies' table.
3846      (force-new
3847       ;; Overrides an existing entry;
3848       ;; just set the header part of the entry.
3849       (setcar (symbol-value id-dep) header)
3850       (setq replaced t))
3851
3852      ;; Renames the existing `header' to a unique Message-ID.
3853      ((not gnus-summary-ignore-duplicates)
3854       ;; An article with this Message-ID has already been seen.
3855       ;; We rename the Message-ID.
3856       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3857            (list header))
3858       (mail-header-set-id header id))
3859
3860      ;; The last case ignores an existing entry, except it adds any
3861      ;; additional Xrefs (in case the two articles came from different
3862      ;; servers.
3863      ;; Also sets `header' to `nil' meaning that the `dependencies'
3864      ;; table was *not* modified.
3865      (t
3866       (mail-header-set-xref
3867        (car (symbol-value id-dep))
3868        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3869                    "")
3870                (or (mail-header-xref header) "")))
3871       (setq header nil)))
3872
3873     (when (and header (not replaced))
3874       ;; First check that we are not creating a References loop.
3875       (setq parent-id (gnus-parent-id (mail-header-references header)))
3876       (setq ref parent-id)
3877       (while (and ref
3878                   (setq ref-dep (intern-soft ref dependencies))
3879                   (boundp ref-dep)
3880                   (setq ref-header (car (symbol-value ref-dep))))
3881         (if (string= id ref)
3882             ;; Yuk!  This is a reference loop.  Make the article be a
3883             ;; root article.
3884             (progn
3885               (mail-header-set-references (car (symbol-value id-dep)) "none")
3886               (setq ref nil)
3887               (setq parent-id nil))
3888           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3889       (setq ref-dep (intern (or parent-id "none") dependencies))
3890       (if (boundp ref-dep)
3891           (setcdr (symbol-value ref-dep)
3892                   (nconc (cdr (symbol-value ref-dep))
3893                          (list (symbol-value id-dep))))
3894         (set ref-dep (list nil (symbol-value id-dep)))))
3895     header))
3896
3897 (defun gnus-extract-message-id-from-in-reply-to (string)
3898   (if (string-match "<[^>]+>" string)
3899       (substring string (match-beginning 0) (match-end 0))
3900     nil))
3901
3902 (defun gnus-build-sparse-threads ()
3903   (let ((headers gnus-newsgroup-headers)
3904         (mail-parse-charset gnus-newsgroup-charset)
3905         (gnus-summary-ignore-duplicates t)
3906         header references generation relations
3907         subject child end new-child date)
3908     ;; First we create an alist of generations/relations, where
3909     ;; generations is how much we trust the relation, and the relation
3910     ;; is parent/child.
3911     (gnus-message 7 "Making sparse threads...")
3912     (save-excursion
3913       (nnheader-set-temp-buffer " *gnus sparse threads*")
3914       (while (setq header (pop headers))
3915         (when (and (setq references (mail-header-references header))
3916                    (not (string= references "")))
3917           (insert references)
3918           (setq child (mail-header-id header)
3919                 subject (mail-header-subject header)
3920                 date (mail-header-date header)
3921                 generation 0)
3922           (while (search-backward ">" nil t)
3923             (setq end (1+ (point)))
3924             (when (search-backward "<" nil t)
3925               (setq new-child (buffer-substring (point) end))
3926               (push (list (incf generation)
3927                           child (setq child new-child)
3928                           subject date)
3929                     relations)))
3930           (when child
3931             (push (list (1+ generation) child nil subject) relations))
3932           (erase-buffer)))
3933       (kill-buffer (current-buffer)))
3934     ;; Sort over trustworthiness.
3935     (mapcar
3936      (lambda (relation)
3937        (when (gnus-dependencies-add-header
3938               (make-full-mail-header
3939                gnus-reffed-article-number
3940                (nth 3 relation) "" (or (nth 4 relation) "")
3941                (nth 1 relation)
3942                (or (nth 2 relation) "") 0 0 "")
3943               gnus-newsgroup-dependencies nil)
3944          (push gnus-reffed-article-number gnus-newsgroup-limit)
3945          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3946          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3947                gnus-newsgroup-reads)
3948          (decf gnus-reffed-article-number)))
3949      (sort relations 'car-less-than-car))
3950     (gnus-message 7 "Making sparse threads...done")))
3951
3952 (defun gnus-build-old-threads ()
3953   ;; Look at all the articles that refer back to old articles, and
3954   ;; fetch the headers for the articles that aren't there.  This will
3955   ;; build complete threads - if the roots haven't been expired by the
3956   ;; server, that is.
3957   (let ((mail-parse-charset gnus-newsgroup-charset)
3958         id heads)
3959     (mapatoms
3960      (lambda (refs)
3961        (when (not (car (symbol-value refs)))
3962          (setq heads (cdr (symbol-value refs)))
3963          (while heads
3964            (if (memq (mail-header-number (caar heads))
3965                      gnus-newsgroup-dormant)
3966                (setq heads (cdr heads))
3967              (setq id (symbol-name refs))
3968              (while (and (setq id (gnus-build-get-header id))
3969                          (not (car (gnus-id-to-thread id)))))
3970              (setq heads nil)))))
3971      gnus-newsgroup-dependencies)))
3972
3973 ;; This function has to be called with point after the article number
3974 ;; on the beginning of the line.
3975 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3976   (let ((eol (gnus-point-at-eol))
3977         (buffer (current-buffer))
3978         header references in-reply-to)
3979
3980     ;; overview: [num subject from date id refs chars lines misc]
3981     (unwind-protect
3982         (let (x)
3983           (narrow-to-region (point) eol)
3984           (unless (eobp)
3985             (forward-char))
3986
3987           (setq header
3988                 (make-full-mail-header
3989                  number                 ; number
3990                  (condition-case ()     ; subject
3991                      (funcall gnus-decode-encoded-word-function
3992                               (setq x (nnheader-nov-field)))
3993                    (error x))
3994                  (condition-case ()     ; from
3995                      (funcall gnus-decode-encoded-word-function
3996                               (setq x (nnheader-nov-field)))
3997                    (error x))
3998                  (nnheader-nov-field)   ; date
3999                  (nnheader-nov-read-message-id) ; id
4000                  (setq references (nnheader-nov-field)) ; refs
4001                  (nnheader-nov-read-integer) ; chars
4002                  (nnheader-nov-read-integer) ; lines
4003                  (unless (eobp)
4004                    (if (looking-at "Xref: ")
4005                        (goto-char (match-end 0)))
4006                    (nnheader-nov-field)) ; Xref
4007                  (nnheader-nov-parse-extra)))) ; extra
4008
4009       (widen))
4010
4011     (when (and (string= references "")
4012                (setq in-reply-to (mail-header-extra header))
4013                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4014       (mail-header-set-references
4015        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4016
4017     (when gnus-alter-header-function
4018       (funcall gnus-alter-header-function header))
4019     (gnus-dependencies-add-header header dependencies force-new)))
4020
4021 (defun gnus-build-get-header (id)
4022   "Look through the buffer of NOV lines and find the header to ID.
4023 Enter this line into the dependencies hash table, and return
4024 the id of the parent article (if any)."
4025   (let ((deps gnus-newsgroup-dependencies)
4026         found header)
4027     (prog1
4028         (save-excursion
4029           (set-buffer nntp-server-buffer)
4030           (let ((case-fold-search nil))
4031             (goto-char (point-min))
4032             (while (and (not found)
4033                         (search-forward id nil t))
4034               (beginning-of-line)
4035               (setq found (looking-at
4036                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4037                                    (regexp-quote id))))
4038               (or found (beginning-of-line 2)))
4039             (when found
4040               (beginning-of-line)
4041               (and
4042                (setq header (gnus-nov-parse-line
4043                              (read (current-buffer)) deps))
4044                (gnus-parent-id (mail-header-references header))))))
4045       (when header
4046         (let ((number (mail-header-number header)))
4047           (push number gnus-newsgroup-limit)
4048           (push header gnus-newsgroup-headers)
4049           (if (memq number gnus-newsgroup-unselected)
4050               (progn
4051                 (setq gnus-newsgroup-unreads
4052                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4053                                                number))
4054                 (setq gnus-newsgroup-unselected
4055                       (delq number gnus-newsgroup-unselected)))
4056             (push number gnus-newsgroup-ancient)))))))
4057
4058 (defun gnus-build-all-threads ()
4059   "Read all the headers."
4060   (let ((gnus-summary-ignore-duplicates t)
4061         (mail-parse-charset gnus-newsgroup-charset)
4062         (dependencies gnus-newsgroup-dependencies)
4063         header article)
4064     (save-excursion
4065       (set-buffer nntp-server-buffer)
4066       (let ((case-fold-search nil))
4067         (goto-char (point-min))
4068         (while (not (eobp))
4069           (ignore-errors
4070             (setq article (read (current-buffer))
4071                   header (gnus-nov-parse-line article dependencies)))
4072           (when header
4073             (save-excursion
4074               (set-buffer gnus-summary-buffer)
4075               (push header gnus-newsgroup-headers)
4076               (if (memq (setq article (mail-header-number header))
4077                         gnus-newsgroup-unselected)
4078                   (progn
4079                     (setq gnus-newsgroup-unreads
4080                           (gnus-add-to-sorted-list
4081                            gnus-newsgroup-unreads article))
4082                     (setq gnus-newsgroup-unselected
4083                           (delq article gnus-newsgroup-unselected)))
4084                 (push article gnus-newsgroup-ancient)))
4085             (forward-line 1)))))))
4086
4087 (defun gnus-summary-update-article-line (article header)
4088   "Update the line for ARTICLE using HEADER."
4089   (let* ((id (mail-header-id header))
4090          (thread (gnus-id-to-thread id)))
4091     (unless thread
4092       (error "Article in no thread"))
4093     ;; Update the thread.
4094     (setcar thread header)
4095     (gnus-summary-goto-subject article)
4096     (let* ((datal (gnus-data-find-list article))
4097            (data (car datal))
4098            (buffer-read-only nil)
4099            (level (gnus-summary-thread-level)))
4100       (gnus-delete-line)
4101       (let ((inserted (- (point)
4102                          (progn
4103                            (gnus-summary-insert-line
4104                             header level nil
4105                             (memq article gnus-newsgroup-undownloaded)
4106                             (gnus-article-mark article)
4107                             (memq article gnus-newsgroup-replied)
4108                             (memq article gnus-newsgroup-expirable)
4109                             ;; Only insert the Subject string when it's different
4110                             ;; from the previous Subject string.
4111                             (if (and
4112                                  gnus-show-threads
4113                                  (gnus-subject-equal
4114                                   (condition-case ()
4115                                       (mail-header-subject
4116                                        (gnus-data-header
4117                                         (cadr
4118                                          (gnus-data-find-list
4119                                           article
4120                                           (gnus-data-list t)))))
4121                                     ;; Error on the side of excessive subjects.
4122                                     (error ""))
4123                                   (mail-header-subject header)))
4124                                 ""
4125                               (mail-header-subject header))
4126                             nil (cdr (assq article gnus-newsgroup-scored))
4127                             (memq article gnus-newsgroup-processable))
4128                            (point)))))
4129         (when (cdr datal)
4130           (gnus-data-update-list
4131            (cdr datal)
4132            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4133
4134 (defun gnus-summary-update-article (article &optional iheader)
4135   "Update ARTICLE in the summary buffer."
4136   (set-buffer gnus-summary-buffer)
4137   (let* ((header (gnus-summary-article-header article))
4138          (id (mail-header-id header))
4139          (data (gnus-data-find article))
4140          (thread (gnus-id-to-thread id))
4141          (references (mail-header-references header))
4142          (parent
4143           (gnus-id-to-thread
4144            (or (gnus-parent-id
4145                 (when (and references
4146                            (not (equal "" references)))
4147                   references))
4148                "none")))
4149          (buffer-read-only nil)
4150          (old (car thread)))
4151     (when thread
4152       (unless iheader
4153         (setcar thread nil)
4154         (when parent
4155           (delq thread parent)))
4156       (if (gnus-summary-insert-subject id header)
4157           ;; Set the (possibly) new article number in the data structure.
4158           (gnus-data-set-number data (gnus-id-to-article id))
4159         (setcar thread old)
4160         nil))))
4161
4162 (defun gnus-rebuild-thread (id &optional line)
4163   "Rebuild the thread containing ID.
4164 If LINE, insert the rebuilt thread starting on line LINE."
4165   (let ((buffer-read-only nil)
4166         old-pos current thread data)
4167     (if (not gnus-show-threads)
4168         (setq thread (list (car (gnus-id-to-thread id))))
4169       ;; Get the thread this article is part of.
4170       (setq thread (gnus-remove-thread id)))
4171     (setq old-pos (gnus-point-at-bol))
4172     (setq current (save-excursion
4173                     (and (re-search-backward "[\r\n]" nil t)
4174                          (gnus-summary-article-number))))
4175     ;; If this is a gathered thread, we have to go some re-gathering.
4176     (when (stringp (car thread))
4177       (let ((subject (car thread))
4178             roots thr)
4179         (setq thread (cdr thread))
4180         (while thread
4181           (unless (memq (setq thr (gnus-id-to-thread
4182                                    (gnus-root-id
4183                                     (mail-header-id (caar thread)))))
4184                         roots)
4185             (push thr roots))
4186           (setq thread (cdr thread)))
4187         ;; We now have all (unique) roots.
4188         (if (= (length roots) 1)
4189             ;; All the loose roots are now one solid root.
4190             (setq thread (car roots))
4191           (setq thread (cons subject (gnus-sort-threads roots))))))
4192     (let (threads)
4193       ;; We then insert this thread into the summary buffer.
4194       (when line
4195         (goto-char (point-min))
4196         (forward-line (1- line)))
4197       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4198         (if gnus-show-threads
4199             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4200           (gnus-summary-prepare-unthreaded thread))
4201         (setq data (nreverse gnus-newsgroup-data))
4202         (setq threads gnus-newsgroup-threads))
4203       ;; We splice the new data into the data structure.
4204       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4205       ;;!!! then we want to insert at the beginning of the buffer.
4206       ;;!!! That happens to be true with Gnus now, but that may
4207       ;;!!! change in the future.  Perhaps.
4208       (gnus-data-enter-list
4209        (if line nil current) data (- (point) old-pos))
4210       (setq gnus-newsgroup-threads
4211             (nconc threads gnus-newsgroup-threads))
4212       (gnus-data-compute-positions))))
4213
4214 (defun gnus-number-to-header (number)
4215   "Return the header for article NUMBER."
4216   (let ((headers gnus-newsgroup-headers))
4217     (while (and headers
4218                 (not (= number (mail-header-number (car headers)))))
4219       (pop headers))
4220     (when headers
4221       (car headers))))
4222
4223 (defun gnus-parent-headers (in-headers &optional generation)
4224   "Return the headers of the GENERATIONeth parent of HEADERS."
4225   (unless generation
4226     (setq generation 1))
4227   (let ((parent t)
4228         (headers in-headers)
4229         references)
4230     (while (and parent
4231                 (not (zerop generation))
4232                 (setq references (mail-header-references headers)))
4233       (setq headers (if (and references
4234                              (setq parent (gnus-parent-id references)))
4235                         (car (gnus-id-to-thread parent))
4236                       nil))
4237       (decf generation))
4238     (and (not (eq headers in-headers))
4239          headers)))
4240
4241 (defun gnus-id-to-thread (id)
4242   "Return the (sub-)thread where ID appears."
4243   (gnus-gethash id gnus-newsgroup-dependencies))
4244
4245 (defun gnus-id-to-article (id)
4246   "Return the article number of ID."
4247   (let ((thread (gnus-id-to-thread id)))
4248     (when (and thread
4249                (car thread))
4250       (mail-header-number (car thread)))))
4251
4252 (defun gnus-id-to-header (id)
4253   "Return the article headers of ID."
4254   (car (gnus-id-to-thread id)))
4255
4256 (defun gnus-article-displayed-root-p (article)
4257   "Say whether ARTICLE is a root(ish) article."
4258   (let ((level (gnus-summary-thread-level article))
4259         (refs (mail-header-references  (gnus-summary-article-header article)))
4260         particle)
4261     (cond
4262      ((null level) nil)
4263      ((zerop level) t)
4264      ((null refs) t)
4265      ((null (gnus-parent-id refs)) t)
4266      ((and (= 1 level)
4267            (null (setq particle (gnus-id-to-article
4268                                  (gnus-parent-id refs))))
4269            (null (gnus-summary-thread-level particle)))))))
4270
4271 (defun gnus-root-id (id)
4272   "Return the id of the root of the thread where ID appears."
4273   (let (last-id prev)
4274     (while (and id (setq prev (car (gnus-id-to-thread id))))
4275       (setq last-id id
4276             id (gnus-parent-id (mail-header-references prev))))
4277     last-id))
4278
4279 (defun gnus-articles-in-thread (thread)
4280   "Return the list of articles in THREAD."
4281   (cons (mail-header-number (car thread))
4282         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4283
4284 (defun gnus-remove-thread (id &optional dont-remove)
4285   "Remove the thread that has ID in it."
4286   (let (headers thread last-id)
4287     ;; First go up in this thread until we find the root.
4288     (setq last-id (gnus-root-id id)
4289           headers (message-flatten-list (gnus-id-to-thread last-id)))
4290     ;; We have now found the real root of this thread.  It might have
4291     ;; been gathered into some loose thread, so we have to search
4292     ;; through the threads to find the thread we wanted.
4293     (let ((threads gnus-newsgroup-threads)
4294           sub)
4295       (while threads
4296         (setq sub (car threads))
4297         (if (stringp (car sub))
4298             ;; This is a gathered thread, so we look at the roots
4299             ;; below it to find whether this article is in this
4300             ;; gathered root.
4301             (progn
4302               (setq sub (cdr sub))
4303               (while sub
4304                 (when (member (caar sub) headers)
4305                   (setq thread (car threads)
4306                         threads nil
4307                         sub nil))
4308                 (setq sub (cdr sub))))
4309           ;; It's an ordinary thread, so we check it.
4310           (when (eq (car sub) (car headers))
4311             (setq thread sub
4312                   threads nil)))
4313         (setq threads (cdr threads)))
4314       ;; If this article is in no thread, then it's a root.
4315       (if thread
4316           (unless dont-remove
4317             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4318         (setq thread (gnus-id-to-thread last-id)))
4319       (when thread
4320         (prog1
4321             thread                      ; We return this thread.
4322           (unless dont-remove
4323             (if (stringp (car thread))
4324                 (progn
4325                   ;; If we use dummy roots, then we have to remove the
4326                   ;; dummy root as well.
4327                   (when (eq gnus-summary-make-false-root 'dummy)
4328                     ;; We go to the dummy root by going to
4329                     ;; the first sub-"thread", and then one line up.
4330                     (gnus-summary-goto-article
4331                      (mail-header-number (caadr thread)))
4332                     (forward-line -1)
4333                     (gnus-delete-line)
4334                     (gnus-data-compute-positions))
4335                   (setq thread (cdr thread))
4336                   (while thread
4337                     (gnus-remove-thread-1 (car thread))
4338                     (setq thread (cdr thread))))
4339               (gnus-remove-thread-1 thread))))))))
4340
4341 (defun gnus-remove-thread-1 (thread)
4342   "Remove the thread THREAD recursively."
4343   (let ((number (mail-header-number (pop thread)))
4344         d)
4345     (setq thread (reverse thread))
4346     (while thread
4347       (gnus-remove-thread-1 (pop thread)))
4348     (when (setq d (gnus-data-find number))
4349       (goto-char (gnus-data-pos d))
4350       (gnus-summary-show-thread)
4351       (gnus-data-remove
4352        number
4353        (- (gnus-point-at-bol)
4354           (prog1
4355               (1+ (gnus-point-at-eol))
4356             (gnus-delete-line)))))))
4357
4358 (defun gnus-sort-threads-1 (threads func)
4359   (sort (mapcar (lambda (thread)
4360                   (cons (car thread)
4361                         (and (cdr thread)
4362                              (gnus-sort-threads-1 (cdr thread) func))))
4363                 threads) func))
4364
4365 (defun gnus-sort-threads (threads)
4366   "Sort THREADS."
4367   (if (not gnus-thread-sort-functions)
4368       threads
4369     (gnus-message 8 "Sorting threads...")
4370     (let ((max-lisp-eval-depth 5000))
4371       (prog1 (gnus-sort-threads-1
4372          threads
4373          (gnus-make-sort-function gnus-thread-sort-functions))
4374         (gnus-message 8 "Sorting threads...done")))))
4375
4376 (defun gnus-sort-articles (articles)
4377   "Sort ARTICLES."
4378   (when gnus-article-sort-functions
4379     (gnus-message 7 "Sorting articles...")
4380     (prog1
4381         (setq gnus-newsgroup-headers
4382               (sort articles (gnus-make-sort-function
4383                               gnus-article-sort-functions)))
4384       (gnus-message 7 "Sorting articles...done"))))
4385
4386 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4387 (defmacro gnus-thread-header (thread)
4388   "Return header of first article in THREAD.
4389 Note that THREAD must never, ever be anything else than a variable -
4390 using some other form will lead to serious barfage."
4391   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4392   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4393   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4394         (vector thread) 2))
4395
4396 (defsubst gnus-article-sort-by-number (h1 h2)
4397   "Sort articles by article number."
4398   (< (mail-header-number h1)
4399      (mail-header-number h2)))
4400
4401 (defun gnus-thread-sort-by-number (h1 h2)
4402   "Sort threads by root article number."
4403   (gnus-article-sort-by-number
4404    (gnus-thread-header h1) (gnus-thread-header h2)))
4405
4406 (defsubst gnus-article-sort-by-random (h1 h2)
4407   "Sort articles by article number."
4408   (zerop (random 2)))
4409
4410 (defun gnus-thread-sort-by-random (h1 h2)
4411   "Sort threads by root article number."
4412   (gnus-article-sort-by-random
4413    (gnus-thread-header h1) (gnus-thread-header h2)))
4414
4415 (defsubst gnus-article-sort-by-lines (h1 h2)
4416   "Sort articles by article Lines header."
4417   (< (mail-header-lines h1)
4418      (mail-header-lines h2)))
4419
4420 (defun gnus-thread-sort-by-lines (h1 h2)
4421   "Sort threads by root article Lines header."
4422   (gnus-article-sort-by-lines
4423    (gnus-thread-header h1) (gnus-thread-header h2)))
4424
4425 (defsubst gnus-article-sort-by-chars (h1 h2)
4426   "Sort articles by octet length."
4427   (< (mail-header-chars h1)
4428      (mail-header-chars h2)))
4429
4430 (defun gnus-thread-sort-by-chars (h1 h2)
4431   "Sort threads by root article octet length."
4432   (gnus-article-sort-by-chars
4433    (gnus-thread-header h1) (gnus-thread-header h2)))
4434
4435 (defsubst gnus-article-sort-by-author (h1 h2)
4436   "Sort articles by root author."
4437   (string-lessp
4438    (let ((extract (funcall
4439                    gnus-extract-address-components
4440                    (mail-header-from h1))))
4441      (or (car extract) (cadr extract) ""))
4442    (let ((extract (funcall
4443                    gnus-extract-address-components
4444                    (mail-header-from h2))))
4445      (or (car extract) (cadr extract) ""))))
4446
4447 (defun gnus-thread-sort-by-author (h1 h2)
4448   "Sort threads by root author."
4449   (gnus-article-sort-by-author
4450    (gnus-thread-header h1)  (gnus-thread-header h2)))
4451
4452 (defsubst gnus-article-sort-by-subject (h1 h2)
4453   "Sort articles by root subject."
4454   (string-lessp
4455    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4456    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4457
4458 (defun gnus-thread-sort-by-subject (h1 h2)
4459   "Sort threads by root subject."
4460   (gnus-article-sort-by-subject
4461    (gnus-thread-header h1) (gnus-thread-header h2)))
4462
4463 (defsubst gnus-article-sort-by-date (h1 h2)
4464   "Sort articles by root article date."
4465   (time-less-p
4466    (gnus-date-get-time (mail-header-date h1))
4467    (gnus-date-get-time (mail-header-date h2))))
4468
4469 (defun gnus-thread-sort-by-date (h1 h2)
4470   "Sort threads by root article date."
4471   (gnus-article-sort-by-date
4472    (gnus-thread-header h1) (gnus-thread-header h2)))
4473
4474 (defsubst gnus-article-sort-by-score (h1 h2)
4475   "Sort articles by root article score.
4476 Unscored articles will be counted as having a score of zero."
4477   (> (or (cdr (assq (mail-header-number h1)
4478                     gnus-newsgroup-scored))
4479          gnus-summary-default-score 0)
4480      (or (cdr (assq (mail-header-number h2)
4481                     gnus-newsgroup-scored))
4482          gnus-summary-default-score 0)))
4483
4484 (defun gnus-thread-sort-by-score (h1 h2)
4485   "Sort threads by root article score."
4486   (gnus-article-sort-by-score
4487    (gnus-thread-header h1) (gnus-thread-header h2)))
4488
4489 (defun gnus-thread-sort-by-total-score (h1 h2)
4490   "Sort threads by the sum of all scores in the thread.
4491 Unscored articles will be counted as having a score of zero."
4492   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4493
4494 (defun gnus-thread-total-score (thread)
4495   ;; This function find the total score of THREAD.
4496   (cond
4497    ((null thread)
4498     0)
4499    ((consp thread)
4500     (if (stringp (car thread))
4501         (apply gnus-thread-score-function 0
4502                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4503       (gnus-thread-total-score-1 thread)))
4504    (t
4505     (gnus-thread-total-score-1 (list thread)))))
4506
4507 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4508   "Sort threads such that the thread with the most recently arrived article comes first."
4509   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4510
4511 (defun gnus-thread-highest-number (thread)
4512   "Return the highest article number in THREAD."
4513   (apply 'max (mapcar (lambda (header)
4514                         (mail-header-number header))
4515                       (message-flatten-list thread))))
4516
4517 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4518   "Sort threads such that the thread with the most recently dated article comes first."
4519   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4520
4521 (defun gnus-thread-latest-date (thread)
4522   "Return the highest article date in THREAD."
4523   (let ((previous-time 0))
4524     (apply 'max
4525            (mapcar
4526             (lambda (header)
4527               (setq previous-time
4528                     (condition-case ()
4529                         (time-to-seconds (mail-header-parse-date
4530                                           (mail-header-date header)))
4531                       (error previous-time))))
4532             (sort
4533              (message-flatten-list thread)
4534              (lambda (h1 h2)
4535                (< (mail-header-number h1)
4536                   (mail-header-number h2))))))))
4537
4538 (defun gnus-thread-total-score-1 (root)
4539   ;; This function find the total score of the thread below ROOT.
4540   (setq root (car root))
4541   (apply gnus-thread-score-function
4542          (or (append
4543               (mapcar 'gnus-thread-total-score
4544                       (cdr (gnus-id-to-thread (mail-header-id root))))
4545               (when (> (mail-header-number root) 0)
4546                 (list (or (cdr (assq (mail-header-number root)
4547                                      gnus-newsgroup-scored))
4548                           gnus-summary-default-score 0))))
4549              (list gnus-summary-default-score)
4550              '(0))))
4551
4552 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4553 (defvar gnus-tmp-prev-subject nil)
4554 (defvar gnus-tmp-false-parent nil)
4555 (defvar gnus-tmp-root-expunged nil)
4556 (defvar gnus-tmp-dummy-line nil)
4557
4558 (eval-when-compile (defvar gnus-tmp-header))
4559 (defun gnus-extra-header (type &optional header)
4560   "Return the extra header of TYPE."
4561   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4562       ""))
4563
4564 (defvar gnus-tmp-thread-tree-header-string "")
4565
4566 (defcustom gnus-sum-thread-tree-root "> "
4567   "With %B spec, used for the root of a thread.
4568 If nil, use subject instead."
4569   :type 'string
4570   :group 'gnus-thread)
4571 (defcustom gnus-sum-thread-tree-false-root "> "
4572   "With %B spec, used for a false root of a thread.
4573 If nil, use subject instead."
4574   :type 'string
4575   :group 'gnus-thread)
4576 (defcustom gnus-sum-thread-tree-single-indent ""
4577   "With %B spec, used for a thread with just one message.
4578 If nil, use subject instead."
4579   :type 'string
4580   :group 'gnus-thread)
4581 (defcustom gnus-sum-thread-tree-vertical "| "
4582   "With %B spec, used for drawing a vertical line."
4583   :type 'string
4584   :group 'gnus-thread)
4585 (defcustom gnus-sum-thread-tree-indent "  "
4586   "With %B spec, used for indenting."
4587   :type 'string
4588   :group 'gnus-thread)
4589 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4590   "With %B spec, used for a leaf with brothers."
4591   :type 'string
4592   :group 'gnus-thread)
4593 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4594   "With %B spec, used for a leaf without brothers."
4595   :type 'string
4596   :group 'gnus-thread)
4597
4598 (defun gnus-summary-prepare-threads (threads)
4599   "Prepare summary buffer from THREADS and indentation LEVEL.
4600 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4601 or a straight list of headers."
4602   (gnus-message 7 "Generating summary...")
4603
4604   (setq gnus-newsgroup-threads threads)
4605   (beginning-of-line)
4606
4607   (let ((gnus-tmp-level 0)
4608         (default-score (or gnus-summary-default-score 0))
4609         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4610         (building-line-count gnus-summary-display-while-building)
4611         (building-count (integerp gnus-summary-display-while-building))
4612         thread number subject stack state gnus-tmp-gathered beg-match
4613         new-roots gnus-tmp-new-adopts thread-end simp-subject
4614         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4615         gnus-tmp-replied gnus-tmp-subject-or-nil
4616         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4617         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4618         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4619         tree-stack)
4620
4621     (setq gnus-tmp-prev-subject nil
4622           gnus-tmp-thread-tree-header-string "")
4623
4624     (if (vectorp (car threads))
4625         ;; If this is a straight (sic) list of headers, then a
4626         ;; threaded summary display isn't required, so we just create
4627         ;; an unthreaded one.
4628         (gnus-summary-prepare-unthreaded threads)
4629
4630       ;; Do the threaded display.
4631
4632       (if gnus-summary-display-while-building
4633           (switch-to-buffer (buffer-name)))
4634       (while (or threads stack gnus-tmp-new-adopts new-roots)
4635
4636         (if (and (= gnus-tmp-level 0)
4637                  (or (not stack)
4638                      (= (caar stack) 0))
4639                  (not gnus-tmp-false-parent)
4640                  (or gnus-tmp-new-adopts new-roots))
4641             (if gnus-tmp-new-adopts
4642                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4643                       thread (list (car gnus-tmp-new-adopts))
4644                       gnus-tmp-header (caar thread)
4645                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4646               (when new-roots
4647                 (setq thread (list (car new-roots))
4648                       gnus-tmp-header (caar thread)
4649                       new-roots (cdr new-roots))))
4650
4651           (if threads
4652               ;; If there are some threads, we do them before the
4653               ;; threads on the stack.
4654               (setq thread threads
4655                     gnus-tmp-header (caar thread))
4656             ;; There were no current threads, so we pop something off
4657             ;; the stack.
4658             (setq state (car stack)
4659                   gnus-tmp-level (car state)
4660                   tree-stack (cadr state)
4661                   thread (caddr state)
4662                   stack (cdr stack)
4663                   gnus-tmp-header (caar thread))))
4664
4665         (setq gnus-tmp-false-parent nil)
4666         (setq gnus-tmp-root-expunged nil)
4667         (setq thread-end nil)
4668
4669         (if (stringp gnus-tmp-header)
4670             ;; The header is a dummy root.
4671             (cond
4672              ((eq gnus-summary-make-false-root 'adopt)
4673               ;; We let the first article adopt the rest.
4674               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4675                                                (cddar thread)))
4676               (setq gnus-tmp-gathered
4677                     (nconc (mapcar
4678                             (lambda (h) (mail-header-number (car h)))
4679                             (cddar thread))
4680                            gnus-tmp-gathered))
4681               (setq thread (cons (list (caar thread)
4682                                        (cadar thread))
4683                                  (cdr thread)))
4684               (setq gnus-tmp-level -1
4685                     gnus-tmp-false-parent t))
4686              ((eq gnus-summary-make-false-root 'empty)
4687               ;; We print adopted articles with empty subject fields.
4688               (setq gnus-tmp-gathered
4689                     (nconc (mapcar
4690                             (lambda (h) (mail-header-number (car h)))
4691                             (cddar thread))
4692                            gnus-tmp-gathered))
4693               (setq gnus-tmp-level -1))
4694              ((eq gnus-summary-make-false-root 'dummy)
4695               ;; We remember that we probably want to output a dummy
4696               ;; root.
4697               (setq gnus-tmp-dummy-line gnus-tmp-header)
4698               (setq gnus-tmp-prev-subject gnus-tmp-header))
4699              (t
4700               ;; We do not make a root for the gathered
4701               ;; sub-threads at all.
4702               (setq gnus-tmp-level -1)))
4703
4704           (setq number (mail-header-number gnus-tmp-header)
4705                 subject (mail-header-subject gnus-tmp-header)
4706                 simp-subject (gnus-simplify-subject-fully subject))
4707
4708           (cond
4709            ;; If the thread has changed subject, we might want to make
4710            ;; this subthread into a root.
4711            ((and (null gnus-thread-ignore-subject)
4712                  (not (zerop gnus-tmp-level))
4713                  gnus-tmp-prev-subject
4714                  (not (string= gnus-tmp-prev-subject simp-subject)))
4715             (setq new-roots (nconc new-roots (list (car thread)))
4716                   thread-end t
4717                   gnus-tmp-header nil))
4718            ;; If the article lies outside the current limit,
4719            ;; then we do not display it.
4720            ((not (memq number gnus-newsgroup-limit))
4721             (setq gnus-tmp-gathered
4722                   (nconc (mapcar
4723                           (lambda (h) (mail-header-number (car h)))
4724                           (cdar thread))
4725                          gnus-tmp-gathered))
4726             (setq gnus-tmp-new-adopts (if (cdar thread)
4727                                           (append gnus-tmp-new-adopts
4728                                                   (cdar thread))
4729                                         gnus-tmp-new-adopts)
4730                   thread-end t
4731                   gnus-tmp-header nil)
4732             (when (zerop gnus-tmp-level)
4733               (setq gnus-tmp-root-expunged t)))
4734            ;; Perhaps this article is to be marked as read?
4735            ((and gnus-summary-mark-below
4736                  (< (or (cdr (assq number gnus-newsgroup-scored))
4737                         default-score)
4738                     gnus-summary-mark-below)
4739                  ;; Don't touch sparse articles.
4740                  (not (gnus-summary-article-sparse-p number))
4741                  (not (gnus-summary-article-ancient-p number)))
4742             (setq gnus-newsgroup-unreads
4743                   (delq number gnus-newsgroup-unreads))
4744             (if gnus-newsgroup-auto-expire
4745                 (setq gnus-newsgroup-expirable
4746                       (gnus-add-to-sorted-list
4747                        gnus-newsgroup-expirable number))
4748               (push (cons number gnus-low-score-mark)
4749                     gnus-newsgroup-reads))))
4750
4751           (when gnus-tmp-header
4752             ;; We may have an old dummy line to output before this
4753             ;; article.
4754             (when (and gnus-tmp-dummy-line
4755                        (gnus-subject-equal
4756                         gnus-tmp-dummy-line
4757                         (mail-header-subject gnus-tmp-header)))
4758               (gnus-summary-insert-dummy-line
4759                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4760               (setq gnus-tmp-dummy-line nil))
4761
4762             ;; Compute the mark.
4763             (setq gnus-tmp-unread (gnus-article-mark number))
4764
4765             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4766                                   gnus-tmp-header gnus-tmp-level)
4767                   gnus-newsgroup-data)
4768
4769             ;; Actually insert the line.
4770             (setq
4771              gnus-tmp-subject-or-nil
4772              (cond
4773               ((and gnus-thread-ignore-subject
4774                     gnus-tmp-prev-subject
4775                     (not (string= gnus-tmp-prev-subject simp-subject)))
4776                subject)
4777               ((zerop gnus-tmp-level)
4778                (if (and (eq gnus-summary-make-false-root 'empty)
4779                         (memq number gnus-tmp-gathered)
4780                         gnus-tmp-prev-subject
4781                         (string= gnus-tmp-prev-subject simp-subject))
4782                    gnus-summary-same-subject
4783                  subject))
4784               (t gnus-summary-same-subject)))
4785             (if (and (eq gnus-summary-make-false-root 'adopt)
4786                      (= gnus-tmp-level 1)
4787                      (memq number gnus-tmp-gathered))
4788                 (setq gnus-tmp-opening-bracket ?\<
4789                       gnus-tmp-closing-bracket ?\>)
4790               (setq gnus-tmp-opening-bracket ?\[
4791                     gnus-tmp-closing-bracket ?\]))
4792             (setq
4793              gnus-tmp-indentation
4794              (aref gnus-thread-indent-array gnus-tmp-level)
4795              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4796              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4797                                 gnus-summary-default-score 0)
4798              gnus-tmp-score-char
4799              (if (or (null gnus-summary-default-score)
4800                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4801                          gnus-summary-zcore-fuzz))
4802                  ?                      ;Whitespace
4803                (if (< gnus-tmp-score gnus-summary-default-score)
4804                    gnus-score-below-mark gnus-score-over-mark))
4805              gnus-tmp-replied
4806              (cond ((memq number gnus-newsgroup-processable)
4807                     gnus-process-mark)
4808                    ((memq number gnus-newsgroup-cached)
4809                     gnus-cached-mark)
4810                    ((memq number gnus-newsgroup-replied)
4811                     gnus-replied-mark)
4812                    ((memq number gnus-newsgroup-forwarded)
4813                     gnus-forwarded-mark)
4814                    ((memq number gnus-newsgroup-saved)
4815                     gnus-saved-mark)
4816                    ((memq number gnus-newsgroup-recent)
4817                     gnus-recent-mark)
4818                    ((memq number gnus-newsgroup-unseen)
4819                     gnus-unseen-mark)
4820                    (t gnus-no-mark))
4821              gnus-tmp-downloaded
4822              (cond ((memq number gnus-newsgroup-undownloaded)
4823                     gnus-undownloaded-mark)
4824                    (gnus-newsgroup-agentized
4825                     gnus-downloaded-mark)
4826                    (t
4827                     gnus-no-mark))
4828              gnus-tmp-from (mail-header-from gnus-tmp-header)
4829              gnus-tmp-name
4830              (cond
4831               ((string-match "<[^>]+> *$" gnus-tmp-from)
4832                (setq beg-match (match-beginning 0))
4833                (or (and (string-match "^\".+\"" gnus-tmp-from)
4834                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4835                    (substring gnus-tmp-from 0 beg-match)))
4836               ((string-match "(.+)" gnus-tmp-from)
4837                (substring gnus-tmp-from
4838                           (1+ (match-beginning 0)) (1- (match-end 0))))
4839               (t gnus-tmp-from))
4840
4841              ;; Do the %B string
4842              gnus-tmp-thread-tree-header-string
4843              (cond
4844               ((not gnus-show-threads) "")
4845               ((zerop gnus-tmp-level)
4846                (cond ((cdar thread)
4847                       (or gnus-sum-thread-tree-root subject))
4848                      (gnus-tmp-new-adopts
4849                       (or gnus-sum-thread-tree-false-root subject))
4850                      (t
4851                       (or gnus-sum-thread-tree-single-indent subject))))
4852               (t
4853                (concat (apply 'concat
4854                               (mapcar (lambda (item)
4855                                         (if (= item 1)
4856                                             gnus-sum-thread-tree-vertical
4857                                           gnus-sum-thread-tree-indent))
4858                                       (cdr (reverse tree-stack))))
4859                        (if (nth 1 thread)
4860                            gnus-sum-thread-tree-leaf-with-other
4861                          gnus-sum-thread-tree-single-leaf)))))
4862             (when (string= gnus-tmp-name "")
4863               (setq gnus-tmp-name gnus-tmp-from))
4864             (unless (numberp gnus-tmp-lines)
4865               (setq gnus-tmp-lines -1))
4866             (if (= gnus-tmp-lines -1)
4867                 (setq gnus-tmp-lines "?")
4868               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4869               (gnus-put-text-property
4870              (point)
4871              (progn (eval gnus-summary-line-format-spec) (point))
4872                'gnus-number number)
4873             (when gnus-visual-p
4874               (forward-line -1)
4875               (gnus-run-hooks 'gnus-summary-update-hook)
4876               (forward-line 1))
4877
4878             (setq gnus-tmp-prev-subject simp-subject)))
4879
4880         (when (nth 1 thread)
4881           (push (list (max 0 gnus-tmp-level)
4882                       (copy-sequence tree-stack)
4883                       (nthcdr 1 thread))
4884                 stack))
4885         (push (if (nth 1 thread) 1 0) tree-stack)
4886         (incf gnus-tmp-level)
4887         (setq threads (if thread-end nil (cdar thread)))
4888         (if gnus-summary-display-while-building
4889             (if building-count
4890                 (progn
4891                   ;; use a set frequency
4892                   (setq building-line-count (1- building-line-count))
4893                   (when (= building-line-count 0)
4894                     (sit-for 0)
4895                     (setq building-line-count
4896                           gnus-summary-display-while-building)))
4897               ;; always
4898               (sit-for 0)))
4899         (unless threads
4900           (setq gnus-tmp-level 0)))))
4901   (gnus-message 7 "Generating summary...done"))
4902
4903 (defun gnus-summary-prepare-unthreaded (headers)
4904   "Generate an unthreaded summary buffer based on HEADERS."
4905   (let (header number mark)
4906
4907     (beginning-of-line)
4908
4909     (while headers
4910       ;; We may have to root out some bad articles...
4911       (when (memq (setq number (mail-header-number
4912                                 (setq header (pop headers))))
4913                   gnus-newsgroup-limit)
4914         ;; Mark article as read when it has a low score.
4915         (when (and gnus-summary-mark-below
4916                    (< (or (cdr (assq number gnus-newsgroup-scored))
4917                           gnus-summary-default-score 0)
4918                       gnus-summary-mark-below)
4919                    (not (gnus-summary-article-ancient-p number)))
4920           (setq gnus-newsgroup-unreads
4921                 (delq number gnus-newsgroup-unreads))
4922           (if gnus-newsgroup-auto-expire
4923               (push number gnus-newsgroup-expirable)
4924             (push (cons number gnus-low-score-mark)
4925                   gnus-newsgroup-reads)))
4926
4927         (setq mark (gnus-article-mark number))
4928         (push (gnus-data-make number mark (1+ (point)) header 0)
4929               gnus-newsgroup-data)
4930         (gnus-summary-insert-line
4931          header 0 number
4932          (memq number gnus-newsgroup-undownloaded)
4933          mark (memq number gnus-newsgroup-replied)
4934          (memq number gnus-newsgroup-expirable)
4935          (mail-header-subject header) nil
4936          (cdr (assq number gnus-newsgroup-scored))
4937          (memq number gnus-newsgroup-processable))))))
4938
4939 (defun gnus-summary-remove-list-identifiers ()
4940   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4941   (let ((regexp (if (consp gnus-list-identifiers)
4942                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4943                   gnus-list-identifiers))
4944         changed subject)
4945     (when regexp
4946       (dolist (header gnus-newsgroup-headers)
4947         (setq subject (mail-header-subject header)
4948               changed nil)
4949         (while (string-match
4950                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4951                 subject)
4952           (setq subject
4953                 (concat (substring subject 0 (match-beginning 2))
4954                         (substring subject (match-end 0)))
4955                 changed t))
4956         (when (and changed
4957                    (string-match
4958                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4959           (setq subject
4960                 (concat (substring subject 0 (match-beginning 1))
4961                         (substring subject (match-end 1)))))
4962         (when changed
4963           (mail-header-set-subject header subject))))))
4964
4965 (defun gnus-fetch-headers (articles)
4966   "Fetch headers of ARTICLES."
4967   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4968     (gnus-message 5 "Fetching headers for %s..." name)
4969     (prog1
4970         (if (eq 'nov
4971                 (setq gnus-headers-retrieved-by
4972                       (gnus-retrieve-headers
4973                        articles gnus-newsgroup-name
4974                        ;; We might want to fetch old headers, but
4975                        ;; not if there is only 1 article.
4976                        (and (or (and
4977                                  (not (eq gnus-fetch-old-headers 'some))
4978                                  (not (numberp gnus-fetch-old-headers)))
4979                                 (> (length articles) 1))
4980                             gnus-fetch-old-headers))))
4981             (gnus-get-newsgroup-headers-xover
4982              articles nil nil gnus-newsgroup-name t)
4983           (gnus-get-newsgroup-headers))
4984       (gnus-message 5 "Fetching headers for %s...done" name))))
4985
4986 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4987   "Select newsgroup GROUP.
4988 If READ-ALL is non-nil, all articles in the group are selected.
4989 If SELECT-ARTICLES, only select those articles from GROUP."
4990   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4991          ;;!!! Dirty hack; should be removed.
4992          (gnus-summary-ignore-duplicates
4993           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4994               t
4995             gnus-summary-ignore-duplicates))
4996          (info (nth 2 entry))
4997          articles fetched-articles cached)
4998
4999     (unless (gnus-check-server
5000              (set (make-local-variable 'gnus-current-select-method)
5001                   (gnus-find-method-for-group group)))
5002       (error "Couldn't open server"))
5003
5004     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5005         (gnus-activate-group group)     ; Or we can activate it...
5006         (progn                          ; Or we bug out.
5007           (when (equal major-mode 'gnus-summary-mode)
5008             (gnus-kill-buffer (current-buffer)))
5009           (error "Couldn't activate group %s: %s"
5010                  group (gnus-status-message group))))
5011
5012     (unless (gnus-request-group group t)
5013       (when (equal major-mode 'gnus-summary-mode)
5014         (gnus-kill-buffer (current-buffer)))
5015       (error "Couldn't request group %s: %s"
5016              group (gnus-status-message group)))
5017
5018     (when gnus-agent
5019       ;; The agent may be storing articles that are no longer in the
5020       ;; server's active range.  If that is the case, the active range
5021       ;; needs to be expanded such that the agent's articles can be
5022       ;; included in the summary.
5023       (let* ((gnus-command-method (gnus-find-method-for-group group))
5024              (alist (gnus-agent-load-alist group))
5025              (active (gnus-active group)))
5026         (if (and (car alist)
5027                  (< (caar alist) (car active)))
5028             (gnus-set-active group (cons (caar alist) (cdr active)))))
5029
5030       (setq gnus-summary-use-undownloaded-faces
5031             (not (gnus-agent-find-parameter
5032                   group
5033                   'agent-disable-undownloaded-faces))))
5034
5035     (setq gnus-newsgroup-name group
5036           gnus-newsgroup-unselected nil
5037           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5038
5039     (let ((display (gnus-group-find-parameter group 'display)))
5040       (setq gnus-newsgroup-display
5041             (cond
5042              ((not (zerop (or (car-safe read-all) 0)))
5043               ;; The user entered the group with C-u SPC/RET, let's show
5044               ;; all articles.
5045               'gnus-not-ignore)
5046              ((eq display 'all)
5047               'gnus-not-ignore)
5048              ((arrayp display)
5049               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5050              ((numberp display)
5051               ;; The following is probably the "correct" solution, but
5052               ;; it makes Gnus fetch all headers and then limit the
5053               ;; articles (which is slow), so instead we hack the
5054               ;; select-articles parameter instead. -- Simon Josefsson
5055               ;; <jas@kth.se>
5056               ;;
5057               ;; (gnus-byte-compile
5058               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5059               ;;                         display)))))
5060               (setq select-articles
5061                     (gnus-uncompress-range
5062                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5063                              (if (> tmp 0)
5064                                  tmp
5065                                1))
5066                            (cdr (gnus-active group)))))
5067               nil)
5068              (t
5069               nil))))
5070
5071     (gnus-summary-setup-default-charset)
5072
5073     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5074     (when (gnus-virtual-group-p group)
5075       (setq cached gnus-newsgroup-cached))
5076
5077     (setq gnus-newsgroup-unreads
5078           (gnus-sorted-ndifference
5079            (gnus-sorted-ndifference gnus-newsgroup-unreads
5080                                     gnus-newsgroup-marked)
5081            gnus-newsgroup-dormant))
5082
5083     (setq gnus-newsgroup-processable nil)
5084
5085     (gnus-update-read-articles group gnus-newsgroup-unreads)
5086
5087     ;; Adjust and set lists of article marks.
5088     (when info
5089       (gnus-adjust-marked-articles info))
5090     (if (setq articles select-articles)
5091         (setq gnus-newsgroup-unselected
5092               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5093       (setq articles (gnus-articles-to-read group read-all)))
5094
5095     (cond
5096      ((null articles)
5097       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5098       'quit)
5099      ((eq articles 0) nil)
5100      (t
5101       ;; Init the dependencies hash table.
5102       (setq gnus-newsgroup-dependencies
5103             (gnus-make-hashtable (length articles)))
5104       (gnus-set-global-variables)
5105       ;; Retrieve the headers and read them in.
5106
5107       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5108
5109       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5110       (when cached
5111         (setq gnus-newsgroup-cached cached))
5112
5113       ;; Suppress duplicates?
5114       (when gnus-suppress-duplicates
5115         (gnus-dup-suppress-articles))
5116
5117       ;; Set the initial limit.
5118       (setq gnus-newsgroup-limit (copy-sequence articles))
5119       ;; Remove canceled articles from the list of unread articles.
5120       (setq fetched-articles
5121             (mapcar (lambda (headers) (mail-header-number headers))
5122                     gnus-newsgroup-headers))
5123       (setq gnus-newsgroup-articles fetched-articles)
5124       (setq gnus-newsgroup-unreads
5125             (gnus-sorted-nintersection
5126              gnus-newsgroup-unreads fetched-articles))
5127       (gnus-compute-unseen-list)
5128
5129       ;; Removed marked articles that do not exist.
5130       (gnus-update-missing-marks
5131        (gnus-sorted-difference articles fetched-articles))
5132       ;; We might want to build some more threads first.
5133       (when (and gnus-fetch-old-headers
5134                  (eq gnus-headers-retrieved-by 'nov))
5135         (if (eq gnus-fetch-old-headers 'invisible)
5136             (gnus-build-all-threads)
5137           (gnus-build-old-threads)))
5138       ;; Let the Gnus agent mark articles as read.
5139       (when gnus-agent
5140         (gnus-agent-get-undownloaded-list))
5141       ;; Remove list identifiers from subject
5142       (when gnus-list-identifiers
5143         (gnus-summary-remove-list-identifiers))
5144       ;; Check whether auto-expire is to be done in this group.
5145       (setq gnus-newsgroup-auto-expire
5146             (gnus-group-auto-expirable-p group))
5147       ;; Set up the article buffer now, if necessary.
5148       (unless gnus-single-article-buffer
5149         (gnus-article-setup-buffer))
5150       ;; First and last article in this newsgroup.
5151       (when gnus-newsgroup-headers
5152         (setq gnus-newsgroup-begin
5153               (mail-header-number (car gnus-newsgroup-headers))
5154               gnus-newsgroup-end
5155               (mail-header-number
5156                (gnus-last-element gnus-newsgroup-headers))))
5157       ;; GROUP is successfully selected.
5158       (or gnus-newsgroup-headers t)))))
5159
5160 (defun gnus-compute-unseen-list ()
5161   ;; The `seen' marks are treated specially.
5162   (if (not gnus-newsgroup-seen)
5163       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5164     (setq gnus-newsgroup-unseen
5165           (gnus-inverse-list-range-intersection
5166            gnus-newsgroup-articles gnus-newsgroup-seen))))
5167
5168 (defun gnus-summary-display-make-predicate (display)
5169   (require 'gnus-agent)
5170   (when (= (length display) 1)
5171     (setq display (car display)))
5172   (unless gnus-summary-display-cache
5173     (dolist (elem (append '((unread . unread)
5174                             (read . read)
5175                             (unseen . unseen))
5176                           gnus-article-mark-lists))
5177       (push (cons (cdr elem)
5178                   (gnus-byte-compile
5179                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5180             gnus-summary-display-cache)))
5181   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5182         (gnus-category-predicate-cache gnus-summary-display-cache))
5183     (gnus-get-predicate display)))
5184
5185 ;; Uses the dynamically bound `number' variable.
5186 (eval-when-compile
5187   (defvar number))
5188 (defun gnus-article-marked-p (type &optional article)
5189   (let ((article (or article number)))
5190     (cond
5191      ((eq type 'tick)
5192       (memq article gnus-newsgroup-marked))
5193      ((eq type 'spam)
5194       (memq article gnus-newsgroup-spam-marked))
5195      ((eq type 'unsend)
5196       (memq article gnus-newsgroup-unsendable))
5197      ((eq type 'undownload)
5198       (memq article gnus-newsgroup-undownloaded))
5199      ((eq type 'download)
5200       (memq article gnus-newsgroup-downloadable))
5201      ((eq type 'unread)
5202       (memq article gnus-newsgroup-unreads))
5203      ((eq type 'read)
5204       (memq article gnus-newsgroup-reads))
5205      ((eq type 'dormant)
5206       (memq article gnus-newsgroup-dormant) )
5207      ((eq type 'expire)
5208       (memq article gnus-newsgroup-expirable))
5209      ((eq type 'reply)
5210       (memq article gnus-newsgroup-replied))
5211      ((eq type 'killed)
5212       (memq article gnus-newsgroup-killed))
5213      ((eq type 'bookmark)
5214       (assq article gnus-newsgroup-bookmarks))
5215      ((eq type 'score)
5216       (assq article gnus-newsgroup-scored))
5217      ((eq type 'save)
5218       (memq article gnus-newsgroup-saved))
5219      ((eq type 'cache)
5220       (memq article gnus-newsgroup-cached))
5221      ((eq type 'forward)
5222       (memq article gnus-newsgroup-forwarded))
5223      ((eq type 'seen)
5224       (not (memq article gnus-newsgroup-unseen)))
5225      ((eq type 'recent)
5226       (memq article gnus-newsgroup-recent))
5227      (t t))))
5228
5229 (defun gnus-articles-to-read (group &optional read-all)
5230   "Find out what articles the user wants to read."
5231   (let* ((display (gnus-group-find-parameter group 'display))
5232          (articles
5233           ;; Select all articles if `read-all' is non-nil, or if there
5234           ;; are no unread articles.
5235           (if (or read-all
5236                   (and (zerop (length gnus-newsgroup-marked))
5237                        (zerop (length gnus-newsgroup-unreads)))
5238                   ;; Fetch all if the predicate is non-nil.
5239                   gnus-newsgroup-display)
5240               ;; We want to select the headers for all the articles in
5241               ;; the group, so we select either all the active
5242               ;; articles in the group, or (if that's nil), the
5243               ;; articles in the cache.
5244               (or
5245                (gnus-uncompress-range (gnus-active group))
5246                (gnus-cache-articles-in-group group))
5247             ;; Select only the "normal" subset of articles.
5248             (gnus-sorted-nunion
5249              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5250              gnus-newsgroup-unreads)))
5251          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5252          (scored (length scored-list))
5253          (number (length articles))
5254          (marked (+ (length gnus-newsgroup-marked)
5255                     (length gnus-newsgroup-dormant)))
5256          (select
5257           (cond
5258            ((numberp read-all)
5259             read-all)
5260            ((numberp gnus-newsgroup-display)
5261             gnus-newsgroup-display)
5262            (t
5263             (condition-case ()
5264                 (cond
5265                  ((and (or (<= scored marked) (= scored number))
5266                        (numberp gnus-large-newsgroup)
5267                        (> number gnus-large-newsgroup))
5268                   (let* ((cursor-in-echo-area nil)
5269                          (initial (gnus-parameter-large-newsgroup-initial
5270                                    gnus-newsgroup-name))
5271                          (input
5272                           (read-string
5273                            (format
5274                             "How many articles from %s (%s %d): "
5275                             (gnus-limit-string
5276                              (gnus-group-decoded-name gnus-newsgroup-name)
5277                              35)
5278                             (if initial "max" "default")
5279                             number)
5280                            (if initial
5281                                (cons (number-to-string initial)
5282                                      0)))))
5283                     (if (string-match "^[ \t]*$" input) number input)))
5284                  ((and (> scored marked) (< scored number)
5285                        (> (- scored number) 20))
5286                   (let ((input
5287                          (read-string
5288                           (format "%s %s (%d scored, %d total): "
5289                                   "How many articles from"
5290                                   (gnus-group-decoded-name group)
5291                                   scored number))))
5292                     (if (string-match "^[ \t]*$" input)
5293                         number input)))
5294                  (t number))
5295               (quit
5296                (message "Quit getting the articles to read")
5297                nil))))))
5298     (setq select (if (stringp select) (string-to-number select) select))
5299     (if (or (null select) (zerop select))
5300         select
5301       (if (and (not (zerop scored)) (<= (abs select) scored))
5302           (progn
5303             (setq articles (sort scored-list '<))
5304             (setq number (length articles)))
5305         (setq articles (copy-sequence articles)))
5306
5307       (when (< (abs select) number)
5308         (if (< select 0)
5309             ;; Select the N oldest articles.
5310             (setcdr (nthcdr (1- (abs select)) articles) nil)
5311           ;; Select the N most recent articles.
5312           (setq articles (nthcdr (- number select) articles))))
5313       (setq gnus-newsgroup-unselected
5314             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5315       (when gnus-alter-articles-to-read-function
5316         (setq articles
5317               (sort
5318                (funcall gnus-alter-articles-to-read-function
5319                         gnus-newsgroup-name articles)
5320                '<)))
5321       articles)))
5322
5323 (defun gnus-killed-articles (killed articles)
5324   (let (out)
5325     (while articles
5326       (when (inline (gnus-member-of-range (car articles) killed))
5327         (push (car articles) out))
5328       (setq articles (cdr articles)))
5329     out))
5330
5331 (defun gnus-uncompress-marks (marks)
5332   "Uncompress the mark ranges in MARKS."
5333   (let ((uncompressed '(score bookmark))
5334         out)
5335     (while marks
5336       (if (memq (caar marks) uncompressed)
5337           (push (car marks) out)
5338         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5339       (setq marks (cdr marks)))
5340     out))
5341
5342 (defun gnus-article-mark-to-type (mark)
5343   "Return the type of MARK."
5344   (or (cadr (assq mark gnus-article-special-mark-lists))
5345       'list))
5346
5347 (defun gnus-article-unpropagatable-p (mark)
5348   "Return whether MARK should be propagated to back end."
5349   (memq mark gnus-article-unpropagated-mark-lists))
5350
5351 (defun gnus-adjust-marked-articles (info)
5352   "Set all article lists and remove all marks that are no longer valid."
5353   (let* ((marked-lists (gnus-info-marks info))
5354          (active (gnus-active (gnus-info-group info)))
5355          (min (car active))
5356          (max (cdr active))
5357          (types gnus-article-mark-lists)
5358          marks var articles article mark mark-type)
5359
5360     (dolist (marks marked-lists)
5361       (setq mark (car marks)
5362             mark-type (gnus-article-mark-to-type mark)
5363             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5364
5365       ;; We set the variable according to the type of the marks list,
5366       ;; and then adjust the marks to a subset of the active articles.
5367       (cond
5368        ;; Adjust "simple" lists.
5369        ((eq mark-type 'list)
5370         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5371         (when (memq mark '(tick dormant expire reply save))
5372           (while articles
5373             (when (or (< (setq article (pop articles)) min) (> article max))
5374               (set var (delq article (symbol-value var)))))))
5375        ;; Adjust assocs.
5376        ((eq mark-type 'tuple)
5377         (set var (setq articles (cdr marks)))
5378         (when (not (listp (cdr (symbol-value var))))
5379           (set var (list (symbol-value var))))
5380         (when (not (listp (cdr articles)))
5381           (setq articles (list articles)))
5382         (while articles
5383           (when (or (not (consp (setq article (pop articles))))
5384                     (< (car article) min)
5385                     (> (car article) max))
5386             (set var (delq article (symbol-value var))))))
5387        ;; Adjust ranges (sloppily).
5388        ((eq mark-type 'range)
5389         (cond
5390          ((eq mark 'seen)
5391           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5392           ;; It should be (seen (NUM1 . NUM2)).
5393           (when (numberp (cddr marks))
5394             (setcdr marks (list (cdr marks))))
5395           (setq articles (cdr marks))
5396           (while (and articles
5397                       (or (and (consp (car articles))
5398                                (> min (cdar articles)))
5399                           (and (numberp (car articles))
5400                                (> min (car articles)))))
5401             (pop articles))
5402           (set var articles))))))))
5403
5404 (defun gnus-update-missing-marks (missing)
5405   "Go through the list of MISSING articles and remove them from the mark lists."
5406   (when missing
5407     (let (var m)
5408       ;; Go through all types.
5409       (dolist (elem gnus-article-mark-lists)
5410         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5411           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5412           (when (symbol-value var)
5413             ;; This list has articles.  So we delete all missing
5414             ;; articles from it.
5415             (setq m missing)
5416             (while m
5417               (set var (delq (pop m) (symbol-value var))))))))))
5418
5419 (defun gnus-update-marks ()
5420   "Enter the various lists of marked articles into the newsgroup info list."
5421   (let ((types gnus-article-mark-lists)
5422         (info (gnus-get-info gnus-newsgroup-name))
5423         type list newmarked symbol delta-marks)
5424     (when info
5425       ;; Add all marks lists to the list of marks lists.
5426       (while (setq type (pop types))
5427         (setq list (symbol-value
5428                     (setq symbol
5429                           (intern (format "gnus-newsgroup-%s" (car type))))))
5430
5431         (when list
5432           ;; Get rid of the entries of the articles that have the
5433           ;; default score.
5434           (when (and (eq (cdr type) 'score)
5435                      gnus-save-score
5436                      list)
5437             (let* ((arts list)
5438                    (prev (cons nil list))
5439                    (all prev))
5440               (while arts
5441                 (if (or (not (consp (car arts)))
5442                         (= (cdar arts) gnus-summary-default-score))
5443                     (setcdr prev (cdr arts))
5444                   (setq prev arts))
5445                 (setq arts (cdr arts)))
5446               (setq list (cdr all)))))
5447
5448         (when (eq (cdr type) 'seen)
5449           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5450
5451         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5452           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5453
5454         (when (and (gnus-check-backend-function
5455                     'request-set-mark gnus-newsgroup-name)
5456                    (not (gnus-article-unpropagatable-p (cdr type))))
5457           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5458                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5459                  (add (gnus-remove-from-range
5460                        (gnus-copy-sequence list) old)))
5461             (when add
5462               (push (list add 'add (list (cdr type))) delta-marks))
5463             (when del
5464               (push (list del 'del (list (cdr type))) delta-marks))))
5465
5466         (when list
5467           (push (cons (cdr type) list) newmarked)))
5468
5469       (when delta-marks
5470         (unless (gnus-check-group gnus-newsgroup-name)
5471           (error "Can't open server for %s" gnus-newsgroup-name))
5472         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5473
5474       ;; Enter these new marks into the info of the group.
5475       (if (nthcdr 3 info)
5476           (setcar (nthcdr 3 info) newmarked)
5477         ;; Add the marks lists to the end of the info.
5478         (when newmarked
5479           (setcdr (nthcdr 2 info) (list newmarked))))
5480
5481       ;; Cut off the end of the info if there's nothing else there.
5482       (let ((i 5))
5483         (while (and (> i 2)
5484                     (not (nth i info)))
5485           (when (nthcdr (decf i) info)
5486             (setcdr (nthcdr i info) nil)))))))
5487
5488 (defun gnus-set-mode-line (where)
5489   "Set the mode line of the article or summary buffers.
5490 If WHERE is `summary', the summary mode line format will be used."
5491   ;; Is this mode line one we keep updated?
5492   (when (and (memq where gnus-updated-mode-lines)
5493              (symbol-value
5494               (intern (format "gnus-%s-mode-line-format-spec" where))))
5495     (let (mode-string)
5496       (save-excursion
5497         ;; We evaluate this in the summary buffer since these
5498         ;; variables are buffer-local to that buffer.
5499         (set-buffer gnus-summary-buffer)
5500        ;; We bind all these variables that are used in the `eval' form
5501         ;; below.
5502         (let* ((mformat (symbol-value
5503                          (intern
5504                           (format "gnus-%s-mode-line-format-spec" where))))
5505                (gnus-tmp-group-name (gnus-group-decoded-name
5506                                      gnus-newsgroup-name))
5507                (gnus-tmp-article-number (or gnus-current-article 0))
5508                (gnus-tmp-unread gnus-newsgroup-unreads)
5509                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5510                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5511                (gnus-tmp-unread-and-unselected
5512                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5513                             (zerop gnus-tmp-unselected))
5514                        "")
5515                       ((zerop gnus-tmp-unselected)
5516                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5517                       (t (format "{%d(+%d) more}"
5518                                  gnus-tmp-unread-and-unticked
5519                                  gnus-tmp-unselected))))
5520                (gnus-tmp-subject
5521                 (if (and gnus-current-headers
5522                          (vectorp gnus-current-headers))
5523                     (gnus-mode-string-quote
5524                      (mail-header-subject gnus-current-headers))
5525                   ""))
5526                bufname-length max-len
5527                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5528           (setq mode-string (eval mformat))
5529           (setq bufname-length (if (string-match "%b" mode-string)
5530                                    (- (length
5531                                        (buffer-name
5532                                         (if (eq where 'summary)
5533                                             nil
5534                                           (get-buffer gnus-article-buffer))))
5535                                       2)
5536                                  0))
5537           (setq max-len (max 4 (if gnus-mode-non-string-length
5538                                    (- (window-width)
5539                                       gnus-mode-non-string-length
5540                                       bufname-length)
5541                                  (length mode-string))))
5542           ;; We might have to chop a bit of the string off...
5543           (when (> (length mode-string) max-len)
5544             (setq mode-string
5545                   (concat (truncate-string-to-width mode-string (- max-len 3))
5546                           "...")))
5547           ;; Pad the mode string a bit.
5548           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5549       ;; Update the mode line.
5550       (setq mode-line-buffer-identification
5551             (gnus-mode-line-buffer-identification (list mode-string)))
5552       (set-buffer-modified-p t))))
5553
5554 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5555   "Go through the HEADERS list and add all Xrefs to a hash table.
5556 The resulting hash table is returned, or nil if no Xrefs were found."
5557   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5558          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5559          (xref-hashtb (gnus-make-hashtable))
5560          start group entry number xrefs header)
5561     (while headers
5562       (setq header (pop headers))
5563       (when (and (setq xrefs (mail-header-xref header))
5564                  (not (memq (setq number (mail-header-number header))
5565                             unreads)))
5566         (setq start 0)
5567         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5568           (setq start (match-end 0))
5569           (setq group (if prefix
5570                           (concat prefix (substring xrefs (match-beginning 1)
5571                                                     (match-end 1)))
5572                         (substring xrefs (match-beginning 1) (match-end 1))))
5573           (setq number
5574                 (string-to-int (substring xrefs (match-beginning 2)
5575                                           (match-end 2))))
5576           (if (setq entry (gnus-gethash group xref-hashtb))
5577               (setcdr entry (cons number (cdr entry)))
5578             (gnus-sethash group (cons number nil) xref-hashtb)))))
5579     (and start xref-hashtb)))
5580
5581 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5582   "Look through all the headers and mark the Xrefs as read."
5583   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5584         name entry info xref-hashtb idlist method nth4)
5585     (save-excursion
5586       (set-buffer gnus-group-buffer)
5587       (when (setq xref-hashtb
5588                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5589         (mapatoms
5590          (lambda (group)
5591            (unless (string= from-newsgroup (setq name (symbol-name group)))
5592              (setq idlist (symbol-value group))
5593              ;; Dead groups are not updated.
5594              (and (prog1
5595                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5596                             info (nth 2 entry))
5597                     (when (stringp (setq nth4 (gnus-info-method info)))
5598                       (setq nth4 (gnus-server-to-method nth4))))
5599                   ;; Only do the xrefs if the group has the same
5600                   ;; select method as the group we have just read.
5601                   (or (gnus-methods-equal-p
5602                        nth4 (gnus-find-method-for-group from-newsgroup))
5603                       virtual
5604                       (equal nth4 (setq method (gnus-find-method-for-group
5605                                                 from-newsgroup)))
5606                       (and (equal (car nth4) (car method))
5607                            (equal (nth 1 nth4) (nth 1 method))))
5608                   gnus-use-cross-reference
5609                   (or (not (eq gnus-use-cross-reference t))
5610                       virtual
5611                       ;; Only do cross-references on subscribed
5612                       ;; groups, if that is what is wanted.
5613                       (<= (gnus-info-level info) gnus-level-subscribed))
5614                   (gnus-group-make-articles-read name idlist))))
5615          xref-hashtb)))))
5616
5617 (defun gnus-compute-read-articles (group articles)
5618   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5619          (info (nth 2 entry))
5620          (active (gnus-active group))
5621          ninfo)
5622     (when entry
5623       ;; First peel off all invalid article numbers.
5624       (when active
5625         (let ((ids articles)
5626               id first)
5627           (while (setq id (pop ids))
5628             (when (and first (> id (cdr active)))
5629               ;; We'll end up in this situation in one particular
5630               ;; obscure situation.  If you re-scan a group and get
5631               ;; a new article that is cross-posted to a different
5632               ;; group that has not been re-scanned, you might get
5633               ;; crossposted article that has a higher number than
5634               ;; Gnus believes possible.  So we re-activate this
5635               ;; group as well.  This might mean doing the
5636               ;; crossposting thingy will *increase* the number
5637               ;; of articles in some groups.  Tsk, tsk.
5638               (setq active (or (gnus-activate-group group) active)))
5639             (when (or (> id (cdr active))
5640                       (< id (car active)))
5641               (setq articles (delq id articles))))))
5642       ;; If the read list is nil, we init it.
5643       (if (and active
5644                (null (gnus-info-read info))
5645                (> (car active) 1))
5646           (setq ninfo (cons 1 (1- (car active))))
5647         (setq ninfo (gnus-info-read info)))
5648       ;; Then we add the read articles to the range.
5649       (gnus-add-to-range
5650        ninfo (setq articles (sort articles '<))))))
5651
5652 (defun gnus-group-make-articles-read (group articles)
5653   "Update the info of GROUP to say that ARTICLES are read."
5654   (let* ((num 0)
5655          (entry (gnus-gethash group gnus-newsrc-hashtb))
5656          (info (nth 2 entry))
5657          (active (gnus-active group))
5658          range)
5659     (when entry
5660       (setq range (gnus-compute-read-articles group articles))
5661       (save-excursion
5662         (set-buffer gnus-group-buffer)
5663         (gnus-undo-register
5664           `(progn
5665              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5666              (gnus-info-set-read ',info ',(gnus-info-read info))
5667              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5668              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5669              (gnus-group-update-group ,group t))))
5670       ;; Add the read articles to the range.
5671       (gnus-info-set-read info range)
5672       (gnus-request-set-mark group (list (list range 'add '(read))))
5673       ;; Then we have to re-compute how many unread
5674       ;; articles there are in this group.
5675       (when active
5676         (cond
5677          ((not range)
5678           (setq num (- (1+ (cdr active)) (car active))))
5679          ((not (listp (cdr range)))
5680           (setq num (- (cdr active) (- (1+ (cdr range))
5681                                        (car range)))))
5682          (t
5683           (while range
5684             (if (numberp (car range))
5685                 (setq num (1+ num))
5686               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5687             (setq range (cdr range)))
5688           (setq num (- (cdr active) num))))
5689         ;; Update the number of unread articles.
5690         (setcar entry num)
5691         ;; Update the group buffer.
5692         (unless (gnus-ephemeral-group-p group)
5693           (gnus-group-update-group group t))))))
5694
5695 (defvar gnus-newsgroup-none-id 0)
5696
5697 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5698   (let ((cur nntp-server-buffer)
5699         (dependencies
5700          (or dependencies
5701              (save-excursion (set-buffer gnus-summary-buffer)
5702                              gnus-newsgroup-dependencies)))
5703         headers id end ref
5704         (mail-parse-charset gnus-newsgroup-charset)
5705         (mail-parse-ignored-charsets
5706          (save-excursion (condition-case nil
5707                              (set-buffer gnus-summary-buffer)
5708                            (error))
5709                          gnus-newsgroup-ignored-charsets)))
5710     (save-excursion
5711       (set-buffer nntp-server-buffer)
5712       ;; Translate all TAB characters into SPACE characters.
5713       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5714       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5715       (ietf-drums-unfold-fws)
5716       (gnus-run-hooks 'gnus-parse-headers-hook)
5717       (let ((case-fold-search t)
5718             in-reply-to header p lines chars)
5719         (goto-char (point-min))
5720         ;; Search to the beginning of the next header.  Error messages
5721         ;; do not begin with 2 or 3.
5722         (while (re-search-forward "^[23][0-9]+ " nil t)
5723           (setq id nil
5724                 ref nil)
5725           ;; This implementation of this function, with nine
5726           ;; search-forwards instead of the one re-search-forward and
5727           ;; a case (which basically was the old function) is actually
5728           ;; about twice as fast, even though it looks messier.  You
5729           ;; can't have everything, I guess.  Speed and elegance
5730           ;; doesn't always go hand in hand.
5731           (setq
5732            header
5733            (vector
5734             ;; Number.
5735             (prog1
5736                 (read cur)
5737               (end-of-line)
5738               (setq p (point))
5739               (narrow-to-region (point)
5740                                 (or (and (search-forward "\n.\n" nil t)
5741                                          (- (point) 2))
5742                                     (point))))
5743             ;; Subject.
5744             (progn
5745               (goto-char p)
5746               (if (search-forward "\nsubject:" nil t)
5747                   (funcall gnus-decode-encoded-word-function
5748                            (nnheader-header-value))
5749                 "(none)"))
5750             ;; From.
5751             (progn
5752               (goto-char p)
5753               (if (search-forward "\nfrom:" nil t)
5754                   (funcall gnus-decode-encoded-word-function
5755                            (nnheader-header-value))
5756                 "(nobody)"))
5757             ;; Date.
5758             (progn
5759               (goto-char p)
5760               (if (search-forward "\ndate:" nil t)
5761                   (nnheader-header-value) ""))
5762             ;; Message-ID.
5763             (progn
5764               (goto-char p)
5765               (setq id (if (re-search-forward
5766                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5767                            ;; We do it this way to make sure the Message-ID
5768                            ;; is (somewhat) syntactically valid.
5769                            (buffer-substring (match-beginning 1)
5770                                              (match-end 1))
5771                          ;; If there was no message-id, we just fake one
5772                          ;; to make subsequent routines simpler.
5773                          (nnheader-generate-fake-message-id))))
5774             ;; References.
5775             (progn
5776               (goto-char p)
5777               (if (search-forward "\nreferences:" nil t)
5778                   (progn
5779                     (setq end (point))
5780                     (prog1
5781                         (nnheader-header-value)
5782                       (setq ref
5783                             (buffer-substring
5784                              (progn
5785                                (end-of-line)
5786                                (search-backward ">" end t)
5787                                (1+ (point)))
5788                              (progn
5789                                (search-backward "<" end t)
5790                                (point))))))
5791                 ;; Get the references from the in-reply-to header if there
5792                 ;; were no references and the in-reply-to header looks
5793                 ;; promising.
5794                 (if (and (search-forward "\nin-reply-to:" nil t)
5795                          (setq in-reply-to (nnheader-header-value))
5796                          (string-match "<[^>]+>" in-reply-to))
5797                     (let (ref2)
5798                       (setq ref (substring in-reply-to (match-beginning 0)
5799                                            (match-end 0)))
5800                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5801                         (setq ref2 (substring in-reply-to (match-beginning 0)
5802                                               (match-end 0)))
5803                         (when (> (length ref2) (length ref))
5804                           (setq ref ref2)))
5805                       ref)
5806                   (setq ref nil))))
5807             ;; Chars.
5808             (progn
5809               (goto-char p)
5810               (if (search-forward "\nchars: " nil t)
5811                   (if (numberp (setq chars (ignore-errors (read cur))))
5812                       chars -1)
5813                 -1))
5814             ;; Lines.
5815             (progn
5816               (goto-char p)
5817               (if (search-forward "\nlines: " nil t)
5818                   (if (numberp (setq lines (ignore-errors (read cur))))
5819                       lines -1)
5820                 -1))
5821             ;; Xref.
5822             (progn
5823               (goto-char p)
5824               (and (search-forward "\nxref:" nil t)
5825                    (nnheader-header-value)))
5826             ;; Extra.
5827             (when gnus-extra-headers
5828               (let ((extra gnus-extra-headers)
5829                     out)
5830                 (while extra
5831                   (goto-char p)
5832                   (when (search-forward
5833                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5834                     (push (cons (car extra) (nnheader-header-value))
5835                           out))
5836                   (pop extra))
5837                 out))))
5838           (when (equal id ref)
5839             (setq ref nil))
5840
5841           (when gnus-alter-header-function
5842             (funcall gnus-alter-header-function header)
5843             (setq id (mail-header-id header)
5844                   ref (gnus-parent-id (mail-header-references header))))
5845
5846           (when (setq header
5847                       (gnus-dependencies-add-header
5848                        header dependencies force-new))
5849             (push header headers))
5850           (goto-char (point-max))
5851           (widen))
5852         (nreverse headers)))))
5853
5854 ;; Goes through the xover lines and returns a list of vectors
5855 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5856                                                   force-new dependencies
5857                                                   group also-fetch-heads)
5858   "Parse the news overview data in the server buffer.
5859 Return a list of headers that match SEQUENCE (see
5860 `nntp-retrieve-headers')."
5861   ;; Get the Xref when the users reads the articles since most/some
5862   ;; NNTP servers do not include Xrefs when using XOVER.
5863   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5864   (let ((mail-parse-charset gnus-newsgroup-charset)
5865         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5866         (cur nntp-server-buffer)
5867         (dependencies (or dependencies gnus-newsgroup-dependencies))
5868         (allp (cond
5869                ((eq gnus-read-all-available-headers t)
5870                 t)
5871                ((stringp gnus-read-all-available-headers)
5872                 (string-match gnus-read-all-available-headers group))
5873                (t
5874                 nil)))
5875         number headers header)
5876     (save-excursion
5877       (set-buffer nntp-server-buffer)
5878       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5879       ;; Allow the user to mangle the headers before parsing them.
5880       (gnus-run-hooks 'gnus-parse-headers-hook)
5881       (goto-char (point-min))
5882       (gnus-parse-without-error
5883         (while (and (or sequence allp)
5884                     (not (eobp)))
5885           (setq number (read cur))
5886           (when (not allp)
5887             (while (and sequence
5888                         (< (car sequence) number))
5889               (setq sequence (cdr sequence))))
5890           (when (and (or allp
5891                          (and sequence
5892                               (eq number (car sequence))))
5893                      (progn
5894                        (setq sequence (cdr sequence))
5895                        (setq header (inline
5896                                       (gnus-nov-parse-line
5897                                        number dependencies force-new)))))
5898             (push header headers))
5899           (forward-line 1)))
5900       ;; A common bug in inn is that if you have posted an article and
5901       ;; then retrieves the active file, it will answer correctly --
5902       ;; the new article is included.  However, a NOV entry for the
5903       ;; article may not have been generated yet, so this may fail.
5904       ;; We work around this problem by retrieving the last few
5905       ;; headers using HEAD.
5906       (if (or (not also-fetch-heads)
5907               (not sequence))
5908           ;; We (probably) got all the headers.
5909           (nreverse headers)
5910         (let ((gnus-nov-is-evil t))
5911           (nconc
5912            (nreverse headers)
5913            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5914              (gnus-get-newsgroup-headers))))))))
5915
5916 (defun gnus-article-get-xrefs ()
5917   "Fill in the Xref value in `gnus-current-headers', if necessary.
5918 This is meant to be called in `gnus-article-internal-prepare-hook'."
5919   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5920                                  gnus-current-headers)))
5921     (or (not gnus-use-cross-reference)
5922         (not headers)
5923         (and (mail-header-xref headers)
5924              (not (string= (mail-header-xref headers) "")))
5925         (let ((case-fold-search t)
5926               xref)
5927           (save-restriction
5928             (nnheader-narrow-to-headers)
5929             (goto-char (point-min))
5930             (when (or (and (not (eobp))
5931                            (eq (downcase (char-after)) ?x)
5932                            (looking-at "Xref:"))
5933                       (search-forward "\nXref:" nil t))
5934               (goto-char (1+ (match-end 0)))
5935               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5936               (mail-header-set-xref headers xref)))))))
5937
5938 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5939   "Find article ID and insert the summary line for that article.
5940 OLD-HEADER can either be a header or a line number to insert
5941 the subject line on."
5942   (let* ((line (and (numberp old-header) old-header))
5943          (old-header (and (vectorp old-header) old-header))
5944          (header (cond ((and old-header use-old-header)
5945                         old-header)
5946                        ((and (numberp id)
5947                              (gnus-number-to-header id))
5948                         (gnus-number-to-header id))
5949                        (t
5950                         (gnus-read-header id))))
5951          (number (and (numberp id) id))
5952          d)
5953     (when header
5954       ;; Rebuild the thread that this article is part of and go to the
5955       ;; article we have fetched.
5956       (when (and (not gnus-show-threads)
5957                  old-header)
5958         (when (and number
5959                    (setq d (gnus-data-find (mail-header-number old-header))))
5960           (goto-char (gnus-data-pos d))
5961           (gnus-data-remove
5962            number
5963            (- (gnus-point-at-bol)
5964               (prog1
5965                   (1+ (gnus-point-at-eol))
5966                 (gnus-delete-line))))))
5967       (when old-header
5968         (mail-header-set-number header (mail-header-number old-header)))
5969       (setq gnus-newsgroup-sparse
5970             (delq (setq number (mail-header-number header))
5971                   gnus-newsgroup-sparse))
5972       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5973       (push number gnus-newsgroup-limit)
5974       (gnus-rebuild-thread (mail-header-id header) line)
5975       (gnus-summary-goto-subject number nil t))
5976     (when (and (numberp number)
5977                (> number 0))
5978       ;; We have to update the boundaries even if we can't fetch the
5979       ;; article if ID is a number -- so that the next `P' or `N'
5980       ;; command will fetch the previous (or next) article even
5981       ;; if the one we tried to fetch this time has been canceled.
5982       (when (> number gnus-newsgroup-end)
5983         (setq gnus-newsgroup-end number))
5984       (when (< number gnus-newsgroup-begin)
5985         (setq gnus-newsgroup-begin number))
5986       (setq gnus-newsgroup-unselected
5987             (delq number gnus-newsgroup-unselected)))
5988     ;; Report back a success?
5989     (and header (mail-header-number header))))
5990
5991 ;;; Process/prefix in the summary buffer
5992
5993 (defun gnus-summary-work-articles (n)
5994   "Return a list of articles to be worked upon.
5995 The prefix argument, the list of process marked articles, and the
5996 current article will be taken into consideration."
5997   (save-excursion
5998     (set-buffer gnus-summary-buffer)
5999     (cond
6000      (n
6001       ;; A numerical prefix has been given.
6002       (setq n (prefix-numeric-value n))
6003       (let ((backward (< n 0))
6004             (n (abs (prefix-numeric-value n)))
6005             articles article)
6006         (save-excursion
6007           (while
6008               (and (> n 0)
6009                    (push (setq article (gnus-summary-article-number))
6010                          articles)
6011                    (if backward
6012                        (gnus-summary-find-prev nil article)
6013                      (gnus-summary-find-next nil article)))
6014             (decf n)))
6015         (nreverse articles)))
6016      ((and (gnus-region-active-p) (mark))
6017       (message "region active")
6018       ;; Work on the region between point and mark.
6019       (let ((max (max (point) (mark)))
6020             articles article)
6021         (save-excursion
6022           (goto-char (min (min (point) (mark))))
6023           (while
6024               (and
6025                (push (setq article (gnus-summary-article-number)) articles)
6026                (gnus-summary-find-next nil article)
6027                (< (point) max)))
6028           (nreverse articles))))
6029      (gnus-newsgroup-processable
6030       ;; There are process-marked articles present.
6031       ;; Save current state.
6032       (gnus-summary-save-process-mark)
6033       ;; Return the list.
6034       (reverse gnus-newsgroup-processable))
6035      (t
6036       ;; Just return the current article.
6037       (list (gnus-summary-article-number))))))
6038
6039 (defmacro gnus-summary-iterate (arg &rest forms)
6040   "Iterate over the process/prefixed articles and do FORMS.
6041 ARG is the interactive prefix given to the command.  FORMS will be
6042 executed with point over the summary line of the articles."
6043   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6044     `(let ((,articles (gnus-summary-work-articles ,arg)))
6045        (while ,articles
6046          (gnus-summary-goto-subject (car ,articles))
6047          ,@forms
6048          (pop ,articles)))))
6049
6050 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6051 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6052
6053 (defun gnus-summary-save-process-mark ()
6054   "Push the current set of process marked articles on the stack."
6055   (interactive)
6056   (push (copy-sequence gnus-newsgroup-processable)
6057         gnus-newsgroup-process-stack))
6058
6059 (defun gnus-summary-kill-process-mark ()
6060   "Push the current set of process marked articles on the stack and unmark."
6061   (interactive)
6062   (gnus-summary-save-process-mark)
6063   (gnus-summary-unmark-all-processable))
6064
6065 (defun gnus-summary-yank-process-mark ()
6066   "Pop the last process mark state off the stack and restore it."
6067   (interactive)
6068   (unless gnus-newsgroup-process-stack
6069     (error "Empty mark stack"))
6070   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6071
6072 (defun gnus-summary-process-mark-set (set)
6073   "Make SET into the current process marked articles."
6074   (gnus-summary-unmark-all-processable)
6075   (while set
6076     (gnus-summary-set-process-mark (pop set))))
6077
6078 ;;; Searching and stuff
6079
6080 (defun gnus-summary-search-group (&optional backward use-level)
6081   "Search for next unread newsgroup.
6082 If optional argument BACKWARD is non-nil, search backward instead."
6083   (save-excursion
6084     (set-buffer gnus-group-buffer)
6085     (when (gnus-group-search-forward
6086            backward nil (if use-level (gnus-group-group-level) nil))
6087       (gnus-group-group-name))))
6088
6089 (defun gnus-summary-best-group (&optional exclude-group)
6090   "Find the name of the best unread group.
6091 If EXCLUDE-GROUP, do not go to this group."
6092   (save-excursion
6093     (set-buffer gnus-group-buffer)
6094     (save-excursion
6095       (gnus-group-best-unread-group exclude-group))))
6096
6097 (defun gnus-summary-find-next (&optional unread article backward)
6098   (if backward
6099       (gnus-summary-find-prev unread article)
6100     (let* ((dummy (gnus-summary-article-intangible-p))
6101            (article (or article (gnus-summary-article-number)))
6102            (data (gnus-data-find-list article))
6103            result)
6104       (when (and (not dummy)
6105                  (or (not gnus-summary-check-current)
6106                      (not unread)
6107                      (not (gnus-data-unread-p (car data)))))
6108         (setq data (cdr data)))
6109       (when (setq result
6110                   (if unread
6111                       (progn
6112                         (while data
6113                           (unless (memq (gnus-data-number (car data))
6114                                         (cond
6115                                          ((eq gnus-auto-goto-ignores
6116                                               'always-undownloaded)
6117                                           gnus-newsgroup-undownloaded)
6118                                          (gnus-plugged
6119                                           nil)
6120                                          ((eq gnus-auto-goto-ignores
6121                                               'unfetched)
6122                                           gnus-newsgroup-unfetched)
6123                                          ((eq gnus-auto-goto-ignores
6124                                               'undownloaded)
6125                                           gnus-newsgroup-undownloaded)))
6126                             (when (gnus-data-unread-p (car data))
6127                               (setq result (car data)
6128                                     data nil)))
6129                           (setq data (cdr data)))
6130                         result)
6131                     (car data)))
6132         (goto-char (gnus-data-pos result))
6133         (gnus-data-number result)))))
6134
6135 (defun gnus-summary-find-prev (&optional unread article)
6136   (let* ((eobp (eobp))
6137          (article (or article (gnus-summary-article-number)))
6138          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6139          result)
6140     (when (and (not eobp)
6141                (or (not gnus-summary-check-current)
6142                    (not unread)
6143                    (not (gnus-data-unread-p (car data)))))
6144       (setq data (cdr data)))
6145     (when (setq result
6146                 (if unread
6147                     (progn
6148                       (while data
6149                         (unless (memq (gnus-data-number (car data))
6150                                       (cond
6151                                        ((eq gnus-auto-goto-ignores
6152                                             'always-undownloaded)
6153                                         gnus-newsgroup-undownloaded)
6154                                        (gnus-plugged
6155                                         nil)
6156                                        ((eq gnus-auto-goto-ignores
6157                                             'unfetched)
6158                                         gnus-newsgroup-unfetched)
6159                                        ((eq gnus-auto-goto-ignores
6160                                             'undownloaded)
6161                                         gnus-newsgroup-undownloaded)))
6162                           (when (gnus-data-unread-p (car data))
6163                             (setq result (car data)
6164                                   data nil)))
6165                         (setq data (cdr data)))
6166                       result)
6167                   (car data)))
6168       (goto-char (gnus-data-pos result))
6169       (gnus-data-number result))))
6170
6171 (defun gnus-summary-find-subject (subject &optional unread backward article)
6172   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6173          (article (or article (gnus-summary-article-number)))
6174          (articles (gnus-data-list backward))
6175          (arts (gnus-data-find-list article articles))
6176          result)
6177     (when (or (not gnus-summary-check-current)
6178               (not unread)
6179               (not (gnus-data-unread-p (car arts))))
6180       (setq arts (cdr arts)))
6181     (while arts
6182       (and (or (not unread)
6183                (gnus-data-unread-p (car arts)))
6184            (vectorp (gnus-data-header (car arts)))
6185            (gnus-subject-equal
6186             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6187            (setq result (car arts)
6188                  arts nil))
6189       (setq arts (cdr arts)))
6190     (and result
6191          (goto-char (gnus-data-pos result))
6192          (gnus-data-number result))))
6193
6194 (defun gnus-summary-search-forward (&optional unread subject backward)
6195   "Search forward for an article.
6196 If UNREAD, look for unread articles.  If SUBJECT, look for
6197 articles with that subject.  If BACKWARD, search backward instead."
6198   (cond (subject (gnus-summary-find-subject subject unread backward))
6199         (backward (gnus-summary-find-prev unread))
6200         (t (gnus-summary-find-next unread))))
6201
6202 (defun gnus-recenter (&optional n)
6203   "Center point in window and redisplay frame.
6204 Also do horizontal recentering."
6205   (interactive "P")
6206   (when (and gnus-auto-center-summary
6207              (not (eq gnus-auto-center-summary 'vertical)))
6208     (gnus-horizontal-recenter))
6209   (recenter n))
6210
6211 (defun gnus-summary-recenter ()
6212   "Center point in the summary window.
6213 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6214 displayed, no centering will be performed."
6215   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6216   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6217   (interactive)
6218   ;; The user has to want it.
6219   (when gnus-auto-center-summary
6220     (let* ((top (cond ((< (window-height) 4) 0)
6221                       ((< (window-height) 7) 1)
6222                       (t (if (numberp gnus-auto-center-summary)
6223                              gnus-auto-center-summary
6224                            2))))
6225            (height (1- (window-height)))
6226            (bottom (save-excursion (goto-char (point-max))
6227                                    (forward-line (- height))
6228                                    (point)))
6229            (window (get-buffer-window (current-buffer))))
6230       (when (get-buffer-window gnus-article-buffer)
6231         ;; Only do recentering when the article buffer is displayed,
6232         ;; Set the window start to either `bottom', which is the biggest
6233         ;; possible valid number, or the second line from the top,
6234         ;; whichever is the least.
6235         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6236           (if (> bottom top-pos)
6237               ;; Keep the second line from the top visible
6238               (set-window-start window top-pos t)
6239             ;; Try to keep the bottom line visible; if it's partially
6240             ;; obscured, either scroll one more line to make it fully
6241             ;; visible, or revert to using TOP-POS.
6242             (save-excursion
6243               (goto-char (point-max))
6244               (forward-line -1)
6245               (let ((last-line-start (point)))
6246                 (goto-char bottom)
6247                 (set-window-start window (point) t)
6248                 (when (not (pos-visible-in-window-p last-line-start window))
6249                   (forward-line 1)
6250                   (set-window-start window (min (point) top-pos) t)))))))
6251       ;; Do horizontal recentering while we're at it.
6252       (when (and (get-buffer-window (current-buffer) t)
6253                  (not (eq gnus-auto-center-summary 'vertical)))
6254         (let ((selected (selected-window)))
6255           (select-window (get-buffer-window (current-buffer) t))
6256           (gnus-summary-position-point)
6257           (gnus-horizontal-recenter)
6258           (select-window selected))))))
6259
6260 (defun gnus-summary-jump-to-group (newsgroup)
6261   "Move point to NEWSGROUP in group mode buffer."
6262   ;; Keep update point of group mode buffer if visible.
6263   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6264       (save-window-excursion
6265         ;; Take care of tree window mode.
6266         (when (get-buffer-window gnus-group-buffer)
6267           (pop-to-buffer gnus-group-buffer))
6268         (gnus-group-jump-to-group newsgroup))
6269     (save-excursion
6270       ;; Take care of tree window mode.
6271       (if (get-buffer-window gnus-group-buffer)
6272           (pop-to-buffer gnus-group-buffer)
6273         (set-buffer gnus-group-buffer))
6274       (gnus-group-jump-to-group newsgroup))))
6275
6276 ;; This function returns a list of article numbers based on the
6277 ;; difference between the ranges of read articles in this group and
6278 ;; the range of active articles.
6279 (defun gnus-list-of-unread-articles (group)
6280   (let* ((read (gnus-info-read (gnus-get-info group)))
6281          (active (or (gnus-active group) (gnus-activate-group group)))
6282          (last (cdr active))
6283          first nlast unread)
6284     ;; If none are read, then all are unread.
6285     (if (not read)
6286         (setq first (car active))
6287       ;; If the range of read articles is a single range, then the
6288       ;; first unread article is the article after the last read
6289       ;; article.  Sounds logical, doesn't it?
6290       (if (and (not (listp (cdr read)))
6291                (or (< (car read) (car active))
6292                    (progn (setq read (list read))
6293                           nil)))
6294           (setq first (max (car active) (1+ (cdr read))))
6295         ;; `read' is a list of ranges.
6296         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6297                                   (caar read)))
6298                   1)
6299           (setq first (car active)))
6300         (while read
6301           (when first
6302             (while (< first nlast)
6303               (push first unread)
6304               (setq first (1+ first))))
6305           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6306           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6307           (setq read (cdr read)))))
6308     ;; And add the last unread articles.
6309     (while (<= first last)
6310       (push first unread)
6311       (setq first (1+ first)))
6312     ;; Return the list of unread articles.
6313     (delq 0 (nreverse unread))))
6314
6315 (defun gnus-list-of-read-articles (group)
6316   "Return a list of unread, unticked and non-dormant articles."
6317   (let* ((info (gnus-get-info group))
6318          (marked (gnus-info-marks info))
6319          (active (gnus-active group)))
6320     (and info active
6321          (gnus-list-range-difference
6322           (gnus-list-range-difference
6323            (gnus-sorted-complement
6324             (gnus-uncompress-range active)
6325             (gnus-list-of-unread-articles group))
6326            (cdr (assq 'dormant marked)))
6327           (cdr (assq 'tick marked))))))
6328
6329 ;; Various summary commands
6330
6331 (defun gnus-summary-select-article-buffer ()
6332   "Reconfigure windows to show article buffer."
6333   (interactive)
6334   (if (not (gnus-buffer-live-p gnus-article-buffer))
6335       (error "There is no article buffer for this summary buffer")
6336     (gnus-configure-windows 'article)
6337     (select-window (get-buffer-window gnus-article-buffer))))
6338
6339 (defun gnus-summary-universal-argument (arg)
6340   "Perform any operation on all articles that are process/prefixed."
6341   (interactive "P")
6342   (let ((articles (gnus-summary-work-articles arg))
6343         func article)
6344     (if (eq
6345          (setq
6346           func
6347           (key-binding
6348            (read-key-sequence
6349             (substitute-command-keys
6350              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6351          'undefined)
6352         (gnus-error 1 "Undefined key")
6353       (save-excursion
6354         (while articles
6355           (gnus-summary-goto-subject (setq article (pop articles)))
6356           (let (gnus-newsgroup-processable)
6357             (command-execute func))
6358           (gnus-summary-remove-process-mark article)))))
6359   (gnus-summary-position-point))
6360
6361 (defun gnus-summary-toggle-truncation (&optional arg)
6362   "Toggle truncation of summary lines.
6363 With ARG, turn line truncation on if ARG is positive."
6364   (interactive "P")
6365   (setq truncate-lines
6366         (if (null arg) (not truncate-lines)
6367           (> (prefix-numeric-value arg) 0)))
6368   (redraw-display))
6369
6370 (defun gnus-summary-find-for-reselect ()
6371   "Return the number of an article to stay on across a reselect.
6372 The current article is considered, then following articles, then previous
6373 articles.  An article is sought which is not cancelled and isn't a temporary
6374 insertion from another group.  If there's no such then return a dummy 0."
6375   (let (found)
6376     (dolist (rev '(nil t))
6377       (unless found      ; don't demand the reverse list if we don't need it
6378         (let ((data (gnus-data-find-list
6379                      (gnus-summary-article-number) (gnus-data-list rev))))
6380           (while (and data (not found))
6381             (if (and (< 0 (gnus-data-number (car data)))
6382                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6383                 (setq found (gnus-data-number (car data))))
6384             (setq data (cdr data))))))
6385     (or found 0)))
6386
6387 (defun gnus-summary-reselect-current-group (&optional all rescan)
6388   "Exit and then reselect the current newsgroup.
6389 The prefix argument ALL means to select all articles."
6390   (interactive "P")
6391   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6392     (error "Ephemeral groups can't be reselected"))
6393   (let ((current-subject (gnus-summary-find-for-reselect))
6394         (group gnus-newsgroup-name))
6395     (setq gnus-newsgroup-begin nil)
6396     (gnus-summary-exit nil 'leave-hidden)
6397     ;; We have to adjust the point of group mode buffer because
6398     ;; point was moved to the next unread newsgroup by exiting.
6399     (gnus-summary-jump-to-group group)
6400     (when rescan
6401       (save-excursion
6402         (gnus-group-get-new-news-this-group 1)))
6403     (gnus-group-read-group all t)
6404     (gnus-summary-goto-subject current-subject nil t)))
6405
6406 (defun gnus-summary-rescan-group (&optional all)
6407   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6408   (interactive "P")
6409   (gnus-summary-reselect-current-group all t))
6410
6411 (defun gnus-summary-update-info (&optional non-destructive)
6412   (save-excursion
6413     (let ((group gnus-newsgroup-name))
6414       (when group
6415         (when gnus-newsgroup-kill-headers
6416           (setq gnus-newsgroup-killed
6417                 (gnus-compress-sequence
6418                  (gnus-sorted-union
6419                   (gnus-list-range-intersection
6420                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6421                   gnus-newsgroup-unreads)
6422                  t)))
6423         (unless (listp (cdr gnus-newsgroup-killed))
6424           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6425         (let ((headers gnus-newsgroup-headers))
6426           ;; Set the new ranges of read articles.
6427           (save-excursion
6428             (set-buffer gnus-group-buffer)
6429             (gnus-undo-force-boundary))
6430           (gnus-update-read-articles
6431            group (gnus-sorted-union
6432                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6433           ;; Set the current article marks.
6434           (let ((gnus-newsgroup-scored
6435                  (if (and (not gnus-save-score)
6436                           (not non-destructive))
6437                      nil
6438                    gnus-newsgroup-scored)))
6439             (save-excursion
6440               (gnus-update-marks)))
6441           ;; Do the cross-ref thing.
6442           (when gnus-use-cross-reference
6443             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6444           ;; Do not switch windows but change the buffer to work.
6445           (set-buffer gnus-group-buffer)
6446           (unless (gnus-ephemeral-group-p group)
6447             (gnus-group-update-group group)))))))
6448
6449 (defun gnus-summary-save-newsrc (&optional force)
6450   "Save the current number of read/marked articles in the dribble buffer.
6451 The dribble buffer will then be saved.
6452 If FORCE (the prefix), also save the .newsrc file(s)."
6453   (interactive "P")
6454   (gnus-summary-update-info t)
6455   (if force
6456       (gnus-save-newsrc-file)
6457     (gnus-dribble-save)))
6458
6459 (defun gnus-summary-exit (&optional temporary leave-hidden)
6460   "Exit reading current newsgroup, and then return to group selection mode.
6461 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6462   (interactive)
6463   (gnus-set-global-variables)
6464   (when (gnus-buffer-live-p gnus-article-buffer)
6465     (save-excursion
6466       (set-buffer gnus-article-buffer)
6467       (mm-destroy-parts gnus-article-mime-handles)
6468       ;; Set it to nil for safety reason.
6469       (setq gnus-article-mime-handle-alist nil)
6470       (setq gnus-article-mime-handles nil)))
6471   (gnus-kill-save-kill-buffer)
6472   (gnus-async-halt-prefetch)
6473   (let* ((group gnus-newsgroup-name)
6474          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6475          (gnus-group-is-exiting-p t)
6476          (mode major-mode)
6477          (group-point nil)
6478          (buf (current-buffer)))
6479     (unless quit-config
6480       ;; Do adaptive scoring, and possibly save score files.
6481       (when gnus-newsgroup-adaptive
6482         (gnus-score-adaptive))
6483       (when gnus-use-scoring
6484         (gnus-score-save)))
6485     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6486     ;; If we have several article buffers, we kill them at exit.
6487     (unless gnus-single-article-buffer
6488       (gnus-kill-buffer gnus-original-article-buffer)
6489       (setq gnus-article-current nil))
6490     (when gnus-use-cache
6491       (gnus-cache-possibly-remove-articles)
6492       (gnus-cache-save-buffers))
6493     (gnus-async-prefetch-remove-group group)
6494     (when gnus-suppress-duplicates
6495       (gnus-dup-enter-articles))
6496     (when gnus-use-trees
6497       (gnus-tree-close group))
6498     (when gnus-use-cache
6499       (gnus-cache-write-active))
6500     ;; Remove entries for this group.
6501     (nnmail-purge-split-history (gnus-group-real-name group))
6502     ;; Make all changes in this group permanent.
6503     (unless quit-config
6504       (gnus-run-hooks 'gnus-exit-group-hook)
6505       (gnus-summary-update-info))
6506     (gnus-close-group group)
6507     ;; Make sure where we were, and go to next newsgroup.
6508     (set-buffer gnus-group-buffer)
6509     (unless quit-config
6510       (gnus-group-jump-to-group group))
6511     (gnus-run-hooks 'gnus-summary-exit-hook)
6512     (unless (or quit-config
6513                 ;; If this group has disappeared from the summary
6514                 ;; buffer, don't skip forwards.
6515                 (not (string= group (gnus-group-group-name))))
6516       (gnus-group-next-unread-group 1))
6517     (setq group-point (point))
6518     (if temporary
6519         nil                             ;Nothing to do.
6520       ;; If we have several article buffers, we kill them at exit.
6521       (unless gnus-single-article-buffer
6522         (gnus-kill-buffer gnus-article-buffer)
6523         (gnus-kill-buffer gnus-original-article-buffer)
6524         (setq gnus-article-current nil))
6525       (set-buffer buf)
6526       (if (not gnus-kill-summary-on-exit)
6527           (progn
6528             (gnus-deaden-summary)
6529             (setq mode nil))
6530         ;; We set all buffer-local variables to nil.  It is unclear why
6531         ;; this is needed, but if we don't, buffer-local variables are
6532         ;; not garbage-collected, it seems.  This would the lead to en
6533         ;; ever-growing Emacs.
6534         (gnus-summary-clear-local-variables)
6535         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6536           (gnus-summary-clear-local-variables))
6537         (when (get-buffer gnus-article-buffer)
6538           (bury-buffer gnus-article-buffer))
6539         ;; We clear the global counterparts of the buffer-local
6540         ;; variables as well, just to be on the safe side.
6541         (set-buffer gnus-group-buffer)
6542         (gnus-summary-clear-local-variables)
6543         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6544           (gnus-summary-clear-local-variables))
6545         ;; Return to group mode buffer.
6546         (when (eq mode 'gnus-summary-mode)
6547           (gnus-kill-buffer buf)))
6548       (setq gnus-current-select-method gnus-select-method)
6549       (if leave-hidden
6550           (set-buffer gnus-group-buffer)
6551         (pop-to-buffer gnus-group-buffer))
6552       (if (not quit-config)
6553           (progn
6554             (goto-char group-point)
6555             (unless leave-hidden
6556               (gnus-configure-windows 'group 'force)))
6557         (gnus-handle-ephemeral-exit quit-config))
6558       ;; Clear the current group name.
6559       (unless quit-config
6560         (setq gnus-newsgroup-name nil)))))
6561
6562 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6563 (defun gnus-summary-exit-no-update (&optional no-questions)
6564   "Quit reading current newsgroup without updating read article info."
6565   (interactive)
6566   (let* ((group gnus-newsgroup-name)
6567          (gnus-group-is-exiting-p t)
6568          (gnus-group-is-exiting-without-update-p t)
6569          (quit-config (gnus-group-quit-config group)))
6570     (when (or no-questions
6571               gnus-expert-user
6572               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6573       (gnus-async-halt-prefetch)
6574       (run-hooks 'gnus-summary-prepare-exit-hook)
6575       (when (gnus-buffer-live-p gnus-article-buffer)
6576         (save-excursion
6577           (set-buffer gnus-article-buffer)
6578           (mm-destroy-parts gnus-article-mime-handles)
6579           ;; Set it to nil for safety reason.
6580           (setq gnus-article-mime-handle-alist nil)
6581           (setq gnus-article-mime-handles nil)))
6582       ;; If we have several article buffers, we kill them at exit.
6583       (unless gnus-single-article-buffer
6584         (gnus-kill-buffer gnus-article-buffer)
6585         (gnus-kill-buffer gnus-original-article-buffer)
6586         (setq gnus-article-current nil))
6587       (if (not gnus-kill-summary-on-exit)
6588           (gnus-deaden-summary)
6589         (gnus-close-group group)
6590         (gnus-summary-clear-local-variables)
6591         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6592           (gnus-summary-clear-local-variables))
6593         (set-buffer gnus-group-buffer)
6594         (gnus-summary-clear-local-variables)
6595         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6596           (gnus-summary-clear-local-variables))
6597         (gnus-kill-buffer gnus-summary-buffer))
6598       (unless gnus-single-article-buffer
6599         (setq gnus-article-current nil))
6600       (when gnus-use-trees
6601         (gnus-tree-close group))
6602       (gnus-async-prefetch-remove-group group)
6603       (when (get-buffer gnus-article-buffer)
6604         (bury-buffer gnus-article-buffer))
6605       ;; Return to the group buffer.
6606       (gnus-configure-windows 'group 'force)
6607       ;; Clear the current group name.
6608       (setq gnus-newsgroup-name nil)
6609       (unless (gnus-ephemeral-group-p group)
6610         (gnus-group-update-group group))
6611       (when (equal (gnus-group-group-name) group)
6612         (gnus-group-next-unread-group 1))
6613       (when quit-config
6614         (gnus-handle-ephemeral-exit quit-config)))))
6615
6616 (defun gnus-handle-ephemeral-exit (quit-config)
6617   "Handle movement when leaving an ephemeral group.
6618 The state which existed when entering the ephemeral is reset."
6619   (if (not (buffer-name (car quit-config)))
6620       (gnus-configure-windows 'group 'force)
6621     (set-buffer (car quit-config))
6622     (cond ((eq major-mode 'gnus-summary-mode)
6623            (gnus-set-global-variables))
6624           ((eq major-mode 'gnus-article-mode)
6625            (save-excursion
6626              ;; The `gnus-summary-buffer' variable may point
6627              ;; to the old summary buffer when using a single
6628              ;; article buffer.
6629              (unless (gnus-buffer-live-p gnus-summary-buffer)
6630                (set-buffer gnus-group-buffer))
6631              (set-buffer gnus-summary-buffer)
6632              (gnus-set-global-variables))))
6633     (if (or (eq (cdr quit-config) 'article)
6634             (eq (cdr quit-config) 'pick))
6635         (progn
6636           ;; The current article may be from the ephemeral group
6637           ;; thus it is best that we reload this article
6638           ;;
6639           ;; If we're exiting from a large digest, this can be
6640           ;; extremely slow.  So, it's better not to reload it. -- jh.
6641           ;;(gnus-summary-show-article)
6642           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6643               (gnus-configure-windows 'pick 'force)
6644             (gnus-configure-windows (cdr quit-config) 'force)))
6645       (gnus-configure-windows (cdr quit-config) 'force))
6646     (when (eq major-mode 'gnus-summary-mode)
6647       (gnus-summary-next-subject 1 nil t)
6648       (gnus-summary-recenter)
6649       (gnus-summary-position-point))))
6650
6651 ;;; Dead summaries.
6652
6653 (defvar gnus-dead-summary-mode-map nil)
6654
6655 (unless gnus-dead-summary-mode-map
6656   (setq gnus-dead-summary-mode-map (make-keymap))
6657   (suppress-keymap gnus-dead-summary-mode-map)
6658   (substitute-key-definition
6659    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6660   (dolist (key '("\C-d" "\r" "\177" [delete]))
6661     (define-key gnus-dead-summary-mode-map
6662       key 'gnus-summary-wake-up-the-dead))
6663   (dolist (key '("q" "Q"))
6664     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6665
6666 (defvar gnus-dead-summary-mode nil
6667   "Minor mode for Gnus summary buffers.")
6668
6669 (defun gnus-dead-summary-mode (&optional arg)
6670   "Minor mode for Gnus summary buffers."
6671   (interactive "P")
6672   (when (eq major-mode 'gnus-summary-mode)
6673     (make-local-variable 'gnus-dead-summary-mode)
6674     (setq gnus-dead-summary-mode
6675           (if (null arg) (not gnus-dead-summary-mode)
6676             (> (prefix-numeric-value arg) 0)))
6677     (when gnus-dead-summary-mode
6678       (gnus-add-minor-mode
6679        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6680
6681 (defun gnus-deaden-summary ()
6682   "Make the current summary buffer into a dead summary buffer."
6683   ;; Kill any previous dead summary buffer.
6684   (when (and gnus-dead-summary
6685              (buffer-name gnus-dead-summary))
6686     (save-excursion
6687       (set-buffer gnus-dead-summary)
6688       (when gnus-dead-summary-mode
6689         (kill-buffer (current-buffer)))))
6690   ;; Make this the current dead summary.
6691   (setq gnus-dead-summary (current-buffer))
6692   (gnus-dead-summary-mode 1)
6693   (let ((name (buffer-name)))
6694     (when (string-match "Summary" name)
6695       (rename-buffer
6696        (concat (substring name 0 (match-beginning 0)) "Dead "
6697                (substring name (match-beginning 0)))
6698        t)
6699       (bury-buffer))))
6700
6701 (defun gnus-kill-or-deaden-summary (buffer)
6702   "Kill or deaden the summary BUFFER."
6703   (save-excursion
6704     (when (and (buffer-name buffer)
6705                (not gnus-single-article-buffer))
6706       (save-excursion
6707         (set-buffer buffer)
6708         (gnus-kill-buffer gnus-article-buffer)
6709         (gnus-kill-buffer gnus-original-article-buffer)))
6710     (cond
6711      ;; Kill the buffer.
6712      (gnus-kill-summary-on-exit
6713       (when (and gnus-use-trees
6714                  (gnus-buffer-exists-p buffer))
6715         (save-excursion
6716           (set-buffer buffer)
6717           (gnus-tree-close gnus-newsgroup-name)))
6718       (gnus-kill-buffer buffer))
6719      ;; Deaden the buffer.
6720      ((gnus-buffer-exists-p buffer)
6721       (save-excursion
6722         (set-buffer buffer)
6723         (gnus-deaden-summary))))))
6724
6725 (defun gnus-summary-wake-up-the-dead (&rest args)
6726   "Wake up the dead summary buffer."
6727   (interactive)
6728   (gnus-dead-summary-mode -1)
6729   (let ((name (buffer-name)))
6730     (when (string-match "Dead " name)
6731       (rename-buffer
6732        (concat (substring name 0 (match-beginning 0))
6733                (substring name (match-end 0)))
6734        t)))
6735   (gnus-message 3 "This dead summary is now alive again"))
6736
6737 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6738 (defun gnus-summary-fetch-faq (&optional faq-dir)
6739   "Fetch the FAQ for the current group.
6740 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6741 in."
6742   (interactive
6743    (list
6744     (when current-prefix-arg
6745       (completing-read
6746        "FAQ dir: " (and (listp gnus-group-faq-directory)
6747                         (mapcar (lambda (file) (list file))
6748                                 gnus-group-faq-directory))))))
6749   (let (gnus-faq-buffer)
6750     (when (setq gnus-faq-buffer
6751                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6752       (gnus-configure-windows 'summary-faq))))
6753
6754 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6755 (defun gnus-summary-describe-group (&optional force)
6756   "Describe the current newsgroup."
6757   (interactive "P")
6758   (gnus-group-describe-group force gnus-newsgroup-name))
6759
6760 (defun gnus-summary-describe-briefly ()
6761   "Describe summary mode commands briefly."
6762   (interactive)
6763   (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")))
6764
6765 ;; Walking around group mode buffer from summary mode.
6766
6767 (defun gnus-summary-next-group (&optional no-article target-group backward)
6768   "Exit current newsgroup and then select next unread newsgroup.
6769 If prefix argument NO-ARTICLE is non-nil, no article is selected
6770 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6771 previous group instead."
6772   (interactive "P")
6773   ;; Stop pre-fetching.
6774   (gnus-async-halt-prefetch)
6775   (let ((current-group gnus-newsgroup-name)
6776         (current-buffer (current-buffer))
6777         entered)
6778     ;; First we semi-exit this group to update Xrefs and all variables.
6779     ;; We can't do a real exit, because the window conf must remain
6780     ;; the same in case the user is prompted for info, and we don't
6781     ;; want the window conf to change before that...
6782     (gnus-summary-exit t)
6783     (while (not entered)
6784       ;; Then we find what group we are supposed to enter.
6785       (set-buffer gnus-group-buffer)
6786       (gnus-group-jump-to-group current-group)
6787       (setq target-group
6788             (or target-group
6789                 (if (eq gnus-keep-same-level 'best)
6790                     (gnus-summary-best-group gnus-newsgroup-name)
6791                   (gnus-summary-search-group backward gnus-keep-same-level))))
6792       (if (not target-group)
6793           ;; There are no further groups, so we return to the group
6794           ;; buffer.
6795           (progn
6796             (gnus-message 5 "Returning to the group buffer")
6797             (setq entered t)
6798             (when (gnus-buffer-live-p current-buffer)
6799               (set-buffer current-buffer)
6800               (gnus-summary-exit))
6801             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6802         ;; We try to enter the target group.
6803         (gnus-group-jump-to-group target-group)
6804         (let ((unreads (gnus-group-group-unread)))
6805           (if (and (or (eq t unreads)
6806                        (and unreads (not (zerop unreads))))
6807                    (gnus-summary-read-group
6808                     target-group nil no-article
6809                     (and (buffer-name current-buffer) current-buffer)
6810                     nil backward))
6811               (setq entered t)
6812             (setq current-group target-group
6813                   target-group nil)))))))
6814
6815 (defun gnus-summary-prev-group (&optional no-article)
6816   "Exit current newsgroup and then select previous unread newsgroup.
6817 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6818   (interactive "P")
6819   (gnus-summary-next-group no-article nil t))
6820
6821 ;; Walking around summary lines.
6822
6823 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6824   "Go to the first subject satisfying any non-nil constraint.
6825 If UNREAD is non-nil, the article should be unread.
6826 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6827 If UNSEEN is non-nil, the article should be unseen.
6828 Returns the article selected or nil if there are no matching articles."
6829   (interactive "P")
6830   (cond
6831    ;; Empty summary.
6832    ((null gnus-newsgroup-data)
6833     (gnus-message 3 "No articles in the group")
6834     nil)
6835    ;; Pick the first article.
6836    ((not (or unread undownloaded unseen))
6837     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6838     (gnus-data-number (car gnus-newsgroup-data)))
6839    ;; Find the first unread article.
6840    (t
6841     (let ((data gnus-newsgroup-data))
6842       (while (and data
6843                   (let ((num (gnus-data-number (car data))))
6844                     (or (memq num gnus-newsgroup-unfetched)
6845                         (not (or (and unread
6846                                       (memq num gnus-newsgroup-unreads))
6847                                  (and undownloaded
6848                                       (memq num gnus-newsgroup-undownloaded))
6849                                  (and unseen
6850                                       (memq num gnus-newsgroup-unseen)))))))
6851         (setq data (cdr data)))
6852       (prog1
6853           (if data
6854               (progn
6855                 (goto-char (gnus-data-pos (car data)))
6856                 (gnus-data-number (car data)))
6857             (gnus-message 3 "No more%s articles"
6858                           (let* ((r (when unread " unread"))
6859                                  (d (when undownloaded " undownloaded"))
6860                                  (s (when unseen " unseen"))
6861                                  (l (delq nil (list r d s))))
6862                             (cond ((= 3 (length l))
6863                                    (concat r "," d ", or" s))
6864                                   ((= 2 (length l))
6865                                    (concat (car l) ", or" (cadr l)))
6866                                   ((= 1 (length l))
6867                                    (car l))
6868                                   (t
6869                                    ""))))
6870             nil
6871             )
6872         (gnus-summary-position-point))))))
6873
6874 (defun gnus-summary-next-subject (n &optional unread dont-display)
6875   "Go to next N'th summary line.
6876 If N is negative, go to the previous N'th subject line.
6877 If UNREAD is non-nil, only unread articles are selected.
6878 The difference between N and the actual number of steps taken is
6879 returned."
6880   (interactive "p")
6881   (let ((backward (< n 0))
6882         (n (abs n)))
6883     (while (and (> n 0)
6884                 (if backward
6885                     (gnus-summary-find-prev unread)
6886                   (gnus-summary-find-next unread)))
6887       (unless (zerop (setq n (1- n)))
6888         (gnus-summary-show-thread)))
6889     (when (/= 0 n)
6890       (gnus-message 7 "No more%s articles"
6891                     (if unread " unread" "")))
6892     (unless dont-display
6893       (gnus-summary-recenter)
6894       (gnus-summary-position-point))
6895     n))
6896
6897 (defun gnus-summary-next-unread-subject (n)
6898   "Go to next N'th unread summary line."
6899   (interactive "p")
6900   (gnus-summary-next-subject n t))
6901
6902 (defun gnus-summary-prev-subject (n &optional unread)
6903   "Go to previous N'th summary line.
6904 If optional argument UNREAD is non-nil, only unread article is selected."
6905   (interactive "p")
6906   (gnus-summary-next-subject (- n) unread))
6907
6908 (defun gnus-summary-prev-unread-subject (n)
6909   "Go to previous N'th unread summary line."
6910   (interactive "p")
6911   (gnus-summary-next-subject (- n) t))
6912
6913 (defun gnus-summary-goto-subjects (articles)
6914   "Insert the subject header for ARTICLES in the current buffer."
6915   (save-excursion
6916     (dolist (article articles)
6917       (gnus-summary-goto-subject article t)))
6918   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6919   (gnus-summary-position-point))
6920  
6921 (defun gnus-summary-goto-subject (article &optional force silent)
6922   "Go the subject line of ARTICLE.
6923 If FORCE, also allow jumping to articles not currently shown."
6924   (interactive "nArticle number: ")
6925   (unless (numberp article)
6926     (error "Article %s is not a number" article))
6927   (let ((b (point))
6928         (data (gnus-data-find article)))
6929     ;; We read in the article if we have to.
6930     (and (not data)
6931          force
6932          (gnus-summary-insert-subject
6933           article
6934           (if (or (numberp force) (vectorp force)) force)
6935           t)
6936          (setq data (gnus-data-find article)))
6937     (goto-char b)
6938     (if (not data)
6939         (progn
6940           (unless silent
6941             (gnus-message 3 "Can't find article %d" article))
6942           nil)
6943       (let ((pt (gnus-data-pos data)))
6944         (goto-char pt)
6945         (gnus-summary-set-article-display-arrow pt))
6946       (gnus-summary-position-point)
6947       article)))
6948
6949 ;; Walking around summary lines with displaying articles.
6950
6951 (defun gnus-summary-expand-window (&optional arg)
6952   "Make the summary buffer take up the entire Emacs frame.
6953 Given a prefix, will force an `article' buffer configuration."
6954   (interactive "P")
6955   (if arg
6956       (gnus-configure-windows 'article 'force)
6957     (gnus-configure-windows 'summary 'force)))
6958
6959 (defun gnus-summary-display-article (article &optional all-header)
6960   "Display ARTICLE in article buffer."
6961   (when (gnus-buffer-live-p gnus-article-buffer)
6962     (with-current-buffer gnus-article-buffer
6963       (mm-enable-multibyte)))
6964   (gnus-set-global-variables)
6965   (when (gnus-buffer-live-p gnus-article-buffer)
6966     (with-current-buffer gnus-article-buffer
6967       (setq gnus-article-charset gnus-newsgroup-charset)
6968       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6969       (mm-enable-multibyte)))
6970   (if (null article)
6971       nil
6972     (prog1
6973         (if gnus-summary-display-article-function
6974             (funcall gnus-summary-display-article-function article all-header)
6975           (gnus-article-prepare article all-header))
6976       (gnus-run-hooks 'gnus-select-article-hook)
6977       (when (and gnus-current-article
6978                  (not (zerop gnus-current-article)))
6979         (gnus-summary-goto-subject gnus-current-article))
6980       (gnus-summary-recenter)
6981       (when (and gnus-use-trees gnus-show-threads)
6982         (gnus-possibly-generate-tree article)
6983         (gnus-highlight-selected-tree article))
6984       ;; Successfully display article.
6985       (gnus-article-set-window-start
6986        (cdr (assq article gnus-newsgroup-bookmarks))))))
6987
6988 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6989   "Select the current article.
6990 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6991 non-nil, the article will be re-fetched even if it already present in
6992 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6993 be displayed."
6994   ;; Make sure we are in the summary buffer to work around bbdb bug.
6995   (unless (eq major-mode 'gnus-summary-mode)
6996     (set-buffer gnus-summary-buffer))
6997   (let ((article (or article (gnus-summary-article-number)))
6998         (all-headers (not (not all-headers))) ;Must be t or nil.
6999         gnus-summary-display-article-function)
7000     (and (not pseudo)
7001          (gnus-summary-article-pseudo-p article)
7002          (error "This is a pseudo-article"))
7003     (save-excursion
7004       (set-buffer gnus-summary-buffer)
7005       (if (or (and gnus-single-article-buffer
7006                    (or (null gnus-current-article)
7007                        (null gnus-article-current)
7008                        (null (get-buffer gnus-article-buffer))
7009                        (not (eq article (cdr gnus-article-current)))
7010                        (not (equal (car gnus-article-current)
7011                                    gnus-newsgroup-name))))
7012               (and (not gnus-single-article-buffer)
7013                    (or (null gnus-current-article)
7014                        (not (eq gnus-current-article article))))
7015               force)
7016           ;; The requested article is different from the current article.
7017           (progn
7018             (gnus-summary-display-article article all-headers)
7019             (when (gnus-buffer-live-p gnus-article-buffer)
7020               (with-current-buffer gnus-article-buffer
7021                 (if (not gnus-article-decoded-p) ;; a local variable
7022                     (mm-disable-multibyte))))
7023             (gnus-article-set-window-start
7024              (cdr (assq article gnus-newsgroup-bookmarks)))
7025             article)
7026         'old))))
7027
7028 (defun gnus-summary-force-verify-and-decrypt ()
7029   "Display buttons for signed/encrypted parts and verify/decrypt them."
7030   (interactive)
7031   (let ((mm-verify-option 'known)
7032         (mm-decrypt-option 'known)
7033         (gnus-article-emulate-mime t)
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 dormant 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               (when (setq ref (message-fetch-field "in-reply-to"))
8022                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8023             (widen))
8024         (setq ref
8025               ;; It's not the current article, so we take a bet on
8026               ;; the value we got from the server.
8027               (mail-header-references header)))
8028       (if (and ref
8029                (not (equal ref "")))
8030           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8031             (gnus-message 1 "Couldn't find parent"))
8032         (gnus-message 1 "No references in article %d"
8033                       (gnus-summary-article-number))
8034         (setq error t))
8035       (decf n))
8036     (gnus-summary-position-point)
8037     n))
8038
8039 (defun gnus-summary-refer-references ()
8040   "Fetch all articles mentioned in the References header.
8041 Return the number of articles fetched."
8042   (interactive)
8043   (let ((ref (mail-header-references (gnus-summary-article-header)))
8044         (current (gnus-summary-article-number))
8045         (n 0))
8046     (if (or (not ref)
8047             (equal ref ""))
8048         (error "No References in the current article")
8049       ;; For each Message-ID in the References header...
8050       (while (string-match "<[^>]*>" ref)
8051         (incf n)
8052         ;; ... fetch that article.
8053         (gnus-summary-refer-article
8054          (prog1 (match-string 0 ref)
8055            (setq ref (substring ref (match-end 0))))))
8056       (gnus-summary-goto-subject current)
8057       (gnus-summary-position-point)
8058       n)))
8059
8060 (defun gnus-summary-refer-thread (&optional limit)
8061   "Fetch all articles in the current thread.
8062 If LIMIT (the numerical prefix), fetch that many old headers instead
8063 of what's specified by the `gnus-refer-thread-limit' variable."
8064   (interactive "P")
8065   (let ((id (mail-header-id (gnus-summary-article-header)))
8066         (limit (if limit (prefix-numeric-value limit)
8067                  gnus-refer-thread-limit)))
8068     (unless (eq gnus-fetch-old-headers 'invisible)
8069       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8070       ;; Retrieve the headers and read them in.
8071       (if (eq (if (numberp limit)
8072                   (gnus-retrieve-headers
8073                    (list (min
8074                           (+ (mail-header-number
8075                               (gnus-summary-article-header))
8076                              limit)
8077                           gnus-newsgroup-end))
8078                    gnus-newsgroup-name (* limit 2))
8079                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8080                 ;; headers.
8081                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8082                                        gnus-newsgroup-name limit))
8083               'nov)
8084           (gnus-build-all-threads)
8085         (error "Can't fetch thread from back ends that don't support NOV"))
8086       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8087     (gnus-summary-limit-include-thread id)))
8088
8089 (defun gnus-summary-refer-article (message-id)
8090   "Fetch an article specified by MESSAGE-ID."
8091   (interactive "sMessage-ID: ")
8092   (when (and (stringp message-id)
8093              (not (zerop (length message-id))))
8094     ;; Construct the correct Message-ID if necessary.
8095     ;; Suggested by tale@pawl.rpi.edu.
8096     (unless (string-match "^<" message-id)
8097       (setq message-id (concat "<" message-id)))
8098     (unless (string-match ">$" message-id)
8099       (setq message-id (concat message-id ">")))
8100     (let* ((header (gnus-id-to-header message-id))
8101            (sparse (and header
8102                         (gnus-summary-article-sparse-p
8103                          (mail-header-number header))
8104                         (memq (mail-header-number header)
8105                               gnus-newsgroup-limit)))
8106            number)
8107       (cond
8108        ;; If the article is present in the buffer we just go to it.
8109        ((and header
8110              (or (not (gnus-summary-article-sparse-p
8111                        (mail-header-number header)))
8112                  sparse))
8113         (prog1
8114             (gnus-summary-goto-article
8115              (mail-header-number header) nil t)
8116           (when sparse
8117             (gnus-summary-update-article (mail-header-number header)))))
8118        (t
8119         ;; We fetch the article.
8120         (catch 'found
8121           (dolist (gnus-override-method (gnus-refer-article-methods))
8122             (when (and (gnus-check-server gnus-override-method)
8123                        ;; Fetch the header,
8124                        (setq number (gnus-summary-insert-subject message-id)))
8125               ;; and display the article.
8126               (gnus-summary-select-article nil nil nil number)
8127               (throw 'found t)))
8128           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8129
8130 (defun gnus-refer-article-methods ()
8131   "Return a list of referable methods."
8132   (cond
8133    ;; No method, so we default to current and native.
8134    ((null gnus-refer-article-method)
8135     (list gnus-current-select-method gnus-select-method))
8136    ;; Current.
8137    ((eq 'current gnus-refer-article-method)
8138     (list gnus-current-select-method))
8139    ;; List of select methods.
8140    ((not (and (symbolp (car gnus-refer-article-method))
8141               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8142     (let (out)
8143       (dolist (method gnus-refer-article-method)
8144         (push (if (eq 'current method)
8145                   gnus-current-select-method
8146                 method)
8147               out))
8148       (nreverse out)))
8149    ;; One single select method.
8150    (t
8151     (list gnus-refer-article-method))))
8152
8153 (defun gnus-summary-edit-parameters ()
8154   "Edit the group parameters of the current group."
8155   (interactive)
8156   (gnus-group-edit-group gnus-newsgroup-name 'params))
8157
8158 (defun gnus-summary-customize-parameters ()
8159   "Customize the group parameters of the current group."
8160   (interactive)
8161   (gnus-group-customize gnus-newsgroup-name))
8162
8163 (defun gnus-summary-enter-digest-group (&optional force)
8164   "Enter an nndoc group based on the current article.
8165 If FORCE, force a digest interpretation.  If not, try
8166 to guess what the document format is."
8167   (interactive "P")
8168   (let ((conf gnus-current-window-configuration))
8169     (save-window-excursion
8170       (save-excursion
8171         (let (gnus-article-prepare-hook
8172               gnus-display-mime-function
8173               gnus-break-pages)
8174           (gnus-summary-select-article))))
8175     (setq gnus-current-window-configuration conf)
8176     (let* ((name (format "%s-%d"
8177                          (gnus-group-prefixed-name
8178                           gnus-newsgroup-name (list 'nndoc ""))
8179                          (save-excursion
8180                            (set-buffer gnus-summary-buffer)
8181                            gnus-current-article)))
8182            (ogroup gnus-newsgroup-name)
8183            (params (append (gnus-info-params (gnus-get-info ogroup))
8184                            (list (cons 'to-group ogroup))
8185                            (list (cons 'save-article-group ogroup))))
8186            (case-fold-search t)
8187            (buf (current-buffer))
8188            dig to-address)
8189       (save-excursion
8190         (set-buffer gnus-original-article-buffer)
8191         ;; Have the digest group inherit the main mail address of
8192         ;; the parent article.
8193         (when (setq to-address (or (gnus-fetch-field "reply-to")
8194                                    (gnus-fetch-field "from")))
8195           (setq params (append
8196                         (list (cons 'to-address
8197                                     (funcall gnus-decode-encoded-word-function
8198                                              to-address))))))
8199         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8200         (insert-buffer-substring gnus-original-article-buffer)
8201         ;; Remove lines that may lead nndoc to misinterpret the
8202         ;; document type.
8203         (narrow-to-region
8204          (goto-char (point-min))
8205          (or (search-forward "\n\n" nil t) (point)))
8206         (goto-char (point-min))
8207         (delete-matching-lines "^Path:\\|^From ")
8208         (widen))
8209       (unwind-protect
8210           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8211                     (gnus-newsgroup-ephemeral-ignored-charsets
8212                      gnus-newsgroup-ignored-charsets))
8213                 (gnus-group-read-ephemeral-group
8214                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8215                               (nndoc-article-type
8216                                ,(if force 'mbox 'guess)))
8217                  t nil nil nil
8218                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8219                                                         "ADAPT")))))
8220               ;; Make all postings to this group go to the parent group.
8221               (nconc (gnus-info-params (gnus-get-info name))
8222                      params)
8223             ;; Couldn't select this doc group.
8224             (switch-to-buffer buf)
8225             (gnus-set-global-variables)
8226             (gnus-configure-windows 'summary)
8227             (gnus-message 3 "Article couldn't be entered?"))
8228         (kill-buffer dig)))))
8229
8230 (defun gnus-summary-read-document (n)
8231   "Open a new group based on the current article(s).
8232 This will allow you to read digests and other similar
8233 documents as newsgroups.
8234 Obeys the standard process/prefix convention."
8235   (interactive "P")
8236   (let* ((articles (gnus-summary-work-articles n))
8237          (ogroup gnus-newsgroup-name)
8238          (params (append (gnus-info-params (gnus-get-info ogroup))
8239                          (list (cons 'to-group ogroup))))
8240          article group egroup groups vgroup)
8241     (while (setq article (pop articles))
8242       (setq group (format "%s-%d" gnus-newsgroup-name article))
8243       (gnus-summary-remove-process-mark article)
8244       (when (gnus-summary-display-article article)
8245         (save-excursion
8246           (with-temp-buffer
8247             (insert-buffer-substring gnus-original-article-buffer)
8248             ;; Remove some headers that may lead nndoc to make
8249             ;; the wrong guess.
8250             (message-narrow-to-head)
8251             (goto-char (point-min))
8252             (delete-matching-lines "^\\(Path\\):\\|^From ")
8253             (widen)
8254             (if (setq egroup
8255                       (gnus-group-read-ephemeral-group
8256                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8257                                      (nndoc-article-type guess))
8258                        t nil t))
8259                 (progn
8260             ;; Make all postings to this group go to the parent group.
8261                   (nconc (gnus-info-params (gnus-get-info egroup))
8262                          params)
8263                   (push egroup groups))
8264               ;; Couldn't select this doc group.
8265               (gnus-error 3 "Article couldn't be entered"))))))
8266     ;; Now we have selected all the documents.
8267     (cond
8268      ((not groups)
8269       (error "None of the articles could be interpreted as documents"))
8270      ((gnus-group-read-ephemeral-group
8271        (setq vgroup (format
8272                      "nnvirtual:%s-%s" gnus-newsgroup-name
8273                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8274        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8275        t
8276        (cons (current-buffer) 'summary)))
8277      (t
8278       (error "Couldn't select virtual nndoc group")))))
8279
8280 (defun gnus-summary-isearch-article (&optional regexp-p)
8281   "Do incremental search forward on the current article.
8282 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8283   (interactive "P")
8284   (gnus-summary-select-article)
8285   (gnus-configure-windows 'article)
8286   (gnus-eval-in-buffer-window gnus-article-buffer
8287     (save-restriction
8288       (widen)
8289       (isearch-forward regexp-p))))
8290
8291 (defun gnus-summary-search-article-forward (regexp &optional backward)
8292   "Search for an article containing REGEXP forward.
8293 If BACKWARD, search backward instead."
8294   (interactive
8295    (list (read-string
8296           (format "Search article %s (regexp%s): "
8297                   (if current-prefix-arg "backward" "forward")
8298                   (if gnus-last-search-regexp
8299                       (concat ", default " gnus-last-search-regexp)
8300                     "")))
8301          current-prefix-arg))
8302   (if (string-equal regexp "")
8303       (setq regexp (or gnus-last-search-regexp ""))
8304     (setq gnus-last-search-regexp regexp)
8305     (setq gnus-article-before-search gnus-current-article))
8306   ;; Intentionally set gnus-last-article.
8307   (setq gnus-last-article gnus-article-before-search)
8308   (let ((gnus-last-article gnus-last-article))
8309     (if (gnus-summary-search-article regexp backward)
8310         (gnus-summary-show-thread)
8311       (error "Search failed: \"%s\"" regexp))))
8312
8313 (defun gnus-summary-search-article-backward (regexp)
8314   "Search for an article containing REGEXP backward."
8315   (interactive
8316    (list (read-string
8317           (format "Search article backward (regexp%s): "
8318                   (if gnus-last-search-regexp
8319                       (concat ", default " gnus-last-search-regexp)
8320                     "")))))
8321   (gnus-summary-search-article-forward regexp 'backward))
8322
8323 (defun gnus-summary-search-article (regexp &optional backward)
8324   "Search for an article containing REGEXP.
8325 Optional argument BACKWARD means do search for backward.
8326 `gnus-select-article-hook' is not called during the search."
8327   ;; We have to require this here to make sure that the following
8328   ;; dynamic binding isn't shadowed by autoloading.
8329   (require 'gnus-async)
8330   (require 'gnus-art)
8331   (let ((gnus-select-article-hook nil)  ;Disable hook.
8332         (gnus-article-prepare-hook nil)
8333         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8334         (gnus-use-article-prefetch nil)
8335         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8336         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8337         (gnus-visual nil)
8338         (gnus-keep-backlog nil)
8339         (gnus-break-pages nil)
8340         (gnus-summary-display-arrow nil)
8341         (gnus-updated-mode-lines nil)
8342         (gnus-auto-center-summary nil)
8343         (sum (current-buffer))
8344         (gnus-display-mime-function nil)
8345         (found nil)
8346         point)
8347     (gnus-save-hidden-threads
8348       (gnus-summary-select-article)
8349       (set-buffer gnus-article-buffer)
8350       (goto-char (window-point (get-buffer-window (current-buffer))))
8351       (when backward
8352         (forward-line -1))
8353       (while (not found)
8354         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8355         (if (if backward
8356                 (re-search-backward regexp nil t)
8357               (re-search-forward regexp nil t))
8358             ;; We found the regexp.
8359             (progn
8360               (setq found 'found)
8361               (beginning-of-line)
8362               (set-window-start
8363                (get-buffer-window (current-buffer))
8364                (point))
8365               (forward-line 1)
8366               (set-window-point
8367                (get-buffer-window (current-buffer))
8368                (point))
8369               (set-buffer sum)
8370               (setq point (point)))
8371           ;; We didn't find it, so we go to the next article.
8372           (set-buffer sum)
8373           (setq found 'not)
8374           (while (eq found 'not)
8375             (if (not (if backward (gnus-summary-find-prev)
8376                        (gnus-summary-find-next)))
8377                 ;; No more articles.
8378                 (setq found t)
8379               ;; Select the next article and adjust point.
8380               (unless (gnus-summary-article-sparse-p
8381                        (gnus-summary-article-number))
8382                 (setq found nil)
8383                 (gnus-summary-select-article)
8384                 (set-buffer gnus-article-buffer)
8385                 (widen)
8386                 (goto-char (if backward (point-max) (point-min))))))))
8387       (gnus-message 7 ""))
8388     ;; Return whether we found the regexp.
8389     (when (eq found 'found)
8390       (goto-char point)
8391       (gnus-summary-show-thread)
8392       (gnus-summary-goto-subject gnus-current-article)
8393       (gnus-summary-position-point)
8394       t)))
8395
8396 (defun gnus-find-matching-articles (header regexp)
8397   "Return a list of all articles that match REGEXP on HEADER.
8398 This search includes all articles in the current group that Gnus has
8399 fetched headers for, whether they are displayed or not."
8400   (let ((articles nil)
8401         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8402         (case-fold-search t))
8403     (dolist (header gnus-newsgroup-headers)
8404       (when (string-match regexp (funcall func header))
8405         (push (mail-header-number header) articles)))
8406     (nreverse articles)))
8407
8408 (defun gnus-summary-find-matching (header regexp &optional backward unread
8409                                           not-case-fold not-matching)
8410   "Return a list of all articles that match REGEXP on HEADER.
8411 The search stars on the current article and goes forwards unless
8412 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8413 If UNREAD is non-nil, only unread articles will
8414 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8415 in the comparisons. If NOT-MATCHING, return a list of all articles that
8416 not match REGEXP on HEADER."
8417   (let ((case-fold-search (not not-case-fold))
8418         articles d func)
8419     (if (consp header)
8420         (if (eq (car header) 'extra)
8421             (setq func
8422                   `(lambda (h)
8423                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8424                          "")))
8425           (error "%s is an invalid header" header))
8426       (unless (fboundp (intern (concat "mail-header-" header)))
8427         (error "%s is not a valid header" header))
8428       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8429     (dolist (d (if (eq backward 'all)
8430                    gnus-newsgroup-data
8431                  (gnus-data-find-list
8432                   (gnus-summary-article-number)
8433                   (gnus-data-list backward))))
8434       (when (and (or (not unread)       ; We want all articles...
8435                      (gnus-data-unread-p d)) ; Or just unreads.
8436                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8437                  (if not-matching
8438                      (not (string-match
8439                            regexp
8440                            (funcall func (gnus-data-header d))))
8441                    (string-match regexp
8442                                  (funcall func (gnus-data-header d)))))
8443         (push (gnus-data-number d) articles))) ; Success!
8444     (nreverse articles)))
8445
8446 (defun gnus-summary-execute-command (header regexp command &optional backward)
8447   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8448 If HEADER is an empty string (or nil), the match is done on the entire
8449 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8450   (interactive
8451    (list (let ((completion-ignore-case t))
8452            (completing-read
8453             "Header name: "
8454             (mapcar (lambda (header) (list (format "%s" header)))
8455                     (append
8456                      '("Number" "Subject" "From" "Lines" "Date"
8457                        "Message-ID" "Xref" "References" "Body")
8458                      gnus-extra-headers))
8459             nil 'require-match))
8460          (read-string "Regexp: ")
8461          (read-key-sequence "Command: ")
8462          current-prefix-arg))
8463   (when (equal header "Body")
8464     (setq header ""))
8465   ;; Hidden thread subtrees must be searched as well.
8466   (gnus-summary-show-all-threads)
8467   ;; We don't want to change current point nor window configuration.
8468   (save-excursion
8469     (save-window-excursion
8470       (let (gnus-visual
8471             gnus-treat-strip-trailing-blank-lines
8472             gnus-treat-strip-leading-blank-lines
8473             gnus-treat-strip-multiple-blank-lines
8474             gnus-treat-hide-boring-headers
8475             gnus-treat-fold-newsgroups
8476             gnus-article-prepare-hook)
8477         (gnus-message 6 "Executing %s..." (key-description command))
8478         ;; We'd like to execute COMMAND interactively so as to give arguments.
8479         (gnus-execute header regexp
8480                       `(call-interactively ',(key-binding command))
8481                       backward)
8482         (gnus-message 6 "Executing %s...done" (key-description command))))))
8483
8484 (defun gnus-summary-beginning-of-article ()
8485   "Scroll the article back to the beginning."
8486   (interactive)
8487   (gnus-summary-select-article)
8488   (gnus-configure-windows 'article)
8489   (gnus-eval-in-buffer-window gnus-article-buffer
8490     (widen)
8491     (goto-char (point-min))
8492     (when gnus-page-broken
8493       (gnus-narrow-to-page))))
8494
8495 (defun gnus-summary-end-of-article ()
8496   "Scroll to the end of the article."
8497   (interactive)
8498   (gnus-summary-select-article)
8499   (gnus-configure-windows 'article)
8500   (gnus-eval-in-buffer-window gnus-article-buffer
8501     (widen)
8502     (goto-char (point-max))
8503     (recenter -3)
8504     (when gnus-page-broken
8505       (gnus-narrow-to-page))))
8506
8507 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8508   "Truncate to LEN and quote all \"(\"'s in STRING."
8509   (gnus-replace-in-string (if (and len (> (length string) len))
8510                               (substring string 0 len)
8511                             string)
8512                           "[()]" "\\\\\\&"))
8513
8514 (defun gnus-summary-print-article (&optional filename n)
8515   "Generate and print a PostScript image of the N next (mail) articles.
8516
8517 If N is negative, print the N previous articles.  If N is nil and articles
8518 have been marked with the process mark, print these instead.
8519
8520 If the optional first argument FILENAME is nil, send the image to the
8521 printer.  If FILENAME is a string, save the PostScript image in a file with
8522 that name.  If FILENAME is a number, prompt the user for the name of the file
8523 to save in."
8524   (interactive (list (ps-print-preprint current-prefix-arg)))
8525   (dolist (article (gnus-summary-work-articles n))
8526     (gnus-summary-select-article nil nil 'pseudo article)
8527     (gnus-eval-in-buffer-window gnus-article-buffer
8528       (gnus-print-buffer))
8529     (gnus-summary-remove-process-mark article))
8530   (ps-despool filename))
8531
8532 (defun gnus-print-buffer ()
8533   (let ((buffer (generate-new-buffer " *print*")))
8534     (unwind-protect
8535         (progn
8536           (copy-to-buffer buffer (point-min) (point-max))
8537           (set-buffer buffer)
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               (gnus-article-emphasize)
8546               (gnus-article-delete-invisible-text)))
8547           (let ((ps-left-header
8548                  (list
8549                   (concat "("
8550                           (gnus-summary-print-truncate-and-quote
8551                            (mail-header-subject gnus-current-headers)
8552                            66) ")")
8553                   (concat "("
8554                           (gnus-summary-print-truncate-and-quote
8555                            (mail-header-from gnus-current-headers)
8556                            45) ")")))
8557                 (ps-right-header
8558                  (list
8559                   "/pagenumberstring load"
8560                   (concat "("
8561                           (mail-header-date gnus-current-headers) ")"))))
8562             (gnus-run-hooks 'gnus-ps-print-hook)
8563             (save-excursion
8564               (if window-system
8565                   (ps-spool-buffer-with-faces)
8566                 (ps-spool-buffer)))))
8567       (kill-buffer buffer))))
8568
8569 (defun gnus-summary-show-article (&optional arg)
8570   "Force redisplaying of the current article.
8571 If ARG (the prefix) is a number, show the article with the charset
8572 defined in `gnus-summary-show-article-charset-alist', or the charset
8573 input.
8574 If ARG (the prefix) is non-nil and not a number, show the raw article
8575 without any article massaging functions being run.  Normally, the key
8576 strokes are `C-u g'."
8577   (interactive "P")
8578   (cond
8579    ((numberp arg)
8580     (gnus-summary-show-article t)
8581     (let ((gnus-newsgroup-charset
8582            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8583                (mm-read-coding-system
8584                 "View as charset: " ;; actually it is coding system.
8585                 (save-excursion
8586                   (set-buffer gnus-article-buffer)
8587                   (mm-detect-coding-region (point) (point-max))))))
8588           (gnus-newsgroup-ignored-charsets 'gnus-all))
8589       (gnus-summary-select-article nil 'force)
8590       (let ((deps gnus-newsgroup-dependencies)
8591             head header lines)
8592         (save-excursion
8593           (set-buffer gnus-original-article-buffer)
8594           (save-restriction
8595             (message-narrow-to-head)
8596             (setq head (buffer-string))
8597             (goto-char (point-min))
8598             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8599               (goto-char (point-max))
8600               (widen)
8601               (setq lines (1- (count-lines (point) (point-max))))))
8602           (with-temp-buffer
8603             (insert (format "211 %d Article retrieved.\n"
8604                             (cdr gnus-article-current)))
8605             (insert head)
8606             (if lines (insert (format "Lines: %d\n" lines)))
8607             (insert ".\n")
8608             (let ((nntp-server-buffer (current-buffer)))
8609               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8610         (gnus-data-set-header
8611          (gnus-data-find (cdr gnus-article-current))
8612          header)
8613         (gnus-summary-update-article-line
8614          (cdr gnus-article-current) header)
8615         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8616           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8617    ((not arg)
8618     ;; Select the article the normal way.
8619     (gnus-summary-select-article nil 'force))
8620    (t
8621     ;; We have to require this here to make sure that the following
8622     ;; dynamic binding isn't shadowed by autoloading.
8623     (require 'gnus-async)
8624     (require 'gnus-art)
8625     ;; Bind the article treatment functions to nil.
8626     (let ((gnus-have-all-headers t)
8627           gnus-article-prepare-hook
8628           gnus-article-decode-hook
8629           gnus-display-mime-function
8630           gnus-break-pages)
8631       ;; Destroy any MIME parts.
8632       (when (gnus-buffer-live-p gnus-article-buffer)
8633         (save-excursion
8634           (set-buffer gnus-article-buffer)
8635           (mm-destroy-parts gnus-article-mime-handles)
8636           ;; Set it to nil for safety reason.
8637           (setq gnus-article-mime-handle-alist nil)
8638           (setq gnus-article-mime-handles nil)))
8639       (gnus-summary-select-article nil 'force))))
8640   (gnus-summary-goto-subject gnus-current-article)
8641   (gnus-summary-position-point))
8642
8643 (defun gnus-summary-show-raw-article ()
8644   "Show the raw article without any article massaging functions being run."
8645   (interactive)
8646   (gnus-summary-show-article t))
8647
8648 (defun gnus-summary-verbose-headers (&optional arg)
8649   "Toggle permanent full header display.
8650 If ARG is a positive number, turn header display on.
8651 If ARG is a negative number, turn header display off."
8652   (interactive "P")
8653   (setq gnus-show-all-headers
8654         (cond ((or (not (numberp arg))
8655                    (zerop arg))
8656                (not gnus-show-all-headers))
8657               ((natnump arg)
8658                t)))
8659   (gnus-summary-show-article))
8660
8661 (defun gnus-summary-toggle-header (&optional arg)
8662   "Show the headers if they are hidden, or hide them if they are shown.
8663 If ARG is a positive number, show the entire header.
8664 If ARG is a negative number, hide the unwanted header lines."
8665   (interactive "P")
8666   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8667                      (get-buffer-window gnus-article-buffer t))))
8668     (with-current-buffer gnus-article-buffer
8669       (widen)
8670       (article-narrow-to-head)
8671       (let* ((buffer-read-only nil)
8672              (inhibit-point-motion-hooks t)
8673              (hidden (if (numberp arg)
8674                          (>= arg 0)
8675                        (gnus-article-hidden-text-p 'headers)))
8676              s e)
8677         (delete-region (point-min) (point-max))
8678         (with-current-buffer gnus-original-article-buffer
8679           (goto-char (setq s (point-min)))
8680           (setq e (if (search-forward "\n\n" nil t)
8681                       (1- (point))
8682                     (point-max))))
8683         (insert-buffer-substring gnus-original-article-buffer s e)
8684         (run-hooks 'gnus-article-decode-hook)
8685         (if hidden
8686             (let ((gnus-treat-hide-headers nil)
8687                   (gnus-treat-hide-boring-headers nil))
8688               (gnus-delete-wash-type 'headers)
8689               (gnus-treat-article 'head))
8690           (gnus-treat-article 'head))
8691         (widen)
8692         (if window
8693             (set-window-start window (goto-char (point-min))))
8694         (setq gnus-page-broken
8695               (when gnus-break-pages
8696                 (gnus-narrow-to-page)
8697                 t))
8698         (gnus-set-mode-line 'article)))))
8699
8700 (defun gnus-summary-show-all-headers ()
8701   "Make all header lines visible."
8702   (interactive)
8703   (gnus-summary-toggle-header 1))
8704
8705 (defun gnus-summary-caesar-message (&optional arg)
8706   "Caesar rotate the current article by 13.
8707 The numerical prefix specifies how many places to rotate each letter
8708 forward."
8709   (interactive "P")
8710   (gnus-summary-select-article)
8711   (let ((mail-header-separator ""))
8712     (gnus-eval-in-buffer-window gnus-article-buffer
8713       (save-restriction
8714         (widen)
8715         (let ((start (window-start))
8716               buffer-read-only)
8717           (message-caesar-buffer-body arg)
8718           (set-window-start (get-buffer-window (current-buffer)) start))))))
8719
8720 (autoload 'unmorse-region "morse"
8721   "Convert morse coded text in region to ordinary ASCII text."
8722   t)
8723
8724 (defun gnus-summary-morse-message (&optional arg)
8725   "Morse decode the current article."
8726   (interactive "P")
8727   (gnus-summary-select-article)
8728   (let ((mail-header-separator ""))
8729     (gnus-eval-in-buffer-window gnus-article-buffer
8730       (save-excursion
8731         (save-restriction
8732           (widen)
8733           (let ((pos (window-start))
8734                 buffer-read-only)
8735             (goto-char (point-min))
8736             (when (message-goto-body)
8737               (gnus-narrow-to-body))
8738             (goto-char (point-min))
8739             (while (re-search-forward "·" (point-max) t)
8740               (replace-match "."))
8741             (unmorse-region (point-min) (point-max))
8742             (widen)
8743             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8744
8745 (defun gnus-summary-stop-page-breaking ()
8746   "Stop page breaking in the current article."
8747   (interactive)
8748   (gnus-summary-select-article)
8749   (gnus-eval-in-buffer-window gnus-article-buffer
8750     (widen)
8751     (when (gnus-visual-p 'page-marker)
8752       (let ((buffer-read-only nil))
8753         (gnus-remove-text-with-property 'gnus-prev)
8754         (gnus-remove-text-with-property 'gnus-next))
8755       (setq gnus-page-broken nil))))
8756
8757 (defun gnus-summary-move-article (&optional n to-newsgroup
8758                                             select-method action)
8759   "Move the current article to a different newsgroup.
8760 If N is a positive number, move the N next articles.
8761 If N is a negative number, move the N previous articles.
8762 If N is nil and any articles have been marked with the process mark,
8763 move those articles instead.
8764 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8765 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8766 re-spool using this method.
8767
8768 When called interactively with TO-NEWSGROUP being nil, the value of
8769 the variable `gnus-move-split-methods' is used for finding a default
8770 for the target newsgroup.
8771
8772 For this function to work, both the current newsgroup and the
8773 newsgroup that you want to move to have to support the `request-move'
8774 and `request-accept' functions.
8775
8776 ACTION can be either `move' (the default), `crosspost' or `copy'."
8777   (interactive "P")
8778   (unless action
8779     (setq action 'move))
8780   ;; Check whether the source group supports the required functions.
8781   (cond ((and (eq action 'move)
8782               (not (gnus-check-backend-function
8783                     'request-move-article gnus-newsgroup-name)))
8784          (error "The current group does not support article moving"))
8785         ((and (eq action 'crosspost)
8786               (not (gnus-check-backend-function
8787                     'request-replace-article gnus-newsgroup-name)))
8788          (error "The current group does not support article editing")))
8789   (let ((articles (gnus-summary-work-articles n))
8790         (prefix (if (gnus-check-backend-function
8791                      'request-move-article gnus-newsgroup-name)
8792                     (gnus-group-real-prefix gnus-newsgroup-name)
8793                   ""))
8794         (names '((move "Move" "Moving")
8795                  (copy "Copy" "Copying")
8796                  (crosspost "Crosspost" "Crossposting")))
8797         (copy-buf (save-excursion
8798                     (nnheader-set-temp-buffer " *copy article*")))
8799         art-group to-method new-xref article to-groups)
8800     (unless (assq action names)
8801       (error "Unknown action %s" action))
8802     ;; Read the newsgroup name.
8803     (when (and (not to-newsgroup)
8804                (not select-method))
8805       (if (and gnus-move-split-methods
8806                (not
8807                 (and (memq gnus-current-article articles)
8808                      (gnus-buffer-live-p gnus-original-article-buffer))))
8809           ;; When `gnus-move-split-methods' is non-nil, we have to
8810           ;; select an article to give `gnus-read-move-group-name' an
8811           ;; opportunity to suggest an appropriate default.  However,
8812           ;; we needn't render or mark the article.
8813           (let ((gnus-display-mime-function nil)
8814                 (gnus-article-prepare-hook nil)
8815                 (gnus-mark-article-hook nil))
8816             (gnus-summary-select-article nil nil nil (car articles))))
8817       (setq to-newsgroup
8818             (gnus-read-move-group-name
8819              (cadr (assq action names))
8820              (symbol-value (intern (format "gnus-current-%s-group" action)))
8821              articles prefix))
8822       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8823     (setq to-method (or select-method
8824                         (gnus-server-to-method
8825                          (gnus-group-method to-newsgroup))))
8826     ;; Check the method we are to move this article to...
8827     (unless (gnus-check-backend-function
8828              'request-accept-article (car to-method))
8829       (error "%s does not support article copying" (car to-method)))
8830     (unless (gnus-check-server to-method)
8831       (error "Can't open server %s" (car to-method)))
8832     (gnus-message 6 "%s to %s: %s..."
8833                   (caddr (assq action names))
8834                   (or (car select-method) to-newsgroup) articles)
8835     (while articles
8836       (setq article (pop articles))
8837       (setq
8838        art-group
8839        (cond
8840         ;; Move the article.
8841         ((eq action 'move)
8842          ;; Remove this article from future suppression.
8843          (gnus-dup-unsuppress-article article)
8844          (gnus-request-move-article
8845           article                       ; Article to move
8846           gnus-newsgroup-name           ; From newsgroup
8847           (nth 1 (gnus-find-method-for-group
8848                   gnus-newsgroup-name)) ; Server
8849           (list 'gnus-request-accept-article
8850                 to-newsgroup (list 'quote select-method)
8851                 (not articles) t)       ; Accept form
8852           (not articles)))              ; Only save nov last time
8853         ;; Copy the article.
8854         ((eq action 'copy)
8855          (save-excursion
8856            (set-buffer copy-buf)
8857            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8858              (gnus-request-accept-article
8859               to-newsgroup select-method (not articles) t))))
8860         ;; Crosspost the article.
8861         ((eq action 'crosspost)
8862          (let ((xref (message-tokenize-header
8863                       (mail-header-xref (gnus-summary-article-header article))
8864                       " ")))
8865            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8866                                   ":" (number-to-string article)))
8867            (unless xref
8868              (setq xref (list (system-name))))
8869            (setq new-xref
8870                  (concat
8871                   (mapconcat 'identity
8872                              (delete "Xref:" (delete new-xref xref))
8873                              " ")
8874                   " " new-xref))
8875            (save-excursion
8876              (set-buffer copy-buf)
8877              ;; First put the article in the destination group.
8878              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8879              (when (consp (setq art-group
8880                                 (gnus-request-accept-article
8881                                  to-newsgroup select-method (not articles))))
8882                (setq new-xref (concat new-xref " " (car art-group)
8883                                       ":"
8884                                       (number-to-string (cdr art-group))))
8885                ;; Now we have the new Xrefs header, so we insert
8886                ;; it and replace the new article.
8887                (nnheader-replace-header "Xref" new-xref)
8888                (gnus-request-replace-article
8889                 (cdr art-group) to-newsgroup (current-buffer))
8890                art-group))))))
8891       (cond
8892        ((not art-group)
8893         (gnus-message 1 "Couldn't %s article %s: %s"
8894                       (cadr (assq action names)) article
8895                       (nnheader-get-report (car to-method))))
8896        ((eq art-group 'junk)
8897         (when (eq action 'move)
8898           (gnus-summary-mark-article article gnus-canceled-mark)
8899           (gnus-message 4 "Deleted article %s" article)
8900           ;; run the delete hook
8901           (run-hook-with-args 'gnus-summary-article-delete-hook
8902                               action
8903                               (gnus-data-header
8904                                (assoc article (gnus-data-list nil)))
8905                               gnus-newsgroup-name nil
8906                               select-method)))
8907        (t
8908         (let* ((pto-group (gnus-group-prefixed-name
8909                            (car art-group) to-method))
8910                (entry
8911                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8912                (info (nth 2 entry))
8913                (to-group (gnus-info-group info))
8914                to-marks)
8915           ;; Update the group that has been moved to.
8916           (when (and info
8917                      (memq action '(move copy)))
8918             (unless (member to-group to-groups)
8919               (push to-group to-groups))
8920
8921             (unless (memq article gnus-newsgroup-unreads)
8922               (push 'read to-marks)
8923               (gnus-info-set-read
8924                info (gnus-add-to-range (gnus-info-read info)
8925                                        (list (cdr art-group)))))
8926
8927             ;; See whether the article is to be put in the cache.
8928             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8929                              gnus-article-mark-lists
8930                            (delete '(expirable . expire)
8931                                    (copy-sequence gnus-article-mark-lists))))
8932                   (to-article (cdr art-group)))
8933
8934               ;; Enter the article into the cache in the new group,
8935               ;; if that is required.
8936               (when gnus-use-cache
8937                 (gnus-cache-possibly-enter-article
8938                  to-group to-article
8939                  (memq article gnus-newsgroup-marked)
8940                  (memq article gnus-newsgroup-dormant)
8941                  (memq article gnus-newsgroup-unreads)))
8942
8943               (when gnus-preserve-marks
8944                 ;; Copy any marks over to the new group.
8945                 (when (and (equal to-group gnus-newsgroup-name)
8946                            (not (memq article gnus-newsgroup-unreads)))
8947                   ;; Mark this article as read in this group.
8948                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8949                   (setcdr (gnus-active to-group) to-article)
8950                   (setcdr gnus-newsgroup-active to-article))
8951
8952                 (while marks
8953                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8954                     (when (memq article (symbol-value
8955                                          (intern (format "gnus-newsgroup-%s"
8956                                                          (caar marks)))))
8957                       (push (cdar marks) to-marks)
8958                       ;; If the other group is the same as this group,
8959                       ;; then we have to add the mark to the list.
8960                       (when (equal to-group gnus-newsgroup-name)
8961                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8962                              (cons to-article
8963                                    (symbol-value
8964                                     (intern (format "gnus-newsgroup-%s"
8965                                                     (caar marks)))))))
8966                       ;; Copy the marks to other group.
8967                       (gnus-add-marked-articles
8968                        to-group (cdar marks) (list to-article) info)))
8969                   (setq marks (cdr marks)))
8970
8971                 (gnus-request-set-mark
8972                  to-group (list (list (list to-article) 'add to-marks))))
8973
8974               (gnus-dribble-enter
8975                (concat "(gnus-group-set-info '"
8976                        (gnus-prin1-to-string (gnus-get-info to-group))
8977                        ")"))))
8978
8979           ;; Update the Xref header in this article to point to
8980           ;; the new crossposted article we have just created.
8981           (when (eq action 'crosspost)
8982             (save-excursion
8983               (set-buffer copy-buf)
8984               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8985               (nnheader-replace-header "Xref" new-xref)
8986               (gnus-request-replace-article
8987                article gnus-newsgroup-name (current-buffer))))
8988
8989           ;; run the move/copy/crosspost/respool hook
8990           (run-hook-with-args 'gnus-summary-article-move-hook
8991                               action
8992                               (gnus-data-header
8993                                (assoc article (gnus-data-list nil)))
8994                               gnus-newsgroup-name
8995                               to-newsgroup
8996                               select-method))
8997
8998         ;;;!!!Why is this necessary?
8999         (set-buffer gnus-summary-buffer)
9000         
9001         (gnus-summary-goto-subject article)
9002         (when (eq action 'move)
9003           (gnus-summary-mark-article article gnus-canceled-mark))))
9004       (gnus-summary-remove-process-mark article))
9005     ;; Re-activate all groups that have been moved to.
9006     (save-excursion
9007       (set-buffer gnus-group-buffer)
9008       (let ((gnus-group-marked to-groups))
9009         (gnus-group-get-new-news-this-group nil t)))
9010
9011     (gnus-kill-buffer copy-buf)
9012     (gnus-summary-position-point)
9013     (gnus-set-mode-line 'summary)))
9014
9015 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9016   "Move the current article to a different newsgroup.
9017 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9018 When called interactively, if TO-NEWSGROUP is nil, use the value of
9019 the variable `gnus-move-split-methods' for finding a default target
9020 newsgroup.
9021 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9022 re-spool using this method."
9023   (interactive "P")
9024   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9025
9026 (defun gnus-summary-crosspost-article (&optional n)
9027   "Crosspost the current article to some other group."
9028   (interactive "P")
9029   (gnus-summary-move-article n nil nil 'crosspost))
9030
9031 (defcustom gnus-summary-respool-default-method nil
9032   "Default method type for respooling an article.
9033 If nil, use to the current newsgroup method."
9034   :type 'symbol
9035   :group 'gnus-summary-mail)
9036
9037 (defcustom gnus-summary-display-while-building nil
9038   "If non-nil, show and update the summary buffer as it's being built.
9039 If the value is t, update the buffer after every line is inserted.  If
9040 the value is an integer (N), update the display every N lines."
9041   :group 'gnus-thread
9042   :type '(choice (const :tag "off" nil)
9043                  number
9044                  (const :tag "frequently" t)))
9045
9046 (defun gnus-summary-respool-article (&optional n method)
9047   "Respool the current article.
9048 The article will be squeezed through the mail spooling process again,
9049 which means that it will be put in some mail newsgroup or other
9050 depending on `nnmail-split-methods'.
9051 If N is a positive number, respool the N next articles.
9052 If N is a negative number, respool the N previous articles.
9053 If N is nil and any articles have been marked with the process mark,
9054 respool those articles instead.
9055
9056 Respooling can be done both from mail groups and \"real\" newsgroups.
9057 In the former case, the articles in question will be moved from the
9058 current group into whatever groups they are destined to.  In the
9059 latter case, they will be copied into the relevant groups."
9060   (interactive
9061    (list current-prefix-arg
9062          (let* ((methods (gnus-methods-using 'respool))
9063                 (methname
9064                  (symbol-name (or gnus-summary-respool-default-method
9065                                   (car (gnus-find-method-for-group
9066                                         gnus-newsgroup-name)))))
9067                 (method
9068                  (gnus-completing-read-with-default
9069                   methname "What backend do you want to use when respooling?"
9070                   methods nil t nil 'gnus-mail-method-history))
9071                 ms)
9072            (cond
9073             ((zerop (length (setq ms (gnus-servers-using-backend
9074                                       (intern method)))))
9075              (list (intern method) ""))
9076             ((= 1 (length ms))
9077              (car ms))
9078             (t
9079              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9080                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9081                            ms-alist))))))))
9082   (unless method
9083     (error "No method given for respooling"))
9084   (if (assoc (symbol-name
9085               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9086              (gnus-methods-using 'respool))
9087       (gnus-summary-move-article n nil method)
9088     (gnus-summary-copy-article n nil method)))
9089
9090 (defun gnus-summary-import-article (file &optional edit)
9091   "Import an arbitrary file into a mail newsgroup."
9092   (interactive "fImport file: \nP")
9093   (let ((group gnus-newsgroup-name)
9094         (now (current-time))
9095         atts lines group-art)
9096     (unless (gnus-check-backend-function 'request-accept-article group)
9097       (error "%s does not support article importing" group))
9098     (or (file-readable-p file)
9099         (not (file-regular-p file))
9100         (error "Can't read %s" file))
9101     (save-excursion
9102       (set-buffer (gnus-get-buffer-create " *import file*"))
9103       (erase-buffer)
9104       (nnheader-insert-file-contents file)
9105       (goto-char (point-min))
9106       (if (nnheader-article-p)
9107           (save-restriction
9108             (goto-char (point-min))
9109             (search-forward "\n\n" nil t)
9110             (narrow-to-region (point-min) (1- (point)))
9111             (goto-char (point-min))
9112             (unless (re-search-forward "^date:" nil t)
9113               (goto-char (point-max))
9114               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9115        ;; This doesn't look like an article, so we fudge some headers.
9116         (setq atts (file-attributes file)
9117               lines (count-lines (point-min) (point-max)))
9118         (insert "From: " (read-string "From: ") "\n"
9119                 "Subject: " (read-string "Subject: ") "\n"
9120                 "Date: " (message-make-date (nth 5 atts)) "\n"
9121                 "Message-ID: " (message-make-message-id) "\n"
9122                 "Lines: " (int-to-string lines) "\n"
9123                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9124       (setq group-art (gnus-request-accept-article group nil t))
9125       (kill-buffer (current-buffer)))
9126     (setq gnus-newsgroup-active (gnus-activate-group group))
9127     (forward-line 1)
9128     (gnus-summary-goto-article (cdr group-art) nil t)
9129     (when edit
9130       (gnus-summary-edit-article))))
9131
9132 (defun gnus-summary-create-article ()
9133   "Create an article in a mail newsgroup."
9134   (interactive)
9135   (let ((group gnus-newsgroup-name)
9136         (now (current-time))
9137         group-art)
9138     (unless (gnus-check-backend-function 'request-accept-article group)
9139       (error "%s does not support article importing" group))
9140     (save-excursion
9141       (set-buffer (gnus-get-buffer-create " *import file*"))
9142       (erase-buffer)
9143       (goto-char (point-min))
9144       ;; This doesn't look like an article, so we fudge some headers.
9145       (insert "From: " (read-string "From: ") "\n"
9146               "Subject: " (read-string "Subject: ") "\n"
9147               "Date: " (message-make-date now) "\n"
9148               "Message-ID: " (message-make-message-id) "\n")
9149       (setq group-art (gnus-request-accept-article group nil t))
9150       (kill-buffer (current-buffer)))
9151     (setq gnus-newsgroup-active (gnus-activate-group group))
9152     (forward-line 1)
9153     (gnus-summary-goto-article (cdr group-art) nil t)
9154     (gnus-summary-edit-article)))
9155
9156 (defun gnus-summary-article-posted-p ()
9157   "Say whether the current (mail) article is available from news as well.
9158 This will be the case if the article has both been mailed and posted."
9159   (interactive)
9160   (let ((id (mail-header-references (gnus-summary-article-header)))
9161         (gnus-override-method (car (gnus-refer-article-methods))))
9162     (if (gnus-request-head id "")
9163         (gnus-message 2 "The current message was found on %s"
9164                       gnus-override-method)
9165       (gnus-message 2 "The current message couldn't be found on %s"
9166                     gnus-override-method)
9167       nil)))
9168
9169 (defun gnus-summary-expire-articles (&optional now)
9170   "Expire all articles that are marked as expirable in the current group."
9171   (interactive)
9172   (when (and (not gnus-group-is-exiting-without-update-p)
9173              (gnus-check-backend-function
9174               'request-expire-articles gnus-newsgroup-name))
9175     ;; This backend supports expiry.
9176     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9177            (expirable (if total
9178                           (progn
9179                             ;; We need to update the info for
9180                             ;; this group for `gnus-list-of-read-articles'
9181                             ;; to give us the right answer.
9182                             (gnus-run-hooks 'gnus-exit-group-hook)
9183                             (gnus-summary-update-info)
9184                             (gnus-list-of-read-articles gnus-newsgroup-name))
9185                         (setq gnus-newsgroup-expirable
9186                               (sort gnus-newsgroup-expirable '<))))
9187            (expiry-wait (if now 'immediate
9188                           (gnus-group-find-parameter
9189                            gnus-newsgroup-name 'expiry-wait)))
9190            (nnmail-expiry-target
9191             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9192                 nnmail-expiry-target))
9193            es)
9194       (when expirable
9195         ;; There are expirable articles in this group, so we run them
9196         ;; through the expiry process.
9197         (gnus-message 6 "Expiring articles...")
9198         (unless (gnus-check-group gnus-newsgroup-name)
9199           (error "Can't open server for %s" gnus-newsgroup-name))
9200         ;; The list of articles that weren't expired is returned.
9201         (save-excursion
9202           (if expiry-wait
9203               (let ((nnmail-expiry-wait-function nil)
9204                     (nnmail-expiry-wait expiry-wait))
9205                 (setq es (gnus-request-expire-articles
9206                           expirable gnus-newsgroup-name)))
9207             (setq es (gnus-request-expire-articles
9208                       expirable gnus-newsgroup-name)))
9209           (unless total
9210             (setq gnus-newsgroup-expirable es))
9211           ;; We go through the old list of expirable, and mark all
9212           ;; really expired articles as nonexistent.
9213           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9214             (let ((gnus-use-cache nil))
9215               (dolist (article expirable)
9216                 (when (and (not (memq article es))
9217                            (gnus-data-find article))
9218                   (gnus-summary-mark-article article gnus-canceled-mark)
9219                   (run-hook-with-args 'gnus-summary-article-expire-hook
9220                                       'delete
9221                                       (gnus-data-header
9222                                        (assoc article (gnus-data-list nil)))
9223                                       gnus-newsgroup-name
9224                                       nil
9225                                       nil))))))
9226         (gnus-message 6 "Expiring articles...done")))))
9227
9228 (defun gnus-summary-expire-articles-now ()
9229   "Expunge all expirable articles in the current group.
9230 This means that *all* articles that are marked as expirable will be
9231 deleted forever, right now."
9232   (interactive)
9233   (or gnus-expert-user
9234       (gnus-yes-or-no-p
9235        "Are you really, really, really sure you want to delete all these messages? ")
9236       (error "Phew!"))
9237   (gnus-summary-expire-articles t))
9238
9239 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9240 (defun gnus-summary-delete-article (&optional n)
9241   "Delete the N next (mail) articles.
9242 This command actually deletes articles.  This is not a marking
9243 command.  The article will disappear forever from your life, never to
9244 return.
9245
9246 If N is negative, delete backwards.
9247 If N is nil and articles have been marked with the process mark,
9248 delete these instead.
9249
9250 If `gnus-novice-user' is non-nil you will be asked for
9251 confirmation before the articles are deleted."
9252   (interactive "P")
9253   (unless (gnus-check-backend-function 'request-expire-articles
9254                                        gnus-newsgroup-name)
9255     (error "The current newsgroup does not support article deletion"))
9256   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9257     (error "Couldn't open server"))
9258   ;; Compute the list of articles to delete.
9259   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9260         (nnmail-expiry-target 'delete)
9261         not-deleted)
9262     (if (and gnus-novice-user
9263              (not (gnus-yes-or-no-p
9264                    (format "Do you really want to delete %s forever? "
9265                            (if (> (length articles) 1)
9266                                (format "these %s articles" (length articles))
9267                              "this article")))))
9268         ()
9269       ;; Delete the articles.
9270       (setq not-deleted (gnus-request-expire-articles
9271                          articles gnus-newsgroup-name 'force))
9272       (while articles
9273         (gnus-summary-remove-process-mark (car articles))
9274         ;; The backend might not have been able to delete the article
9275         ;; after all.
9276         (unless (memq (car articles) not-deleted)
9277           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9278         (let* ((article (car articles))
9279                (id (mail-header-id (gnus-data-header
9280                                     (assoc article (gnus-data-list nil))))))
9281           (run-hook-with-args 'gnus-summary-article-delete-hook
9282                               'delete id gnus-newsgroup-name nil
9283                               nil))
9284         (setq articles (cdr articles)))
9285       (when not-deleted
9286         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9287     (gnus-summary-position-point)
9288     (gnus-set-mode-line 'summary)
9289     not-deleted))
9290
9291 (defun gnus-summary-edit-article (&optional arg)
9292   "Edit the current article.
9293 This will have permanent effect only in mail groups.
9294 If ARG is nil, edit the decoded articles.
9295 If ARG is 1, edit the raw articles.
9296 If ARG is 2, edit the raw articles even in read-only groups.
9297 If ARG is 3, edit the articles with the current handles.
9298 Otherwise, allow editing of articles even in read-only
9299 groups."
9300   (interactive "P")
9301   (let (force raw current-handles)
9302     (cond
9303      ((null arg))
9304      ((eq arg 1)
9305       (setq raw t))
9306      ((eq arg 2)
9307       (setq raw t
9308             force t))
9309      ((eq arg 3)
9310       (setq current-handles
9311             (and (gnus-buffer-live-p gnus-article-buffer)
9312                  (with-current-buffer gnus-article-buffer
9313                    (prog1
9314                        gnus-article-mime-handles
9315                      (setq gnus-article-mime-handles nil))))))
9316      (t
9317       (setq force t)))
9318     (when (and raw (not force)
9319                (member gnus-newsgroup-name '("nndraft:delayed"
9320                                              "nndraft:drafts"
9321                                              "nndraft:queue")))
9322       (error "Can't edit the raw article in group %s"
9323              gnus-newsgroup-name))
9324     (save-excursion
9325       (set-buffer gnus-summary-buffer)
9326       (let ((mail-parse-charset gnus-newsgroup-charset)
9327             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9328         (gnus-set-global-variables)
9329         (when (and (not force)
9330                    (gnus-group-read-only-p))
9331           (error "The current newsgroup does not support article editing"))
9332         (gnus-summary-show-article t)
9333         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9334           (with-current-buffer gnus-article-buffer
9335             (mm-enable-multibyte)))
9336         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9337             (setq raw t))
9338         (gnus-article-edit-article
9339          (if raw 'ignore
9340            `(lambda ()
9341               (let ((mbl mml-buffer-list))
9342                 (setq mml-buffer-list nil)
9343                 (mime-to-mml ,'current-handles)
9344                 (let ((mbl1 mml-buffer-list))
9345                   (setq mml-buffer-list mbl)
9346                   (set (make-local-variable 'mml-buffer-list) mbl1))
9347                 (gnus-make-local-hook 'kill-buffer-hook)
9348                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9349          `(lambda (no-highlight)
9350             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9351                   (message-options message-options)
9352                   (message-options-set-recipient)
9353                   (mail-parse-ignored-charsets
9354                    ',gnus-newsgroup-ignored-charsets))
9355               ,(if (not raw) '(progn
9356                                 (mml-to-mime)
9357                                 (mml-destroy-buffers)
9358                                 (remove-hook 'kill-buffer-hook
9359                                              'mml-destroy-buffers t)
9360                                 (kill-local-variable 'mml-buffer-list)))
9361               (gnus-summary-edit-article-done
9362                ,(or (mail-header-references gnus-current-headers) "")
9363                ,(gnus-group-read-only-p)
9364                ,gnus-summary-buffer no-highlight))))))))
9365
9366 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9367
9368 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9369                                                  no-highlight)
9370   "Make edits to the current article permanent."
9371   (interactive)
9372   (save-excursion
9373    ;; The buffer restriction contains the entire article if it exists.
9374     (when (article-goto-body)
9375       (let ((lines (count-lines (point) (point-max)))
9376             (length (- (point-max) (point)))
9377             (case-fold-search t)
9378             (body (copy-marker (point))))
9379         (goto-char (point-min))
9380         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9381           (delete-region (match-beginning 1) (match-end 1))
9382           (insert (number-to-string length)))
9383         (goto-char (point-min))
9384         (when (re-search-forward
9385                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9386           (delete-region (match-beginning 1) (match-end 1))
9387           (insert (number-to-string length)))
9388         (goto-char (point-min))
9389         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9390           (delete-region (match-beginning 1) (match-end 1))
9391           (insert (number-to-string lines))))))
9392   ;; Replace the article.
9393   (let ((buf (current-buffer)))
9394     (with-temp-buffer
9395       (insert-buffer-substring buf)
9396
9397       (if (and (not read-only)
9398                (not (gnus-request-replace-article
9399                      (cdr gnus-article-current) (car gnus-article-current)
9400                      (current-buffer) t)))
9401           (error "Couldn't replace article")
9402         ;; Update the summary buffer.
9403         (if (and references
9404                  (equal (message-tokenize-header references " ")
9405                         (message-tokenize-header
9406                          (or (message-fetch-field "references") "") " ")))
9407             ;; We only have to update this line.
9408             (save-excursion
9409               (save-restriction
9410                 (message-narrow-to-head)
9411                 (let ((head (buffer-string))
9412                       header)
9413                   (with-temp-buffer
9414                     (insert (format "211 %d Article retrieved.\n"
9415                                     (cdr gnus-article-current)))
9416                     (insert head)
9417                     (insert ".\n")
9418                     (let ((nntp-server-buffer (current-buffer)))
9419                       (setq header (car (gnus-get-newsgroup-headers
9420                                          nil t))))
9421                     (save-excursion
9422                       (set-buffer gnus-summary-buffer)
9423                       (gnus-data-set-header
9424                        (gnus-data-find (cdr gnus-article-current))
9425                        header)
9426                       (gnus-summary-update-article-line
9427                        (cdr gnus-article-current) header)
9428                       (if (gnus-summary-goto-subject
9429                            (cdr gnus-article-current) nil t)
9430                           (gnus-summary-update-secondary-mark
9431                            (cdr gnus-article-current))))))))
9432           ;; Update threads.
9433           (set-buffer (or buffer gnus-summary-buffer))
9434           (gnus-summary-update-article (cdr gnus-article-current))
9435           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9436               (gnus-summary-update-secondary-mark
9437                (cdr gnus-article-current))))
9438         ;; Prettify the article buffer again.
9439         (unless no-highlight
9440           (save-excursion
9441             (set-buffer gnus-article-buffer)
9442             ;;;!!! Fix this -- article should be rehighlighted.
9443             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9444             (set-buffer gnus-original-article-buffer)
9445             (gnus-request-article
9446              (cdr gnus-article-current)
9447              (car gnus-article-current) (current-buffer))))
9448         ;; Prettify the summary buffer line.
9449         (when (gnus-visual-p 'summary-highlight 'highlight)
9450           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9451
9452 (defun gnus-summary-edit-wash (key)
9453   "Perform editing command KEY in the article buffer."
9454   (interactive
9455    (list
9456     (progn
9457       (message "%s" (concat (this-command-keys) "- "))
9458       (read-char))))
9459   (message "")
9460   (gnus-summary-edit-article)
9461   (execute-kbd-macro (concat (this-command-keys) key))
9462   (gnus-article-edit-done))
9463
9464 ;;; Respooling
9465
9466 (defun gnus-summary-respool-query (&optional silent trace)
9467   "Query where the respool algorithm would put this article."
9468   (interactive)
9469   (let (gnus-mark-article-hook)
9470     (gnus-summary-select-article)
9471     (save-excursion
9472       (set-buffer gnus-original-article-buffer)
9473       (save-restriction
9474         (message-narrow-to-head)
9475         (let ((groups (nnmail-article-group 'identity trace)))
9476           (unless silent
9477             (if groups
9478                 (message "This message would go to %s"
9479                          (mapconcat 'car groups ", "))
9480               (message "This message would go to no groups"))
9481             groups))))))
9482
9483 (defun gnus-summary-respool-trace ()
9484   "Trace where the respool algorithm would put this article.
9485 Display a buffer showing all fancy splitting patterns which matched."
9486   (interactive)
9487   (gnus-summary-respool-query nil t))
9488
9489 ;; Summary marking commands.
9490
9491 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9492   "Mark articles which has the same subject as read, and then select the next.
9493 If UNMARK is positive, remove any kind of mark.
9494 If UNMARK is negative, tick articles."
9495   (interactive "P")
9496   (when unmark
9497     (setq unmark (prefix-numeric-value unmark)))
9498   (let ((count
9499          (gnus-summary-mark-same-subject
9500           (gnus-summary-article-subject) unmark)))
9501     ;; Select next unread article.  If auto-select-same mode, should
9502     ;; select the first unread article.
9503     (gnus-summary-next-article t (and gnus-auto-select-same
9504                                       (gnus-summary-article-subject)))
9505     (gnus-message 7 "%d article%s marked as %s"
9506                   count (if (= count 1) " is" "s are")
9507                   (if unmark "unread" "read"))))
9508
9509 (defun gnus-summary-kill-same-subject (&optional unmark)
9510   "Mark articles which has the same subject as read.
9511 If UNMARK is positive, remove any kind of mark.
9512 If UNMARK is negative, tick articles."
9513   (interactive "P")
9514   (when unmark
9515     (setq unmark (prefix-numeric-value unmark)))
9516   (let ((count
9517          (gnus-summary-mark-same-subject
9518           (gnus-summary-article-subject) unmark)))
9519     ;; If marked as read, go to next unread subject.
9520     (when (null unmark)
9521       ;; Go to next unread subject.
9522       (gnus-summary-next-subject 1 t))
9523     (gnus-message 7 "%d articles are marked as %s"
9524                   count (if unmark "unread" "read"))))
9525
9526 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9527   "Mark articles with same SUBJECT as read, and return marked number.
9528 If optional argument UNMARK is positive, remove any kinds of marks.
9529 If optional argument UNMARK is negative, mark articles as unread instead."
9530   (let ((count 1))
9531     (save-excursion
9532       (cond
9533        ((null unmark)                   ; Mark as read.
9534         (while (and
9535                 (progn
9536                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9537                   (gnus-summary-show-thread) t)
9538                 (gnus-summary-find-subject subject))
9539           (setq count (1+ count))))
9540        ((> unmark 0)                    ; Tick.
9541         (while (and
9542                 (progn
9543                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9544                   (gnus-summary-show-thread) t)
9545                 (gnus-summary-find-subject subject))
9546           (setq count (1+ count))))
9547        (t                               ; Mark as unread.
9548         (while (and
9549                 (progn
9550                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9551                   (gnus-summary-show-thread) t)
9552                 (gnus-summary-find-subject subject))
9553           (setq count (1+ count)))))
9554       (gnus-set-mode-line 'summary)
9555       ;; Return the number of marked articles.
9556       count)))
9557
9558 (defun gnus-summary-mark-as-processable (n &optional unmark)
9559   "Set the process mark on the next N articles.
9560 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9561 the process mark instead.  The difference between N and the actual
9562 number of articles marked is returned."
9563   (interactive "P")
9564   (if (and (null n) (gnus-region-active-p))
9565       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9566     (setq n (prefix-numeric-value n))
9567     (let ((backward (< n 0))
9568           (n (abs n)))
9569       (while (and
9570               (> n 0)
9571               (if unmark
9572                   (gnus-summary-remove-process-mark
9573                    (gnus-summary-article-number))
9574                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9575               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9576         (setq n (1- n)))
9577       (when (/= 0 n)
9578         (gnus-message 7 "No more articles"))
9579       (gnus-summary-recenter)
9580       (gnus-summary-position-point)
9581       n)))
9582
9583 (defun gnus-summary-unmark-as-processable (n)
9584   "Remove the process mark from the next N articles.
9585 If N is negative, unmark backward instead.  The difference between N and
9586 the actual number of articles unmarked is returned."
9587   (interactive "P")
9588   (gnus-summary-mark-as-processable n t))
9589
9590 (defun gnus-summary-unmark-all-processable ()
9591   "Remove the process mark from all articles."
9592   (interactive)
9593   (save-excursion
9594     (while gnus-newsgroup-processable
9595       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9596   (gnus-summary-position-point))
9597
9598 (defun gnus-summary-add-mark (article type)
9599   "Mark ARTICLE with a mark of TYPE."
9600   (let ((vtype (car (assq type gnus-article-mark-lists)))
9601         var)
9602     (if (not vtype)
9603         (error "No such mark type: %s" type)
9604       (setq var (intern (format "gnus-newsgroup-%s" type)))
9605       (set var (cons article (symbol-value var)))
9606       (if (memq type '(processable cached replied forwarded recent saved))
9607           (gnus-summary-update-secondary-mark article)
9608         ;;; !!! This is bogus.  We should find out what primary
9609         ;;; !!! mark we want to set.
9610         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9611
9612 (defun gnus-summary-mark-as-expirable (n)
9613   "Mark N articles forward as expirable.
9614 If N is negative, mark backward instead.  The difference between N and
9615 the actual number of articles marked is returned."
9616   (interactive "p")
9617   (gnus-summary-mark-forward n gnus-expirable-mark))
9618
9619 (defun gnus-summary-mark-as-spam (n)
9620   "Mark N articles forward as spam.
9621 If N is negative, mark backward instead.  The difference between N and
9622 the actual number of articles marked is returned."
9623   (interactive "p")
9624   (gnus-summary-mark-forward n gnus-spam-mark))
9625
9626 (defun gnus-summary-mark-article-as-replied (article)
9627   "Mark ARTICLE as replied to and update the summary line.
9628 ARTICLE can also be a list of articles."
9629   (interactive (list (gnus-summary-article-number)))
9630   (let ((articles (if (listp article) article (list article))))
9631     (dolist (article articles)
9632       (unless (numberp article)
9633         (error "%s is not a number" article))
9634       (push article gnus-newsgroup-replied)
9635       (let ((buffer-read-only nil))
9636         (when (gnus-summary-goto-subject article nil t)
9637           (gnus-summary-update-secondary-mark article))))))
9638
9639 (defun gnus-summary-mark-article-as-forwarded (article)
9640   "Mark ARTICLE as forwarded and update the summary line.
9641 ARTICLE can also be a list of articles."
9642   (let ((articles (if (listp article) article (list article))))
9643     (dolist (article articles)
9644       (push article gnus-newsgroup-forwarded)
9645       (let ((buffer-read-only nil))
9646         (when (gnus-summary-goto-subject article nil t)
9647           (gnus-summary-update-secondary-mark article))))))
9648
9649 (defun gnus-summary-set-bookmark (article)
9650   "Set a bookmark in current article."
9651   (interactive (list (gnus-summary-article-number)))
9652   (when (or (not (get-buffer gnus-article-buffer))
9653             (not gnus-current-article)
9654             (not gnus-article-current)
9655             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9656     (error "No current article selected"))
9657   ;; Remove old bookmark, if one exists.
9658   (gnus-pull article gnus-newsgroup-bookmarks)
9659   ;; Set the new bookmark, which is on the form
9660   ;; (article-number . line-number-in-body).
9661   (push
9662    (cons article
9663          (save-excursion
9664            (set-buffer gnus-article-buffer)
9665            (count-lines
9666             (min (point)
9667                  (save-excursion
9668                    (article-goto-body)
9669                    (point)))
9670             (point))))
9671    gnus-newsgroup-bookmarks)
9672   (gnus-message 6 "A bookmark has been added to the current article."))
9673
9674 (defun gnus-summary-remove-bookmark (article)
9675   "Remove the bookmark from the current article."
9676   (interactive (list (gnus-summary-article-number)))
9677   ;; Remove old bookmark, if one exists.
9678   (if (not (assq article gnus-newsgroup-bookmarks))
9679       (gnus-message 6 "No bookmark in current article.")
9680     (gnus-pull article gnus-newsgroup-bookmarks)
9681     (gnus-message 6 "Removed bookmark.")))
9682
9683 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9684 (defun gnus-summary-mark-as-dormant (n)
9685   "Mark N articles forward as dormant.
9686 If N is negative, mark backward instead.  The difference between N and
9687 the actual number of articles marked is returned."
9688   (interactive "p")
9689   (gnus-summary-mark-forward n gnus-dormant-mark))
9690
9691 (defun gnus-summary-set-process-mark (article)
9692   "Set the process mark on ARTICLE and update the summary line."
9693   (setq gnus-newsgroup-processable
9694         (cons article
9695               (delq article gnus-newsgroup-processable)))
9696   (when (gnus-summary-goto-subject article)
9697     (gnus-summary-show-thread)
9698     (gnus-summary-goto-subject article)
9699     (gnus-summary-update-secondary-mark article)))
9700
9701 (defun gnus-summary-remove-process-mark (article)
9702   "Remove the process mark from ARTICLE and update the summary line."
9703   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9704   (when (gnus-summary-goto-subject article)
9705     (gnus-summary-show-thread)
9706     (gnus-summary-goto-subject article)
9707     (gnus-summary-update-secondary-mark article)))
9708
9709 (defun gnus-summary-set-saved-mark (article)
9710   "Set the process mark on ARTICLE and update the summary line."
9711   (push article gnus-newsgroup-saved)
9712   (when (gnus-summary-goto-subject article)
9713     (gnus-summary-update-secondary-mark article)))
9714
9715 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9716   "Mark N articles as read forwards.
9717 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9718 The difference between N and the actual number of articles marked is
9719 returned.
9720 If NO-EXPIRE, auto-expiry will be inhibited."
9721   (interactive "p")
9722   (gnus-summary-show-thread)
9723   (let ((backward (< n 0))
9724         (gnus-summary-goto-unread
9725          (and gnus-summary-goto-unread
9726               (not (eq gnus-summary-goto-unread 'never))
9727               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9728                                     gnus-ticked-mark gnus-dormant-mark)))))
9729         (n (abs n))
9730         (mark (or mark gnus-del-mark)))
9731     (while (and (> n 0)
9732                 (gnus-summary-mark-article nil mark no-expire)
9733                 (zerop (gnus-summary-next-subject
9734                         (if backward -1 1)
9735                         (and gnus-summary-goto-unread
9736                              (not (eq gnus-summary-goto-unread 'never)))
9737                         t)))
9738       (setq n (1- n)))
9739     (when (/= 0 n)
9740       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9741     (gnus-summary-recenter)
9742     (gnus-summary-position-point)
9743     (gnus-set-mode-line 'summary)
9744     n))
9745
9746 (defun gnus-summary-mark-article-as-read (mark)
9747   "Mark the current article quickly as read with MARK."
9748   (let ((article (gnus-summary-article-number)))
9749     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9750     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9751     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9752     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9753     (push (cons article mark) gnus-newsgroup-reads)
9754     ;; Possibly remove from cache, if that is used.
9755     (when gnus-use-cache
9756       (gnus-cache-enter-remove-article article))
9757     ;; Allow the backend to change the mark.
9758     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9759     ;; Check for auto-expiry.
9760     (when (and gnus-newsgroup-auto-expire
9761                (memq mark gnus-auto-expirable-marks))
9762       (setq mark gnus-expirable-mark)
9763       ;; Let the backend know about the mark change.
9764       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9765       (push article gnus-newsgroup-expirable))
9766     ;; Set the mark in the buffer.
9767     (gnus-summary-update-mark mark 'unread)
9768     t))
9769
9770 (defun gnus-summary-mark-article-as-unread (mark)
9771   "Mark the current article quickly as unread with MARK."
9772   (let* ((article (gnus-summary-article-number))
9773          (old-mark (gnus-summary-article-mark article)))
9774     ;; Allow the backend to change the mark.
9775     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9776     (if (eq mark old-mark)
9777         t
9778       (if (<= article 0)
9779           (progn
9780             (gnus-error 1 "Can't mark negative article numbers")
9781             nil)
9782         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9783         (setq gnus-newsgroup-spam-marked
9784               (delq article gnus-newsgroup-spam-marked))
9785         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9786         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9787         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9788         (cond ((= mark gnus-ticked-mark)
9789                (setq gnus-newsgroup-marked
9790                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9791                                               article)))
9792               ((= mark gnus-spam-mark)
9793                (setq gnus-newsgroup-spam-marked
9794                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9795                                               article)))
9796               ((= mark gnus-dormant-mark)
9797                (setq gnus-newsgroup-dormant
9798                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9799                                               article)))
9800               (t
9801                (setq gnus-newsgroup-unreads
9802                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9803                                               article))))
9804         (gnus-pull article gnus-newsgroup-reads)
9805
9806         ;; See whether the article is to be put in the cache.
9807         (and gnus-use-cache
9808              (vectorp (gnus-summary-article-header article))
9809              (save-excursion
9810                (gnus-cache-possibly-enter-article
9811                 gnus-newsgroup-name article
9812                 (= mark gnus-ticked-mark)
9813                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9814
9815         ;; Fix the mark.
9816         (gnus-summary-update-mark mark 'unread)
9817         t))))
9818
9819 (defun gnus-summary-mark-article (&optional article mark no-expire)
9820   "Mark ARTICLE with MARK.  MARK can be any character.
9821 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9822 `??' (dormant) and `?E' (expirable).
9823 If MARK is nil, then the default character `?r' is used.
9824 If ARTICLE is nil, then the article on the current line will be
9825 marked.
9826 If NO-EXPIRE, auto-expiry will be inhibited."
9827   ;; The mark might be a string.
9828   (when (stringp mark)
9829     (setq mark (aref mark 0)))
9830   ;; If no mark is given, then we check auto-expiring.
9831   (when (null mark)
9832     (setq mark gnus-del-mark))
9833   (when (and (not no-expire)
9834              gnus-newsgroup-auto-expire
9835              (memq mark gnus-auto-expirable-marks))
9836     (setq mark gnus-expirable-mark))
9837   (let ((article (or article (gnus-summary-article-number)))
9838         (old-mark (gnus-summary-article-mark article)))
9839     ;; Allow the backend to change the mark.
9840     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9841     (if (eq mark old-mark)
9842         t
9843       (unless article
9844         (error "No article on current line"))
9845       (if (not (if (or (= mark gnus-unread-mark)
9846                        (= mark gnus-ticked-mark)
9847                        (= mark gnus-spam-mark)
9848                        (= mark gnus-dormant-mark))
9849                    (gnus-mark-article-as-unread article mark)
9850                  (gnus-mark-article-as-read article mark)))
9851           t
9852         ;; See whether the article is to be put in the cache.
9853         (and gnus-use-cache
9854              (not (= mark gnus-canceled-mark))
9855              (vectorp (gnus-summary-article-header article))
9856              (save-excursion
9857                (gnus-cache-possibly-enter-article
9858                 gnus-newsgroup-name article
9859                 (= mark gnus-ticked-mark)
9860                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9861
9862         (when (gnus-summary-goto-subject article nil t)
9863           (let ((buffer-read-only nil))
9864             (gnus-summary-show-thread)
9865             ;; Fix the mark.
9866             (gnus-summary-update-mark mark 'unread)
9867             t))))))
9868
9869 (defun gnus-summary-update-secondary-mark (article)
9870   "Update the secondary (read, process, cache) mark."
9871   (gnus-summary-update-mark
9872    (cond ((memq article gnus-newsgroup-processable)
9873           gnus-process-mark)
9874          ((memq article gnus-newsgroup-cached)
9875           gnus-cached-mark)
9876          ((memq article gnus-newsgroup-replied)
9877           gnus-replied-mark)
9878          ((memq article gnus-newsgroup-forwarded)
9879           gnus-forwarded-mark)
9880          ((memq article gnus-newsgroup-saved)
9881           gnus-saved-mark)
9882          ((memq article gnus-newsgroup-recent)
9883           gnus-recent-mark)
9884          ((memq article gnus-newsgroup-unseen)
9885           gnus-unseen-mark)
9886          (t gnus-no-mark))
9887    'replied)
9888   (when (gnus-visual-p 'summary-highlight 'highlight)
9889     (gnus-run-hooks 'gnus-summary-update-hook))
9890   t)
9891
9892 (defun gnus-summary-update-download-mark (article)
9893   "Update the download mark."
9894   (gnus-summary-update-mark
9895    (cond ((memq article gnus-newsgroup-undownloaded)
9896           gnus-undownloaded-mark)
9897          (gnus-newsgroup-agentized
9898           gnus-downloaded-mark)
9899          (t
9900           gnus-no-mark))
9901    'download)
9902   (gnus-summary-update-line t)
9903   t)
9904
9905 (defun gnus-summary-update-mark (mark type)
9906   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9907         (buffer-read-only nil))
9908     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9909     (when forward
9910       (when (looking-at "\r")
9911         (incf forward))
9912       (when (<= (+ forward (point)) (point-max))
9913         ;; Go to the right position on the line.
9914         (goto-char (+ forward (point)))
9915         ;; Replace the old mark with the new mark.
9916         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9917         ;; Optionally update the marks by some user rule.
9918         (when (eq type 'unread)
9919           (gnus-data-set-mark
9920            (gnus-data-find (gnus-summary-article-number)) mark)
9921           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9922
9923 (defun gnus-mark-article-as-read (article &optional mark)
9924   "Enter ARTICLE in the pertinent lists and remove it from others."
9925   ;; Make the article expirable.
9926   (let ((mark (or mark gnus-del-mark)))
9927     (setq gnus-newsgroup-expirable
9928           (if (= mark gnus-expirable-mark)
9929               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9930             (delq article gnus-newsgroup-expirable)))
9931     ;; Remove from unread and marked lists.
9932     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9933     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9934     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9935     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9936     (push (cons article mark) gnus-newsgroup-reads)
9937     ;; Possibly remove from cache, if that is used.
9938     (when gnus-use-cache
9939       (gnus-cache-enter-remove-article article))
9940     t))
9941
9942 (defun gnus-mark-article-as-unread (article &optional mark)
9943   "Enter ARTICLE in the pertinent lists and remove it from others."
9944   (let ((mark (or mark gnus-ticked-mark)))
9945     (if (<= article 0)
9946         (progn
9947           (gnus-error 1 "Can't mark negative article numbers")
9948           nil)
9949       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9950             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9951             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9952             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9953             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9954
9955       ;; Unsuppress duplicates?
9956       (when gnus-suppress-duplicates
9957         (gnus-dup-unsuppress-article article))
9958
9959       (cond ((= mark gnus-ticked-mark)
9960              (setq gnus-newsgroup-marked
9961                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9962             ((= mark gnus-spam-mark)
9963              (setq gnus-newsgroup-spam-marked
9964                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9965                                             article)))
9966             ((= mark gnus-dormant-mark)
9967              (setq gnus-newsgroup-dormant
9968                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9969             (t
9970              (setq gnus-newsgroup-unreads
9971                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9972       (gnus-pull article gnus-newsgroup-reads)
9973       t)))
9974
9975 (defalias 'gnus-summary-mark-as-unread-forward
9976   'gnus-summary-tick-article-forward)
9977 (make-obsolete 'gnus-summary-mark-as-unread-forward
9978                'gnus-summary-tick-article-forward)
9979 (defun gnus-summary-tick-article-forward (n)
9980   "Tick N articles forwards.
9981 If N is negative, tick backwards instead.
9982 The difference between N and the number of articles ticked is returned."
9983   (interactive "p")
9984   (gnus-summary-mark-forward n gnus-ticked-mark))
9985
9986 (defalias 'gnus-summary-mark-as-unread-backward
9987   'gnus-summary-tick-article-backward)
9988 (make-obsolete 'gnus-summary-mark-as-unread-backward
9989                'gnus-summary-tick-article-backward)
9990 (defun gnus-summary-tick-article-backward (n)
9991   "Tick N articles backwards.
9992 The difference between N and the number of articles ticked is returned."
9993   (interactive "p")
9994   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9995
9996 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9997 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9998 (defun gnus-summary-tick-article (&optional article clear-mark)
9999   "Mark current article as unread.
10000 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10001 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10002   (interactive)
10003   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10004                                        gnus-ticked-mark)))
10005
10006 (defun gnus-summary-mark-as-read-forward (n)
10007   "Mark N articles as read forwards.
10008 If N is negative, mark backwards instead.
10009 The difference between N and the actual number of articles marked is
10010 returned."
10011   (interactive "p")
10012   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10013
10014 (defun gnus-summary-mark-as-read-backward (n)
10015   "Mark the N articles as read backwards.
10016 The difference between N and the actual number of articles marked is
10017 returned."
10018   (interactive "p")
10019   (gnus-summary-mark-forward
10020    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10021
10022 (defun gnus-summary-mark-as-read (&optional article mark)
10023   "Mark current article as read.
10024 ARTICLE specifies the article to be marked as read.
10025 MARK specifies a string to be inserted at the beginning of the line."
10026   (gnus-summary-mark-article article mark))
10027
10028 (defun gnus-summary-clear-mark-forward (n)
10029   "Clear marks from N articles forward.
10030 If N is negative, clear backward instead.
10031 The difference between N and the number of marks cleared is returned."
10032   (interactive "p")
10033   (gnus-summary-mark-forward n gnus-unread-mark))
10034
10035 (defun gnus-summary-clear-mark-backward (n)
10036   "Clear marks from N articles backward.
10037 The difference between N and the number of marks cleared is returned."
10038   (interactive "p")
10039   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10040
10041 (defun gnus-summary-mark-unread-as-read ()
10042   "Intended to be used by `gnus-summary-mark-article-hook'."
10043   (when (memq gnus-current-article gnus-newsgroup-unreads)
10044     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10045
10046 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10047   "Intended to be used by `gnus-summary-mark-article-hook'."
10048   (let ((mark (gnus-summary-article-mark)))
10049     (when (or (gnus-unread-mark-p mark)
10050               (gnus-read-mark-p mark))
10051       (gnus-summary-mark-article gnus-current-article
10052                                  (or new-mark gnus-read-mark)))))
10053
10054 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10055   "Intended to be used by `gnus-summary-mark-article-hook'."
10056   (let ((mark (gnus-summary-article-mark)))
10057     (when (or (gnus-unread-mark-p mark)
10058               (gnus-read-mark-p mark))
10059       (gnus-summary-mark-article (gnus-summary-article-number)
10060                                  (or new-mark gnus-read-mark)))))
10061
10062 (defun gnus-summary-mark-unread-as-ticked ()
10063   "Intended to be used by `gnus-summary-mark-article-hook'."
10064   (when (memq gnus-current-article gnus-newsgroup-unreads)
10065     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10066
10067 (defun gnus-summary-mark-region-as-read (point mark all)
10068   "Mark all unread articles between point and mark as read.
10069 If given a prefix, mark all articles between point and mark as read,
10070 even ticked and dormant ones."
10071   (interactive "r\nP")
10072   (save-excursion
10073     (let (article)
10074       (goto-char point)
10075       (beginning-of-line)
10076       (while (and
10077               (< (point) mark)
10078               (progn
10079                 (when (or all
10080                           (memq (setq article (gnus-summary-article-number))
10081                                 gnus-newsgroup-unreads))
10082                   (gnus-summary-mark-article article gnus-del-mark))
10083                 t)
10084               (gnus-summary-find-next))))))
10085
10086 (defun gnus-summary-mark-below (score mark)
10087   "Mark articles with score less than SCORE with MARK."
10088   (interactive "P\ncMark: ")
10089   (setq score (if score
10090                   (prefix-numeric-value score)
10091                 (or gnus-summary-default-score 0)))
10092   (save-excursion
10093     (set-buffer gnus-summary-buffer)
10094     (goto-char (point-min))
10095     (while
10096         (progn
10097           (and (< (gnus-summary-article-score) score)
10098                (gnus-summary-mark-article nil mark))
10099           (gnus-summary-find-next)))))
10100
10101 (defun gnus-summary-kill-below (&optional score)
10102   "Mark articles with score below SCORE as read."
10103   (interactive "P")
10104   (gnus-summary-mark-below score gnus-killed-mark))
10105
10106 (defun gnus-summary-clear-above (&optional score)
10107   "Clear all marks from articles with score above SCORE."
10108   (interactive "P")
10109   (gnus-summary-mark-above score gnus-unread-mark))
10110
10111 (defun gnus-summary-tick-above (&optional score)
10112   "Tick all articles with score above SCORE."
10113   (interactive "P")
10114   (gnus-summary-mark-above score gnus-ticked-mark))
10115
10116 (defun gnus-summary-mark-above (score mark)
10117   "Mark articles with score over SCORE with MARK."
10118   (interactive "P\ncMark: ")
10119   (setq score (if score
10120                   (prefix-numeric-value score)
10121                 (or gnus-summary-default-score 0)))
10122   (save-excursion
10123     (set-buffer gnus-summary-buffer)
10124     (goto-char (point-min))
10125     (while (and (progn
10126                   (when (> (gnus-summary-article-score) score)
10127                     (gnus-summary-mark-article nil mark))
10128                   t)
10129                 (gnus-summary-find-next)))))
10130
10131 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10132 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10133 (defun gnus-summary-limit-include-expunged (&optional no-error)
10134   "Display all the hidden articles that were expunged for low scores."
10135   (interactive)
10136   (let ((buffer-read-only nil))
10137     (let ((scored gnus-newsgroup-scored)
10138           headers h)
10139       (while scored
10140         (unless (gnus-summary-article-header (caar scored))
10141           (and (setq h (gnus-number-to-header (caar scored)))
10142                (< (cdar scored) gnus-summary-expunge-below)
10143                (push h headers)))
10144         (setq scored (cdr scored)))
10145       (if (not headers)
10146           (when (not no-error)
10147             (error "No expunged articles hidden"))
10148         (goto-char (point-min))
10149         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10150         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10151         (mapcar (lambda (x) (push (mail-header-number x)
10152                                   gnus-newsgroup-limit))
10153                 headers)
10154         (gnus-summary-prepare-unthreaded (nreverse headers))
10155         (goto-char (point-min))
10156         (gnus-summary-position-point)
10157         t))))
10158
10159 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10160   "Mark all unread articles in this newsgroup as read.
10161 If prefix argument ALL is non-nil, ticked and dormant articles will
10162 also be marked as read.
10163 If QUIETLY is non-nil, no questions will be asked.
10164
10165 If TO-HERE is non-nil, it should be a point in the buffer.  All
10166 articles before (after, if REVERSE is set) this point will be marked
10167 as read.
10168
10169 Note that this function will only catch up the unread article
10170 in the current summary buffer limitation.
10171
10172 The number of articles marked as read is returned."
10173   (interactive "P")
10174   (prog1
10175       (save-excursion
10176         (when (or quietly
10177                   (not gnus-interactive-catchup) ;Without confirmation?
10178                   gnus-expert-user
10179                   (gnus-y-or-n-p
10180                    (if all
10181                        "Mark absolutely all articles as read? "
10182                      "Mark all unread articles as read? ")))
10183           (if (and not-mark
10184                    (not gnus-newsgroup-adaptive)
10185                    (not gnus-newsgroup-auto-expire)
10186                    (not gnus-suppress-duplicates)
10187                    (or (not gnus-use-cache)
10188                        (eq gnus-use-cache 'passive)))
10189               (progn
10190                 (when all
10191                   (setq gnus-newsgroup-marked nil
10192                         gnus-newsgroup-spam-marked nil
10193                         gnus-newsgroup-dormant nil))
10194                 (setq gnus-newsgroup-unreads
10195                       (gnus-sorted-nunion
10196                        (gnus-intersection gnus-newsgroup-unreads
10197                                           gnus-newsgroup-downloadable)
10198                        gnus-newsgroup-unfetched)))
10199             ;; We actually mark all articles as canceled, which we
10200             ;; have to do when using auto-expiry or adaptive scoring.
10201             (gnus-summary-show-all-threads)
10202             (if (and to-here reverse)
10203                 (progn
10204                   (goto-char to-here)
10205                   (gnus-summary-mark-current-read-and-unread-as-read
10206                    gnus-catchup-mark)
10207                   (while (gnus-summary-find-next (not all))
10208                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10209               (when (gnus-summary-first-subject (not all))
10210                 (while (and
10211                         (if to-here (< (point) to-here) t)
10212                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10213                         (gnus-summary-find-next (not all))))))
10214             (gnus-set-mode-line 'summary))
10215           t))
10216     (gnus-summary-position-point)))
10217
10218 (defun gnus-summary-catchup-to-here (&optional all)
10219   "Mark all unticked articles before the current one as read.
10220 If ALL is non-nil, also mark ticked and dormant articles as read."
10221   (interactive "P")
10222   (save-excursion
10223     (gnus-save-hidden-threads
10224       (let ((beg (point)))
10225         ;; We check that there are unread articles.
10226         (when (or all (gnus-summary-find-prev))
10227           (gnus-summary-catchup all t beg)))))
10228   (gnus-summary-position-point))
10229
10230 (defun gnus-summary-catchup-from-here (&optional all)
10231   "Mark all unticked articles after (and including) the current one as read.
10232 If ALL is non-nil, also mark ticked and dormant articles as read."
10233   (interactive "P")
10234   (save-excursion
10235     (gnus-save-hidden-threads
10236       (let ((beg (point)))
10237         ;; We check that there are unread articles.
10238         (when (or all (gnus-summary-find-next))
10239           (gnus-summary-catchup all t beg nil t)))))
10240   (gnus-summary-position-point))
10241
10242 (defun gnus-summary-catchup-all (&optional quietly)
10243   "Mark all articles in this newsgroup as read.
10244 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10245 instead, which marks only unread articles as read."
10246   (interactive "P")
10247   (gnus-summary-catchup t quietly))
10248
10249 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10250   "Mark all unread articles in this group as read, then exit.
10251 If prefix argument ALL is non-nil, all articles are marked as read.
10252 If QUIETLY is non-nil, no questions will be asked."
10253   (interactive "P")
10254   (when (gnus-summary-catchup all quietly nil 'fast)
10255     ;; Select next newsgroup or exit.
10256     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10257              (eq gnus-auto-select-next 'quietly))
10258         (gnus-summary-next-group nil)
10259       (gnus-summary-exit))))
10260
10261 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10262   "Mark all articles in this newsgroup as read, and then exit.
10263 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10264 instead, which marks only unread articles as read."
10265   (interactive "P")
10266   (gnus-summary-catchup-and-exit t quietly))
10267
10268 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10269   "Mark all articles in this group as read and select the next group.
10270 If given a prefix, mark all articles, unread as well as ticked, as
10271 read."
10272   (interactive "P")
10273   (save-excursion
10274     (gnus-summary-catchup all))
10275   (gnus-summary-next-group))
10276
10277 ;;;
10278 ;;; with article
10279 ;;;
10280
10281 (defmacro gnus-with-article (article &rest forms)
10282   "Select ARTICLE and perform FORMS in the original article buffer.
10283 Then replace the article with the result."
10284   `(progn
10285      ;; We don't want the article to be marked as read.
10286      (let (gnus-mark-article-hook)
10287        (gnus-summary-select-article t t nil ,article))
10288      (set-buffer gnus-original-article-buffer)
10289      ,@forms
10290      (if (not (gnus-check-backend-function
10291                'request-replace-article (car gnus-article-current)))
10292          (gnus-message 5 "Read-only group; not replacing")
10293        (unless (gnus-request-replace-article
10294                 ,article (car gnus-article-current)
10295                 (current-buffer) t)
10296          (error "Couldn't replace article")))
10297      ;; The cache and backlog have to be flushed somewhat.
10298      (when gnus-keep-backlog
10299        (gnus-backlog-remove-article
10300         (car gnus-article-current) (cdr gnus-article-current)))
10301      (when gnus-use-cache
10302        (gnus-cache-update-article
10303         (car gnus-article-current) (cdr gnus-article-current)))))
10304
10305 (put 'gnus-with-article 'lisp-indent-function 1)
10306 (put 'gnus-with-article 'edebug-form-spec '(form body))
10307
10308 ;; Thread-based commands.
10309
10310 (defun gnus-summary-articles-in-thread (&optional article)
10311   "Return a list of all articles in the current thread.
10312 If ARTICLE is non-nil, return all articles in the thread that starts
10313 with that article."
10314   (let* ((article (or article (gnus-summary-article-number)))
10315          (data (gnus-data-find-list article))
10316          (top-level (gnus-data-level (car data)))
10317          (top-subject
10318           (cond ((null gnus-thread-operation-ignore-subject)
10319                  (gnus-simplify-subject-re
10320                   (mail-header-subject (gnus-data-header (car data)))))
10321                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10322                  (gnus-simplify-subject-fuzzy
10323                   (mail-header-subject (gnus-data-header (car data)))))
10324                 (t nil)))
10325          (end-point (save-excursion
10326                       (if (gnus-summary-go-to-next-thread)
10327                           (point) (point-max))))
10328          articles)
10329     (while (and data
10330                 (< (gnus-data-pos (car data)) end-point))
10331       (when (or (not top-subject)
10332                 (string= top-subject
10333                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10334                              (gnus-simplify-subject-fuzzy
10335                               (mail-header-subject
10336                                (gnus-data-header (car data))))
10337                            (gnus-simplify-subject-re
10338                             (mail-header-subject
10339                              (gnus-data-header (car data)))))))
10340         (push (gnus-data-number (car data)) articles))
10341       (unless (and (setq data (cdr data))
10342                    (> (gnus-data-level (car data)) top-level))
10343         (setq data nil)))
10344     ;; Return the list of articles.
10345     (nreverse articles)))
10346
10347 (defun gnus-summary-rethread-current ()
10348   "Rethread the thread the current article is part of."
10349   (interactive)
10350   (let* ((gnus-show-threads t)
10351          (article (gnus-summary-article-number))
10352          (id (mail-header-id (gnus-summary-article-header)))
10353          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10354     (unless id
10355       (error "No article on the current line"))
10356     (gnus-rebuild-thread id)
10357     (gnus-summary-goto-subject article)))
10358
10359 (defun gnus-summary-reparent-thread ()
10360   "Make the current article child of the marked (or previous) article.
10361
10362 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10363 is non-nil or the Subject: of both articles are the same."
10364   (interactive)
10365   (unless (not (gnus-group-read-only-p))
10366     (error "The current newsgroup does not support article editing"))
10367   (unless (<= (length gnus-newsgroup-processable) 1)
10368     (error "No more than one article may be marked"))
10369   (save-window-excursion
10370     (let ((gnus-article-buffer " *reparent*")
10371           (current-article (gnus-summary-article-number))
10372           ;; First grab the marked article, otherwise one line up.
10373           (parent-article (if (not (null gnus-newsgroup-processable))
10374                               (car gnus-newsgroup-processable)
10375                             (save-excursion
10376                               (if (eq (forward-line -1) 0)
10377                                   (gnus-summary-article-number)
10378                                 (error "Beginning of summary buffer"))))))
10379       (unless (not (eq current-article parent-article))
10380         (error "An article may not be self-referential"))
10381       (let ((message-id (mail-header-id
10382                          (gnus-summary-article-header parent-article))))
10383         (unless (and message-id (not (equal message-id "")))
10384           (error "No message-id in desired parent"))
10385         (gnus-with-article current-article
10386           (save-restriction
10387             (goto-char (point-min))
10388             (message-narrow-to-head)
10389             (if (re-search-forward "^References: " nil t)
10390                 (progn
10391                   (re-search-forward "^[^ \t]" nil t)
10392                   (forward-line -1)
10393                   (end-of-line)
10394                   (insert " " message-id))
10395               (insert "References: " message-id "\n"))))
10396         (set-buffer gnus-summary-buffer)
10397         (gnus-summary-unmark-all-processable)
10398         (gnus-summary-update-article current-article)
10399         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10400             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10401         (gnus-summary-rethread-current)
10402         (gnus-message 3 "Article %d is now the child of article %d"
10403                       current-article parent-article)))))
10404
10405 (defun gnus-summary-toggle-threads (&optional arg)
10406   "Toggle showing conversation threads.
10407 If ARG is positive number, turn showing conversation threads on."
10408   (interactive "P")
10409   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10410     (setq gnus-show-threads
10411           (if (null arg) (not gnus-show-threads)
10412             (> (prefix-numeric-value arg) 0)))
10413     (gnus-summary-prepare)
10414     (gnus-summary-goto-subject current)
10415     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10416     (gnus-summary-position-point)))
10417
10418 (defun gnus-summary-show-all-threads ()
10419   "Show all threads."
10420   (interactive)
10421   (save-excursion
10422     (let ((buffer-read-only nil))
10423       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10424   (gnus-summary-position-point))
10425
10426 (defun gnus-summary-show-thread ()
10427   "Show thread subtrees.
10428 Returns nil if no thread was there to be shown."
10429   (interactive)
10430   (let ((buffer-read-only nil)
10431         (orig (point))
10432         (end (gnus-point-at-eol))
10433         ;; Leave point at bol
10434         (beg (progn (beginning-of-line) (point))))
10435     (prog1
10436         ;; Any hidden lines here?
10437         (search-forward "\r" end t)
10438       (subst-char-in-region beg end ?\^M ?\n t)
10439       (goto-char orig)
10440       (gnus-summary-position-point))))
10441
10442 (defun gnus-summary-maybe-hide-threads ()
10443   "If requested, hide the threads that should be hidden."
10444   (when (and gnus-show-threads
10445              gnus-thread-hide-subtree)
10446     (gnus-summary-hide-all-threads
10447      (if (or (consp gnus-thread-hide-subtree)
10448              (functionp gnus-thread-hide-subtree))
10449          (gnus-make-predicate gnus-thread-hide-subtree)
10450        nil))))
10451
10452 ;;; Hiding predicates.
10453
10454 (defun gnus-article-unread-p (header)
10455   (memq (mail-header-number header) gnus-newsgroup-unreads))
10456
10457 (defun gnus-article-unseen-p (header)
10458   (memq (mail-header-number header) gnus-newsgroup-unseen))
10459
10460 (defun gnus-map-articles (predicate articles)
10461   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10462   (apply 'gnus-or (mapcar predicate
10463                           (mapcar 'gnus-summary-article-header articles))))
10464
10465 (defun gnus-summary-hide-all-threads (&optional predicate)
10466   "Hide all thread subtrees.
10467 If PREDICATE is supplied, threads that satisfy this predicate
10468 will not be hidden."
10469   (interactive)
10470   (save-excursion
10471     (goto-char (point-min))
10472     (let ((end nil))
10473       (while (not end)
10474         (when (or (not predicate)
10475                   (gnus-map-articles
10476                    predicate (gnus-summary-article-children)))
10477             (gnus-summary-hide-thread))
10478         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10479   (gnus-summary-position-point))
10480
10481 (defun gnus-summary-hide-thread ()
10482   "Hide thread subtrees.
10483 If PREDICATE is supplied, threads that satisfy this predicate
10484 will not be hidden.
10485 Returns nil if no threads were there to be hidden."
10486   (interactive)
10487   (let ((buffer-read-only nil)
10488         (start (point))
10489         (article (gnus-summary-article-number)))
10490     (goto-char start)
10491     ;; Go forward until either the buffer ends or the subthread
10492     ;; ends.
10493     (when (and (not (eobp))
10494                (or (zerop (gnus-summary-next-thread 1 t))
10495                    (goto-char (point-max))))
10496       (prog1
10497           (if (and (> (point) start)
10498                    (search-backward "\n" start t))
10499               (progn
10500                 (subst-char-in-region start (point) ?\n ?\^M)
10501                 (gnus-summary-goto-subject article))
10502             (goto-char start)
10503             nil)))))
10504
10505 (defun gnus-summary-go-to-next-thread (&optional previous)
10506   "Go to the same level (or less) next thread.
10507 If PREVIOUS is non-nil, go to previous thread instead.
10508 Return the article number moved to, or nil if moving was impossible."
10509   (let ((level (gnus-summary-thread-level))
10510         (way (if previous -1 1))
10511         (beg (point)))
10512     (forward-line way)
10513     (while (and (not (eobp))
10514                 (< level (gnus-summary-thread-level)))
10515       (forward-line way))
10516     (if (eobp)
10517         (progn
10518           (goto-char beg)
10519           nil)
10520       (setq beg (point))
10521       (prog1
10522           (gnus-summary-article-number)
10523         (goto-char beg)))))
10524
10525 (defun gnus-summary-next-thread (n &optional silent)
10526   "Go to the same level next N'th thread.
10527 If N is negative, search backward instead.
10528 Returns the difference between N and the number of skips actually
10529 done.
10530
10531 If SILENT, don't output messages."
10532   (interactive "p")
10533   (let ((backward (< n 0))
10534         (n (abs n)))
10535     (while (and (> n 0)
10536                 (gnus-summary-go-to-next-thread backward))
10537       (decf n))
10538     (unless silent
10539       (gnus-summary-position-point))
10540     (when (and (not silent) (/= 0 n))
10541       (gnus-message 7 "No more threads"))
10542     n))
10543
10544 (defun gnus-summary-prev-thread (n)
10545   "Go to the same level previous N'th thread.
10546 Returns the difference between N and the number of skips actually
10547 done."
10548   (interactive "p")
10549   (gnus-summary-next-thread (- n)))
10550
10551 (defun gnus-summary-go-down-thread ()
10552   "Go down one level in the current thread."
10553   (let ((children (gnus-summary-article-children)))
10554     (when children
10555       (gnus-summary-goto-subject (car children)))))
10556
10557 (defun gnus-summary-go-up-thread ()
10558   "Go up one level in the current thread."
10559   (let ((parent (gnus-summary-article-parent)))
10560     (when parent
10561       (gnus-summary-goto-subject parent))))
10562
10563 (defun gnus-summary-down-thread (n)
10564   "Go down thread N steps.
10565 If N is negative, go up instead.
10566 Returns the difference between N and how many steps down that were
10567 taken."
10568   (interactive "p")
10569   (let ((up (< n 0))
10570         (n (abs n)))
10571     (while (and (> n 0)
10572                 (if up (gnus-summary-go-up-thread)
10573                   (gnus-summary-go-down-thread)))
10574       (setq n (1- n)))
10575     (gnus-summary-position-point)
10576     (when (/= 0 n)
10577       (gnus-message 7 "Can't go further"))
10578     n))
10579
10580 (defun gnus-summary-up-thread (n)
10581   "Go up thread N steps.
10582 If N is negative, go down instead.
10583 Returns the difference between N and how many steps down that were
10584 taken."
10585   (interactive "p")
10586   (gnus-summary-down-thread (- n)))
10587
10588 (defun gnus-summary-top-thread ()
10589   "Go to the top of the thread."
10590   (interactive)
10591   (while (gnus-summary-go-up-thread))
10592   (gnus-summary-article-number))
10593
10594 (defun gnus-summary-kill-thread (&optional unmark)
10595   "Mark articles under current thread as read.
10596 If the prefix argument is positive, remove any kinds of marks.
10597 If the prefix argument is negative, tick articles instead."
10598   (interactive "P")
10599   (when unmark
10600     (setq unmark (prefix-numeric-value unmark)))
10601   (let ((articles (gnus-summary-articles-in-thread)))
10602     (save-excursion
10603       ;; Expand the thread.
10604       (gnus-summary-show-thread)
10605       ;; Mark all the articles.
10606       (while articles
10607         (gnus-summary-goto-subject (car articles))
10608         (cond ((null unmark)
10609                (gnus-summary-mark-article-as-read gnus-killed-mark))
10610               ((> unmark 0)
10611                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10612               (t
10613                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10614         (setq articles (cdr articles))))
10615     ;; Hide killed subtrees.
10616     (and (null unmark)
10617          gnus-thread-hide-killed
10618          (gnus-summary-hide-thread))
10619     ;; If marked as read, go to next unread subject.
10620     (when (null unmark)
10621       ;; Go to next unread subject.
10622       (gnus-summary-next-subject 1 t)))
10623   (gnus-set-mode-line 'summary))
10624
10625 ;; Summary sorting commands
10626
10627 (defun gnus-summary-sort-by-number (&optional reverse)
10628   "Sort the summary buffer by article number.
10629 Argument REVERSE means reverse order."
10630   (interactive "P")
10631   (gnus-summary-sort 'number reverse))
10632
10633 (defun gnus-summary-sort-by-random (&optional reverse)
10634   "Randomize the order in the summary buffer.
10635 Argument REVERSE means to randomize in reverse order."
10636   (interactive "P")
10637   (gnus-summary-sort 'random reverse))
10638
10639 (defun gnus-summary-sort-by-author (&optional reverse)
10640   "Sort the summary buffer by author name alphabetically.
10641 If `case-fold-search' is non-nil, case of letters is ignored.
10642 Argument REVERSE means reverse order."
10643   (interactive "P")
10644   (gnus-summary-sort 'author reverse))
10645
10646 (defun gnus-summary-sort-by-subject (&optional reverse)
10647   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10648 If `case-fold-search' is non-nil, case of letters is ignored.
10649 Argument REVERSE means reverse order."
10650   (interactive "P")
10651   (gnus-summary-sort 'subject reverse))
10652
10653 (defun gnus-summary-sort-by-date (&optional reverse)
10654   "Sort the summary buffer by date.
10655 Argument REVERSE means reverse order."
10656   (interactive "P")
10657   (gnus-summary-sort 'date reverse))
10658
10659 (defun gnus-summary-sort-by-score (&optional reverse)
10660   "Sort the summary buffer by score.
10661 Argument REVERSE means reverse order."
10662   (interactive "P")
10663   (gnus-summary-sort 'score reverse))
10664
10665 (defun gnus-summary-sort-by-lines (&optional reverse)
10666   "Sort the summary buffer by the number of lines.
10667 Argument REVERSE means reverse order."
10668   (interactive "P")
10669   (gnus-summary-sort 'lines reverse))
10670
10671 (defun gnus-summary-sort-by-chars (&optional reverse)
10672   "Sort the summary buffer by article length.
10673 Argument REVERSE means reverse order."
10674   (interactive "P")
10675   (gnus-summary-sort 'chars reverse))
10676
10677 (defun gnus-summary-sort-by-original (&optional reverse)
10678   "Sort the summary buffer using the default sorting method.
10679 Argument REVERSE means reverse order."
10680   (interactive "P")
10681   (let* ((buffer-read-only)
10682          (gnus-summary-prepare-hook nil))
10683     ;; We do the sorting by regenerating the threads.
10684     (gnus-summary-prepare)
10685     ;; Hide subthreads if needed.
10686     (gnus-summary-maybe-hide-threads)))
10687
10688 (defun gnus-summary-sort (predicate reverse)
10689   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10690   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10691          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10692          (gnus-thread-sort-functions
10693           (if (not reverse)
10694               thread
10695             `(lambda (t1 t2)
10696                (,thread t2 t1))))
10697          (gnus-sort-gathered-threads-function
10698           gnus-thread-sort-functions)
10699          (gnus-article-sort-functions
10700           (if (not reverse)
10701               article
10702             `(lambda (t1 t2)
10703                (,article t2 t1))))
10704          (buffer-read-only)
10705          (gnus-summary-prepare-hook nil))
10706     ;; We do the sorting by regenerating the threads.
10707     (gnus-summary-prepare)
10708     ;; Hide subthreads if needed.
10709     (gnus-summary-maybe-hide-threads)))
10710
10711 ;; Summary saving commands.
10712
10713 (defun gnus-summary-save-article (&optional n not-saved)
10714   "Save the current article using the default saver function.
10715 If N is a positive number, save the N next articles.
10716 If N is a negative number, save the N previous articles.
10717 If N is nil and any articles have been marked with the process mark,
10718 save those articles instead.
10719 The variable `gnus-default-article-saver' specifies the saver function."
10720   (interactive "P")
10721   (let* ((articles (gnus-summary-work-articles n))
10722          (save-buffer (save-excursion
10723                         (nnheader-set-temp-buffer " *Gnus Save*")))
10724          (num (length articles))
10725          header file)
10726     (dolist (article articles)
10727       (setq header (gnus-summary-article-header article))
10728       (if (not (vectorp header))
10729           ;; This is a pseudo-article.
10730           (if (assq 'name header)
10731               (gnus-copy-file (cdr (assq 'name header)))
10732             (gnus-message 1 "Article %d is unsaveable" article))
10733         ;; This is a real article.
10734         (save-window-excursion
10735           (let ((gnus-display-mime-function nil)
10736                 (gnus-article-prepare-hook nil))
10737             (gnus-summary-select-article t nil nil article)))
10738         (save-excursion
10739           (set-buffer save-buffer)
10740           (erase-buffer)
10741           (insert-buffer-substring gnus-original-article-buffer))
10742         (setq file (gnus-article-save save-buffer file num))
10743         (gnus-summary-remove-process-mark article)
10744         (unless not-saved
10745           (gnus-summary-set-saved-mark article))))
10746     (gnus-kill-buffer save-buffer)
10747     (gnus-summary-position-point)
10748     (gnus-set-mode-line 'summary)
10749     n))
10750
10751 (defun gnus-summary-pipe-output (&optional arg headers)
10752   "Pipe the current article to a subprocess.
10753 If N is a positive number, pipe the N next articles.
10754 If N is a negative number, pipe the N previous articles.
10755 If N is nil and any articles have been marked with the process mark,
10756 pipe those articles instead.
10757 If HEADERS (the symbolic prefix), include the headers, too."
10758   (interactive (gnus-interactive "P\ny"))
10759   (require 'gnus-art)
10760   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10761         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10762     (gnus-summary-save-article arg t))
10763   (let ((buffer (get-buffer "*Shell Command Output*")))
10764     (when (and buffer
10765                (not (zerop (buffer-size buffer))))
10766       (gnus-configure-windows 'pipe))))
10767
10768 (defun gnus-summary-save-article-mail (&optional arg)
10769   "Append the current article to an mail file.
10770 If N is a positive number, save the N next articles.
10771 If N is a negative number, save the N previous articles.
10772 If N is nil and any articles have been marked with the process mark,
10773 save those articles instead."
10774   (interactive "P")
10775   (require 'gnus-art)
10776   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10777     (gnus-summary-save-article arg)))
10778
10779 (defun gnus-summary-save-article-rmail (&optional arg)
10780   "Append the current article to an rmail file.
10781 If N is a positive number, save the N next articles.
10782 If N is a negative number, save the N previous articles.
10783 If N is nil and any articles have been marked with the process mark,
10784 save those articles instead."
10785   (interactive "P")
10786   (require 'gnus-art)
10787   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10788     (gnus-summary-save-article arg)))
10789
10790 (defun gnus-summary-save-article-file (&optional arg)
10791   "Append the current article to a file.
10792 If N is a positive number, save the N next articles.
10793 If N is a negative number, save the N previous articles.
10794 If N is nil and any articles have been marked with the process mark,
10795 save those articles instead."
10796   (interactive "P")
10797   (require 'gnus-art)
10798   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10799     (gnus-summary-save-article arg)))
10800
10801 (defun gnus-summary-write-article-file (&optional arg)
10802   "Write the current article to a file, deleting the previous file.
10803 If N is a positive number, save the N next articles.
10804 If N is a negative number, save the N previous articles.
10805 If N is nil and any articles have been marked with the process mark,
10806 save those articles instead."
10807   (interactive "P")
10808   (require 'gnus-art)
10809   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10810     (gnus-summary-save-article arg)))
10811
10812 (defun gnus-summary-save-article-body-file (&optional arg)
10813   "Append the current article body to a file.
10814 If N is a positive number, save the N next articles.
10815 If N is a negative number, save the N previous articles.
10816 If N is nil and any articles have been marked with the process mark,
10817 save those articles instead."
10818   (interactive "P")
10819   (require 'gnus-art)
10820   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10821     (gnus-summary-save-article arg)))
10822
10823 (defun gnus-summary-muttprint (&optional arg)
10824   "Print the current article using Muttprint.
10825 If N is a positive number, save the N next articles.
10826 If N is a negative number, save the N previous articles.
10827 If N is nil and any articles have been marked with the process mark,
10828 save those articles instead."
10829   (interactive "P")
10830   (require 'gnus-art)
10831   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10832     (gnus-summary-save-article arg t)))
10833
10834 (defun gnus-summary-pipe-message (program)
10835   "Pipe the current article through PROGRAM."
10836   (interactive "sProgram: ")
10837   (gnus-summary-select-article)
10838   (let ((mail-header-separator ""))
10839     (gnus-eval-in-buffer-window gnus-article-buffer
10840       (save-restriction
10841         (widen)
10842         (let ((start (window-start))
10843               buffer-read-only)
10844           (message-pipe-buffer-body program)
10845           (set-window-start (get-buffer-window (current-buffer)) start))))))
10846
10847 (defun gnus-get-split-value (methods)
10848   "Return a value based on the split METHODS."
10849   (let (split-name method result match)
10850     (when methods
10851       (save-excursion
10852         (set-buffer gnus-original-article-buffer)
10853         (save-restriction
10854           (nnheader-narrow-to-headers)
10855           (while (and methods (not split-name))
10856             (goto-char (point-min))
10857             (setq method (pop methods))
10858             (setq match (car method))
10859             (when (cond
10860                    ((stringp match)
10861                     ;; Regular expression.
10862                     (ignore-errors
10863                       (re-search-forward match nil t)))
10864                    ((functionp match)
10865                     ;; Function.
10866                     (save-restriction
10867                       (widen)
10868                       (setq result (funcall match gnus-newsgroup-name))))
10869                    ((consp match)
10870                     ;; Form.
10871                     (save-restriction
10872                       (widen)
10873                       (setq result (eval match)))))
10874               (setq split-name (cdr method))
10875               (cond ((stringp result)
10876                      (push (expand-file-name
10877                             result gnus-article-save-directory)
10878                            split-name))
10879                     ((consp result)
10880                      (setq split-name (append result split-name)))))))))
10881     (nreverse split-name)))
10882
10883 (defun gnus-valid-move-group-p (group)
10884   (and (boundp group)
10885        (symbol-name group)
10886        (symbol-value group)
10887        (gnus-get-function (gnus-find-method-for-group
10888                            (symbol-name group)) 'request-accept-article t)))
10889
10890 (defun gnus-read-move-group-name (prompt default articles prefix)
10891   "Read a group name."
10892   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10893          (minibuffer-confirm-incomplete nil) ; XEmacs
10894          (prom
10895           (format "%s %s to:"
10896                   prompt
10897                   (if (> (length articles) 1)
10898                       (format "these %d articles" (length articles))
10899                     "this article")))
10900          (to-newsgroup
10901           (cond
10902            ((null split-name)
10903             (gnus-completing-read-with-default
10904              default prom
10905              gnus-active-hashtb
10906              'gnus-valid-move-group-p
10907              nil prefix
10908              'gnus-group-history))
10909            ((= 1 (length split-name))
10910             (gnus-completing-read-with-default
10911              (car split-name) prom
10912              gnus-active-hashtb
10913              'gnus-valid-move-group-p
10914              nil nil
10915              'gnus-group-history))
10916            (t
10917             (gnus-completing-read-with-default
10918              nil prom
10919              (mapcar (lambda (el) (list el))
10920                      (nreverse split-name))
10921              nil nil nil
10922              'gnus-group-history))))
10923          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10924     (when to-newsgroup
10925       (if (or (string= to-newsgroup "")
10926               (string= to-newsgroup prefix))
10927           (setq to-newsgroup default))
10928       (unless to-newsgroup
10929         (error "No group name entered"))
10930       (or (gnus-active to-newsgroup)
10931           (gnus-activate-group to-newsgroup nil nil to-method)
10932           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10933                                      to-newsgroup))
10934               (or (and (gnus-request-create-group to-newsgroup to-method)
10935                        (gnus-activate-group
10936                         to-newsgroup nil nil to-method)
10937                        (gnus-subscribe-group to-newsgroup))
10938                   (error "Couldn't create group %s" to-newsgroup)))
10939           (error "No such group: %s" to-newsgroup)))
10940     to-newsgroup))
10941
10942 (defun gnus-summary-save-parts (type dir n &optional reverse)
10943   "Save parts matching TYPE to DIR.
10944 If REVERSE, save parts that do not match TYPE."
10945   (interactive
10946    (list (read-string "Save parts of type: "
10947                       (or (car gnus-summary-save-parts-type-history)
10948                           gnus-summary-save-parts-default-mime)
10949                       'gnus-summary-save-parts-type-history)
10950          (setq gnus-summary-save-parts-last-directory
10951                (read-file-name "Save to directory: "
10952                                gnus-summary-save-parts-last-directory
10953                                nil t))
10954          current-prefix-arg))
10955   (gnus-summary-iterate n
10956     (let ((gnus-display-mime-function nil)
10957           (gnus-inhibit-treatment t))
10958       (gnus-summary-select-article))
10959     (save-excursion
10960       (set-buffer gnus-article-buffer)
10961       (let ((handles (or gnus-article-mime-handles
10962                          (mm-dissect-buffer nil gnus-article-loose-mime)
10963                          (and gnus-article-emulate-mime
10964                               (mm-uu-dissect)))))
10965         (when handles
10966           (gnus-summary-save-parts-1 type dir handles reverse)
10967           (unless gnus-article-mime-handles ;; Don't destroy this case.
10968             (mm-destroy-parts handles)))))))
10969
10970 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10971   (if (stringp (car handle))
10972       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10973               (cdr handle))
10974     (when (if reverse
10975               (not (string-match type (mm-handle-media-type handle)))
10976             (string-match type (mm-handle-media-type handle)))
10977       (let ((file (expand-file-name
10978                    (file-name-nondirectory
10979                     (or
10980                      (mail-content-type-get
10981                       (mm-handle-disposition handle) 'filename)
10982                      (concat gnus-newsgroup-name
10983                              "." (number-to-string
10984                                   (cdr gnus-article-current)))))
10985                    dir)))
10986         (unless (file-exists-p file)
10987           (mm-save-part-to-file handle file))))))
10988
10989 ;; Summary extract commands
10990
10991 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10992   (let ((buffer-read-only nil)
10993         (article (gnus-summary-article-number))
10994         after-article b e)
10995     (unless (gnus-summary-goto-subject article)
10996       (error "No such article: %d" article))
10997     (gnus-summary-position-point)
10998     ;; If all commands are to be bunched up on one line, we collect
10999     ;; them here.
11000     (unless gnus-view-pseudos-separately
11001       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11002             files action)
11003         (while ps
11004           (setq action (cdr (assq 'action (car ps))))
11005           (setq files (list (cdr (assq 'name (car ps)))))
11006           (while (and ps (cdr ps)
11007                       (string= (or action "1")
11008                                (or (cdr (assq 'action (cadr ps))) "2")))
11009             (push (cdr (assq 'name (cadr ps))) files)
11010             (setcdr ps (cddr ps)))
11011           (when files
11012             (when (not (string-match "%s" action))
11013               (push " " files))
11014             (push " " files)
11015             (when (assq 'execute (car ps))
11016               (setcdr (assq 'execute (car ps))
11017                       (funcall (if (string-match "%s" action)
11018                                    'format 'concat)
11019                                action
11020                                (mapconcat
11021                                 (lambda (f)
11022                                   (if (equal f " ")
11023                                       f
11024                                     (mm-quote-arg f)))
11025                                 files " ")))))
11026           (setq ps (cdr ps)))))
11027     (if (and gnus-view-pseudos (not not-view))
11028         (while pslist
11029           (when (assq 'execute (car pslist))
11030             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11031                                   (eq gnus-view-pseudos 'not-confirm)))
11032           (setq pslist (cdr pslist)))
11033       (save-excursion
11034         (while pslist
11035           (setq after-article (or (cdr (assq 'article (car pslist)))
11036                                   (gnus-summary-article-number)))
11037           (gnus-summary-goto-subject after-article)
11038           (forward-line 1)
11039           (setq b (point))
11040           (insert "    " (file-name-nondirectory
11041                           (cdr (assq 'name (car pslist))))
11042                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11043           (setq e (point))
11044           (forward-line -1)             ; back to `b'
11045           (gnus-add-text-properties
11046            b (1- e) (list 'gnus-number gnus-reffed-article-number
11047                           gnus-mouse-face-prop gnus-mouse-face))
11048           (gnus-data-enter
11049            after-article gnus-reffed-article-number
11050            gnus-unread-mark b (car pslist) 0 (- e b))
11051           (setq gnus-newsgroup-unreads
11052                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11053                                          gnus-reffed-article-number))
11054           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11055           (setq pslist (cdr pslist)))))))
11056
11057 (defun gnus-pseudos< (p1 p2)
11058   (let ((c1 (cdr (assq 'action p1)))
11059         (c2 (cdr (assq 'action p2))))
11060     (and c1 c2 (string< c1 c2))))
11061
11062 (defun gnus-request-pseudo-article (props)
11063   (cond ((assq 'execute props)
11064          (gnus-execute-command (cdr (assq 'execute props)))))
11065   (let ((gnus-current-article (gnus-summary-article-number)))
11066     (gnus-run-hooks 'gnus-mark-article-hook)))
11067
11068 (defun gnus-execute-command (command &optional automatic)
11069   (save-excursion
11070     (gnus-article-setup-buffer)
11071     (set-buffer gnus-article-buffer)
11072     (setq buffer-read-only nil)
11073     (let ((command (if automatic command
11074                      (read-string "Command: " (cons command 0)))))
11075       (erase-buffer)
11076       (insert "$ " command "\n\n")
11077       (if gnus-view-pseudo-asynchronously
11078           (start-process "gnus-execute" (current-buffer) shell-file-name
11079                          shell-command-switch command)
11080         (call-process shell-file-name nil t nil
11081                       shell-command-switch command)))))
11082
11083 ;; Summary kill commands.
11084
11085 (defun gnus-summary-edit-global-kill (article)
11086   "Edit the \"global\" kill file."
11087   (interactive (list (gnus-summary-article-number)))
11088   (gnus-group-edit-global-kill article))
11089
11090 (defun gnus-summary-edit-local-kill ()
11091   "Edit a local kill file applied to the current newsgroup."
11092   (interactive)
11093   (setq gnus-current-headers (gnus-summary-article-header))
11094   (gnus-group-edit-local-kill
11095    (gnus-summary-article-number) gnus-newsgroup-name))
11096
11097 ;;; Header reading.
11098
11099 (defun gnus-read-header (id &optional header)
11100   "Read the headers of article ID and enter them into the Gnus system."
11101   (let ((group gnus-newsgroup-name)
11102         (gnus-override-method
11103          (or
11104           gnus-override-method
11105           (and (gnus-news-group-p gnus-newsgroup-name)
11106                (car (gnus-refer-article-methods)))))
11107         where)
11108     ;; First we check to see whether the header in question is already
11109     ;; fetched.
11110     (if (stringp id)
11111         ;; This is a Message-ID.
11112         (setq header (or header (gnus-id-to-header id)))
11113       ;; This is an article number.
11114       (setq header (or header (gnus-summary-article-header id))))
11115     (if (and header
11116              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11117         ;; We have found the header.
11118         header
11119       ;; If this is a sparse article, we have to nix out its
11120       ;; previous entry in the thread hashtb.
11121       (when (and header
11122                  (gnus-summary-article-sparse-p (mail-header-number header)))
11123         (let* ((parent (gnus-parent-id (mail-header-references header)))
11124                (thread (and parent (gnus-id-to-thread parent))))
11125           (when thread
11126             (delq (assq header thread) thread))))
11127       ;; We have to really fetch the header to this article.
11128       (save-excursion
11129         (set-buffer nntp-server-buffer)
11130         (when (setq where (gnus-request-head id group))
11131           (nnheader-fold-continuation-lines)
11132           (goto-char (point-max))
11133           (insert ".\n")
11134           (goto-char (point-min))
11135           (insert "211 ")
11136           (princ (cond
11137                   ((numberp id) id)
11138                   ((cdr where) (cdr where))
11139                   (header (mail-header-number header))
11140                   (t gnus-reffed-article-number))
11141                  (current-buffer))
11142           (insert " Article retrieved.\n"))
11143         (if (or (not where)
11144                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11145             ()                          ; Malformed head.
11146           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11147             (when (and (stringp id)
11148                        (not (string= (gnus-group-real-name group)
11149                                      (car where))))
11150               ;; If we fetched by Message-ID and the article came
11151               ;; from a different group, we fudge some bogus article
11152               ;; numbers for this article.
11153               (mail-header-set-number header gnus-reffed-article-number))
11154             (save-excursion
11155               (set-buffer gnus-summary-buffer)
11156               (decf gnus-reffed-article-number)
11157               (gnus-remove-header (mail-header-number header))
11158               (push header gnus-newsgroup-headers)
11159               (setq gnus-current-headers header)
11160               (push (mail-header-number header) gnus-newsgroup-limit)))
11161           header)))))
11162
11163 (defun gnus-remove-header (number)
11164   "Remove header NUMBER from `gnus-newsgroup-headers'."
11165   (if (and gnus-newsgroup-headers
11166            (= number (mail-header-number (car gnus-newsgroup-headers))))
11167       (pop gnus-newsgroup-headers)
11168     (let ((headers gnus-newsgroup-headers))
11169       (while (and (cdr headers)
11170                   (not (= number (mail-header-number (cadr headers)))))
11171         (pop headers))
11172       (when (cdr headers)
11173         (setcdr headers (cddr headers))))))
11174
11175 ;;;
11176 ;;; summary highlights
11177 ;;;
11178
11179 (defun gnus-highlight-selected-summary ()
11180   "Highlight selected article in summary buffer."
11181   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11182   (when gnus-summary-selected-face
11183     (save-excursion
11184       (let* ((beg (gnus-point-at-bol))
11185              (end (gnus-point-at-eol))
11186              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11187              (from (if (get-text-property beg gnus-mouse-face-prop)
11188                        beg
11189                      (or (next-single-property-change
11190                           beg gnus-mouse-face-prop nil end)
11191                          beg)))
11192              (to
11193               (if (= from end)
11194                   (- from 2)
11195                 (or (next-single-property-change
11196                      from gnus-mouse-face-prop nil end)
11197                     end))))
11198         ;; If no mouse-face prop on line we will have to = from = end,
11199         ;; so we highlight the entire line instead.
11200         (when (= (+ to 2) from)
11201           (setq from beg)
11202           (setq to end))
11203         (if gnus-newsgroup-selected-overlay
11204             ;; Move old overlay.
11205             (gnus-move-overlay
11206              gnus-newsgroup-selected-overlay from to (current-buffer))
11207           ;; Create new overlay.
11208           (gnus-overlay-put
11209            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11210            'face gnus-summary-selected-face))))))
11211
11212 (defvar gnus-summary-highlight-line-cached nil)
11213 (defvar gnus-summary-highlight-line-trigger nil)
11214
11215 (defun gnus-summary-highlight-line-0 ()
11216   (if (and (eq gnus-summary-highlight-line-trigger
11217                gnus-summary-highlight)
11218            gnus-summary-highlight-line-cached)
11219       gnus-summary-highlight-line-cached
11220     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11221           gnus-summary-highlight-line-cached
11222           (let* ((cond (list 'cond))
11223                  (c cond)
11224                  (list gnus-summary-highlight))
11225             (while list
11226               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11227                               nil))
11228               (setq c (cdr c)
11229                     list (cdr list)))
11230             (gnus-byte-compile (list 'lambda nil cond))))))
11231
11232 (defun gnus-summary-highlight-line ()
11233   "Highlight current line according to `gnus-summary-highlight'."
11234   (let* ((beg (gnus-point-at-bol))
11235          (article (or (gnus-summary-article-number) gnus-current-article))
11236          (score (or (cdr (assq article
11237                                gnus-newsgroup-scored))
11238                     gnus-summary-default-score 0))
11239          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11240          (inhibit-read-only t)
11241          (default gnus-summary-default-score)
11242          (default-high gnus-summary-default-high-score)
11243          (default-low gnus-summary-default-low-score)
11244          (uncached (and gnus-summary-use-undownloaded-faces
11245                         (memq article gnus-newsgroup-undownloaded))))
11246     (let ((face (funcall (gnus-summary-highlight-line-0))))
11247       (unless (eq face (get-text-property beg 'face))
11248         (gnus-put-text-property-excluding-characters-with-faces
11249          beg (gnus-point-at-eol) 'face
11250          (setq face (if (boundp face) (symbol-value face) face)))
11251         (when gnus-summary-highlight-line-function
11252           (funcall gnus-summary-highlight-line-function article face))))))
11253
11254 (defun gnus-update-read-articles (group unread &optional compute)
11255   "Update the list of read articles in GROUP.
11256 UNREAD is a sorted list."
11257   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11258          (entry (gnus-gethash group gnus-newsrc-hashtb))
11259          (info (nth 2 entry))
11260          (prev 1)
11261          read)
11262     (if (or (not info) (not active))
11263         ;; There is no info on this group if it was, in fact,
11264         ;; killed.  Gnus stores no information on killed groups, so
11265         ;; there's nothing to be done.
11266         ;; One could store the information somewhere temporarily,
11267         ;; perhaps...  Hmmm...
11268         ()
11269       ;; Remove any negative articles numbers.
11270       (while (and unread (< (car unread) 0))
11271         (setq unread (cdr unread)))
11272       ;; Remove any expired article numbers
11273       (while (and unread (< (car unread) (car active)))
11274         (setq unread (cdr unread)))
11275       ;; Compute the ranges of read articles by looking at the list of
11276       ;; unread articles.
11277       (while unread
11278         (when (/= (car unread) prev)
11279           (push (if (= prev (1- (car unread))) prev
11280                   (cons prev (1- (car unread))))
11281                 read))
11282         (setq prev (1+ (car unread)))
11283         (setq unread (cdr unread)))
11284       (when (<= prev (cdr active))
11285         (push (cons prev (cdr active)) read))
11286       (setq read (if (> (length read) 1) (nreverse read) read))
11287       (if compute
11288           read
11289         (save-excursion
11290           (let (setmarkundo)
11291             ;; Propagate the read marks to the backend.
11292             (when (gnus-check-backend-function 'request-set-mark group)
11293               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11294                     (add (gnus-remove-from-range read (gnus-info-read info))))
11295                 (when (or add del)
11296                   (unless (gnus-check-group group)
11297                     (error "Can't open server for %s" group))
11298                   (gnus-request-set-mark
11299                    group (delq nil (list (if add (list add 'add '(read)))
11300                                          (if del (list del 'del '(read))))))
11301                   (setq setmarkundo
11302                         `(gnus-request-set-mark
11303                           ,group
11304                           ',(delq nil (list
11305                                        (if del (list del 'add '(read)))
11306                                        (if add (list add 'del '(read))))))))))
11307             (set-buffer gnus-group-buffer)
11308             (gnus-undo-register
11309               `(progn
11310                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11311                  (gnus-info-set-read ',info ',(gnus-info-read info))
11312                  (gnus-get-unread-articles-in-group ',info
11313                                                     (gnus-active ,group))
11314                  (gnus-group-update-group ,group t)
11315                  ,setmarkundo))))
11316         ;; Enter this list into the group info.
11317         (gnus-info-set-read info read)
11318         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11319         (gnus-get-unread-articles-in-group info (gnus-active group))
11320         t))))
11321
11322 (defun gnus-offer-save-summaries ()
11323   "Offer to save all active summary buffers."
11324   (let (buffers)
11325     ;; Go through all buffers and find all summaries.
11326     (dolist (buffer (buffer-list))
11327       (when (and (setq buffer (buffer-name buffer))
11328                  (string-match "Summary" buffer)
11329                  (save-excursion
11330                    (set-buffer buffer)
11331                    ;; We check that this is, indeed, a summary buffer.
11332                    (and (eq major-mode 'gnus-summary-mode)
11333                         ;; Also make sure this isn't bogus.
11334                         gnus-newsgroup-prepared
11335                         ;; Also make sure that this isn't a
11336                         ;; dead summary buffer.
11337                         (not gnus-dead-summary-mode))))
11338         (push buffer buffers)))
11339     ;; Go through all these summary buffers and offer to save them.
11340     (when buffers
11341       (save-excursion
11342         (map-y-or-n-p
11343          "Update summary buffer %s? "
11344          (lambda (buf)
11345            (switch-to-buffer buf)
11346            (gnus-summary-exit))
11347          buffers)))))
11348
11349 (defun gnus-summary-setup-default-charset ()
11350   "Setup newsgroup default charset."
11351   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11352       (setq gnus-newsgroup-charset nil)
11353     (let* ((ignored-charsets
11354             (or gnus-newsgroup-ephemeral-ignored-charsets
11355                 (append
11356                  (and gnus-newsgroup-name
11357                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11358                  gnus-newsgroup-ignored-charsets))))
11359       (setq gnus-newsgroup-charset
11360             (or gnus-newsgroup-ephemeral-charset
11361                 (and gnus-newsgroup-name
11362                      (gnus-parameter-charset gnus-newsgroup-name))
11363                 gnus-default-charset))
11364       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11365            ignored-charsets))))
11366
11367 ;;;
11368 ;;; Mime Commands
11369 ;;;
11370
11371 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11372   "Display the current article buffer fully MIME-buttonized.
11373 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11374 treated as multipart/mixed."
11375   (interactive "P")
11376   (require 'gnus-art)
11377   (let ((gnus-unbuttonized-mime-types nil)
11378         (gnus-mime-display-multipart-as-mixed show-all-parts))
11379     (gnus-summary-show-article)))
11380
11381 (defun gnus-summary-repair-multipart (article)
11382   "Add a Content-Type header to a multipart article without one."
11383   (interactive (list (gnus-summary-article-number)))
11384   (gnus-with-article article
11385     (message-narrow-to-head)
11386     (message-remove-header "Mime-Version")
11387     (goto-char (point-max))
11388     (insert "Mime-Version: 1.0\n")
11389     (widen)
11390     (when (search-forward "\n--" nil t)
11391       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11392         (message-narrow-to-head)
11393         (message-remove-header "Content-Type")
11394         (goto-char (point-max))
11395         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11396                         separator))
11397         (widen))))
11398   (let (gnus-mark-article-hook)
11399     (gnus-summary-select-article t t nil article)))
11400
11401 (defun gnus-summary-toggle-display-buttonized ()
11402   "Toggle the buttonizing of the article buffer."
11403   (interactive)
11404   (require 'gnus-art)
11405   (if (setq gnus-inhibit-mime-unbuttonizing
11406             (not gnus-inhibit-mime-unbuttonizing))
11407       (let ((gnus-unbuttonized-mime-types nil))
11408         (gnus-summary-show-article))
11409     (gnus-summary-show-article)))
11410
11411 ;;;
11412 ;;; Generic summary marking commands
11413 ;;;
11414
11415 (defvar gnus-summary-marking-alist
11416   '((read gnus-del-mark "d")
11417     (unread gnus-unread-mark "u")
11418     (ticked gnus-ticked-mark "!")
11419     (dormant gnus-dormant-mark "?")
11420     (expirable gnus-expirable-mark "e"))
11421   "An alist of names/marks/keystrokes.")
11422
11423 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11424 (defvar gnus-summary-mark-map)
11425
11426 (defun gnus-summary-make-all-marking-commands ()
11427   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11428   (dolist (elem gnus-summary-marking-alist)
11429     (apply 'gnus-summary-make-marking-command elem)))
11430
11431 (defun gnus-summary-make-marking-command (name mark keystroke)
11432   (let ((map (make-sparse-keymap)))
11433     (define-key gnus-summary-generic-mark-map keystroke map)
11434     (dolist (lway `((next "next" next nil "n")
11435                     (next-unread "next unread" next t "N")
11436                     (prev "previous" prev nil "p")
11437                     (prev-unread "previous unread" prev t "P")
11438                     (nomove "" nil nil ,keystroke)))
11439       (let ((func (gnus-summary-make-marking-command-1
11440                    mark (car lway) lway name)))
11441         (setq func (eval func))
11442         (define-key map (nth 4 lway) func)))))
11443
11444 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11445   `(defun ,(intern
11446             (format "gnus-summary-put-mark-as-%s%s"
11447                     name (if (eq way 'nomove)
11448                              ""
11449                            (concat "-" (symbol-name way)))))
11450      (n)
11451      ,(format
11452        "Mark the current article as %s%s.
11453 If N, the prefix, then repeat N times.
11454 If N is negative, move in reverse order.
11455 The difference between N and the actual number of articles marked is
11456 returned."
11457        name (cadr lway))
11458      (interactive "p")
11459      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11460
11461 (defun gnus-summary-generic-mark (n mark move unread)
11462   "Mark N articles with MARK."
11463   (unless (eq major-mode 'gnus-summary-mode)
11464     (error "This command can only be used in the summary buffer"))
11465   (gnus-summary-show-thread)
11466   (let ((nummove
11467          (cond
11468           ((eq move 'next) 1)
11469           ((eq move 'prev) -1)
11470           (t 0))))
11471     (if (zerop nummove)
11472         (setq n 1)
11473       (when (< n 0)
11474         (setq n (abs n)
11475               nummove (* -1 nummove))))
11476     (while (and (> n 0)
11477                 (gnus-summary-mark-article nil mark)
11478                 (zerop (gnus-summary-next-subject nummove unread t)))
11479       (setq n (1- n)))
11480     (when (/= 0 n)
11481       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11482     (gnus-summary-recenter)
11483     (gnus-summary-position-point)
11484     (gnus-set-mode-line 'summary)
11485     n))
11486
11487 (defun gnus-summary-insert-articles (articles)
11488   (when (setq articles
11489               (gnus-sorted-difference articles
11490                                       (mapcar (lambda (h)
11491                                                 (mail-header-number h))
11492                                               gnus-newsgroup-headers)))
11493     (setq gnus-newsgroup-headers
11494           (gnus-merge 'list
11495                       gnus-newsgroup-headers
11496                       (gnus-fetch-headers articles)
11497                       'gnus-article-sort-by-number))
11498     ;; Suppress duplicates?
11499     (when gnus-suppress-duplicates
11500       (gnus-dup-suppress-articles))
11501
11502     ;; We might want to build some more threads first.
11503     (when (and gnus-fetch-old-headers
11504                (eq gnus-headers-retrieved-by 'nov))
11505       (if (eq gnus-fetch-old-headers 'invisible)
11506           (gnus-build-all-threads)
11507         (gnus-build-old-threads)))
11508     ;; Let the Gnus agent mark articles as read.
11509     (when gnus-agent
11510       (gnus-agent-get-undownloaded-list))
11511     ;; Remove list identifiers from subject
11512     (when gnus-list-identifiers
11513       (gnus-summary-remove-list-identifiers))
11514     ;; First and last article in this newsgroup.
11515     (when gnus-newsgroup-headers
11516       (setq gnus-newsgroup-begin
11517             (mail-header-number (car gnus-newsgroup-headers))
11518             gnus-newsgroup-end
11519             (mail-header-number
11520              (gnus-last-element gnus-newsgroup-headers))))
11521     (when gnus-use-scoring
11522       (gnus-possibly-score-headers))))
11523
11524 (defun gnus-summary-insert-old-articles (&optional all)
11525   "Insert all old articles in this group.
11526 If ALL is non-nil, already read articles become readable.
11527 If ALL is a number, fetch this number of articles."
11528   (interactive "P")
11529   (prog1
11530       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11531             older len)
11532         (setq older
11533               ;; Some nntp servers lie about their active range.  When
11534               ;; this happens, the active range can be in the millions.
11535               ;; Use a compressed range to avoid creating a huge list.
11536               (gnus-range-difference (list gnus-newsgroup-active) old))
11537         (setq len (gnus-range-length older))
11538         (cond
11539          ((null older) nil)
11540          ((numberp all)
11541           (if (< all len)
11542               (let ((older-range (nreverse older)))
11543                 (setq older nil)
11544
11545                 (while (> all 0)
11546                   (let* ((r (pop older-range))
11547                          (min (if (numberp r) r (car r)))
11548                          (max (if (numberp r) r (cdr r))))
11549                     (while (and (<= min max)
11550                                 (> all 0))
11551                       (push max older)
11552                       (setq all (1- all)
11553                             max (1- max))))))
11554             (setq older (gnus-uncompress-range older))))
11555          (all
11556           (setq older (gnus-uncompress-range older)))
11557          (t
11558           (when (and (numberp gnus-large-newsgroup)
11559                    (> len gnus-large-newsgroup))
11560               (let* ((cursor-in-echo-area nil)
11561                      (initial (gnus-parameter-large-newsgroup-initial
11562                                gnus-newsgroup-name))
11563                      (input
11564                       (read-string
11565                        (format
11566                         "How many articles from %s (%s %d): "
11567                         (gnus-limit-string
11568                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11569                         (if initial "max" "default")
11570                         len)
11571                        (if initial
11572                            (cons (number-to-string initial)
11573                                  0)))))
11574                 (unless (string-match "^[ \t]*$" input)
11575                   (setq all (string-to-number input))
11576                   (if (< all len)
11577                       (let ((older-range (nreverse older)))
11578                         (setq older nil)
11579
11580                         (while (> all 0)
11581                           (let* ((r (pop older-range))
11582                                  (min (if (numberp r) r (car r)))
11583                                  (max (if (numberp r) r (cdr r))))
11584                             (while (and (<= min max)
11585                                         (> all 0))
11586                               (push max older)
11587                               (setq all (1- all)
11588                                     max (1- max))))))))))
11589           (setq older (gnus-uncompress-range older))))
11590         (if (not older)
11591             (message "No old news.")
11592           (gnus-summary-insert-articles older)
11593           (gnus-summary-limit (gnus-sorted-nunion old older))))
11594     (gnus-summary-position-point)))
11595
11596 (defun gnus-summary-insert-new-articles ()
11597   "Insert all new articles in this group."
11598   (interactive)
11599   (prog1
11600       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11601             (old-active gnus-newsgroup-active)
11602             (nnmail-fetched-sources (list t))
11603             i new)
11604         (setq gnus-newsgroup-active
11605               (gnus-activate-group gnus-newsgroup-name 'scan))
11606         (setq i (cdr gnus-newsgroup-active))
11607         (while (> i (cdr old-active))
11608           (push i new)
11609           (decf i))
11610         (if (not new)
11611             (message "No gnus is bad news.")
11612           (gnus-summary-insert-articles new)
11613           (setq gnus-newsgroup-unreads
11614                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11615           (gnus-summary-limit (gnus-sorted-nunion old new))))
11616     (gnus-summary-position-point)))
11617
11618 (gnus-summary-make-all-marking-commands)
11619
11620 (gnus-ems-redefine)
11621
11622 (provide 'gnus-sum)
11623
11624 (run-hooks 'gnus-sum-load-hook)
11625
11626 ;; Local Variables:
11627 ;; coding: iso-8859-1
11628 ;; End:
11629
11630 ;;; gnus-sum.el ends here