(gnus-summary-next-page): Mention
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload 'mm-uu-dissect "mm-uu")
48 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
49   "Deuglify broken Outlook (Express) articles and redisplay."
50   t)
51 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
52 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
53 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
54
55 (defcustom gnus-kill-summary-on-exit t
56   "*If non-nil, kill the summary buffer when you exit from it.
57 If nil, the summary will become a \"*Dead Summary*\" buffer, and
58 it will be killed sometime later."
59   :group 'gnus-summary-exit
60   :type 'boolean)
61
62 (defcustom gnus-fetch-old-headers nil
63   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
64 If an unread article in the group refers to an older, already read (or
65 just marked as read) article, the old article will not normally be
66 displayed in the Summary buffer.  If this variable is non-nil, Gnus
67 will attempt to grab the headers to the old articles, and thereby
68 build complete threads.  If it has the value `some', only enough
69 headers to connect otherwise loose threads will be displayed.  This
70 variable can also be a number.  In that case, no more than that number
71 of old headers will be fetched.  If it has the value `invisible', all
72 old headers will be fetched, but none will be displayed.
73
74 The server has to support NOV for any of this to work."
75   :group 'gnus-thread
76   :type '(choice (const :tag "off" nil)
77                  (const some)
78                  number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-refer-thread-limit 200
82   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
83 If t, fetch all the available old headers."
84   :group 'gnus-thread
85   :type '(choice number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-summary-make-false-root 'adopt
89   "*nil means that Gnus won't gather loose threads.
90 If the root of a thread has expired or been read in a previous
91 session, the information necessary to build a complete thread has been
92 lost.  Instead of having many small sub-threads from this original thread
93 scattered all over the summary buffer, Gnus can gather them.
94
95 If non-nil, Gnus will try to gather all loose sub-threads from an
96 original thread into one large thread.
97
98 If this variable is non-nil, it should be one of `none', `adopt',
99 `dummy' or `empty'.
100
101 If this variable is `none', Gnus will not make a false root, but just
102 present the sub-threads after another.
103 If this variable is `dummy', Gnus will create a dummy root that will
104 have all the sub-threads as children.
105 If this variable is `adopt', Gnus will make one of the \"children\"
106 the parent and mark all the step-children as such.
107 If this variable is `empty', the \"children\" are printed with empty
108 subject fields.  (Or rather, they will be printed with a string
109 given by the `gnus-summary-same-subject' variable.)"
110   :group 'gnus-thread
111   :type '(choice (const :tag "off" nil)
112                  (const none)
113                  (const dummy)
114                  (const adopt)
115                  (const empty)))
116
117 (defcustom gnus-summary-make-false-root-always nil
118   "Always make a false dummy root."
119   :group 'gnus-thread
120   :type 'boolean)
121
122 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
123   "*A regexp to match subjects to be excluded from loose thread gathering.
124 As loose thread gathering is done on subjects only, that means that
125 there can be many false gatherings performed.  By rooting out certain
126 common subjects, gathering might become saner."
127   :group 'gnus-thread
128   :type 'regexp)
129
130 (defcustom gnus-summary-gather-subject-limit nil
131   "*Maximum length of subject comparisons when gathering loose threads.
132 Use nil to compare full subjects.  Setting this variable to a low
133 number will help gather threads that have been corrupted by
134 newsreaders chopping off subject lines, but it might also mean that
135 unrelated articles that have subject that happen to begin with the
136 same few characters will be incorrectly gathered.
137
138 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
139 comparing subjects."
140   :group 'gnus-thread
141   :type '(choice (const :tag "off" nil)
142                  (const fuzzy)
143                  (sexp :menu-tag "on" t)))
144
145 (defcustom gnus-simplify-subject-functions nil
146   "List of functions taking a string argument that simplify subjects.
147 The functions are applied recursively.
148
149 Useful functions to put in this list include:
150 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
151 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 \(they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-default-high-score 0
216   "*Default threshold for a high scored article.
217 An article will be highlighted as high scored if its score is greater
218 than this score."
219   :group 'gnus-score-default
220   :type 'integer)
221
222 (defcustom gnus-summary-default-low-score 0
223   "*Default threshold for a low scored article.
224 An article will be highlighted as low scored if its score is smaller
225 than this score."
226   :group 'gnus-score-default
227   :type 'integer)
228
229 (defcustom gnus-summary-zcore-fuzz 0
230   "*Fuzziness factor for the zcore in the summary buffer.
231 Articles with scores closer than this to `gnus-summary-default-score'
232 will not be marked."
233   :group 'gnus-summary-format
234   :type 'integer)
235
236 (defcustom gnus-simplify-subject-fuzzy-regexp nil
237   "*Strings to be removed when doing fuzzy matches.
238 This can either be a regular expression or list of regular expressions
239 that will be removed from subject strings if fuzzy subject
240 simplification is selected."
241   :group 'gnus-thread
242   :type '(repeat regexp))
243
244 (defcustom gnus-show-threads t
245   "*If non-nil, display threads in summary mode."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-hide-subtree nil
250   "*If non-nil, hide all threads initially.
251 This can be a predicate specifier which says which threads to hide.
252 If threads are hidden, you have to run the command
253 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
254 to expose hidden threads."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-hide-killed t
259   "*If non-nil, hide killed threads automatically."
260   :group 'gnus-thread
261   :type 'boolean)
262
263 (defcustom gnus-thread-ignore-subject t
264   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
265 If nil, articles that have different subjects from their parents will
266 start separate threads."
267   :group 'gnus-thread
268   :type 'boolean)
269
270 (defcustom gnus-thread-operation-ignore-subject t
271   "*If non-nil, subjects will be ignored when doing thread commands.
272 This affects commands like `gnus-summary-kill-thread' and
273 `gnus-summary-lower-thread'.
274
275 If this variable is nil, articles in the same thread with different
276 subjects will not be included in the operation in question.  If this
277 variable is `fuzzy', only articles that have subjects that are fuzzily
278 equal will be included."
279   :group 'gnus-thread
280   :type '(choice (const :tag "off" nil)
281                  (const fuzzy)
282                  (sexp :tag "on" t)))
283
284 (defcustom gnus-thread-indent-level 4
285   "*Number that says how much each sub-thread should be indented."
286   :group 'gnus-thread
287   :type 'integer)
288
289 (defcustom gnus-auto-extend-newsgroup t
290   "*If non-nil, extend newsgroup forward and backward when requested."
291   :group 'gnus-summary-choose
292   :type 'boolean)
293
294 (defcustom gnus-auto-select-first t
295   "*If non-nil, select the article under point.
296 Which article this is is controlled by the `gnus-auto-select-subject'
297 variable.
298
299 If you want to prevent automatic selection of articles in some
300 newsgroups, set the variable to nil in `gnus-select-group-hook'."
301   :group 'gnus-group-select
302   :type '(choice (const :tag "none" nil)
303                  (sexp :menu-tag "first" t)))
304
305 (defcustom gnus-auto-select-subject 'unread
306   "*Says what subject to place under point when entering a group.
307
308 This variable can either be the symbols `first' (place point on the
309 first subject), `unread' (place point on the subject line of the first
310 unread article), `best' (place point on the subject line of the
311 higest-scored article), `unseen' (place point on the subject line of
312 the first unseen article), 'unseen-or-unread' (place point on the subject
313 line of the first unseen article or, if all article have been seen, on the
314 subject line of the first unread article), or a function to be called to
315 place point on some subject line."
316   :group 'gnus-group-select
317   :type '(choice (const best)
318                  (const unread)
319                  (const first)
320                  (const unseen)
321                  (const unseen-or-unread)))
322
323 (defcustom gnus-auto-select-next t
324   "*If non-nil, offer to go to the next group from the end of the previous.
325 If the value is t and the next newsgroup is empty, Gnus will exit
326 summary mode and go back to group mode.  If the value is neither nil
327 nor t, Gnus will select the following unread newsgroup.  In
328 particular, if the value is the symbol `quietly', the next unread
329 newsgroup will be selected without any confirmation, and if it is
330 `almost-quietly', the next group will be selected without any
331 confirmation if you are located on the last article in the group.
332 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
333 will go to the next group without confirmation."
334   :group 'gnus-summary-maneuvering
335   :type '(choice (const :tag "off" nil)
336                  (const quietly)
337                  (const almost-quietly)
338                  (const slightly-quietly)
339                  (sexp :menu-tag "on" t)))
340
341 (defcustom gnus-auto-select-same nil
342   "*If non-nil, select the next article with the same subject.
343 If there are no more articles with the same subject, go to
344 the first unread article."
345   :group 'gnus-summary-maneuvering
346   :type 'boolean)
347
348 (defcustom gnus-auto-goto-ignores 'unfetched
349   "*Says how to handle unfetched articles when maneuvering.
350
351 This variable can either be the symbols nil (maneuver to any
352 article), `undownloaded' (maneuvering while unplugged ignores articles
353 that have not been fetched), `always-undownloaded' (maneuvering always
354 ignores articles that have not been fetched), `unfetched' (maneuvering
355 ignores articles whose headers have not been fetched).
356
357 NOTE: The list of unfetched articles will always be nil when plugged
358 and, when unplugged, a subset of the undownloaded article list."
359   :group 'gnus-summary-maneuvering
360   :type '(choice (const :tag "None" nil)
361                  (const :tag "Undownloaded when unplugged" undownloaded)
362                  (const :tag "Undownloaded" always-undownloaded)
363                  (const :tag "Unfetched" unfetched)))
364
365 (defcustom gnus-summary-check-current nil
366   "*If non-nil, consider the current article when moving.
367 The \"unread\" movement commands will stay on the same line if the
368 current article is unread."
369   :group 'gnus-summary-maneuvering
370   :type 'boolean)
371
372 (defcustom gnus-auto-center-summary t
373   "*If non-nil, always center the current summary buffer.
374 In particular, if `vertical' do only vertical recentering.  If non-nil
375 and non-`vertical', do both horizontal and vertical recentering."
376   :group 'gnus-summary-maneuvering
377   :type '(choice (const :tag "none" nil)
378                  (const vertical)
379                  (integer :tag "height")
380                  (sexp :menu-tag "both" t)))
381
382 (defvar gnus-auto-center-group t
383   "*If non-nil, always center the group buffer.")
384
385 (defcustom gnus-show-all-headers nil
386   "*If non-nil, don't hide any headers."
387   :group 'gnus-article-hiding
388   :group 'gnus-article-headers
389   :type 'boolean)
390
391 (defcustom gnus-summary-ignore-duplicates nil
392   "*If non-nil, ignore articles with identical Message-ID headers."
393   :group 'gnus-summary
394   :type 'boolean)
395
396 (defcustom gnus-single-article-buffer t
397   "*If non-nil, display all articles in the same buffer.
398 If nil, each group will get its own article buffer."
399   :group 'gnus-article-various
400   :type 'boolean)
401
402 (defcustom gnus-break-pages t
403   "*If non-nil, do page breaking on articles.
404 The page delimiter is specified by the `gnus-page-delimiter'
405 variable."
406   :group 'gnus-article-various
407   :type 'boolean)
408
409 (defcustom gnus-move-split-methods nil
410   "*Variable used to suggest where articles are to be moved to.
411 It uses the same syntax as the `gnus-split-methods' variable.
412 However, whereas `gnus-split-methods' specifies file names as targets,
413 this variable specifies group names."
414   :group 'gnus-summary-mail
415   :type '(repeat (choice (list :value (fun) function)
416                          (cons :value ("" "") regexp (repeat string))
417                          (sexp :value nil))))
418
419 (defcustom gnus-unread-mark ?           ;Whitespace
420   "*Mark used for unread articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-ticked-mark ?!
425   "*Mark used for ticked articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-dormant-mark ??
430   "*Mark used for dormant articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-del-mark ?r
435   "*Mark used for del'd articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-read-mark ?R
440   "*Mark used for read articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-expirable-mark ?E
445   "*Mark used for expirable articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-killed-mark ?K
450   "*Mark used for killed articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-spam-mark ?$
455   "*Mark used for spam articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-souped-mark ?F
460   "*Mark used for souped articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-kill-file-mark ?X
465   "*Mark used for articles killed by kill files."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-low-score-mark ?Y
470   "*Mark used for articles with a low score."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-catchup-mark ?C
475   "*Mark used for articles that are caught up."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-replied-mark ?A
480   "*Mark used for articles that have been replied to."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-forwarded-mark ?F
485   "*Mark used for articles that have been forwarded."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-recent-mark ?N
490   "*Mark used for articles that are recent."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-cached-mark ?*
495   "*Mark used for articles that are in the cache."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-saved-mark ?S
500   "*Mark used for articles that have been saved."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-unseen-mark ?.
505   "*Mark used for articles that haven't been seen."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-no-mark ?               ;Whitespace
510   "*Mark used for articles that have no other secondary mark."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-ancient-mark ?O
515   "*Mark used for ancient articles."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-sparse-mark ?Q
520   "*Mark used for sparsely reffed articles."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-canceled-mark ?G
525   "*Mark used for canceled articles."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-duplicate-mark ?M
530   "*Mark used for duplicate articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-undownloaded-mark ?-
535   "*Mark used for articles that weren't downloaded."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-downloaded-mark ?+
540   "*Mark used for articles that were downloaded."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-downloadable-mark ?%
545   "*Mark used for articles that are to be downloaded."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-unsendable-mark ?=
550   "*Mark used for articles that won't be sent."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-score-over-mark ?+
555   "*Score mark used for articles with high scores."
556   :group 'gnus-summary-marks
557   :type 'character)
558
559 (defcustom gnus-score-below-mark ?-
560   "*Score mark used for articles with low scores."
561   :group 'gnus-summary-marks
562   :type 'character)
563
564 (defcustom gnus-empty-thread-mark ?     ;Whitespace
565   "*There is no thread under the article."
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-not-empty-thread-mark ?=
570   "*There is a thread under the article."
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-view-pseudo-asynchronously nil
575   "*If non-nil, Gnus will view pseudo-articles asynchronously."
576   :group 'gnus-extract-view
577   :type 'boolean)
578
579 (defcustom gnus-auto-expirable-marks
580   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
581         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
582         gnus-souped-mark gnus-duplicate-mark)
583   "*The list of marks converted into expiration if a group is auto-expirable."
584   :version "21.1"
585   :group 'gnus-summary
586   :type '(repeat character))
587
588 (defcustom gnus-inhibit-user-auto-expire t
589   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
590   :version "21.1"
591   :group 'gnus-summary
592   :type 'boolean)
593
594 (defcustom gnus-view-pseudos nil
595   "*If `automatic', pseudo-articles will be viewed automatically.
596 If `not-confirm', pseudos will be viewed automatically, and the user
597 will not be asked to confirm the command."
598   :group 'gnus-extract-view
599   :type '(choice (const :tag "off" nil)
600                  (const automatic)
601                  (const not-confirm)))
602
603 (defcustom gnus-view-pseudos-separately t
604   "*If non-nil, one pseudo-article will be created for each file to be viewed.
605 If nil, all files that use the same viewing command will be given as a
606 list of parameters to that command."
607   :group 'gnus-extract-view
608   :type 'boolean)
609
610 (defcustom gnus-insert-pseudo-articles t
611   "*If non-nil, insert pseudo-articles when decoding articles."
612   :group 'gnus-extract-view
613   :type 'boolean)
614
615 (defcustom gnus-summary-dummy-line-format
616   "   %(:                             :%) %S\n"
617   "*The format specification for the dummy roots in the summary buffer.
618 It works along the same lines as a normal formatting string,
619 with some simple extensions.
620
621 %S  The subject
622
623 General format specifiers can also be used.
624 See `(gnus)Formatting Variables'."
625   :link '(custom-manual "(gnus)Formatting Variables")
626   :group 'gnus-threading
627   :type 'string)
628
629 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
630   "*The format specification for the summary mode line.
631 It works along the same lines as a normal formatting string,
632 with some simple extensions:
633
634 %G  Group name
635 %p  Unprefixed group name
636 %A  Current article number
637 %z  Current article score
638 %V  Gnus version
639 %U  Number of unread articles in the group
640 %e  Number of unselected articles in the group
641 %Z  A string with unread/unselected article counts
642 %g  Shortish group name
643 %S  Subject of the current article
644 %u  User-defined spec
645 %s  Current score file name
646 %d  Number of dormant articles
647 %r  Number of articles that have been marked as read in this session
648 %E  Number of articles expunged by the score files"
649   :group 'gnus-summary-format
650   :type 'string)
651
652 (defcustom gnus-list-identifiers nil
653   "Regexp that matches list identifiers to be removed from subject.
654 This can also be a list of regexps."
655   :version "21.1"
656   :group 'gnus-summary-format
657   :group 'gnus-article-hiding
658   :type '(choice (const :tag "none" nil)
659                  (regexp :value ".*")
660                  (repeat :value (".*") regexp)))
661
662 (defcustom gnus-summary-mark-below 0
663   "*Mark all articles with a score below this variable as read.
664 This variable is local to each summary buffer and usually set by the
665 score file."
666   :group 'gnus-score-default
667   :type 'integer)
668
669 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
670   "*List of functions used for sorting articles in the summary buffer.
671
672 Each function takes two articles and returns non-nil if the first
673 article should be sorted before the other.  If you use more than one
674 function, the primary sort function should be the last.  You should
675 probably always include `gnus-article-sort-by-number' in the list of
676 sorting functions -- preferably first.  Also note that sorting by date
677 is often much slower than sorting by number, and the sorting order is
678 very similar.  (Sorting by date means sorting by the time the message
679 was sent, sorting by number means sorting by arrival time.)
680
681 Ready-made functions include `gnus-article-sort-by-number',
682 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
683 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
684 and `gnus-article-sort-by-score'.
685
686 When threading is turned on, the variable `gnus-thread-sort-functions'
687 controls how articles are sorted."
688   :group 'gnus-summary-sort
689   :type '(repeat (choice (function-item gnus-article-sort-by-number)
690                          (function-item gnus-article-sort-by-author)
691                          (function-item gnus-article-sort-by-subject)
692                          (function-item gnus-article-sort-by-date)
693                          (function-item gnus-article-sort-by-score)
694                          (function-item gnus-article-sort-by-random)
695                          (function :tag "other"))))
696
697 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
698   "*List of functions used for sorting threads in the summary buffer.
699 By default, threads are sorted by article number.
700
701 Each function takes two threads and returns non-nil if the first
702 thread should be sorted before the other.  If you use more than one
703 function, the primary sort function should be the last.  You should
704 probably always include `gnus-thread-sort-by-number' in the list of
705 sorting functions -- preferably first.  Also note that sorting by date
706 is often much slower than sorting by number, and the sorting order is
707 very similar.  (Sorting by date means sorting by the time the message
708 was sent, sorting by number means sorting by arrival time.)
709
710 Ready-made functions include `gnus-thread-sort-by-number',
711 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
712 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
713 `gnus-thread-sort-by-most-recent-number',
714 `gnus-thread-sort-by-most-recent-date',
715 `gnus-thread-sort-by-random', and
716 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
717
718 When threading is turned off, the variable
719 `gnus-article-sort-functions' controls how articles are sorted."
720   :group 'gnus-summary-sort
721   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
722                          (function-item gnus-thread-sort-by-author)
723                          (function-item gnus-thread-sort-by-subject)
724                          (function-item gnus-thread-sort-by-date)
725                          (function-item gnus-thread-sort-by-score)
726                          (function-item gnus-thread-sort-by-total-score)
727                          (function-item gnus-thread-sort-by-random)
728                          (function :tag "other"))))
729
730 (defcustom gnus-thread-score-function '+
731   "*Function used for calculating the total score of a thread.
732
733 The function is called with the scores of the article and each
734 subthread and should then return the score of the thread.
735
736 Some functions you can use are `+', `max', or `min'."
737   :group 'gnus-summary-sort
738   :type 'function)
739
740 (defcustom gnus-summary-expunge-below nil
741   "All articles that have a score less than this variable will be expunged.
742 This variable is local to the summary buffers."
743   :group 'gnus-score-default
744   :type '(choice (const :tag "off" nil)
745                  integer))
746
747 (defcustom gnus-thread-expunge-below nil
748   "All threads that have a total score less than this variable will be expunged.
749 See `gnus-thread-score-function' for en explanation of what a
750 \"thread score\" is.
751
752 This variable is local to the summary buffers."
753   :group 'gnus-threading
754   :group 'gnus-score-default
755   :type '(choice (const :tag "off" nil)
756                  integer))
757
758 (defcustom gnus-summary-mode-hook nil
759   "*A hook for Gnus summary mode.
760 This hook is run before any variables are set in the summary buffer."
761   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
762   :group 'gnus-summary-various
763   :type 'hook)
764
765 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
766 (when (featurep 'xemacs)
767   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
768   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
769   (add-hook 'gnus-summary-mode-hook
770             'gnus-xmas-switch-horizontal-scrollbar-off))
771
772 (defcustom gnus-summary-menu-hook nil
773   "*Hook run after the creation of the summary mode menu."
774   :group 'gnus-summary-visual
775   :type 'hook)
776
777 (defcustom gnus-summary-exit-hook nil
778   "*A hook called on exit from the summary buffer.
779 It will be called with point in the group buffer."
780   :group 'gnus-summary-exit
781   :type 'hook)
782
783 (defcustom gnus-summary-prepare-hook nil
784   "*A hook called after the summary buffer has been generated.
785 If you want to modify the summary buffer, you can use this hook."
786   :group 'gnus-summary-various
787   :type 'hook)
788
789 (defcustom gnus-summary-prepared-hook nil
790   "*A hook called as the last thing after the summary buffer has been generated."
791   :group 'gnus-summary-various
792   :type 'hook)
793
794 (defcustom gnus-summary-generate-hook nil
795   "*A hook run just before generating the summary buffer.
796 This hook is commonly used to customize threading variables and the
797 like."
798   :group 'gnus-summary-various
799   :type 'hook)
800
801 (defcustom gnus-select-group-hook nil
802   "*A hook called when a newsgroup is selected.
803
804 If you'd like to simplify subjects like the
805 `gnus-summary-next-same-subject' command does, you can use the
806 following hook:
807
808  (add-hook gnus-select-group-hook
809            (lambda ()
810              (mapcar (lambda (header)
811                        (mail-header-set-subject
812                         header
813                         (gnus-simplify-subject
814                          (mail-header-subject header) 're-only)))
815                      gnus-newsgroup-headers)))"
816   :group 'gnus-group-select
817   :type 'hook)
818
819 (defcustom gnus-select-article-hook nil
820   "*A hook called when an article is selected."
821   :group 'gnus-summary-choose
822   :options '(gnus-agent-fetch-selected-article)
823   :type 'hook)
824
825 (defcustom gnus-visual-mark-article-hook
826   (list 'gnus-highlight-selected-summary)
827   "*Hook run after selecting an article in the summary buffer.
828 It is meant to be used for highlighting the article in some way.  It
829 is not run if `gnus-visual' is nil."
830   :group 'gnus-summary-visual
831   :type 'hook)
832
833 (defcustom gnus-parse-headers-hook nil
834   "*A hook called before parsing the headers."
835   :group 'gnus-various
836   :type 'hook)
837
838 (defcustom gnus-exit-group-hook nil
839   "*A hook called when exiting summary mode.
840 This hook is not called from the non-updating exit commands like `Q'."
841   :group 'gnus-various
842   :type 'hook)
843
844 (defcustom gnus-summary-update-hook
845   (list 'gnus-summary-highlight-line)
846   "*A hook called when a summary line is changed.
847 The hook will not be called if `gnus-visual' is nil.
848
849 The default function `gnus-summary-highlight-line' will
850 highlight the line according to the `gnus-summary-highlight'
851 variable."
852   :group 'gnus-summary-visual
853   :type 'hook)
854
855 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
856   "*A hook called when an article is selected for the first time.
857 The hook is intended to mark an article as read (or unread)
858 automatically when it is selected."
859   :group 'gnus-summary-choose
860   :type 'hook)
861
862 (defcustom gnus-group-no-more-groups-hook nil
863   "*A hook run when returning to group mode having no more (unread) groups."
864   :group 'gnus-group-select
865   :type 'hook)
866
867 (defcustom gnus-ps-print-hook nil
868   "*A hook run before ps-printing something from Gnus."
869   :group 'gnus-summary
870   :type 'hook)
871
872 (defcustom gnus-summary-article-move-hook nil
873   "*A hook called after an article is moved, copied, respooled, or crossposted."
874   :group 'gnus-summary
875   :type 'hook)
876
877 (defcustom gnus-summary-article-delete-hook nil
878   "*A hook called after an article is deleted."
879   :group 'gnus-summary
880   :type 'hook)
881
882 (defcustom gnus-summary-article-expire-hook nil
883   "*A hook called after an article is expired."
884   :group 'gnus-summary
885   :type 'hook)
886
887 (defcustom gnus-summary-display-arrow
888   (and (fboundp 'display-graphic-p)
889        (display-graphic-p))
890   "*If non-nil, display an arrow highlighting the current article."
891   :version "21.1"
892   :group 'gnus-summary
893   :type 'boolean)
894
895 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
896   "Face used for highlighting the current article in the summary buffer."
897   :group 'gnus-summary-visual
898   :type 'face)
899
900 (defvar gnus-tmp-downloaded nil)
901
902 (defcustom gnus-summary-highlight
903   '(((eq mark gnus-canceled-mark)
904      . gnus-summary-cancelled-face)
905     ((and uncached (> score default-high))
906      . gnus-summary-high-undownloaded-face)
907     ((and uncached (< score default-low))
908      . gnus-summary-low-undownloaded-face)
909     (uncached
910      . gnus-summary-normal-undownloaded-face)
911     ((and (> score default-high)
912           (or (eq mark gnus-dormant-mark)
913               (eq mark gnus-ticked-mark)))
914      . gnus-summary-high-ticked-face)
915     ((and (< score default-low)
916           (or (eq mark gnus-dormant-mark)
917               (eq mark gnus-ticked-mark)))
918      . gnus-summary-low-ticked-face)
919     ((or (eq mark gnus-dormant-mark)
920          (eq mark gnus-ticked-mark))
921      . gnus-summary-normal-ticked-face)
922     ((and (> score default-high) (eq mark gnus-ancient-mark))
923      . gnus-summary-high-ancient-face)
924     ((and (< score default-low) (eq mark gnus-ancient-mark))
925      . gnus-summary-low-ancient-face)
926     ((eq mark gnus-ancient-mark)
927      . gnus-summary-normal-ancient-face)
928     ((and (> score default-high) (eq mark gnus-unread-mark))
929      . gnus-summary-high-unread-face)
930     ((and (< score default-low) (eq mark gnus-unread-mark))
931      . gnus-summary-low-unread-face)
932     ((eq mark gnus-unread-mark)
933      . gnus-summary-normal-unread-face)
934     ((> score default-high)
935      . gnus-summary-high-read-face)
936     ((< score default-low)
937      . gnus-summary-low-read-face)
938     (t
939      . gnus-summary-normal-read-face))
940   "*Controls the highlighting of summary buffer lines.
941
942 A list of (FORM . FACE) pairs.  When deciding how a a particular
943 summary line should be displayed, each form is evaluated.  The content
944 of the face field after the first true form is used.  You can change
945 how those summary lines are displayed, by editing the face field.
946
947 You can use the following variables in the FORM field.
948
949 score:        The article's score
950 default:      The default article score.
951 default-high: The default score for high scored articles.
952 default-low:  The default score for low scored articles.
953 below:        The score below which articles are automatically marked as read.
954 mark:         The articles mark."
955   :group 'gnus-summary-visual
956   :type '(repeat (cons (sexp :tag "Form" nil)
957                        face)))
958
959 (defcustom gnus-alter-header-function nil
960   "Function called to allow alteration of article header structures.
961 The function is called with one parameter, the article header vector,
962 which it may alter in any way."
963   :type '(choice (const :tag "None" nil)
964                  function)
965   :group 'gnus-summary)
966
967 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
968   "Variable that says which function should be used to decode a string with encoded words.")
969
970 (defcustom gnus-extra-headers '(To Newsgroups)
971   "*Extra headers to parse."
972   :version "21.1"
973   :group 'gnus-summary
974   :type '(repeat symbol))
975
976 (defcustom gnus-ignored-from-addresses
977   (and user-mail-address (regexp-quote user-mail-address))
978   "*Regexp of From headers that may be suppressed in favor of To headers."
979   :version "21.1"
980   :group 'gnus-summary
981   :type 'regexp)
982
983 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
984   "List of charsets that should be ignored.
985 When these charsets are used in the \"charset\" parameter, the
986 default charset will be used instead."
987   :version "21.1"
988   :type '(repeat symbol)
989   :group 'gnus-charset)
990
991 (gnus-define-group-parameter
992  ignored-charsets
993  :type list
994  :function-document
995  "Return the ignored charsets of GROUP."
996  :variable gnus-group-ignored-charsets-alist
997  :variable-default
998  '(("alt\\.chinese\\.text" iso-8859-1))
999  :variable-document
1000  "Alist of regexps (to match group names) and charsets that should be ignored.
1001 When these charsets are used in the \"charset\" parameter, the
1002 default charset will be used instead."
1003  :variable-group gnus-charset
1004  :variable-type '(repeat (cons (regexp :tag "Group")
1005                                (repeat symbol)))
1006  :parameter-type '(choice :tag "Ignored charsets"
1007                           :value nil
1008                           (repeat (symbol)))
1009  :parameter-document       "\
1010 List of charsets that should be ignored.
1011
1012 When these charsets are used in the \"charset\" parameter, the
1013 default charset will be used instead.")
1014
1015 (defcustom gnus-group-highlight-words-alist nil
1016   "Alist of group regexps and highlight regexps.
1017 This variable uses the same syntax as `gnus-emphasis-alist'."
1018   :version "21.1"
1019   :type '(repeat (cons (regexp :tag "Group")
1020                        (repeat (list (regexp :tag "Highlight regexp")
1021                                      (number :tag "Group for entire word" 0)
1022                                      (number :tag "Group for displayed part" 0)
1023                                      (symbol :tag "Face"
1024                                              gnus-emphasis-highlight-words)))))
1025   :group 'gnus-summary-visual)
1026
1027 (defcustom gnus-summary-show-article-charset-alist
1028   nil
1029   "Alist of number and charset.
1030 The article will be shown with the charset corresponding to the
1031 numbered argument.
1032 For example: ((1 . cn-gb-2312) (2 . big5))."
1033   :version "21.1"
1034   :type '(repeat (cons (number :tag "Argument" 1)
1035                        (symbol :tag "Charset")))
1036   :group 'gnus-charset)
1037
1038 (defcustom gnus-preserve-marks t
1039   "Whether marks are preserved when moving, copying and respooling messages."
1040   :version "21.1"
1041   :type 'boolean
1042   :group 'gnus-summary-marks)
1043
1044 (defcustom gnus-alter-articles-to-read-function nil
1045   "Function to be called to alter the list of articles to be selected."
1046   :type '(choice (const nil) function)
1047   :group 'gnus-summary)
1048
1049 (defcustom gnus-orphan-score nil
1050   "*All orphans get this score added.  Set in the score file."
1051   :group 'gnus-score-default
1052   :type '(choice (const nil)
1053                  integer))
1054
1055 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1056   "*A regexp to match MIME parts when saving multiple parts of a
1057 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1058 This regexp will be used by default when prompting the user for which
1059 type of files to save."
1060   :group 'gnus-summary
1061   :type 'regexp)
1062
1063 (defcustom gnus-read-all-available-headers nil
1064   "Whether Gnus should parse all headers made available to it.
1065 This is mostly relevant for slow backends where the user may
1066 wish to widen the summary buffer to include all headers
1067 that were fetched.  Say, for nnultimate groups."
1068   :group 'gnus-summary
1069   :type '(choice boolean regexp))
1070
1071 (defcustom gnus-summary-muttprint-program "muttprint"
1072   "Command (and optional arguments) used to run Muttprint."
1073   :version "21.3"
1074   :group 'gnus-summary
1075   :type 'string)
1076
1077 (defcustom gnus-article-loose-mime nil
1078   "If non-nil, don't require MIME-Version header.
1079 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1080 supply the MIME-Version header or deliberately strip it From the mail.
1081 Set it to non-nil, Gnus will treat some articles as MIME even if
1082 the MIME-Version header is missed."
1083   :version "21.3"
1084   :type 'boolean
1085   :group 'gnus-article-mime)
1086
1087 (defcustom gnus-article-emulate-mime t
1088   "If non-nil, use MIME emulation for uuencode and the like.
1089 This means that Gnus will search message bodies for text that look
1090 like uuencoded bits, yEncoded bits, and so on, and present that using
1091 the normal Gnus MIME machinery."
1092   :type 'boolean
1093   :group 'gnus-article-mime)
1094
1095 ;;; Internal variables
1096
1097 (defvar gnus-summary-display-cache nil)
1098 (defvar gnus-article-mime-handles nil)
1099 (defvar gnus-article-decoded-p nil)
1100 (defvar gnus-article-charset nil)
1101 (defvar gnus-article-ignored-charsets nil)
1102 (defvar gnus-scores-exclude-files nil)
1103 (defvar gnus-page-broken nil)
1104
1105 (defvar gnus-original-article nil)
1106 (defvar gnus-article-internal-prepare-hook nil)
1107 (defvar gnus-newsgroup-process-stack nil)
1108
1109 (defvar gnus-thread-indent-array nil)
1110 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1111 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1112   "Function called to sort the articles within a thread after it has been gathered together.")
1113
1114 (defvar gnus-summary-save-parts-type-history nil)
1115 (defvar gnus-summary-save-parts-last-directory nil)
1116
1117 ;; Avoid highlighting in kill files.
1118 (defvar gnus-summary-inhibit-highlight nil)
1119 (defvar gnus-newsgroup-selected-overlay nil)
1120 (defvar gnus-inhibit-limiting nil)
1121 (defvar gnus-newsgroup-adaptive-score-file nil)
1122 (defvar gnus-current-score-file nil)
1123 (defvar gnus-current-move-group nil)
1124 (defvar gnus-current-copy-group nil)
1125 (defvar gnus-current-crosspost-group nil)
1126 (defvar gnus-newsgroup-display nil)
1127
1128 (defvar gnus-newsgroup-dependencies nil)
1129 (defvar gnus-newsgroup-adaptive nil)
1130 (defvar gnus-summary-display-article-function nil)
1131 (defvar gnus-summary-highlight-line-function nil
1132   "Function called after highlighting a summary line.")
1133
1134 (defvar gnus-summary-line-format-alist
1135   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1136     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1137     (?s gnus-tmp-subject-or-nil ?s)
1138     (?n gnus-tmp-name ?s)
1139     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1140         ?s)
1141     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1142             gnus-tmp-from) ?s)
1143     (?F gnus-tmp-from ?s)
1144     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1145     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1146     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1147     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1148     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1149     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1150     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1151     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1152     (?L gnus-tmp-lines ?s)
1153     (?O gnus-tmp-downloaded ?c)
1154     (?I gnus-tmp-indentation ?s)
1155     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1156     (?R gnus-tmp-replied ?c)
1157     (?\[ gnus-tmp-opening-bracket ?c)
1158     (?\] gnus-tmp-closing-bracket ?c)
1159     (?\> (make-string gnus-tmp-level ? ) ?s)
1160     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1161     (?i gnus-tmp-score ?d)
1162     (?z gnus-tmp-score-char ?c)
1163     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1164     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1165     (?U gnus-tmp-unread ?c)
1166     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1167         ?s)
1168     (?t (gnus-summary-number-of-articles-in-thread
1169          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1170         ?d)
1171     (?e (gnus-summary-number-of-articles-in-thread
1172          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1173         ?c)
1174     (?u gnus-tmp-user-defined ?s)
1175     (?P (gnus-pick-line-number) ?d)
1176     (?B gnus-tmp-thread-tree-header-string ?s)
1177     (user-date (gnus-user-date
1178                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1179   "An alist of format specifications that can appear in summary lines.
1180 These are paired with what variables they correspond with, along with
1181 the type of the variable (string, integer, character, etc).")
1182
1183 (defvar gnus-summary-dummy-line-format-alist
1184   `((?S gnus-tmp-subject ?s)
1185     (?N gnus-tmp-number ?d)
1186     (?u gnus-tmp-user-defined ?s)))
1187
1188 (defvar gnus-summary-mode-line-format-alist
1189   `((?G gnus-tmp-group-name ?s)
1190     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1191     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1192     (?A gnus-tmp-article-number ?d)
1193     (?Z gnus-tmp-unread-and-unselected ?s)
1194     (?V gnus-version ?s)
1195     (?U gnus-tmp-unread-and-unticked ?d)
1196     (?S gnus-tmp-subject ?s)
1197     (?e gnus-tmp-unselected ?d)
1198     (?u gnus-tmp-user-defined ?s)
1199     (?d (length gnus-newsgroup-dormant) ?d)
1200     (?t (length gnus-newsgroup-marked) ?d)
1201     (?h (length gnus-newsgroup-spam-marked) ?d)
1202     (?r (length gnus-newsgroup-reads) ?d)
1203     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1204     (?E gnus-newsgroup-expunged-tally ?d)
1205     (?s (gnus-current-score-file-nondirectory) ?s)))
1206
1207 (defvar gnus-last-search-regexp nil
1208   "Default regexp for article search command.")
1209
1210 (defvar gnus-last-shell-command nil
1211   "Default shell command on article.")
1212
1213 (defvar gnus-newsgroup-agentized nil
1214   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1215 (defvar gnus-newsgroup-begin nil)
1216 (defvar gnus-newsgroup-end nil)
1217 (defvar gnus-newsgroup-last-rmail nil)
1218 (defvar gnus-newsgroup-last-mail nil)
1219 (defvar gnus-newsgroup-last-folder nil)
1220 (defvar gnus-newsgroup-last-file nil)
1221 (defvar gnus-newsgroup-auto-expire nil)
1222 (defvar gnus-newsgroup-active nil)
1223
1224 (defvar gnus-newsgroup-data nil)
1225 (defvar gnus-newsgroup-data-reverse nil)
1226 (defvar gnus-newsgroup-limit nil)
1227 (defvar gnus-newsgroup-limits nil)
1228
1229 (defvar gnus-newsgroup-unreads nil
1230   "Sorted list of unread articles in the current newsgroup.")
1231
1232 (defvar gnus-newsgroup-unselected nil
1233   "Sorted list of unselected unread articles in the current newsgroup.")
1234
1235 (defvar gnus-newsgroup-reads nil
1236   "Alist of read articles and article marks in the current newsgroup.")
1237
1238 (defvar gnus-newsgroup-expunged-tally nil)
1239
1240 (defvar gnus-newsgroup-marked nil
1241   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1242
1243 (defvar gnus-newsgroup-spam-marked nil
1244   "List of ranges of articles that have been marked as spam.")
1245
1246 (defvar gnus-newsgroup-killed nil
1247   "List of ranges of articles that have been through the scoring process.")
1248
1249 (defvar gnus-newsgroup-cached nil
1250   "Sorted list of articles that come from the article cache.")
1251
1252 (defvar gnus-newsgroup-saved nil
1253   "List of articles that have been saved.")
1254
1255 (defvar gnus-newsgroup-kill-headers nil)
1256
1257 (defvar gnus-newsgroup-replied nil
1258   "List of articles that have been replied to in the current newsgroup.")
1259
1260 (defvar gnus-newsgroup-forwarded nil
1261   "List of articles that have been forwarded in the current newsgroup.")
1262
1263 (defvar gnus-newsgroup-recent nil
1264   "List of articles that have are recent in the current newsgroup.")
1265
1266 (defvar gnus-newsgroup-expirable nil
1267   "Sorted list of articles in the current newsgroup that can be expired.")
1268
1269 (defvar gnus-newsgroup-processable nil
1270   "List of articles in the current newsgroup that can be processed.")
1271
1272 (defvar gnus-newsgroup-downloadable nil
1273   "Sorted list of articles in the current newsgroup that can be processed.")
1274
1275 (defvar gnus-newsgroup-unfetched nil
1276   "Sorted list of articles in the current newsgroup whose headers have
1277 not been fetched into the agent.
1278
1279 This list will always be a subset of gnus-newsgroup-undownloaded.")
1280
1281 (defvar gnus-newsgroup-undownloaded nil
1282   "List of articles in the current newsgroup that haven't been downloaded.")
1283
1284 (defvar gnus-newsgroup-unsendable nil
1285   "List of articles in the current newsgroup that won't be sent.")
1286
1287 (defvar gnus-newsgroup-bookmarks nil
1288   "List of articles in the current newsgroup that have bookmarks.")
1289
1290 (defvar gnus-newsgroup-dormant nil
1291   "Sorted list of dormant articles in the current newsgroup.")
1292
1293 (defvar gnus-newsgroup-unseen nil
1294   "List of unseen articles in the current newsgroup.")
1295
1296 (defvar gnus-newsgroup-seen nil
1297   "Range of seen articles in the current newsgroup.")
1298
1299 (defvar gnus-newsgroup-articles nil
1300   "List of articles in the current newsgroup.")
1301
1302 (defvar gnus-newsgroup-scored nil
1303   "List of scored articles in the current newsgroup.")
1304
1305 (defvar gnus-newsgroup-headers nil
1306   "List of article headers in the current newsgroup.")
1307
1308 (defvar gnus-newsgroup-threads nil)
1309
1310 (defvar gnus-newsgroup-prepared nil
1311   "Whether the current group has been prepared properly.")
1312
1313 (defvar gnus-newsgroup-ancient nil
1314   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1315
1316 (defvar gnus-newsgroup-sparse nil)
1317
1318 (defvar gnus-current-article nil)
1319 (defvar gnus-article-current nil)
1320 (defvar gnus-current-headers nil)
1321 (defvar gnus-have-all-headers nil)
1322 (defvar gnus-last-article nil)
1323 (defvar gnus-newsgroup-history nil)
1324 (defvar gnus-newsgroup-charset nil)
1325 (defvar gnus-newsgroup-ephemeral-charset nil)
1326 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1327
1328 (defvar gnus-article-before-search nil)
1329
1330 (defvar gnus-summary-local-variables
1331   '(gnus-newsgroup-name
1332     gnus-newsgroup-begin gnus-newsgroup-end
1333     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1334     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1335     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1336     gnus-newsgroup-unselected gnus-newsgroup-marked
1337     gnus-newsgroup-spam-marked
1338     gnus-newsgroup-reads gnus-newsgroup-saved
1339     gnus-newsgroup-replied gnus-newsgroup-forwarded
1340     gnus-newsgroup-recent
1341     gnus-newsgroup-expirable
1342     gnus-newsgroup-processable gnus-newsgroup-killed
1343     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1344     gnus-newsgroup-unfetched
1345     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1346     gnus-newsgroup-seen gnus-newsgroup-articles
1347     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1348     gnus-newsgroup-headers gnus-newsgroup-threads
1349     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1350     gnus-current-article gnus-current-headers gnus-have-all-headers
1351     gnus-last-article gnus-article-internal-prepare-hook
1352     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1353     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1354     gnus-thread-expunge-below
1355     gnus-score-alist gnus-current-score-file
1356     (gnus-summary-expunge-below . global)
1357     (gnus-summary-mark-below . global)
1358     (gnus-orphan-score . global)
1359     gnus-newsgroup-active gnus-scores-exclude-files
1360     gnus-newsgroup-history gnus-newsgroup-ancient
1361     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1362     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1363     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1364     (gnus-newsgroup-expunged-tally . 0)
1365     gnus-cache-removable-articles gnus-newsgroup-cached
1366     gnus-newsgroup-data gnus-newsgroup-data-reverse
1367     gnus-newsgroup-limit gnus-newsgroup-limits
1368     gnus-newsgroup-charset gnus-newsgroup-display)
1369   "Variables that are buffer-local to the summary buffers.")
1370
1371 (defvar gnus-newsgroup-variables nil
1372   "A list of variables that have separate values in different newsgroups.
1373 A list of newsgroup (summary buffer) local variables, or cons of
1374 variables and their default values (when the default values are not
1375 nil), that should be made global while the summary buffer is active.
1376 These variables can be used to set variables in the group parameters
1377 while still allowing them to affect operations done in other
1378 buffers. For example:
1379
1380 \(setq gnus-newsgroup-variables
1381      '(message-use-followup-to
1382        (gnus-visible-headers .
1383          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1384 ")
1385
1386 ;; Byte-compiler warning.
1387 (eval-when-compile
1388   ;; Bind features so that require will believe that gnus-sum has
1389   ;; already been loaded (avoids infinite recursion)
1390   (let ((features (cons 'gnus-sum features)))
1391     ;; Several of the declarations in gnus-sum are needed to load the
1392     ;; following files. Right now, these definitions have been
1393     ;; compiled but not defined (evaluated).  We could either do a
1394     ;; eval-and-compile about all of the declarations or evaluate the
1395     ;; source file.
1396     (if (boundp 'gnus-newsgroup-variables)
1397         nil
1398       (load "gnus-sum.el" t t t))
1399     (require 'gnus)
1400     (require 'gnus-agent)
1401     (require 'gnus-art)))
1402
1403 ;; MIME stuff.
1404
1405 (defvar gnus-decode-encoded-word-methods
1406   '(mail-decode-encoded-word-string)
1407   "List of methods used to decode encoded words.
1408
1409 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1410 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1411 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1412 whose names match REGEXP.
1413
1414 For example:
1415 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1416  mail-decode-encoded-word-string
1417  (\"chinese\" . rfc1843-decode-string))")
1418
1419 (defvar gnus-decode-encoded-word-methods-cache nil)
1420
1421 (defun gnus-multi-decode-encoded-word-string (string)
1422   "Apply the functions from `gnus-encoded-word-methods' that match."
1423   (unless (and gnus-decode-encoded-word-methods-cache
1424                (eq gnus-newsgroup-name
1425                    (car gnus-decode-encoded-word-methods-cache)))
1426     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1427     (mapcar (lambda (x)
1428               (if (symbolp x)
1429                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1430                 (if (and gnus-newsgroup-name
1431                          (string-match (car x) gnus-newsgroup-name))
1432                     (nconc gnus-decode-encoded-word-methods-cache
1433                            (list (cdr x))))))
1434             gnus-decode-encoded-word-methods))
1435   (let ((xlist gnus-decode-encoded-word-methods-cache))
1436     (pop xlist)
1437     (while xlist
1438       (setq string (funcall (pop xlist) string))))
1439   string)
1440
1441 ;; Subject simplification.
1442
1443 (defun gnus-simplify-whitespace (str)
1444   "Remove excessive whitespace from STR."
1445   ;; Multiple spaces.
1446   (while (string-match "[ \t][ \t]+" str)
1447     (setq str (concat (substring str 0 (match-beginning 0))
1448                         " "
1449                         (substring str (match-end 0)))))
1450   ;; Leading spaces.
1451   (when (string-match "^[ \t]+" str)
1452     (setq str (substring str (match-end 0))))
1453   ;; Trailing spaces.
1454   (when (string-match "[ \t]+$" str)
1455     (setq str (substring str 0 (match-beginning 0))))
1456   str)
1457
1458 (defun gnus-simplify-all-whitespace (str)
1459   "Remove all whitespace from STR."
1460   (while (string-match "[ \t\n]+" str)
1461     (setq str (replace-match "" nil nil str)))
1462   str)
1463
1464 (defsubst gnus-simplify-subject-re (subject)
1465   "Remove \"Re:\" from subject lines."
1466   (if (string-match message-subject-re-regexp subject)
1467       (substring subject (match-end 0))
1468     subject))
1469
1470 (defun gnus-simplify-subject (subject &optional re-only)
1471   "Remove `Re:' and words in parentheses.
1472 If RE-ONLY is non-nil, strip leading `Re:'s only."
1473   (let ((case-fold-search t))           ;Ignore case.
1474     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1475     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1476       (setq subject (substring subject (match-end 0))))
1477     ;; Remove uninteresting prefixes.
1478     (when (and (not re-only)
1479                gnus-simplify-ignored-prefixes
1480                (string-match gnus-simplify-ignored-prefixes subject))
1481       (setq subject (substring subject (match-end 0))))
1482     ;; Remove words in parentheses from end.
1483     (unless re-only
1484       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1485         (setq subject (substring subject 0 (match-beginning 0)))))
1486     ;; Return subject string.
1487     subject))
1488
1489 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1490 ;; all whitespace.
1491 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1492   (goto-char (point-min))
1493   (while (re-search-forward regexp nil t)
1494     (replace-match (or newtext ""))))
1495
1496 (defun gnus-simplify-buffer-fuzzy ()
1497   "Simplify string in the buffer fuzzily.
1498 The string in the accessible portion of the current buffer is simplified.
1499 It is assumed to be a single-line subject.
1500 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1501 matter is removed.  Additional things can be deleted by setting
1502 `gnus-simplify-subject-fuzzy-regexp'."
1503   (let ((case-fold-search t)
1504         (modified-tick))
1505     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1506
1507     (while (not (eq modified-tick (buffer-modified-tick)))
1508       (setq modified-tick (buffer-modified-tick))
1509       (cond
1510        ((listp gnus-simplify-subject-fuzzy-regexp)
1511         (mapcar 'gnus-simplify-buffer-fuzzy-step
1512                 gnus-simplify-subject-fuzzy-regexp))
1513        (gnus-simplify-subject-fuzzy-regexp
1514         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1515       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1516       (gnus-simplify-buffer-fuzzy-step
1517        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1518       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1519
1520     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1521     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1522     (gnus-simplify-buffer-fuzzy-step " $")
1523     (gnus-simplify-buffer-fuzzy-step "^ +")))
1524
1525 (defun gnus-simplify-subject-fuzzy (subject)
1526   "Simplify a subject string fuzzily.
1527 See `gnus-simplify-buffer-fuzzy' for details."
1528   (save-excursion
1529     (gnus-set-work-buffer)
1530     (let ((case-fold-search t))
1531       ;; Remove uninteresting prefixes.
1532       (when (and gnus-simplify-ignored-prefixes
1533                  (string-match gnus-simplify-ignored-prefixes subject))
1534         (setq subject (substring subject (match-end 0))))
1535       (insert subject)
1536       (inline (gnus-simplify-buffer-fuzzy))
1537       (buffer-string))))
1538
1539 (defsubst gnus-simplify-subject-fully (subject)
1540   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1541   (cond
1542    (gnus-simplify-subject-functions
1543     (gnus-map-function gnus-simplify-subject-functions subject))
1544    ((null gnus-summary-gather-subject-limit)
1545     (gnus-simplify-subject-re subject))
1546    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1547     (gnus-simplify-subject-fuzzy subject))
1548    ((numberp gnus-summary-gather-subject-limit)
1549     (gnus-limit-string (gnus-simplify-subject-re subject)
1550                        gnus-summary-gather-subject-limit))
1551    (t
1552     subject)))
1553
1554 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1555   "Check whether two subjects are equal.
1556 If optional argument SIMPLE-FIRST is t, first argument is already
1557 simplified."
1558   (cond
1559    ((null simple-first)
1560     (equal (gnus-simplify-subject-fully s1)
1561            (gnus-simplify-subject-fully s2)))
1562    (t
1563     (equal s1
1564            (gnus-simplify-subject-fully s2)))))
1565
1566 (defun gnus-summary-bubble-group ()
1567   "Increase the score of the current group.
1568 This is a handy function to add to `gnus-summary-exit-hook' to
1569 increase the score of each group you read."
1570   (gnus-group-add-score gnus-newsgroup-name))
1571
1572 \f
1573 ;;;
1574 ;;; Gnus summary mode
1575 ;;;
1576
1577 (put 'gnus-summary-mode 'mode-class 'special)
1578
1579 (defvar gnus-article-commands-menu)
1580
1581 ;; Non-orthogonal keys
1582
1583 (gnus-define-keys gnus-summary-mode-map
1584   " " gnus-summary-next-page
1585   "\177" gnus-summary-prev-page
1586   [delete] gnus-summary-prev-page
1587   [backspace] gnus-summary-prev-page
1588   "\r" gnus-summary-scroll-up
1589   "\M-\r" gnus-summary-scroll-down
1590   "n" gnus-summary-next-unread-article
1591   "p" gnus-summary-prev-unread-article
1592   "N" gnus-summary-next-article
1593   "P" gnus-summary-prev-article
1594   "\M-\C-n" gnus-summary-next-same-subject
1595   "\M-\C-p" gnus-summary-prev-same-subject
1596   "\M-n" gnus-summary-next-unread-subject
1597   "\M-p" gnus-summary-prev-unread-subject
1598   "." gnus-summary-first-unread-article
1599   "," gnus-summary-best-unread-article
1600   "\M-s" gnus-summary-search-article-forward
1601   "\M-r" gnus-summary-search-article-backward
1602   "<" gnus-summary-beginning-of-article
1603   ">" gnus-summary-end-of-article
1604   "j" gnus-summary-goto-article
1605   "^" gnus-summary-refer-parent-article
1606   "\M-^" gnus-summary-refer-article
1607   "u" gnus-summary-tick-article-forward
1608   "!" gnus-summary-tick-article-forward
1609   "U" gnus-summary-tick-article-backward
1610   "d" gnus-summary-mark-as-read-forward
1611   "D" gnus-summary-mark-as-read-backward
1612   "E" gnus-summary-mark-as-expirable
1613   "\M-u" gnus-summary-clear-mark-forward
1614   "\M-U" gnus-summary-clear-mark-backward
1615   "k" gnus-summary-kill-same-subject-and-select
1616   "\C-k" gnus-summary-kill-same-subject
1617   "\M-\C-k" gnus-summary-kill-thread
1618   "\M-\C-l" gnus-summary-lower-thread
1619   "e" gnus-summary-edit-article
1620   "#" gnus-summary-mark-as-processable
1621   "\M-#" gnus-summary-unmark-as-processable
1622   "\M-\C-t" gnus-summary-toggle-threads
1623   "\M-\C-s" gnus-summary-show-thread
1624   "\M-\C-h" gnus-summary-hide-thread
1625   "\M-\C-f" gnus-summary-next-thread
1626   "\M-\C-b" gnus-summary-prev-thread
1627   [(meta down)] gnus-summary-next-thread
1628   [(meta up)] gnus-summary-prev-thread
1629   "\M-\C-u" gnus-summary-up-thread
1630   "\M-\C-d" gnus-summary-down-thread
1631   "&" gnus-summary-execute-command
1632   "c" gnus-summary-catchup-and-exit
1633   "\C-w" gnus-summary-mark-region-as-read
1634   "\C-t" gnus-summary-toggle-truncation
1635   "?" gnus-summary-mark-as-dormant
1636   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1637   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1638   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1639   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1640   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1641   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1642   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1643   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1644   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1645   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1646   "=" gnus-summary-expand-window
1647   "\C-x\C-s" gnus-summary-reselect-current-group
1648   "\M-g" gnus-summary-rescan-group
1649   "w" gnus-summary-stop-page-breaking
1650   "\C-c\C-r" gnus-summary-caesar-message
1651   "f" gnus-summary-followup
1652   "F" gnus-summary-followup-with-original
1653   "C" gnus-summary-cancel-article
1654   "r" gnus-summary-reply
1655   "R" gnus-summary-reply-with-original
1656   "\C-c\C-f" gnus-summary-mail-forward
1657   "o" gnus-summary-save-article
1658   "\C-o" gnus-summary-save-article-mail
1659   "|" gnus-summary-pipe-output
1660   "\M-k" gnus-summary-edit-local-kill
1661   "\M-K" gnus-summary-edit-global-kill
1662   ;; "V" gnus-version
1663   "\C-c\C-d" gnus-summary-describe-group
1664   "q" gnus-summary-exit
1665   "Q" gnus-summary-exit-no-update
1666   "\C-c\C-i" gnus-info-find-node
1667   gnus-mouse-2 gnus-mouse-pick-article
1668   "m" gnus-summary-mail-other-window
1669   "a" gnus-summary-post-news
1670   "i" gnus-summary-news-other-window
1671   "x" gnus-summary-limit-to-unread
1672   "s" gnus-summary-isearch-article
1673   "t" gnus-summary-toggle-header
1674   "g" gnus-summary-show-article
1675   "l" gnus-summary-goto-last-article
1676   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1677   "\C-d" gnus-summary-enter-digest-group
1678   "\M-\C-d" gnus-summary-read-document
1679   "\M-\C-e" gnus-summary-edit-parameters
1680   "\M-\C-a" gnus-summary-customize-parameters
1681   "\C-c\C-b" gnus-bug
1682   "*" gnus-cache-enter-article
1683   "\M-*" gnus-cache-remove-article
1684   "\M-&" gnus-summary-universal-argument
1685   "\C-l" gnus-recenter
1686   "I" gnus-summary-increase-score
1687   "L" gnus-summary-lower-score
1688   "\M-i" gnus-symbolic-argument
1689   "h" gnus-summary-select-article-buffer
1690
1691   "b" gnus-article-view-part
1692   "\M-t" gnus-summary-toggle-display-buttonized
1693
1694   "V" gnus-summary-score-map
1695   "X" gnus-uu-extract-map
1696   "S" gnus-summary-send-map)
1697
1698 ;; Sort of orthogonal keymap
1699 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1700   "t" gnus-summary-tick-article-forward
1701   "!" gnus-summary-tick-article-forward
1702   "d" gnus-summary-mark-as-read-forward
1703   "r" gnus-summary-mark-as-read-forward
1704   "c" gnus-summary-clear-mark-forward
1705   " " gnus-summary-clear-mark-forward
1706   "e" gnus-summary-mark-as-expirable
1707   "x" gnus-summary-mark-as-expirable
1708   "?" gnus-summary-mark-as-dormant
1709   "b" gnus-summary-set-bookmark
1710   "B" gnus-summary-remove-bookmark
1711   "#" gnus-summary-mark-as-processable
1712   "\M-#" gnus-summary-unmark-as-processable
1713   "S" gnus-summary-limit-include-expunged
1714   "C" gnus-summary-catchup
1715   "H" gnus-summary-catchup-to-here
1716   "h" gnus-summary-catchup-from-here
1717   "\C-c" gnus-summary-catchup-all
1718   "k" gnus-summary-kill-same-subject-and-select
1719   "K" gnus-summary-kill-same-subject
1720   "P" gnus-uu-mark-map)
1721
1722 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1723   "c" gnus-summary-clear-above
1724   "u" gnus-summary-tick-above
1725   "m" gnus-summary-mark-above
1726   "k" gnus-summary-kill-below)
1727
1728 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1729   "/" gnus-summary-limit-to-subject
1730   "n" gnus-summary-limit-to-articles
1731   "w" gnus-summary-pop-limit
1732   "s" gnus-summary-limit-to-subject
1733   "a" gnus-summary-limit-to-author
1734   "u" gnus-summary-limit-to-unread
1735   "m" gnus-summary-limit-to-marks
1736   "M" gnus-summary-limit-exclude-marks
1737   "v" gnus-summary-limit-to-score
1738   "*" gnus-summary-limit-include-cached
1739   "D" gnus-summary-limit-include-dormant
1740   "T" gnus-summary-limit-include-thread
1741   "d" gnus-summary-limit-exclude-dormant
1742   "t" gnus-summary-limit-to-age
1743   "." gnus-summary-limit-to-unseen
1744   "x" gnus-summary-limit-to-extra
1745   "p" gnus-summary-limit-to-display-predicate
1746   "E" gnus-summary-limit-include-expunged
1747   "c" gnus-summary-limit-exclude-childless-dormant
1748   "C" gnus-summary-limit-mark-excluded-as-read
1749   "o" gnus-summary-insert-old-articles
1750   "N" gnus-summary-insert-new-articles)
1751
1752 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1753   "n" gnus-summary-next-unread-article
1754   "p" gnus-summary-prev-unread-article
1755   "N" gnus-summary-next-article
1756   "P" gnus-summary-prev-article
1757   "\C-n" gnus-summary-next-same-subject
1758   "\C-p" gnus-summary-prev-same-subject
1759   "\M-n" gnus-summary-next-unread-subject
1760   "\M-p" gnus-summary-prev-unread-subject
1761   "f" gnus-summary-first-unread-article
1762   "b" gnus-summary-best-unread-article
1763   "j" gnus-summary-goto-article
1764   "g" gnus-summary-goto-subject
1765   "l" gnus-summary-goto-last-article
1766   "o" gnus-summary-pop-article)
1767
1768 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1769   "k" gnus-summary-kill-thread
1770   "l" gnus-summary-lower-thread
1771   "i" gnus-summary-raise-thread
1772   "T" gnus-summary-toggle-threads
1773   "t" gnus-summary-rethread-current
1774   "^" gnus-summary-reparent-thread
1775   "s" gnus-summary-show-thread
1776   "S" gnus-summary-show-all-threads
1777   "h" gnus-summary-hide-thread
1778   "H" gnus-summary-hide-all-threads
1779   "n" gnus-summary-next-thread
1780   "p" gnus-summary-prev-thread
1781   "u" gnus-summary-up-thread
1782   "o" gnus-summary-top-thread
1783   "d" gnus-summary-down-thread
1784   "#" gnus-uu-mark-thread
1785   "\M-#" gnus-uu-unmark-thread)
1786
1787 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1788   "g" gnus-summary-prepare
1789   "c" gnus-summary-insert-cached-articles
1790   "d" gnus-summary-insert-dormant-articles)
1791
1792 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1793   "c" gnus-summary-catchup-and-exit
1794   "C" gnus-summary-catchup-all-and-exit
1795   "E" gnus-summary-exit-no-update
1796   "Q" gnus-summary-exit
1797   "Z" gnus-summary-exit
1798   "n" gnus-summary-catchup-and-goto-next-group
1799   "R" gnus-summary-reselect-current-group
1800   "G" gnus-summary-rescan-group
1801   "N" gnus-summary-next-group
1802   "s" gnus-summary-save-newsrc
1803   "P" gnus-summary-prev-group)
1804
1805 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1806   " " gnus-summary-next-page
1807   "n" gnus-summary-next-page
1808   "\177" gnus-summary-prev-page
1809   [delete] gnus-summary-prev-page
1810   "p" gnus-summary-prev-page
1811   "\r" gnus-summary-scroll-up
1812   "\M-\r" gnus-summary-scroll-down
1813   "<" gnus-summary-beginning-of-article
1814   ">" gnus-summary-end-of-article
1815   "b" gnus-summary-beginning-of-article
1816   "e" gnus-summary-end-of-article
1817   "^" gnus-summary-refer-parent-article
1818   "r" gnus-summary-refer-parent-article
1819   "D" gnus-summary-enter-digest-group
1820   "R" gnus-summary-refer-references
1821   "T" gnus-summary-refer-thread
1822   "g" gnus-summary-show-article
1823   "s" gnus-summary-isearch-article
1824   "P" gnus-summary-print-article
1825   "M" gnus-mailing-list-insinuate
1826   "t" gnus-article-babel)
1827
1828 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1829   "b" gnus-article-add-buttons
1830   "B" gnus-article-add-buttons-to-head
1831   "o" gnus-article-treat-overstrike
1832   "e" gnus-article-emphasize
1833   "w" gnus-article-fill-cited-article
1834   "Q" gnus-article-fill-long-lines
1835   "C" gnus-article-capitalize-sentences
1836   "c" gnus-article-remove-cr
1837   "q" gnus-article-de-quoted-unreadable
1838   "6" gnus-article-de-base64-unreadable
1839   "Z" gnus-article-decode-HZ
1840   "h" gnus-article-wash-html
1841   "u" gnus-article-unsplit-urls
1842   "s" gnus-summary-force-verify-and-decrypt
1843   "f" gnus-article-display-x-face
1844   "l" gnus-summary-stop-page-breaking
1845   "r" gnus-summary-caesar-message
1846   "m" gnus-summary-morse-message
1847   "t" gnus-summary-toggle-header
1848   "g" gnus-treat-smiley
1849   "v" gnus-summary-verbose-headers
1850   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1851   "p" gnus-article-verify-x-pgp-sig
1852   "d" gnus-article-treat-dumbquotes)
1853
1854 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1855   ;; mnemonic: deuglif*Y*
1856   "u" gnus-article-outlook-unwrap-lines
1857   "a" gnus-article-outlook-repair-attribution
1858   "c" gnus-article-outlook-rearrange-citation
1859   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1860
1861 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1862   "a" gnus-article-hide
1863   "h" gnus-article-hide-headers
1864   "b" gnus-article-hide-boring-headers
1865   "s" gnus-article-hide-signature
1866   "c" gnus-article-hide-citation
1867   "C" gnus-article-hide-citation-in-followups
1868   "l" gnus-article-hide-list-identifiers
1869   "B" gnus-article-strip-banner
1870   "P" gnus-article-hide-pem
1871   "\C-c" gnus-article-hide-citation-maybe)
1872
1873 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1874   "a" gnus-article-highlight
1875   "h" gnus-article-highlight-headers
1876   "c" gnus-article-highlight-citation
1877   "s" gnus-article-highlight-signature)
1878
1879 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1880   "f" gnus-article-treat-fold-headers
1881   "u" gnus-article-treat-unfold-headers
1882   "n" gnus-article-treat-fold-newsgroups)
1883
1884 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1885   "x" gnus-article-display-x-face
1886   "d" gnus-article-display-face
1887   "s" gnus-treat-smiley
1888   "D" gnus-article-remove-images
1889   "f" gnus-treat-from-picon
1890   "m" gnus-treat-mail-picon
1891   "n" gnus-treat-newsgroups-picon)
1892
1893 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1894   "w" gnus-article-decode-mime-words
1895   "c" gnus-article-decode-charset
1896   "v" gnus-mime-view-all-parts
1897   "b" gnus-article-view-part)
1898
1899 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1900   "z" gnus-article-date-ut
1901   "u" gnus-article-date-ut
1902   "l" gnus-article-date-local
1903   "p" gnus-article-date-english
1904   "e" gnus-article-date-lapsed
1905   "o" gnus-article-date-original
1906   "i" gnus-article-date-iso8601
1907   "s" gnus-article-date-user)
1908
1909 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1910   "t" gnus-article-remove-trailing-blank-lines
1911   "l" gnus-article-strip-leading-blank-lines
1912   "m" gnus-article-strip-multiple-blank-lines
1913   "a" gnus-article-strip-blank-lines
1914   "A" gnus-article-strip-all-blank-lines
1915   "s" gnus-article-strip-leading-space
1916   "e" gnus-article-strip-trailing-space
1917   "w" gnus-article-remove-leading-whitespace)
1918
1919 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1920   "v" gnus-version
1921   "f" gnus-summary-fetch-faq
1922   "d" gnus-summary-describe-group
1923   "h" gnus-summary-describe-briefly
1924   "i" gnus-info-find-node
1925   "c" gnus-group-fetch-charter
1926   "C" gnus-group-fetch-control)
1927
1928 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1929   "e" gnus-summary-expire-articles
1930   "\M-\C-e" gnus-summary-expire-articles-now
1931   "\177" gnus-summary-delete-article
1932   [delete] gnus-summary-delete-article
1933   [backspace] gnus-summary-delete-article
1934   "m" gnus-summary-move-article
1935   "r" gnus-summary-respool-article
1936   "w" gnus-summary-edit-article
1937   "c" gnus-summary-copy-article
1938   "B" gnus-summary-crosspost-article
1939   "q" gnus-summary-respool-query
1940   "t" gnus-summary-respool-trace
1941   "i" gnus-summary-import-article
1942   "I" gnus-summary-create-article
1943   "p" gnus-summary-article-posted-p)
1944
1945 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1946   "o" gnus-summary-save-article
1947   "m" gnus-summary-save-article-mail
1948   "F" gnus-summary-write-article-file
1949   "r" gnus-summary-save-article-rmail
1950   "f" gnus-summary-save-article-file
1951   "b" gnus-summary-save-article-body-file
1952   "h" gnus-summary-save-article-folder
1953   "v" gnus-summary-save-article-vm
1954   "p" gnus-summary-pipe-output
1955   "P" gnus-summary-muttprint
1956   "s" gnus-soup-add-article)
1957
1958 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1959   "b" gnus-summary-display-buttonized
1960   "m" gnus-summary-repair-multipart
1961   "v" gnus-article-view-part
1962   "o" gnus-article-save-part
1963   "c" gnus-article-copy-part
1964   "C" gnus-article-view-part-as-charset
1965   "e" gnus-article-view-part-externally
1966   "E" gnus-article-encrypt-body
1967   "i" gnus-article-inline-part
1968   "|" gnus-article-pipe-part)
1969
1970 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1971   "p" gnus-summary-mark-as-processable
1972   "u" gnus-summary-unmark-as-processable
1973   "U" gnus-summary-unmark-all-processable
1974   "v" gnus-uu-mark-over
1975   "s" gnus-uu-mark-series
1976   "r" gnus-uu-mark-region
1977   "g" gnus-uu-unmark-region
1978   "R" gnus-uu-mark-by-regexp
1979   "G" gnus-uu-unmark-by-regexp
1980   "t" gnus-uu-mark-thread
1981   "T" gnus-uu-unmark-thread
1982   "a" gnus-uu-mark-all
1983   "b" gnus-uu-mark-buffer
1984   "S" gnus-uu-mark-sparse
1985   "k" gnus-summary-kill-process-mark
1986   "y" gnus-summary-yank-process-mark
1987   "w" gnus-summary-save-process-mark
1988   "i" gnus-uu-invert-processable)
1989
1990 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1991   ;;"x" gnus-uu-extract-any
1992   "m" gnus-summary-save-parts
1993   "u" gnus-uu-decode-uu
1994   "U" gnus-uu-decode-uu-and-save
1995   "s" gnus-uu-decode-unshar
1996   "S" gnus-uu-decode-unshar-and-save
1997   "o" gnus-uu-decode-save
1998   "O" gnus-uu-decode-save
1999   "b" gnus-uu-decode-binhex
2000   "B" gnus-uu-decode-binhex
2001   "p" gnus-uu-decode-postscript
2002   "P" gnus-uu-decode-postscript-and-save)
2003
2004 (gnus-define-keys
2005     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2006   "u" gnus-uu-decode-uu-view
2007   "U" gnus-uu-decode-uu-and-save-view
2008   "s" gnus-uu-decode-unshar-view
2009   "S" gnus-uu-decode-unshar-and-save-view
2010   "o" gnus-uu-decode-save-view
2011   "O" gnus-uu-decode-save-view
2012   "b" gnus-uu-decode-binhex-view
2013   "B" gnus-uu-decode-binhex-view
2014   "p" gnus-uu-decode-postscript-view
2015   "P" gnus-uu-decode-postscript-and-save-view)
2016
2017 (defvar gnus-article-post-menu nil)
2018
2019 (defconst gnus-summary-menu-maxlen 20)
2020
2021 (defun gnus-summary-menu-split (menu)
2022   ;; If we have lots of elements, divide them into groups of 20
2023   ;; and make a pane (or submenu) for each one.
2024   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2025       (let ((menu menu) sublists next
2026             (i 1))
2027         (while menu
2028           ;; Pull off the next gnus-summary-menu-maxlen elements
2029           ;; and make them the next element of sublist.
2030           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2031           (if next
2032               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2033                       nil))
2034           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2035                                              (aref (car (last menu)) 0)) menu)
2036                                sublists))
2037           (setq i (1+ i))
2038           (setq menu next))
2039         (nreverse sublists))
2040     ;; Few elements--put them all in one pane.
2041     menu))
2042
2043 (defun gnus-summary-make-menu-bar ()
2044   (gnus-turn-off-edit-menu 'summary)
2045
2046   (unless (boundp 'gnus-summary-misc-menu)
2047
2048     (easy-menu-define
2049       gnus-summary-kill-menu gnus-summary-mode-map ""
2050       (cons
2051        "Score"
2052        (nconc
2053         (list
2054          ["Customize" gnus-score-customize t])
2055         (gnus-make-score-map 'increase)
2056         (gnus-make-score-map 'lower)
2057         '(("Mark"
2058            ["Kill below" gnus-summary-kill-below t]
2059            ["Mark above" gnus-summary-mark-above t]
2060            ["Tick above" gnus-summary-tick-above t]
2061            ["Clear above" gnus-summary-clear-above t])
2062           ["Current score" gnus-summary-current-score t]
2063           ["Set score" gnus-summary-set-score t]
2064           ["Switch current score file..." gnus-score-change-score-file t]
2065           ["Set mark below..." gnus-score-set-mark-below t]
2066           ["Set expunge below..." gnus-score-set-expunge-below t]
2067           ["Edit current score file" gnus-score-edit-current-scores t]
2068           ["Edit score file" gnus-score-edit-file t]
2069           ["Trace score" gnus-score-find-trace t]
2070           ["Find words" gnus-score-find-favourite-words t]
2071           ["Rescore buffer" gnus-summary-rescore t]
2072           ["Increase score..." gnus-summary-increase-score t]
2073           ["Lower score..." gnus-summary-lower-score t]))))
2074
2075     ;; Define both the Article menu in the summary buffer and the
2076     ;; equivalent Commands menu in the article buffer here for
2077     ;; consistency.
2078     (let ((innards
2079            `(("Hide"
2080               ["All" gnus-article-hide t]
2081               ["Headers" gnus-article-hide-headers t]
2082               ["Signature" gnus-article-hide-signature t]
2083               ["Citation" gnus-article-hide-citation t]
2084               ["List identifiers" gnus-article-hide-list-identifiers t]
2085               ["Banner" gnus-article-strip-banner t]
2086               ["Boring headers" gnus-article-hide-boring-headers t])
2087              ("Highlight"
2088               ["All" gnus-article-highlight t]
2089               ["Headers" gnus-article-highlight-headers t]
2090               ["Signature" gnus-article-highlight-signature t]
2091               ["Citation" gnus-article-highlight-citation t])
2092              ("MIME"
2093               ["Words" gnus-article-decode-mime-words t]
2094               ["Charset" gnus-article-decode-charset t]
2095               ["QP" gnus-article-de-quoted-unreadable t]
2096               ["Base64" gnus-article-de-base64-unreadable t]
2097               ["View MIME buttons" gnus-summary-display-buttonized t]
2098               ["View all" gnus-mime-view-all-parts t]
2099               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2100               ["Encrypt body" gnus-article-encrypt-body
2101                :active (not (gnus-group-read-only-p))
2102                ,@(if (featurep 'xemacs) nil
2103                    '(:help "Encrypt the message body on disk"))]
2104               ["Extract all parts" gnus-summary-save-parts t]
2105               ("Multipart"
2106                ["Repair multipart" gnus-summary-repair-multipart t]
2107                ["Add buttons" gnus-summary-display-buttonized t]
2108                ["Pipe part" gnus-article-pipe-part t]
2109                ["Inline part" gnus-article-inline-part t]
2110                ["Encrypt body" gnus-article-encrypt-body
2111                 :active (not (gnus-group-read-only-p))
2112                ,@(if (featurep 'xemacs) nil
2113                    '(:help "Encrypt the message body on disk"))]
2114                ["View part externally" gnus-article-view-part-externally t]
2115                ["View part with charset" gnus-article-view-part-as-charset t]
2116                ["Copy part" gnus-article-copy-part t]
2117                ["Save part" gnus-article-save-part t]
2118                ["View part" gnus-article-view-part t]))
2119              ("Date"
2120               ["Local" gnus-article-date-local t]
2121               ["ISO8601" gnus-article-date-iso8601 t]
2122               ["UT" gnus-article-date-ut t]
2123               ["Original" gnus-article-date-original t]
2124               ["Lapsed" gnus-article-date-lapsed t]
2125               ["User-defined" gnus-article-date-user t])
2126              ("Display"
2127               ["Remove images" gnus-article-remove-images t]
2128               ["Toggle smiley" gnus-treat-smiley t]
2129               ["Show X-Face" gnus-article-display-x-face t]
2130               ["Show picons in From" gnus-treat-from-picon t]
2131               ["Show picons in mail headers" gnus-treat-mail-picon t]
2132               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2133               ("View as different encoding"
2134                ,@(gnus-summary-menu-split
2135                   (mapcar
2136                    (lambda (cs)
2137                      ;; Since easymenu under Emacs doesn't allow
2138                      ;; lambda forms for menu commands, we should
2139                      ;; provide intern'ed function symbols.
2140                      (let ((command (intern (format "\
2141 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2142                        (fset command
2143                              `(lambda ()
2144                                 (interactive)
2145                                 (let ((gnus-summary-show-article-charset-alist
2146                                        '((1 . ,cs))))
2147                                   (gnus-summary-show-article 1))))
2148                        `[,(symbol-name cs) ,command t]))
2149                    (sort (if (fboundp 'coding-system-list)
2150                              (coding-system-list)
2151                            (mapcar 'car mm-mime-mule-charset-alist))
2152                          'string<)))))
2153              ("Washing"
2154               ("Remove Blanks"
2155                ["Leading" gnus-article-strip-leading-blank-lines t]
2156                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2157                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2158                ["All of the above" gnus-article-strip-blank-lines t]
2159                ["All" gnus-article-strip-all-blank-lines t]
2160                ["Leading space" gnus-article-strip-leading-space t]
2161                ["Trailing space" gnus-article-strip-trailing-space t]
2162                ["Leading space in headers"
2163                 gnus-article-remove-leading-whitespace t])
2164               ["Overstrike" gnus-article-treat-overstrike t]
2165               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2166               ["Emphasis" gnus-article-emphasize t]
2167               ["Word wrap" gnus-article-fill-cited-article t]
2168               ["Fill long lines" gnus-article-fill-long-lines t]
2169               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2170               ["CR" gnus-article-remove-cr t]
2171               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2172               ["Base64" gnus-article-de-base64-unreadable t]
2173               ["Rot 13" gnus-summary-caesar-message
2174                ,@(if (featurep 'xemacs) '(t)
2175                    '(:help "\"Caesar rotate\" article by 13"))]
2176               ["Morse decode" gnus-summary-morse-message t]
2177               ["Unix pipe..." gnus-summary-pipe-message t]
2178               ["Add buttons" gnus-article-add-buttons t]
2179               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2180               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2181               ["Verbose header" gnus-summary-verbose-headers t]
2182               ["Toggle header" gnus-summary-toggle-header t]
2183               ["Unfold headers" gnus-article-treat-unfold-headers t]
2184               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2185               ["Html" gnus-article-wash-html t]
2186               ["URLs" gnus-article-unsplit-urls t]
2187               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2188               ["HZ" gnus-article-decode-HZ t]
2189               ("(Outlook) Deuglify"
2190                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2191                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2192                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2193                ["Full (Outlook) deuglify"
2194                 gnus-article-outlook-deuglify-article t])
2195               )
2196              ("Output"
2197               ["Save in default format" gnus-summary-save-article
2198                ,@(if (featurep 'xemacs) '(t)
2199                    '(:help "Save article using default method"))]
2200               ["Save in file" gnus-summary-save-article-file
2201                ,@(if (featurep 'xemacs) '(t)
2202                    '(:help "Save article in file"))]
2203               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2204               ["Save in MH folder" gnus-summary-save-article-folder t]
2205               ["Save in VM folder" gnus-summary-save-article-vm t]
2206               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2207               ["Save body in file" gnus-summary-save-article-body-file t]
2208               ["Pipe through a filter" gnus-summary-pipe-output t]
2209               ["Add to SOUP packet" gnus-soup-add-article t]
2210               ["Print with Muttprint" gnus-summary-muttprint t]
2211               ["Print" gnus-summary-print-article t])
2212              ("Backend"
2213               ["Respool article..." gnus-summary-respool-article t]
2214               ["Move article..." gnus-summary-move-article
2215                (gnus-check-backend-function
2216                 'request-move-article gnus-newsgroup-name)]
2217               ["Copy article..." gnus-summary-copy-article t]
2218               ["Crosspost article..." gnus-summary-crosspost-article
2219                (gnus-check-backend-function
2220                 'request-replace-article gnus-newsgroup-name)]
2221               ["Import file..." gnus-summary-import-article
2222                (gnus-check-backend-function
2223                 'request-accept-article gnus-newsgroup-name)]
2224               ["Create article..." gnus-summary-create-article
2225                (gnus-check-backend-function
2226                 'request-accept-article gnus-newsgroup-name)]
2227               ["Check if posted" gnus-summary-article-posted-p t]
2228               ["Edit article" gnus-summary-edit-article
2229                (not (gnus-group-read-only-p))]
2230               ["Delete article" gnus-summary-delete-article
2231                (gnus-check-backend-function
2232                 'request-expire-articles gnus-newsgroup-name)]
2233               ["Query respool" gnus-summary-respool-query t]
2234               ["Trace respool" gnus-summary-respool-trace t]
2235               ["Delete expirable articles" gnus-summary-expire-articles-now
2236                (gnus-check-backend-function
2237                 'request-expire-articles gnus-newsgroup-name)])
2238              ("Extract"
2239               ["Uudecode" gnus-uu-decode-uu
2240                ,@(if (featurep 'xemacs) '(t)
2241                    '(:help "Decode uuencoded article(s)"))]
2242               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2243               ["Unshar" gnus-uu-decode-unshar t]
2244               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2245               ["Save" gnus-uu-decode-save t]
2246               ["Binhex" gnus-uu-decode-binhex t]
2247               ["Postscript" gnus-uu-decode-postscript t]
2248               ["All MIME parts" gnus-summary-save-parts t])
2249              ("Cache"
2250               ["Enter article" gnus-cache-enter-article t]
2251               ["Remove article" gnus-cache-remove-article t])
2252              ["Translate" gnus-article-babel t]
2253              ["Select article buffer" gnus-summary-select-article-buffer t]
2254              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2255              ["Isearch article..." gnus-summary-isearch-article t]
2256              ["Beginning of the article" gnus-summary-beginning-of-article t]
2257              ["End of the article" gnus-summary-end-of-article t]
2258              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2259              ["Fetch referenced articles" gnus-summary-refer-references t]
2260              ["Fetch current thread" gnus-summary-refer-thread t]
2261              ["Fetch article with id..." gnus-summary-refer-article t]
2262              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2263              ["Redisplay" gnus-summary-show-article t]
2264              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2265       (easy-menu-define
2266         gnus-summary-article-menu gnus-summary-mode-map ""
2267         (cons "Article" innards))
2268
2269       (if (not (keymapp gnus-summary-article-menu))
2270           (easy-menu-define
2271             gnus-article-commands-menu gnus-article-mode-map ""
2272             (cons "Commands" innards))
2273         ;; in Emacs, don't share menu.
2274         (setq gnus-article-commands-menu
2275               (copy-keymap gnus-summary-article-menu))
2276         (define-key gnus-article-mode-map [menu-bar commands]
2277           (cons "Commands" gnus-article-commands-menu))))
2278
2279     (easy-menu-define
2280       gnus-summary-thread-menu gnus-summary-mode-map ""
2281       '("Threads"
2282         ["Find all messages in thread" gnus-summary-refer-thread t]
2283         ["Toggle threading" gnus-summary-toggle-threads t]
2284         ["Hide threads" gnus-summary-hide-all-threads t]
2285         ["Show threads" gnus-summary-show-all-threads t]
2286         ["Hide thread" gnus-summary-hide-thread t]
2287         ["Show thread" gnus-summary-show-thread t]
2288         ["Go to next thread" gnus-summary-next-thread t]
2289         ["Go to previous thread" gnus-summary-prev-thread t]
2290         ["Go down thread" gnus-summary-down-thread t]
2291         ["Go up thread" gnus-summary-up-thread t]
2292         ["Top of thread" gnus-summary-top-thread t]
2293         ["Mark thread as read" gnus-summary-kill-thread t]
2294         ["Lower thread score" gnus-summary-lower-thread t]
2295         ["Raise thread score" gnus-summary-raise-thread t]
2296         ["Rethread current" gnus-summary-rethread-current t]))
2297
2298     (easy-menu-define
2299       gnus-summary-post-menu gnus-summary-mode-map ""
2300       `("Post"
2301         ["Send a message (mail or news)" gnus-summary-post-news
2302          ,@(if (featurep 'xemacs) '(t)
2303              '(:help "Post an article"))]
2304         ["Followup" gnus-summary-followup
2305          ,@(if (featurep 'xemacs) '(t)
2306              '(:help "Post followup to this article"))]
2307         ["Followup and yank" gnus-summary-followup-with-original
2308          ,@(if (featurep 'xemacs) '(t)
2309              '(:help "Post followup to this article, quoting its contents"))]
2310         ["Supersede article" gnus-summary-supersede-article t]
2311         ["Cancel article" gnus-summary-cancel-article
2312          ,@(if (featurep 'xemacs) '(t)
2313              '(:help "Cancel an article you posted"))]
2314         ["Reply" gnus-summary-reply t]
2315         ["Reply and yank" gnus-summary-reply-with-original t]
2316         ["Wide reply" gnus-summary-wide-reply t]
2317         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2318          ,@(if (featurep 'xemacs) '(t)
2319              '(:help "Mail a reply, quoting this article"))]
2320         ["Very wide reply" gnus-summary-very-wide-reply t]
2321         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2322          ,@(if (featurep 'xemacs) '(t)
2323              '(:help "Mail a very wide reply, quoting this article"))]
2324         ["Mail forward" gnus-summary-mail-forward t]
2325         ["Post forward" gnus-summary-post-forward t]
2326         ["Digest and mail" gnus-uu-digest-mail-forward t]
2327         ["Digest and post" gnus-uu-digest-post-forward t]
2328         ["Resend message" gnus-summary-resend-message t]
2329         ["Resend message edit" gnus-summary-resend-message-edit t]
2330         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2331         ["Send a mail" gnus-summary-mail-other-window t]
2332         ["Create a local message" gnus-summary-news-other-window t]
2333         ["Uuencode and post" gnus-uu-post-news
2334          ,@(if (featurep 'xemacs) '(t)
2335              '(:help "Post a uuencoded article"))]
2336         ["Followup via news" gnus-summary-followup-to-mail t]
2337         ["Followup via news and yank"
2338          gnus-summary-followup-to-mail-with-original t]
2339         ;;("Draft"
2340         ;;["Send" gnus-summary-send-draft t]
2341         ;;["Send bounced" gnus-resend-bounced-mail t])
2342         ))
2343
2344     (cond
2345      ((not (keymapp gnus-summary-post-menu))
2346       (setq gnus-article-post-menu gnus-summary-post-menu))
2347      ((not gnus-article-post-menu)
2348       ;; Don't share post menu.
2349       (setq gnus-article-post-menu
2350             (copy-keymap gnus-summary-post-menu))))
2351     (define-key gnus-article-mode-map [menu-bar post]
2352       (cons "Post" gnus-article-post-menu))
2353
2354     (easy-menu-define
2355       gnus-summary-misc-menu gnus-summary-mode-map ""
2356       `("Gnus"
2357         ("Mark Read"
2358          ["Mark as read" gnus-summary-mark-as-read-forward t]
2359          ["Mark same subject and select"
2360           gnus-summary-kill-same-subject-and-select t]
2361          ["Mark same subject" gnus-summary-kill-same-subject t]
2362          ["Catchup" gnus-summary-catchup
2363           ,@(if (featurep 'xemacs) '(t)
2364               '(:help "Mark unread articles in this group as read"))]
2365          ["Catchup all" gnus-summary-catchup-all t]
2366          ["Catchup to here" gnus-summary-catchup-to-here t]
2367          ["Catchup from here" gnus-summary-catchup-from-here t]
2368          ["Catchup region" gnus-summary-mark-region-as-read 
2369           (gnus-mark-active-p)]
2370          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2371         ("Mark Various"
2372          ["Tick" gnus-summary-tick-article-forward t]
2373          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2374          ["Remove marks" gnus-summary-clear-mark-forward t]
2375          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2376          ["Set bookmark" gnus-summary-set-bookmark t]
2377          ["Remove bookmark" gnus-summary-remove-bookmark t])
2378         ("Limit to"
2379          ["Marks..." gnus-summary-limit-to-marks t]
2380          ["Subject..." gnus-summary-limit-to-subject t]
2381          ["Author..." gnus-summary-limit-to-author t]
2382          ["Age..." gnus-summary-limit-to-age t]
2383          ["Extra..." gnus-summary-limit-to-extra t]
2384          ["Score..." gnus-summary-limit-to-score t]
2385          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2386          ["Unread" gnus-summary-limit-to-unread t]
2387          ["Unseen" gnus-summary-limit-to-unseen t]
2388          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2389          ["Next articles" gnus-summary-limit-to-articles t]
2390          ["Pop limit" gnus-summary-pop-limit t]
2391          ["Show dormant" gnus-summary-limit-include-dormant t]
2392          ["Hide childless dormant"
2393           gnus-summary-limit-exclude-childless-dormant t]
2394          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2395          ["Hide marked" gnus-summary-limit-exclude-marks t]
2396          ["Show expunged" gnus-summary-limit-include-expunged t])
2397         ("Process Mark"
2398          ["Set mark" gnus-summary-mark-as-processable t]
2399          ["Remove mark" gnus-summary-unmark-as-processable t]
2400          ["Remove all marks" gnus-summary-unmark-all-processable t]
2401          ["Mark above" gnus-uu-mark-over t]
2402          ["Mark series" gnus-uu-mark-series t]
2403          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2404          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2405          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2406          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2407          ["Mark all" gnus-uu-mark-all t]
2408          ["Mark buffer" gnus-uu-mark-buffer t]
2409          ["Mark sparse" gnus-uu-mark-sparse t]
2410          ["Mark thread" gnus-uu-mark-thread t]
2411          ["Unmark thread" gnus-uu-unmark-thread t]
2412          ("Process Mark Sets"
2413           ["Kill" gnus-summary-kill-process-mark t]
2414           ["Yank" gnus-summary-yank-process-mark
2415            gnus-newsgroup-process-stack]
2416           ["Save" gnus-summary-save-process-mark t]
2417           ["Run command on marked..." gnus-summary-universal-argument t]))
2418         ("Scroll article"
2419          ["Page forward" gnus-summary-next-page
2420           ,@(if (featurep 'xemacs) '(t)
2421               '(:help "Show next page of article"))]
2422          ["Page backward" gnus-summary-prev-page
2423           ,@(if (featurep 'xemacs) '(t)
2424               '(:help "Show previous page of article"))]
2425          ["Line forward" gnus-summary-scroll-up t])
2426         ("Move"
2427          ["Next unread article" gnus-summary-next-unread-article t]
2428          ["Previous unread article" gnus-summary-prev-unread-article t]
2429          ["Next article" gnus-summary-next-article t]
2430          ["Previous article" gnus-summary-prev-article t]
2431          ["Next unread subject" gnus-summary-next-unread-subject t]
2432          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2433          ["Next article same subject" gnus-summary-next-same-subject t]
2434          ["Previous article same subject" gnus-summary-prev-same-subject t]
2435          ["First unread article" gnus-summary-first-unread-article t]
2436          ["Best unread article" gnus-summary-best-unread-article t]
2437          ["Go to subject number..." gnus-summary-goto-subject t]
2438          ["Go to article number..." gnus-summary-goto-article t]
2439          ["Go to the last article" gnus-summary-goto-last-article t]
2440          ["Pop article off history" gnus-summary-pop-article t])
2441         ("Sort"
2442          ["Sort by number" gnus-summary-sort-by-number t]
2443          ["Sort by author" gnus-summary-sort-by-author t]
2444          ["Sort by subject" gnus-summary-sort-by-subject t]
2445          ["Sort by date" gnus-summary-sort-by-date t]
2446          ["Sort by score" gnus-summary-sort-by-score t]
2447          ["Sort by lines" gnus-summary-sort-by-lines t]
2448          ["Sort by characters" gnus-summary-sort-by-chars t]
2449          ["Randomize" gnus-summary-sort-by-random t]
2450          ["Original sort" gnus-summary-sort-by-original t])
2451         ("Help"
2452          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2453          ["Describe group" gnus-summary-describe-group t]
2454          ["Fetch charter" gnus-group-fetch-charter
2455           ,@(if (featurep 'xemacs) nil
2456               '(:help "Display the charter of the current group"))]
2457          ["Fetch control message" gnus-group-fetch-control
2458           ,@(if (featurep 'xemacs) nil
2459               '(:help "Display the archived control message for the current group"))]
2460          ["Read manual" gnus-info-find-node t])
2461         ("Modes"
2462          ["Pick and read" gnus-pick-mode t]
2463          ["Binary" gnus-binary-mode t])
2464         ("Regeneration"
2465          ["Regenerate" gnus-summary-prepare t]
2466          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2467          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2468          ["Toggle threading" gnus-summary-toggle-threads t])
2469         ["See old articles" gnus-summary-insert-old-articles t]
2470         ["See new articles" gnus-summary-insert-new-articles t]
2471         ["Filter articles..." gnus-summary-execute-command t]
2472         ["Run command on articles..." gnus-summary-universal-argument t]
2473         ["Search articles forward..." gnus-summary-search-article-forward t]
2474         ["Search articles backward..." gnus-summary-search-article-backward t]
2475         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2476         ["Expand window" gnus-summary-expand-window t]
2477         ["Expire expirable articles" gnus-summary-expire-articles
2478          (gnus-check-backend-function
2479           'request-expire-articles gnus-newsgroup-name)]
2480         ["Edit local kill file" gnus-summary-edit-local-kill t]
2481         ["Edit main kill file" gnus-summary-edit-global-kill t]
2482         ["Edit group parameters" gnus-summary-edit-parameters t]
2483         ["Customize group parameters" gnus-summary-customize-parameters t]
2484         ["Send a bug report" gnus-bug t]
2485         ("Exit"
2486          ["Catchup and exit" gnus-summary-catchup-and-exit
2487           ,@(if (featurep 'xemacs) '(t)
2488               '(:help "Mark unread articles in this group as read, then exit"))]
2489          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2490          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2491          ["Exit group" gnus-summary-exit
2492           ,@(if (featurep 'xemacs) '(t)
2493               '(:help "Exit current group, return to group selection mode"))]
2494          ["Exit group without updating" gnus-summary-exit-no-update t]
2495          ["Exit and goto next group" gnus-summary-next-group t]
2496          ["Exit and goto prev group" gnus-summary-prev-group t]
2497          ["Reselect group" gnus-summary-reselect-current-group t]
2498          ["Rescan group" gnus-summary-rescan-group t]
2499          ["Update dribble" gnus-summary-save-newsrc t])))
2500
2501     (gnus-run-hooks 'gnus-summary-menu-hook)))
2502
2503 (defvar gnus-summary-tool-bar-map nil)
2504
2505 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2506 (defun gnus-summary-make-tool-bar ()
2507   (if (and (fboundp 'tool-bar-add-item-from-menu)
2508            (default-value 'tool-bar-mode)
2509            (not gnus-summary-tool-bar-map))
2510       (setq gnus-summary-tool-bar-map
2511             (let ((tool-bar-map (make-sparse-keymap))
2512                   (load-path (mm-image-load-path)))
2513               (tool-bar-add-item-from-menu
2514                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2515               (tool-bar-add-item-from-menu
2516                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2517               (tool-bar-add-item-from-menu
2518                'gnus-summary-post-news "post" gnus-summary-mode-map)
2519               (tool-bar-add-item-from-menu
2520                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2521               (tool-bar-add-item-from-menu
2522                'gnus-summary-followup "followup" gnus-summary-mode-map)
2523               (tool-bar-add-item-from-menu
2524                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2525               (tool-bar-add-item-from-menu
2526                'gnus-summary-reply "reply" gnus-summary-mode-map)
2527               (tool-bar-add-item-from-menu
2528                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2529               (tool-bar-add-item-from-menu
2530                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2531               (tool-bar-add-item-from-menu
2532                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2533               (tool-bar-add-item-from-menu
2534                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2535               (tool-bar-add-item-from-menu
2536                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2537               (tool-bar-add-item-from-menu
2538                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2539               (tool-bar-add-item-from-menu
2540                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2541               (tool-bar-add-item-from-menu
2542                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2543               tool-bar-map)))
2544   (if gnus-summary-tool-bar-map
2545       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2546
2547 (defun gnus-score-set-default (var value)
2548   "A version of set that updates the GNU Emacs menu-bar."
2549   (set var value)
2550   ;; It is the message that forces the active status to be updated.
2551   (message ""))
2552
2553 (defun gnus-make-score-map (type)
2554   "Make a summary score map of type TYPE."
2555   (if t
2556       nil
2557     (let ((headers '(("author" "from" string)
2558                      ("subject" "subject" string)
2559                      ("article body" "body" string)
2560                      ("article head" "head" string)
2561                      ("xref" "xref" string)
2562                      ("extra header" "extra" string)
2563                      ("lines" "lines" number)
2564                      ("followups to author" "followup" string)))
2565           (types '((number ("less than" <)
2566                            ("greater than" >)
2567                            ("equal" =))
2568                    (string ("substring" s)
2569                            ("exact string" e)
2570                            ("fuzzy string" f)
2571                            ("regexp" r))))
2572           (perms '(("temporary" (current-time-string))
2573                    ("permanent" nil)
2574                    ("immediate" now)))
2575           header)
2576       (list
2577        (apply
2578         'nconc
2579         (list
2580          (if (eq type 'lower)
2581              "Lower score"
2582            "Increase score"))
2583         (let (outh)
2584           (while headers
2585             (setq header (car headers))
2586             (setq outh
2587                   (cons
2588                    (apply
2589                     'nconc
2590                     (list (car header))
2591                     (let ((ts (cdr (assoc (nth 2 header) types)))
2592                           outt)
2593                       (while ts
2594                         (setq outt
2595                               (cons
2596                                (apply
2597                                 'nconc
2598                                 (list (caar ts))
2599                                 (let ((ps perms)
2600                                       outp)
2601                                   (while ps
2602                                     (setq outp
2603                                           (cons
2604                                            (vector
2605                                             (caar ps)
2606                                             (list
2607                                              'gnus-summary-score-entry
2608                                              (nth 1 header)
2609                                              (if (or (string= (nth 1 header)
2610                                                               "head")
2611                                                      (string= (nth 1 header)
2612                                                               "body"))
2613                                                  ""
2614                                                (list 'gnus-summary-header
2615                                                      (nth 1 header)))
2616                                              (list 'quote (nth 1 (car ts)))
2617                                              (list 'gnus-score-delta-default
2618                                                    nil)
2619                                              (nth 1 (car ps))
2620                                              t)
2621                                             t)
2622                                            outp))
2623                                     (setq ps (cdr ps)))
2624                                   (list (nreverse outp))))
2625                                outt))
2626                         (setq ts (cdr ts)))
2627                       (list (nreverse outt))))
2628                    outh))
2629             (setq headers (cdr headers)))
2630           (list (nreverse outh))))))))
2631
2632 \f
2633
2634 (defun gnus-summary-mode (&optional group)
2635   "Major mode for reading articles.
2636
2637 All normal editing commands are switched off.
2638 \\<gnus-summary-mode-map>
2639 Each line in this buffer represents one article.  To read an
2640 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2641 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2642 respectively.
2643
2644 You can also post articles and send mail from this buffer.  To
2645 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2646 of an article, type `\\[gnus-summary-reply]'.
2647
2648 There are approx. one gazillion commands you can execute in this
2649 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2650
2651 The following commands are available:
2652
2653 \\{gnus-summary-mode-map}"
2654   (interactive)
2655   (kill-all-local-variables)
2656   (when (gnus-visual-p 'summary-menu 'menu)
2657     (gnus-summary-make-menu-bar)
2658     (gnus-summary-make-tool-bar))
2659   (gnus-summary-make-local-variables)
2660   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2661     (gnus-summary-make-local-variables))
2662   (gnus-make-thread-indent-array)
2663   (gnus-simplify-mode-line)
2664   (setq major-mode 'gnus-summary-mode)
2665   (setq mode-name "Summary")
2666   (make-local-variable 'minor-mode-alist)
2667   (use-local-map gnus-summary-mode-map)
2668   (buffer-disable-undo)
2669   (setq buffer-read-only t)             ;Disable modification
2670   (setq truncate-lines t)
2671   (setq selective-display t)
2672   (setq selective-display-ellipses t)   ;Display `...'
2673   (gnus-summary-set-display-table)
2674   (gnus-set-default-directory)
2675   (setq gnus-newsgroup-name group)
2676   (make-local-variable 'gnus-summary-line-format)
2677   (make-local-variable 'gnus-summary-line-format-spec)
2678   (make-local-variable 'gnus-summary-dummy-line-format)
2679   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2680   (make-local-variable 'gnus-summary-mark-positions)
2681   (gnus-make-local-hook 'pre-command-hook)
2682   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2683   (gnus-run-hooks 'gnus-summary-mode-hook)
2684   (turn-on-gnus-mailing-list-mode)
2685   (mm-enable-multibyte)
2686   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2687   (gnus-update-summary-mark-positions))
2688
2689 (defun gnus-summary-make-local-variables ()
2690   "Make all the local summary buffer variables."
2691   (let (global)
2692     (dolist (local gnus-summary-local-variables)
2693       (if (consp local)
2694           (progn
2695             (if (eq (cdr local) 'global)
2696                 ;; Copy the global value of the variable.
2697                 (setq global (symbol-value (car local)))
2698               ;; Use the value from the list.
2699               (setq global (eval (cdr local))))
2700             (set (make-local-variable (car local)) global))
2701         ;; Simple nil-valued local variable.
2702         (set (make-local-variable local) nil)))))
2703
2704 (defun gnus-summary-clear-local-variables ()
2705   (let ((locals gnus-summary-local-variables))
2706     (while locals
2707       (if (consp (car locals))
2708           (and (vectorp (caar locals))
2709                (set (caar locals) nil))
2710         (and (vectorp (car locals))
2711              (set (car locals) nil)))
2712       (setq locals (cdr locals)))))
2713
2714 ;; Summary data functions.
2715
2716 (defmacro gnus-data-number (data)
2717   `(car ,data))
2718
2719 (defmacro gnus-data-set-number (data number)
2720   `(setcar ,data ,number))
2721
2722 (defmacro gnus-data-mark (data)
2723   `(nth 1 ,data))
2724
2725 (defmacro gnus-data-set-mark (data mark)
2726   `(setcar (nthcdr 1 ,data) ,mark))
2727
2728 (defmacro gnus-data-pos (data)
2729   `(nth 2 ,data))
2730
2731 (defmacro gnus-data-set-pos (data pos)
2732   `(setcar (nthcdr 2 ,data) ,pos))
2733
2734 (defmacro gnus-data-header (data)
2735   `(nth 3 ,data))
2736
2737 (defmacro gnus-data-set-header (data header)
2738   `(setf (nth 3 ,data) ,header))
2739
2740 (defmacro gnus-data-level (data)
2741   `(nth 4 ,data))
2742
2743 (defmacro gnus-data-unread-p (data)
2744   `(= (nth 1 ,data) gnus-unread-mark))
2745
2746 (defmacro gnus-data-read-p (data)
2747   `(/= (nth 1 ,data) gnus-unread-mark))
2748
2749 (defmacro gnus-data-pseudo-p (data)
2750   `(consp (nth 3 ,data)))
2751
2752 (defmacro gnus-data-find (number)
2753   `(assq ,number gnus-newsgroup-data))
2754
2755 (defmacro gnus-data-find-list (number &optional data)
2756   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2757      (memq (assq ,number bdata)
2758            bdata)))
2759
2760 (defmacro gnus-data-make (number mark pos header level)
2761   `(list ,number ,mark ,pos ,header ,level))
2762
2763 (defun gnus-data-enter (after-article number mark pos header level offset)
2764   (let ((data (gnus-data-find-list after-article)))
2765     (unless data
2766       (error "No such article: %d" after-article))
2767     (setcdr data (cons (gnus-data-make number mark pos header level)
2768                        (cdr data)))
2769     (setq gnus-newsgroup-data-reverse nil)
2770     (gnus-data-update-list (cddr data) offset)))
2771
2772 (defun gnus-data-enter-list (after-article list &optional offset)
2773   (when list
2774     (let ((data (and after-article (gnus-data-find-list after-article)))
2775           (ilist list))
2776       (if (not (or data
2777                    after-article))
2778           (let ((odata gnus-newsgroup-data))
2779             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2780             (when offset
2781               (gnus-data-update-list odata offset)))
2782       ;; Find the last element in the list to be spliced into the main
2783         ;; list.
2784         (while (cdr list)
2785           (setq list (cdr list)))
2786         (if (not data)
2787             (progn
2788               (setcdr list gnus-newsgroup-data)
2789               (setq gnus-newsgroup-data ilist)
2790               (when offset
2791                 (gnus-data-update-list (cdr list) offset)))
2792           (setcdr list (cdr data))
2793           (setcdr data ilist)
2794           (when offset
2795             (gnus-data-update-list (cdr list) offset))))
2796       (setq gnus-newsgroup-data-reverse nil))))
2797
2798 (defun gnus-data-remove (article &optional offset)
2799   (let ((data gnus-newsgroup-data))
2800     (if (= (gnus-data-number (car data)) article)
2801         (progn
2802           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2803                 gnus-newsgroup-data-reverse nil)
2804           (when offset
2805             (gnus-data-update-list gnus-newsgroup-data offset)))
2806       (while (cdr data)
2807         (when (= (gnus-data-number (cadr data)) article)
2808           (setcdr data (cddr data))
2809           (when offset
2810             (gnus-data-update-list (cdr data) offset))
2811           (setq data nil
2812                 gnus-newsgroup-data-reverse nil))
2813         (setq data (cdr data))))))
2814
2815 (defmacro gnus-data-list (backward)
2816   `(if ,backward
2817        (or gnus-newsgroup-data-reverse
2818            (setq gnus-newsgroup-data-reverse
2819                  (reverse gnus-newsgroup-data)))
2820      gnus-newsgroup-data))
2821
2822 (defun gnus-data-update-list (data offset)
2823   "Add OFFSET to the POS of all data entries in DATA."
2824   (setq gnus-newsgroup-data-reverse nil)
2825   (while data
2826     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2827     (setq data (cdr data))))
2828
2829 (defun gnus-summary-article-pseudo-p (article)
2830   "Say whether this article is a pseudo article or not."
2831   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2832
2833 (defmacro gnus-summary-article-sparse-p (article)
2834   "Say whether this article is a sparse article or not."
2835   `(memq ,article gnus-newsgroup-sparse))
2836
2837 (defmacro gnus-summary-article-ancient-p (article)
2838   "Say whether this article is a sparse article or not."
2839   `(memq ,article gnus-newsgroup-ancient))
2840
2841 (defun gnus-article-parent-p (number)
2842   "Say whether this article is a parent or not."
2843   (let ((data (gnus-data-find-list number)))
2844     (and (cdr data)              ; There has to be an article after...
2845          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2846             (gnus-data-level (nth 1 data))))))
2847
2848 (defun gnus-article-children (number)
2849   "Return a list of all children to NUMBER."
2850   (let* ((data (gnus-data-find-list number))
2851          (level (gnus-data-level (car data)))
2852          children)
2853     (setq data (cdr data))
2854     (while (and data
2855                 (= (gnus-data-level (car data)) (1+ level)))
2856       (push (gnus-data-number (car data)) children)
2857       (setq data (cdr data)))
2858     children))
2859
2860 (defmacro gnus-summary-skip-intangible ()
2861   "If the current article is intangible, then jump to a different article."
2862   '(let ((to (get-text-property (point) 'gnus-intangible)))
2863      (and to (gnus-summary-goto-subject to))))
2864
2865 (defmacro gnus-summary-article-intangible-p ()
2866   "Say whether this article is intangible or not."
2867   '(get-text-property (point) 'gnus-intangible))
2868
2869 (defun gnus-article-read-p (article)
2870   "Say whether ARTICLE is read or not."
2871   (not (or (memq article gnus-newsgroup-marked)
2872            (memq article gnus-newsgroup-spam-marked)
2873            (memq article gnus-newsgroup-unreads)
2874            (memq article gnus-newsgroup-unselected)
2875            (memq article gnus-newsgroup-dormant))))
2876
2877 ;; Some summary mode macros.
2878
2879 (defmacro gnus-summary-article-number ()
2880   "The article number of the article on the current line.
2881 If there isn't an article number here, then we return the current
2882 article number."
2883   '(progn
2884      (gnus-summary-skip-intangible)
2885      (or (get-text-property (point) 'gnus-number)
2886          (gnus-summary-last-subject))))
2887
2888 (defmacro gnus-summary-article-header (&optional number)
2889   "Return the header of article NUMBER."
2890   `(gnus-data-header (gnus-data-find
2891                       ,(or number '(gnus-summary-article-number)))))
2892
2893 (defmacro gnus-summary-thread-level (&optional number)
2894   "Return the level of thread that starts with article NUMBER."
2895   `(if (and (eq gnus-summary-make-false-root 'dummy)
2896             (get-text-property (point) 'gnus-intangible))
2897        0
2898      (gnus-data-level (gnus-data-find
2899                        ,(or number '(gnus-summary-article-number))))))
2900
2901 (defmacro gnus-summary-article-mark (&optional number)
2902   "Return the mark of article NUMBER."
2903   `(gnus-data-mark (gnus-data-find
2904                     ,(or number '(gnus-summary-article-number)))))
2905
2906 (defmacro gnus-summary-article-pos (&optional number)
2907   "Return the position of the line of article NUMBER."
2908   `(gnus-data-pos (gnus-data-find
2909                    ,(or number '(gnus-summary-article-number)))))
2910
2911 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2912 (defmacro gnus-summary-article-subject (&optional number)
2913   "Return current subject string or nil if nothing."
2914   `(let ((headers
2915           ,(if number
2916                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2917              '(gnus-data-header (assq (gnus-summary-article-number)
2918                                       gnus-newsgroup-data)))))
2919      (and headers
2920           (vectorp headers)
2921           (mail-header-subject headers))))
2922
2923 (defmacro gnus-summary-article-score (&optional number)
2924   "Return current article score."
2925   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2926                   gnus-newsgroup-scored))
2927        gnus-summary-default-score 0))
2928
2929 (defun gnus-summary-article-children (&optional number)
2930   "Return a list of article numbers that are children of article NUMBER."
2931   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2932          (level (gnus-data-level (car data)))
2933          l children)
2934     (while (and (setq data (cdr data))
2935                 (> (setq l (gnus-data-level (car data))) level))
2936       (and (= (1+ level) l)
2937            (push (gnus-data-number (car data))
2938                  children)))
2939     (nreverse children)))
2940
2941 (defun gnus-summary-article-parent (&optional number)
2942   "Return the article number of the parent of article NUMBER."
2943   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2944                                     (gnus-data-list t)))
2945          (level (gnus-data-level (car data))))
2946     (if (zerop level)
2947         ()                              ; This is a root.
2948       ;; We search until we find an article with a level less than
2949       ;; this one.  That function has to be the parent.
2950       (while (and (setq data (cdr data))
2951                   (not (< (gnus-data-level (car data)) level))))
2952       (and data (gnus-data-number (car data))))))
2953
2954 (defun gnus-unread-mark-p (mark)
2955   "Say whether MARK is the unread mark."
2956   (= mark gnus-unread-mark))
2957
2958 (defun gnus-read-mark-p (mark)
2959   "Say whether MARK is one of the marks that mark as read.
2960 This is all marks except unread, ticked, dormant, and expirable."
2961   (not (or (= mark gnus-unread-mark)
2962            (= mark gnus-ticked-mark)
2963            (= mark gnus-spam-mark)
2964            (= mark gnus-dormant-mark)
2965            (= mark gnus-expirable-mark))))
2966
2967 (defmacro gnus-article-mark (number)
2968   "Return the MARK of article NUMBER.
2969 This macro should only be used when computing the mark the \"first\"
2970 time; i.e., when generating the summary lines.  After that,
2971 `gnus-summary-article-mark' should be used to examine the
2972 marks of articles."
2973   `(cond
2974     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2975     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2976     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2977     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2978     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2979     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2980     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2981     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2982            gnus-ancient-mark))))
2983
2984 ;; Saving hidden threads.
2985
2986 (defmacro gnus-save-hidden-threads (&rest forms)
2987   "Save hidden threads, eval FORMS, and restore the hidden threads."
2988   (let ((config (make-symbol "config")))
2989     `(let ((,config (gnus-hidden-threads-configuration)))
2990        (unwind-protect
2991            (save-excursion
2992              ,@forms)
2993          (gnus-restore-hidden-threads-configuration ,config)))))
2994 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2995 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2996
2997 (defun gnus-data-compute-positions ()
2998   "Compute the positions of all articles."
2999   (setq gnus-newsgroup-data-reverse nil)
3000   (let ((data gnus-newsgroup-data))
3001     (save-excursion
3002       (gnus-save-hidden-threads
3003         (gnus-summary-show-all-threads)
3004         (goto-char (point-min))
3005         (while data
3006           (while (get-text-property (point) 'gnus-intangible)
3007             (forward-line 1))
3008           (gnus-data-set-pos (car data) (+ (point) 3))
3009           (setq data (cdr data))
3010           (forward-line 1))))))
3011
3012 (defun gnus-hidden-threads-configuration ()
3013   "Return the current hidden threads configuration."
3014   (save-excursion
3015     (let (config)
3016       (goto-char (point-min))
3017       (while (search-forward "\r" nil t)
3018         (push (1- (point)) config))
3019       config)))
3020
3021 (defun gnus-restore-hidden-threads-configuration (config)
3022   "Restore hidden threads configuration from CONFIG."
3023   (save-excursion
3024     (let (point buffer-read-only)
3025       (while (setq point (pop config))
3026         (when (and (< point (point-max))
3027                    (goto-char point)
3028                    (eq (char-after) ?\n))
3029           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3030
3031 ;; Various summary mode internalish functions.
3032
3033 (defun gnus-mouse-pick-article (e)
3034   (interactive "e")
3035   (mouse-set-point e)
3036   (gnus-summary-next-page nil t))
3037
3038 (defun gnus-summary-set-display-table ()
3039   "Change the display table.
3040 Odd characters have a tendency to mess
3041 up nicely formatted displays - we make all possible glyphs
3042 display only a single character."
3043
3044   ;; We start from the standard display table, if any.
3045   (let ((table (or (copy-sequence standard-display-table)
3046                    (make-display-table)))
3047         (i 32))
3048     ;; Nix out all the control chars...
3049     (while (>= (setq i (1- i)) 0)
3050       (aset table i [??]))
3051    ;; ... but not newline and cr, of course.  (cr is necessary for the
3052     ;; selective display).
3053     (aset table ?\n nil)
3054     (aset table ?\r nil)
3055     ;; We keep TAB as well.
3056     (aset table ?\t nil)
3057     ;; We nix out any glyphs over 126 that are not set already.
3058     (let ((i 256))
3059       (while (>= (setq i (1- i)) 127)
3060         ;; Only modify if the entry is nil.
3061         (unless (aref table i)
3062           (aset table i [??]))))
3063     (setq buffer-display-table table)))
3064
3065 (defun gnus-summary-set-article-display-arrow (pos)
3066   "Update the overlay arrow to point to line at position POS."
3067   (when (and gnus-summary-display-arrow
3068              (boundp 'overlay-arrow-position)
3069              (boundp 'overlay-arrow-string))
3070     (save-excursion
3071       (goto-char pos)
3072       (beginning-of-line)
3073       (unless overlay-arrow-position
3074         (setq overlay-arrow-position (make-marker)))
3075       (setq overlay-arrow-string "=>"
3076             overlay-arrow-position (set-marker overlay-arrow-position
3077                                                (point)
3078                                                (current-buffer))))))
3079
3080 (defun gnus-summary-setup-buffer (group)
3081   "Initialize summary buffer."
3082   (let ((buffer (gnus-summary-buffer-name group))
3083         (dead-name (concat "*Dead Summary "
3084                            (gnus-group-decoded-name group) "*")))
3085     ;; If a dead summary buffer exists, we kill it.
3086     (when (gnus-buffer-live-p dead-name)
3087       (gnus-kill-buffer dead-name))
3088     (if (get-buffer buffer)
3089         (progn
3090           (set-buffer buffer)
3091           (setq gnus-summary-buffer (current-buffer))
3092           (not gnus-newsgroup-prepared))
3093       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3094       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3095       (gnus-summary-mode group)
3096       (when gnus-carpal
3097         (gnus-carpal-setup-buffer 'summary))
3098       (unless gnus-single-article-buffer
3099         (make-local-variable 'gnus-article-buffer)
3100         (make-local-variable 'gnus-article-current)
3101         (make-local-variable 'gnus-original-article-buffer))
3102       (setq gnus-newsgroup-name group)
3103       ;; Set any local variables in the group parameters.
3104       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3105       t)))
3106
3107 (defun gnus-set-global-variables ()
3108   "Set the global equivalents of the buffer-local variables.
3109 They are set to the latest values they had.  These reflect the summary
3110 buffer that was in action when the last article was fetched."
3111   (when (eq major-mode 'gnus-summary-mode)
3112     (setq gnus-summary-buffer (current-buffer))
3113     (let ((name gnus-newsgroup-name)
3114           (marked gnus-newsgroup-marked)
3115           (spam gnus-newsgroup-spam-marked)
3116           (unread gnus-newsgroup-unreads)
3117           (headers gnus-current-headers)
3118           (data gnus-newsgroup-data)
3119           (summary gnus-summary-buffer)
3120           (article-buffer gnus-article-buffer)
3121           (original gnus-original-article-buffer)
3122           (gac gnus-article-current)
3123           (reffed gnus-reffed-article-number)
3124           (score-file gnus-current-score-file)
3125           (default-charset gnus-newsgroup-charset)
3126           vlist)
3127       (let ((locals gnus-newsgroup-variables))
3128         (while locals
3129           (if (consp (car locals))
3130               (push (eval (caar locals)) vlist)
3131             (push (eval (car locals)) vlist))
3132           (setq locals (cdr locals)))
3133         (setq vlist (nreverse vlist)))
3134       (save-excursion
3135         (set-buffer gnus-group-buffer)
3136         (setq gnus-newsgroup-name name
3137               gnus-newsgroup-marked marked
3138               gnus-newsgroup-spam-marked spam
3139               gnus-newsgroup-unreads unread
3140               gnus-current-headers headers
3141               gnus-newsgroup-data data
3142               gnus-article-current gac
3143               gnus-summary-buffer summary
3144               gnus-article-buffer article-buffer
3145               gnus-original-article-buffer original
3146               gnus-reffed-article-number reffed
3147               gnus-current-score-file score-file
3148               gnus-newsgroup-charset default-charset)
3149         (let ((locals gnus-newsgroup-variables))
3150           (while locals
3151             (if (consp (car locals))
3152                 (set (caar locals) (pop vlist))
3153               (set (car locals) (pop vlist)))
3154             (setq locals (cdr locals))))
3155         ;; The article buffer also has local variables.
3156         (when (gnus-buffer-live-p gnus-article-buffer)
3157           (set-buffer gnus-article-buffer)
3158           (setq gnus-summary-buffer summary))))))
3159
3160 (defun gnus-summary-article-unread-p (article)
3161   "Say whether ARTICLE is unread or not."
3162   (memq article gnus-newsgroup-unreads))
3163
3164 (defun gnus-summary-first-article-p (&optional article)
3165   "Return whether ARTICLE is the first article in the buffer."
3166   (if (not (setq article (or article (gnus-summary-article-number))))
3167       nil
3168     (eq article (caar gnus-newsgroup-data))))
3169
3170 (defun gnus-summary-last-article-p (&optional article)
3171   "Return whether ARTICLE is the last article in the buffer."
3172   (if (not (setq article (or article (gnus-summary-article-number))))
3173       ;; All non-existent numbers are the last article.  :-)
3174       t
3175     (not (cdr (gnus-data-find-list article)))))
3176
3177 (defun gnus-make-thread-indent-array ()
3178   (let ((n 200))
3179     (unless (and gnus-thread-indent-array
3180                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3181       (setq gnus-thread-indent-array (make-vector 201 "")
3182             gnus-thread-indent-array-level gnus-thread-indent-level)
3183       (while (>= n 0)
3184         (aset gnus-thread-indent-array n
3185               (make-string (* n gnus-thread-indent-level) ? ))
3186         (setq n (1- n))))))
3187
3188 (defun gnus-update-summary-mark-positions ()
3189   "Compute where the summary marks are to go."
3190   (save-excursion
3191     (when (gnus-buffer-exists-p gnus-summary-buffer)
3192       (set-buffer gnus-summary-buffer))
3193     (let ((gnus-replied-mark 129)
3194           (gnus-score-below-mark 130)
3195           (gnus-score-over-mark 130)
3196           (gnus-undownloaded-mark 131)
3197           (spec gnus-summary-line-format-spec)
3198           gnus-visual pos)
3199       (save-excursion
3200         (gnus-set-work-buffer)
3201         (let ((gnus-summary-line-format-spec spec)
3202               (gnus-newsgroup-downloadable '(0)))
3203           (gnus-summary-insert-line
3204            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3205            0 nil t 128 t nil "" nil 1)
3206           (goto-char (point-min))
3207           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3208                                              (- (point) (point-min) 1)))))
3209           (goto-char (point-min))
3210           (push (cons 'replied (and (search-forward "\201" nil t)
3211                                     (- (point) (point-min) 1)))
3212                 pos)
3213           (goto-char (point-min))
3214           (push (cons 'score (and (search-forward "\202" nil t)
3215                                   (- (point) (point-min) 1)))
3216                 pos)
3217           (goto-char (point-min))
3218           (push (cons 'download
3219                       (and (search-forward "\203" nil t)
3220                            (- (point) (point-min) 1)))
3221                 pos)))
3222       (setq gnus-summary-mark-positions pos))))
3223
3224 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3225   "Insert a dummy root in the summary buffer."
3226   (beginning-of-line)
3227   (gnus-add-text-properties
3228    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3229    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3230
3231 (defun gnus-summary-extract-address-component (from)
3232   (or (car (funcall gnus-extract-address-components from))
3233       from))
3234
3235 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3236   (let ((mail-parse-charset gnus-newsgroup-charset)
3237         ; Is it really necessary to do this next part for each summary line?
3238         ; Luckily, doesn't seem to slow things down much.
3239         (mail-parse-ignored-charsets
3240          (save-excursion (set-buffer gnus-summary-buffer)
3241                          gnus-newsgroup-ignored-charsets)))
3242     (or
3243      (and gnus-ignored-from-addresses
3244           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3245           (let ((extra-headers (mail-header-extra header))
3246                 to
3247                 newsgroups)
3248             (cond
3249              ((setq to (cdr (assq 'To extra-headers)))
3250               (concat "-> "
3251                       (inline
3252                         (gnus-summary-extract-address-component
3253                          (funcall gnus-decode-encoded-word-function to)))))
3254              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3255               (concat "=> " newsgroups)))))
3256      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3257
3258 (defun gnus-summary-insert-line (gnus-tmp-header
3259                                  gnus-tmp-level gnus-tmp-current
3260                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3261                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3262                                  &optional gnus-tmp-dummy gnus-tmp-score
3263                                  gnus-tmp-process)
3264   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3265          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3266          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3267          (gnus-tmp-score-char
3268           (if (or (null gnus-summary-default-score)
3269                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3270                       gnus-summary-zcore-fuzz))
3271               ?                         ;Whitespace
3272             (if (< gnus-tmp-score gnus-summary-default-score)
3273                 gnus-score-below-mark gnus-score-over-mark)))
3274          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3275          (gnus-tmp-replied
3276           (cond (gnus-tmp-process gnus-process-mark)
3277                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3278                  gnus-cached-mark)
3279                 (gnus-tmp-replied gnus-replied-mark)
3280                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3281                  gnus-forwarded-mark)
3282                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3283                  gnus-saved-mark)
3284                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3285                  gnus-recent-mark)
3286                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3287                  gnus-unseen-mark)
3288                 (t gnus-no-mark)))
3289          (gnus-tmp-downloaded
3290           (cond (undownloaded 
3291                  gnus-undownloaded-mark)
3292                 (gnus-newsgroup-agentized
3293                  gnus-downloaded-mark)
3294                 (t
3295                  gnus-no-mark)))
3296          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3297          (gnus-tmp-name
3298           (cond
3299            ((string-match "<[^>]+> *$" gnus-tmp-from)
3300             (let ((beg (match-beginning 0)))
3301               (or (and (string-match "^\".+\"" gnus-tmp-from)
3302                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3303                   (substring gnus-tmp-from 0 beg))))
3304            ((string-match "(.+)" gnus-tmp-from)
3305             (substring gnus-tmp-from
3306                        (1+ (match-beginning 0)) (1- (match-end 0))))
3307            (t gnus-tmp-from)))
3308          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3309          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3310          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3311          (buffer-read-only nil))
3312     (when (string= gnus-tmp-name "")
3313       (setq gnus-tmp-name gnus-tmp-from))
3314     (unless (numberp gnus-tmp-lines)
3315       (setq gnus-tmp-lines -1))
3316     (if (= gnus-tmp-lines -1)
3317         (setq gnus-tmp-lines "?")
3318       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines))) 
3319       (gnus-put-text-property
3320      (point)
3321      (progn (eval gnus-summary-line-format-spec) (point))
3322        'gnus-number gnus-tmp-number)
3323     (when (gnus-visual-p 'summary-highlight 'highlight)
3324       (forward-line -1)
3325       (gnus-run-hooks 'gnus-summary-update-hook)
3326       (forward-line 1))))
3327
3328 (defun gnus-summary-update-line (&optional dont-update)
3329   "Update summary line after change."
3330   (when (and gnus-summary-default-score
3331              (not gnus-summary-inhibit-highlight))
3332     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3333            (article (gnus-summary-article-number))
3334            (score (gnus-summary-article-score article)))
3335       (unless dont-update
3336         (if (and gnus-summary-mark-below
3337                  (< (gnus-summary-article-score)
3338                     gnus-summary-mark-below))
3339             ;; This article has a low score, so we mark it as read.
3340             (when (memq article gnus-newsgroup-unreads)
3341               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3342           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3343             ;; This article was previously marked as read on account
3344             ;; of a low score, but now it has risen, so we mark it as
3345             ;; unread.
3346             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3347         (gnus-summary-update-mark
3348          (if (or (null gnus-summary-default-score)
3349                  (<= (abs (- score gnus-summary-default-score))
3350                      gnus-summary-zcore-fuzz))
3351              ?                          ;Whitespace
3352            (if (< score gnus-summary-default-score)
3353                gnus-score-below-mark gnus-score-over-mark))
3354          'score))
3355       ;; Do visual highlighting.
3356       (when (gnus-visual-p 'summary-highlight 'highlight)
3357         (gnus-run-hooks 'gnus-summary-update-hook)))))
3358
3359 (defvar gnus-tmp-new-adopts nil)
3360
3361 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3362   "Return the number of articles in THREAD.
3363 This may be 0 in some cases -- if none of the articles in
3364 the thread are to be displayed."
3365   (let* ((number
3366          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3367           (cond
3368            ((not (listp thread))
3369             1)
3370            ((and (consp thread) (cdr thread))
3371             (apply
3372              '+ 1 (mapcar
3373                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3374            ((null thread)
3375             1)
3376            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3377             1)
3378            (t 0))))
3379     (when (and level (zerop level) gnus-tmp-new-adopts)
3380       (incf number
3381             (apply '+ (mapcar
3382                        'gnus-summary-number-of-articles-in-thread
3383                        gnus-tmp-new-adopts))))
3384     (if char
3385         (if (> number 1) gnus-not-empty-thread-mark
3386           gnus-empty-thread-mark)
3387       number)))
3388
3389 (defsubst gnus-summary-line-message-size (head)
3390   "Return pretty-printed version of message size.
3391 This function is intended to be used in
3392 `gnus-summary-line-format-alist'."
3393   (let ((c (or (mail-header-chars head) -1)))
3394     (cond ((< c 0) "n/a")               ; chars not available
3395           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3396           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3397           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3398           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3399
3400
3401 (defun gnus-summary-set-local-parameters (group)
3402   "Go through the local params of GROUP and set all variable specs in that list."
3403   (let ((params (gnus-group-find-parameter group))
3404         (vars '(quit-config))           ; Ignore quit-config.
3405         elem)
3406     (while params
3407       (setq elem (car params)
3408             params (cdr params))
3409       (and (consp elem)                 ; Has to be a cons.
3410            (consp (cdr elem))           ; The cdr has to be a list.
3411            (symbolp (car elem))         ; Has to be a symbol in there.
3412            (not (memq (car elem) vars))
3413            (ignore-errors               ; So we set it.
3414              (push (car elem) vars)
3415              (make-local-variable (car elem))
3416              (set (car elem) (eval (nth 1 elem))))))))
3417
3418 (defun gnus-summary-read-group (group &optional show-all no-article
3419                                       kill-buffer no-display backward
3420                                       select-articles)
3421   "Start reading news in newsgroup GROUP.
3422 If SHOW-ALL is non-nil, already read articles are also listed.
3423 If NO-ARTICLE is non-nil, no article is selected initially.
3424 If NO-DISPLAY, don't generate a summary buffer."
3425   (let (result)
3426     (while (and group
3427                 (null (setq result
3428                             (let ((gnus-auto-select-next nil))
3429                               (or (gnus-summary-read-group-1
3430                                    group show-all no-article
3431                                    kill-buffer no-display
3432                                    select-articles)
3433                                   (setq show-all nil
3434                                         select-articles nil)))))
3435                 (eq gnus-auto-select-next 'quietly))
3436       (set-buffer gnus-group-buffer)
3437       ;; The entry function called above goes to the next
3438       ;; group automatically, so we go two groups back
3439       ;; if we are searching for the previous group.
3440       (when backward
3441         (gnus-group-prev-unread-group 2))
3442       (if (not (equal group (gnus-group-group-name)))
3443           (setq group (gnus-group-group-name))
3444         (setq group nil)))
3445     result))
3446
3447 (defun gnus-summary-read-group-1 (group show-all no-article
3448                                         kill-buffer no-display
3449                                         &optional select-articles)
3450   ;; Killed foreign groups can't be entered.
3451   ;;  (when (and (not (gnus-group-native-p group))
3452   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3453   ;;    (error "Dead non-native groups can't be entered"))
3454   (gnus-message 5 "Retrieving newsgroup: %s..."
3455                 (gnus-group-decoded-name group))
3456   (let* ((new-group (gnus-summary-setup-buffer group))
3457          (quit-config (gnus-group-quit-config group))
3458          (did-select (and new-group (gnus-select-newsgroup
3459                                      group show-all select-articles))))
3460     (cond
3461      ;; This summary buffer exists already, so we just select it.
3462      ((not new-group)
3463       (gnus-set-global-variables)
3464       (when kill-buffer
3465         (gnus-kill-or-deaden-summary kill-buffer))
3466       (gnus-configure-windows 'summary 'force)
3467       (gnus-set-mode-line 'summary)
3468       (gnus-summary-position-point)
3469       (message "")
3470       t)
3471      ;; We couldn't select this group.
3472      ((null did-select)
3473       (when (and (eq major-mode 'gnus-summary-mode)
3474                  (not (equal (current-buffer) kill-buffer)))
3475         (kill-buffer (current-buffer))
3476         (if (not quit-config)
3477             (progn
3478               ;; Update the info -- marks might need to be removed,
3479               ;; for instance.
3480               (gnus-summary-update-info)
3481               (set-buffer gnus-group-buffer)
3482               (gnus-group-jump-to-group group)
3483               (gnus-group-next-unread-group 1))
3484           (gnus-handle-ephemeral-exit quit-config)))
3485       (let ((grpinfo (gnus-get-info group)))
3486         (if (null (gnus-info-read grpinfo))
3487             (gnus-message 3 "Group %s contains no messages"
3488                           (gnus-group-decoded-name group))
3489           (gnus-message 3 "Can't select group")))
3490       nil)
3491      ;; The user did a `C-g' while prompting for number of articles,
3492      ;; so we exit this group.
3493      ((eq did-select 'quit)
3494       (and (eq major-mode 'gnus-summary-mode)
3495            (not (equal (current-buffer) kill-buffer))
3496            (kill-buffer (current-buffer)))
3497       (when kill-buffer
3498         (gnus-kill-or-deaden-summary kill-buffer))
3499       (if (not quit-config)
3500           (progn
3501             (set-buffer gnus-group-buffer)
3502             (gnus-group-jump-to-group group)
3503             (gnus-group-next-unread-group 1)
3504             (gnus-configure-windows 'group 'force))
3505         (gnus-handle-ephemeral-exit quit-config))
3506       ;; Finally signal the quit.
3507       (signal 'quit nil))
3508      ;; The group was successfully selected.
3509      (t
3510       (gnus-set-global-variables)
3511       ;; Save the active value in effect when the group was entered.
3512       (setq gnus-newsgroup-active
3513             (gnus-copy-sequence
3514              (gnus-active gnus-newsgroup-name)))
3515       ;; You can change the summary buffer in some way with this hook.
3516       (gnus-run-hooks 'gnus-select-group-hook)
3517       (gnus-update-format-specifications
3518        nil 'summary 'summary-mode 'summary-dummy)
3519       (gnus-update-summary-mark-positions)
3520       ;; Do score processing.
3521       (when gnus-use-scoring
3522         (gnus-possibly-score-headers))
3523       ;; Check whether to fill in the gaps in the threads.
3524       (when gnus-build-sparse-threads
3525         (gnus-build-sparse-threads))
3526       ;; Find the initial limit.
3527       (if gnus-show-threads
3528           (if show-all
3529               (let ((gnus-newsgroup-dormant nil))
3530                 (gnus-summary-initial-limit show-all))
3531             (gnus-summary-initial-limit show-all))
3532         ;; When unthreaded, all articles are always shown.
3533         (setq gnus-newsgroup-limit
3534               (mapcar
3535                (lambda (header) (mail-header-number header))
3536                gnus-newsgroup-headers)))
3537       ;; Generate the summary buffer.
3538       (unless no-display
3539         (gnus-summary-prepare))
3540       (when gnus-use-trees
3541         (gnus-tree-open group)
3542         (setq gnus-summary-highlight-line-function
3543               'gnus-tree-highlight-article))
3544       ;; If the summary buffer is empty, but there are some low-scored
3545       ;; articles or some excluded dormants, we include these in the
3546       ;; buffer.
3547       (when (and (zerop (buffer-size))
3548                  (not no-display))
3549         (cond (gnus-newsgroup-dormant
3550                (gnus-summary-limit-include-dormant))
3551               ((and gnus-newsgroup-scored show-all)
3552                (gnus-summary-limit-include-expunged t))))
3553       ;; Function `gnus-apply-kill-file' must be called in this hook.
3554       (gnus-run-hooks 'gnus-apply-kill-hook)
3555       (if (and (zerop (buffer-size))
3556                (not no-display))
3557           (progn
3558             ;; This newsgroup is empty.
3559             (gnus-summary-catchup-and-exit nil t)
3560             (gnus-message 6 "No unread news")
3561             (when kill-buffer
3562               (gnus-kill-or-deaden-summary kill-buffer))
3563             ;; Return nil from this function.
3564             nil)
3565         ;; Hide conversation thread subtrees.  We cannot do this in
3566         ;; gnus-summary-prepare-hook since kill processing may not
3567         ;; work with hidden articles.
3568         (gnus-summary-maybe-hide-threads)
3569         (when kill-buffer
3570           (gnus-kill-or-deaden-summary kill-buffer))
3571         (gnus-summary-auto-select-subject)
3572         ;; Show first unread article if requested.
3573         (if (and (not no-article)
3574                  (not no-display)
3575                  gnus-newsgroup-unreads
3576                  gnus-auto-select-first)
3577             (progn
3578               (gnus-configure-windows 'summary)
3579               (let ((art (gnus-summary-article-number)))
3580                 (unless (and (not gnus-plugged)
3581                              (or (memq art gnus-newsgroup-undownloaded)
3582                                  (memq art gnus-newsgroup-downloadable)))
3583                   (gnus-summary-goto-article art))))
3584           ;; Don't select any articles.
3585           (gnus-summary-position-point)
3586           (gnus-configure-windows 'summary 'force)
3587           (gnus-set-mode-line 'summary))
3588         (when (and gnus-auto-center-group
3589                    (get-buffer-window gnus-group-buffer t))
3590           ;; Gotta use windows, because recenter does weird stuff if
3591           ;; the current buffer ain't the displayed window.
3592           (let ((owin (selected-window)))
3593             (select-window (get-buffer-window gnus-group-buffer t))
3594             (when (gnus-group-goto-group group)
3595               (recenter))
3596             (select-window owin)))
3597         ;; Mark this buffer as "prepared".
3598         (setq gnus-newsgroup-prepared t)
3599         (gnus-run-hooks 'gnus-summary-prepared-hook)
3600         (unless (gnus-ephemeral-group-p group)
3601           (gnus-group-update-group group))
3602         t)))))
3603
3604 (defun gnus-summary-auto-select-subject ()
3605   "Select the subject line on initial group entry."
3606   (goto-char (point-min))
3607   (cond
3608    ((eq gnus-auto-select-subject 'best)
3609     (gnus-summary-best-unread-subject))
3610    ((eq gnus-auto-select-subject 'unread)
3611     (gnus-summary-first-unread-subject))
3612    ((eq gnus-auto-select-subject 'unseen)
3613     (gnus-summary-first-unseen-subject))
3614    ((eq gnus-auto-select-subject 'unseen-or-unread)
3615     (gnus-summary-first-unseen-or-unread-subject))
3616    ((eq gnus-auto-select-subject 'first)
3617     ;; Do nothing.
3618     )
3619    ((functionp gnus-auto-select-subject)
3620     (funcall gnus-auto-select-subject))))
3621
3622 (defun gnus-summary-prepare ()
3623   "Generate the summary buffer."
3624   (interactive)
3625   (let ((buffer-read-only nil))
3626     (erase-buffer)
3627     (setq gnus-newsgroup-data nil
3628           gnus-newsgroup-data-reverse nil)
3629     (gnus-run-hooks 'gnus-summary-generate-hook)
3630     ;; Generate the buffer, either with threads or without.
3631     (when gnus-newsgroup-headers
3632       (gnus-summary-prepare-threads
3633        (if gnus-show-threads
3634            (gnus-sort-gathered-threads
3635             (funcall gnus-summary-thread-gathering-function
3636                      (gnus-sort-threads
3637                       (gnus-cut-threads (gnus-make-threads)))))
3638          ;; Unthreaded display.
3639          (gnus-sort-articles gnus-newsgroup-headers))))
3640     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3641     ;; Call hooks for modifying summary buffer.
3642     (goto-char (point-min))
3643     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3644
3645 (defsubst gnus-general-simplify-subject (subject)
3646   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3647   (setq subject
3648         (cond
3649          ;; Truncate the subject.
3650          (gnus-simplify-subject-functions
3651           (gnus-map-function gnus-simplify-subject-functions subject))
3652          ((numberp gnus-summary-gather-subject-limit)
3653           (setq subject (gnus-simplify-subject-re subject))
3654           (if (> (length subject) gnus-summary-gather-subject-limit)
3655               (substring subject 0 gnus-summary-gather-subject-limit)
3656             subject))
3657          ;; Fuzzily simplify it.
3658          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3659           (gnus-simplify-subject-fuzzy subject))
3660          ;; Just remove the leading "Re:".
3661          (t
3662           (gnus-simplify-subject-re subject))))
3663
3664   (if (and gnus-summary-gather-exclude-subject
3665            (string-match gnus-summary-gather-exclude-subject subject))
3666       nil                         ; This article shouldn't be gathered
3667     subject))
3668
3669 (defun gnus-summary-simplify-subject-query ()
3670   "Query where the respool algorithm would put this article."
3671   (interactive)
3672   (gnus-summary-select-article)
3673   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3674
3675 (defun gnus-gather-threads-by-subject (threads)
3676   "Gather threads by looking at Subject headers."
3677   (if (not gnus-summary-make-false-root)
3678       threads
3679     (let ((hashtb (gnus-make-hashtable 1024))
3680           (prev threads)
3681           (result threads)
3682           subject hthread whole-subject)
3683       (while threads
3684         (setq subject (gnus-general-simplify-subject
3685                        (setq whole-subject (mail-header-subject
3686                                             (caar threads)))))
3687         (when subject
3688           (if (setq hthread (gnus-gethash subject hashtb))
3689               (progn
3690                 ;; We enter a dummy root into the thread, if we
3691                 ;; haven't done that already.
3692                 (unless (stringp (caar hthread))
3693                   (setcar hthread (list whole-subject (car hthread))))
3694                 ;; We add this new gathered thread to this gathered
3695                 ;; thread.
3696                 (setcdr (car hthread)
3697                         (nconc (cdar hthread) (list (car threads))))
3698                 ;; Remove it from the list of threads.
3699                 (setcdr prev (cdr threads))
3700                 (setq threads prev))
3701             ;; Enter this thread into the hash table.
3702             (gnus-sethash subject
3703                           (if gnus-summary-make-false-root-always
3704                               (progn
3705                                 ;; If you want a dummy root above all
3706                                 ;; threads...
3707                                 (setcar threads (list whole-subject
3708                                                       (car threads)))
3709                                 threads)
3710                             threads)
3711                           hashtb)))
3712         (setq prev threads)
3713         (setq threads (cdr threads)))
3714       result)))
3715
3716 (defun gnus-gather-threads-by-references (threads)
3717   "Gather threads by looking at References headers."
3718   (let ((idhashtb (gnus-make-hashtable 1024))
3719         (thhashtb (gnus-make-hashtable 1024))
3720         (prev threads)
3721         (result threads)
3722         ids references id gthread gid entered ref)
3723     (while threads
3724       (when (setq references (mail-header-references (caar threads)))
3725         (setq id (mail-header-id (caar threads))
3726               ids (inline (gnus-split-references references))
3727               entered nil)
3728         (while (setq ref (pop ids))
3729           (setq ids (delete ref ids))
3730           (if (not (setq gid (gnus-gethash ref idhashtb)))
3731               (progn
3732                 (gnus-sethash ref id idhashtb)
3733                 (gnus-sethash id threads thhashtb))
3734             (setq gthread (gnus-gethash gid thhashtb))
3735             (unless entered
3736               ;; We enter a dummy root into the thread, if we
3737               ;; haven't done that already.
3738               (unless (stringp (caar gthread))
3739                 (setcar gthread (list (mail-header-subject (caar gthread))
3740                                       (car gthread))))
3741               ;; We add this new gathered thread to this gathered
3742               ;; thread.
3743               (setcdr (car gthread)
3744                       (nconc (cdar gthread) (list (car threads)))))
3745             ;; Add it into the thread hash table.
3746             (gnus-sethash id gthread thhashtb)
3747             (setq entered t)
3748             ;; Remove it from the list of threads.
3749             (setcdr prev (cdr threads))
3750             (setq threads prev))))
3751       (setq prev threads)
3752       (setq threads (cdr threads)))
3753     result))
3754
3755 (defun gnus-sort-gathered-threads (threads)
3756   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3757   (let ((result threads))
3758     (while threads
3759       (when (stringp (caar threads))
3760         (setcdr (car threads)
3761                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3762       (setq threads (cdr threads)))
3763     result))
3764
3765 (defun gnus-thread-loop-p (root thread)
3766   "Say whether ROOT is in THREAD."
3767   (let ((stack (list thread))
3768         (infloop 0)
3769         th)
3770     (while (setq thread (pop stack))
3771       (setq th (cdr thread))
3772       (while (and th
3773                   (not (eq (caar th) root)))
3774         (pop th))
3775       (if th
3776           ;; We have found a loop.
3777           (let (ref-dep)
3778             (setcdr thread (delq (car th) (cdr thread)))
3779             (if (boundp (setq ref-dep (intern "none"
3780                                               gnus-newsgroup-dependencies)))
3781                 (setcdr (symbol-value ref-dep)
3782                         (nconc (cdr (symbol-value ref-dep))
3783                                (list (car th))))
3784               (set ref-dep (list nil (car th))))
3785             (setq infloop 1
3786                   stack nil))
3787         ;; Push all the subthreads onto the stack.
3788         (push (cdr thread) stack)))
3789     infloop))
3790
3791 (defun gnus-make-threads ()
3792   "Go through the dependency hashtb and find the roots.  Return all threads."
3793   (let (threads)
3794     (while (catch 'infloop
3795              (mapatoms
3796               (lambda (refs)
3797                 ;; Deal with self-referencing References loops.
3798                 (when (and (car (symbol-value refs))
3799                            (not (zerop
3800                                  (apply
3801                                   '+
3802                                   (mapcar
3803                                    (lambda (thread)
3804                                      (gnus-thread-loop-p
3805                                       (car (symbol-value refs)) thread))
3806                                    (cdr (symbol-value refs)))))))
3807                   (setq threads nil)
3808                   (throw 'infloop t))
3809                 (unless (car (symbol-value refs))
3810                   ;; These threads do not refer back to any other
3811                   ;; articles, so they're roots.
3812                   (setq threads (append (cdr (symbol-value refs)) threads))))
3813               gnus-newsgroup-dependencies)))
3814     threads))
3815
3816 ;; Build the thread tree.
3817 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3818   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3819
3820 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3821 if it was already present.
3822
3823 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3824 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3825 Message-IDs will be renamed to a unique Message-ID before being
3826 entered.
3827
3828 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3829   (let* ((id (mail-header-id header))
3830          (id-dep (and id (intern id dependencies)))
3831          parent-id ref ref-dep ref-header replaced)
3832     ;; Enter this `header' in the `dependencies' table.
3833     (cond
3834      ((not id-dep)
3835       (setq header nil))
3836      ;; The first two cases do the normal part: enter a new `header'
3837      ;; in the `dependencies' table.
3838      ((not (boundp id-dep))
3839       (set id-dep (list header)))
3840      ((null (car (symbol-value id-dep)))
3841       (setcar (symbol-value id-dep) header))
3842
3843      ;; From here the `header' was already present in the
3844      ;; `dependencies' table.
3845      (force-new
3846       ;; Overrides an existing entry;
3847       ;; just set the header part of the entry.
3848       (setcar (symbol-value id-dep) header)
3849       (setq replaced t))
3850
3851      ;; Renames the existing `header' to a unique Message-ID.
3852      ((not gnus-summary-ignore-duplicates)
3853       ;; An article with this Message-ID has already been seen.
3854       ;; We rename the Message-ID.
3855       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3856            (list header))
3857       (mail-header-set-id header id))
3858
3859      ;; The last case ignores an existing entry, except it adds any
3860      ;; additional Xrefs (in case the two articles came from different
3861      ;; servers.
3862      ;; Also sets `header' to `nil' meaning that the `dependencies'
3863      ;; table was *not* modified.
3864      (t
3865       (mail-header-set-xref
3866        (car (symbol-value id-dep))
3867        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3868                    "")
3869                (or (mail-header-xref header) "")))
3870       (setq header nil)))
3871
3872     (when (and header (not replaced))
3873       ;; First check that we are not creating a References loop.
3874       (setq parent-id (gnus-parent-id (mail-header-references header)))
3875       (setq ref parent-id)
3876       (while (and ref
3877                   (setq ref-dep (intern-soft ref dependencies))
3878                   (boundp ref-dep)
3879                   (setq ref-header (car (symbol-value ref-dep))))
3880         (if (string= id ref)
3881             ;; Yuk!  This is a reference loop.  Make the article be a
3882             ;; root article.
3883             (progn
3884               (mail-header-set-references (car (symbol-value id-dep)) "none")
3885               (setq ref nil)
3886               (setq parent-id nil))
3887           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3888       (setq ref-dep (intern (or parent-id "none") dependencies))
3889       (if (boundp ref-dep)
3890           (setcdr (symbol-value ref-dep)
3891                   (nconc (cdr (symbol-value ref-dep))
3892                          (list (symbol-value id-dep))))
3893         (set ref-dep (list nil (symbol-value id-dep)))))
3894     header))
3895
3896 (defun gnus-extract-message-id-from-in-reply-to (string)
3897   (if (string-match "<[^>]+>" string)
3898       (substring string (match-beginning 0) (match-end 0))
3899     nil))
3900
3901 (defun gnus-build-sparse-threads ()
3902   (let ((headers gnus-newsgroup-headers)
3903         (mail-parse-charset gnus-newsgroup-charset)
3904         (gnus-summary-ignore-duplicates t)
3905         header references generation relations
3906         subject child end new-child date)
3907     ;; First we create an alist of generations/relations, where
3908     ;; generations is how much we trust the relation, and the relation
3909     ;; is parent/child.
3910     (gnus-message 7 "Making sparse threads...")
3911     (save-excursion
3912       (nnheader-set-temp-buffer " *gnus sparse threads*")
3913       (while (setq header (pop headers))
3914         (when (and (setq references (mail-header-references header))
3915                    (not (string= references "")))
3916           (insert references)
3917           (setq child (mail-header-id header)
3918                 subject (mail-header-subject header)
3919                 date (mail-header-date header)
3920                 generation 0)
3921           (while (search-backward ">" nil t)
3922             (setq end (1+ (point)))
3923             (when (search-backward "<" nil t)
3924               (setq new-child (buffer-substring (point) end))
3925               (push (list (incf generation)
3926                           child (setq child new-child)
3927                           subject date)
3928                     relations)))
3929           (when child
3930             (push (list (1+ generation) child nil subject) relations))
3931           (erase-buffer)))
3932       (kill-buffer (current-buffer)))
3933     ;; Sort over trustworthiness.
3934     (mapcar
3935      (lambda (relation)
3936        (when (gnus-dependencies-add-header
3937               (make-full-mail-header
3938                gnus-reffed-article-number
3939                (nth 3 relation) "" (or (nth 4 relation) "")
3940                (nth 1 relation)
3941                (or (nth 2 relation) "") 0 0 "")
3942               gnus-newsgroup-dependencies nil)
3943          (push gnus-reffed-article-number gnus-newsgroup-limit)
3944          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3945          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3946                gnus-newsgroup-reads)
3947          (decf gnus-reffed-article-number)))
3948      (sort relations 'car-less-than-car))
3949     (gnus-message 7 "Making sparse threads...done")))
3950
3951 (defun gnus-build-old-threads ()
3952   ;; Look at all the articles that refer back to old articles, and
3953   ;; fetch the headers for the articles that aren't there.  This will
3954   ;; build complete threads - if the roots haven't been expired by the
3955   ;; server, that is.
3956   (let ((mail-parse-charset gnus-newsgroup-charset)
3957         id heads)
3958     (mapatoms
3959      (lambda (refs)
3960        (when (not (car (symbol-value refs)))
3961          (setq heads (cdr (symbol-value refs)))
3962          (while heads
3963            (if (memq (mail-header-number (caar heads))
3964                      gnus-newsgroup-dormant)
3965                (setq heads (cdr heads))
3966              (setq id (symbol-name refs))
3967              (while (and (setq id (gnus-build-get-header id))
3968                          (not (car (gnus-id-to-thread id)))))
3969              (setq heads nil)))))
3970      gnus-newsgroup-dependencies)))
3971
3972 ;; This function has to be called with point after the article number
3973 ;; on the beginning of the line.
3974 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3975   (let ((eol (gnus-point-at-eol))
3976         (buffer (current-buffer))
3977         header references in-reply-to)
3978
3979     ;; overview: [num subject from date id refs chars lines misc]
3980     (unwind-protect
3981         (let (x)
3982           (narrow-to-region (point) eol)
3983           (unless (eobp)
3984             (forward-char))
3985
3986           (setq header
3987                 (make-full-mail-header
3988                  number                 ; number
3989                  (condition-case ()     ; subject
3990                      (funcall gnus-decode-encoded-word-function
3991                               (setq x (nnheader-nov-field)))
3992                    (error x))
3993                  (condition-case ()     ; from
3994                      (funcall gnus-decode-encoded-word-function
3995                               (setq x (nnheader-nov-field)))
3996                    (error x))
3997                  (nnheader-nov-field)   ; date
3998                  (nnheader-nov-read-message-id) ; id
3999                  (setq references (nnheader-nov-field)) ; refs
4000                  (nnheader-nov-read-integer) ; chars
4001                  (nnheader-nov-read-integer) ; lines
4002                  (unless (eobp)
4003                    (if (looking-at "Xref: ")
4004                        (goto-char (match-end 0)))
4005                    (nnheader-nov-field)) ; Xref
4006                  (nnheader-nov-parse-extra)))) ; extra
4007
4008       (widen))
4009
4010     (when (and (string= references "")
4011                (setq in-reply-to (mail-header-extra header))
4012                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4013       (mail-header-set-references
4014        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4015
4016     (when gnus-alter-header-function
4017       (funcall gnus-alter-header-function header))
4018     (gnus-dependencies-add-header header dependencies force-new)))
4019
4020 (defun gnus-build-get-header (id)
4021   "Look through the buffer of NOV lines and find the header to ID.
4022 Enter this line into the dependencies hash table, and return
4023 the id of the parent article (if any)."
4024   (let ((deps gnus-newsgroup-dependencies)
4025         found header)
4026     (prog1
4027         (save-excursion
4028           (set-buffer nntp-server-buffer)
4029           (let ((case-fold-search nil))
4030             (goto-char (point-min))
4031             (while (and (not found)
4032                         (search-forward id nil t))
4033               (beginning-of-line)
4034               (setq found (looking-at
4035                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4036                                    (regexp-quote id))))
4037               (or found (beginning-of-line 2)))
4038             (when found
4039               (beginning-of-line)
4040               (and
4041                (setq header (gnus-nov-parse-line
4042                              (read (current-buffer)) deps))
4043                (gnus-parent-id (mail-header-references header))))))
4044       (when header
4045         (let ((number (mail-header-number header)))
4046           (push number gnus-newsgroup-limit)
4047           (push header gnus-newsgroup-headers)
4048           (if (memq number gnus-newsgroup-unselected)
4049               (progn
4050                 (setq gnus-newsgroup-unreads
4051                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4052                                                number))
4053                 (setq gnus-newsgroup-unselected
4054                       (delq number gnus-newsgroup-unselected)))
4055             (push number gnus-newsgroup-ancient)))))))
4056
4057 (defun gnus-build-all-threads ()
4058   "Read all the headers."
4059   (let ((gnus-summary-ignore-duplicates t)
4060         (mail-parse-charset gnus-newsgroup-charset)
4061         (dependencies gnus-newsgroup-dependencies)
4062         header article)
4063     (save-excursion
4064       (set-buffer nntp-server-buffer)
4065       (let ((case-fold-search nil))
4066         (goto-char (point-min))
4067         (while (not (eobp))
4068           (ignore-errors
4069             (setq article (read (current-buffer))
4070                   header (gnus-nov-parse-line article dependencies)))
4071           (when header
4072             (save-excursion
4073               (set-buffer gnus-summary-buffer)
4074               (push header gnus-newsgroup-headers)
4075               (if (memq (setq article (mail-header-number header))
4076                         gnus-newsgroup-unselected)
4077                   (progn
4078                     (setq gnus-newsgroup-unreads
4079                           (gnus-add-to-sorted-list
4080                            gnus-newsgroup-unreads article))
4081                     (setq gnus-newsgroup-unselected
4082                           (delq article gnus-newsgroup-unselected)))
4083                 (push article gnus-newsgroup-ancient)))
4084             (forward-line 1)))))))
4085
4086 (defun gnus-summary-update-article-line (article header)
4087   "Update the line for ARTICLE using HEADERS."
4088   (let* ((id (mail-header-id header))
4089          (thread (gnus-id-to-thread id)))
4090     (unless thread
4091       (error "Article in no thread"))
4092     ;; Update the thread.
4093     (setcar thread header)
4094     (gnus-summary-goto-subject article)
4095     (let* ((datal (gnus-data-find-list article))
4096            (data (car datal))
4097            (buffer-read-only nil)
4098            (level (gnus-summary-thread-level)))
4099       (gnus-delete-line)
4100       (let ((inserted (- (point)
4101                          (progn
4102                            (gnus-summary-insert-line
4103                             header level nil 
4104                             (memq article gnus-newsgroup-undownloaded)
4105                             (gnus-article-mark article)
4106                             (memq article gnus-newsgroup-replied)
4107                             (memq article gnus-newsgroup-expirable)
4108                             ;; Only insert the Subject string when it's different
4109                             ;; from the previous Subject string.
4110                             (if (and
4111                                  gnus-show-threads
4112                                  (gnus-subject-equal
4113                                   (condition-case ()
4114                                       (mail-header-subject
4115                                        (gnus-data-header
4116                                         (cadr
4117                                          (gnus-data-find-list
4118                                           article
4119                                           (gnus-data-list t)))))
4120                                     ;; Error on the side of excessive subjects.
4121                                     (error ""))
4122                                   (mail-header-subject header)))
4123                                 ""
4124                               (mail-header-subject header))
4125                             nil (cdr (assq article gnus-newsgroup-scored))
4126                             (memq article gnus-newsgroup-processable))
4127                            (point)))))
4128         (when (cdr datal)
4129           (gnus-data-update-list
4130            (cdr datal) 
4131            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4132
4133 (defun gnus-summary-update-article (article &optional iheader)
4134   "Update ARTICLE in the summary buffer."
4135   (set-buffer gnus-summary-buffer)
4136   (let* ((header (gnus-summary-article-header article))
4137          (id (mail-header-id header))
4138          (data (gnus-data-find article))
4139          (thread (gnus-id-to-thread id))
4140          (references (mail-header-references header))
4141          (parent
4142           (gnus-id-to-thread
4143            (or (gnus-parent-id
4144                 (when (and references
4145                            (not (equal "" references)))
4146                   references))
4147                "none")))
4148          (buffer-read-only nil)
4149          (old (car thread)))
4150     (when thread
4151       (unless iheader
4152         (setcar thread nil)
4153         (when parent
4154           (delq thread parent)))
4155       (if (gnus-summary-insert-subject id header)
4156           ;; Set the (possibly) new article number in the data structure.
4157           (gnus-data-set-number data (gnus-id-to-article id))
4158         (setcar thread old)
4159         nil))))
4160
4161 (defun gnus-rebuild-thread (id &optional line)
4162   "Rebuild the thread containing ID.
4163 If LINE, insert the rebuilt thread starting on line LINE."
4164   (let ((buffer-read-only nil)
4165         old-pos current thread data)
4166     (if (not gnus-show-threads)
4167         (setq thread (list (car (gnus-id-to-thread id))))
4168       ;; Get the thread this article is part of.
4169       (setq thread (gnus-remove-thread id)))
4170     (setq old-pos (gnus-point-at-bol))
4171     (setq current (save-excursion
4172                     (and (re-search-backward "[\r\n]" nil t)
4173                          (gnus-summary-article-number))))
4174     ;; If this is a gathered thread, we have to go some re-gathering.
4175     (when (stringp (car thread))
4176       (let ((subject (car thread))
4177             roots thr)
4178         (setq thread (cdr thread))
4179         (while thread
4180           (unless (memq (setq thr (gnus-id-to-thread
4181                                    (gnus-root-id
4182                                     (mail-header-id (caar thread)))))
4183                         roots)
4184             (push thr roots))
4185           (setq thread (cdr thread)))
4186         ;; We now have all (unique) roots.
4187         (if (= (length roots) 1)
4188             ;; All the loose roots are now one solid root.
4189             (setq thread (car roots))
4190           (setq thread (cons subject (gnus-sort-threads roots))))))
4191     (let (threads)
4192       ;; We then insert this thread into the summary buffer.
4193       (when line
4194         (goto-char (point-min))
4195         (forward-line (1- line)))
4196       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4197         (if gnus-show-threads
4198             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4199           (gnus-summary-prepare-unthreaded thread))
4200         (setq data (nreverse gnus-newsgroup-data))
4201         (setq threads gnus-newsgroup-threads))
4202       ;; We splice the new data into the data structure.
4203       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4204       ;;!!! then we want to insert at the beginning of the buffer.
4205       ;;!!! That happens to be true with Gnus now, but that may
4206       ;;!!! change in the future.  Perhaps.
4207       (gnus-data-enter-list
4208        (if line nil current) data (- (point) old-pos))
4209       (setq gnus-newsgroup-threads
4210             (nconc threads gnus-newsgroup-threads))
4211       (gnus-data-compute-positions))))
4212
4213 (defun gnus-number-to-header (number)
4214   "Return the header for article NUMBER."
4215   (let ((headers gnus-newsgroup-headers))
4216     (while (and headers
4217                 (not (= number (mail-header-number (car headers)))))
4218       (pop headers))
4219     (when headers
4220       (car headers))))
4221
4222 (defun gnus-parent-headers (in-headers &optional generation)
4223   "Return the headers of the GENERATIONeth parent of HEADERS."
4224   (unless generation
4225     (setq generation 1))
4226   (let ((parent t)
4227         (headers in-headers)
4228         references)
4229     (while (and parent
4230                 (not (zerop generation))
4231                 (setq references (mail-header-references headers)))
4232       (setq headers (if (and references
4233                              (setq parent (gnus-parent-id references)))
4234                         (car (gnus-id-to-thread parent))
4235                       nil))
4236       (decf generation))
4237     (and (not (eq headers in-headers))
4238          headers)))
4239
4240 (defun gnus-id-to-thread (id)
4241   "Return the (sub-)thread where ID appears."
4242   (gnus-gethash id gnus-newsgroup-dependencies))
4243
4244 (defun gnus-id-to-article (id)
4245   "Return the article number of ID."
4246   (let ((thread (gnus-id-to-thread id)))
4247     (when (and thread
4248                (car thread))
4249       (mail-header-number (car thread)))))
4250
4251 (defun gnus-id-to-header (id)
4252   "Return the article headers of ID."
4253   (car (gnus-id-to-thread id)))
4254
4255 (defun gnus-article-displayed-root-p (article)
4256   "Say whether ARTICLE is a root(ish) article."
4257   (let ((level (gnus-summary-thread-level article))
4258         (refs (mail-header-references  (gnus-summary-article-header article)))
4259         particle)
4260     (cond
4261      ((null level) nil)
4262      ((zerop level) t)
4263      ((null refs) t)
4264      ((null (gnus-parent-id refs)) t)
4265      ((and (= 1 level)
4266            (null (setq particle (gnus-id-to-article
4267                                  (gnus-parent-id refs))))
4268            (null (gnus-summary-thread-level particle)))))))
4269
4270 (defun gnus-root-id (id)
4271   "Return the id of the root of the thread where ID appears."
4272   (let (last-id prev)
4273     (while (and id (setq prev (car (gnus-id-to-thread id))))
4274       (setq last-id id
4275             id (gnus-parent-id (mail-header-references prev))))
4276     last-id))
4277
4278 (defun gnus-articles-in-thread (thread)
4279   "Return the list of articles in THREAD."
4280   (cons (mail-header-number (car thread))
4281         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4282
4283 (defun gnus-remove-thread (id &optional dont-remove)
4284   "Remove the thread that has ID in it."
4285   (let (headers thread last-id)
4286     ;; First go up in this thread until we find the root.
4287     (setq last-id (gnus-root-id id)
4288           headers (message-flatten-list (gnus-id-to-thread last-id)))
4289     ;; We have now found the real root of this thread.  It might have
4290     ;; been gathered into some loose thread, so we have to search
4291     ;; through the threads to find the thread we wanted.
4292     (let ((threads gnus-newsgroup-threads)
4293           sub)
4294       (while threads
4295         (setq sub (car threads))
4296         (if (stringp (car sub))
4297             ;; This is a gathered thread, so we look at the roots
4298             ;; below it to find whether this article is in this
4299             ;; gathered root.
4300             (progn
4301               (setq sub (cdr sub))
4302               (while sub
4303                 (when (member (caar sub) headers)
4304                   (setq thread (car threads)
4305                         threads nil
4306                         sub nil))
4307                 (setq sub (cdr sub))))
4308           ;; It's an ordinary thread, so we check it.
4309           (when (eq (car sub) (car headers))
4310             (setq thread sub
4311                   threads nil)))
4312         (setq threads (cdr threads)))
4313       ;; If this article is in no thread, then it's a root.
4314       (if thread
4315           (unless dont-remove
4316             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4317         (setq thread (gnus-id-to-thread last-id)))
4318       (when thread
4319         (prog1
4320             thread                      ; We return this thread.
4321           (unless dont-remove
4322             (if (stringp (car thread))
4323                 (progn
4324                   ;; If we use dummy roots, then we have to remove the
4325                   ;; dummy root as well.
4326                   (when (eq gnus-summary-make-false-root 'dummy)
4327                     ;; We go to the dummy root by going to
4328                     ;; the first sub-"thread", and then one line up.
4329                     (gnus-summary-goto-article
4330                      (mail-header-number (caadr thread)))
4331                     (forward-line -1)
4332                     (gnus-delete-line)
4333                     (gnus-data-compute-positions))
4334                   (setq thread (cdr thread))
4335                   (while thread
4336                     (gnus-remove-thread-1 (car thread))
4337                     (setq thread (cdr thread))))
4338               (gnus-remove-thread-1 thread))))))))
4339
4340 (defun gnus-remove-thread-1 (thread)
4341   "Remove the thread THREAD recursively."
4342   (let ((number (mail-header-number (pop thread)))
4343         d)
4344     (setq thread (reverse thread))
4345     (while thread
4346       (gnus-remove-thread-1 (pop thread)))
4347     (when (setq d (gnus-data-find number))
4348       (goto-char (gnus-data-pos d))
4349       (gnus-summary-show-thread)
4350       (gnus-data-remove
4351        number
4352        (- (gnus-point-at-bol)
4353           (prog1
4354               (1+ (gnus-point-at-eol))
4355             (gnus-delete-line)))))))
4356
4357 (defun gnus-sort-threads-1 (threads func)
4358   (sort (mapcar (lambda (thread)
4359                   (cons (car thread)
4360                         (and (cdr thread)
4361                              (gnus-sort-threads-1 (cdr thread) func))))
4362                 threads) func))
4363
4364 (defun gnus-sort-threads (threads)
4365   "Sort THREADS."
4366   (if (not gnus-thread-sort-functions)
4367       threads
4368     (gnus-message 8 "Sorting threads...")
4369     (let ((max-lisp-eval-depth 5000))
4370       (prog1 (gnus-sort-threads-1
4371          threads
4372          (gnus-make-sort-function gnus-thread-sort-functions))
4373         (gnus-message 8 "Sorting threads...done")))))
4374
4375 (defun gnus-sort-articles (articles)
4376   "Sort ARTICLES."
4377   (when gnus-article-sort-functions
4378     (gnus-message 7 "Sorting articles...")
4379     (prog1
4380         (setq gnus-newsgroup-headers
4381               (sort articles (gnus-make-sort-function
4382                               gnus-article-sort-functions)))
4383       (gnus-message 7 "Sorting articles...done"))))
4384
4385 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4386 (defmacro gnus-thread-header (thread)
4387   "Return header of first article in THREAD.
4388 Note that THREAD must never, ever be anything else than a variable -
4389 using some other form will lead to serious barfage."
4390   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4391   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4392   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4393         (vector thread) 2))
4394
4395 (defsubst gnus-article-sort-by-number (h1 h2)
4396   "Sort articles by article number."
4397   (< (mail-header-number h1)
4398      (mail-header-number h2)))
4399
4400 (defun gnus-thread-sort-by-number (h1 h2)
4401   "Sort threads by root article number."
4402   (gnus-article-sort-by-number
4403    (gnus-thread-header h1) (gnus-thread-header h2)))
4404
4405 (defsubst gnus-article-sort-by-random (h1 h2)
4406   "Sort articles by article number."
4407   (zerop (random 2)))
4408
4409 (defun gnus-thread-sort-by-random (h1 h2)
4410   "Sort threads by root article number."
4411   (gnus-article-sort-by-random
4412    (gnus-thread-header h1) (gnus-thread-header h2)))
4413
4414 (defsubst gnus-article-sort-by-lines (h1 h2)
4415   "Sort articles by article Lines header."
4416   (< (mail-header-lines h1)
4417      (mail-header-lines h2)))
4418
4419 (defun gnus-thread-sort-by-lines (h1 h2)
4420   "Sort threads by root article Lines header."
4421   (gnus-article-sort-by-lines
4422    (gnus-thread-header h1) (gnus-thread-header h2)))
4423
4424 (defsubst gnus-article-sort-by-chars (h1 h2)
4425   "Sort articles by octet length."
4426   (< (mail-header-chars h1)
4427      (mail-header-chars h2)))
4428
4429 (defun gnus-thread-sort-by-chars (h1 h2)
4430   "Sort threads by root article octet length."
4431   (gnus-article-sort-by-chars
4432    (gnus-thread-header h1) (gnus-thread-header h2)))
4433
4434 (defsubst gnus-article-sort-by-author (h1 h2)
4435   "Sort articles by root author."
4436   (string-lessp
4437    (let ((extract (funcall
4438                    gnus-extract-address-components
4439                    (mail-header-from h1))))
4440      (or (car extract) (cadr extract) ""))
4441    (let ((extract (funcall
4442                    gnus-extract-address-components
4443                    (mail-header-from h2))))
4444      (or (car extract) (cadr extract) ""))))
4445
4446 (defun gnus-thread-sort-by-author (h1 h2)
4447   "Sort threads by root author."
4448   (gnus-article-sort-by-author
4449    (gnus-thread-header h1)  (gnus-thread-header h2)))
4450
4451 (defsubst gnus-article-sort-by-subject (h1 h2)
4452   "Sort articles by root subject."
4453   (string-lessp
4454    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4455    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4456
4457 (defun gnus-thread-sort-by-subject (h1 h2)
4458   "Sort threads by root subject."
4459   (gnus-article-sort-by-subject
4460    (gnus-thread-header h1) (gnus-thread-header h2)))
4461
4462 (defsubst gnus-article-sort-by-date (h1 h2)
4463   "Sort articles by root article date."
4464   (time-less-p
4465    (gnus-date-get-time (mail-header-date h1))
4466    (gnus-date-get-time (mail-header-date h2))))
4467
4468 (defun gnus-thread-sort-by-date (h1 h2)
4469   "Sort threads by root article date."
4470   (gnus-article-sort-by-date
4471    (gnus-thread-header h1) (gnus-thread-header h2)))
4472
4473 (defsubst gnus-article-sort-by-score (h1 h2)
4474   "Sort articles by root article score.
4475 Unscored articles will be counted as having a score of zero."
4476   (> (or (cdr (assq (mail-header-number h1)
4477                     gnus-newsgroup-scored))
4478          gnus-summary-default-score 0)
4479      (or (cdr (assq (mail-header-number h2)
4480                     gnus-newsgroup-scored))
4481          gnus-summary-default-score 0)))
4482
4483 (defun gnus-thread-sort-by-score (h1 h2)
4484   "Sort threads by root article score."
4485   (gnus-article-sort-by-score
4486    (gnus-thread-header h1) (gnus-thread-header h2)))
4487
4488 (defun gnus-thread-sort-by-total-score (h1 h2)
4489   "Sort threads by the sum of all scores in the thread.
4490 Unscored articles will be counted as having a score of zero."
4491   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4492
4493 (defun gnus-thread-total-score (thread)
4494   ;; This function find the total score of THREAD.
4495   (cond
4496    ((null thread)
4497     0)
4498    ((consp thread)
4499     (if (stringp (car thread))
4500         (apply gnus-thread-score-function 0
4501                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4502       (gnus-thread-total-score-1 thread)))
4503    (t
4504     (gnus-thread-total-score-1 (list thread)))))
4505
4506 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4507   "Sort threads such that the thread with the most recently arrived article comes first."
4508   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4509
4510 (defun gnus-thread-highest-number (thread)
4511   "Return the highest article number in THREAD."
4512   (apply 'max (mapcar (lambda (header)
4513                         (mail-header-number header))
4514                       (message-flatten-list thread))))
4515
4516 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4517   "Sort threads such that the thread with the most recently dated article comes first."
4518   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4519
4520 (defun gnus-thread-latest-date (thread)
4521   "Return the highest article date in THREAD."
4522   (let ((previous-time 0))
4523     (apply 'max
4524            (mapcar
4525             (lambda (header)
4526               (setq previous-time
4527                     (time-to-seconds
4528                      (condition-case ()
4529                          (mail-header-parse-date (mail-header-date header))
4530                        (error previous-time)))))
4531             (sort
4532              (message-flatten-list thread)
4533              (lambda (h1 h2)
4534                (< (mail-header-number h1)
4535                   (mail-header-number h2))))))))
4536
4537 (defun gnus-thread-total-score-1 (root)
4538   ;; This function find the total score of the thread below ROOT.
4539   (setq root (car root))
4540   (apply gnus-thread-score-function
4541          (or (append
4542               (mapcar 'gnus-thread-total-score
4543                       (cdr (gnus-id-to-thread (mail-header-id root))))
4544               (when (> (mail-header-number root) 0)
4545                 (list (or (cdr (assq (mail-header-number root)
4546                                      gnus-newsgroup-scored))
4547                           gnus-summary-default-score 0))))
4548              (list gnus-summary-default-score)
4549              '(0))))
4550
4551 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4552 (defvar gnus-tmp-prev-subject nil)
4553 (defvar gnus-tmp-false-parent nil)
4554 (defvar gnus-tmp-root-expunged nil)
4555 (defvar gnus-tmp-dummy-line nil)
4556
4557 (eval-when-compile (defvar gnus-tmp-header))
4558 (defun gnus-extra-header (type &optional header)
4559   "Return the extra header of TYPE."
4560   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4561       ""))
4562
4563 (defvar gnus-tmp-thread-tree-header-string "")
4564
4565 (defcustom gnus-sum-thread-tree-root "> "
4566   "With %B spec, used for the root of a thread.
4567 If nil, use subject instead."
4568   :type 'string
4569   :group 'gnus-thread)
4570 (defcustom gnus-sum-thread-tree-false-root "> "
4571   "With %B spec, used for a false root of a thread.
4572 If nil, use subject instead."
4573   :type 'string
4574   :group 'gnus-thread)
4575 (defcustom gnus-sum-thread-tree-single-indent ""
4576   "With %B spec, used for a thread with just one message.
4577 If nil, use subject instead."
4578   :type 'string
4579   :group 'gnus-thread)
4580 (defcustom gnus-sum-thread-tree-vertical "| "
4581   "With %B spec, used for drawing a vertical line."
4582   :type 'string
4583   :group 'gnus-thread)
4584 (defcustom gnus-sum-thread-tree-indent "  "
4585   "With %B spec, used for indenting."
4586   :type 'string
4587   :group 'gnus-thread)
4588 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4589   "With %B spec, used for a leaf with brothers."
4590   :type 'string
4591   :group 'gnus-thread)
4592 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4593   "With %B spec, used for a leaf without brothers."
4594   :type 'string
4595   :group 'gnus-thread)
4596
4597 (defun gnus-summary-prepare-threads (threads)
4598   "Prepare summary buffer from THREADS and indentation LEVEL.
4599 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4600 or a straight list of headers."
4601   (gnus-message 7 "Generating summary...")
4602
4603   (setq gnus-newsgroup-threads threads)
4604   (beginning-of-line)
4605
4606   (let ((gnus-tmp-level 0)
4607         (default-score (or gnus-summary-default-score 0))
4608         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4609         (building-line-count gnus-summary-display-while-building)
4610         (building-count (integerp gnus-summary-display-while-building))
4611         thread number subject stack state gnus-tmp-gathered beg-match
4612         new-roots gnus-tmp-new-adopts thread-end simp-subject
4613         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4614         gnus-tmp-replied gnus-tmp-subject-or-nil
4615         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4616         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4617         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4618         tree-stack)
4619
4620     (setq gnus-tmp-prev-subject nil
4621           gnus-tmp-thread-tree-header-string "")
4622
4623     (if (vectorp (car threads))
4624         ;; If this is a straight (sic) list of headers, then a
4625         ;; threaded summary display isn't required, so we just create
4626         ;; an unthreaded one.
4627         (gnus-summary-prepare-unthreaded threads)
4628
4629       ;; Do the threaded display.
4630
4631       (if gnus-summary-display-while-building
4632           (switch-to-buffer (buffer-name)))
4633       (while (or threads stack gnus-tmp-new-adopts new-roots)
4634
4635         (if (and (= gnus-tmp-level 0)
4636                  (or (not stack)
4637                      (= (caar stack) 0))
4638                  (not gnus-tmp-false-parent)
4639                  (or gnus-tmp-new-adopts new-roots))
4640             (if gnus-tmp-new-adopts
4641                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4642                       thread (list (car gnus-tmp-new-adopts))
4643                       gnus-tmp-header (caar thread)
4644                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4645               (when new-roots
4646                 (setq thread (list (car new-roots))
4647                       gnus-tmp-header (caar thread)
4648                       new-roots (cdr new-roots))))
4649
4650           (if threads
4651               ;; If there are some threads, we do them before the
4652               ;; threads on the stack.
4653               (setq thread threads
4654                     gnus-tmp-header (caar thread))
4655             ;; There were no current threads, so we pop something off
4656             ;; the stack.
4657             (setq state (car stack)
4658                   gnus-tmp-level (car state)
4659                   tree-stack (cadr state)
4660                   thread (caddr state)
4661                   stack (cdr stack)
4662                   gnus-tmp-header (caar thread))))
4663
4664         (setq gnus-tmp-false-parent nil)
4665         (setq gnus-tmp-root-expunged nil)
4666         (setq thread-end nil)
4667
4668         (if (stringp gnus-tmp-header)
4669             ;; The header is a dummy root.
4670             (cond
4671              ((eq gnus-summary-make-false-root 'adopt)
4672               ;; We let the first article adopt the rest.
4673               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4674                                                (cddar thread)))
4675               (setq gnus-tmp-gathered
4676                     (nconc (mapcar
4677                             (lambda (h) (mail-header-number (car h)))
4678                             (cddar thread))
4679                            gnus-tmp-gathered))
4680               (setq thread (cons (list (caar thread)
4681                                        (cadar thread))
4682                                  (cdr thread)))
4683               (setq gnus-tmp-level -1
4684                     gnus-tmp-false-parent t))
4685              ((eq gnus-summary-make-false-root 'empty)
4686               ;; We print adopted articles with empty subject fields.
4687               (setq gnus-tmp-gathered
4688                     (nconc (mapcar
4689                             (lambda (h) (mail-header-number (car h)))
4690                             (cddar thread))
4691                            gnus-tmp-gathered))
4692               (setq gnus-tmp-level -1))
4693              ((eq gnus-summary-make-false-root 'dummy)
4694               ;; We remember that we probably want to output a dummy
4695               ;; root.
4696               (setq gnus-tmp-dummy-line gnus-tmp-header)
4697               (setq gnus-tmp-prev-subject gnus-tmp-header))
4698              (t
4699               ;; We do not make a root for the gathered
4700               ;; sub-threads at all.
4701               (setq gnus-tmp-level -1)))
4702
4703           (setq number (mail-header-number gnus-tmp-header)
4704                 subject (mail-header-subject gnus-tmp-header)
4705                 simp-subject (gnus-simplify-subject-fully subject))
4706
4707           (cond
4708            ;; If the thread has changed subject, we might want to make
4709            ;; this subthread into a root.
4710            ((and (null gnus-thread-ignore-subject)
4711                  (not (zerop gnus-tmp-level))
4712                  gnus-tmp-prev-subject
4713                  (not (string= gnus-tmp-prev-subject simp-subject)))
4714             (setq new-roots (nconc new-roots (list (car thread)))
4715                   thread-end t
4716                   gnus-tmp-header nil))
4717            ;; If the article lies outside the current limit,
4718            ;; then we do not display it.
4719            ((not (memq number gnus-newsgroup-limit))
4720             (setq gnus-tmp-gathered
4721                   (nconc (mapcar
4722                           (lambda (h) (mail-header-number (car h)))
4723                           (cdar thread))
4724                          gnus-tmp-gathered))
4725             (setq gnus-tmp-new-adopts (if (cdar thread)
4726                                           (append gnus-tmp-new-adopts
4727                                                   (cdar thread))
4728                                         gnus-tmp-new-adopts)
4729                   thread-end t
4730                   gnus-tmp-header nil)
4731             (when (zerop gnus-tmp-level)
4732               (setq gnus-tmp-root-expunged t)))
4733            ;; Perhaps this article is to be marked as read?
4734            ((and gnus-summary-mark-below
4735                  (< (or (cdr (assq number gnus-newsgroup-scored))
4736                         default-score)
4737                     gnus-summary-mark-below)
4738                  ;; Don't touch sparse articles.
4739                  (not (gnus-summary-article-sparse-p number))
4740                  (not (gnus-summary-article-ancient-p number)))
4741             (setq gnus-newsgroup-unreads
4742                   (delq number gnus-newsgroup-unreads))
4743             (if gnus-newsgroup-auto-expire
4744                 (setq gnus-newsgroup-expirable
4745                       (gnus-add-to-sorted-list
4746                        gnus-newsgroup-expirable number))
4747               (push (cons number gnus-low-score-mark)
4748                     gnus-newsgroup-reads))))
4749
4750           (when gnus-tmp-header
4751             ;; We may have an old dummy line to output before this
4752             ;; article.
4753             (when (and gnus-tmp-dummy-line
4754                        (gnus-subject-equal
4755                         gnus-tmp-dummy-line
4756                         (mail-header-subject gnus-tmp-header)))
4757               (gnus-summary-insert-dummy-line
4758                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4759               (setq gnus-tmp-dummy-line nil))
4760
4761             ;; Compute the mark.
4762             (setq gnus-tmp-unread (gnus-article-mark number))
4763
4764             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4765                                   gnus-tmp-header gnus-tmp-level)
4766                   gnus-newsgroup-data)
4767
4768             ;; Actually insert the line.
4769             (setq
4770              gnus-tmp-subject-or-nil
4771              (cond
4772               ((and gnus-thread-ignore-subject
4773                     gnus-tmp-prev-subject
4774                     (not (string= gnus-tmp-prev-subject simp-subject)))
4775                subject)
4776               ((zerop gnus-tmp-level)
4777                (if (and (eq gnus-summary-make-false-root 'empty)
4778                         (memq number gnus-tmp-gathered)
4779                         gnus-tmp-prev-subject
4780                         (string= gnus-tmp-prev-subject simp-subject))
4781                    gnus-summary-same-subject
4782                  subject))
4783               (t gnus-summary-same-subject)))
4784             (if (and (eq gnus-summary-make-false-root 'adopt)
4785                      (= gnus-tmp-level 1)
4786                      (memq number gnus-tmp-gathered))
4787                 (setq gnus-tmp-opening-bracket ?\<
4788                       gnus-tmp-closing-bracket ?\>)
4789               (setq gnus-tmp-opening-bracket ?\[
4790                     gnus-tmp-closing-bracket ?\]))
4791             (setq
4792              gnus-tmp-indentation
4793              (aref gnus-thread-indent-array gnus-tmp-level)
4794              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4795              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4796                                 gnus-summary-default-score 0)
4797              gnus-tmp-score-char
4798              (if (or (null gnus-summary-default-score)
4799                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4800                          gnus-summary-zcore-fuzz))
4801                  ?                      ;Whitespace
4802                (if (< gnus-tmp-score gnus-summary-default-score)
4803                    gnus-score-below-mark gnus-score-over-mark))
4804              gnus-tmp-replied
4805              (cond ((memq number gnus-newsgroup-processable)
4806                     gnus-process-mark)
4807                    ((memq number gnus-newsgroup-cached)
4808                     gnus-cached-mark)
4809                    ((memq number gnus-newsgroup-replied)
4810                     gnus-replied-mark)
4811                    ((memq number gnus-newsgroup-forwarded)
4812                     gnus-forwarded-mark)
4813                    ((memq number gnus-newsgroup-saved)
4814                     gnus-saved-mark)
4815                    ((memq number gnus-newsgroup-recent)
4816                     gnus-recent-mark)
4817                    ((memq number gnus-newsgroup-unseen)
4818                     gnus-unseen-mark)
4819                    (t gnus-no-mark))
4820              gnus-tmp-downloaded
4821              (cond ((memq number gnus-newsgroup-undownloaded) 
4822                     gnus-undownloaded-mark)
4823                    (gnus-newsgroup-agentized
4824                     gnus-downloaded-mark)
4825                    (t
4826                     gnus-no-mark))
4827              gnus-tmp-from (mail-header-from gnus-tmp-header)
4828              gnus-tmp-name
4829              (cond
4830               ((string-match "<[^>]+> *$" gnus-tmp-from)
4831                (setq beg-match (match-beginning 0))
4832                (or (and (string-match "^\".+\"" gnus-tmp-from)
4833                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4834                    (substring gnus-tmp-from 0 beg-match)))
4835               ((string-match "(.+)" gnus-tmp-from)
4836                (substring gnus-tmp-from
4837                           (1+ (match-beginning 0)) (1- (match-end 0))))
4838               (t gnus-tmp-from))
4839
4840              ;; Do the %B string
4841              gnus-tmp-thread-tree-header-string
4842              (cond
4843               ((not gnus-show-threads) "")
4844               ((zerop gnus-tmp-level)
4845                (cond ((cdar thread)
4846                       (or gnus-sum-thread-tree-root subject))
4847                      (gnus-tmp-new-adopts
4848                       (or gnus-sum-thread-tree-false-root subject))
4849                      (t
4850                       (or gnus-sum-thread-tree-single-indent subject))))
4851               (t
4852                (concat (apply 'concat
4853                               (mapcar (lambda (item)
4854                                         (if (= item 1)
4855                                             gnus-sum-thread-tree-vertical
4856                                           gnus-sum-thread-tree-indent))
4857                                       (cdr (reverse tree-stack))))
4858                        (if (nth 1 thread)
4859                            gnus-sum-thread-tree-leaf-with-other
4860                          gnus-sum-thread-tree-single-leaf)))))
4861             (when (string= gnus-tmp-name "")
4862               (setq gnus-tmp-name gnus-tmp-from))
4863             (unless (numberp gnus-tmp-lines)
4864               (setq gnus-tmp-lines -1))
4865             (if (= gnus-tmp-lines -1)
4866                 (setq gnus-tmp-lines "?")
4867               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4868               (gnus-put-text-property
4869              (point)
4870              (progn (eval gnus-summary-line-format-spec) (point))
4871                'gnus-number number)
4872             (when gnus-visual-p
4873               (forward-line -1)
4874               (gnus-run-hooks 'gnus-summary-update-hook)
4875               (forward-line 1))
4876
4877             (setq gnus-tmp-prev-subject simp-subject)))
4878
4879         (when (nth 1 thread)
4880           (push (list (max 0 gnus-tmp-level)
4881                       (copy-sequence tree-stack)
4882                       (nthcdr 1 thread))
4883                 stack))
4884         (push (if (nth 1 thread) 1 0) tree-stack)
4885         (incf gnus-tmp-level)
4886         (setq threads (if thread-end nil (cdar thread)))
4887         (if gnus-summary-display-while-building
4888             (if building-count
4889                 (progn
4890                   ;; use a set frequency
4891                   (setq building-line-count (1- building-line-count))
4892                   (when (= building-line-count 0)
4893                     (sit-for 0)
4894                     (setq building-line-count
4895                           gnus-summary-display-while-building)))
4896               ;; always
4897               (sit-for 0)))
4898         (unless threads
4899           (setq gnus-tmp-level 0)))))
4900   (gnus-message 7 "Generating summary...done"))
4901
4902 (defun gnus-summary-prepare-unthreaded (headers)
4903   "Generate an unthreaded summary buffer based on HEADERS."
4904   (let (header number mark)
4905
4906     (beginning-of-line)
4907
4908     (while headers
4909       ;; We may have to root out some bad articles...
4910       (when (memq (setq number (mail-header-number
4911                                 (setq header (pop headers))))
4912                   gnus-newsgroup-limit)
4913         ;; Mark article as read when it has a low score.
4914         (when (and gnus-summary-mark-below
4915                    (< (or (cdr (assq number gnus-newsgroup-scored))
4916                           gnus-summary-default-score 0)
4917                       gnus-summary-mark-below)
4918                    (not (gnus-summary-article-ancient-p number)))
4919           (setq gnus-newsgroup-unreads
4920                 (delq number gnus-newsgroup-unreads))
4921           (if gnus-newsgroup-auto-expire
4922               (push number gnus-newsgroup-expirable)
4923             (push (cons number gnus-low-score-mark)
4924                   gnus-newsgroup-reads)))
4925
4926         (setq mark (gnus-article-mark number))
4927         (push (gnus-data-make number mark (1+ (point)) header 0)
4928               gnus-newsgroup-data)
4929         (gnus-summary-insert-line
4930          header 0 number
4931          (memq number gnus-newsgroup-undownloaded)
4932          mark (memq number gnus-newsgroup-replied)
4933          (memq number gnus-newsgroup-expirable)
4934          (mail-header-subject header) nil
4935          (cdr (assq number gnus-newsgroup-scored))
4936          (memq number gnus-newsgroup-processable))))))
4937
4938 (defun gnus-summary-remove-list-identifiers ()
4939   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4940   (let ((regexp (if (consp gnus-list-identifiers)
4941                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4942                   gnus-list-identifiers))
4943         changed subject)
4944     (when regexp
4945       (dolist (header gnus-newsgroup-headers)
4946         (setq subject (mail-header-subject header)
4947               changed nil)
4948         (while (string-match
4949                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4950                 subject)
4951           (setq subject
4952                 (concat (substring subject 0 (match-beginning 2))
4953                         (substring subject (match-end 0)))
4954                 changed t))
4955         (when (and changed
4956                    (string-match
4957                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4958           (setq subject
4959                 (concat (substring subject 0 (match-beginning 1))
4960                         (substring subject (match-end 1)))))
4961         (when changed
4962           (mail-header-set-subject header subject))))))
4963
4964 (defun gnus-fetch-headers (articles)
4965   "Fetch headers of ARTICLES."
4966   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4967     (gnus-message 5 "Fetching headers for %s..." name)
4968     (prog1
4969         (if (eq 'nov
4970                 (setq gnus-headers-retrieved-by
4971                       (gnus-retrieve-headers
4972                        articles gnus-newsgroup-name
4973                        ;; We might want to fetch old headers, but
4974                        ;; not if there is only 1 article.
4975                        (and (or (and
4976                                  (not (eq gnus-fetch-old-headers 'some))
4977                                  (not (numberp gnus-fetch-old-headers)))
4978                                 (> (length articles) 1))
4979                             gnus-fetch-old-headers))))
4980             (gnus-get-newsgroup-headers-xover
4981              articles nil nil gnus-newsgroup-name t)
4982           (gnus-get-newsgroup-headers))
4983       (gnus-message 5 "Fetching headers for %s...done" name))))
4984
4985 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4986   "Select newsgroup GROUP.
4987 If READ-ALL is non-nil, all articles in the group are selected.
4988 If SELECT-ARTICLES, only select those articles from GROUP."
4989   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4990          ;;!!! Dirty hack; should be removed.
4991          (gnus-summary-ignore-duplicates
4992           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4993               t
4994             gnus-summary-ignore-duplicates))
4995          (info (nth 2 entry))
4996          articles fetched-articles cached)
4997
4998     (unless (gnus-check-server
4999              (set (make-local-variable 'gnus-current-select-method)
5000                   (gnus-find-method-for-group group)))
5001       (error "Couldn't open server"))
5002
5003     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5004         (gnus-activate-group group)     ; Or we can activate it...
5005         (progn                          ; Or we bug out.
5006           (when (equal major-mode 'gnus-summary-mode)
5007             (gnus-kill-buffer (current-buffer)))
5008           (error "Couldn't activate group %s: %s"
5009                  group (gnus-status-message group))))
5010
5011     (unless (gnus-request-group group t)
5012       (when (equal major-mode 'gnus-summary-mode)
5013         (gnus-kill-buffer (current-buffer)))
5014       (error "Couldn't request group %s: %s"
5015              group (gnus-status-message group)))
5016
5017     (when gnus-agent
5018       ;; The agent may be storing articles that are no longer in the
5019       ;; server's active range.  If that is the case, the active range
5020       ;; needs to be expanded such that the agent's articles can be
5021       ;; included in the summary.
5022       (let* ((gnus-command-method (gnus-find-method-for-group group))
5023              (alist (gnus-agent-load-alist group))
5024              (active (gnus-active group)))
5025         (if (and (car alist)
5026                  (< (caar alist) (car active)))
5027             (gnus-set-active group (cons (caar alist) (cdr active))))))
5028
5029     (setq gnus-newsgroup-name group
5030           gnus-newsgroup-unselected nil
5031           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5032
5033     (let ((display (gnus-group-find-parameter group 'display)))
5034       (setq gnus-newsgroup-display
5035             (cond
5036              ((not (zerop (or (car-safe read-all) 0)))
5037               ;; The user entered the group with C-u SPC/RET, let's show
5038               ;; all articles.
5039               'gnus-not-ignore)
5040              ((eq display 'all)
5041               'gnus-not-ignore)
5042              ((arrayp display)
5043               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5044              ((numberp display)
5045               ;; The following is probably the "correct" solution, but
5046               ;; it makes Gnus fetch all headers and then limit the
5047               ;; articles (which is slow), so instead we hack the
5048               ;; select-articles parameter instead. -- Simon Josefsson
5049               ;; <jas@kth.se>
5050               ;;
5051               ;; (gnus-byte-compile
5052               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5053               ;;                         display)))))
5054               (setq select-articles
5055                     (gnus-uncompress-range
5056                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5057                              (if (> tmp 0)
5058                                  tmp
5059                                1))
5060                            (cdr (gnus-active group)))))
5061               nil)
5062              (t
5063               nil))))
5064
5065     (gnus-summary-setup-default-charset)
5066
5067     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5068     (when (gnus-virtual-group-p group)
5069       (setq cached gnus-newsgroup-cached))
5070
5071     (setq gnus-newsgroup-unreads
5072           (gnus-sorted-ndifference
5073            (gnus-sorted-ndifference gnus-newsgroup-unreads
5074                                     gnus-newsgroup-marked)
5075            gnus-newsgroup-dormant))
5076
5077     (setq gnus-newsgroup-processable nil)
5078
5079     (gnus-update-read-articles group gnus-newsgroup-unreads)
5080
5081     ;; Adjust and set lists of article marks.
5082     (when info
5083       (gnus-adjust-marked-articles info))
5084     (if (setq articles select-articles)
5085         (setq gnus-newsgroup-unselected
5086               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5087       (setq articles (gnus-articles-to-read group read-all)))
5088
5089     (cond
5090      ((null articles)
5091       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5092       'quit)
5093      ((eq articles 0) nil)
5094      (t
5095       ;; Init the dependencies hash table.
5096       (setq gnus-newsgroup-dependencies
5097             (gnus-make-hashtable (length articles)))
5098       (gnus-set-global-variables)
5099       ;; Retrieve the headers and read them in.
5100
5101       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5102
5103       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5104       (when cached
5105         (setq gnus-newsgroup-cached cached))
5106
5107       ;; Suppress duplicates?
5108       (when gnus-suppress-duplicates
5109         (gnus-dup-suppress-articles))
5110
5111       ;; Set the initial limit.
5112       (setq gnus-newsgroup-limit (copy-sequence articles))
5113       ;; Remove canceled articles from the list of unread articles.
5114       (setq fetched-articles
5115             (mapcar (lambda (headers) (mail-header-number headers))
5116                     gnus-newsgroup-headers))
5117       (setq gnus-newsgroup-articles fetched-articles)
5118       (setq gnus-newsgroup-unreads
5119             (gnus-sorted-nintersection
5120              gnus-newsgroup-unreads fetched-articles))
5121       (gnus-compute-unseen-list)
5122
5123       ;; Removed marked articles that do not exist.
5124       (gnus-update-missing-marks
5125        (gnus-sorted-difference articles fetched-articles))
5126       ;; We might want to build some more threads first.
5127       (when (and gnus-fetch-old-headers
5128                  (eq gnus-headers-retrieved-by 'nov))
5129         (if (eq gnus-fetch-old-headers 'invisible)
5130             (gnus-build-all-threads)
5131           (gnus-build-old-threads)))
5132       ;; Let the Gnus agent mark articles as read.
5133       (when gnus-agent
5134         (gnus-agent-get-undownloaded-list))
5135       ;; Remove list identifiers from subject
5136       (when gnus-list-identifiers
5137         (gnus-summary-remove-list-identifiers))
5138       ;; Check whether auto-expire is to be done in this group.
5139       (setq gnus-newsgroup-auto-expire
5140             (gnus-group-auto-expirable-p group))
5141       ;; Set up the article buffer now, if necessary.
5142       (unless gnus-single-article-buffer
5143         (gnus-article-setup-buffer))
5144       ;; First and last article in this newsgroup.
5145       (when gnus-newsgroup-headers
5146         (setq gnus-newsgroup-begin
5147               (mail-header-number (car gnus-newsgroup-headers))
5148               gnus-newsgroup-end
5149               (mail-header-number
5150                (gnus-last-element gnus-newsgroup-headers))))
5151       ;; GROUP is successfully selected.
5152       (or gnus-newsgroup-headers t)))))
5153
5154 (defun gnus-compute-unseen-list ()
5155   ;; The `seen' marks are treated specially.
5156   (if (not gnus-newsgroup-seen)
5157       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5158     (setq gnus-newsgroup-unseen
5159           (gnus-inverse-list-range-intersection
5160            gnus-newsgroup-articles gnus-newsgroup-seen))))
5161
5162 (defun gnus-summary-display-make-predicate (display)
5163   (require 'gnus-agent)
5164   (when (= (length display) 1)
5165     (setq display (car display)))
5166   (unless gnus-summary-display-cache
5167     (dolist (elem (append '((unread . unread)
5168                             (read . read)
5169                             (unseen . unseen))
5170                           gnus-article-mark-lists))
5171       (push (cons (cdr elem)
5172                   (gnus-byte-compile
5173                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5174             gnus-summary-display-cache)))
5175   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5176         (gnus-category-predicate-cache gnus-summary-display-cache))
5177     (gnus-get-predicate display)))
5178
5179 ;; Uses the dynamically bound `number' variable.
5180 (eval-when-compile
5181   (defvar number))
5182 (defun gnus-article-marked-p (type &optional article)
5183   (let ((article (or article number)))
5184     (cond
5185      ((eq type 'tick)
5186       (memq article gnus-newsgroup-marked))
5187      ((eq type 'spam)
5188       (memq article gnus-newsgroup-spam-marked))
5189      ((eq type 'unsend)
5190       (memq article gnus-newsgroup-unsendable))
5191      ((eq type 'undownload)
5192       (memq article gnus-newsgroup-undownloaded))
5193      ((eq type 'download)
5194       (memq article gnus-newsgroup-downloadable))
5195      ((eq type 'unread)
5196       (memq article gnus-newsgroup-unreads))
5197      ((eq type 'read)
5198       (memq article gnus-newsgroup-reads))
5199      ((eq type 'dormant)
5200       (memq article gnus-newsgroup-dormant) )
5201      ((eq type 'expire)
5202       (memq article gnus-newsgroup-expirable))
5203      ((eq type 'reply)
5204       (memq article gnus-newsgroup-replied))
5205      ((eq type 'killed)
5206       (memq article gnus-newsgroup-killed))
5207      ((eq type 'bookmark)
5208       (assq article gnus-newsgroup-bookmarks))
5209      ((eq type 'score)
5210       (assq article gnus-newsgroup-scored))
5211      ((eq type 'save)
5212       (memq article gnus-newsgroup-saved))
5213      ((eq type 'cache)
5214       (memq article gnus-newsgroup-cached))
5215      ((eq type 'forward)
5216       (memq article gnus-newsgroup-forwarded))
5217      ((eq type 'seen)
5218       (not (memq article gnus-newsgroup-unseen)))
5219      ((eq type 'recent)
5220       (memq article gnus-newsgroup-recent))
5221      (t t))))
5222
5223 (defun gnus-articles-to-read (group &optional read-all)
5224   "Find out what articles the user wants to read."
5225   (let* ((display (gnus-group-find-parameter group 'display))
5226          (articles
5227           ;; Select all articles if `read-all' is non-nil, or if there
5228           ;; are no unread articles.
5229           (if (or read-all
5230                   (and (zerop (length gnus-newsgroup-marked))
5231                        (zerop (length gnus-newsgroup-unreads)))
5232                   ;; Fetch all if the predicate is non-nil.
5233                   gnus-newsgroup-display)
5234               ;; We want to select the headers for all the articles in
5235               ;; the group, so we select either all the active
5236               ;; articles in the group, or (if that's nil), the
5237               ;; articles in the cache.
5238               (or
5239                (gnus-uncompress-range (gnus-active group))
5240                (gnus-cache-articles-in-group group))
5241             ;; Select only the "normal" subset of articles.
5242             (gnus-sorted-nunion
5243              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5244              gnus-newsgroup-unreads)))
5245          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5246          (scored (length scored-list))
5247          (number (length articles))
5248          (marked (+ (length gnus-newsgroup-marked)
5249                     (length gnus-newsgroup-dormant)))
5250          (select
5251           (cond
5252            ((numberp read-all)
5253             read-all)
5254            ((numberp gnus-newsgroup-display)
5255             gnus-newsgroup-display)
5256            (t
5257             (condition-case ()
5258                 (cond
5259                  ((and (or (<= scored marked) (= scored number))
5260                        (numberp gnus-large-newsgroup)
5261                        (> number gnus-large-newsgroup))
5262                   (let* ((cursor-in-echo-area nil)
5263                          (initial (gnus-parameter-large-newsgroup-initial
5264                                    gnus-newsgroup-name))
5265                          (input
5266                           (read-string
5267                            (format
5268                             "How many articles from %s (%s %d): "
5269                             (gnus-limit-string
5270                              (gnus-group-decoded-name gnus-newsgroup-name)
5271                              35)
5272                             (if initial "max" "default")
5273                             number)
5274                            (if initial
5275                                (cons (number-to-string initial)
5276                                      0)))))
5277                     (if (string-match "^[ \t]*$" input) number input)))
5278                  ((and (> scored marked) (< scored number)
5279                        (> (- scored number) 20))
5280                   (let ((input
5281                          (read-string
5282                           (format "%s %s (%d scored, %d total): "
5283                                   "How many articles from"
5284                                   (gnus-group-decoded-name group)
5285                                   scored number))))
5286                     (if (string-match "^[ \t]*$" input)
5287                         number input)))
5288                  (t number))
5289               (quit
5290                (message "Quit getting the articles to read")
5291                nil))))))
5292     (setq select (if (stringp select) (string-to-number select) select))
5293     (if (or (null select) (zerop select))
5294         select
5295       (if (and (not (zerop scored)) (<= (abs select) scored))
5296           (progn
5297             (setq articles (sort scored-list '<))
5298             (setq number (length articles)))
5299         (setq articles (copy-sequence articles)))
5300
5301       (when (< (abs select) number)
5302         (if (< select 0)
5303             ;; Select the N oldest articles.
5304             (setcdr (nthcdr (1- (abs select)) articles) nil)
5305           ;; Select the N most recent articles.
5306           (setq articles (nthcdr (- number select) articles))))
5307       (setq gnus-newsgroup-unselected
5308             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5309       (when gnus-alter-articles-to-read-function
5310         (setq articles
5311               (sort
5312                (funcall gnus-alter-articles-to-read-function
5313                         gnus-newsgroup-name articles)
5314                '<)))
5315       articles)))
5316
5317 (defun gnus-killed-articles (killed articles)
5318   (let (out)
5319     (while articles
5320       (when (inline (gnus-member-of-range (car articles) killed))
5321         (push (car articles) out))
5322       (setq articles (cdr articles)))
5323     out))
5324
5325 (defun gnus-uncompress-marks (marks)
5326   "Uncompress the mark ranges in MARKS."
5327   (let ((uncompressed '(score bookmark))
5328         out)
5329     (while marks
5330       (if (memq (caar marks) uncompressed)
5331           (push (car marks) out)
5332         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5333       (setq marks (cdr marks)))
5334     out))
5335
5336 (defun gnus-article-mark-to-type (mark)
5337   "Return the type of MARK."
5338   (or (cadr (assq mark gnus-article-special-mark-lists))
5339       'list))
5340
5341 (defun gnus-article-unpropagatable-p (mark)
5342   "Return whether MARK should be propagated to backend."
5343   (memq mark gnus-article-unpropagated-mark-lists))
5344
5345 (defun gnus-adjust-marked-articles (info)
5346   "Set all article lists and remove all marks that are no longer valid."
5347   (let* ((marked-lists (gnus-info-marks info))
5348          (active (gnus-active (gnus-info-group info)))
5349          (min (car active))
5350          (max (cdr active))
5351          (types gnus-article-mark-lists)
5352          marks var articles article mark mark-type)
5353
5354     (dolist (marks marked-lists)
5355       (setq mark (car marks)
5356             mark-type (gnus-article-mark-to-type mark)
5357             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5358
5359       ;; We set the variable according to the type of the marks list,
5360       ;; and then adjust the marks to a subset of the active articles.
5361       (cond
5362        ;; Adjust "simple" lists.
5363        ((eq mark-type 'list)
5364         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5365         (when (memq mark '(tick dormant expire reply save))
5366           (while articles
5367             (when (or (< (setq article (pop articles)) min) (> article max))
5368               (set var (delq article (symbol-value var)))))))
5369        ;; Adjust assocs.
5370        ((eq mark-type 'tuple)
5371         (set var (setq articles (cdr marks)))
5372         (when (not (listp (cdr (symbol-value var))))
5373           (set var (list (symbol-value var))))
5374         (when (not (listp (cdr articles)))
5375           (setq articles (list articles)))
5376         (while articles
5377           (when (or (not (consp (setq article (pop articles))))
5378                     (< (car article) min)
5379                     (> (car article) max))
5380             (set var (delq article (symbol-value var))))))
5381        ;; Adjust ranges (sloppily).
5382        ((eq mark-type 'range)
5383         (cond
5384          ((eq mark 'seen)
5385           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5386           ;; It should be (seen (NUM1 . NUM2)).
5387           (when (numberp (cddr marks))
5388             (setcdr marks (list (cdr marks))))
5389           (setq articles (cdr marks))
5390           (while (and articles
5391                       (or (and (consp (car articles))
5392                                (> min (cdar articles)))
5393                           (and (numberp (car articles))
5394                                (> min (car articles)))))
5395             (pop articles))
5396           (set var articles))))))))
5397
5398 (defun gnus-update-missing-marks (missing)
5399   "Go through the list of MISSING articles and remove them from the mark lists."
5400   (when missing
5401     (let (var m)
5402       ;; Go through all types.
5403       (dolist (elem gnus-article-mark-lists)
5404         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5405           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5406           (when (symbol-value var)
5407             ;; This list has articles.  So we delete all missing
5408             ;; articles from it.
5409             (setq m missing)
5410             (while m
5411               (set var (delq (pop m) (symbol-value var))))))))))
5412
5413 (defun gnus-update-marks ()
5414   "Enter the various lists of marked articles into the newsgroup info list."
5415   (let ((types gnus-article-mark-lists)
5416         (info (gnus-get-info gnus-newsgroup-name))
5417         type list newmarked symbol delta-marks)
5418     (when info
5419       ;; Add all marks lists to the list of marks lists.
5420       (while (setq type (pop types))
5421         (setq list (symbol-value
5422                     (setq symbol
5423                           (intern (format "gnus-newsgroup-%s" (car type))))))
5424
5425         (when list
5426           ;; Get rid of the entries of the articles that have the
5427           ;; default score.
5428           (when (and (eq (cdr type) 'score)
5429                      gnus-save-score
5430                      list)
5431             (let* ((arts list)
5432                    (prev (cons nil list))
5433                    (all prev))
5434               (while arts
5435                 (if (or (not (consp (car arts)))
5436                         (= (cdar arts) gnus-summary-default-score))
5437                     (setcdr prev (cdr arts))
5438                   (setq prev arts))
5439                 (setq arts (cdr arts)))
5440               (setq list (cdr all)))))
5441
5442         (when (eq (cdr type) 'seen)
5443           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5444
5445         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5446           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5447
5448         (when (and (gnus-check-backend-function
5449                     'request-set-mark gnus-newsgroup-name)
5450                    (not (gnus-article-unpropagatable-p (cdr type))))
5451           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5452                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5453                  (add (gnus-remove-from-range
5454                        (gnus-copy-sequence list) old)))
5455             (when add
5456               (push (list add 'add (list (cdr type))) delta-marks))
5457             (when del
5458               (push (list del 'del (list (cdr type))) delta-marks))))
5459
5460         (when list
5461           (push (cons (cdr type) list) newmarked)))
5462
5463       (when delta-marks
5464         (unless (gnus-check-group gnus-newsgroup-name)
5465           (error "Can't open server for %s" gnus-newsgroup-name))
5466         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5467
5468       ;; Enter these new marks into the info of the group.
5469       (if (nthcdr 3 info)
5470           (setcar (nthcdr 3 info) newmarked)
5471         ;; Add the marks lists to the end of the info.
5472         (when newmarked
5473           (setcdr (nthcdr 2 info) (list newmarked))))
5474
5475       ;; Cut off the end of the info if there's nothing else there.
5476       (let ((i 5))
5477         (while (and (> i 2)
5478                     (not (nth i info)))
5479           (when (nthcdr (decf i) info)
5480             (setcdr (nthcdr i info) nil)))))))
5481
5482 (defun gnus-set-mode-line (where)
5483   "Set the mode line of the article or summary buffers.
5484 If WHERE is `summary', the summary mode line format will be used."
5485   ;; Is this mode line one we keep updated?
5486   (when (and (memq where gnus-updated-mode-lines)
5487              (symbol-value
5488               (intern (format "gnus-%s-mode-line-format-spec" where))))
5489     (let (mode-string)
5490       (save-excursion
5491         ;; We evaluate this in the summary buffer since these
5492         ;; variables are buffer-local to that buffer.
5493         (set-buffer gnus-summary-buffer)
5494        ;; We bind all these variables that are used in the `eval' form
5495         ;; below.
5496         (let* ((mformat (symbol-value
5497                          (intern
5498                           (format "gnus-%s-mode-line-format-spec" where))))
5499                (gnus-tmp-group-name (gnus-group-decoded-name
5500                                      gnus-newsgroup-name))
5501                (gnus-tmp-article-number (or gnus-current-article 0))
5502                (gnus-tmp-unread gnus-newsgroup-unreads)
5503                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5504                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5505                (gnus-tmp-unread-and-unselected
5506                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5507                             (zerop gnus-tmp-unselected))
5508                        "")
5509                       ((zerop gnus-tmp-unselected)
5510                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5511                       (t (format "{%d(+%d) more}"
5512                                  gnus-tmp-unread-and-unticked
5513                                  gnus-tmp-unselected))))
5514                (gnus-tmp-subject
5515                 (if (and gnus-current-headers
5516                          (vectorp gnus-current-headers))
5517                     (gnus-mode-string-quote
5518                      (mail-header-subject gnus-current-headers))
5519                   ""))
5520                bufname-length max-len
5521                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5522           (setq mode-string (eval mformat))
5523           (setq bufname-length (if (string-match "%b" mode-string)
5524                                    (- (length
5525                                        (buffer-name
5526                                         (if (eq where 'summary)
5527                                             nil
5528                                           (get-buffer gnus-article-buffer))))
5529                                       2)
5530                                  0))
5531           (setq max-len (max 4 (if gnus-mode-non-string-length
5532                                    (- (window-width)
5533                                       gnus-mode-non-string-length
5534                                       bufname-length)
5535                                  (length mode-string))))
5536           ;; We might have to chop a bit of the string off...
5537           (when (> (length mode-string) max-len)
5538             (setq mode-string
5539                   (concat (truncate-string-to-width mode-string (- max-len 3))
5540                           "...")))
5541           ;; Pad the mode string a bit.
5542           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5543       ;; Update the mode line.
5544       (setq mode-line-buffer-identification
5545             (gnus-mode-line-buffer-identification (list mode-string)))
5546       (set-buffer-modified-p t))))
5547
5548 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5549   "Go through the HEADERS list and add all Xrefs to a hash table.
5550 The resulting hash table is returned, or nil if no Xrefs were found."
5551   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5552          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5553          (xref-hashtb (gnus-make-hashtable))
5554          start group entry number xrefs header)
5555     (while headers
5556       (setq header (pop headers))
5557       (when (and (setq xrefs (mail-header-xref header))
5558                  (not (memq (setq number (mail-header-number header))
5559                             unreads)))
5560         (setq start 0)
5561         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5562           (setq start (match-end 0))
5563           (setq group (if prefix
5564                           (concat prefix (substring xrefs (match-beginning 1)
5565                                                     (match-end 1)))
5566                         (substring xrefs (match-beginning 1) (match-end 1))))
5567           (setq number
5568                 (string-to-int (substring xrefs (match-beginning 2)
5569                                           (match-end 2))))
5570           (if (setq entry (gnus-gethash group xref-hashtb))
5571               (setcdr entry (cons number (cdr entry)))
5572             (gnus-sethash group (cons number nil) xref-hashtb)))))
5573     (and start xref-hashtb)))
5574
5575 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5576   "Look through all the headers and mark the Xrefs as read."
5577   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5578         name entry info xref-hashtb idlist method nth4)
5579     (save-excursion
5580       (set-buffer gnus-group-buffer)
5581       (when (setq xref-hashtb
5582                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5583         (mapatoms
5584          (lambda (group)
5585            (unless (string= from-newsgroup (setq name (symbol-name group)))
5586              (setq idlist (symbol-value group))
5587              ;; Dead groups are not updated.
5588              (and (prog1
5589                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5590                             info (nth 2 entry))
5591                     (when (stringp (setq nth4 (gnus-info-method info)))
5592                       (setq nth4 (gnus-server-to-method nth4))))
5593                   ;; Only do the xrefs if the group has the same
5594                   ;; select method as the group we have just read.
5595                   (or (gnus-methods-equal-p
5596                        nth4 (gnus-find-method-for-group from-newsgroup))
5597                       virtual
5598                       (equal nth4 (setq method (gnus-find-method-for-group
5599                                                 from-newsgroup)))
5600                       (and (equal (car nth4) (car method))
5601                            (equal (nth 1 nth4) (nth 1 method))))
5602                   gnus-use-cross-reference
5603                   (or (not (eq gnus-use-cross-reference t))
5604                       virtual
5605                       ;; Only do cross-references on subscribed
5606                       ;; groups, if that is what is wanted.
5607                       (<= (gnus-info-level info) gnus-level-subscribed))
5608                   (gnus-group-make-articles-read name idlist))))
5609          xref-hashtb)))))
5610
5611 (defun gnus-compute-read-articles (group articles)
5612   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5613          (info (nth 2 entry))
5614          (active (gnus-active group))
5615          ninfo)
5616     (when entry
5617       ;; First peel off all invalid article numbers.
5618       (when active
5619         (let ((ids articles)
5620               id first)
5621           (while (setq id (pop ids))
5622             (when (and first (> id (cdr active)))
5623               ;; We'll end up in this situation in one particular
5624               ;; obscure situation.  If you re-scan a group and get
5625               ;; a new article that is cross-posted to a different
5626               ;; group that has not been re-scanned, you might get
5627               ;; crossposted article that has a higher number than
5628               ;; Gnus believes possible.  So we re-activate this
5629               ;; group as well.  This might mean doing the
5630               ;; crossposting thingy will *increase* the number
5631               ;; of articles in some groups.  Tsk, tsk.
5632               (setq active (or (gnus-activate-group group) active)))
5633             (when (or (> id (cdr active))
5634                       (< id (car active)))
5635               (setq articles (delq id articles))))))
5636       ;; If the read list is nil, we init it.
5637       (if (and active
5638                (null (gnus-info-read info))
5639                (> (car active) 1))
5640           (setq ninfo (cons 1 (1- (car active))))
5641         (setq ninfo (gnus-info-read info)))
5642       ;; Then we add the read articles to the range.
5643       (gnus-add-to-range
5644        ninfo (setq articles (sort articles '<))))))
5645
5646 (defun gnus-group-make-articles-read (group articles)
5647   "Update the info of GROUP to say that ARTICLES are read."
5648   (let* ((num 0)
5649          (entry (gnus-gethash group gnus-newsrc-hashtb))
5650          (info (nth 2 entry))
5651          (active (gnus-active group))
5652          range)
5653     (when entry
5654       (setq range (gnus-compute-read-articles group articles))
5655       (save-excursion
5656         (set-buffer gnus-group-buffer)
5657         (gnus-undo-register
5658           `(progn
5659              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5660              (gnus-info-set-read ',info ',(gnus-info-read info))
5661              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5662              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5663              (gnus-group-update-group ,group t))))
5664       ;; Add the read articles to the range.
5665       (gnus-info-set-read info range)
5666       (gnus-request-set-mark group (list (list range 'add '(read))))
5667       ;; Then we have to re-compute how many unread
5668       ;; articles there are in this group.
5669       (when active
5670         (cond
5671          ((not range)
5672           (setq num (- (1+ (cdr active)) (car active))))
5673          ((not (listp (cdr range)))
5674           (setq num (- (cdr active) (- (1+ (cdr range))
5675                                        (car range)))))
5676          (t
5677           (while range
5678             (if (numberp (car range))
5679                 (setq num (1+ num))
5680               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5681             (setq range (cdr range)))
5682           (setq num (- (cdr active) num))))
5683         ;; Update the number of unread articles.
5684         (setcar entry num)
5685         ;; Update the group buffer.
5686         (unless (gnus-ephemeral-group-p group)
5687           (gnus-group-update-group group t))))))
5688
5689 (defvar gnus-newsgroup-none-id 0)
5690
5691 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5692   (let ((cur nntp-server-buffer)
5693         (dependencies
5694          (or dependencies
5695              (save-excursion (set-buffer gnus-summary-buffer)
5696                              gnus-newsgroup-dependencies)))
5697         headers id end ref
5698         (mail-parse-charset gnus-newsgroup-charset)
5699         (mail-parse-ignored-charsets
5700          (save-excursion (condition-case nil
5701                              (set-buffer gnus-summary-buffer)
5702                            (error))
5703                          gnus-newsgroup-ignored-charsets)))
5704     (save-excursion
5705       (set-buffer nntp-server-buffer)
5706       ;; Translate all TAB characters into SPACE characters.
5707       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5708       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5709       (gnus-run-hooks 'gnus-parse-headers-hook)
5710       (let ((case-fold-search t)
5711             in-reply-to header p lines chars)
5712         (goto-char (point-min))
5713         ;; Search to the beginning of the next header.  Error messages
5714         ;; do not begin with 2 or 3.
5715         (while (re-search-forward "^[23][0-9]+ " nil t)
5716           (setq id nil
5717                 ref nil)
5718           ;; This implementation of this function, with nine
5719           ;; search-forwards instead of the one re-search-forward and
5720           ;; a case (which basically was the old function) is actually
5721           ;; about twice as fast, even though it looks messier.  You
5722           ;; can't have everything, I guess.  Speed and elegance
5723           ;; doesn't always go hand in hand.
5724           (setq
5725            header
5726            (vector
5727             ;; Number.
5728             (prog1
5729                 (read cur)
5730               (end-of-line)
5731               (setq p (point))
5732               (narrow-to-region (point)
5733                                 (or (and (search-forward "\n.\n" nil t)
5734                                          (- (point) 2))
5735                                     (point))))
5736             ;; Subject.
5737             (progn
5738               (goto-char p)
5739               (if (search-forward "\nsubject:" nil t)
5740                   (funcall gnus-decode-encoded-word-function
5741                            (nnheader-header-value))
5742                 "(none)"))
5743             ;; From.
5744             (progn
5745               (goto-char p)
5746               (if (search-forward "\nfrom:" nil t)
5747                   (funcall gnus-decode-encoded-word-function
5748                            (nnheader-header-value))
5749                 "(nobody)"))
5750             ;; Date.
5751             (progn
5752               (goto-char p)
5753               (if (search-forward "\ndate:" nil t)
5754                   (nnheader-header-value) ""))
5755             ;; Message-ID.
5756             (progn
5757               (goto-char p)
5758               (setq id (if (re-search-forward
5759                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5760                            ;; We do it this way to make sure the Message-ID
5761                            ;; is (somewhat) syntactically valid.
5762                            (buffer-substring (match-beginning 1)
5763                                              (match-end 1))
5764                          ;; If there was no message-id, we just fake one
5765                          ;; to make subsequent routines simpler.
5766                          (nnheader-generate-fake-message-id))))
5767             ;; References.
5768             (progn
5769               (goto-char p)
5770               (if (search-forward "\nreferences:" nil t)
5771                   (progn
5772                     (setq end (point))
5773                     (prog1
5774                         (nnheader-header-value)
5775                       (setq ref
5776                             (buffer-substring
5777                              (progn
5778                                (end-of-line)
5779                                (search-backward ">" end t)
5780                                (1+ (point)))
5781                              (progn
5782                                (search-backward "<" end t)
5783                                (point))))))
5784                 ;; Get the references from the in-reply-to header if there
5785                 ;; were no references and the in-reply-to header looks
5786                 ;; promising.
5787                 (if (and (search-forward "\nin-reply-to:" nil t)
5788                          (setq in-reply-to (nnheader-header-value))
5789                          (string-match "<[^>]+>" in-reply-to))
5790                     (let (ref2)
5791                       (setq ref (substring in-reply-to (match-beginning 0)
5792                                            (match-end 0)))
5793                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5794                         (setq ref2 (substring in-reply-to (match-beginning 0)
5795                                               (match-end 0)))
5796                         (when (> (length ref2) (length ref))
5797                           (setq ref ref2)))
5798                       ref)
5799                   (setq ref nil))))
5800             ;; Chars.
5801             (progn
5802               (goto-char p)
5803               (if (search-forward "\nchars: " nil t)
5804                   (if (numberp (setq chars (ignore-errors (read cur))))
5805                       chars -1)
5806                 -1))
5807             ;; Lines.
5808             (progn
5809               (goto-char p)
5810               (if (search-forward "\nlines: " nil t)
5811                   (if (numberp (setq lines (ignore-errors (read cur))))
5812                       lines -1)
5813                 -1))
5814             ;; Xref.
5815             (progn
5816               (goto-char p)
5817               (and (search-forward "\nxref:" nil t)
5818                    (nnheader-header-value)))
5819             ;; Extra.
5820             (when gnus-extra-headers
5821               (let ((extra gnus-extra-headers)
5822                     out)
5823                 (while extra
5824                   (goto-char p)
5825                   (when (search-forward
5826                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5827                     (push (cons (car extra) (nnheader-header-value))
5828                           out))
5829                   (pop extra))
5830                 out))))
5831           (when (equal id ref)
5832             (setq ref nil))
5833
5834           (when gnus-alter-header-function
5835             (funcall gnus-alter-header-function header)
5836             (setq id (mail-header-id header)
5837                   ref (gnus-parent-id (mail-header-references header))))
5838
5839           (when (setq header
5840                       (gnus-dependencies-add-header
5841                        header dependencies force-new))
5842             (push header headers))
5843           (goto-char (point-max))
5844           (widen))
5845         (nreverse headers)))))
5846
5847 ;; Goes through the xover lines and returns a list of vectors
5848 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5849                                                   force-new dependencies
5850                                                   group also-fetch-heads)
5851   "Parse the news overview data in the server buffer.
5852 Return a list of headers that match SEQUENCE (see
5853 `nntp-retrieve-headers')."
5854   ;; Get the Xref when the users reads the articles since most/some
5855   ;; NNTP servers do not include Xrefs when using XOVER.
5856   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5857   (let ((mail-parse-charset gnus-newsgroup-charset)
5858         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5859         (cur nntp-server-buffer)
5860         (dependencies (or dependencies gnus-newsgroup-dependencies))
5861         (allp (cond
5862                ((eq gnus-read-all-available-headers t)
5863                 t)
5864                ((stringp gnus-read-all-available-headers)
5865                 (string-match gnus-read-all-available-headers group))
5866                (t
5867                 nil)))
5868         number headers header)
5869     (save-excursion
5870       (set-buffer nntp-server-buffer)
5871       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5872       ;; Allow the user to mangle the headers before parsing them.
5873       (gnus-run-hooks 'gnus-parse-headers-hook)
5874       (goto-char (point-min))
5875       (gnus-parse-without-error
5876         (while (and (or sequence allp)
5877                     (not (eobp)))
5878           (setq number (read cur))
5879           (when (not allp)
5880             (while (and sequence
5881                         (< (car sequence) number))
5882               (setq sequence (cdr sequence))))
5883           (when (and (or allp
5884                          (and sequence
5885                               (eq number (car sequence))))
5886                      (progn
5887                        (setq sequence (cdr sequence))
5888                        (setq header (inline
5889                                       (gnus-nov-parse-line
5890                                        number dependencies force-new)))))
5891             (push header headers))
5892           (forward-line 1)))
5893       ;; A common bug in inn is that if you have posted an article and
5894       ;; then retrieves the active file, it will answer correctly --
5895       ;; the new article is included.  However, a NOV entry for the
5896       ;; article may not have been generated yet, so this may fail.
5897       ;; We work around this problem by retrieving the last few
5898       ;; headers using HEAD.
5899       (if (or (not also-fetch-heads)
5900               (not sequence))
5901           ;; We (probably) got all the headers.
5902           (nreverse headers)
5903         (let ((gnus-nov-is-evil t))
5904           (nconc
5905            (nreverse headers)
5906            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5907              (gnus-get-newsgroup-headers))))))))
5908
5909 (defun gnus-article-get-xrefs ()
5910   "Fill in the Xref value in `gnus-current-headers', if necessary.
5911 This is meant to be called in `gnus-article-internal-prepare-hook'."
5912   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5913                                  gnus-current-headers)))
5914     (or (not gnus-use-cross-reference)
5915         (not headers)
5916         (and (mail-header-xref headers)
5917              (not (string= (mail-header-xref headers) "")))
5918         (let ((case-fold-search t)
5919               xref)
5920           (save-restriction
5921             (nnheader-narrow-to-headers)
5922             (goto-char (point-min))
5923             (when (or (and (not (eobp))
5924                            (eq (downcase (char-after)) ?x)
5925                            (looking-at "Xref:"))
5926                       (search-forward "\nXref:" nil t))
5927               (goto-char (1+ (match-end 0)))
5928               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5929               (mail-header-set-xref headers xref)))))))
5930
5931 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5932   "Find article ID and insert the summary line for that article.
5933 OLD-HEADER can either be a header or a line number to insert
5934 the subject line on."
5935   (let* ((line (and (numberp old-header) old-header))
5936          (old-header (and (vectorp old-header) old-header))
5937          (header (cond ((and old-header use-old-header)
5938                         old-header)
5939                        ((and (numberp id)
5940                              (gnus-number-to-header id))
5941                         (gnus-number-to-header id))
5942                        (t
5943                         (gnus-read-header id))))
5944          (number (and (numberp id) id))
5945          d)
5946     (when header
5947       ;; Rebuild the thread that this article is part of and go to the
5948       ;; article we have fetched.
5949       (when (and (not gnus-show-threads)
5950                  old-header)
5951         (when (and number
5952                    (setq d (gnus-data-find (mail-header-number old-header))))
5953           (goto-char (gnus-data-pos d))
5954           (gnus-data-remove
5955            number
5956            (- (gnus-point-at-bol)
5957               (prog1
5958                   (1+ (gnus-point-at-eol))
5959                 (gnus-delete-line))))))
5960       (when old-header
5961         (mail-header-set-number header (mail-header-number old-header)))
5962       (setq gnus-newsgroup-sparse
5963             (delq (setq number (mail-header-number header))
5964                   gnus-newsgroup-sparse))
5965       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5966       (push number gnus-newsgroup-limit)
5967       (gnus-rebuild-thread (mail-header-id header) line)
5968       (gnus-summary-goto-subject number nil t))
5969     (when (and (numberp number)
5970                (> number 0))
5971       ;; We have to update the boundaries even if we can't fetch the
5972       ;; article if ID is a number -- so that the next `P' or `N'
5973       ;; command will fetch the previous (or next) article even
5974       ;; if the one we tried to fetch this time has been canceled.
5975       (when (> number gnus-newsgroup-end)
5976         (setq gnus-newsgroup-end number))
5977       (when (< number gnus-newsgroup-begin)
5978         (setq gnus-newsgroup-begin number))
5979       (setq gnus-newsgroup-unselected
5980             (delq number gnus-newsgroup-unselected)))
5981     ;; Report back a success?
5982     (and header (mail-header-number header))))
5983
5984 ;;; Process/prefix in the summary buffer
5985
5986 (defun gnus-summary-work-articles (n)
5987   "Return a list of articles to be worked upon.
5988 The prefix argument, the list of process marked articles, and the
5989 current article will be taken into consideration."
5990   (save-excursion
5991     (set-buffer gnus-summary-buffer)
5992     (cond
5993      (n
5994       ;; A numerical prefix has been given.
5995       (setq n (prefix-numeric-value n))
5996       (let ((backward (< n 0))
5997             (n (abs (prefix-numeric-value n)))
5998             articles article)
5999         (save-excursion
6000           (while
6001               (and (> n 0)
6002                    (push (setq article (gnus-summary-article-number))
6003                          articles)
6004                    (if backward
6005                        (gnus-summary-find-prev nil article)
6006                      (gnus-summary-find-next nil article)))
6007             (decf n)))
6008         (nreverse articles)))
6009      ((and (gnus-region-active-p) (mark))
6010       (message "region active")
6011       ;; Work on the region between point and mark.
6012       (let ((max (max (point) (mark)))
6013             articles article)
6014         (save-excursion
6015           (goto-char (min (min (point) (mark))))
6016           (while
6017               (and
6018                (push (setq article (gnus-summary-article-number)) articles)
6019                (gnus-summary-find-next nil article)
6020                (< (point) max)))
6021           (nreverse articles))))
6022      (gnus-newsgroup-processable
6023       ;; There are process-marked articles present.
6024       ;; Save current state.
6025       (gnus-summary-save-process-mark)
6026       ;; Return the list.
6027       (reverse gnus-newsgroup-processable))
6028      (t
6029       ;; Just return the current article.
6030       (list (gnus-summary-article-number))))))
6031
6032 (defmacro gnus-summary-iterate (arg &rest forms)
6033   "Iterate over the process/prefixed articles and do FORMS.
6034 ARG is the interactive prefix given to the command.  FORMS will be
6035 executed with point over the summary line of the articles."
6036   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6037     `(let ((,articles (gnus-summary-work-articles ,arg)))
6038        (while ,articles
6039          (gnus-summary-goto-subject (car ,articles))
6040          ,@forms
6041          (pop ,articles)))))
6042
6043 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6044 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6045
6046 (defun gnus-summary-save-process-mark ()
6047   "Push the current set of process marked articles on the stack."
6048   (interactive)
6049   (push (copy-sequence gnus-newsgroup-processable)
6050         gnus-newsgroup-process-stack))
6051
6052 (defun gnus-summary-kill-process-mark ()
6053   "Push the current set of process marked articles on the stack and unmark."
6054   (interactive)
6055   (gnus-summary-save-process-mark)
6056   (gnus-summary-unmark-all-processable))
6057
6058 (defun gnus-summary-yank-process-mark ()
6059   "Pop the last process mark state off the stack and restore it."
6060   (interactive)
6061   (unless gnus-newsgroup-process-stack
6062     (error "Empty mark stack"))
6063   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6064
6065 (defun gnus-summary-process-mark-set (set)
6066   "Make SET into the current process marked articles."
6067   (gnus-summary-unmark-all-processable)
6068   (while set
6069     (gnus-summary-set-process-mark (pop set))))
6070
6071 ;;; Searching and stuff
6072
6073 (defun gnus-summary-search-group (&optional backward use-level)
6074   "Search for next unread newsgroup.
6075 If optional argument BACKWARD is non-nil, search backward instead."
6076   (save-excursion
6077     (set-buffer gnus-group-buffer)
6078     (when (gnus-group-search-forward
6079            backward nil (if use-level (gnus-group-group-level) nil))
6080       (gnus-group-group-name))))
6081
6082 (defun gnus-summary-best-group (&optional exclude-group)
6083   "Find the name of the best unread group.
6084 If EXCLUDE-GROUP, do not go to this group."
6085   (save-excursion
6086     (set-buffer gnus-group-buffer)
6087     (save-excursion
6088       (gnus-group-best-unread-group exclude-group))))
6089
6090 (defun gnus-summary-find-next (&optional unread article backward)
6091   (if backward (gnus-summary-find-prev unread article)
6092     (let* ((dummy (gnus-summary-article-intangible-p))
6093            (article (or article (gnus-summary-article-number)))
6094            (data (gnus-data-find-list article))
6095            result)
6096       (when (and (not dummy)
6097                  (or (not gnus-summary-check-current)
6098                      (not unread)
6099                      (not (gnus-data-unread-p (car data)))))
6100         (setq data (cdr data)))
6101       (when (setq result
6102                   (if unread
6103                       (progn
6104                         (while data
6105                           (unless (memq (gnus-data-number (car data)) 
6106                                         (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6107                                                gnus-newsgroup-undownloaded)
6108                                               (gnus-plugged
6109                                                nil)
6110                                               ((eq gnus-auto-goto-ignores 'unfetched)
6111                                                gnus-newsgroup-unfetched)
6112                                               ((eq gnus-auto-goto-ignores 'undownloaded)
6113                                                gnus-newsgroup-undownloaded)))
6114                             (when (gnus-data-unread-p (car data))
6115                               (setq result (car data)
6116                                     data nil)))
6117                           (setq data (cdr data)))
6118                         result)
6119                     (car data)))
6120         (goto-char (gnus-data-pos result))
6121         (gnus-data-number result)))))
6122
6123 (defun gnus-summary-find-prev (&optional unread article)
6124   (let* ((eobp (eobp))
6125          (article (or article (gnus-summary-article-number)))
6126          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6127          result)
6128     (when (and (not eobp)
6129                (or (not gnus-summary-check-current)
6130                    (not unread)
6131                    (not (gnus-data-unread-p (car data)))))
6132       (setq data (cdr data)))
6133     (when (setq result
6134                 (if unread
6135                     (progn
6136                       (while data
6137                         (unless (memq (gnus-data-number (car data))
6138                                       (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6139                                              gnus-newsgroup-undownloaded)
6140                                             (gnus-plugged
6141                                              nil)
6142                                             ((eq gnus-auto-goto-ignores 'unfetched)
6143                                              gnus-newsgroup-unfetched)
6144                                             ((eq gnus-auto-goto-ignores 'undownloaded)
6145                                              gnus-newsgroup-undownloaded)))
6146                           (when (gnus-data-unread-p (car data))
6147                             (setq result (car data)
6148                                   data nil)))
6149                         (setq data (cdr data)))
6150                       result)
6151                   (car data)))
6152       (goto-char (gnus-data-pos result))
6153       (gnus-data-number result))))
6154
6155 (defun gnus-summary-find-subject (subject &optional unread backward article)
6156   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6157          (article (or article (gnus-summary-article-number)))
6158          (articles (gnus-data-list backward))
6159          (arts (gnus-data-find-list article articles))
6160          result)
6161     (when (or (not gnus-summary-check-current)
6162               (not unread)
6163               (not (gnus-data-unread-p (car arts))))
6164       (setq arts (cdr arts)))
6165     (while arts
6166       (and (or (not unread)
6167                (gnus-data-unread-p (car arts)))
6168            (vectorp (gnus-data-header (car arts)))
6169            (gnus-subject-equal
6170             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6171            (setq result (car arts)
6172                  arts nil))
6173       (setq arts (cdr arts)))
6174     (and result
6175          (goto-char (gnus-data-pos result))
6176          (gnus-data-number result))))
6177
6178 (defun gnus-summary-search-forward (&optional unread subject backward)
6179   "Search forward for an article.
6180 If UNREAD, look for unread articles.  If SUBJECT, look for
6181 articles with that subject.  If BACKWARD, search backward instead."
6182   (cond (subject (gnus-summary-find-subject subject unread backward))
6183         (backward (gnus-summary-find-prev unread))
6184         (t (gnus-summary-find-next unread))))
6185
6186 (defun gnus-recenter (&optional n)
6187   "Center point in window and redisplay frame.
6188 Also do horizontal recentering."
6189   (interactive "P")
6190   (when (and gnus-auto-center-summary
6191              (not (eq gnus-auto-center-summary 'vertical)))
6192     (gnus-horizontal-recenter))
6193   (recenter n))
6194
6195 (defun gnus-summary-recenter ()
6196   "Center point in the summary window.
6197 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6198 displayed, no centering will be performed."
6199   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6200   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6201   (interactive)
6202   ;; The user has to want it.
6203   (when gnus-auto-center-summary
6204     (let* ((top (cond ((< (window-height) 4) 0)
6205                       ((< (window-height) 7) 1)
6206                       (t (if (numberp gnus-auto-center-summary)
6207                              gnus-auto-center-summary
6208                            2))))
6209            (height (1- (window-height)))
6210            (bottom (save-excursion (goto-char (point-max))
6211                                    (forward-line (- height))
6212                                    (point)))
6213            (window (get-buffer-window (current-buffer))))
6214       (when (get-buffer-window gnus-article-buffer)
6215         ;; Only do recentering when the article buffer is displayed,
6216         ;; Set the window start to either `bottom', which is the biggest
6217         ;; possible valid number, or the second line from the top,
6218         ;; whichever is the least.
6219         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6220           (if (> bottom top-pos)
6221               ;; Keep the second line from the top visible
6222               (set-window-start window top-pos t)
6223             ;; Try to keep the bottom line visible; if it's partially
6224             ;; obscured, either scroll one more line to make it fully
6225             ;; visible, or revert to using TOP-POS.
6226             (save-excursion
6227               (goto-char (point-max))
6228               (forward-line -1)
6229               (let ((last-line-start (point)))
6230                 (goto-char bottom)
6231                 (set-window-start window (point) t)
6232                 (when (not (pos-visible-in-window-p last-line-start window))
6233                   (forward-line 1)
6234                   (set-window-start window (min (point) top-pos) t)))))))
6235       ;; Do horizontal recentering while we're at it.
6236       (when (and (get-buffer-window (current-buffer) t)
6237                  (not (eq gnus-auto-center-summary 'vertical)))
6238         (let ((selected (selected-window)))
6239           (select-window (get-buffer-window (current-buffer) t))
6240           (gnus-summary-position-point)
6241           (gnus-horizontal-recenter)
6242           (select-window selected))))))
6243
6244 (defun gnus-summary-jump-to-group (newsgroup)
6245   "Move point to NEWSGROUP in group mode buffer."
6246   ;; Keep update point of group mode buffer if visible.
6247   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6248       (save-window-excursion
6249         ;; Take care of tree window mode.
6250         (when (get-buffer-window gnus-group-buffer)
6251           (pop-to-buffer gnus-group-buffer))
6252         (gnus-group-jump-to-group newsgroup))
6253     (save-excursion
6254       ;; Take care of tree window mode.
6255       (if (get-buffer-window gnus-group-buffer)
6256           (pop-to-buffer gnus-group-buffer)
6257         (set-buffer gnus-group-buffer))
6258       (gnus-group-jump-to-group newsgroup))))
6259
6260 ;; This function returns a list of article numbers based on the
6261 ;; difference between the ranges of read articles in this group and
6262 ;; the range of active articles.
6263 (defun gnus-list-of-unread-articles (group)
6264   (let* ((read (gnus-info-read (gnus-get-info group)))
6265          (active (or (gnus-active group) (gnus-activate-group group)))
6266          (last (cdr active))
6267          first nlast unread)
6268     ;; If none are read, then all are unread.
6269     (if (not read)
6270         (setq first (car active))
6271       ;; If the range of read articles is a single range, then the
6272       ;; first unread article is the article after the last read
6273       ;; article.  Sounds logical, doesn't it?
6274       (if (and (not (listp (cdr read)))
6275                (or (< (car read) (car active))
6276                    (progn (setq read (list read))
6277                           nil)))
6278           (setq first (max (car active) (1+ (cdr read))))
6279         ;; `read' is a list of ranges.
6280         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6281                                   (caar read)))
6282                   1)
6283           (setq first (car active)))
6284         (while read
6285           (when first
6286             (while (< first nlast)
6287               (push first unread)
6288               (setq first (1+ first))))
6289           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6290           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6291           (setq read (cdr read)))))
6292     ;; And add the last unread articles.
6293     (while (<= first last)
6294       (push first unread)
6295       (setq first (1+ first)))
6296     ;; Return the list of unread articles.
6297     (delq 0 (nreverse unread))))
6298
6299 (defun gnus-list-of-read-articles (group)
6300   "Return a list of unread, unticked and non-dormant articles."
6301   (let* ((info (gnus-get-info group))
6302          (marked (gnus-info-marks info))
6303          (active (gnus-active group)))
6304     (and info active
6305          (gnus-list-range-difference
6306           (gnus-list-range-difference
6307            (gnus-sorted-complement
6308             (gnus-uncompress-range active)
6309             (gnus-list-of-unread-articles group))
6310            (cdr (assq 'dormant marked)))
6311           (cdr (assq 'tick marked))))))
6312
6313 ;; Various summary commands
6314
6315 (defun gnus-summary-select-article-buffer ()
6316   "Reconfigure windows to show article buffer."
6317   (interactive)
6318   (if (not (gnus-buffer-live-p gnus-article-buffer))
6319       (error "There is no article buffer for this summary buffer")
6320     (gnus-configure-windows 'article)
6321     (select-window (get-buffer-window gnus-article-buffer))))
6322
6323 (defun gnus-summary-universal-argument (arg)
6324   "Perform any operation on all articles that are process/prefixed."
6325   (interactive "P")
6326   (let ((articles (gnus-summary-work-articles arg))
6327         func article)
6328     (if (eq
6329          (setq
6330           func
6331           (key-binding
6332            (read-key-sequence
6333             (substitute-command-keys
6334              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6335          'undefined)
6336         (gnus-error 1 "Undefined key")
6337       (save-excursion
6338         (while articles
6339           (gnus-summary-goto-subject (setq article (pop articles)))
6340           (let (gnus-newsgroup-processable)
6341             (command-execute func))
6342           (gnus-summary-remove-process-mark article)))))
6343   (gnus-summary-position-point))
6344
6345 (defun gnus-summary-toggle-truncation (&optional arg)
6346   "Toggle truncation of summary lines.
6347 With arg, turn line truncation on if arg is positive."
6348   (interactive "P")
6349   (setq truncate-lines
6350         (if (null arg) (not truncate-lines)
6351           (> (prefix-numeric-value arg) 0)))
6352   (redraw-display))
6353
6354 (defun gnus-summary-find-for-reselect ()
6355   "Return the number of an article to stay on across a reselect.
6356 The current article is considered, then following articles, then previous
6357 articles.  An article is sought which is not cancelled and isn't a temporary
6358 insertion from another group.  If there's no such then return a dummy 0."
6359   (let (found)
6360     (dolist (rev '(nil t))
6361       (unless found      ; don't demand the reverse list if we don't need it
6362         (let ((data (gnus-data-find-list
6363                      (gnus-summary-article-number) (gnus-data-list rev))))
6364           (while (and data (not found))
6365             (if (and (< 0 (gnus-data-number (car data)))
6366                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6367                 (setq found (gnus-data-number (car data))))
6368             (setq data (cdr data))))))
6369     (or found 0)))
6370
6371 (defun gnus-summary-reselect-current-group (&optional all rescan)
6372   "Exit and then reselect the current newsgroup.
6373 The prefix argument ALL means to select all articles."
6374   (interactive "P")
6375   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6376     (error "Ephemeral groups can't be reselected"))
6377   (let ((current-subject (gnus-summary-find-for-reselect))
6378         (group gnus-newsgroup-name))
6379     (setq gnus-newsgroup-begin nil)
6380     (gnus-summary-exit)
6381     ;; We have to adjust the point of group mode buffer because
6382     ;; point was moved to the next unread newsgroup by exiting.
6383     (gnus-summary-jump-to-group group)
6384     (when rescan
6385       (save-excursion
6386         (gnus-group-get-new-news-this-group 1)))
6387     (gnus-group-read-group all t)
6388     (gnus-summary-goto-subject current-subject nil t)))
6389
6390 (defun gnus-summary-rescan-group (&optional all)
6391   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6392   (interactive "P")
6393   (gnus-summary-reselect-current-group all t))
6394
6395 (defun gnus-summary-update-info (&optional non-destructive)
6396   (save-excursion
6397     (let ((group gnus-newsgroup-name))
6398       (when group
6399         (when gnus-newsgroup-kill-headers
6400           (setq gnus-newsgroup-killed
6401                 (gnus-compress-sequence
6402                  (gnus-sorted-union
6403                   (gnus-list-range-intersection
6404                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6405                   gnus-newsgroup-unreads)
6406                  t)))
6407         (unless (listp (cdr gnus-newsgroup-killed))
6408           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6409         (let ((headers gnus-newsgroup-headers))
6410           ;; Set the new ranges of read articles.
6411           (save-excursion
6412             (set-buffer gnus-group-buffer)
6413             (gnus-undo-force-boundary))
6414           (gnus-update-read-articles
6415            group (gnus-sorted-union
6416                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6417           ;; Set the current article marks.
6418           (let ((gnus-newsgroup-scored
6419                  (if (and (not gnus-save-score)
6420                           (not non-destructive))
6421                      nil
6422                    gnus-newsgroup-scored)))
6423             (save-excursion
6424               (gnus-update-marks)))
6425           ;; Do the cross-ref thing.
6426           (when gnus-use-cross-reference
6427             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6428           ;; Do not switch windows but change the buffer to work.
6429           (set-buffer gnus-group-buffer)
6430           (unless (gnus-ephemeral-group-p group)
6431             (gnus-group-update-group group)))))))
6432
6433 (defun gnus-summary-save-newsrc (&optional force)
6434   "Save the current number of read/marked articles in the dribble buffer.
6435 The dribble buffer will then be saved.
6436 If FORCE (the prefix), also save the .newsrc file(s)."
6437   (interactive "P")
6438   (gnus-summary-update-info t)
6439   (if force
6440       (gnus-save-newsrc-file)
6441     (gnus-dribble-save)))
6442
6443 (defun gnus-summary-exit (&optional temporary)
6444   "Exit reading current newsgroup, and then return to group selection mode.
6445 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6446   (interactive)
6447   (gnus-set-global-variables)
6448   (when (gnus-buffer-live-p gnus-article-buffer)
6449     (save-excursion
6450       (set-buffer gnus-article-buffer)
6451       (mm-destroy-parts gnus-article-mime-handles)
6452       ;; Set it to nil for safety reason.
6453       (setq gnus-article-mime-handle-alist nil)
6454       (setq gnus-article-mime-handles nil)))
6455   (gnus-kill-save-kill-buffer)
6456   (gnus-async-halt-prefetch)
6457   (let* ((group gnus-newsgroup-name)
6458          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6459          (gnus-group-is-exiting-p t)
6460          (mode major-mode)
6461          (group-point nil)
6462          (buf (current-buffer)))
6463     (unless quit-config
6464       ;; Do adaptive scoring, and possibly save score files.
6465       (when gnus-newsgroup-adaptive
6466         (gnus-score-adaptive))
6467       (when gnus-use-scoring
6468         (gnus-score-save)))
6469     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6470     ;; If we have several article buffers, we kill them at exit.
6471     (unless gnus-single-article-buffer
6472       (gnus-kill-buffer gnus-original-article-buffer)
6473       (setq gnus-article-current nil))
6474     (when gnus-use-cache
6475       (gnus-cache-possibly-remove-articles)
6476       (gnus-cache-save-buffers))
6477     (gnus-async-prefetch-remove-group group)
6478     (when gnus-suppress-duplicates
6479       (gnus-dup-enter-articles))
6480     (when gnus-use-trees
6481       (gnus-tree-close group))
6482     (when gnus-use-cache
6483       (gnus-cache-write-active))
6484     ;; Remove entries for this group.
6485     (nnmail-purge-split-history (gnus-group-real-name group))
6486     ;; Make all changes in this group permanent.
6487     (unless quit-config
6488       (gnus-run-hooks 'gnus-exit-group-hook)
6489       (gnus-summary-update-info))
6490     (gnus-close-group group)
6491     ;; Make sure where we were, and go to next newsgroup.
6492     (set-buffer gnus-group-buffer)
6493     (unless quit-config
6494       (gnus-group-jump-to-group group))
6495     (gnus-run-hooks 'gnus-summary-exit-hook)
6496     (unless (or quit-config
6497                 ;; If this group has disappeared from the summary
6498                 ;; buffer, don't skip forwards.
6499                 (not (string= group (gnus-group-group-name))))
6500       (gnus-group-next-unread-group 1))
6501     (setq group-point (point))
6502     (if temporary
6503         nil                             ;Nothing to do.
6504       ;; If we have several article buffers, we kill them at exit.
6505       (unless gnus-single-article-buffer
6506         (gnus-kill-buffer gnus-article-buffer)
6507         (gnus-kill-buffer gnus-original-article-buffer)
6508         (setq gnus-article-current nil))
6509       (set-buffer buf)
6510       (if (not gnus-kill-summary-on-exit)
6511           (progn
6512             (gnus-deaden-summary)
6513             (setq mode nil))
6514         ;; We set all buffer-local variables to nil.  It is unclear why
6515         ;; this is needed, but if we don't, buffer-local variables are
6516         ;; not garbage-collected, it seems.  This would the lead to en
6517         ;; ever-growing Emacs.
6518         (gnus-summary-clear-local-variables)
6519         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6520           (gnus-summary-clear-local-variables))
6521         (when (get-buffer gnus-article-buffer)
6522           (bury-buffer gnus-article-buffer))
6523         ;; We clear the global counterparts of the buffer-local
6524         ;; variables as well, just to be on the safe side.
6525         (set-buffer gnus-group-buffer)
6526         (gnus-summary-clear-local-variables)
6527         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6528           (gnus-summary-clear-local-variables))
6529         ;; Return to group mode buffer.
6530         (when (eq mode 'gnus-summary-mode)
6531           (gnus-kill-buffer buf)))
6532       (setq gnus-current-select-method gnus-select-method)
6533       (pop-to-buffer gnus-group-buffer)
6534       (if (not quit-config)
6535           (progn
6536             (goto-char group-point)
6537             (gnus-configure-windows 'group 'force))
6538         (gnus-handle-ephemeral-exit quit-config))
6539       ;; Clear the current group name.
6540       (unless quit-config
6541         (setq gnus-newsgroup-name nil)))))
6542
6543 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6544 (defun gnus-summary-exit-no-update (&optional no-questions)
6545   "Quit reading current newsgroup without updating read article info."
6546   (interactive)
6547   (let* ((group gnus-newsgroup-name)
6548          (gnus-group-is-exiting-p t)
6549          (gnus-group-is-exiting-without-update-p t)
6550          (quit-config (gnus-group-quit-config group)))
6551     (when (or no-questions
6552               gnus-expert-user
6553               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6554       (gnus-async-halt-prefetch)
6555       (run-hooks 'gnus-summary-prepare-exit-hook)
6556       (when (gnus-buffer-live-p gnus-article-buffer)
6557         (save-excursion
6558           (set-buffer gnus-article-buffer)
6559           (mm-destroy-parts gnus-article-mime-handles)
6560           ;; Set it to nil for safety reason.
6561           (setq gnus-article-mime-handle-alist nil)
6562           (setq gnus-article-mime-handles nil)))
6563       ;; If we have several article buffers, we kill them at exit.
6564       (unless gnus-single-article-buffer
6565         (gnus-kill-buffer gnus-article-buffer)
6566         (gnus-kill-buffer gnus-original-article-buffer)
6567         (setq gnus-article-current nil))
6568       (if (not gnus-kill-summary-on-exit)
6569           (gnus-deaden-summary)
6570         (gnus-close-group group)
6571         (gnus-summary-clear-local-variables)
6572         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6573           (gnus-summary-clear-local-variables))
6574         (set-buffer gnus-group-buffer)
6575         (gnus-summary-clear-local-variables)
6576         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6577           (gnus-summary-clear-local-variables))
6578         (gnus-kill-buffer gnus-summary-buffer))
6579       (unless gnus-single-article-buffer
6580         (setq gnus-article-current nil))
6581       (when gnus-use-trees
6582         (gnus-tree-close group))
6583       (gnus-async-prefetch-remove-group group)
6584       (when (get-buffer gnus-article-buffer)
6585         (bury-buffer gnus-article-buffer))
6586       ;; Return to the group buffer.
6587       (gnus-configure-windows 'group 'force)
6588       ;; Clear the current group name.
6589       (setq gnus-newsgroup-name nil)
6590       (unless (gnus-ephemeral-group-p group)
6591         (gnus-group-update-group group))
6592       (when (equal (gnus-group-group-name) group)
6593         (gnus-group-next-unread-group 1))
6594       (when quit-config
6595         (gnus-handle-ephemeral-exit quit-config)))))
6596
6597 (defun gnus-handle-ephemeral-exit (quit-config)
6598   "Handle movement when leaving an ephemeral group.
6599 The state which existed when entering the ephemeral is reset."
6600   (if (not (buffer-name (car quit-config)))
6601       (gnus-configure-windows 'group 'force)
6602     (set-buffer (car quit-config))
6603     (cond ((eq major-mode 'gnus-summary-mode)
6604            (gnus-set-global-variables))
6605           ((eq major-mode 'gnus-article-mode)
6606            (save-excursion
6607              ;; The `gnus-summary-buffer' variable may point
6608              ;; to the old summary buffer when using a single
6609              ;; article buffer.
6610              (unless (gnus-buffer-live-p gnus-summary-buffer)
6611                (set-buffer gnus-group-buffer))
6612              (set-buffer gnus-summary-buffer)
6613              (gnus-set-global-variables))))
6614     (if (or (eq (cdr quit-config) 'article)
6615             (eq (cdr quit-config) 'pick))
6616         (progn
6617           ;; The current article may be from the ephemeral group
6618           ;; thus it is best that we reload this article
6619           (gnus-summary-show-article)
6620           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6621               (gnus-configure-windows 'pick 'force)
6622             (gnus-configure-windows (cdr quit-config) 'force)))
6623       (gnus-configure-windows (cdr quit-config) 'force))
6624     (when (eq major-mode 'gnus-summary-mode)
6625       (gnus-summary-next-subject 1 nil t)
6626       (gnus-summary-recenter)
6627       (gnus-summary-position-point))))
6628
6629 ;;; Dead summaries.
6630
6631 (defvar gnus-dead-summary-mode-map nil)
6632
6633 (unless gnus-dead-summary-mode-map
6634   (setq gnus-dead-summary-mode-map (make-keymap))
6635   (suppress-keymap gnus-dead-summary-mode-map)
6636   (substitute-key-definition
6637    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6638   (dolist (key '("\C-d" "\r" "\177" [delete]))
6639     (define-key gnus-dead-summary-mode-map
6640       key 'gnus-summary-wake-up-the-dead))
6641   (dolist (key '("q" "Q"))
6642     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6643
6644 (defvar gnus-dead-summary-mode nil
6645   "Minor mode for Gnus summary buffers.")
6646
6647 (defun gnus-dead-summary-mode (&optional arg)
6648   "Minor mode for Gnus summary buffers."
6649   (interactive "P")
6650   (when (eq major-mode 'gnus-summary-mode)
6651     (make-local-variable 'gnus-dead-summary-mode)
6652     (setq gnus-dead-summary-mode
6653           (if (null arg) (not gnus-dead-summary-mode)
6654             (> (prefix-numeric-value arg) 0)))
6655     (when gnus-dead-summary-mode
6656       (gnus-add-minor-mode
6657        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6658
6659 (defun gnus-deaden-summary ()
6660   "Make the current summary buffer into a dead summary buffer."
6661   ;; Kill any previous dead summary buffer.
6662   (when (and gnus-dead-summary
6663              (buffer-name gnus-dead-summary))
6664     (save-excursion
6665       (set-buffer gnus-dead-summary)
6666       (when gnus-dead-summary-mode
6667         (kill-buffer (current-buffer)))))
6668   ;; Make this the current dead summary.
6669   (setq gnus-dead-summary (current-buffer))
6670   (gnus-dead-summary-mode 1)
6671   (let ((name (buffer-name)))
6672     (when (string-match "Summary" name)
6673       (rename-buffer
6674        (concat (substring name 0 (match-beginning 0)) "Dead "
6675                (substring name (match-beginning 0)))
6676        t)
6677       (bury-buffer))))
6678
6679 (defun gnus-kill-or-deaden-summary (buffer)
6680   "Kill or deaden the summary BUFFER."
6681   (save-excursion
6682     (when (and (buffer-name buffer)
6683                (not gnus-single-article-buffer))
6684       (save-excursion
6685         (set-buffer buffer)
6686         (gnus-kill-buffer gnus-article-buffer)
6687         (gnus-kill-buffer gnus-original-article-buffer)))
6688     (cond
6689      ;; Kill the buffer.
6690      (gnus-kill-summary-on-exit
6691       (when (and gnus-use-trees
6692                  (gnus-buffer-exists-p buffer))
6693         (save-excursion
6694           (set-buffer buffer)
6695           (gnus-tree-close gnus-newsgroup-name)))
6696       (gnus-kill-buffer buffer))
6697      ;; Deaden the buffer.
6698      ((gnus-buffer-exists-p buffer)
6699       (save-excursion
6700         (set-buffer buffer)
6701         (gnus-deaden-summary))))))
6702
6703 (defun gnus-summary-wake-up-the-dead (&rest args)
6704   "Wake up the dead summary buffer."
6705   (interactive)
6706   (gnus-dead-summary-mode -1)
6707   (let ((name (buffer-name)))
6708     (when (string-match "Dead " name)
6709       (rename-buffer
6710        (concat (substring name 0 (match-beginning 0))
6711                (substring name (match-end 0)))
6712        t)))
6713   (gnus-message 3 "This dead summary is now alive again"))
6714
6715 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6716 (defun gnus-summary-fetch-faq (&optional faq-dir)
6717   "Fetch the FAQ for the current group.
6718 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6719 in."
6720   (interactive
6721    (list
6722     (when current-prefix-arg
6723       (completing-read
6724        "FAQ dir: " (and (listp gnus-group-faq-directory)
6725                         (mapcar (lambda (file) (list file))
6726                                 gnus-group-faq-directory))))))
6727   (let (gnus-faq-buffer)
6728     (when (setq gnus-faq-buffer
6729                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6730       (gnus-configure-windows 'summary-faq))))
6731
6732 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6733 (defun gnus-summary-describe-group (&optional force)
6734   "Describe the current newsgroup."
6735   (interactive "P")
6736   (gnus-group-describe-group force gnus-newsgroup-name))
6737
6738 (defun gnus-summary-describe-briefly ()
6739   "Describe summary mode commands briefly."
6740   (interactive)
6741   (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")))
6742
6743 ;; Walking around group mode buffer from summary mode.
6744
6745 (defun gnus-summary-next-group (&optional no-article target-group backward)
6746   "Exit current newsgroup and then select next unread newsgroup.
6747 If prefix argument NO-ARTICLE is non-nil, no article is selected
6748 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6749 previous group instead."
6750   (interactive "P")
6751   ;; Stop pre-fetching.
6752   (gnus-async-halt-prefetch)
6753   (let ((current-group gnus-newsgroup-name)
6754         (current-buffer (current-buffer))
6755         entered)
6756     ;; First we semi-exit this group to update Xrefs and all variables.
6757     ;; We can't do a real exit, because the window conf must remain
6758     ;; the same in case the user is prompted for info, and we don't
6759     ;; want the window conf to change before that...
6760     (gnus-summary-exit t)
6761     (while (not entered)
6762       ;; Then we find what group we are supposed to enter.
6763       (set-buffer gnus-group-buffer)
6764       (gnus-group-jump-to-group current-group)
6765       (setq target-group
6766             (or target-group
6767                 (if (eq gnus-keep-same-level 'best)
6768                     (gnus-summary-best-group gnus-newsgroup-name)
6769                   (gnus-summary-search-group backward gnus-keep-same-level))))
6770       (if (not target-group)
6771           ;; There are no further groups, so we return to the group
6772           ;; buffer.
6773           (progn
6774             (gnus-message 5 "Returning to the group buffer")
6775             (setq entered t)
6776             (when (gnus-buffer-live-p current-buffer)
6777               (set-buffer current-buffer)
6778               (gnus-summary-exit))
6779             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6780         ;; We try to enter the target group.
6781         (gnus-group-jump-to-group target-group)
6782         (let ((unreads (gnus-group-group-unread)))
6783           (if (and (or (eq t unreads)
6784                        (and unreads (not (zerop unreads))))
6785                    (gnus-summary-read-group
6786                     target-group nil no-article
6787                     (and (buffer-name current-buffer) current-buffer)
6788                     nil backward))
6789               (setq entered t)
6790             (setq current-group target-group
6791                   target-group nil)))))))
6792
6793 (defun gnus-summary-prev-group (&optional no-article)
6794   "Exit current newsgroup and then select previous unread newsgroup.
6795 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6796   (interactive "P")
6797   (gnus-summary-next-group no-article nil t))
6798
6799 ;; Walking around summary lines.
6800
6801 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6802   "Go to the first subject satisfying any non-nil constraint.
6803 If UNREAD is non-nil, the article should be unread.
6804 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6805 If UNSEED is non-nil, the article should be unseen.
6806 Returns the article selected or nil if there are no matching articles."
6807   (interactive "P")
6808   (cond
6809    ;; Empty summary.
6810    ((null gnus-newsgroup-data)
6811     (gnus-message 3 "No articles in the group")
6812     nil)
6813    ;; Pick the first article.
6814    ((not (or unread undownloaded unseen))
6815     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6816     (gnus-data-number (car gnus-newsgroup-data)))
6817    ;; Find the first unread article.
6818    (t
6819     (let ((data gnus-newsgroup-data))
6820       (while (and data
6821                   (let ((num (gnus-data-number (car data))))
6822                     (or (memq num gnus-newsgroup-unfetched)
6823                         (not (or (and unread
6824                                       (memq num gnus-newsgroup-unreads))
6825                                  (and undownloaded
6826                                       (memq num gnus-newsgroup-undownloaded))
6827                                  (and unseen
6828                                       (memq num gnus-newsgroup-unseen)))))))
6829         (setq data (cdr data)))
6830       (prog1 
6831           (if data
6832               (progn
6833                 (goto-char (gnus-data-pos (car data)))
6834                 (gnus-data-number (car data)))
6835             (gnus-message 3 "No more%s articles"
6836                           (let* ((r (when unread " unread"))
6837                                  (d (when undownloaded " undownloaded"))
6838                                  (s (when unseen " unseen"))
6839                                  (l (delq nil (list r d s))))
6840                             (cond ((= 3 (length l))
6841                                    (concat r "," d ", or" s))
6842                                   ((= 2 (length l))
6843                                    (concat (car l) ", or" (cadr l)))
6844                                   ((= 1 (length l))
6845                                    (car l))
6846                                   (t
6847                                    ""))))
6848             nil
6849             )
6850         (gnus-summary-position-point))))))
6851
6852 (defun gnus-summary-next-subject (n &optional unread dont-display)
6853   "Go to next N'th summary line.
6854 If N is negative, go to the previous N'th subject line.
6855 If UNREAD is non-nil, only unread articles are selected.
6856 The difference between N and the actual number of steps taken is
6857 returned."
6858   (interactive "p")
6859   (let ((backward (< n 0))
6860         (n (abs n)))
6861     (while (and (> n 0)
6862                 (if backward
6863                     (gnus-summary-find-prev unread)
6864                   (gnus-summary-find-next unread)))
6865       (unless (zerop (setq n (1- n)))
6866         (gnus-summary-show-thread)))
6867     (when (/= 0 n)
6868       (gnus-message 7 "No more%s articles"
6869                     (if unread " unread" "")))
6870     (unless dont-display
6871       (gnus-summary-recenter)
6872       (gnus-summary-position-point))
6873     n))
6874
6875 (defun gnus-summary-next-unread-subject (n)
6876   "Go to next N'th unread summary line."
6877   (interactive "p")
6878   (gnus-summary-next-subject n t))
6879
6880 (defun gnus-summary-prev-subject (n &optional unread)
6881   "Go to previous N'th summary line.
6882 If optional argument UNREAD is non-nil, only unread article is selected."
6883   (interactive "p")
6884   (gnus-summary-next-subject (- n) unread))
6885
6886 (defun gnus-summary-prev-unread-subject (n)
6887   "Go to previous N'th unread summary line."
6888   (interactive "p")
6889   (gnus-summary-next-subject (- n) t))
6890
6891 (defun gnus-summary-goto-subjects (articles)
6892   "Insert the subject header for ARTICLES in the current buffer."
6893   (save-excursion
6894     (dolist (article articles)
6895       (gnus-summary-goto-subject article t)))
6896   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6897   (gnus-summary-position-point))
6898  
6899 (defun gnus-summary-goto-subject (article &optional force silent)
6900   "Go the subject line of ARTICLE.
6901 If FORCE, also allow jumping to articles not currently shown."
6902   (interactive "nArticle number: ")
6903   (unless (numberp article)
6904     (error "Article %s is not a number" article))
6905   (let ((b (point))
6906         (data (gnus-data-find article)))
6907     ;; We read in the article if we have to.
6908     (and (not data)
6909          force
6910          (gnus-summary-insert-subject
6911           article
6912           (if (or (numberp force) (vectorp force)) force)
6913           t)
6914          (setq data (gnus-data-find article)))
6915     (goto-char b)
6916     (if (not data)
6917         (progn
6918           (unless silent
6919             (gnus-message 3 "Can't find article %d" article))
6920           nil)
6921       (let ((pt (gnus-data-pos data)))
6922         (goto-char pt)
6923         (gnus-summary-set-article-display-arrow pt))
6924       (gnus-summary-position-point)
6925       article)))
6926
6927 ;; Walking around summary lines with displaying articles.
6928
6929 (defun gnus-summary-expand-window (&optional arg)
6930   "Make the summary buffer take up the entire Emacs frame.
6931 Given a prefix, will force an `article' buffer configuration."
6932   (interactive "P")
6933   (if arg
6934       (gnus-configure-windows 'article 'force)
6935     (gnus-configure-windows 'summary 'force)))
6936
6937 (defun gnus-summary-display-article (article &optional all-header)
6938   "Display ARTICLE in article buffer."
6939   (when (gnus-buffer-live-p gnus-article-buffer)
6940     (with-current-buffer gnus-article-buffer
6941       (mm-enable-multibyte)))
6942   (gnus-set-global-variables)
6943   (when (gnus-buffer-live-p gnus-article-buffer)
6944     (with-current-buffer gnus-article-buffer
6945       (setq gnus-article-charset gnus-newsgroup-charset)
6946       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6947       (mm-enable-multibyte)))
6948   (if (null article)
6949       nil
6950     (prog1
6951         (if gnus-summary-display-article-function
6952             (funcall gnus-summary-display-article-function article all-header)
6953           (gnus-article-prepare article all-header))
6954       (gnus-run-hooks 'gnus-select-article-hook)
6955       (when (and gnus-current-article
6956                  (not (zerop gnus-current-article)))
6957         (gnus-summary-goto-subject gnus-current-article))
6958       (gnus-summary-recenter)
6959       (when (and gnus-use-trees gnus-show-threads)
6960         (gnus-possibly-generate-tree article)
6961         (gnus-highlight-selected-tree article))
6962       ;; Successfully display article.
6963       (gnus-article-set-window-start
6964        (cdr (assq article gnus-newsgroup-bookmarks))))))
6965
6966 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6967   "Select the current article.
6968 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6969 non-nil, the article will be re-fetched even if it already present in
6970 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6971 be displayed."
6972   ;; Make sure we are in the summary buffer to work around bbdb bug.
6973   (unless (eq major-mode 'gnus-summary-mode)
6974     (set-buffer gnus-summary-buffer))
6975   (let ((article (or article (gnus-summary-article-number)))
6976         (all-headers (not (not all-headers))) ;Must be t or nil.
6977         gnus-summary-display-article-function)
6978     (and (not pseudo)
6979          (gnus-summary-article-pseudo-p article)
6980          (error "This is a pseudo-article"))
6981     (save-excursion
6982       (set-buffer gnus-summary-buffer)
6983       (if (or (and gnus-single-article-buffer
6984                    (or (null gnus-current-article)
6985                        (null gnus-article-current)
6986                        (null (get-buffer gnus-article-buffer))
6987                        (not (eq article (cdr gnus-article-current)))
6988                        (not (equal (car gnus-article-current)
6989                                    gnus-newsgroup-name))))
6990               (and (not gnus-single-article-buffer)
6991                    (or (null gnus-current-article)
6992                        (not (eq gnus-current-article article))))
6993               force)
6994           ;; The requested article is different from the current article.
6995           (progn
6996             (gnus-summary-display-article article all-headers)
6997             (when (gnus-buffer-live-p gnus-article-buffer)
6998               (with-current-buffer gnus-article-buffer
6999                 (if (not gnus-article-decoded-p) ;; a local variable
7000                     (mm-disable-multibyte))))
7001             (gnus-article-set-window-start
7002              (cdr (assq article gnus-newsgroup-bookmarks)))
7003             article)
7004         'old))))
7005
7006 (defun gnus-summary-force-verify-and-decrypt ()
7007   "Display buttons for signed/encrypted parts and verify/decrypt them."
7008   (interactive)
7009   (let ((mm-verify-option 'known)
7010         (mm-decrypt-option 'known)
7011         (gnus-buttonized-mime-types (append (list "multipart/signed"
7012                                                   "multipart/encrypted")
7013                                             gnus-buttonized-mime-types)))
7014     (gnus-summary-select-article nil 'force)))
7015
7016 (defun gnus-summary-set-current-mark (&optional current-mark)
7017   "Obsolete function."
7018   nil)
7019
7020 (defun gnus-summary-next-article (&optional unread subject backward push)
7021   "Select the next article.
7022 If UNREAD, only unread articles are selected.
7023 If SUBJECT, only articles with SUBJECT are selected.
7024 If BACKWARD, the previous article is selected instead of the next."
7025   (interactive "P")
7026   (cond
7027    ;; Is there such an article?
7028    ((and (gnus-summary-search-forward unread subject backward)
7029          (or (gnus-summary-display-article (gnus-summary-article-number))
7030              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7031     (gnus-summary-position-point))
7032    ;; If not, we try the first unread, if that is wanted.
7033    ((and subject
7034          gnus-auto-select-same
7035          (gnus-summary-first-unread-article))
7036     (gnus-summary-position-point)
7037     (gnus-message 6 "Wrapped"))
7038    ;; Try to get next/previous article not displayed in this group.
7039    ((and gnus-auto-extend-newsgroup
7040          (not unread) (not subject))
7041     (gnus-summary-goto-article
7042      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7043      nil (count-lines (point-min) (point))))
7044    ;; Go to next/previous group.
7045    (t
7046     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7047       (gnus-summary-jump-to-group gnus-newsgroup-name))
7048     (let ((cmd last-command-char)
7049           (point
7050            (save-excursion
7051              (set-buffer gnus-group-buffer)
7052              (point)))
7053           (group
7054            (if (eq gnus-keep-same-level 'best)
7055                (gnus-summary-best-group gnus-newsgroup-name)
7056              (gnus-summary-search-group backward gnus-keep-same-level))))
7057       ;; For some reason, the group window gets selected.  We change
7058       ;; it back.
7059       (select-window (get-buffer-window (current-buffer)))
7060       ;; Select next unread newsgroup automagically.
7061       (cond
7062        ((or (not gnus-auto-select-next)
7063             (not cmd))
7064         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7065        ((or (eq gnus-auto-select-next 'quietly)
7066             (and (eq gnus-auto-select-next 'slightly-quietly)
7067                  push)
7068             (and (eq gnus-auto-select-next 'almost-quietly)
7069                  (gnus-summary-last-article-p)))
7070         ;; Select quietly.
7071         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7072             (gnus-summary-exit)
7073           (gnus-message 7 "No more%s articles (%s)..."
7074                         (if unread " unread" "")
7075                         (if group (concat "selecting " group)
7076                           "exiting"))
7077           (gnus-summary-next-group nil group backward)))
7078        (t
7079         (when (gnus-key-press-event-p last-input-event)
7080           (gnus-summary-walk-group-buffer
7081            gnus-newsgroup-name cmd unread backward point))))))))
7082
7083 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7084   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7085                       (?\C-p (gnus-group-prev-unread-group 1))))
7086         (cursor-in-echo-area t)
7087         keve key group ended prompt)
7088     (save-excursion
7089       (set-buffer gnus-group-buffer)
7090       (goto-char start)
7091       (setq group
7092             (if (eq gnus-keep-same-level 'best)
7093                 (gnus-summary-best-group gnus-newsgroup-name)
7094               (gnus-summary-search-group backward gnus-keep-same-level))))
7095     (while (not ended)
7096       (setq prompt
7097             (format
7098              "No more%s articles%s " (if unread " unread" "")
7099              (if (and group
7100                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7101                  (format " (Type %s for %s [%s])"
7102                          (single-key-description cmd) group
7103                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7104                (format " (Type %s to exit %s)"
7105                        (single-key-description cmd)
7106                        gnus-newsgroup-name))))
7107       ;; Confirm auto selection.
7108       (setq key (car (setq keve (gnus-read-event-char prompt)))
7109             ended t)
7110       (cond
7111        ((assq key keystrokes)
7112         (let ((obuf (current-buffer)))
7113           (switch-to-buffer gnus-group-buffer)
7114           (when group
7115             (gnus-group-jump-to-group group))
7116           (eval (cadr (assq key keystrokes)))
7117           (setq group (gnus-group-group-name))
7118           (switch-to-buffer obuf))
7119         (setq ended nil))
7120        ((equal key cmd)
7121         (if (or (not group)
7122                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7123             (gnus-summary-exit)
7124           (gnus-summary-next-group nil group backward)))
7125        (t
7126         (push (cdr keve) unread-command-events))))))
7127
7128 (defun gnus-summary-next-unread-article ()
7129   "Select unread article after current one."
7130   (interactive)
7131   (gnus-summary-next-article
7132    (or (not (eq gnus-summary-goto-unread 'never))
7133        (gnus-summary-last-article-p (gnus-summary-article-number)))
7134    (and gnus-auto-select-same
7135         (gnus-summary-article-subject))))
7136
7137 (defun gnus-summary-prev-article (&optional unread subject)
7138   "Select the article after the current one.
7139 If UNREAD is non-nil, only unread articles are selected."
7140   (interactive "P")
7141   (gnus-summary-next-article unread subject t))
7142
7143 (defun gnus-summary-prev-unread-article ()
7144   "Select unread article before current one."
7145   (interactive)
7146   (gnus-summary-prev-article
7147    (or (not (eq gnus-summary-goto-unread 'never))
7148        (gnus-summary-first-article-p (gnus-summary-article-number)))
7149    (and gnus-auto-select-same
7150         (gnus-summary-article-subject))))
7151
7152 (defun gnus-summary-next-page (&optional lines circular stop)
7153   "Show next page of the selected article.
7154 If at the end of the current article, select the next article.
7155 LINES says how many lines should be scrolled up.
7156
7157 If CIRCULAR is non-nil, go to the start of the article instead of
7158 selecting the next article when reaching the end of the current
7159 article.
7160
7161 If STOP is non-nil, just stop when reaching the end of the message.
7162
7163 Also see the variable `gnus-article-skip-boring'."
7164   (interactive "P")
7165   (setq gnus-summary-buffer (current-buffer))
7166   (gnus-set-global-variables)
7167   (let ((article (gnus-summary-article-number))
7168         (article-window (get-buffer-window gnus-article-buffer t))
7169         endp)
7170     ;; If the buffer is empty, we have no article.
7171     (unless article
7172       (error "No article to select"))
7173     (gnus-configure-windows 'article)
7174     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7175         (if (and (eq gnus-summary-goto-unread 'never)
7176                  (not (gnus-summary-last-article-p article)))
7177             (gnus-summary-next-article)
7178           (gnus-summary-next-unread-article))
7179       (if (or (null gnus-current-article)
7180               (null gnus-article-current)
7181               (/= article (cdr gnus-article-current))
7182               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7183           ;; Selected subject is different from current article's.
7184           (gnus-summary-display-article article)
7185         (when article-window
7186           (gnus-eval-in-buffer-window gnus-article-buffer
7187             (setq endp (or (gnus-article-next-page lines)
7188                            (gnus-article-only-boring-p))))
7189           (when endp
7190             (cond (stop
7191                    (gnus-message 3 "End of message"))
7192                   (circular
7193                    (gnus-summary-beginning-of-article))
7194                   (lines
7195                    (gnus-message 3 "End of message"))
7196                   ((null lines)
7197                    (if (and (eq gnus-summary-goto-unread 'never)
7198                             (not (gnus-summary-last-article-p article)))
7199                        (gnus-summary-next-article)
7200                      (gnus-summary-next-unread-article))))))))
7201     (gnus-summary-recenter)
7202     (gnus-summary-position-point)))
7203
7204 (defun gnus-summary-prev-page (&optional lines move)
7205   "Show previous page of selected article.
7206 Argument LINES specifies lines to be scrolled down.
7207 If MOVE, move to the previous unread article if point is at
7208 the beginning of the buffer."
7209   (interactive "P")
7210   (let ((article (gnus-summary-article-number))
7211         (article-window (get-buffer-window gnus-article-buffer t))
7212         endp)
7213     (gnus-configure-windows 'article)
7214     (if (or (null gnus-current-article)
7215             (null gnus-article-current)
7216             (/= article (cdr gnus-article-current))
7217             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7218         ;; Selected subject is different from current article's.
7219         (gnus-summary-display-article article)
7220       (gnus-summary-recenter)
7221       (when article-window
7222         (gnus-eval-in-buffer-window gnus-article-buffer
7223           (setq endp (gnus-article-prev-page lines)))
7224         (when (and move endp)
7225           (cond (lines
7226                  (gnus-message 3 "Beginning of message"))
7227                 ((null lines)
7228                  (if (and (eq gnus-summary-goto-unread 'never)
7229                           (not (gnus-summary-first-article-p article)))
7230                      (gnus-summary-prev-article)
7231                    (gnus-summary-prev-unread-article))))))))
7232   (gnus-summary-position-point))
7233
7234 (defun gnus-summary-prev-page-or-article (&optional lines)
7235   "Show previous page of selected article.
7236 Argument LINES specifies lines to be scrolled down.
7237 If at the beginning of the article, go to the next article."
7238   (interactive "P")
7239   (gnus-summary-prev-page lines t))
7240
7241 (defun gnus-summary-scroll-up (lines)
7242   "Scroll up (or down) one line current article.
7243 Argument LINES specifies lines to be scrolled up (or down if negative)."
7244   (interactive "p")
7245   (gnus-configure-windows 'article)
7246   (gnus-summary-show-thread)
7247   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7248     (gnus-eval-in-buffer-window gnus-article-buffer
7249       (cond ((> lines 0)
7250              (when (gnus-article-next-page lines)
7251                (gnus-message 3 "End of message")))
7252             ((< lines 0)
7253              (gnus-article-prev-page (- lines))))))
7254   (gnus-summary-recenter)
7255   (gnus-summary-position-point))
7256
7257 (defun gnus-summary-scroll-down (lines)
7258   "Scroll down (or up) one line current article.
7259 Argument LINES specifies lines to be scrolled down (or up if negative)."
7260   (interactive "p")
7261   (gnus-summary-scroll-up (- lines)))
7262
7263 (defun gnus-summary-next-same-subject ()
7264   "Select next article which has the same subject as current one."
7265   (interactive)
7266   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7267
7268 (defun gnus-summary-prev-same-subject ()
7269   "Select previous article which has the same subject as current one."
7270   (interactive)
7271   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7272
7273 (defun gnus-summary-next-unread-same-subject ()
7274   "Select next unread article which has the same subject as current one."
7275   (interactive)
7276   (gnus-summary-next-article t (gnus-summary-article-subject)))
7277
7278 (defun gnus-summary-prev-unread-same-subject ()
7279   "Select previous unread article which has the same subject as current one."
7280   (interactive)
7281   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7282
7283 (defun gnus-summary-first-unread-article ()
7284   "Select the first unread article.
7285 Return nil if there are no unread articles."
7286   (interactive)
7287   (prog1
7288       (when (gnus-summary-first-subject t)
7289         (gnus-summary-show-thread)
7290         (gnus-summary-first-subject t)
7291         (gnus-summary-display-article (gnus-summary-article-number)))
7292     (gnus-summary-position-point)))
7293
7294 (defun gnus-summary-first-unread-subject ()
7295   "Place the point on the subject line of the first unread article.
7296 Return nil if there are no unread articles."
7297   (interactive)
7298   (prog1
7299       (when (gnus-summary-first-subject t)
7300         (gnus-summary-show-thread)
7301         (gnus-summary-first-subject t))
7302     (gnus-summary-position-point)))
7303
7304 (defun gnus-summary-first-unseen-subject ()
7305   "Place the point on the subject line of the first unseen article.
7306 Return nil if there are no unseen articles."
7307   (interactive)
7308   (prog1
7309       (when (gnus-summary-first-subject nil nil t)
7310         (gnus-summary-show-thread)
7311         (gnus-summary-first-subject nil nil t))
7312     (gnus-summary-position-point)))
7313
7314 (defun gnus-summary-first-unseen-or-unread-subject ()
7315   "Place the point on the subject line of the first unseen article or,
7316 if all article have been seen, on the subject line of the first unread
7317 article."
7318   (interactive)
7319   (prog1
7320       (unless (when (gnus-summary-first-subject nil nil t)
7321                 (gnus-summary-show-thread)
7322                 (gnus-summary-first-subject nil nil t))
7323         (when (gnus-summary-first-subject t)
7324           (gnus-summary-show-thread)
7325           (gnus-summary-first-subject t)))
7326     (gnus-summary-position-point)))
7327
7328 (defun gnus-summary-first-article ()
7329   "Select the first article.
7330 Return nil if there are no articles."
7331   (interactive)
7332   (prog1
7333       (when (gnus-summary-first-subject)
7334         (gnus-summary-show-thread)
7335         (gnus-summary-first-subject)
7336         (gnus-summary-display-article (gnus-summary-article-number)))
7337     (gnus-summary-position-point)))
7338
7339 (defun gnus-summary-best-unread-article (&optional arg)
7340   "Select the unread article with the highest score.
7341 If given a prefix argument, select the next unread article that has a
7342 score higher than the default score."
7343   (interactive "P")
7344   (let ((article (if arg
7345                      (gnus-summary-better-unread-subject)
7346                    (gnus-summary-best-unread-subject))))
7347     (if article
7348         (gnus-summary-goto-article article)
7349       (error "No unread articles"))))
7350
7351 (defun gnus-summary-best-unread-subject ()
7352   "Select the unread subject with the highest score."
7353   (interactive)
7354   (let ((best -1000000)
7355         (data gnus-newsgroup-data)
7356         article score)
7357     (while data
7358       (and (gnus-data-unread-p (car data))
7359            (> (setq score
7360                     (gnus-summary-article-score (gnus-data-number (car data))))
7361               best)
7362            (setq best score
7363                  article (gnus-data-number (car data))))
7364       (setq data (cdr data)))
7365     (when article
7366       (gnus-summary-goto-subject article))
7367     (gnus-summary-position-point)
7368     article))
7369
7370 (defun gnus-summary-better-unread-subject ()
7371   "Select the first unread subject that has a score over the default score."
7372   (interactive)
7373   (let ((data gnus-newsgroup-data)
7374         article score)
7375     (while (and (setq article (gnus-data-number (car data)))
7376                 (or (gnus-data-read-p (car data))
7377                     (not (> (gnus-summary-article-score article)
7378                             gnus-summary-default-score))))
7379       (setq data (cdr data)))
7380     (when article
7381       (gnus-summary-goto-subject article))
7382     (gnus-summary-position-point)
7383     article))
7384
7385 (defun gnus-summary-last-subject ()
7386   "Go to the last displayed subject line in the group."
7387   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7388     (when article
7389       (gnus-summary-goto-subject article))))
7390
7391 (defun gnus-summary-goto-article (article &optional all-headers force)
7392   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7393 If ALL-HEADERS is non-nil, no header lines are hidden.
7394 If FORCE, go to the article even if it isn't displayed.  If FORCE
7395 is a number, it is the line the article is to be displayed on."
7396   (interactive
7397    (list
7398     (completing-read
7399      "Article number or Message-ID: "
7400      (mapcar (lambda (number) (list (int-to-string number)))
7401              gnus-newsgroup-limit))
7402     current-prefix-arg
7403     t))
7404   (prog1
7405       (if (and (stringp article)
7406                (string-match "@" article))
7407           (gnus-summary-refer-article article)
7408         (when (stringp article)
7409           (setq article (string-to-number article)))
7410         (if (gnus-summary-goto-subject article force)
7411             (gnus-summary-display-article article all-headers)
7412           (gnus-message 4 "Couldn't go to article %s" article) nil))
7413     (gnus-summary-position-point)))
7414
7415 (defun gnus-summary-goto-last-article ()
7416   "Go to the previously read article."
7417   (interactive)
7418   (prog1
7419       (when gnus-last-article
7420         (gnus-summary-goto-article gnus-last-article nil t))
7421     (gnus-summary-position-point)))
7422
7423 (defun gnus-summary-pop-article (number)
7424   "Pop one article off the history and go to the previous.
7425 NUMBER articles will be popped off."
7426   (interactive "p")
7427   (let (to)
7428     (setq gnus-newsgroup-history
7429           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7430     (if to
7431         (gnus-summary-goto-article (car to) nil t)
7432       (error "Article history empty")))
7433   (gnus-summary-position-point))
7434
7435 ;; Summary commands and functions for limiting the summary buffer.
7436
7437 (defun gnus-summary-limit-to-articles (n)
7438   "Limit the summary buffer to the next N articles.
7439 If not given a prefix, use the process marked articles instead."
7440   (interactive "P")
7441   (prog1
7442       (let ((articles (gnus-summary-work-articles n)))
7443         (setq gnus-newsgroup-processable nil)
7444         (gnus-summary-limit articles))
7445     (gnus-summary-position-point)))
7446
7447 (defun gnus-summary-pop-limit (&optional total)
7448   "Restore the previous limit.
7449 If given a prefix, remove all limits."
7450   (interactive "P")
7451   (when total
7452     (setq gnus-newsgroup-limits
7453           (list (mapcar (lambda (h) (mail-header-number h))
7454                         gnus-newsgroup-headers))))
7455   (unless gnus-newsgroup-limits
7456     (error "No limit to pop"))
7457   (prog1
7458       (gnus-summary-limit nil 'pop)
7459     (gnus-summary-position-point)))
7460
7461 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7462   "Limit the summary buffer to articles that have subjects that match a regexp.
7463 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7464   (interactive
7465    (list (read-string (if current-prefix-arg
7466                           "Exclude subject (regexp): "
7467                         "Limit to subject (regexp): "))
7468          nil current-prefix-arg))
7469   (unless header
7470     (setq header "subject"))
7471   (when (not (equal "" subject))
7472     (prog1
7473         (let ((articles (gnus-summary-find-matching
7474                          (or header "subject") subject 'all nil nil
7475                          not-matching)))
7476           (unless articles
7477             (error "Found no matches for \"%s\"" subject))
7478           (gnus-summary-limit articles))
7479       (gnus-summary-position-point))))
7480
7481 (defun gnus-summary-limit-to-author (from &optional not-matching)
7482   "Limit the summary buffer to articles that have authors that match a regexp.
7483 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7484   (interactive
7485    (list (read-string (if current-prefix-arg
7486                           "Exclude author (regexp): "
7487                         "Limit to author (regexp): "))
7488          current-prefix-arg))
7489   (gnus-summary-limit-to-subject from "from" not-matching))
7490
7491 (defun gnus-summary-limit-to-age (age &optional younger-p)
7492   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7493 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7494 articles that are younger than AGE days."
7495   (interactive
7496    (let ((younger current-prefix-arg)
7497          (days-got nil)
7498          days)
7499      (while (not days-got)
7500        (setq days (if younger
7501                       (read-string "Limit to articles younger than (in days, older when negative): ")
7502                     (read-string
7503                      "Limit to articles older than (in days, younger when negative): ")))
7504        (when (> (length days) 0)
7505          (setq days (read days)))
7506        (if (numberp days)
7507            (progn
7508              (setq days-got t)
7509              (if (< days 0)
7510                  (progn
7511                    (setq younger (not younger))
7512                    (setq days (* days -1)))))
7513          (message "Please enter a number.")
7514          (sleep-for 1)))
7515      (list days younger)))
7516   (prog1
7517       (let ((data gnus-newsgroup-data)
7518             (cutoff (days-to-time age))
7519             articles d date is-younger)
7520         (while (setq d (pop data))
7521           (when (and (vectorp (gnus-data-header d))
7522                      (setq date (mail-header-date (gnus-data-header d))))
7523             (setq is-younger (time-less-p
7524                               (time-since (condition-case ()
7525                                               (date-to-time date)
7526                                             (error '(0 0))))
7527                               cutoff))
7528             (when (if younger-p
7529                       is-younger
7530                     (not is-younger))
7531               (push (gnus-data-number d) articles))))
7532         (gnus-summary-limit (nreverse articles)))
7533     (gnus-summary-position-point)))
7534
7535 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7536   "Limit the summary buffer to articles that match an 'extra' header."
7537   (interactive
7538    (let ((header
7539           (intern
7540            (gnus-completing-read-with-default
7541             (symbol-name (car gnus-extra-headers))
7542             (if current-prefix-arg
7543                 "Exclude extra header:"
7544               "Limit extra header:")
7545             (mapcar (lambda (x)
7546                       (cons (symbol-name x) x))
7547                     gnus-extra-headers)
7548             nil
7549             t))))
7550      (list header
7551            (read-string (format "%s header %s (regexp): "
7552                                 (if current-prefix-arg "Exclude" "Limit to")
7553                                 header))
7554            current-prefix-arg)))
7555   (when (not (equal "" regexp))
7556     (prog1
7557         (let ((articles (gnus-summary-find-matching
7558                          (cons 'extra header) regexp 'all nil nil
7559                          not-matching)))
7560           (unless articles
7561             (error "Found no matches for \"%s\"" regexp))
7562           (gnus-summary-limit articles))
7563       (gnus-summary-position-point))))
7564
7565 (defun gnus-summary-limit-to-display-predicate ()
7566   "Limit the summary buffer to the predicated in the `display' group parameter."
7567   (interactive)
7568   (unless gnus-newsgroup-display
7569     (error "There is no `display' group parameter"))
7570   (let (articles)
7571     (dolist (number gnus-newsgroup-articles)
7572       (when (funcall gnus-newsgroup-display)
7573         (push number articles)))
7574     (gnus-summary-limit articles))
7575   (gnus-summary-position-point))
7576
7577 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7578 (make-obsolete
7579  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7580
7581 (defun gnus-summary-limit-to-unread (&optional all)
7582   "Limit the summary buffer to articles that are not marked as read.
7583 If ALL is non-nil, limit strictly to unread articles."
7584   (interactive "P")
7585   (if all
7586       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7587     (gnus-summary-limit-to-marks
7588      ;; Concat all the marks that say that an article is read and have
7589      ;; those removed.
7590      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7591            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7592            gnus-low-score-mark gnus-expirable-mark
7593            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7594            gnus-duplicate-mark gnus-souped-mark)
7595      'reverse)))
7596
7597 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7598 (make-obsolete 'gnus-summary-delete-marked-with
7599                'gnus-summary-limit-exclude-marks)
7600
7601 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7602   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7603 If REVERSE, limit the summary buffer to articles that are marked
7604 with MARKS.  MARKS can either be a string of marks or a list of marks.
7605 Returns how many articles were removed."
7606   (interactive "sMarks: ")
7607   (gnus-summary-limit-to-marks marks t))
7608
7609 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7610   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7611 If REVERSE (the prefix), limit the summary buffer to articles that are
7612 not marked with MARKS.  MARKS can either be a string of marks or a
7613 list of marks.
7614 Returns how many articles were removed."
7615   (interactive "sMarks: \nP")
7616   (prog1
7617       (let ((data gnus-newsgroup-data)
7618             (marks (if (listp marks) marks
7619                      (append marks nil))) ; Transform to list.
7620             articles)
7621         (while data
7622           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7623                   (memq (gnus-data-mark (car data)) marks))
7624             (push (gnus-data-number (car data)) articles))
7625           (setq data (cdr data)))
7626         (gnus-summary-limit articles))
7627     (gnus-summary-position-point)))
7628
7629 (defun gnus-summary-limit-to-score (score)
7630   "Limit to articles with score at or above SCORE."
7631   (interactive "NLimit to articles with score of at least: ")
7632   (let ((data gnus-newsgroup-data)
7633         articles)
7634     (while data
7635       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7636                 score)
7637         (push (gnus-data-number (car data)) articles))
7638       (setq data (cdr data)))
7639     (prog1
7640         (gnus-summary-limit articles)
7641       (gnus-summary-position-point))))
7642
7643 (defun gnus-summary-limit-to-unseen ()
7644   "Limit to unseen articles."
7645   (interactive)
7646   (prog1
7647       (gnus-summary-limit gnus-newsgroup-unseen)
7648     (gnus-summary-position-point)))
7649
7650 (defun gnus-summary-limit-include-thread (id)
7651   "Display all the hidden articles that is in the thread with ID in it.
7652 When called interactively, ID is the Message-ID of the current
7653 article."
7654   (interactive (list (mail-header-id (gnus-summary-article-header))))
7655   (let ((articles (gnus-articles-in-thread
7656                    (gnus-id-to-thread (gnus-root-id id)))))
7657     (prog1
7658         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7659       (gnus-summary-limit-include-matching-articles
7660        "subject"
7661        (regexp-quote (gnus-simplify-subject-re
7662                       (mail-header-subject (gnus-id-to-header id)))))
7663       (gnus-summary-position-point))))
7664
7665 (defun gnus-summary-limit-include-matching-articles (header regexp)
7666   "Display all the hidden articles that have HEADERs that match REGEXP."
7667   (interactive (list (read-string "Match on header: ")
7668                      (read-string "Regexp: ")))
7669   (let ((articles (gnus-find-matching-articles header regexp)))
7670     (prog1
7671         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7672       (gnus-summary-position-point))))
7673
7674 (defun gnus-summary-insert-dormant-articles ()
7675   "Insert all the dormat articles for this group into the current buffer."
7676   (interactive)
7677   (let ((gnus-verbose (max 6 gnus-verbose)))
7678     (if (not gnus-newsgroup-dormant)
7679         (gnus-message 3 "No cached articles for this group")
7680       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7681
7682 (defun gnus-summary-limit-include-dormant ()
7683   "Display all the hidden articles that are marked as dormant.
7684 Note that this command only works on a subset of the articles currently
7685 fetched for this group."
7686   (interactive)
7687   (unless gnus-newsgroup-dormant
7688     (error "There are no dormant articles in this group"))
7689   (prog1
7690       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7691     (gnus-summary-position-point)))
7692
7693 (defun gnus-summary-limit-exclude-dormant ()
7694   "Hide all dormant articles."
7695   (interactive)
7696   (prog1
7697       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7698     (gnus-summary-position-point)))
7699
7700 (defun gnus-summary-limit-exclude-childless-dormant ()
7701   "Hide all dormant articles that have no children."
7702   (interactive)
7703   (let ((data (gnus-data-list t))
7704         articles d children)
7705     ;; Find all articles that are either not dormant or have
7706     ;; children.
7707     (while (setq d (pop data))
7708       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7709                 (and (setq children
7710                            (gnus-article-children (gnus-data-number d)))
7711                      (let (found)
7712                        (while children
7713                          (when (memq (car children) articles)
7714                            (setq children nil
7715                                  found t))
7716                          (pop children))
7717                        found)))
7718         (push (gnus-data-number d) articles)))
7719     ;; Do the limiting.
7720     (prog1
7721         (gnus-summary-limit articles)
7722       (gnus-summary-position-point))))
7723
7724 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7725   "Mark all unread excluded articles as read.
7726 If ALL, mark even excluded ticked and dormants as read."
7727   (interactive "P")
7728   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7729   (let ((articles (gnus-sorted-ndifference
7730                    (sort
7731                     (mapcar (lambda (h) (mail-header-number h))
7732                             gnus-newsgroup-headers)
7733                     '<)
7734                    gnus-newsgroup-limit))
7735         article)
7736     (setq gnus-newsgroup-unreads
7737           (gnus-sorted-intersection gnus-newsgroup-unreads
7738                                     gnus-newsgroup-limit))
7739     (if all
7740         (setq gnus-newsgroup-dormant nil
7741               gnus-newsgroup-marked nil
7742               gnus-newsgroup-reads
7743               (nconc
7744                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7745                gnus-newsgroup-reads))
7746       (while (setq article (pop articles))
7747         (unless (or (memq article gnus-newsgroup-dormant)
7748                     (memq article gnus-newsgroup-marked))
7749           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7750
7751 (defun gnus-summary-limit (articles &optional pop)
7752   (if pop
7753       ;; We pop the previous limit off the stack and use that.
7754       (setq articles (car gnus-newsgroup-limits)
7755             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7756     ;; We use the new limit, so we push the old limit on the stack.
7757     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7758   ;; Set the limit.
7759   (setq gnus-newsgroup-limit articles)
7760   (let ((total (length gnus-newsgroup-data))
7761         (data (gnus-data-find-list (gnus-summary-article-number)))
7762         (gnus-summary-mark-below nil)   ; Inhibit this.
7763         found)
7764     ;; This will do all the work of generating the new summary buffer
7765     ;; according to the new limit.
7766     (gnus-summary-prepare)
7767     ;; Hide any threads, possibly.
7768     (gnus-summary-maybe-hide-threads)
7769     ;; Try to return to the article you were at, or one in the
7770     ;; neighborhood.
7771     (when data
7772       ;; We try to find some article after the current one.
7773       (while data
7774         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7775           (setq data nil
7776                 found t))
7777         (setq data (cdr data))))
7778     (unless found
7779       ;; If there is no data, that means that we were after the last
7780       ;; article.  The same goes when we can't find any articles
7781       ;; after the current one.
7782       (goto-char (point-max))
7783       (gnus-summary-find-prev))
7784     (gnus-set-mode-line 'summary)
7785     ;; We return how many articles were removed from the summary
7786     ;; buffer as a result of the new limit.
7787     (- total (length gnus-newsgroup-data))))
7788
7789 (defsubst gnus-invisible-cut-children (threads)
7790   (let ((num 0))
7791     (while threads
7792       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7793         (incf num))
7794       (pop threads))
7795     (< num 2)))
7796
7797 (defsubst gnus-cut-thread (thread)
7798   "Go forwards in the thread until we find an article that we want to display."
7799   (when (or (eq gnus-fetch-old-headers 'some)
7800             (eq gnus-fetch-old-headers 'invisible)
7801             (numberp gnus-fetch-old-headers)
7802             (eq gnus-build-sparse-threads 'some)
7803             (eq gnus-build-sparse-threads 'more))
7804     ;; Deal with old-fetched headers and sparse threads.
7805     (while (and
7806             thread
7807             (or
7808              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7809              (gnus-summary-article-ancient-p
7810               (mail-header-number (car thread))))
7811             (if (or (<= (length (cdr thread)) 1)
7812                     (eq gnus-fetch-old-headers 'invisible))
7813                 (setq gnus-newsgroup-limit
7814                       (delq (mail-header-number (car thread))
7815                             gnus-newsgroup-limit)
7816                       thread (cadr thread))
7817               (when (gnus-invisible-cut-children (cdr thread))
7818                 (let ((th (cdr thread)))
7819                   (while th
7820                     (if (memq (mail-header-number (caar th))
7821                               gnus-newsgroup-limit)
7822                         (setq thread (car th)
7823                               th nil)
7824                       (setq th (cdr th))))))))))
7825   thread)
7826
7827 (defun gnus-cut-threads (threads)
7828   "Cut off all uninteresting articles from the beginning of threads."
7829   (when (or (eq gnus-fetch-old-headers 'some)
7830             (eq gnus-fetch-old-headers 'invisible)
7831             (numberp gnus-fetch-old-headers)
7832             (eq gnus-build-sparse-threads 'some)
7833             (eq gnus-build-sparse-threads 'more))
7834     (let ((th threads))
7835       (while th
7836         (setcar th (gnus-cut-thread (car th)))
7837         (setq th (cdr th)))))
7838   ;; Remove nixed out threads.
7839   (delq nil threads))
7840
7841 (defun gnus-summary-initial-limit (&optional show-if-empty)
7842   "Figure out what the initial limit is supposed to be on group entry.
7843 This entails weeding out unwanted dormants, low-scored articles,
7844 fetch-old-headers verbiage, and so on."
7845   ;; Most groups have nothing to remove.
7846   (if (or gnus-inhibit-limiting
7847           (and (null gnus-newsgroup-dormant)
7848                (eq gnus-newsgroup-display 'gnus-not-ignore)
7849                (not (eq gnus-fetch-old-headers 'some))
7850                (not (numberp gnus-fetch-old-headers))
7851                (not (eq gnus-fetch-old-headers 'invisible))
7852                (null gnus-summary-expunge-below)
7853                (not (eq gnus-build-sparse-threads 'some))
7854                (not (eq gnus-build-sparse-threads 'more))
7855                (null gnus-thread-expunge-below)
7856                (not gnus-use-nocem)))
7857       ()                                ; Do nothing.
7858     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7859     (setq gnus-newsgroup-limit nil)
7860     (mapatoms
7861      (lambda (node)
7862        (unless (car (symbol-value node))
7863          ;; These threads have no parents -- they are roots.
7864          (let ((nodes (cdr (symbol-value node)))
7865                thread)
7866            (while nodes
7867              (if (and gnus-thread-expunge-below
7868                       (< (gnus-thread-total-score (car nodes))
7869                          gnus-thread-expunge-below))
7870                  (gnus-expunge-thread (pop nodes))
7871                (setq thread (pop nodes))
7872                (gnus-summary-limit-children thread))))))
7873      gnus-newsgroup-dependencies)
7874     ;; If this limitation resulted in an empty group, we might
7875     ;; pop the previous limit and use it instead.
7876     (when (and (not gnus-newsgroup-limit)
7877                show-if-empty)
7878       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7879     gnus-newsgroup-limit))
7880
7881 (defun gnus-summary-limit-children (thread)
7882   "Return 1 if this subthread is visible and 0 if it is not."
7883   ;; First we get the number of visible children to this thread.  This
7884   ;; is done by recursing down the thread using this function, so this
7885   ;; will really go down to a leaf article first, before slowly
7886   ;; working its way up towards the root.
7887   (when thread
7888     (let* ((max-lisp-eval-depth 5000)
7889            (children
7890            (if (cdr thread)
7891                (apply '+ (mapcar 'gnus-summary-limit-children
7892                                  (cdr thread)))
7893              0))
7894           (number (mail-header-number (car thread)))
7895           score)
7896       (if (and
7897            (not (memq number gnus-newsgroup-marked))
7898            (or
7899             ;; If this article is dormant and has absolutely no visible
7900             ;; children, then this article isn't visible.
7901             (and (memq number gnus-newsgroup-dormant)
7902                  (zerop children))
7903             ;; If this is "fetch-old-headered" and there is no
7904             ;; visible children, then we don't want this article.
7905             (and (or (eq gnus-fetch-old-headers 'some)
7906                      (numberp gnus-fetch-old-headers))
7907                  (gnus-summary-article-ancient-p number)
7908                  (zerop children))
7909             ;; If this is "fetch-old-headered" and `invisible', then
7910             ;; we don't want this article.
7911             (and (eq gnus-fetch-old-headers 'invisible)
7912                  (gnus-summary-article-ancient-p number))
7913             ;; If this is a sparsely inserted article with no children,
7914             ;; we don't want it.
7915             (and (eq gnus-build-sparse-threads 'some)
7916                  (gnus-summary-article-sparse-p number)
7917                  (zerop children))
7918             ;; If we use expunging, and this article is really
7919             ;; low-scored, then we don't want this article.
7920             (when (and gnus-summary-expunge-below
7921                        (< (setq score
7922                                 (or (cdr (assq number gnus-newsgroup-scored))
7923                                     gnus-summary-default-score))
7924                           gnus-summary-expunge-below))
7925               ;; We increase the expunge-tally here, but that has
7926               ;; nothing to do with the limits, really.
7927               (incf gnus-newsgroup-expunged-tally)
7928               ;; We also mark as read here, if that's wanted.
7929               (when (and gnus-summary-mark-below
7930                          (< score gnus-summary-mark-below))
7931                 (setq gnus-newsgroup-unreads
7932                       (delq number gnus-newsgroup-unreads))
7933                 (if gnus-newsgroup-auto-expire
7934                     (push number gnus-newsgroup-expirable)
7935                   (push (cons number gnus-low-score-mark)
7936                         gnus-newsgroup-reads)))
7937               t)
7938             ;; Do the `display' group parameter.
7939             (and gnus-newsgroup-display
7940                  (not (funcall gnus-newsgroup-display)))
7941             ;; Check NoCeM things.
7942             (if (and gnus-use-nocem
7943                      (gnus-nocem-unwanted-article-p
7944                       (mail-header-id (car thread))))
7945                 (progn
7946                   (setq gnus-newsgroup-unreads
7947                         (delq number gnus-newsgroup-unreads))
7948                   t))))
7949           ;; Nope, invisible article.
7950           0
7951         ;; Ok, this article is to be visible, so we add it to the limit
7952         ;; and return 1.
7953         (push number gnus-newsgroup-limit)
7954         1))))
7955
7956 (defun gnus-expunge-thread (thread)
7957   "Mark all articles in THREAD as read."
7958   (let* ((number (mail-header-number (car thread))))
7959     (incf gnus-newsgroup-expunged-tally)
7960     ;; We also mark as read here, if that's wanted.
7961     (setq gnus-newsgroup-unreads
7962           (delq number gnus-newsgroup-unreads))
7963     (if gnus-newsgroup-auto-expire
7964         (push number gnus-newsgroup-expirable)
7965       (push (cons number gnus-low-score-mark)
7966             gnus-newsgroup-reads)))
7967   ;; Go recursively through all subthreads.
7968   (mapcar 'gnus-expunge-thread (cdr thread)))
7969
7970 ;; Summary article oriented commands
7971
7972 (defun gnus-summary-refer-parent-article (n)
7973   "Refer parent article N times.
7974 If N is negative, go to ancestor -N instead.
7975 The difference between N and the number of articles fetched is returned."
7976   (interactive "p")
7977   (let ((skip 1)
7978         error header ref)
7979     (when (not (natnump n))
7980       (setq skip (abs n)
7981             n 1))
7982     (while (and (> n 0)
7983                 (not error))
7984       (setq header (gnus-summary-article-header))
7985       (if (and (eq (mail-header-number header)
7986                    (cdr gnus-article-current))
7987                (equal gnus-newsgroup-name
7988                       (car gnus-article-current)))
7989           ;; If we try to find the parent of the currently
7990           ;; displayed article, then we take a look at the actual
7991           ;; References header, since this is slightly more
7992           ;; reliable than the References field we got from the
7993           ;; server.
7994           (save-excursion
7995             (set-buffer gnus-original-article-buffer)
7996             (nnheader-narrow-to-headers)
7997             (unless (setq ref (message-fetch-field "references"))
7998               (setq ref (message-fetch-field "in-reply-to")))
7999             (widen))
8000         (setq ref
8001               ;; It's not the current article, so we take a bet on
8002               ;; the value we got from the server.
8003               (mail-header-references header)))
8004       (if (and ref
8005                (not (equal ref "")))
8006           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8007             (gnus-message 1 "Couldn't find parent"))
8008         (gnus-message 1 "No references in article %d"
8009                       (gnus-summary-article-number))
8010         (setq error t))
8011       (decf n))
8012     (gnus-summary-position-point)
8013     n))
8014
8015 (defun gnus-summary-refer-references ()
8016   "Fetch all articles mentioned in the References header.
8017 Return the number of articles fetched."
8018   (interactive)
8019   (let ((ref (mail-header-references (gnus-summary-article-header)))
8020         (current (gnus-summary-article-number))
8021         (n 0))
8022     (if (or (not ref)
8023             (equal ref ""))
8024         (error "No References in the current article")
8025       ;; For each Message-ID in the References header...
8026       (while (string-match "<[^>]*>" ref)
8027         (incf n)
8028         ;; ... fetch that article.
8029         (gnus-summary-refer-article
8030          (prog1 (match-string 0 ref)
8031            (setq ref (substring ref (match-end 0))))))
8032       (gnus-summary-goto-subject current)
8033       (gnus-summary-position-point)
8034       n)))
8035
8036 (defun gnus-summary-refer-thread (&optional limit)
8037   "Fetch all articles in the current thread.
8038 If LIMIT (the numerical prefix), fetch that many old headers instead
8039 of what's specified by the `gnus-refer-thread-limit' variable."
8040   (interactive "P")
8041   (let ((id (mail-header-id (gnus-summary-article-header)))
8042         (limit (if limit (prefix-numeric-value limit)
8043                  gnus-refer-thread-limit)))
8044     (unless (eq gnus-fetch-old-headers 'invisible)
8045       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8046       ;; Retrieve the headers and read them in.
8047       (if (eq (if (numberp limit)
8048                   (gnus-retrieve-headers
8049                    (list (min
8050                           (+ (mail-header-number
8051                               (gnus-summary-article-header))
8052                              limit)
8053                           gnus-newsgroup-end))
8054                    gnus-newsgroup-name (* limit 2))
8055                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8056                 ;; headers.
8057                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8058                                        gnus-newsgroup-name limit))
8059               'nov)
8060           (gnus-build-all-threads)
8061         (error "Can't fetch thread from backends that don't support NOV"))
8062       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8063     (gnus-summary-limit-include-thread id)))
8064
8065 (defun gnus-summary-refer-article (message-id)
8066   "Fetch an article specified by MESSAGE-ID."
8067   (interactive "sMessage-ID: ")
8068   (when (and (stringp message-id)
8069              (not (zerop (length message-id))))
8070     ;; Construct the correct Message-ID if necessary.
8071     ;; Suggested by tale@pawl.rpi.edu.
8072     (unless (string-match "^<" message-id)
8073       (setq message-id (concat "<" message-id)))
8074     (unless (string-match ">$" message-id)
8075       (setq message-id (concat message-id ">")))
8076     (let* ((header (gnus-id-to-header message-id))
8077            (sparse (and header
8078                         (gnus-summary-article-sparse-p
8079                          (mail-header-number header))
8080                         (memq (mail-header-number header)
8081                               gnus-newsgroup-limit)))
8082            number)
8083       (cond
8084        ;; If the article is present in the buffer we just go to it.
8085        ((and header
8086              (or (not (gnus-summary-article-sparse-p
8087                        (mail-header-number header)))
8088                  sparse))
8089         (prog1
8090             (gnus-summary-goto-article
8091              (mail-header-number header) nil t)
8092           (when sparse
8093             (gnus-summary-update-article (mail-header-number header)))))
8094        (t
8095         ;; We fetch the article.
8096         (catch 'found
8097           (dolist (gnus-override-method (gnus-refer-article-methods))
8098             (when (and (gnus-check-server gnus-override-method)
8099                        ;; Fetch the header,
8100                        (setq number (gnus-summary-insert-subject message-id)))
8101               ;; and display the article.
8102               (gnus-summary-select-article nil nil nil number)
8103               (throw 'found t)))
8104           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8105
8106 (defun gnus-refer-article-methods ()
8107   "Return a list of referable methods."
8108   (cond
8109    ;; No method, so we default to current and native.
8110    ((null gnus-refer-article-method)
8111     (list gnus-current-select-method gnus-select-method))
8112    ;; Current.
8113    ((eq 'current gnus-refer-article-method)
8114     (list gnus-current-select-method))
8115    ;; List of select methods.
8116    ((not (and (symbolp (car gnus-refer-article-method))
8117               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8118     (let (out)
8119       (dolist (method gnus-refer-article-method)
8120         (push (if (eq 'current method)
8121                   gnus-current-select-method
8122                 method)
8123               out))
8124       (nreverse out)))
8125    ;; One single select method.
8126    (t
8127     (list gnus-refer-article-method))))
8128
8129 (defun gnus-summary-edit-parameters ()
8130   "Edit the group parameters of the current group."
8131   (interactive)
8132   (gnus-group-edit-group gnus-newsgroup-name 'params))
8133
8134 (defun gnus-summary-customize-parameters ()
8135   "Customize the group parameters of the current group."
8136   (interactive)
8137   (gnus-group-customize gnus-newsgroup-name))
8138
8139 (defun gnus-summary-enter-digest-group (&optional force)
8140   "Enter an nndoc group based on the current article.
8141 If FORCE, force a digest interpretation.  If not, try
8142 to guess what the document format is."
8143   (interactive "P")
8144   (let ((conf gnus-current-window-configuration))
8145     (save-excursion
8146       (gnus-summary-select-article))
8147     (setq gnus-current-window-configuration conf)
8148     (let* ((name (format "%s-%d"
8149                          (gnus-group-prefixed-name
8150                           gnus-newsgroup-name (list 'nndoc ""))
8151                          (save-excursion
8152                            (set-buffer gnus-summary-buffer)
8153                            gnus-current-article)))
8154            (ogroup gnus-newsgroup-name)
8155            (params (append (gnus-info-params (gnus-get-info ogroup))
8156                            (list (cons 'to-group ogroup))
8157                            (list (cons 'save-article-group ogroup))))
8158            (case-fold-search t)
8159            (buf (current-buffer))
8160            dig to-address)
8161       (save-excursion
8162         (set-buffer gnus-original-article-buffer)
8163         ;; Have the digest group inherit the main mail address of
8164         ;; the parent article.
8165         (when (setq to-address (or (gnus-fetch-field "reply-to")
8166                                    (gnus-fetch-field "from")))
8167           (setq params (append
8168                         (list (cons 'to-address
8169                                     (funcall gnus-decode-encoded-word-function
8170                                              to-address))))))
8171         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8172         (insert-buffer-substring gnus-original-article-buffer)
8173         ;; Remove lines that may lead nndoc to misinterpret the
8174         ;; document type.
8175         (narrow-to-region
8176          (goto-char (point-min))
8177          (or (search-forward "\n\n" nil t) (point)))
8178         (goto-char (point-min))
8179         (delete-matching-lines "^Path:\\|^From ")
8180         (widen))
8181       (unwind-protect
8182           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8183                     (gnus-newsgroup-ephemeral-ignored-charsets
8184                      gnus-newsgroup-ignored-charsets))
8185                 (gnus-group-read-ephemeral-group
8186                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8187                               (nndoc-article-type
8188                                ,(if force 'mbox 'guess)))
8189                  t nil nil nil
8190                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8191                                                         "ADAPT")))))
8192               ;; Make all postings to this group go to the parent group.
8193               (nconc (gnus-info-params (gnus-get-info name))
8194                      params)
8195             ;; Couldn't select this doc group.
8196             (switch-to-buffer buf)
8197             (gnus-set-global-variables)
8198             (gnus-configure-windows 'summary)
8199             (gnus-message 3 "Article couldn't be entered?"))
8200         (kill-buffer dig)))))
8201
8202 (defun gnus-summary-read-document (n)
8203   "Open a new group based on the current article(s).
8204 This will allow you to read digests and other similar
8205 documents as newsgroups.
8206 Obeys the standard process/prefix convention."
8207   (interactive "P")
8208   (let* ((articles (gnus-summary-work-articles n))
8209          (ogroup gnus-newsgroup-name)
8210          (params (append (gnus-info-params (gnus-get-info ogroup))
8211                          (list (cons 'to-group ogroup))))
8212          article group egroup groups vgroup)
8213     (while (setq article (pop articles))
8214       (setq group (format "%s-%d" gnus-newsgroup-name article))
8215       (gnus-summary-remove-process-mark article)
8216       (when (gnus-summary-display-article article)
8217         (save-excursion
8218           (with-temp-buffer
8219             (insert-buffer-substring gnus-original-article-buffer)
8220             ;; Remove some headers that may lead nndoc to make
8221             ;; the wrong guess.
8222             (message-narrow-to-head)
8223             (goto-char (point-min))
8224             (delete-matching-lines "^\\(Path\\):\\|^From ")
8225             (widen)
8226             (if (setq egroup
8227                       (gnus-group-read-ephemeral-group
8228                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8229                                      (nndoc-article-type guess))
8230                        t nil t))
8231                 (progn
8232             ;; Make all postings to this group go to the parent group.
8233                   (nconc (gnus-info-params (gnus-get-info egroup))
8234                          params)
8235                   (push egroup groups))
8236               ;; Couldn't select this doc group.
8237               (gnus-error 3 "Article couldn't be entered"))))))
8238     ;; Now we have selected all the documents.
8239     (cond
8240      ((not groups)
8241       (error "None of the articles could be interpreted as documents"))
8242      ((gnus-group-read-ephemeral-group
8243        (setq vgroup (format
8244                      "nnvirtual:%s-%s" gnus-newsgroup-name
8245                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8246        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8247        t
8248        (cons (current-buffer) 'summary)))
8249      (t
8250       (error "Couldn't select virtual nndoc group")))))
8251
8252 (defun gnus-summary-isearch-article (&optional regexp-p)
8253   "Do incremental search forward on the current article.
8254 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8255   (interactive "P")
8256   (gnus-summary-select-article)
8257   (gnus-configure-windows 'article)
8258   (gnus-eval-in-buffer-window gnus-article-buffer
8259     (save-restriction
8260       (widen)
8261       (isearch-forward regexp-p))))
8262
8263 (defun gnus-summary-search-article-forward (regexp &optional backward)
8264   "Search for an article containing REGEXP forward.
8265 If BACKWARD, search backward instead."
8266   (interactive
8267    (list (read-string
8268           (format "Search article %s (regexp%s): "
8269                   (if current-prefix-arg "backward" "forward")
8270                   (if gnus-last-search-regexp
8271                       (concat ", default " gnus-last-search-regexp)
8272                     "")))
8273          current-prefix-arg))
8274   (if (string-equal regexp "")
8275       (setq regexp (or gnus-last-search-regexp ""))
8276     (setq gnus-last-search-regexp regexp)
8277     (setq gnus-article-before-search gnus-current-article))
8278   ;; Intentionally set gnus-last-article.
8279   (setq gnus-last-article gnus-article-before-search)
8280   (let ((gnus-last-article gnus-last-article))
8281     (if (gnus-summary-search-article regexp backward)
8282         (gnus-summary-show-thread)
8283       (error "Search failed: \"%s\"" regexp))))
8284
8285 (defun gnus-summary-search-article-backward (regexp)
8286   "Search for an article containing REGEXP backward."
8287   (interactive
8288    (list (read-string
8289           (format "Search article backward (regexp%s): "
8290                   (if gnus-last-search-regexp
8291                       (concat ", default " gnus-last-search-regexp)
8292                     "")))))
8293   (gnus-summary-search-article-forward regexp 'backward))
8294
8295 (defun gnus-summary-search-article (regexp &optional backward)
8296   "Search for an article containing REGEXP.
8297 Optional argument BACKWARD means do search for backward.
8298 `gnus-select-article-hook' is not called during the search."
8299   ;; We have to require this here to make sure that the following
8300   ;; dynamic binding isn't shadowed by autoloading.
8301   (require 'gnus-async)
8302   (require 'gnus-art)
8303   (let ((gnus-select-article-hook nil)  ;Disable hook.
8304         (gnus-article-prepare-hook nil)
8305         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8306         (gnus-use-article-prefetch nil)
8307         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8308         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8309         (gnus-visual nil)
8310         (gnus-keep-backlog nil)
8311         (gnus-break-pages nil)
8312         (gnus-summary-display-arrow nil)
8313         (gnus-updated-mode-lines nil)
8314         (gnus-auto-center-summary nil)
8315         (sum (current-buffer))
8316         (gnus-display-mime-function nil)
8317         (found nil)
8318         point)
8319     (gnus-save-hidden-threads
8320       (gnus-summary-select-article)
8321       (set-buffer gnus-article-buffer)
8322       (goto-char (window-point (get-buffer-window (current-buffer))))
8323       (when backward
8324         (forward-line -1))
8325       (while (not found)
8326         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8327         (if (if backward
8328                 (re-search-backward regexp nil t)
8329               (re-search-forward regexp nil t))
8330             ;; We found the regexp.
8331             (progn
8332               (setq found 'found)
8333               (beginning-of-line)
8334               (set-window-start
8335                (get-buffer-window (current-buffer))
8336                (point))
8337               (forward-line 1)
8338               (set-window-point
8339                (get-buffer-window (current-buffer))
8340                (point))
8341               (set-buffer sum)
8342               (setq point (point)))
8343           ;; We didn't find it, so we go to the next article.
8344           (set-buffer sum)
8345           (setq found 'not)
8346           (while (eq found 'not)
8347             (if (not (if backward (gnus-summary-find-prev)
8348                        (gnus-summary-find-next)))
8349                 ;; No more articles.
8350                 (setq found t)
8351               ;; Select the next article and adjust point.
8352               (unless (gnus-summary-article-sparse-p
8353                        (gnus-summary-article-number))
8354                 (setq found nil)
8355                 (gnus-summary-select-article)
8356                 (set-buffer gnus-article-buffer)
8357                 (widen)
8358                 (goto-char (if backward (point-max) (point-min))))))))
8359       (gnus-message 7 ""))
8360     ;; Return whether we found the regexp.
8361     (when (eq found 'found)
8362       (goto-char point)
8363       (gnus-summary-show-thread)
8364       (gnus-summary-goto-subject gnus-current-article)
8365       (gnus-summary-position-point)
8366       t)))
8367
8368 (defun gnus-find-matching-articles (header regexp)
8369   "Return a list of all articles that match REGEXP on HEADER.
8370 This search includes all articles in the current group that Gnus has
8371 fetched headers for, whether they are displayed or not."
8372   (let ((articles nil)
8373         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8374         (case-fold-search t))
8375     (dolist (header gnus-newsgroup-headers)
8376       (when (string-match regexp (funcall func header))
8377         (push (mail-header-number header) articles)))
8378     (nreverse articles)))
8379
8380 (defun gnus-summary-find-matching (header regexp &optional backward unread
8381                                           not-case-fold not-matching)
8382   "Return a list of all articles that match REGEXP on HEADER.
8383 The search stars on the current article and goes forwards unless
8384 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8385 If UNREAD is non-nil, only unread articles will
8386 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8387 in the comparisons. If NOT-MATCHING, return a list of all articles that
8388 not match REGEXP on HEADER."
8389   (let ((case-fold-search (not not-case-fold))
8390         articles d func)
8391     (if (consp header)
8392         (if (eq (car header) 'extra)
8393             (setq func
8394                   `(lambda (h)
8395                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8396                          "")))
8397           (error "%s is an invalid header" header))
8398       (unless (fboundp (intern (concat "mail-header-" header)))
8399         (error "%s is not a valid header" header))
8400       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8401     (dolist (d (if (eq backward 'all)
8402                    gnus-newsgroup-data
8403                  (gnus-data-find-list
8404                   (gnus-summary-article-number)
8405                   (gnus-data-list backward))))
8406       (when (and (or (not unread)       ; We want all articles...
8407                      (gnus-data-unread-p d)) ; Or just unreads.
8408                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8409                  (if not-matching
8410                      (not (string-match
8411                            regexp
8412                            (funcall func (gnus-data-header d))))
8413                    (string-match regexp
8414                                  (funcall func (gnus-data-header d)))))
8415         (push (gnus-data-number d) articles))) ; Success!
8416     (nreverse articles)))
8417
8418 (defun gnus-summary-execute-command (header regexp command &optional backward)
8419   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8420 If HEADER is an empty string (or nil), the match is done on the entire
8421 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8422   (interactive
8423    (list (let ((completion-ignore-case t))
8424            (completing-read
8425             "Header name: "
8426             (mapcar (lambda (header) (list (format "%s" header)))
8427                     (append
8428                      '("Number" "Subject" "From" "Lines" "Date"
8429                        "Message-ID" "Xref" "References" "Body")
8430                      gnus-extra-headers))
8431             nil 'require-match))
8432          (read-string "Regexp: ")
8433          (read-key-sequence "Command: ")
8434          current-prefix-arg))
8435   (when (equal header "Body")
8436     (setq header ""))
8437   ;; Hidden thread subtrees must be searched as well.
8438   (gnus-summary-show-all-threads)
8439   ;; We don't want to change current point nor window configuration.
8440   (save-excursion
8441     (save-window-excursion
8442       (let (gnus-visual
8443             gnus-treat-strip-trailing-blank-lines
8444             gnus-treat-strip-leading-blank-lines
8445             gnus-treat-strip-multiple-blank-lines
8446             gnus-treat-hide-boring-headers
8447             gnus-treat-fold-newsgroups
8448             gnus-article-prepare-hook)
8449         (gnus-message 6 "Executing %s..." (key-description command))
8450         ;; We'd like to execute COMMAND interactively so as to give arguments.
8451         (gnus-execute header regexp
8452                       `(call-interactively ',(key-binding command))
8453                       backward)
8454         (gnus-message 6 "Executing %s...done" (key-description command))))))
8455
8456 (defun gnus-summary-beginning-of-article ()
8457   "Scroll the article back to the beginning."
8458   (interactive)
8459   (gnus-summary-select-article)
8460   (gnus-configure-windows 'article)
8461   (gnus-eval-in-buffer-window gnus-article-buffer
8462     (widen)
8463     (goto-char (point-min))
8464     (when gnus-page-broken
8465       (gnus-narrow-to-page))))
8466
8467 (defun gnus-summary-end-of-article ()
8468   "Scroll to the end of the article."
8469   (interactive)
8470   (gnus-summary-select-article)
8471   (gnus-configure-windows 'article)
8472   (gnus-eval-in-buffer-window gnus-article-buffer
8473     (widen)
8474     (goto-char (point-max))
8475     (recenter -3)
8476     (when gnus-page-broken
8477       (gnus-narrow-to-page))))
8478
8479 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8480   "Truncate to LEN and quote all \"(\"'s in STRING."
8481   (gnus-replace-in-string (if (and len (> (length string) len))
8482                               (substring string 0 len)
8483                             string)
8484                           "[()]" "\\\\\\&"))
8485
8486 (defun gnus-summary-print-article (&optional filename n)
8487   "Generate and print a PostScript image of the N next (mail) articles.
8488
8489 If N is negative, print the N previous articles.  If N is nil and articles
8490 have been marked with the process mark, print these instead.
8491
8492 If the optional first argument FILENAME is nil, send the image to the
8493 printer.  If FILENAME is a string, save the PostScript image in a file with
8494 that name.  If FILENAME is a number, prompt the user for the name of the file
8495 to save in."
8496   (interactive (list (ps-print-preprint current-prefix-arg)))
8497   (dolist (article (gnus-summary-work-articles n))
8498     (gnus-summary-select-article nil nil 'pseudo article)
8499     (gnus-eval-in-buffer-window gnus-article-buffer
8500       (gnus-print-buffer))
8501     (gnus-summary-remove-process-mark article))
8502   (ps-despool filename))
8503
8504 (defun gnus-print-buffer ()
8505   (let ((buffer (generate-new-buffer " *print*")))
8506     (unwind-protect
8507         (progn
8508           (copy-to-buffer buffer (point-min) (point-max))
8509           (set-buffer buffer)
8510           (gnus-article-delete-invisible-text)
8511           (gnus-remove-text-with-property 'gnus-decoration)
8512           (when (gnus-visual-p 'article-highlight 'highlight)
8513             ;; Copy-to-buffer doesn't copy overlay.  So redo
8514             ;; highlight.
8515             (let ((gnus-article-buffer buffer))
8516               (gnus-article-highlight-citation t)
8517               (gnus-article-highlight-signature)))
8518           (let ((ps-left-header
8519                  (list
8520                   (concat "("
8521                           (gnus-summary-print-truncate-and-quote
8522                            (mail-header-subject gnus-current-headers)
8523                            66) ")")
8524                   (concat "("
8525                           (gnus-summary-print-truncate-and-quote
8526                            (mail-header-from gnus-current-headers)
8527                            45) ")")))
8528                 (ps-right-header
8529                  (list
8530                   "/pagenumberstring load"
8531                   (concat "("
8532                           (mail-header-date gnus-current-headers) ")"))))
8533             (gnus-run-hooks 'gnus-ps-print-hook)
8534             (save-excursion
8535               (if window-system
8536                   (ps-spool-buffer-with-faces)
8537                 (ps-spool-buffer)))))
8538       (kill-buffer buffer))))
8539
8540 (defun gnus-summary-show-article (&optional arg)
8541   "Force redisplaying of the current article.
8542 If ARG (the prefix) is a number, show the article with the charset
8543 defined in `gnus-summary-show-article-charset-alist', or the charset
8544 input.
8545 If ARG (the prefix) is non-nil and not a number, show the raw article
8546 without any article massaging functions being run.  Normally, the key
8547 strokes are `C-u g'."
8548   (interactive "P")
8549   (cond
8550    ((numberp arg)
8551     (gnus-summary-show-article t)
8552     (let ((gnus-newsgroup-charset
8553            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8554                (mm-read-coding-system
8555                 "View as charset: " ;; actually it is coding system.
8556                 (save-excursion
8557                   (set-buffer gnus-article-buffer)
8558                   (mm-detect-coding-region (point) (point-max))))))
8559           (gnus-newsgroup-ignored-charsets 'gnus-all))
8560       (gnus-summary-select-article nil 'force)
8561       (let ((deps gnus-newsgroup-dependencies)
8562             head header lines)
8563         (save-excursion
8564           (set-buffer gnus-original-article-buffer)
8565           (save-restriction
8566             (message-narrow-to-head)
8567             (setq head (buffer-string))
8568             (goto-char (point-min))
8569             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8570               (goto-char (point-max))
8571               (widen)
8572               (setq lines (1- (count-lines (point) (point-max))))))
8573           (with-temp-buffer
8574             (insert (format "211 %d Article retrieved.\n"
8575                             (cdr gnus-article-current)))
8576             (insert head)
8577             (if lines (insert (format "Lines: %d\n" lines)))
8578             (insert ".\n")
8579             (let ((nntp-server-buffer (current-buffer)))
8580               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8581         (gnus-data-set-header
8582          (gnus-data-find (cdr gnus-article-current))
8583          header)
8584         (gnus-summary-update-article-line
8585          (cdr gnus-article-current) header)
8586         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8587           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8588    ((not arg)
8589     ;; Select the article the normal way.
8590     (gnus-summary-select-article nil 'force))
8591    (t
8592     ;; We have to require this here to make sure that the following
8593     ;; dynamic binding isn't shadowed by autoloading.
8594     (require 'gnus-async)
8595     (require 'gnus-art)
8596     ;; Bind the article treatment functions to nil.
8597     (let ((gnus-have-all-headers t)
8598           gnus-article-prepare-hook
8599           gnus-article-decode-hook
8600           gnus-display-mime-function
8601           gnus-break-pages)
8602       ;; Destroy any MIME parts.
8603       (when (gnus-buffer-live-p gnus-article-buffer)
8604         (save-excursion
8605           (set-buffer gnus-article-buffer)
8606           (mm-destroy-parts gnus-article-mime-handles)
8607           ;; Set it to nil for safety reason.
8608           (setq gnus-article-mime-handle-alist nil)
8609           (setq gnus-article-mime-handles nil)))
8610       (gnus-summary-select-article nil 'force))))
8611   (gnus-summary-goto-subject gnus-current-article)
8612   (gnus-summary-position-point))
8613
8614 (defun gnus-summary-show-raw-article ()
8615   "Show the raw article without any article massaging functions being run."
8616   (interactive)
8617   (gnus-summary-show-article t))
8618
8619 (defun gnus-summary-verbose-headers (&optional arg)
8620   "Toggle permanent full header display.
8621 If ARG is a positive number, turn header display on.
8622 If ARG is a negative number, turn header display off."
8623   (interactive "P")
8624   (setq gnus-show-all-headers
8625         (cond ((or (not (numberp arg))
8626                    (zerop arg))
8627                (not gnus-show-all-headers))
8628               ((natnump arg)
8629                t)))
8630   (gnus-summary-show-article))
8631
8632 (defun gnus-summary-toggle-header (&optional arg)
8633   "Show the headers if they are hidden, or hide them if they are shown.
8634 If ARG is a positive number, show the entire header.
8635 If ARG is a negative number, hide the unwanted header lines."
8636   (interactive "P")
8637   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8638                      (get-buffer-window gnus-article-buffer t))))
8639     (with-current-buffer gnus-article-buffer
8640       (widen)
8641       (article-narrow-to-head)
8642       (let* ((buffer-read-only nil)
8643              (inhibit-point-motion-hooks t)
8644              (hidden (if (numberp arg)
8645                          (>= arg 0)
8646                        (gnus-article-hidden-text-p 'headers)))
8647              s e)
8648         (delete-region (point-min) (point-max))
8649         (with-current-buffer gnus-original-article-buffer
8650           (goto-char (setq s (point-min)))
8651           (setq e (if (search-forward "\n\n" nil t)
8652                       (1- (point))
8653                     (point-max))))
8654         (insert-buffer-substring gnus-original-article-buffer s e)
8655         (run-hooks 'gnus-article-decode-hook)
8656         (if hidden
8657             (let ((gnus-treat-hide-headers nil)
8658                   (gnus-treat-hide-boring-headers nil))
8659               (gnus-delete-wash-type 'headers)
8660               (gnus-treat-article 'head))
8661           (gnus-treat-article 'head))
8662         (widen)
8663         (if window
8664             (set-window-start window (goto-char (point-min))))
8665         (setq gnus-page-broken
8666               (when gnus-break-pages
8667                 (gnus-narrow-to-page)
8668                 t))
8669         (gnus-set-mode-line 'article)))))
8670
8671 (defun gnus-summary-show-all-headers ()
8672   "Make all header lines visible."
8673   (interactive)
8674   (gnus-summary-toggle-header 1))
8675
8676 (defun gnus-summary-caesar-message (&optional arg)
8677   "Caesar rotate the current article by 13.
8678 The numerical prefix specifies how many places to rotate each letter
8679 forward."
8680   (interactive "P")
8681   (gnus-summary-select-article)
8682   (let ((mail-header-separator ""))
8683     (gnus-eval-in-buffer-window gnus-article-buffer
8684       (save-restriction
8685         (widen)
8686         (let ((start (window-start))
8687               buffer-read-only)
8688           (message-caesar-buffer-body arg)
8689           (set-window-start (get-buffer-window (current-buffer)) start))))))
8690
8691 (autoload 'unmorse-region "morse"
8692   "Convert morse coded text in region to ordinary ASCII text."
8693   t)
8694
8695 (defun gnus-summary-morse-message (&optional arg)
8696   "Morse decode the current article."
8697   (interactive "P")
8698   (gnus-summary-select-article)
8699   (let ((mail-header-separator ""))
8700     (gnus-eval-in-buffer-window gnus-article-buffer
8701       (save-excursion
8702         (save-restriction
8703           (widen)
8704           (let ((pos (window-start))
8705                 buffer-read-only)
8706             (goto-char (point-min))
8707             (when (message-goto-body)
8708               (gnus-narrow-to-body))
8709             (goto-char (point-min))
8710             (while (re-search-forward "·" (point-max) t)
8711               (replace-match "."))
8712             (unmorse-region (point-min) (point-max))
8713             (widen)
8714             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8715
8716 (defun gnus-summary-stop-page-breaking ()
8717   "Stop page breaking in the current article."
8718   (interactive)
8719   (gnus-summary-select-article)
8720   (gnus-eval-in-buffer-window gnus-article-buffer
8721     (widen)
8722     (when (gnus-visual-p 'page-marker)
8723       (let ((buffer-read-only nil))
8724         (gnus-remove-text-with-property 'gnus-prev)
8725         (gnus-remove-text-with-property 'gnus-next))
8726       (setq gnus-page-broken nil))))
8727
8728 (defun gnus-summary-move-article (&optional n to-newsgroup
8729                                             select-method action)
8730   "Move the current article to a different newsgroup.
8731 If N is a positive number, move the N next articles.
8732 If N is a negative number, move the N previous articles.
8733 If N is nil and any articles have been marked with the process mark,
8734 move those articles instead.
8735 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8736 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8737 re-spool using this method.
8738
8739 When called interactively with TO-NEWSGROUP being nil, the value of
8740 the variable `gnus-move-split-methods' is used for finding a default
8741 for the target newsgroup.
8742
8743 For this function to work, both the current newsgroup and the
8744 newsgroup that you want to move to have to support the `request-move'
8745 and `request-accept' functions.
8746
8747 ACTION can be either `move' (the default), `crosspost' or `copy'."
8748   (interactive "P")
8749   (unless action
8750     (setq action 'move))
8751   ;; Check whether the source group supports the required functions.
8752   (cond ((and (eq action 'move)
8753               (not (gnus-check-backend-function
8754                     'request-move-article gnus-newsgroup-name)))
8755          (error "The current group does not support article moving"))
8756         ((and (eq action 'crosspost)
8757               (not (gnus-check-backend-function
8758                     'request-replace-article gnus-newsgroup-name)))
8759          (error "The current group does not support article editing")))
8760   (let ((articles (gnus-summary-work-articles n))
8761         (prefix (if (gnus-check-backend-function
8762                      'request-move-article gnus-newsgroup-name)
8763                     (gnus-group-real-prefix gnus-newsgroup-name)
8764                   ""))
8765         (names '((move "Move" "Moving")
8766                  (copy "Copy" "Copying")
8767                  (crosspost "Crosspost" "Crossposting")))
8768         (copy-buf (save-excursion
8769                     (nnheader-set-temp-buffer " *copy article*")))
8770         art-group to-method new-xref article to-groups)
8771     (unless (assq action names)
8772       (error "Unknown action %s" action))
8773     ;; Read the newsgroup name.
8774     (when (and (not to-newsgroup)
8775                (not select-method))
8776       (if (and gnus-move-split-methods
8777                (not
8778                 (and (memq gnus-current-article articles)
8779                      (gnus-buffer-live-p gnus-original-article-buffer))))
8780           ;; When `gnus-move-split-methods' is non-nil, we have to
8781           ;; select an article to give `gnus-read-move-group-name' an
8782           ;; opportunity to suggest an appropriate default.  However,
8783           ;; we needn't render or mark the article.
8784           (let ((gnus-display-mime-function nil)
8785                 (gnus-article-prepare-hook nil)
8786                 (gnus-mark-article-hook nil))
8787             (gnus-summary-select-article nil nil nil (car articles))))
8788       (setq to-newsgroup
8789             (gnus-read-move-group-name
8790              (cadr (assq action names))
8791              (symbol-value (intern (format "gnus-current-%s-group" action)))
8792              articles prefix))
8793       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8794     (setq to-method (or select-method
8795                         (gnus-server-to-method
8796                          (gnus-group-method to-newsgroup))))
8797     ;; Check the method we are to move this article to...
8798     (unless (gnus-check-backend-function
8799              'request-accept-article (car to-method))
8800       (error "%s does not support article copying" (car to-method)))
8801     (unless (gnus-check-server to-method)
8802       (error "Can't open server %s" (car to-method)))
8803     (gnus-message 6 "%s to %s: %s..."
8804                   (caddr (assq action names))
8805                   (or (car select-method) to-newsgroup) articles)
8806     (while articles
8807       (setq article (pop articles))
8808       (setq
8809        art-group
8810        (cond
8811         ;; Move the article.
8812         ((eq action 'move)
8813          ;; Remove this article from future suppression.
8814          (gnus-dup-unsuppress-article article)
8815          (gnus-request-move-article
8816           article                       ; Article to move
8817           gnus-newsgroup-name           ; From newsgroup
8818           (nth 1 (gnus-find-method-for-group
8819                   gnus-newsgroup-name)) ; Server
8820           (list 'gnus-request-accept-article
8821                 to-newsgroup (list 'quote select-method)
8822                 (not articles) t)       ; Accept form
8823           (not articles)))              ; Only save nov last time
8824         ;; Copy the article.
8825         ((eq action 'copy)
8826          (save-excursion
8827            (set-buffer copy-buf)
8828            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8829              (gnus-request-accept-article
8830               to-newsgroup select-method (not articles) t))))
8831         ;; Crosspost the article.
8832         ((eq action 'crosspost)
8833          (let ((xref (message-tokenize-header
8834                       (mail-header-xref (gnus-summary-article-header article))
8835                       " ")))
8836            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8837                                   ":" (number-to-string article)))
8838            (unless xref
8839              (setq xref (list (system-name))))
8840            (setq new-xref
8841                  (concat
8842                   (mapconcat 'identity
8843                              (delete "Xref:" (delete new-xref xref))
8844                              " ")
8845                   " " new-xref))
8846            (save-excursion
8847              (set-buffer copy-buf)
8848              ;; First put the article in the destination group.
8849              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8850              (when (consp (setq art-group
8851                                 (gnus-request-accept-article
8852                                  to-newsgroup select-method (not articles))))
8853                (setq new-xref (concat new-xref " " (car art-group)
8854                                       ":"
8855                                       (number-to-string (cdr art-group))))
8856                ;; Now we have the new Xrefs header, so we insert
8857                ;; it and replace the new article.
8858                (nnheader-replace-header "Xref" new-xref)
8859                (gnus-request-replace-article
8860                 (cdr art-group) to-newsgroup (current-buffer))
8861                art-group))))))
8862       (cond
8863        ((not art-group)
8864         (gnus-message 1 "Couldn't %s article %s: %s"
8865                       (cadr (assq action names)) article
8866                       (nnheader-get-report (car to-method))))
8867        ((eq art-group 'junk)
8868         (when (eq action 'move)
8869           (gnus-summary-mark-article article gnus-canceled-mark)
8870           (gnus-message 4 "Deleted article %s" article)
8871           ;; run the delete hook
8872           (run-hook-with-args 'gnus-summary-article-delete-hook
8873                               action
8874                               (gnus-data-header
8875                                (assoc article (gnus-data-list nil)))
8876                               gnus-newsgroup-name nil
8877                               select-method)))
8878        (t
8879         (let* ((pto-group (gnus-group-prefixed-name
8880                            (car art-group) to-method))
8881                (entry
8882                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8883                (info (nth 2 entry))
8884                (to-group (gnus-info-group info))
8885                to-marks)
8886           ;; Update the group that has been moved to.
8887           (when (and info
8888                      (memq action '(move copy)))
8889             (unless (member to-group to-groups)
8890               (push to-group to-groups))
8891
8892             (unless (memq article gnus-newsgroup-unreads)
8893               (push 'read to-marks)
8894               (gnus-info-set-read
8895                info (gnus-add-to-range (gnus-info-read info)
8896                                        (list (cdr art-group)))))
8897
8898             ;; See whether the article is to be put in the cache.
8899             (let ((marks gnus-article-mark-lists)
8900                   (to-article (cdr art-group)))
8901
8902               ;; Enter the article into the cache in the new group,
8903               ;; if that is required.
8904               (when gnus-use-cache
8905                 (gnus-cache-possibly-enter-article
8906                  to-group to-article
8907                  (memq article gnus-newsgroup-marked)
8908                  (memq article gnus-newsgroup-dormant)
8909                  (memq article gnus-newsgroup-unreads)))
8910
8911               (when gnus-preserve-marks
8912                 ;; Copy any marks over to the new group.
8913                 (when (and (equal to-group gnus-newsgroup-name)
8914                            (not (memq article gnus-newsgroup-unreads)))
8915                   ;; Mark this article as read in this group.
8916                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8917                   (setcdr (gnus-active to-group) to-article)
8918                   (setcdr gnus-newsgroup-active to-article))
8919
8920                 (while marks
8921                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8922                     (when (memq article (symbol-value
8923                                          (intern (format "gnus-newsgroup-%s"
8924                                                          (caar marks)))))
8925                       (push (cdar marks) to-marks)
8926                       ;; If the other group is the same as this group,
8927                       ;; then we have to add the mark to the list.
8928                       (when (equal to-group gnus-newsgroup-name)
8929                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8930                              (cons to-article
8931                                    (symbol-value
8932                                     (intern (format "gnus-newsgroup-%s"
8933                                                     (caar marks)))))))
8934                       ;; Copy the marks to other group.
8935                       (gnus-add-marked-articles
8936                        to-group (cdar marks) (list to-article) info)))
8937                   (setq marks (cdr marks)))
8938
8939                 (gnus-request-set-mark
8940                  to-group (list (list (list to-article) 'add to-marks))))
8941
8942               (gnus-dribble-enter
8943                (concat "(gnus-group-set-info '"
8944                        (gnus-prin1-to-string (gnus-get-info to-group))
8945                        ")"))))
8946
8947           ;; Update the Xref header in this article to point to
8948           ;; the new crossposted article we have just created.
8949           (when (eq action 'crosspost)
8950             (save-excursion
8951               (set-buffer copy-buf)
8952               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8953               (nnheader-replace-header "Xref" new-xref)
8954               (gnus-request-replace-article
8955                article gnus-newsgroup-name (current-buffer))))
8956
8957           ;; run the move/copy/crosspost/respool hook
8958           (run-hook-with-args 'gnus-summary-article-move-hook 
8959                               action
8960                               (gnus-data-header 
8961                                (assoc article (gnus-data-list nil)))
8962                               gnus-newsgroup-name
8963                               to-newsgroup
8964                               select-method))
8965
8966         ;;;!!!Why is this necessary?
8967         (set-buffer gnus-summary-buffer)
8968         
8969         (gnus-summary-goto-subject article)
8970         (when (eq action 'move)
8971           (gnus-summary-mark-article article gnus-canceled-mark))))
8972       (gnus-summary-remove-process-mark article))
8973     ;; Re-activate all groups that have been moved to.
8974     (save-excursion
8975       (set-buffer gnus-group-buffer)
8976       (let ((gnus-group-marked to-groups))
8977         (gnus-group-get-new-news-this-group nil t)))
8978
8979     (gnus-kill-buffer copy-buf)
8980     (gnus-summary-position-point)
8981     (gnus-set-mode-line 'summary)))
8982
8983 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8984   "Move the current article to a different newsgroup.
8985 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8986 When called interactively, if TO-NEWSGROUP is nil, use the value of
8987 the variable `gnus-move-split-methods' for finding a default target
8988 newsgroup.
8989 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8990 re-spool using this method."
8991   (interactive "P")
8992   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8993
8994 (defun gnus-summary-crosspost-article (&optional n)
8995   "Crosspost the current article to some other group."
8996   (interactive "P")
8997   (gnus-summary-move-article n nil nil 'crosspost))
8998
8999 (defcustom gnus-summary-respool-default-method nil
9000   "Default method type for respooling an article.
9001 If nil, use to the current newsgroup method."
9002   :type 'symbol
9003   :group 'gnus-summary-mail)
9004
9005 (defcustom gnus-summary-display-while-building nil
9006   "If non-nil, show and update the summary buffer as it's being built.
9007 If the value is t, update the buffer after every line is inserted.  If
9008 the value is an integer (N), update the display every N lines."
9009   :group 'gnus-thread
9010   :type '(choice (const :tag "off" nil)
9011                  number
9012                  (const :tag "frequently" t)))
9013
9014 (defun gnus-summary-respool-article (&optional n method)
9015   "Respool the current article.
9016 The article will be squeezed through the mail spooling process again,
9017 which means that it will be put in some mail newsgroup or other
9018 depending on `nnmail-split-methods'.
9019 If N is a positive number, respool the N next articles.
9020 If N is a negative number, respool the N previous articles.
9021 If N is nil and any articles have been marked with the process mark,
9022 respool those articles instead.
9023
9024 Respooling can be done both from mail groups and \"real\" newsgroups.
9025 In the former case, the articles in question will be moved from the
9026 current group into whatever groups they are destined to.  In the
9027 latter case, they will be copied into the relevant groups."
9028   (interactive
9029    (list current-prefix-arg
9030          (let* ((methods (gnus-methods-using 'respool))
9031                 (methname
9032                  (symbol-name (or gnus-summary-respool-default-method
9033                                   (car (gnus-find-method-for-group
9034                                         gnus-newsgroup-name)))))
9035                 (method
9036                  (gnus-completing-read-with-default
9037                   methname "What backend do you want to use when respooling?"
9038                   methods nil t nil 'gnus-mail-method-history))
9039                 ms)
9040            (cond
9041             ((zerop (length (setq ms (gnus-servers-using-backend
9042                                       (intern method)))))
9043              (list (intern method) ""))
9044             ((= 1 (length ms))
9045              (car ms))
9046             (t
9047              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9048                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9049                            ms-alist))))))))
9050   (unless method
9051     (error "No method given for respooling"))
9052   (if (assoc (symbol-name
9053               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9054              (gnus-methods-using 'respool))
9055       (gnus-summary-move-article n nil method)
9056     (gnus-summary-copy-article n nil method)))
9057
9058 (defun gnus-summary-import-article (file &optional edit)
9059   "Import an arbitrary file into a mail newsgroup."
9060   (interactive "fImport file: \nP")
9061   (let ((group gnus-newsgroup-name)
9062         (now (current-time))
9063         atts lines group-art)
9064     (unless (gnus-check-backend-function 'request-accept-article group)
9065       (error "%s does not support article importing" group))
9066     (or (file-readable-p file)
9067         (not (file-regular-p file))
9068         (error "Can't read %s" file))
9069     (save-excursion
9070       (set-buffer (gnus-get-buffer-create " *import file*"))
9071       (erase-buffer)
9072       (nnheader-insert-file-contents file)
9073       (goto-char (point-min))
9074       (if (nnheader-article-p)
9075           (save-restriction
9076             (goto-char (point-min))
9077             (search-forward "\n\n" nil t)
9078             (narrow-to-region (point-min) (1- (point)))
9079             (goto-char (point-min))
9080             (unless (re-search-forward "^date:" nil t)
9081               (goto-char (point-max))
9082               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9083        ;; This doesn't look like an article, so we fudge some headers.
9084         (setq atts (file-attributes file)
9085               lines (count-lines (point-min) (point-max)))
9086         (insert "From: " (read-string "From: ") "\n"
9087                 "Subject: " (read-string "Subject: ") "\n"
9088                 "Date: " (message-make-date (nth 5 atts)) "\n"
9089                 "Message-ID: " (message-make-message-id) "\n"
9090                 "Lines: " (int-to-string lines) "\n"
9091                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9092       (setq group-art (gnus-request-accept-article group nil t))
9093       (kill-buffer (current-buffer)))
9094     (setq gnus-newsgroup-active (gnus-activate-group group))
9095     (forward-line 1)
9096     (gnus-summary-goto-article (cdr group-art) nil t)
9097     (when edit
9098       (gnus-summary-edit-article))))
9099
9100 (defun gnus-summary-create-article ()
9101   "Create an article in a mail newsgroup."
9102   (interactive)
9103   (let ((group gnus-newsgroup-name)
9104         (now (current-time))
9105         group-art)
9106     (unless (gnus-check-backend-function 'request-accept-article group)
9107       (error "%s does not support article importing" group))
9108     (save-excursion
9109       (set-buffer (gnus-get-buffer-create " *import file*"))
9110       (erase-buffer)
9111       (goto-char (point-min))
9112       ;; This doesn't look like an article, so we fudge some headers.
9113       (insert "From: " (read-string "From: ") "\n"
9114               "Subject: " (read-string "Subject: ") "\n"
9115               "Date: " (message-make-date now) "\n"
9116               "Message-ID: " (message-make-message-id) "\n")
9117       (setq group-art (gnus-request-accept-article group nil t))
9118       (kill-buffer (current-buffer)))
9119     (setq gnus-newsgroup-active (gnus-activate-group group))
9120     (forward-line 1)
9121     (gnus-summary-goto-article (cdr group-art) nil t)
9122     (gnus-summary-edit-article)))
9123
9124 (defun gnus-summary-article-posted-p ()
9125   "Say whether the current (mail) article is available from news as well.
9126 This will be the case if the article has both been mailed and posted."
9127   (interactive)
9128   (let ((id (mail-header-references (gnus-summary-article-header)))
9129         (gnus-override-method (car (gnus-refer-article-methods))))
9130     (if (gnus-request-head id "")
9131         (gnus-message 2 "The current message was found on %s"
9132                       gnus-override-method)
9133       (gnus-message 2 "The current message couldn't be found on %s"
9134                     gnus-override-method)
9135       nil)))
9136
9137 (defun gnus-summary-expire-articles (&optional now)
9138   "Expire all articles that are marked as expirable in the current group."
9139   (interactive)
9140   (when (and (not gnus-group-is-exiting-without-update-p)
9141              (gnus-check-backend-function
9142               'request-expire-articles gnus-newsgroup-name))
9143     ;; This backend supports expiry.
9144     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9145            (expirable (if total
9146                           (progn
9147                             ;; We need to update the info for
9148                             ;; this group for `gnus-list-of-read-articles'
9149                             ;; to give us the right answer.
9150                             (gnus-run-hooks 'gnus-exit-group-hook)
9151                             (gnus-summary-update-info)
9152                             (gnus-list-of-read-articles gnus-newsgroup-name))
9153                         (setq gnus-newsgroup-expirable
9154                               (sort gnus-newsgroup-expirable '<))))
9155            (expiry-wait (if now 'immediate
9156                           (gnus-group-find-parameter
9157                            gnus-newsgroup-name 'expiry-wait)))
9158            (nnmail-expiry-target
9159             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9160                 nnmail-expiry-target))
9161            es)
9162       (when expirable
9163         ;; There are expirable articles in this group, so we run them
9164         ;; through the expiry process.
9165         (gnus-message 6 "Expiring articles...")
9166         (unless (gnus-check-group gnus-newsgroup-name)
9167           (error "Can't open server for %s" gnus-newsgroup-name))
9168         ;; The list of articles that weren't expired is returned.
9169         (save-excursion
9170           (if expiry-wait
9171               (let ((nnmail-expiry-wait-function nil)
9172                     (nnmail-expiry-wait expiry-wait))
9173                 (setq es (gnus-request-expire-articles
9174                           expirable gnus-newsgroup-name)))
9175             (setq es (gnus-request-expire-articles
9176                       expirable gnus-newsgroup-name)))
9177           (unless total
9178             (setq gnus-newsgroup-expirable es))
9179           ;; We go through the old list of expirable, and mark all
9180           ;; really expired articles as nonexistent.
9181           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9182             (let ((gnus-use-cache nil))
9183               (dolist (article expirable)
9184                 (when (and (not (memq article es))
9185                            (gnus-data-find article))
9186                   (gnus-summary-mark-article article gnus-canceled-mark)
9187                   (run-hook-with-args 'gnus-summary-article-expire-hook
9188                                       'delete
9189                                       (gnus-data-header
9190                                        (assoc article (gnus-data-list nil)))
9191                                       gnus-newsgroup-name
9192                                       nil
9193                                       nil))))))
9194         (gnus-message 6 "Expiring articles...done")))))
9195
9196 (defun gnus-summary-expire-articles-now ()
9197   "Expunge all expirable articles in the current group.
9198 This means that *all* articles that are marked as expirable will be
9199 deleted forever, right now."
9200   (interactive)
9201   (or gnus-expert-user
9202       (gnus-yes-or-no-p
9203        "Are you really, really, really sure you want to delete all these messages? ")
9204       (error "Phew!"))
9205   (gnus-summary-expire-articles t))
9206
9207 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9208 (defun gnus-summary-delete-article (&optional n)
9209   "Delete the N next (mail) articles.
9210 This command actually deletes articles.  This is not a marking
9211 command.  The article will disappear forever from your life, never to
9212 return.
9213
9214 If N is negative, delete backwards.
9215 If N is nil and articles have been marked with the process mark,
9216 delete these instead.
9217
9218 If `gnus-novice-user' is non-nil you will be asked for
9219 confirmation before the articles are deleted."
9220   (interactive "P")
9221   (unless (gnus-check-backend-function 'request-expire-articles
9222                                        gnus-newsgroup-name)
9223     (error "The current newsgroup does not support article deletion"))
9224   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9225     (error "Couldn't open server"))
9226   ;; Compute the list of articles to delete.
9227   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9228         (nnmail-expiry-target 'delete)
9229         not-deleted)
9230     (if (and gnus-novice-user
9231              (not (gnus-yes-or-no-p
9232                    (format "Do you really want to delete %s forever? "
9233                            (if (> (length articles) 1)
9234                                (format "these %s articles" (length articles))
9235                              "this article")))))
9236         ()
9237       ;; Delete the articles.
9238       (setq not-deleted (gnus-request-expire-articles
9239                          articles gnus-newsgroup-name 'force))
9240       (while articles
9241         (gnus-summary-remove-process-mark (car articles))
9242         ;; The backend might not have been able to delete the article
9243         ;; after all.
9244         (unless (memq (car articles) not-deleted)
9245           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9246         (let* ((article (car articles))
9247                (id (mail-header-id (gnus-data-header 
9248                                     (assoc article (gnus-data-list nil))))))
9249           (run-hook-with-args 'gnus-summary-article-delete-hook
9250                               'delete id gnus-newsgroup-name nil
9251                               nil))
9252         (setq articles (cdr articles)))
9253       (when not-deleted
9254         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9255     (gnus-summary-position-point)
9256     (gnus-set-mode-line 'summary)
9257     not-deleted))
9258
9259 (defun gnus-summary-edit-article (&optional arg)
9260   "Edit the current article.
9261 This will have permanent effect only in mail groups.
9262 If ARG is nil, edit the decoded articles.
9263 If ARG is 1, edit the raw articles.
9264 If ARG is 2, edit the raw articles even in read-only groups.
9265 If ARG is 3, edit the articles with the current handles.
9266 Otherwise, allow editing of articles even in read-only
9267 groups."
9268   (interactive "P")
9269   (let (force raw current-handles)
9270     (cond
9271      ((null arg))
9272      ((eq arg 1)
9273       (setq raw t))
9274      ((eq arg 2)
9275       (setq raw t
9276             force t))
9277      ((eq arg 3)
9278       (setq current-handles
9279             (and (gnus-buffer-live-p gnus-article-buffer)
9280                  (with-current-buffer gnus-article-buffer
9281                    (prog1
9282                        gnus-article-mime-handles
9283                      (setq gnus-article-mime-handles nil))))))
9284      (t
9285       (setq force t)))
9286     (when (and raw (not force)
9287                (member gnus-newsgroup-name '("nndraft:delayed"
9288                                              "nndraft:drafts"
9289                                              "nndraft:queue")))
9290       (error "Can't edit the raw article in group %s"
9291              gnus-newsgroup-name))
9292     (save-excursion
9293       (set-buffer gnus-summary-buffer)
9294       (let ((mail-parse-charset gnus-newsgroup-charset)
9295             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9296         (gnus-set-global-variables)
9297         (when (and (not force)
9298                    (gnus-group-read-only-p))
9299           (error "The current newsgroup does not support article editing"))
9300         (gnus-summary-show-article t)
9301         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9302           (with-current-buffer gnus-article-buffer
9303             (mm-enable-multibyte)))
9304         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9305             (setq raw t))
9306         (gnus-article-edit-article
9307          (if raw 'ignore
9308            `(lambda ()
9309               (let ((mbl mml-buffer-list))
9310                 (setq mml-buffer-list nil)
9311                 (mime-to-mml ,'current-handles)
9312                 (let ((mbl1 mml-buffer-list))
9313                   (setq mml-buffer-list mbl)
9314                   (set (make-local-variable 'mml-buffer-list) mbl1))
9315                 (gnus-make-local-hook 'kill-buffer-hook)
9316                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9317          `(lambda (no-highlight)
9318             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9319                   (message-options message-options)
9320                   (message-options-set-recipient)
9321                   (mail-parse-ignored-charsets
9322                    ',gnus-newsgroup-ignored-charsets))
9323               ,(if (not raw) '(progn
9324                                 (mml-to-mime)
9325                                 (mml-destroy-buffers)
9326                                 (remove-hook 'kill-buffer-hook
9327                                              'mml-destroy-buffers t)
9328                                 (kill-local-variable 'mml-buffer-list)))
9329               (gnus-summary-edit-article-done
9330                ,(or (mail-header-references gnus-current-headers) "")
9331                ,(gnus-group-read-only-p)
9332                ,gnus-summary-buffer no-highlight))))))))
9333
9334 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9335
9336 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9337                                                  no-highlight)
9338   "Make edits to the current article permanent."
9339   (interactive)
9340   (save-excursion
9341    ;; The buffer restriction contains the entire article if it exists.
9342     (when (article-goto-body)
9343       (let ((lines (count-lines (point) (point-max)))
9344             (length (- (point-max) (point)))
9345             (case-fold-search t)
9346             (body (copy-marker (point))))
9347         (goto-char (point-min))
9348         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9349           (delete-region (match-beginning 1) (match-end 1))
9350           (insert (number-to-string length)))
9351         (goto-char (point-min))
9352         (when (re-search-forward
9353                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9354           (delete-region (match-beginning 1) (match-end 1))
9355           (insert (number-to-string length)))
9356         (goto-char (point-min))
9357         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9358           (delete-region (match-beginning 1) (match-end 1))
9359           (insert (number-to-string lines))))))
9360   ;; Replace the article.
9361   (let ((buf (current-buffer)))
9362     (with-temp-buffer
9363       (insert-buffer-substring buf)
9364
9365       (if (and (not read-only)
9366                (not (gnus-request-replace-article
9367                      (cdr gnus-article-current) (car gnus-article-current)
9368                      (current-buffer) t)))
9369           (error "Couldn't replace article")
9370         ;; Update the summary buffer.
9371         (if (and references
9372                  (equal (message-tokenize-header references " ")
9373                         (message-tokenize-header
9374                          (or (message-fetch-field "references") "") " ")))
9375             ;; We only have to update this line.
9376             (save-excursion
9377               (save-restriction
9378                 (message-narrow-to-head)
9379                 (let ((head (buffer-string))
9380                       header)
9381                   (with-temp-buffer
9382                     (insert (format "211 %d Article retrieved.\n"
9383                                     (cdr gnus-article-current)))
9384                     (insert head)
9385                     (insert ".\n")
9386                     (let ((nntp-server-buffer (current-buffer)))
9387                       (setq header (car (gnus-get-newsgroup-headers
9388                                          nil t))))
9389                     (save-excursion
9390                       (set-buffer gnus-summary-buffer)
9391                       (gnus-data-set-header
9392                        (gnus-data-find (cdr gnus-article-current))
9393                        header)
9394                       (gnus-summary-update-article-line
9395                        (cdr gnus-article-current) header)
9396                       (if (gnus-summary-goto-subject
9397                            (cdr gnus-article-current) nil t)
9398                           (gnus-summary-update-secondary-mark
9399                            (cdr gnus-article-current))))))))
9400           ;; Update threads.
9401           (set-buffer (or buffer gnus-summary-buffer))
9402           (gnus-summary-update-article (cdr gnus-article-current))
9403           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9404               (gnus-summary-update-secondary-mark
9405                (cdr gnus-article-current))))
9406         ;; Prettify the article buffer again.
9407         (unless no-highlight
9408           (save-excursion
9409             (set-buffer gnus-article-buffer)
9410             ;;;!!! Fix this -- article should be rehighlighted.
9411             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9412             (set-buffer gnus-original-article-buffer)
9413             (gnus-request-article
9414              (cdr gnus-article-current)
9415              (car gnus-article-current) (current-buffer))))
9416         ;; Prettify the summary buffer line.
9417         (when (gnus-visual-p 'summary-highlight 'highlight)
9418           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9419
9420 (defun gnus-summary-edit-wash (key)
9421   "Perform editing command KEY in the article buffer."
9422   (interactive
9423    (list
9424     (progn
9425       (message "%s" (concat (this-command-keys) "- "))
9426       (read-char))))
9427   (message "")
9428   (gnus-summary-edit-article)
9429   (execute-kbd-macro (concat (this-command-keys) key))
9430   (gnus-article-edit-done))
9431
9432 ;;; Respooling
9433
9434 (defun gnus-summary-respool-query (&optional silent trace)
9435   "Query where the respool algorithm would put this article."
9436   (interactive)
9437   (let (gnus-mark-article-hook)
9438     (gnus-summary-select-article)
9439     (save-excursion
9440       (set-buffer gnus-original-article-buffer)
9441       (save-restriction
9442         (message-narrow-to-head)
9443         (let ((groups (nnmail-article-group 'identity trace)))
9444           (unless silent
9445             (if groups
9446                 (message "This message would go to %s"
9447                          (mapconcat 'car groups ", "))
9448               (message "This message would go to no groups"))
9449             groups))))))
9450
9451 (defun gnus-summary-respool-trace ()
9452   "Trace where the respool algorithm would put this article.
9453 Display a buffer showing all fancy splitting patterns which matched."
9454   (interactive)
9455   (gnus-summary-respool-query nil t))
9456
9457 ;; Summary marking commands.
9458
9459 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9460   "Mark articles which has the same subject as read, and then select the next.
9461 If UNMARK is positive, remove any kind of mark.
9462 If UNMARK is negative, tick articles."
9463   (interactive "P")
9464   (when unmark
9465     (setq unmark (prefix-numeric-value unmark)))
9466   (let ((count
9467          (gnus-summary-mark-same-subject
9468           (gnus-summary-article-subject) unmark)))
9469     ;; Select next unread article.  If auto-select-same mode, should
9470     ;; select the first unread article.
9471     (gnus-summary-next-article t (and gnus-auto-select-same
9472                                       (gnus-summary-article-subject)))
9473     (gnus-message 7 "%d article%s marked as %s"
9474                   count (if (= count 1) " is" "s are")
9475                   (if unmark "unread" "read"))))
9476
9477 (defun gnus-summary-kill-same-subject (&optional unmark)
9478   "Mark articles which has the same subject as read.
9479 If UNMARK is positive, remove any kind of mark.
9480 If UNMARK is negative, tick articles."
9481   (interactive "P")
9482   (when unmark
9483     (setq unmark (prefix-numeric-value unmark)))
9484   (let ((count
9485          (gnus-summary-mark-same-subject
9486           (gnus-summary-article-subject) unmark)))
9487     ;; If marked as read, go to next unread subject.
9488     (when (null unmark)
9489       ;; Go to next unread subject.
9490       (gnus-summary-next-subject 1 t))
9491     (gnus-message 7 "%d articles are marked as %s"
9492                   count (if unmark "unread" "read"))))
9493
9494 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9495   "Mark articles with same SUBJECT as read, and return marked number.
9496 If optional argument UNMARK is positive, remove any kinds of marks.
9497 If optional argument UNMARK is negative, mark articles as unread instead."
9498   (let ((count 1))
9499     (save-excursion
9500       (cond
9501        ((null unmark)                   ; Mark as read.
9502         (while (and
9503                 (progn
9504                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9505                   (gnus-summary-show-thread) t)
9506                 (gnus-summary-find-subject subject))
9507           (setq count (1+ count))))
9508        ((> unmark 0)                    ; Tick.
9509         (while (and
9510                 (progn
9511                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9512                   (gnus-summary-show-thread) t)
9513                 (gnus-summary-find-subject subject))
9514           (setq count (1+ count))))
9515        (t                               ; Mark as unread.
9516         (while (and
9517                 (progn
9518                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9519                   (gnus-summary-show-thread) t)
9520                 (gnus-summary-find-subject subject))
9521           (setq count (1+ count)))))
9522       (gnus-set-mode-line 'summary)
9523       ;; Return the number of marked articles.
9524       count)))
9525
9526 (defun gnus-summary-mark-as-processable (n &optional unmark)
9527   "Set the process mark on the next N articles.
9528 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9529 the process mark instead.  The difference between N and the actual
9530 number of articles marked is returned."
9531   (interactive "P")
9532   (if (and (null n) (gnus-region-active-p))
9533       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9534     (setq n (prefix-numeric-value n))
9535     (let ((backward (< n 0))
9536           (n (abs n)))
9537       (while (and
9538               (> n 0)
9539               (if unmark
9540                   (gnus-summary-remove-process-mark
9541                    (gnus-summary-article-number))
9542                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9543               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9544         (setq n (1- n)))
9545       (when (/= 0 n)
9546         (gnus-message 7 "No more articles"))
9547       (gnus-summary-recenter)
9548       (gnus-summary-position-point)
9549       n)))
9550
9551 (defun gnus-summary-unmark-as-processable (n)
9552   "Remove the process mark from the next N articles.
9553 If N is negative, unmark backward instead.  The difference between N and
9554 the actual number of articles unmarked is returned."
9555   (interactive "P")
9556   (gnus-summary-mark-as-processable n t))
9557
9558 (defun gnus-summary-unmark-all-processable ()
9559   "Remove the process mark from all articles."
9560   (interactive)
9561   (save-excursion
9562     (while gnus-newsgroup-processable
9563       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9564   (gnus-summary-position-point))
9565
9566 (defun gnus-summary-add-mark (article type)
9567   "Mark ARTICLE with a mark of TYPE."
9568   (let ((vtype (car (assq type gnus-article-mark-lists)))
9569         var)
9570     (if (not vtype)
9571         (error "No such mark type: %s" type)
9572       (setq var (intern (format "gnus-newsgroup-%s" type)))
9573       (set var (cons article (symbol-value var)))
9574       (if (memq type '(processable cached replied forwarded recent saved))
9575           (gnus-summary-update-secondary-mark article)
9576         ;;; !!! This is bogus.  We should find out what primary
9577         ;;; !!! mark we want to set.
9578         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9579
9580 (defun gnus-summary-mark-as-expirable (n)
9581   "Mark N articles forward as expirable.
9582 If N is negative, mark backward instead.  The difference between N and
9583 the actual number of articles marked is returned."
9584   (interactive "p")
9585   (gnus-summary-mark-forward n gnus-expirable-mark))
9586
9587 (defun gnus-summary-mark-as-spam (n)
9588   "Mark N articles forward as spam.
9589 If N is negative, mark backward instead.  The difference between N and
9590 the actual number of articles marked is returned."
9591   (interactive "p")
9592   (gnus-summary-mark-forward n gnus-spam-mark))
9593
9594 (defun gnus-summary-mark-article-as-replied (article)
9595   "Mark ARTICLE as replied to and update the summary line.
9596 ARTICLE can also be a list of articles."
9597   (interactive (list (gnus-summary-article-number)))
9598   (let ((articles (if (listp article) article (list article))))
9599     (dolist (article articles)
9600       (unless (numberp article)
9601         (error "%s is not a number" article))
9602       (push article gnus-newsgroup-replied)
9603       (let ((buffer-read-only nil))
9604         (when (gnus-summary-goto-subject article nil t)
9605           (gnus-summary-update-secondary-mark article))))))
9606
9607 (defun gnus-summary-mark-article-as-forwarded (article)
9608   "Mark ARTICLE as forwarded and update the summary line.
9609 ARTICLE can also be a list of articles."
9610   (let ((articles (if (listp article) article (list article))))
9611     (dolist (article articles)
9612       (push article gnus-newsgroup-forwarded)
9613       (let ((buffer-read-only nil))
9614         (when (gnus-summary-goto-subject article nil t)
9615           (gnus-summary-update-secondary-mark article))))))
9616
9617 (defun gnus-summary-set-bookmark (article)
9618   "Set a bookmark in current article."
9619   (interactive (list (gnus-summary-article-number)))
9620   (when (or (not (get-buffer gnus-article-buffer))
9621             (not gnus-current-article)
9622             (not gnus-article-current)
9623             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9624     (error "No current article selected"))
9625   ;; Remove old bookmark, if one exists.
9626   (gnus-pull article gnus-newsgroup-bookmarks)
9627   ;; Set the new bookmark, which is on the form
9628   ;; (article-number . line-number-in-body).
9629   (push
9630    (cons article
9631          (save-excursion
9632            (set-buffer gnus-article-buffer)
9633            (count-lines
9634             (min (point)
9635                  (save-excursion
9636                    (article-goto-body)
9637                    (point)))
9638             (point))))
9639    gnus-newsgroup-bookmarks)
9640   (gnus-message 6 "A bookmark has been added to the current article."))
9641
9642 (defun gnus-summary-remove-bookmark (article)
9643   "Remove the bookmark from the current article."
9644   (interactive (list (gnus-summary-article-number)))
9645   ;; Remove old bookmark, if one exists.
9646   (if (not (assq article gnus-newsgroup-bookmarks))
9647       (gnus-message 6 "No bookmark in current article.")
9648     (gnus-pull article gnus-newsgroup-bookmarks)
9649     (gnus-message 6 "Removed bookmark.")))
9650
9651 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9652 (defun gnus-summary-mark-as-dormant (n)
9653   "Mark N articles forward as dormant.
9654 If N is negative, mark backward instead.  The difference between N and
9655 the actual number of articles marked is returned."
9656   (interactive "p")
9657   (gnus-summary-mark-forward n gnus-dormant-mark))
9658
9659 (defun gnus-summary-set-process-mark (article)
9660   "Set the process mark on ARTICLE and update the summary line."
9661   (setq gnus-newsgroup-processable
9662         (cons article
9663               (delq article gnus-newsgroup-processable)))
9664   (when (gnus-summary-goto-subject article)
9665     (gnus-summary-show-thread)
9666     (gnus-summary-goto-subject article)
9667     (gnus-summary-update-secondary-mark article)))
9668
9669 (defun gnus-summary-remove-process-mark (article)
9670   "Remove the process mark from ARTICLE and update the summary line."
9671   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9672   (when (gnus-summary-goto-subject article)
9673     (gnus-summary-show-thread)
9674     (gnus-summary-goto-subject article)
9675     (gnus-summary-update-secondary-mark article)))
9676
9677 (defun gnus-summary-set-saved-mark (article)
9678   "Set the process mark on ARTICLE and update the summary line."
9679   (push article gnus-newsgroup-saved)
9680   (when (gnus-summary-goto-subject article)
9681     (gnus-summary-update-secondary-mark article)))
9682
9683 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9684   "Mark N articles as read forwards.
9685 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9686 The difference between N and the actual number of articles marked is
9687 returned.
9688 If NO-EXPIRE, auto-expiry will be inhibited."
9689   (interactive "p")
9690   (gnus-summary-show-thread)
9691   (let ((backward (< n 0))
9692         (gnus-summary-goto-unread
9693          (and gnus-summary-goto-unread
9694               (not (eq gnus-summary-goto-unread 'never))
9695               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9696                                     gnus-ticked-mark gnus-dormant-mark)))))
9697         (n (abs n))
9698         (mark (or mark gnus-del-mark)))
9699     (while (and (> n 0)
9700                 (gnus-summary-mark-article nil mark no-expire)
9701                 (zerop (gnus-summary-next-subject
9702                         (if backward -1 1)
9703                         (and gnus-summary-goto-unread
9704                              (not (eq gnus-summary-goto-unread 'never)))
9705                         t)))
9706       (setq n (1- n)))
9707     (when (/= 0 n)
9708       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9709     (gnus-summary-recenter)
9710     (gnus-summary-position-point)
9711     (gnus-set-mode-line 'summary)
9712     n))
9713
9714 (defun gnus-summary-mark-article-as-read (mark)
9715   "Mark the current article quickly as read with MARK."
9716   (let ((article (gnus-summary-article-number)))
9717     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9718     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9719     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9720     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9721     (push (cons article mark) gnus-newsgroup-reads)
9722     ;; Possibly remove from cache, if that is used.
9723     (when gnus-use-cache
9724       (gnus-cache-enter-remove-article article))
9725     ;; Allow the backend to change the mark.
9726     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9727     ;; Check for auto-expiry.
9728     (when (and gnus-newsgroup-auto-expire
9729                (memq mark gnus-auto-expirable-marks))
9730       (setq mark gnus-expirable-mark)
9731       ;; Let the backend know about the mark change.
9732       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9733       (push article gnus-newsgroup-expirable))
9734     ;; Set the mark in the buffer.
9735     (gnus-summary-update-mark mark 'unread)
9736     t))
9737
9738 (defun gnus-summary-mark-article-as-unread (mark)
9739   "Mark the current article quickly as unread with MARK."
9740   (let* ((article (gnus-summary-article-number))
9741          (old-mark (gnus-summary-article-mark article)))
9742     ;; Allow the backend to change the mark.
9743     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9744     (if (eq mark old-mark)
9745         t
9746       (if (<= article 0)
9747           (progn
9748             (gnus-error 1 "Can't mark negative article numbers")
9749             nil)
9750         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9751         (setq gnus-newsgroup-spam-marked
9752               (delq article gnus-newsgroup-spam-marked))
9753         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9754         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9755         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9756         (cond ((= mark gnus-ticked-mark)
9757                (setq gnus-newsgroup-marked
9758                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9759                                               article)))
9760               ((= mark gnus-spam-mark)
9761                (setq gnus-newsgroup-spam-marked
9762                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9763                                               article)))
9764               ((= mark gnus-dormant-mark)
9765                (setq gnus-newsgroup-dormant
9766                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9767                                               article)))
9768               (t
9769                (setq gnus-newsgroup-unreads
9770                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9771                                               article))))
9772         (gnus-pull article gnus-newsgroup-reads)
9773
9774         ;; See whether the article is to be put in the cache.
9775         (and gnus-use-cache
9776              (vectorp (gnus-summary-article-header article))
9777              (save-excursion
9778                (gnus-cache-possibly-enter-article
9779                 gnus-newsgroup-name article
9780                 (= mark gnus-ticked-mark)
9781                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9782
9783         ;; Fix the mark.
9784         (gnus-summary-update-mark mark 'unread)
9785         t))))
9786
9787 (defun gnus-summary-mark-article (&optional article mark no-expire)
9788   "Mark ARTICLE with MARK.  MARK can be any character.
9789 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9790 `??' (dormant) and `?E' (expirable).
9791 If MARK is nil, then the default character `?r' is used.
9792 If ARTICLE is nil, then the article on the current line will be
9793 marked.
9794 If NO-EXPIRE, auto-expiry will be inhibited."
9795   ;; The mark might be a string.
9796   (when (stringp mark)
9797     (setq mark (aref mark 0)))
9798   ;; If no mark is given, then we check auto-expiring.
9799   (when (null mark)
9800     (setq mark gnus-del-mark))
9801   (when (and (not no-expire)
9802              gnus-newsgroup-auto-expire
9803              (memq mark gnus-auto-expirable-marks))
9804     (setq mark gnus-expirable-mark))
9805   (let ((article (or article (gnus-summary-article-number)))
9806         (old-mark (gnus-summary-article-mark article)))
9807     ;; Allow the backend to change the mark.
9808     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9809     (if (eq mark old-mark)
9810         t
9811       (unless article
9812         (error "No article on current line"))
9813       (if (not (if (or (= mark gnus-unread-mark)
9814                        (= mark gnus-ticked-mark)
9815                        (= mark gnus-spam-mark)
9816                        (= mark gnus-dormant-mark))
9817                    (gnus-mark-article-as-unread article mark)
9818                  (gnus-mark-article-as-read article mark)))
9819           t
9820         ;; See whether the article is to be put in the cache.
9821         (and gnus-use-cache
9822              (not (= mark gnus-canceled-mark))
9823              (vectorp (gnus-summary-article-header article))
9824              (save-excursion
9825                (gnus-cache-possibly-enter-article
9826                 gnus-newsgroup-name article
9827                 (= mark gnus-ticked-mark)
9828                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9829
9830         (when (gnus-summary-goto-subject article nil t)
9831           (let ((buffer-read-only nil))
9832             (gnus-summary-show-thread)
9833             ;; Fix the mark.
9834             (gnus-summary-update-mark mark 'unread)
9835             t))))))
9836
9837 (defun gnus-summary-update-secondary-mark (article)
9838   "Update the secondary (read, process, cache) mark."
9839   (gnus-summary-update-mark
9840    (cond ((memq article gnus-newsgroup-processable)
9841           gnus-process-mark)
9842          ((memq article gnus-newsgroup-cached)
9843           gnus-cached-mark)
9844          ((memq article gnus-newsgroup-replied)
9845           gnus-replied-mark)
9846          ((memq article gnus-newsgroup-forwarded)
9847           gnus-forwarded-mark)
9848          ((memq article gnus-newsgroup-saved)
9849           gnus-saved-mark)
9850          ((memq article gnus-newsgroup-recent)
9851           gnus-recent-mark)
9852          ((memq article gnus-newsgroup-unseen)
9853           gnus-unseen-mark)
9854          (t gnus-no-mark))
9855    'replied)
9856   (when (gnus-visual-p 'summary-highlight 'highlight)
9857     (gnus-run-hooks 'gnus-summary-update-hook))
9858   t)
9859
9860 (defun gnus-summary-update-download-mark (article)
9861   "Update the download mark."
9862   (gnus-summary-update-mark
9863    (cond ((memq article gnus-newsgroup-undownloaded) 
9864           gnus-undownloaded-mark)
9865          (gnus-newsgroup-agentized
9866           gnus-downloaded-mark)
9867          (t
9868           gnus-no-mark))
9869    'download)
9870   (gnus-summary-update-line t)
9871   t)
9872
9873 (defun gnus-summary-update-mark (mark type)
9874   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9875         (buffer-read-only nil))
9876     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9877     (when forward
9878       (when (looking-at "\r")
9879         (incf forward))
9880       (when (<= (+ forward (point)) (point-max))
9881         ;; Go to the right position on the line.
9882         (goto-char (+ forward (point)))
9883         ;; Replace the old mark with the new mark.
9884         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9885         ;; Optionally update the marks by some user rule.
9886         (when (eq type 'unread)
9887           (gnus-data-set-mark
9888            (gnus-data-find (gnus-summary-article-number)) mark)
9889           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9890
9891 (defun gnus-mark-article-as-read (article &optional mark)
9892   "Enter ARTICLE in the pertinent lists and remove it from others."
9893   ;; Make the article expirable.
9894   (let ((mark (or mark gnus-del-mark)))
9895     (setq gnus-newsgroup-expirable
9896           (if (= mark gnus-expirable-mark)
9897               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9898             (delq article gnus-newsgroup-expirable)))
9899     ;; Remove from unread and marked lists.
9900     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9901     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9902     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9903     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9904     (push (cons article mark) gnus-newsgroup-reads)
9905     ;; Possibly remove from cache, if that is used.
9906     (when gnus-use-cache
9907       (gnus-cache-enter-remove-article article))
9908     t))
9909
9910 (defun gnus-mark-article-as-unread (article &optional mark)
9911   "Enter ARTICLE in the pertinent lists and remove it from others."
9912   (let ((mark (or mark gnus-ticked-mark)))
9913     (if (<= article 0)
9914         (progn
9915           (gnus-error 1 "Can't mark negative article numbers")
9916           nil)
9917       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9918             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9919             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9920             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9921             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9922
9923       ;; Unsuppress duplicates?
9924       (when gnus-suppress-duplicates
9925         (gnus-dup-unsuppress-article article))
9926
9927       (cond ((= mark gnus-ticked-mark)
9928              (setq gnus-newsgroup-marked
9929                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9930             ((= mark gnus-spam-mark)
9931              (setq gnus-newsgroup-spam-marked
9932                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9933                                             article)))
9934             ((= mark gnus-dormant-mark)
9935              (setq gnus-newsgroup-dormant
9936                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9937             (t
9938              (setq gnus-newsgroup-unreads
9939                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9940       (gnus-pull article gnus-newsgroup-reads)
9941       t)))
9942
9943 (defalias 'gnus-summary-mark-as-unread-forward
9944   'gnus-summary-tick-article-forward)
9945 (make-obsolete 'gnus-summary-mark-as-unread-forward
9946                'gnus-summary-tick-article-forward)
9947 (defun gnus-summary-tick-article-forward (n)
9948   "Tick N articles forwards.
9949 If N is negative, tick backwards instead.
9950 The difference between N and the number of articles ticked is returned."
9951   (interactive "p")
9952   (gnus-summary-mark-forward n gnus-ticked-mark))
9953
9954 (defalias 'gnus-summary-mark-as-unread-backward
9955   'gnus-summary-tick-article-backward)
9956 (make-obsolete 'gnus-summary-mark-as-unread-backward
9957                'gnus-summary-tick-article-backward)
9958 (defun gnus-summary-tick-article-backward (n)
9959   "Tick N articles backwards.
9960 The difference between N and the number of articles ticked is returned."
9961   (interactive "p")
9962   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9963
9964 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9965 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9966 (defun gnus-summary-tick-article (&optional article clear-mark)
9967   "Mark current article as unread.
9968 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9969 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9970   (interactive)
9971   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9972                                        gnus-ticked-mark)))
9973
9974 (defun gnus-summary-mark-as-read-forward (n)
9975   "Mark N articles as read forwards.
9976 If N is negative, mark backwards instead.
9977 The difference between N and the actual number of articles marked is
9978 returned."
9979   (interactive "p")
9980   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9981
9982 (defun gnus-summary-mark-as-read-backward (n)
9983   "Mark the N articles as read backwards.
9984 The difference between N and the actual number of articles marked is
9985 returned."
9986   (interactive "p")
9987   (gnus-summary-mark-forward
9988    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9989
9990 (defun gnus-summary-mark-as-read (&optional article mark)
9991   "Mark current article as read.
9992 ARTICLE specifies the article to be marked as read.
9993 MARK specifies a string to be inserted at the beginning of the line."
9994   (gnus-summary-mark-article article mark))
9995
9996 (defun gnus-summary-clear-mark-forward (n)
9997   "Clear marks from N articles forward.
9998 If N is negative, clear backward instead.
9999 The difference between N and the number of marks cleared is returned."
10000   (interactive "p")
10001   (gnus-summary-mark-forward n gnus-unread-mark))
10002
10003 (defun gnus-summary-clear-mark-backward (n)
10004   "Clear marks from N articles backward.
10005 The difference between N and the number of marks cleared is returned."
10006   (interactive "p")
10007   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10008
10009 (defun gnus-summary-mark-unread-as-read ()
10010   "Intended to be used by `gnus-summary-mark-article-hook'."
10011   (when (memq gnus-current-article gnus-newsgroup-unreads)
10012     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10013
10014 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10015   "Intended to be used by `gnus-summary-mark-article-hook'."
10016   (let ((mark (gnus-summary-article-mark)))
10017     (when (or (gnus-unread-mark-p mark)
10018               (gnus-read-mark-p mark))
10019       (gnus-summary-mark-article gnus-current-article
10020                                  (or new-mark gnus-read-mark)))))
10021
10022 (defun gnus-summary-mark-unread-as-ticked ()
10023   "Intended to be used by `gnus-summary-mark-article-hook'."
10024   (when (memq gnus-current-article gnus-newsgroup-unreads)
10025     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10026
10027 (defun gnus-summary-mark-region-as-read (point mark all)
10028   "Mark all unread articles between point and mark as read.
10029 If given a prefix, mark all articles between point and mark as read,
10030 even ticked and dormant ones."
10031   (interactive "r\nP")
10032   (save-excursion
10033     (let (article)
10034       (goto-char point)
10035       (beginning-of-line)
10036       (while (and
10037               (< (point) mark)
10038               (progn
10039                 (when (or all
10040                           (memq (setq article (gnus-summary-article-number))
10041                                 gnus-newsgroup-unreads))
10042                   (gnus-summary-mark-article article gnus-del-mark))
10043                 t)
10044               (gnus-summary-find-next))))))
10045
10046 (defun gnus-summary-mark-below (score mark)
10047   "Mark articles with score less than SCORE with MARK."
10048   (interactive "P\ncMark: ")
10049   (setq score (if score
10050                   (prefix-numeric-value score)
10051                 (or gnus-summary-default-score 0)))
10052   (save-excursion
10053     (set-buffer gnus-summary-buffer)
10054     (goto-char (point-min))
10055     (while
10056         (progn
10057           (and (< (gnus-summary-article-score) score)
10058                (gnus-summary-mark-article nil mark))
10059           (gnus-summary-find-next)))))
10060
10061 (defun gnus-summary-kill-below (&optional score)
10062   "Mark articles with score below SCORE as read."
10063   (interactive "P")
10064   (gnus-summary-mark-below score gnus-killed-mark))
10065
10066 (defun gnus-summary-clear-above (&optional score)
10067   "Clear all marks from articles with score above SCORE."
10068   (interactive "P")
10069   (gnus-summary-mark-above score gnus-unread-mark))
10070
10071 (defun gnus-summary-tick-above (&optional score)
10072   "Tick all articles with score above SCORE."
10073   (interactive "P")
10074   (gnus-summary-mark-above score gnus-ticked-mark))
10075
10076 (defun gnus-summary-mark-above (score mark)
10077   "Mark articles with score over SCORE with MARK."
10078   (interactive "P\ncMark: ")
10079   (setq score (if score
10080                   (prefix-numeric-value score)
10081                 (or gnus-summary-default-score 0)))
10082   (save-excursion
10083     (set-buffer gnus-summary-buffer)
10084     (goto-char (point-min))
10085     (while (and (progn
10086                   (when (> (gnus-summary-article-score) score)
10087                     (gnus-summary-mark-article nil mark))
10088                   t)
10089                 (gnus-summary-find-next)))))
10090
10091 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10092 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10093 (defun gnus-summary-limit-include-expunged (&optional no-error)
10094   "Display all the hidden articles that were expunged for low scores."
10095   (interactive)
10096   (let ((buffer-read-only nil))
10097     (let ((scored gnus-newsgroup-scored)
10098           headers h)
10099       (while scored
10100         (unless (gnus-summary-article-header (caar scored))
10101           (and (setq h (gnus-number-to-header (caar scored)))
10102                (< (cdar scored) gnus-summary-expunge-below)
10103                (push h headers)))
10104         (setq scored (cdr scored)))
10105       (if (not headers)
10106           (when (not no-error)
10107             (error "No expunged articles hidden"))
10108         (goto-char (point-min))
10109         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10110         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10111         (mapcar (lambda (x) (push (mail-header-number x)
10112                                   gnus-newsgroup-limit))
10113                 headers)
10114         (gnus-summary-prepare-unthreaded (nreverse headers))
10115         (goto-char (point-min))
10116         (gnus-summary-position-point)
10117         t))))
10118
10119 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10120   "Mark all unread articles in this newsgroup as read.
10121 If prefix argument ALL is non-nil, ticked and dormant articles will
10122 also be marked as read.
10123 If QUIETLY is non-nil, no questions will be asked.
10124 If TO-HERE is non-nil, it should be a point in the buffer.  All
10125 articles before (after, if REVERSE is set) this point will be marked as read.
10126 Note that this function will only catch up the unread article
10127 in the current summary buffer limitation.
10128 The number of articles marked as read is returned."
10129   (interactive "P")
10130   (prog1
10131       (save-excursion
10132         (when (or quietly
10133                   (not gnus-interactive-catchup) ;Without confirmation?
10134                   gnus-expert-user
10135                   (gnus-y-or-n-p
10136                    (if all
10137                        "Mark absolutely all articles as read? "
10138                      "Mark all unread articles as read? ")))
10139           (if (and not-mark
10140                    (not gnus-newsgroup-adaptive)
10141                    (not gnus-newsgroup-auto-expire)
10142                    (not gnus-suppress-duplicates)
10143                    (or (not gnus-use-cache)
10144                        (eq gnus-use-cache 'passive)))
10145               (progn
10146                 (when all
10147                   (setq gnus-newsgroup-marked nil
10148                         gnus-newsgroup-spam-marked nil
10149                         gnus-newsgroup-dormant nil))
10150                 (setq gnus-newsgroup-unreads
10151                       (gnus-sorted-nunion
10152                        (gnus-intersection gnus-newsgroup-unreads
10153                                           gnus-newsgroup-downloadable)
10154                        gnus-newsgroup-unfetched)))
10155             ;; We actually mark all articles as canceled, which we
10156             ;; have to do when using auto-expiry or adaptive scoring.
10157             (gnus-summary-show-all-threads)
10158             (if (and to-here reverse)
10159                 (progn
10160                   (goto-char to-here)
10161                   (gnus-summary-mark-read-and-unread-as-read gnus-catchup-mark)
10162                   (while (gnus-summary-find-next (not all))
10163                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10164               (when (gnus-summary-first-subject (not all))
10165                 (while (and
10166                         (if to-here (< (point) to-here) t)
10167                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10168                         (gnus-summary-find-next (not all))))))
10169             (gnus-set-mode-line 'summary))
10170           t))
10171     (gnus-summary-position-point)))
10172
10173 (defun gnus-summary-catchup-to-here (&optional all)
10174   "Mark all unticked articles before the current one as read.
10175 If ALL is non-nil, also mark ticked and dormant articles as read."
10176   (interactive "P")
10177   (save-excursion
10178     (gnus-save-hidden-threads
10179       (let ((beg (point)))
10180         ;; We check that there are unread articles.
10181         (when (or all (gnus-summary-find-prev))
10182           (gnus-summary-catchup all t beg)))))
10183   (gnus-summary-position-point))
10184
10185 (defun gnus-summary-catchup-from-here (&optional all)
10186   "Mark all unticked articles after (and including) the current one as read.
10187 If ALL is non-nil, also mark ticked and dormant articles as read."
10188   (interactive "P")
10189   (save-excursion
10190     (gnus-save-hidden-threads
10191       (let ((beg (point)))
10192         ;; We check that there are unread articles.
10193         (when (or all (gnus-summary-find-next))
10194           (gnus-summary-catchup all t beg nil t)))))
10195
10196   (gnus-summary-position-point))
10197 (defun gnus-summary-catchup-all (&optional quietly)
10198   "Mark all articles in this newsgroup as read.
10199 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10200 instead, which marks only unread articles as read."
10201   (interactive "P")
10202   (gnus-summary-catchup t quietly))
10203
10204 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10205   "Mark all unread articles in this group as read, then exit.
10206 If prefix argument ALL is non-nil, all articles are marked as read.
10207 If QUIETLY is non-nil, no questions will be asked."
10208   (interactive "P")
10209   (when (gnus-summary-catchup all quietly nil 'fast)
10210     ;; Select next newsgroup or exit.
10211     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10212              (eq gnus-auto-select-next 'quietly))
10213         (gnus-summary-next-group nil)
10214       (gnus-summary-exit))))
10215
10216 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10217   "Mark all articles in this newsgroup as read, and then exit.
10218 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10219 instead, which marks only unread articles as read."
10220   (interactive "P")
10221   (gnus-summary-catchup-and-exit t quietly))
10222
10223 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10224   "Mark all articles in this group as read and select the next group.
10225 If given a prefix, mark all articles, unread as well as ticked, as
10226 read."
10227   (interactive "P")
10228   (save-excursion
10229     (gnus-summary-catchup all))
10230   (gnus-summary-next-group))
10231
10232 ;;;
10233 ;;; with article
10234 ;;;
10235
10236 (defmacro gnus-with-article (article &rest forms)
10237   "Select ARTICLE and perform FORMS in the original article buffer.
10238 Then replace the article with the result."
10239   `(progn
10240      ;; We don't want the article to be marked as read.
10241      (let (gnus-mark-article-hook)
10242        (gnus-summary-select-article t t nil ,article))
10243      (set-buffer gnus-original-article-buffer)
10244      ,@forms
10245      (if (not (gnus-check-backend-function
10246                'request-replace-article (car gnus-article-current)))
10247          (gnus-message 5 "Read-only group; not replacing")
10248        (unless (gnus-request-replace-article
10249                 ,article (car gnus-article-current)
10250                 (current-buffer) t)
10251          (error "Couldn't replace article")))
10252      ;; The cache and backlog have to be flushed somewhat.
10253      (when gnus-keep-backlog
10254        (gnus-backlog-remove-article
10255         (car gnus-article-current) (cdr gnus-article-current)))
10256      (when gnus-use-cache
10257        (gnus-cache-update-article
10258         (car gnus-article-current) (cdr gnus-article-current)))))
10259
10260 (put 'gnus-with-article 'lisp-indent-function 1)
10261 (put 'gnus-with-article 'edebug-form-spec '(form body))
10262
10263 ;; Thread-based commands.
10264
10265 (defun gnus-summary-articles-in-thread (&optional article)
10266   "Return a list of all articles in the current thread.
10267 If ARTICLE is non-nil, return all articles in the thread that starts
10268 with that article."
10269   (let* ((article (or article (gnus-summary-article-number)))
10270          (data (gnus-data-find-list article))
10271          (top-level (gnus-data-level (car data)))
10272          (top-subject
10273           (cond ((null gnus-thread-operation-ignore-subject)
10274                  (gnus-simplify-subject-re
10275                   (mail-header-subject (gnus-data-header (car data)))))
10276                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10277                  (gnus-simplify-subject-fuzzy
10278                   (mail-header-subject (gnus-data-header (car data)))))
10279                 (t nil)))
10280          (end-point (save-excursion
10281                       (if (gnus-summary-go-to-next-thread)
10282                           (point) (point-max))))
10283          articles)
10284     (while (and data
10285                 (< (gnus-data-pos (car data)) end-point))
10286       (when (or (not top-subject)
10287                 (string= top-subject
10288                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10289                              (gnus-simplify-subject-fuzzy
10290                               (mail-header-subject
10291                                (gnus-data-header (car data))))
10292                            (gnus-simplify-subject-re
10293                             (mail-header-subject
10294                              (gnus-data-header (car data)))))))
10295         (push (gnus-data-number (car data)) articles))
10296       (unless (and (setq data (cdr data))
10297                    (> (gnus-data-level (car data)) top-level))
10298         (setq data nil)))
10299     ;; Return the list of articles.
10300     (nreverse articles)))
10301
10302 (defun gnus-summary-rethread-current ()
10303   "Rethread the thread the current article is part of."
10304   (interactive)
10305   (let* ((gnus-show-threads t)
10306          (article (gnus-summary-article-number))
10307          (id (mail-header-id (gnus-summary-article-header)))
10308          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10309     (unless id
10310       (error "No article on the current line"))
10311     (gnus-rebuild-thread id)
10312     (gnus-summary-goto-subject article)))
10313
10314 (defun gnus-summary-reparent-thread ()
10315   "Make the current article child of the marked (or previous) article.
10316
10317 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10318 is non-nil or the Subject: of both articles are the same."
10319   (interactive)
10320   (unless (not (gnus-group-read-only-p))
10321     (error "The current newsgroup does not support article editing"))
10322   (unless (<= (length gnus-newsgroup-processable) 1)
10323     (error "No more than one article may be marked"))
10324   (save-window-excursion
10325     (let ((gnus-article-buffer " *reparent*")
10326           (current-article (gnus-summary-article-number))
10327           ;; First grab the marked article, otherwise one line up.
10328           (parent-article (if (not (null gnus-newsgroup-processable))
10329                               (car gnus-newsgroup-processable)
10330                             (save-excursion
10331                               (if (eq (forward-line -1) 0)
10332                                   (gnus-summary-article-number)
10333                                 (error "Beginning of summary buffer"))))))
10334       (unless (not (eq current-article parent-article))
10335         (error "An article may not be self-referential"))
10336       (let ((message-id (mail-header-id
10337                          (gnus-summary-article-header parent-article))))
10338         (unless (and message-id (not (equal message-id "")))
10339           (error "No message-id in desired parent"))
10340         (gnus-with-article current-article
10341           (save-restriction
10342             (goto-char (point-min))
10343             (message-narrow-to-head)
10344             (if (re-search-forward "^References: " nil t)
10345                 (progn
10346                   (re-search-forward "^[^ \t]" nil t)
10347                   (forward-line -1)
10348                   (end-of-line)
10349                   (insert " " message-id))
10350               (insert "References: " message-id "\n"))))
10351         (set-buffer gnus-summary-buffer)
10352         (gnus-summary-unmark-all-processable)
10353         (gnus-summary-update-article current-article)
10354         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10355             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10356         (gnus-summary-rethread-current)
10357         (gnus-message 3 "Article %d is now the child of article %d"
10358                       current-article parent-article)))))
10359
10360 (defun gnus-summary-toggle-threads (&optional arg)
10361   "Toggle showing conversation threads.
10362 If ARG is positive number, turn showing conversation threads on."
10363   (interactive "P")
10364   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10365     (setq gnus-show-threads
10366           (if (null arg) (not gnus-show-threads)
10367             (> (prefix-numeric-value arg) 0)))
10368     (gnus-summary-prepare)
10369     (gnus-summary-goto-subject current)
10370     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10371     (gnus-summary-position-point)))
10372
10373 (defun gnus-summary-show-all-threads ()
10374   "Show all threads."
10375   (interactive)
10376   (save-excursion
10377     (let ((buffer-read-only nil))
10378       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10379   (gnus-summary-position-point))
10380
10381 (defun gnus-summary-show-thread ()
10382   "Show thread subtrees.
10383 Returns nil if no thread was there to be shown."
10384   (interactive)
10385   (let ((buffer-read-only nil)
10386         (orig (point))
10387         (end (gnus-point-at-eol))
10388         ;; Leave point at bol
10389         (beg (progn (beginning-of-line) (point))))
10390     (prog1
10391         ;; Any hidden lines here?
10392         (search-forward "\r" end t)
10393       (subst-char-in-region beg end ?\^M ?\n t)
10394       (goto-char orig)
10395       (gnus-summary-position-point))))
10396
10397 (defun gnus-summary-maybe-hide-threads ()
10398   "If requested, hide the threads that should be hidden."
10399   (when (and gnus-show-threads
10400              gnus-thread-hide-subtree)
10401     (gnus-summary-hide-all-threads
10402      (if (or (consp gnus-thread-hide-subtree)
10403              (functionp gnus-thread-hide-subtree))
10404          (gnus-make-predicate gnus-thread-hide-subtree)
10405        nil))))
10406
10407 ;;; Hiding predicates.
10408
10409 (defun gnus-article-unread-p (header)
10410   (memq (mail-header-number header) gnus-newsgroup-unreads))
10411
10412 (defun gnus-article-unseen-p (header)
10413   (memq (mail-header-number header) gnus-newsgroup-unseen))
10414
10415 (defun gnus-map-articles (predicate articles)
10416   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10417   (apply 'gnus-or (mapcar predicate
10418                           (mapcar 'gnus-summary-article-header articles))))
10419
10420 (defun gnus-summary-hide-all-threads (&optional predicate)
10421   "Hide all thread subtrees.
10422 If PREDICATE is supplied, threads that satisfy this predicate
10423 will not be hidden."
10424   (interactive)
10425   (save-excursion
10426     (goto-char (point-min))
10427     (let ((end nil))
10428       (while (not end)
10429         (when (or (not predicate)
10430                   (gnus-map-articles
10431                    predicate (gnus-summary-article-children)))
10432             (gnus-summary-hide-thread))
10433         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10434   (gnus-summary-position-point))
10435
10436 (defun gnus-summary-hide-thread ()
10437   "Hide thread subtrees.
10438 If PREDICATE is supplied, threads that satisfy this predicate
10439 will not be hidden.
10440 Returns nil if no threads were there to be hidden."
10441   (interactive)
10442   (let ((buffer-read-only nil)
10443         (start (point))
10444         (article (gnus-summary-article-number)))
10445     (goto-char start)
10446     ;; Go forward until either the buffer ends or the subthread
10447     ;; ends.
10448     (when (and (not (eobp))
10449                (or (zerop (gnus-summary-next-thread 1 t))
10450                    (goto-char (point-max))))
10451       (prog1
10452           (if (and (> (point) start)
10453                    (search-backward "\n" start t))
10454               (progn
10455                 (subst-char-in-region start (point) ?\n ?\^M)
10456                 (gnus-summary-goto-subject article))
10457             (goto-char start)
10458             nil)))))
10459
10460 (defun gnus-summary-go-to-next-thread (&optional previous)
10461   "Go to the same level (or less) next thread.
10462 If PREVIOUS is non-nil, go to previous thread instead.
10463 Return the article number moved to, or nil if moving was impossible."
10464   (let ((level (gnus-summary-thread-level))
10465         (way (if previous -1 1))
10466         (beg (point)))
10467     (forward-line way)
10468     (while (and (not (eobp))
10469                 (< level (gnus-summary-thread-level)))
10470       (forward-line way))
10471     (if (eobp)
10472         (progn
10473           (goto-char beg)
10474           nil)
10475       (setq beg (point))
10476       (prog1
10477           (gnus-summary-article-number)
10478         (goto-char beg)))))
10479
10480 (defun gnus-summary-next-thread (n &optional silent)
10481   "Go to the same level next N'th thread.
10482 If N is negative, search backward instead.
10483 Returns the difference between N and the number of skips actually
10484 done.
10485
10486 If SILENT, don't output messages."
10487   (interactive "p")
10488   (let ((backward (< n 0))
10489         (n (abs n)))
10490     (while (and (> n 0)
10491                 (gnus-summary-go-to-next-thread backward))
10492       (decf n))
10493     (unless silent
10494       (gnus-summary-position-point))
10495     (when (and (not silent) (/= 0 n))
10496       (gnus-message 7 "No more threads"))
10497     n))
10498
10499 (defun gnus-summary-prev-thread (n)
10500   "Go to the same level previous N'th thread.
10501 Returns the difference between N and the number of skips actually
10502 done."
10503   (interactive "p")
10504   (gnus-summary-next-thread (- n)))
10505
10506 (defun gnus-summary-go-down-thread ()
10507   "Go down one level in the current thread."
10508   (let ((children (gnus-summary-article-children)))
10509     (when children
10510       (gnus-summary-goto-subject (car children)))))
10511
10512 (defun gnus-summary-go-up-thread ()
10513   "Go up one level in the current thread."
10514   (let ((parent (gnus-summary-article-parent)))
10515     (when parent
10516       (gnus-summary-goto-subject parent))))
10517
10518 (defun gnus-summary-down-thread (n)
10519   "Go down thread N steps.
10520 If N is negative, go up instead.
10521 Returns the difference between N and how many steps down that were
10522 taken."
10523   (interactive "p")
10524   (let ((up (< n 0))
10525         (n (abs n)))
10526     (while (and (> n 0)
10527                 (if up (gnus-summary-go-up-thread)
10528                   (gnus-summary-go-down-thread)))
10529       (setq n (1- n)))
10530     (gnus-summary-position-point)
10531     (when (/= 0 n)
10532       (gnus-message 7 "Can't go further"))
10533     n))
10534
10535 (defun gnus-summary-up-thread (n)
10536   "Go up thread N steps.
10537 If N is negative, go down instead.
10538 Returns the difference between N and how many steps down that were
10539 taken."
10540   (interactive "p")
10541   (gnus-summary-down-thread (- n)))
10542
10543 (defun gnus-summary-top-thread ()
10544   "Go to the top of the thread."
10545   (interactive)
10546   (while (gnus-summary-go-up-thread))
10547   (gnus-summary-article-number))
10548
10549 (defun gnus-summary-kill-thread (&optional unmark)
10550   "Mark articles under current thread as read.
10551 If the prefix argument is positive, remove any kinds of marks.
10552 If the prefix argument is negative, tick articles instead."
10553   (interactive "P")
10554   (when unmark
10555     (setq unmark (prefix-numeric-value unmark)))
10556   (let ((articles (gnus-summary-articles-in-thread)))
10557     (save-excursion
10558       ;; Expand the thread.
10559       (gnus-summary-show-thread)
10560       ;; Mark all the articles.
10561       (while articles
10562         (gnus-summary-goto-subject (car articles))
10563         (cond ((null unmark)
10564                (gnus-summary-mark-article-as-read gnus-killed-mark))
10565               ((> unmark 0)
10566                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10567               (t
10568                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10569         (setq articles (cdr articles))))
10570     ;; Hide killed subtrees.
10571     (and (null unmark)
10572          gnus-thread-hide-killed
10573          (gnus-summary-hide-thread))
10574     ;; If marked as read, go to next unread subject.
10575     (when (null unmark)
10576       ;; Go to next unread subject.
10577       (gnus-summary-next-subject 1 t)))
10578   (gnus-set-mode-line 'summary))
10579
10580 ;; Summary sorting commands
10581
10582 (defun gnus-summary-sort-by-number (&optional reverse)
10583   "Sort the summary buffer by article number.
10584 Argument REVERSE means reverse order."
10585   (interactive "P")
10586   (gnus-summary-sort 'number reverse))
10587
10588 (defun gnus-summary-sort-by-random (&optional reverse)
10589   "Randomize the order in the summary buffer.
10590 Argument REVERSE means to randomize in reverse order."
10591   (interactive "P")
10592   (gnus-summary-sort 'random reverse))
10593
10594 (defun gnus-summary-sort-by-author (&optional reverse)
10595   "Sort the summary buffer by author name alphabetically.
10596 If `case-fold-search' is non-nil, case of letters is ignored.
10597 Argument REVERSE means reverse order."
10598   (interactive "P")
10599   (gnus-summary-sort 'author reverse))
10600
10601 (defun gnus-summary-sort-by-subject (&optional reverse)
10602   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10603 If `case-fold-search' is non-nil, case of letters is ignored.
10604 Argument REVERSE means reverse order."
10605   (interactive "P")
10606   (gnus-summary-sort 'subject reverse))
10607
10608 (defun gnus-summary-sort-by-date (&optional reverse)
10609   "Sort the summary buffer by date.
10610 Argument REVERSE means reverse order."
10611   (interactive "P")
10612   (gnus-summary-sort 'date reverse))
10613
10614 (defun gnus-summary-sort-by-score (&optional reverse)
10615   "Sort the summary buffer by score.
10616 Argument REVERSE means reverse order."
10617   (interactive "P")
10618   (gnus-summary-sort 'score reverse))
10619
10620 (defun gnus-summary-sort-by-lines (&optional reverse)
10621   "Sort the summary buffer by the number of lines.
10622 Argument REVERSE means reverse order."
10623   (interactive "P")
10624   (gnus-summary-sort 'lines reverse))
10625
10626 (defun gnus-summary-sort-by-chars (&optional reverse)
10627   "Sort the summary buffer by article length.
10628 Argument REVERSE means reverse order."
10629   (interactive "P")
10630   (gnus-summary-sort 'chars reverse))
10631
10632 (defun gnus-summary-sort-by-original (&optional reverse)
10633   "Sort the summary buffer using the default sorting method.
10634 Argument REVERSE means reverse order."
10635   (interactive "P")
10636   (let* ((buffer-read-only)
10637          (gnus-summary-prepare-hook nil))
10638     ;; We do the sorting by regenerating the threads.
10639     (gnus-summary-prepare)
10640     ;; Hide subthreads if needed.
10641     (gnus-summary-maybe-hide-threads)))
10642
10643 (defun gnus-summary-sort (predicate reverse)
10644   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10645   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10646          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10647          (gnus-thread-sort-functions
10648           (if (not reverse)
10649               thread
10650             `(lambda (t1 t2)
10651                (,thread t2 t1))))
10652          (gnus-sort-gathered-threads-function
10653           gnus-thread-sort-functions)
10654          (gnus-article-sort-functions
10655           (if (not reverse)
10656               article
10657             `(lambda (t1 t2)
10658                (,article t2 t1))))
10659          (buffer-read-only)
10660          (gnus-summary-prepare-hook nil))
10661     ;; We do the sorting by regenerating the threads.
10662     (gnus-summary-prepare)
10663     ;; Hide subthreads if needed.
10664     (gnus-summary-maybe-hide-threads)))
10665
10666 ;; Summary saving commands.
10667
10668 (defun gnus-summary-save-article (&optional n not-saved)
10669   "Save the current article using the default saver function.
10670 If N is a positive number, save the N next articles.
10671 If N is a negative number, save the N previous articles.
10672 If N is nil and any articles have been marked with the process mark,
10673 save those articles instead.
10674 The variable `gnus-default-article-saver' specifies the saver function."
10675   (interactive "P")
10676   (let* ((articles (gnus-summary-work-articles n))
10677          (save-buffer (save-excursion
10678                         (nnheader-set-temp-buffer " *Gnus Save*")))
10679          (num (length articles))
10680          header file)
10681     (dolist (article articles)
10682       (setq header (gnus-summary-article-header article))
10683       (if (not (vectorp header))
10684           ;; This is a pseudo-article.
10685           (if (assq 'name header)
10686               (gnus-copy-file (cdr (assq 'name header)))
10687             (gnus-message 1 "Article %d is unsaveable" article))
10688         ;; This is a real article.
10689         (save-window-excursion
10690           (let ((gnus-display-mime-function nil)
10691                 (gnus-article-prepare-hook nil))
10692             (gnus-summary-select-article t nil nil article)))
10693         (save-excursion
10694           (set-buffer save-buffer)
10695           (erase-buffer)
10696           (insert-buffer-substring gnus-original-article-buffer))
10697         (setq file (gnus-article-save save-buffer file num))
10698         (gnus-summary-remove-process-mark article)
10699         (unless not-saved
10700           (gnus-summary-set-saved-mark article))))
10701     (gnus-kill-buffer save-buffer)
10702     (gnus-summary-position-point)
10703     (gnus-set-mode-line 'summary)
10704     n))
10705
10706 (defun gnus-summary-pipe-output (&optional arg headers)
10707   "Pipe the current article to a subprocess.
10708 If N is a positive number, pipe the N next articles.
10709 If N is a negative number, pipe the N previous articles.
10710 If N is nil and any articles have been marked with the process mark,
10711 pipe those articles instead.
10712 If HEADERS (the symbolic prefix), include the headers, too."
10713   (interactive (gnus-interactive "P\ny"))
10714   (require 'gnus-art)
10715   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10716         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10717     (gnus-summary-save-article arg t))
10718   (let ((buffer (get-buffer "*Shell Command Output*")))
10719     (when (and buffer
10720                (not (zerop (buffer-size buffer))))
10721       (gnus-configure-windows 'pipe))))
10722
10723 (defun gnus-summary-save-article-mail (&optional arg)
10724   "Append the current article to an mail file.
10725 If N is a positive number, save the N next articles.
10726 If N is a negative number, save the N previous articles.
10727 If N is nil and any articles have been marked with the process mark,
10728 save those articles instead."
10729   (interactive "P")
10730   (require 'gnus-art)
10731   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10732     (gnus-summary-save-article arg)))
10733
10734 (defun gnus-summary-save-article-rmail (&optional arg)
10735   "Append the current article to an rmail file.
10736 If N is a positive number, save the N next articles.
10737 If N is a negative number, save the N previous articles.
10738 If N is nil and any articles have been marked with the process mark,
10739 save those articles instead."
10740   (interactive "P")
10741   (require 'gnus-art)
10742   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10743     (gnus-summary-save-article arg)))
10744
10745 (defun gnus-summary-save-article-file (&optional arg)
10746   "Append the current article to a file.
10747 If N is a positive number, save the N next articles.
10748 If N is a negative number, save the N previous articles.
10749 If N is nil and any articles have been marked with the process mark,
10750 save those articles instead."
10751   (interactive "P")
10752   (require 'gnus-art)
10753   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10754     (gnus-summary-save-article arg)))
10755
10756 (defun gnus-summary-write-article-file (&optional arg)
10757   "Write the current article to a file, deleting the previous file.
10758 If N is a positive number, save the N next articles.
10759 If N is a negative number, save the N previous articles.
10760 If N is nil and any articles have been marked with the process mark,
10761 save those articles instead."
10762   (interactive "P")
10763   (require 'gnus-art)
10764   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10765     (gnus-summary-save-article arg)))
10766
10767 (defun gnus-summary-save-article-body-file (&optional arg)
10768   "Append the current article body to a file.
10769 If N is a positive number, save the N next articles.
10770 If N is a negative number, save the N previous articles.
10771 If N is nil and any articles have been marked with the process mark,
10772 save those articles instead."
10773   (interactive "P")
10774   (require 'gnus-art)
10775   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10776     (gnus-summary-save-article arg)))
10777
10778 (defun gnus-summary-muttprint (&optional arg)
10779   "Print the current article using Muttprint.
10780 If N is a positive number, save the N next articles.
10781 If N is a negative number, save the N previous articles.
10782 If N is nil and any articles have been marked with the process mark,
10783 save those articles instead."
10784   (interactive "P")
10785   (require 'gnus-art)
10786   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10787     (gnus-summary-save-article arg t)))
10788
10789 (defun gnus-summary-pipe-message (program)
10790   "Pipe the current article through PROGRAM."
10791   (interactive "sProgram: ")
10792   (gnus-summary-select-article)
10793   (let ((mail-header-separator ""))
10794     (gnus-eval-in-buffer-window gnus-article-buffer
10795       (save-restriction
10796         (widen)
10797         (let ((start (window-start))
10798               buffer-read-only)
10799           (message-pipe-buffer-body program)
10800           (set-window-start (get-buffer-window (current-buffer)) start))))))
10801
10802 (defun gnus-get-split-value (methods)
10803   "Return a value based on the split METHODS."
10804   (let (split-name method result match)
10805     (when methods
10806       (save-excursion
10807         (set-buffer gnus-original-article-buffer)
10808         (save-restriction
10809           (nnheader-narrow-to-headers)
10810           (while (and methods (not split-name))
10811             (goto-char (point-min))
10812             (setq method (pop methods))
10813             (setq match (car method))
10814             (when (cond
10815                    ((stringp match)
10816                     ;; Regular expression.
10817                     (ignore-errors
10818                       (re-search-forward match nil t)))
10819                    ((functionp match)
10820                     ;; Function.
10821                     (save-restriction
10822                       (widen)
10823                       (setq result (funcall match gnus-newsgroup-name))))
10824                    ((consp match)
10825                     ;; Form.
10826                     (save-restriction
10827                       (widen)
10828                       (setq result (eval match)))))
10829               (setq split-name (cdr method))
10830               (cond ((stringp result)
10831                      (push (expand-file-name
10832                             result gnus-article-save-directory)
10833                            split-name))
10834                     ((consp result)
10835                      (setq split-name (append result split-name)))))))))
10836     (nreverse split-name)))
10837
10838 (defun gnus-valid-move-group-p (group)
10839   (and (boundp group)
10840        (symbol-name group)
10841        (symbol-value group)
10842        (gnus-get-function (gnus-find-method-for-group
10843                            (symbol-name group)) 'request-accept-article t)))
10844
10845 (defun gnus-read-move-group-name (prompt default articles prefix)
10846   "Read a group name."
10847   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10848          (minibuffer-confirm-incomplete nil) ; XEmacs
10849          (prom
10850           (format "%s %s to:"
10851                   prompt
10852                   (if (> (length articles) 1)
10853                       (format "these %d articles" (length articles))
10854                     "this article")))
10855          (to-newsgroup
10856           (cond
10857            ((null split-name)
10858             (gnus-completing-read-with-default
10859              default prom
10860              gnus-active-hashtb
10861              'gnus-valid-move-group-p
10862              nil prefix
10863              'gnus-group-history))
10864            ((= 1 (length split-name))
10865             (gnus-completing-read-with-default
10866              (car split-name) prom
10867              gnus-active-hashtb
10868              'gnus-valid-move-group-p
10869              nil nil
10870              'gnus-group-history))
10871            (t
10872             (gnus-completing-read-with-default
10873              nil prom
10874              (mapcar (lambda (el) (list el))
10875                      (nreverse split-name))
10876              nil nil nil
10877              'gnus-group-history))))
10878          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10879     (when to-newsgroup
10880       (if (or (string= to-newsgroup "")
10881               (string= to-newsgroup prefix))
10882           (setq to-newsgroup default))
10883       (unless to-newsgroup
10884         (error "No group name entered"))
10885       (or (gnus-active to-newsgroup)
10886           (gnus-activate-group to-newsgroup nil nil to-method)
10887           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10888                                      to-newsgroup))
10889               (or (and (gnus-request-create-group to-newsgroup to-method)
10890                        (gnus-activate-group
10891                         to-newsgroup nil nil to-method)
10892                        (gnus-subscribe-group to-newsgroup))
10893                   (error "Couldn't create group %s" to-newsgroup)))
10894           (error "No such group: %s" to-newsgroup)))
10895     to-newsgroup))
10896
10897 (defun gnus-summary-save-parts (type dir n &optional reverse)
10898   "Save parts matching TYPE to DIR.
10899 If REVERSE, save parts that do not match TYPE."
10900   (interactive
10901    (list (read-string "Save parts of type: "
10902                       (or (car gnus-summary-save-parts-type-history)
10903                           gnus-summary-save-parts-default-mime)
10904                       'gnus-summary-save-parts-type-history)
10905          (setq gnus-summary-save-parts-last-directory
10906                (read-file-name "Save to directory: "
10907                                gnus-summary-save-parts-last-directory
10908                                nil t))
10909          current-prefix-arg))
10910   (gnus-summary-iterate n
10911     (let ((gnus-display-mime-function nil)
10912           (gnus-inhibit-treatment t))
10913       (gnus-summary-select-article))
10914     (save-excursion
10915       (set-buffer gnus-article-buffer)
10916       (let ((handles (or gnus-article-mime-handles
10917                          (mm-dissect-buffer nil gnus-article-loose-mime)
10918                          (and gnus-article-emulate-mime
10919                               (mm-uu-dissect)))))
10920         (when handles
10921           (gnus-summary-save-parts-1 type dir handles reverse)
10922           (unless gnus-article-mime-handles ;; Don't destroy this case.
10923             (mm-destroy-parts handles)))))))
10924
10925 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10926   (if (stringp (car handle))
10927       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10928               (cdr handle))
10929     (when (if reverse
10930               (not (string-match type (mm-handle-media-type handle)))
10931             (string-match type (mm-handle-media-type handle)))
10932       (let ((file (expand-file-name
10933                    (file-name-nondirectory
10934                     (or
10935                      (mail-content-type-get
10936                       (mm-handle-disposition handle) 'filename)
10937                      (concat gnus-newsgroup-name
10938                              "." (number-to-string
10939                                   (cdr gnus-article-current)))))
10940                    dir)))
10941         (unless (file-exists-p file)
10942           (mm-save-part-to-file handle file))))))
10943
10944 ;; Summary extract commands
10945
10946 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10947   (let ((buffer-read-only nil)
10948         (article (gnus-summary-article-number))
10949         after-article b e)
10950     (unless (gnus-summary-goto-subject article)
10951       (error "No such article: %d" article))
10952     (gnus-summary-position-point)
10953     ;; If all commands are to be bunched up on one line, we collect
10954     ;; them here.
10955     (unless gnus-view-pseudos-separately
10956       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10957             files action)
10958         (while ps
10959           (setq action (cdr (assq 'action (car ps))))
10960           (setq files (list (cdr (assq 'name (car ps)))))
10961           (while (and ps (cdr ps)
10962                       (string= (or action "1")
10963                                (or (cdr (assq 'action (cadr ps))) "2")))
10964             (push (cdr (assq 'name (cadr ps))) files)
10965             (setcdr ps (cddr ps)))
10966           (when files
10967             (when (not (string-match "%s" action))
10968               (push " " files))
10969             (push " " files)
10970             (when (assq 'execute (car ps))
10971               (setcdr (assq 'execute (car ps))
10972                       (funcall (if (string-match "%s" action)
10973                                    'format 'concat)
10974                                action
10975                                (mapconcat
10976                                 (lambda (f)
10977                                   (if (equal f " ")
10978                                       f
10979                                     (mm-quote-arg f)))
10980                                 files " ")))))
10981           (setq ps (cdr ps)))))
10982     (if (and gnus-view-pseudos (not not-view))
10983         (while pslist
10984           (when (assq 'execute (car pslist))
10985             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10986                                   (eq gnus-view-pseudos 'not-confirm)))
10987           (setq pslist (cdr pslist)))
10988       (save-excursion
10989         (while pslist
10990           (setq after-article (or (cdr (assq 'article (car pslist)))
10991                                   (gnus-summary-article-number)))
10992           (gnus-summary-goto-subject after-article)
10993           (forward-line 1)
10994           (setq b (point))
10995           (insert "    " (file-name-nondirectory
10996                           (cdr (assq 'name (car pslist))))
10997                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10998           (setq e (point))
10999           (forward-line -1)             ; back to `b'
11000           (gnus-add-text-properties
11001            b (1- e) (list 'gnus-number gnus-reffed-article-number
11002                           gnus-mouse-face-prop gnus-mouse-face))
11003           (gnus-data-enter
11004            after-article gnus-reffed-article-number
11005            gnus-unread-mark b (car pslist) 0 (- e b))
11006           (setq gnus-newsgroup-unreads
11007                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11008                                          gnus-reffed-article-number))
11009           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11010           (setq pslist (cdr pslist)))))))
11011
11012 (defun gnus-pseudos< (p1 p2)
11013   (let ((c1 (cdr (assq 'action p1)))
11014         (c2 (cdr (assq 'action p2))))
11015     (and c1 c2 (string< c1 c2))))
11016
11017 (defun gnus-request-pseudo-article (props)
11018   (cond ((assq 'execute props)
11019          (gnus-execute-command (cdr (assq 'execute props)))))
11020   (let ((gnus-current-article (gnus-summary-article-number)))
11021     (gnus-run-hooks 'gnus-mark-article-hook)))
11022
11023 (defun gnus-execute-command (command &optional automatic)
11024   (save-excursion
11025     (gnus-article-setup-buffer)
11026     (set-buffer gnus-article-buffer)
11027     (setq buffer-read-only nil)
11028     (let ((command (if automatic command
11029                      (read-string "Command: " (cons command 0)))))
11030       (erase-buffer)
11031       (insert "$ " command "\n\n")
11032       (if gnus-view-pseudo-asynchronously
11033           (start-process "gnus-execute" (current-buffer) shell-file-name
11034                          shell-command-switch command)
11035         (call-process shell-file-name nil t nil
11036                       shell-command-switch command)))))
11037
11038 ;; Summary kill commands.
11039
11040 (defun gnus-summary-edit-global-kill (article)
11041   "Edit the \"global\" kill file."
11042   (interactive (list (gnus-summary-article-number)))
11043   (gnus-group-edit-global-kill article))
11044
11045 (defun gnus-summary-edit-local-kill ()
11046   "Edit a local kill file applied to the current newsgroup."
11047   (interactive)
11048   (setq gnus-current-headers (gnus-summary-article-header))
11049   (gnus-group-edit-local-kill
11050    (gnus-summary-article-number) gnus-newsgroup-name))
11051
11052 ;;; Header reading.
11053
11054 (defun gnus-read-header (id &optional header)
11055   "Read the headers of article ID and enter them into the Gnus system."
11056   (let ((group gnus-newsgroup-name)
11057         (gnus-override-method
11058          (or
11059           gnus-override-method
11060           (and (gnus-news-group-p gnus-newsgroup-name)
11061                (car (gnus-refer-article-methods)))))
11062         where)
11063     ;; First we check to see whether the header in question is already
11064     ;; fetched.
11065     (if (stringp id)
11066         ;; This is a Message-ID.
11067         (setq header (or header (gnus-id-to-header id)))
11068       ;; This is an article number.
11069       (setq header (or header (gnus-summary-article-header id))))
11070     (if (and header
11071              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11072         ;; We have found the header.
11073         header
11074       ;; If this is a sparse article, we have to nix out its
11075       ;; previous entry in the thread hashtb.
11076       (when (and header
11077                  (gnus-summary-article-sparse-p (mail-header-number header)))
11078         (let* ((parent (gnus-parent-id (mail-header-references header)))
11079                (thread (and parent (gnus-id-to-thread parent))))
11080           (when thread
11081             (delq (assq header thread) thread))))
11082       ;; We have to really fetch the header to this article.
11083       (save-excursion
11084         (set-buffer nntp-server-buffer)
11085         (when (setq where (gnus-request-head id group))
11086           (nnheader-fold-continuation-lines)
11087           (goto-char (point-max))
11088           (insert ".\n")
11089           (goto-char (point-min))
11090           (insert "211 ")
11091           (princ (cond
11092                   ((numberp id) id)
11093                   ((cdr where) (cdr where))
11094                   (header (mail-header-number header))
11095                   (t gnus-reffed-article-number))
11096                  (current-buffer))
11097           (insert " Article retrieved.\n"))
11098         (if (or (not where)
11099                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11100             ()                          ; Malformed head.
11101           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11102             (when (and (stringp id)
11103                        (not (string= (gnus-group-real-name group)
11104                                      (car where))))
11105               ;; If we fetched by Message-ID and the article came
11106               ;; from a different group, we fudge some bogus article
11107               ;; numbers for this article.
11108               (mail-header-set-number header gnus-reffed-article-number))
11109             (save-excursion
11110               (set-buffer gnus-summary-buffer)
11111               (decf gnus-reffed-article-number)
11112               (gnus-remove-header (mail-header-number header))
11113               (push header gnus-newsgroup-headers)
11114               (setq gnus-current-headers header)
11115               (push (mail-header-number header) gnus-newsgroup-limit)))
11116           header)))))
11117
11118 (defun gnus-remove-header (number)
11119   "Remove header NUMBER from `gnus-newsgroup-headers'."
11120   (if (and gnus-newsgroup-headers
11121            (= number (mail-header-number (car gnus-newsgroup-headers))))
11122       (pop gnus-newsgroup-headers)
11123     (let ((headers gnus-newsgroup-headers))
11124       (while (and (cdr headers)
11125                   (not (= number (mail-header-number (cadr headers)))))
11126         (pop headers))
11127       (when (cdr headers)
11128         (setcdr headers (cddr headers))))))
11129
11130 ;;;
11131 ;;; summary highlights
11132 ;;;
11133
11134 (defun gnus-highlight-selected-summary ()
11135   "Highlight selected article in summary buffer."
11136   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11137   (when gnus-summary-selected-face
11138     (save-excursion
11139       (let* ((beg (gnus-point-at-bol))
11140              (end (gnus-point-at-eol))
11141              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11142              (from (if (get-text-property beg gnus-mouse-face-prop)
11143                        beg
11144                      (or (next-single-property-change
11145                           beg gnus-mouse-face-prop nil end)
11146                          beg)))
11147              (to
11148               (if (= from end)
11149                   (- from 2)
11150                 (or (next-single-property-change
11151                      from gnus-mouse-face-prop nil end)
11152                     end))))
11153         ;; If no mouse-face prop on line we will have to = from = end,
11154         ;; so we highlight the entire line instead.
11155         (when (= (+ to 2) from)
11156           (setq from beg)
11157           (setq to end))
11158         (if gnus-newsgroup-selected-overlay
11159             ;; Move old overlay.
11160             (gnus-move-overlay
11161              gnus-newsgroup-selected-overlay from to (current-buffer))
11162           ;; Create new overlay.
11163           (gnus-overlay-put
11164            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11165            'face gnus-summary-selected-face))))))
11166
11167 (defvar gnus-summary-highlight-line-cached nil)
11168 (defvar gnus-summary-highlight-line-trigger nil)
11169
11170 (defun gnus-summary-highlight-line-0 ()
11171   (if (and (eq gnus-summary-highlight-line-trigger 
11172                gnus-summary-highlight)
11173            gnus-summary-highlight-line-cached)
11174       gnus-summary-highlight-line-cached
11175     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11176           gnus-summary-highlight-line-cached
11177           (let* ((cond (list 'cond))
11178                  (c cond)
11179                  (list gnus-summary-highlight))
11180             (while list
11181               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11182                               nil))
11183               (setq c (cdr c)
11184                     list (cdr list)))
11185             (gnus-byte-compile (list 'lambda nil cond))))))
11186
11187 (defun gnus-summary-highlight-line ()
11188   "Highlight current line according to `gnus-summary-highlight'."
11189   (let* ((beg (gnus-point-at-bol))
11190          (article (or (gnus-summary-article-number) gnus-current-article))
11191          (score (or (cdr (assq article
11192                                gnus-newsgroup-scored))
11193                     gnus-summary-default-score 0))
11194          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11195          (inhibit-read-only t)
11196          (default gnus-summary-default-score)
11197          (default-high gnus-summary-default-high-score)
11198          (default-low gnus-summary-default-low-score)
11199          (uncached (memq article gnus-newsgroup-undownloaded))
11200          (downloaded (not uncached)))
11201     (let ((face (funcall (gnus-summary-highlight-line-0))))
11202       (unless (eq face (get-text-property beg 'face))
11203         (gnus-put-text-property-excluding-characters-with-faces
11204          beg (gnus-point-at-eol) 'face
11205          (setq face (if (boundp face) (symbol-value face) face)))
11206         (when gnus-summary-highlight-line-function
11207           (funcall gnus-summary-highlight-line-function article face))))))
11208
11209 (defun gnus-update-read-articles (group unread &optional compute)
11210   "Update the list of read articles in GROUP.
11211 UNREAD is a sorted list."
11212   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11213          (entry (gnus-gethash group gnus-newsrc-hashtb))
11214          (info (nth 2 entry))
11215          (prev 1)
11216          read)
11217     (if (or (not info) (not active))
11218         ;; There is no info on this group if it was, in fact,
11219         ;; killed.  Gnus stores no information on killed groups, so
11220         ;; there's nothing to be done.
11221         ;; One could store the information somewhere temporarily,
11222         ;; perhaps...  Hmmm...
11223         ()
11224       ;; Remove any negative articles numbers.
11225       (while (and unread (< (car unread) 0))
11226         (setq unread (cdr unread)))
11227       ;; Remove any expired article numbers
11228       (while (and unread (< (car unread) (car active)))
11229         (setq unread (cdr unread)))
11230       ;; Compute the ranges of read articles by looking at the list of
11231       ;; unread articles.
11232       (while unread
11233         (when (/= (car unread) prev)
11234           (push (if (= prev (1- (car unread))) prev
11235                   (cons prev (1- (car unread))))
11236                 read))
11237         (setq prev (1+ (car unread)))
11238         (setq unread (cdr unread)))
11239       (when (<= prev (cdr active))
11240         (push (cons prev (cdr active)) read))
11241       (setq read (if (> (length read) 1) (nreverse read) read))
11242       (if compute
11243           read
11244         (save-excursion
11245           (let (setmarkundo)
11246             ;; Propagate the read marks to the backend.
11247             (when (gnus-check-backend-function 'request-set-mark group)
11248               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11249                     (add (gnus-remove-from-range read (gnus-info-read info))))
11250                 (when (or add del)
11251                   (unless (gnus-check-group group)
11252                     (error "Can't open server for %s" group))
11253                   (gnus-request-set-mark
11254                    group (delq nil (list (if add (list add 'add '(read)))
11255                                          (if del (list del 'del '(read))))))
11256                   (setq setmarkundo
11257                         `(gnus-request-set-mark
11258                           ,group
11259                           ',(delq nil (list
11260                                        (if del (list del 'add '(read)))
11261                                        (if add (list add 'del '(read))))))))))
11262             (set-buffer gnus-group-buffer)
11263             (gnus-undo-register
11264               `(progn
11265                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11266                  (gnus-info-set-read ',info ',(gnus-info-read info))
11267                  (gnus-get-unread-articles-in-group ',info
11268                                                     (gnus-active ,group))
11269                  (gnus-group-update-group ,group t)
11270                  ,setmarkundo))))
11271         ;; Enter this list into the group info.
11272         (gnus-info-set-read info read)
11273         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11274         (gnus-get-unread-articles-in-group info (gnus-active group))
11275         t))))
11276
11277 (defun gnus-offer-save-summaries ()
11278   "Offer to save all active summary buffers."
11279   (let (buffers)
11280     ;; Go through all buffers and find all summaries.
11281     (dolist (buffer (buffer-list))
11282       (when (and (setq buffer (buffer-name buffer))
11283                  (string-match "Summary" buffer)
11284                  (save-excursion
11285                    (set-buffer buffer)
11286                    ;; We check that this is, indeed, a summary buffer.
11287                    (and (eq major-mode 'gnus-summary-mode)
11288                         ;; Also make sure this isn't bogus.
11289                         gnus-newsgroup-prepared
11290                         ;; Also make sure that this isn't a
11291                         ;; dead summary buffer.
11292                         (not gnus-dead-summary-mode))))
11293         (push buffer buffers)))
11294     ;; Go through all these summary buffers and offer to save them.
11295     (when buffers
11296       (save-excursion
11297         (map-y-or-n-p
11298          "Update summary buffer %s? "
11299          (lambda (buf)
11300            (switch-to-buffer buf)
11301            (gnus-summary-exit))
11302          buffers)))))
11303
11304 (defun gnus-summary-setup-default-charset ()
11305   "Setup newsgroup default charset."
11306   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11307       (setq gnus-newsgroup-charset nil)
11308     (let* ((ignored-charsets
11309             (or gnus-newsgroup-ephemeral-ignored-charsets
11310                 (append
11311                  (and gnus-newsgroup-name
11312                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11313                  gnus-newsgroup-ignored-charsets))))
11314       (setq gnus-newsgroup-charset
11315             (or gnus-newsgroup-ephemeral-charset
11316                 (and gnus-newsgroup-name
11317                      (gnus-parameter-charset gnus-newsgroup-name))
11318                 gnus-default-charset))
11319       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11320            ignored-charsets))))
11321
11322 ;;;
11323 ;;; Mime Commands
11324 ;;;
11325
11326 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11327   "Display the current article buffer fully MIME-buttonized.
11328 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11329 treated as multipart/mixed."
11330   (interactive "P")
11331   (require 'gnus-art)
11332   (let ((gnus-unbuttonized-mime-types nil)
11333         (gnus-mime-display-multipart-as-mixed show-all-parts))
11334     (gnus-summary-show-article)))
11335
11336 (defun gnus-summary-repair-multipart (article)
11337   "Add a Content-Type header to a multipart article without one."
11338   (interactive (list (gnus-summary-article-number)))
11339   (gnus-with-article article
11340     (message-narrow-to-head)
11341     (message-remove-header "Mime-Version")
11342     (goto-char (point-max))
11343     (insert "Mime-Version: 1.0\n")
11344     (widen)
11345     (when (search-forward "\n--" nil t)
11346       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11347         (message-narrow-to-head)
11348         (message-remove-header "Content-Type")
11349         (goto-char (point-max))
11350         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11351                         separator))
11352         (widen))))
11353   (let (gnus-mark-article-hook)
11354     (gnus-summary-select-article t t nil article)))
11355
11356 (defun gnus-summary-toggle-display-buttonized ()
11357   "Toggle the buttonizing of the article buffer."
11358   (interactive)
11359   (require 'gnus-art)
11360   (if (setq gnus-inhibit-mime-unbuttonizing
11361             (not gnus-inhibit-mime-unbuttonizing))
11362       (let ((gnus-unbuttonized-mime-types nil))
11363         (gnus-summary-show-article))
11364     (gnus-summary-show-article)))
11365
11366 ;;;
11367 ;;; Generic summary marking commands
11368 ;;;
11369
11370 (defvar gnus-summary-marking-alist
11371   '((read gnus-del-mark "d")
11372     (unread gnus-unread-mark "u")
11373     (ticked gnus-ticked-mark "!")
11374     (dormant gnus-dormant-mark "?")
11375     (expirable gnus-expirable-mark "e"))
11376   "An alist of names/marks/keystrokes.")
11377
11378 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11379 (defvar gnus-summary-mark-map)
11380
11381 (defun gnus-summary-make-all-marking-commands ()
11382   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11383   (dolist (elem gnus-summary-marking-alist)
11384     (apply 'gnus-summary-make-marking-command elem)))
11385
11386 (defun gnus-summary-make-marking-command (name mark keystroke)
11387   (let ((map (make-sparse-keymap)))
11388     (define-key gnus-summary-generic-mark-map keystroke map)
11389     (dolist (lway `((next "next" next nil "n")
11390                     (next-unread "next unread" next t "N")
11391                     (prev "previous" prev nil "p")
11392                     (prev-unread "previous unread" prev t "P")
11393                     (nomove "" nil nil ,keystroke)))
11394       (let ((func (gnus-summary-make-marking-command-1
11395                    mark (car lway) lway name)))
11396         (setq func (eval func))
11397         (define-key map (nth 4 lway) func)))))
11398
11399 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11400   `(defun ,(intern
11401             (format "gnus-summary-put-mark-as-%s%s"
11402                     name (if (eq way 'nomove)
11403                              ""
11404                            (concat "-" (symbol-name way)))))
11405      (n)
11406      ,(format
11407        "Mark the current article as %s%s.
11408 If N, the prefix, then repeat N times.
11409 If N is negative, move in reverse order.
11410 The difference between N and the actual number of articles marked is
11411 returned."
11412        name (cadr lway))
11413      (interactive "p")
11414      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11415
11416 (defun gnus-summary-generic-mark (n mark move unread)
11417   "Mark N articles with MARK."
11418   (unless (eq major-mode 'gnus-summary-mode)
11419     (error "This command can only be used in the summary buffer"))
11420   (gnus-summary-show-thread)
11421   (let ((nummove
11422          (cond
11423           ((eq move 'next) 1)
11424           ((eq move 'prev) -1)
11425           (t 0))))
11426     (if (zerop nummove)
11427         (setq n 1)
11428       (when (< n 0)
11429         (setq n (abs n)
11430               nummove (* -1 nummove))))
11431     (while (and (> n 0)
11432                 (gnus-summary-mark-article nil mark)
11433                 (zerop (gnus-summary-next-subject nummove unread t)))
11434       (setq n (1- n)))
11435     (when (/= 0 n)
11436       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11437     (gnus-summary-recenter)
11438     (gnus-summary-position-point)
11439     (gnus-set-mode-line 'summary)
11440     n))
11441
11442 (defun gnus-summary-insert-articles (articles)
11443   (when (setq articles
11444               (gnus-sorted-difference articles
11445                                       (mapcar (lambda (h)
11446                                                 (mail-header-number h))
11447                                               gnus-newsgroup-headers)))
11448     (setq gnus-newsgroup-headers
11449           (gnus-merge 'list
11450                       gnus-newsgroup-headers
11451                       (gnus-fetch-headers articles)
11452                       'gnus-article-sort-by-number))
11453     ;; Suppress duplicates?
11454     (when gnus-suppress-duplicates
11455       (gnus-dup-suppress-articles))
11456
11457     ;; We might want to build some more threads first.
11458     (when (and gnus-fetch-old-headers
11459                (eq gnus-headers-retrieved-by 'nov))
11460       (if (eq gnus-fetch-old-headers 'invisible)
11461           (gnus-build-all-threads)
11462         (gnus-build-old-threads)))
11463     ;; Let the Gnus agent mark articles as read.
11464     (when gnus-agent
11465       (gnus-agent-get-undownloaded-list))
11466     ;; Remove list identifiers from subject
11467     (when gnus-list-identifiers
11468       (gnus-summary-remove-list-identifiers))
11469     ;; First and last article in this newsgroup.
11470     (when gnus-newsgroup-headers
11471       (setq gnus-newsgroup-begin
11472             (mail-header-number (car gnus-newsgroup-headers))
11473             gnus-newsgroup-end
11474             (mail-header-number
11475              (gnus-last-element gnus-newsgroup-headers))))
11476     (when gnus-use-scoring
11477       (gnus-possibly-score-headers))))
11478
11479 (defun gnus-summary-insert-old-articles (&optional all)
11480   "Insert all old articles in this group.
11481 If ALL is non-nil, already read articles become readable.
11482 If ALL is a number, fetch this number of articles."
11483   (interactive "P")
11484   (prog1
11485       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11486             older len)
11487         (setq older
11488               ;; Some nntp servers lie about their active range.  When
11489               ;; this happens, the active range can be in the millions.
11490               ;; Use a compressed range to avoid creating a huge list.
11491               (gnus-range-difference (list gnus-newsgroup-active) old))
11492         (setq len (gnus-range-length older))
11493         (cond
11494          ((null older) nil)
11495          ((numberp all)
11496           (if (< all len)
11497               (let ((older-range (nreverse older)))
11498                 (setq older nil)
11499
11500                 (while (> all 0)
11501                   (let* ((r (pop older-range))
11502                          (min (if (numberp r) r (car r)))
11503                          (max (if (numberp r) r (cdr r))))
11504                     (while (and (<= min max)
11505                                 (> all 0))
11506                       (push max older)
11507                       (setq all (1- all)
11508                             max (1- max))))))
11509             (setq older (gnus-uncompress-range older))))
11510          (all
11511           (setq older (gnus-uncompress-range older)))
11512          (t
11513           (when (and (numberp gnus-large-newsgroup)
11514                    (> len gnus-large-newsgroup))
11515               (let* ((cursor-in-echo-area nil)
11516                      (initial (gnus-parameter-large-newsgroup-initial
11517                                gnus-newsgroup-name))
11518                      (input
11519                       (read-string
11520                        (format
11521                         "How many articles from %s (%s %d): "
11522                         (gnus-limit-string
11523                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11524                         (if initial "max" "default")
11525                         len)
11526                        (if initial
11527                            (cons (number-to-string initial)
11528                                  0)))))
11529                 (unless (string-match "^[ \t]*$" input)
11530                   (setq all (string-to-number input))
11531                   (if (< all len)
11532                       (let ((older-range (nreverse older)))
11533                         (setq older nil)
11534
11535                         (while (> all 0)
11536                           (let* ((r (pop older-range))
11537                                  (min (if (numberp r) r (car r)))
11538                                  (max (if (numberp r) r (cdr r))))
11539                             (while (and (<= min max)
11540                                         (> all 0))
11541                               (push max older)
11542                               (setq all (1- all)
11543                                     max (1- max))))))))))
11544           (setq older (gnus-uncompress-range older))))
11545         (if (not older)
11546             (message "No old news.")
11547           (gnus-summary-insert-articles older)
11548           (gnus-summary-limit (gnus-sorted-nunion old older))))
11549     (gnus-summary-position-point)))
11550
11551 (defun gnus-summary-insert-new-articles ()
11552   "Insert all new articles in this group."
11553   (interactive)
11554   (prog1
11555       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11556             (old-active gnus-newsgroup-active)
11557             (nnmail-fetched-sources (list t))
11558             i new)
11559         (setq gnus-newsgroup-active
11560               (gnus-activate-group gnus-newsgroup-name 'scan))
11561         (setq i (cdr gnus-newsgroup-active))
11562         (while (> i (cdr old-active))
11563           (push i new)
11564           (decf i))
11565         (if (not new)
11566             (message "No gnus is bad news.")
11567           (gnus-summary-insert-articles new)
11568           (setq gnus-newsgroup-unreads
11569                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11570           (gnus-summary-limit (gnus-sorted-nunion old new))))
11571     (gnus-summary-position-point)))
11572
11573 (gnus-summary-make-all-marking-commands)
11574
11575 (gnus-ems-redefine)
11576
11577 (provide 'gnus-sum)
11578
11579 (run-hooks 'gnus-sum-load-hook)
11580
11581 ;; Local Variables:
11582 ;; coding: iso-8859-1
11583 ;; End:
11584
11585 ;;; gnus-sum.el ends here