7035aa37456a9b0e54b29a6ee34a2271f8dfdb05
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile
32   (require 'cl)
33   (require 'gnus-clfns)
34   (defvar tool-bar-map))
35
36 (require 'gnus)
37 (require 'gnus-group)
38 (require 'gnus-spec)
39 (require 'gnus-range)
40 (require 'gnus-int)
41 (require 'gnus-undo)
42 (require 'gnus-util)
43 (require 'nnoo)
44 (require 'mime-view)
45
46 (eval-when-compile
47   (require 'mime-play)
48   (require 'static))
49
50 (eval-and-compile
51   (autoload 'pgg-decrypt-region "pgg" nil t)
52   (autoload 'pgg-verify-region "pgg" nil t))
53
54 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
55 (autoload 'gnus-cache-write-active "gnus-cache")
56 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
57 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
58 (autoload 'mm-uu-dissect "mm-uu")
59 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
60   "Deuglify broken Outlook (Express) articles and redisplay."
61   t)
62 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
63 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
64 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
65
66 (defcustom gnus-kill-summary-on-exit t
67   "*If non-nil, kill the summary buffer when you exit from it.
68 If nil, the summary will become a \"*Dead Summary*\" buffer, and
69 it will be killed sometime later."
70   :group 'gnus-summary-exit
71   :type 'boolean)
72
73 (defcustom gnus-fetch-old-headers nil
74   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
75 If an unread article in the group refers to an older, already read (or
76 just marked as read) article, the old article will not normally be
77 displayed in the Summary buffer.  If this variable is non-nil, Gnus
78 will attempt to grab the headers to the old articles, and thereby
79 build complete threads.  If it has the value `some', only enough
80 headers to connect otherwise loose threads will be displayed.  This
81 variable can also be a number.  In that case, no more than that number
82 of old headers will be fetched.  If it has the value `invisible', all
83 old headers will be fetched, but none will be displayed.
84
85 The server has to support NOV for any of this to work."
86   :group 'gnus-thread
87   :type '(choice (const :tag "off" nil)
88                  (const some)
89                  number
90                  (sexp :menu-tag "other" t)))
91
92 (defcustom gnus-refer-thread-limit 200
93   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
94 If t, fetch all the available old headers."
95   :group 'gnus-thread
96   :type '(choice number
97                  (sexp :menu-tag "other" t)))
98
99 (defcustom gnus-summary-make-false-root 'adopt
100   "*nil means that Gnus won't gather loose threads.
101 If the root of a thread has expired or been read in a previous
102 session, the information necessary to build a complete thread has been
103 lost.  Instead of having many small sub-threads from this original thread
104 scattered all over the summary buffer, Gnus can gather them.
105
106 If non-nil, Gnus will try to gather all loose sub-threads from an
107 original thread into one large thread.
108
109 If this variable is non-nil, it should be one of `none', `adopt',
110 `dummy' or `empty'.
111
112 If this variable is `none', Gnus will not make a false root, but just
113 present the sub-threads after another.
114 If this variable is `dummy', Gnus will create a dummy root that will
115 have all the sub-threads as children.
116 If this variable is `adopt', Gnus will make one of the \"children\"
117 the parent and mark all the step-children as such.
118 If this variable is `empty', the \"children\" are printed with empty
119 subject fields.  (Or rather, they will be printed with a string
120 given by the `gnus-summary-same-subject' variable.)"
121   :group 'gnus-thread
122   :type '(choice (const :tag "off" nil)
123                  (const none)
124                  (const dummy)
125                  (const adopt)
126                  (const empty)))
127
128 (defcustom gnus-summary-make-false-root-always nil
129   "Always make a false dummy root."
130   :group 'gnus-thread
131   :type 'boolean)
132
133 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
134   "*A regexp to match subjects to be excluded from loose thread gathering.
135 As loose thread gathering is done on subjects only, that means that
136 there can be many false gatherings performed.  By rooting out certain
137 common subjects, gathering might become saner."
138   :group 'gnus-thread
139   :type 'regexp)
140
141 (defcustom gnus-summary-gather-subject-limit nil
142   "*Maximum length of subject comparisons when gathering loose threads.
143 Use nil to compare full subjects.  Setting this variable to a low
144 number will help gather threads that have been corrupted by
145 newsreaders chopping off subject lines, but it might also mean that
146 unrelated articles that have subject that happen to begin with the
147 same few characters will be incorrectly gathered.
148
149 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
150 comparing subjects."
151   :group 'gnus-thread
152   :type '(choice (const :tag "off" nil)
153                  (const fuzzy)
154                  (sexp :menu-tag "on" t)))
155
156 (defcustom gnus-simplify-subject-functions nil
157   "List of functions taking a string argument that simplify subjects.
158 The functions are applied recursively.
159
160 Useful functions to put in this list include:
161 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
162 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
163   :group 'gnus-thread
164   :type '(repeat function))
165
166 (defcustom gnus-simplify-ignored-prefixes nil
167   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
168   :group 'gnus-thread
169   :type '(choice (const :tag "off" nil)
170                  regexp))
171
172 (defcustom gnus-build-sparse-threads nil
173   "*If non-nil, fill in the gaps in threads.
174 If `some', only fill in the gaps that are needed to tie loose threads
175 together.  If `more', fill in all leaf nodes that Gnus can find.  If
176 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
177   :group 'gnus-thread
178   :type '(choice (const :tag "off" nil)
179                  (const some)
180                  (const more)
181                  (sexp :menu-tag "all" t)))
182
183 (defcustom gnus-summary-thread-gathering-function
184   'gnus-gather-threads-by-subject
185   "*Function used for gathering loose threads.
186 There are two pre-defined functions: `gnus-gather-threads-by-subject',
187 which only takes Subjects into consideration; and
188 `gnus-gather-threads-by-references', which compared the References
189 headers of the articles to find matches."
190   :group 'gnus-thread
191   :type '(radio (function-item gnus-gather-threads-by-subject)
192                 (function-item gnus-gather-threads-by-references)
193                 (function :tag "other")))
194
195 (defcustom gnus-summary-same-subject ""
196   "*String indicating that the current article has the same subject as the previous.
197 This variable will only be used if the value of
198 `gnus-summary-make-false-root' is `empty'."
199   :group 'gnus-summary-format
200   :type 'string)
201
202 (defcustom gnus-summary-goto-unread t
203   "*If t, many commands will go to the next unread article.
204 This applies to marking commands as well as other commands that
205 \"naturally\" select the next article, like, for instance, `SPC' at
206 the end of an article.
207
208 If nil, the marking commands do NOT go to the next unread article
209 \(they go to the next article instead).  If `never', commands that
210 usually go to the next unread article, will go to the next article,
211 whether it is read or not."
212   :group 'gnus-summary-marks
213   :link '(custom-manual "(gnus)Setting Marks")
214   :type '(choice (const :tag "off" nil)
215                  (const never)
216                  (sexp :menu-tag "on" t)))
217
218 (defcustom gnus-summary-default-score 0
219   "*Default article score level.
220 All scores generated by the score files will be added to this score.
221 If this variable is nil, scoring will be disabled."
222   :group 'gnus-score-default
223   :type '(choice (const :tag "disable")
224                  integer))
225
226 (defcustom gnus-summary-default-high-score 0
227   "*Default threshold for a high scored article.
228 An article will be highlighted as high scored if its score is greater
229 than this score."
230   :group 'gnus-score-default
231   :type 'integer)
232
233 (defcustom gnus-summary-default-low-score 0
234   "*Default threshold for a low scored article.
235 An article will be highlighted as low scored if its score is smaller
236 than this score."
237   :group 'gnus-score-default
238   :type 'integer)
239
240 (defcustom gnus-summary-zcore-fuzz 0
241   "*Fuzziness factor for the zcore in the summary buffer.
242 Articles with scores closer than this to `gnus-summary-default-score'
243 will not be marked."
244   :group 'gnus-summary-format
245   :type 'integer)
246
247 (defcustom gnus-simplify-subject-fuzzy-regexp nil
248   "*Strings to be removed when doing fuzzy matches.
249 This can either be a regular expression or list of regular expressions
250 that will be removed from subject strings if fuzzy subject
251 simplification is selected."
252   :group 'gnus-thread
253   :type '(repeat regexp))
254
255 (defcustom gnus-show-threads t
256   "*If non-nil, display threads in summary mode."
257   :group 'gnus-thread
258   :type 'boolean)
259
260 (defcustom gnus-thread-hide-subtree nil
261   "*If non-nil, hide all threads initially.
262 This can be a predicate specifier which says which threads to hide.
263 If threads are hidden, you have to run the command
264 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
265 to expose hidden threads."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-hide-killed t
270   "*If non-nil, hide killed threads automatically."
271   :group 'gnus-thread
272   :type 'boolean)
273
274 (defcustom gnus-thread-ignore-subject t
275   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
276 If nil, articles that have different subjects from their parents will
277 start separate threads."
278   :group 'gnus-thread
279   :type 'boolean)
280
281 (defcustom gnus-thread-operation-ignore-subject t
282   "*If non-nil, subjects will be ignored when doing thread commands.
283 This affects commands like `gnus-summary-kill-thread' and
284 `gnus-summary-lower-thread'.
285
286 If this variable is nil, articles in the same thread with different
287 subjects will not be included in the operation in question.  If this
288 variable is `fuzzy', only articles that have subjects that are fuzzily
289 equal will be included."
290   :group 'gnus-thread
291   :type '(choice (const :tag "off" nil)
292                  (const fuzzy)
293                  (sexp :tag "on" t)))
294
295 (defcustom gnus-thread-indent-level 4
296   "*Number that says how much each sub-thread should be indented."
297   :group 'gnus-thread
298   :type 'integer)
299
300 (defcustom gnus-auto-extend-newsgroup t
301   "*If non-nil, extend newsgroup forward and backward when requested."
302   :group 'gnus-summary-choose
303   :type 'boolean)
304
305 (defcustom gnus-auto-select-first t
306   "*If non-nil, select the article under point.
307 Which article this is is controlled by the `gnus-auto-select-subject'
308 variable.
309
310 If you want to prevent automatic selection of articles in some
311 newsgroups, set the variable to nil in `gnus-select-group-hook'."
312   :group 'gnus-group-select
313   :type '(choice (const :tag "none" nil)
314                  (sexp :menu-tag "first" t)))
315
316 (defcustom gnus-auto-select-subject 'unread
317   "*Says what subject to place under point when entering a group.
318
319 This variable can either be the symbols `first' (place point on the
320 first subject), `unread' (place point on the subject line of the first
321 unread article), `best' (place point on the subject line of the
322 higest-scored article), `unseen' (place point on the subject line of
323 the first unseen article), 'unseen-or-unread' (place point on the subject
324 line of the first unseen article or, if all article have been seen, on the
325 subject line of the first unread article), or a function to be called to
326 place point on some subject line."
327   :group 'gnus-group-select
328   :type '(choice (const best)
329                  (const unread)
330                  (const first)
331                  (const unseen)
332                  (const unseen-or-unread)))
333
334 (defcustom gnus-dont-select-after-jump-to-other-group nil
335   "If non-nil, don't select the first unread article after entering the
336 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
337 it is depend on the value of `gnus-auto-select-first' whether to select
338 or not."
339   :group 'gnus-group-select
340   :type 'boolean)
341
342 (defcustom gnus-auto-select-next t
343   "*If non-nil, offer to go to the next group from the end of the previous.
344 If the value is t and the next newsgroup is empty, Gnus will exit
345 summary mode and go back to group mode.  If the value is neither nil
346 nor t, Gnus will select the following unread newsgroup.  In
347 particular, if the value is the symbol `quietly', the next unread
348 newsgroup will be selected without any confirmation, and if it is
349 `almost-quietly', the next group will be selected without any
350 confirmation if you are located on the last article in the group.
351 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
352 will go to the next group without confirmation."
353   :group 'gnus-summary-maneuvering
354   :type '(choice (const :tag "off" nil)
355                  (const quietly)
356                  (const almost-quietly)
357                  (const slightly-quietly)
358                  (sexp :menu-tag "on" t)))
359
360 (defcustom gnus-auto-select-same nil
361   "*If non-nil, select the next article with the same subject.
362 If there are no more articles with the same subject, go to
363 the first unread article."
364   :group 'gnus-summary-maneuvering
365   :type 'boolean)
366
367 (defcustom gnus-auto-goto-ignores 'unfetched
368   "*Says how to handle unfetched articles when maneuvering.
369
370 This variable can either be the symbols `nil' (maneuver to any
371 article), `undownloaded' (maneuvering while unplugged ignores articles
372 that have not been fetched), `always-undownloaded' (maneuvering always
373 ignores articles that have not been fetched), `unfetched' (maneuvering
374 ignores articles whose headers have not been fetched).
375
376 NOTE: The list of unfetched articles will always be nil when plugged
377 and, when unplugged, a subset of the undownloaded article list."
378   :group 'gnus-summary-maneuvering
379   :type '(choice (const :tag "None" nil)
380                  (const :tag "Undownloaded when unplugged" undownloaded)
381                  (const :tag "Undownloaded" always-undownloaded)
382                  (const :tag "Unfetched" unfetched)))
383
384 (defcustom gnus-summary-check-current nil
385   "*If non-nil, consider the current article when moving.
386 The \"unread\" movement commands will stay on the same line if the
387 current article is unread."
388   :group 'gnus-summary-maneuvering
389   :type 'boolean)
390
391 (defcustom gnus-auto-center-summary t
392   "*If non-nil, always center the current summary buffer.
393 In particular, if `vertical' do only vertical recentering.  If non-nil
394 and non-`vertical', do both horizontal and vertical recentering."
395   :group 'gnus-summary-maneuvering
396   :type '(choice (const :tag "none" nil)
397                  (const vertical)
398                  (integer :tag "height")
399                  (sexp :menu-tag "both" t)))
400
401 (defvar gnus-auto-center-group t
402   "*If non-nil, always center the group buffer.")
403
404 (defcustom gnus-show-all-headers nil
405   "*If non-nil, don't hide any headers."
406   :group 'gnus-article-hiding
407   :group 'gnus-article-headers
408   :type 'boolean)
409
410 (defcustom gnus-summary-ignore-duplicates nil
411   "*If non-nil, ignore articles with identical Message-ID headers."
412   :group 'gnus-summary
413   :type 'boolean)
414
415 (defcustom gnus-single-article-buffer t
416   "*If non-nil, display all articles in the same buffer.
417 If nil, each group will get its own article buffer."
418   :group 'gnus-article-various
419   :type 'boolean)
420
421 (defcustom gnus-break-pages t
422   "*If non-nil, do page breaking on articles.
423 The page delimiter is specified by the `gnus-page-delimiter'
424 variable."
425   :group 'gnus-article-various
426   :type 'boolean)
427
428 (defcustom gnus-show-mime t
429   "*If non-nil, do mime processing of articles.
430 The articles will simply be fed to the function given by
431 `gnus-article-display-method-for-mime'."
432   :group 'gnus-article-mime
433   :type 'boolean)
434
435 (defcustom gnus-move-split-methods nil
436   "*Variable used to suggest where articles are to be moved to.
437 It uses the same syntax as the `gnus-split-methods' variable.
438 However, whereas `gnus-split-methods' specifies file names as targets,
439 this variable specifies group names."
440   :group 'gnus-summary-mail
441   :type '(repeat (choice (list :value (fun) function)
442                          (cons :value ("" "") regexp (repeat string))
443                          (sexp :value nil))))
444
445 (defcustom gnus-unread-mark ?\ ;;;Whitespace
446   "*Mark used for unread articles."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-ticked-mark ?!
451   "*Mark used for ticked articles."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-dormant-mark ??
456   "*Mark used for dormant articles."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-del-mark ?r
461   "*Mark used for del'd articles."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-read-mark ?R
466   "*Mark used for read articles."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-expirable-mark ?E
471   "*Mark used for expirable articles."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-killed-mark ?K
476   "*Mark used for killed articles."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-spam-mark ?$
481   "*Mark used for spam articles."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-souped-mark ?F
486   "*Mark used for souped articles."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-kill-file-mark ?X
491   "*Mark used for articles killed by kill files."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-low-score-mark ?Y
496   "*Mark used for articles with a low score."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-catchup-mark ?C
501   "*Mark used for articles that are caught up."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-replied-mark ?A
506   "*Mark used for articles that have been replied to."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-forwarded-mark ?F
511   "*Mark used for articles that have been forwarded."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-recent-mark ?N
516   "*Mark used for articles that are recent."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-cached-mark ?*
521   "*Mark used for articles that are in the cache."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-saved-mark ?S
526   "*Mark used for articles that have been saved."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-unseen-mark ?.
531   "*Mark used for articles that haven't been seen."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-no-mark ?\ ;;;Whitespace
536   "*Mark used for articles that have no other secondary mark."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-ancient-mark ?O
541   "*Mark used for ancient articles."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-sparse-mark ?Q
546   "*Mark used for sparsely reffed articles."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-canceled-mark ?G
551   "*Mark used for canceled articles."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-duplicate-mark ?M
556   "*Mark used for duplicate articles."
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-undownloaded-mark ?-
561   "*Mark used for articles that weren't downloaded."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-downloaded-mark ?+
566   "*Mark used for articles that were downloaded."
567   :group 'gnus-summary-marks
568   :type 'character)
569
570 (defcustom gnus-downloadable-mark ?%
571   "*Mark used for articles that are to be downloaded."
572   :group 'gnus-summary-marks
573   :type 'character)
574
575 (defcustom gnus-unsendable-mark ?=
576   "*Mark used for articles that won't be sent."
577   :group 'gnus-summary-marks
578   :type 'character)
579
580 (defcustom gnus-score-over-mark ?+
581   "*Score mark used for articles with high scores."
582   :group 'gnus-summary-marks
583   :type 'character)
584
585 (defcustom gnus-score-below-mark ?-
586   "*Score mark used for articles with low scores."
587   :group 'gnus-summary-marks
588   :type 'character)
589
590 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
591   "*There is no thread under the article."
592   :group 'gnus-summary-marks
593   :type 'character)
594
595 (defcustom gnus-not-empty-thread-mark ?=
596   "*There is a thread under the article."
597   :group 'gnus-summary-marks
598   :type 'character)
599
600 (defcustom gnus-view-pseudo-asynchronously nil
601   "*If non-nil, Gnus will view pseudo-articles asynchronously."
602   :group 'gnus-extract-view
603   :type 'boolean)
604
605 (defcustom gnus-auto-expirable-marks
606   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
607         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
608         gnus-souped-mark gnus-duplicate-mark)
609   "*The list of marks converted into expiration if a group is auto-expirable."
610   :version "21.1"
611   :group 'gnus-summary
612   :type '(repeat character))
613
614 (defcustom gnus-inhibit-user-auto-expire t
615   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
616   :version "21.1"
617   :group 'gnus-summary
618   :type 'boolean)
619
620 (defcustom gnus-view-pseudos nil
621   "*If `automatic', pseudo-articles will be viewed automatically.
622 If `not-confirm', pseudos will be viewed automatically, and the user
623 will not be asked to confirm the command."
624   :group 'gnus-extract-view
625   :type '(choice (const :tag "off" nil)
626                  (const automatic)
627                  (const not-confirm)))
628
629 (defcustom gnus-view-pseudos-separately t
630   "*If non-nil, one pseudo-article will be created for each file to be viewed.
631 If nil, all files that use the same viewing command will be given as a
632 list of parameters to that command."
633   :group 'gnus-extract-view
634   :type 'boolean)
635
636 (defcustom gnus-insert-pseudo-articles t
637   "*If non-nil, insert pseudo-articles when decoding articles."
638   :group 'gnus-extract-view
639   :type 'boolean)
640
641 (defcustom gnus-summary-dummy-line-format
642   "   %(:                             :%) %S\n"
643   "*The format specification for the dummy roots in the summary buffer.
644 It works along the same lines as a normal formatting string,
645 with some simple extensions.
646
647 %S  The subject
648
649 General format specifiers can also be used.
650 See `(gnus)Formatting Variables'."
651   :link '(custom-manual "(gnus)Formatting Variables")
652   :group 'gnus-threading
653   :type 'string)
654
655 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
656   "*The format specification for the summary mode line.
657 It works along the same lines as a normal formatting string,
658 with some simple extensions:
659
660 %G  Group name
661 %p  Unprefixed group name
662 %A  Current article number
663 %z  Current article score
664 %V  Gnus version
665 %U  Number of unread articles in the group
666 %e  Number of unselected articles in the group
667 %Z  A string with unread/unselected article counts
668 %g  Shortish group name
669 %S  Subject of the current article
670 %u  User-defined spec
671 %s  Current score file name
672 %d  Number of dormant articles
673 %r  Number of articles that have been marked as read in this session
674 %E  Number of articles expunged by the score files"
675   :group 'gnus-summary-format
676   :type 'string)
677
678 (defcustom gnus-list-identifiers nil
679   "Regexp that matches list identifiers to be removed from subject.
680 This can also be a list of regexps."
681   :version "21.1"
682   :group 'gnus-summary-format
683   :group 'gnus-article-hiding
684   :type '(choice (const :tag "none" nil)
685                  (regexp :value ".*")
686                  (repeat :value (".*") regexp)))
687
688 (defcustom gnus-summary-mark-below 0
689   "*Mark all articles with a score below this variable as read.
690 This variable is local to each summary buffer and usually set by the
691 score file."
692   :group 'gnus-score-default
693   :type 'integer)
694
695 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
696   "*List of functions used for sorting articles in the summary buffer.
697
698 Each function takes two articles and returns non-nil if the first
699 article should be sorted before the other.  If you use more than one
700 function, the primary sort function should be the last.  You should
701 probably always include `gnus-article-sort-by-number' in the list of
702 sorting functions -- preferably first.  Also note that sorting by date
703 is often much slower than sorting by number, and the sorting order is
704 very similar.  (Sorting by date means sorting by the time the message
705 was sent, sorting by number means sorting by arrival time.)
706
707 Ready-made functions include `gnus-article-sort-by-number',
708 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
709 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
710 and `gnus-article-sort-by-score'.
711
712 When threading is turned on, the variable `gnus-thread-sort-functions'
713 controls how articles are sorted."
714   :group 'gnus-summary-sort
715   :type '(repeat (choice (function-item gnus-article-sort-by-number)
716                          (function-item gnus-article-sort-by-author)
717                          (function-item gnus-article-sort-by-subject)
718                          (function-item gnus-article-sort-by-date)
719                          (function-item gnus-article-sort-by-score)
720                          (function-item gnus-article-sort-by-random)
721                          (function :tag "other"))))
722
723 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
724   "*List of functions used for sorting threads in the summary buffer.
725 By default, threads are sorted by article number.
726
727 Each function takes two threads and returns non-nil if the first
728 thread should be sorted before the other.  If you use more than one
729 function, the primary sort function should be the last.  You should
730 probably always include `gnus-thread-sort-by-number' in the list of
731 sorting functions -- preferably first.  Also note that sorting by date
732 is often much slower than sorting by number, and the sorting order is
733 very similar.  (Sorting by date means sorting by the time the message
734 was sent, sorting by number means sorting by arrival time.)
735
736 Ready-made functions include `gnus-thread-sort-by-number',
737 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
738 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
739 `gnus-thread-sort-by-most-recent-number',
740 `gnus-thread-sort-by-most-recent-date',
741 `gnus-thread-sort-by-random', and
742 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
743
744 When threading is turned off, the variable
745 `gnus-article-sort-functions' controls how articles are sorted."
746   :group 'gnus-summary-sort
747   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
748                          (function-item gnus-thread-sort-by-author)
749                          (function-item gnus-thread-sort-by-subject)
750                          (function-item gnus-thread-sort-by-date)
751                          (function-item gnus-thread-sort-by-score)
752                          (function-item gnus-thread-sort-by-total-score)
753                          (function-item gnus-thread-sort-by-random)
754                          (function :tag "other"))))
755
756 (defcustom gnus-thread-score-function '+
757   "*Function used for calculating the total score of a thread.
758
759 The function is called with the scores of the article and each
760 subthread and should then return the score of the thread.
761
762 Some functions you can use are `+', `max', or `min'."
763   :group 'gnus-summary-sort
764   :type 'function)
765
766 (defcustom gnus-summary-expunge-below nil
767   "All articles that have a score less than this variable will be expunged.
768 This variable is local to the summary buffers."
769   :group 'gnus-score-default
770   :type '(choice (const :tag "off" nil)
771                  integer))
772
773 (defcustom gnus-thread-expunge-below nil
774   "All threads that have a total score less than this variable will be expunged.
775 See `gnus-thread-score-function' for en explanation of what a
776 \"thread score\" is.
777
778 This variable is local to the summary buffers."
779   :group 'gnus-threading
780   :group 'gnus-score-default
781   :type '(choice (const :tag "off" nil)
782                  integer))
783
784 (defcustom gnus-summary-mode-hook nil
785   "*A hook for Gnus summary mode.
786 This hook is run before any variables are set in the summary buffer."
787   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
788   :group 'gnus-summary-various
789   :type 'hook)
790
791 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
792 (when (featurep 'xemacs)
793   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
794   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
795   (add-hook 'gnus-summary-mode-hook
796             'gnus-xmas-switch-horizontal-scrollbar-off))
797
798 (defcustom gnus-summary-menu-hook nil
799   "*Hook run after the creation of the summary mode menu."
800   :group 'gnus-summary-visual
801   :type 'hook)
802
803 (defcustom gnus-summary-exit-hook nil
804   "*A hook called on exit from the summary buffer.
805 It will be called with point in the group buffer."
806   :group 'gnus-summary-exit
807   :type 'hook)
808
809 (defcustom gnus-summary-prepare-hook nil
810   "*A hook called after the summary buffer has been generated.
811 If you want to modify the summary buffer, you can use this hook."
812   :group 'gnus-summary-various
813   :type 'hook)
814
815 (defcustom gnus-summary-prepared-hook nil
816   "*A hook called as the last thing after the summary buffer has been generated."
817   :group 'gnus-summary-various
818   :type 'hook)
819
820 (defcustom gnus-summary-generate-hook nil
821   "*A hook run just before generating the summary buffer.
822 This hook is commonly used to customize threading variables and the
823 like."
824   :group 'gnus-summary-various
825   :type 'hook)
826
827 (defcustom gnus-select-group-hook nil
828   "*A hook called when a newsgroup is selected.
829
830 If you'd like to simplify subjects like the
831 `gnus-summary-next-same-subject' command does, you can use the
832 following hook:
833
834  (add-hook gnus-select-group-hook
835            (lambda ()
836              (mapcar (lambda (header)
837                        (mail-header-set-subject
838                         header
839                         (gnus-simplify-subject
840                          (mail-header-subject header) 're-only)))
841                      gnus-newsgroup-headers)))"
842   :group 'gnus-group-select
843   :type 'hook)
844
845 (defcustom gnus-select-article-hook nil
846   "*A hook called when an article is selected."
847   :group 'gnus-summary-choose
848   :options '(gnus-agent-fetch-selected-article)
849   :type 'hook)
850
851 (defcustom gnus-visual-mark-article-hook
852   (list 'gnus-highlight-selected-summary)
853   "*Hook run after selecting an article in the summary buffer.
854 It is meant to be used for highlighting the article in some way.  It
855 is not run if `gnus-visual' is nil."
856   :group 'gnus-summary-visual
857   :type 'hook)
858
859 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
860   "*A hook called before parsing the headers."
861   :group 'gnus-various
862   :type 'hook)
863
864 (defcustom gnus-exit-group-hook nil
865   "*A hook called when exiting summary mode.
866 This hook is not called from the non-updating exit commands like `Q'."
867   :group 'gnus-various
868   :type 'hook)
869
870 (defcustom gnus-summary-update-hook
871   (list 'gnus-summary-highlight-line)
872   "*A hook called when a summary line is changed.
873 The hook will not be called if `gnus-visual' is nil.
874
875 The default function `gnus-summary-highlight-line' will
876 highlight the line according to the `gnus-summary-highlight'
877 variable."
878   :group 'gnus-summary-visual
879   :type 'hook)
880
881 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
882   "*A hook called when an article is selected for the first time.
883 The hook is intended to mark an article as read (or unread)
884 automatically when it is selected."
885   :group 'gnus-summary-choose
886   :type 'hook)
887
888 (defcustom gnus-group-no-more-groups-hook nil
889   "*A hook run when returning to group mode having no more (unread) groups."
890   :group 'gnus-group-select
891   :type 'hook)
892
893 (defcustom gnus-ps-print-hook nil
894   "*A hook run before ps-printing something from Gnus."
895   :group 'gnus-summary
896   :type 'hook)
897
898 (defcustom gnus-summary-article-move-hook nil
899   "*A hook called after an article is moved, copied, respooled, or crossposted."
900   :group 'gnus-summary
901   :type 'hook)
902
903 (defcustom gnus-summary-article-delete-hook nil
904   "*A hook called after an article is deleted."
905   :group 'gnus-summary
906   :type 'hook)
907
908 (defcustom gnus-summary-article-expire-hook nil
909   "*A hook called after an article is expired."
910   :group 'gnus-summary
911   :type 'hook)
912
913 (defcustom gnus-summary-display-arrow
914   (and (fboundp 'display-graphic-p)
915        (display-graphic-p))
916   "*If non-nil, display an arrow highlighting the current article."
917   :version "21.1"
918   :group 'gnus-summary
919   :type 'boolean)
920
921 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
922   "Face used for highlighting the current article in the summary buffer."
923   :group 'gnus-summary-visual
924   :type 'face)
925
926 (defvar gnus-tmp-downloaded nil)
927
928 (defcustom gnus-summary-highlight
929   '(((eq mark gnus-canceled-mark)
930      . gnus-summary-cancelled-face)
931     ((and uncached (> score default-high))
932      . gnus-summary-high-undownloaded-face)
933     ((and uncached (< score default-low))
934      . gnus-summary-low-undownloaded-face)
935     (uncached
936      . gnus-summary-normal-undownloaded-face)
937     ((and (> score default-high)
938           (or (eq mark gnus-dormant-mark)
939               (eq mark gnus-ticked-mark)))
940      . gnus-summary-high-ticked-face)
941     ((and (< score default-low)
942           (or (eq mark gnus-dormant-mark)
943               (eq mark gnus-ticked-mark)))
944      . gnus-summary-low-ticked-face)
945     ((or (eq mark gnus-dormant-mark)
946          (eq mark gnus-ticked-mark))
947      . gnus-summary-normal-ticked-face)
948     ((and (> score default-high) (eq mark gnus-ancient-mark))
949      . gnus-summary-high-ancient-face)
950     ((and (< score default-low) (eq mark gnus-ancient-mark))
951      . gnus-summary-low-ancient-face)
952     ((eq mark gnus-ancient-mark)
953      . gnus-summary-normal-ancient-face)
954     ((and (> score default-high) (eq mark gnus-unread-mark))
955      . gnus-summary-high-unread-face)
956     ((and (< score default-low) (eq mark gnus-unread-mark))
957      . gnus-summary-low-unread-face)
958     ((eq mark gnus-unread-mark)
959      . gnus-summary-normal-unread-face)
960     ((> score default-high)
961      . gnus-summary-high-read-face)
962     ((< score default-low)
963      . gnus-summary-low-read-face)
964     (t
965      . gnus-summary-normal-read-face))
966   "*Controls the highlighting of summary buffer lines.
967
968 A list of (FORM . FACE) pairs.  When deciding how a a particular
969 summary line should be displayed, each form is evaluated.  The content
970 of the face field after the first true form is used.  You can change
971 how those summary lines are displayed, by editing the face field.
972
973 You can use the following variables in the FORM field.
974
975 score:        The article's score
976 default:      The default article score.
977 default-high: The default score for high scored articles.
978 default-low:  The default score for low scored articles.
979 below:        The score below which articles are automatically marked as read.
980 mark:         The articles mark."
981   :group 'gnus-summary-visual
982   :type '(repeat (cons (sexp :tag "Form" nil)
983                        face)))
984
985 (defcustom gnus-alter-header-function nil
986   "Function called to allow alteration of article header structures.
987 The function is called with one parameter, the article header vector,
988 which it may alter in any way."
989   :type '(choice (const :tag "None" nil)
990                  function)
991   :group 'gnus-summary)
992
993 (defvar gnus-decode-encoded-word-function
994   (mime-find-field-decoder 'From 'nov)
995   "Variable that says which function should be used to decode a string with encoded words.")
996
997 (defcustom gnus-extra-headers '(To Newsgroups)
998   "*Extra headers to parse."
999   :version "21.1"
1000   :group 'gnus-summary
1001   :type '(repeat symbol))
1002
1003 (defcustom gnus-ignored-from-addresses
1004   (and user-mail-address (regexp-quote user-mail-address))
1005   "*Regexp of From headers that may be suppressed in favor of To headers."
1006   :version "21.1"
1007   :group 'gnus-summary
1008   :type 'regexp)
1009
1010 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1011   "List of charsets that should be ignored.
1012 When these charsets are used in the \"charset\" parameter, the
1013 default charset will be used instead."
1014   :version "21.1"
1015   :type '(repeat symbol)
1016   :group 'gnus-charset)
1017
1018 (gnus-define-group-parameter
1019  ignored-charsets
1020  :type list
1021  :function-document
1022  "Return the ignored charsets of GROUP."
1023  :variable gnus-group-ignored-charsets-alist
1024  :variable-default
1025  '(("alt\\.chinese\\.text" iso-8859-1))
1026  :variable-document
1027  "Alist of regexps (to match group names) and charsets that should be ignored.
1028 When these charsets are used in the \"charset\" parameter, the
1029 default charset will be used instead."
1030  :variable-group gnus-charset
1031  :variable-type '(repeat (cons (regexp :tag "Group")
1032                                (repeat symbol)))
1033  :parameter-type '(choice :tag "Ignored charsets"
1034                           :value nil
1035                           (repeat (symbol)))
1036  :parameter-document       "\
1037 List of charsets that should be ignored.
1038
1039 When these charsets are used in the \"charset\" parameter, the
1040 default charset will be used instead.")
1041
1042 (defcustom gnus-group-highlight-words-alist nil
1043   "Alist of group regexps and highlight regexps.
1044 This variable uses the same syntax as `gnus-emphasis-alist'."
1045   :version "21.1"
1046   :type '(repeat (cons (regexp :tag "Group")
1047                        (repeat (list (regexp :tag "Highlight regexp")
1048                                      (number :tag "Group for entire word" 0)
1049                                      (number :tag "Group for displayed part" 0)
1050                                      (symbol :tag "Face"
1051                                              gnus-emphasis-highlight-words)))))
1052   :group 'gnus-summary-visual)
1053
1054 (defcustom gnus-use-wheel nil
1055   "Use Intelli-mouse on summary movement"
1056   :type 'boolean
1057   :group 'gnus-summary-maneuvering)
1058
1059 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1060   "Amount to scroll messages by spinning the mouse wheel.
1061 This is actually a cons cell, where the first item is the amount to scroll
1062 on a normal wheel event, and the second is the amount to scroll when the
1063 wheel is moved with the shift key depressed."
1064   :type '(cons (integer :tag "Shift") integer)
1065   :group 'gnus-summary-maneuvering)
1066
1067 (defcustom gnus-wheel-edge-resistance 2
1068   "How hard it should be to change the current article
1069 by moving the mouse over the edge of the article window."
1070   :type 'integer
1071   :group 'gnus-summary-maneuvering)
1072
1073 (defcustom gnus-summary-show-article-charset-alist
1074   nil
1075   "Alist of number and charset.
1076 The article will be shown with the charset corresponding to the
1077 numbered argument.
1078 For example: ((1 . cn-gb-2312) (2 . big5))."
1079   :version "21.1"
1080   :type '(repeat (cons (number :tag "Argument" 1)
1081                        (symbol :tag "Charset")))
1082   :group 'gnus-charset)
1083
1084 (defcustom gnus-preserve-marks t
1085   "Whether marks are preserved when moving, copying and respooling messages."
1086   :version "21.1"
1087   :type 'boolean
1088   :group 'gnus-summary-marks)
1089
1090 (defcustom gnus-alter-articles-to-read-function nil
1091   "Function to be called to alter the list of articles to be selected."
1092   :type '(choice (const nil) function)
1093   :group 'gnus-summary)
1094
1095 (defcustom gnus-orphan-score nil
1096   "*All orphans get this score added.  Set in the score file."
1097   :group 'gnus-score-default
1098   :type '(choice (const nil)
1099                  integer))
1100
1101 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1102   "*A regexp to match MIME parts when saving multiple parts of a
1103 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1104 This regexp will be used by default when prompting the user for which
1105 type of files to save."
1106   :group 'gnus-summary
1107   :type 'regexp)
1108
1109 (defcustom gnus-read-all-available-headers nil
1110   "Whether Gnus should parse all headers made available to it.
1111 This is mostly relevant for slow backends where the user may
1112 wish to widen the summary buffer to include all headers
1113 that were fetched.  Say, for nnultimate groups."
1114   :group 'gnus-summary
1115   :type '(choice boolean regexp))
1116
1117 (defcustom gnus-summary-muttprint-program "muttprint"
1118   "Command (and optional arguments) used to run Muttprint."
1119   :version "21.3"
1120   :group 'gnus-summary
1121   :type 'string)
1122
1123 (defcustom gnus-article-loose-mime nil
1124   "If non-nil, don't require MIME-Version header.
1125 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1126 supply the MIME-Version header or deliberately strip it From the mail.
1127 Set it to non-nil, Gnus will treat some articles as MIME even if
1128 the MIME-Version header is missed."
1129   :version "21.3"
1130   :type 'boolean
1131   :group 'gnus-article-mime)
1132
1133 (defcustom gnus-article-emulate-mime t
1134   "If non-nil, use MIME emulation for uuencode and the like.
1135 This means that Gnus will search message bodies for text that look
1136 like uuencoded bits, yEncoded bits, and so on, and present that using
1137 the normal Gnus MIME machinery."
1138   :type 'boolean
1139   :group 'gnus-article-mime)
1140
1141 ;;; Internal variables
1142
1143 (defvar gnus-summary-display-cache nil)
1144 (defvar gnus-article-mime-handles nil)
1145 (defvar gnus-article-decoded-p nil)
1146 (defvar gnus-article-charset nil)
1147 (defvar gnus-article-ignored-charsets nil)
1148 (defvar gnus-scores-exclude-files nil)
1149 (defvar gnus-page-broken nil)
1150
1151 (defvar gnus-original-article nil)
1152 (defvar gnus-article-internal-prepare-hook nil)
1153 (defvar gnus-newsgroup-process-stack nil)
1154
1155 (defvar gnus-thread-indent-array nil)
1156 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1157 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1158   "Function called to sort the articles within a thread after it has been gathered together.")
1159
1160 (defvar gnus-summary-save-parts-type-history nil)
1161 (defvar gnus-summary-save-parts-last-directory nil)
1162
1163 ;; Avoid highlighting in kill files.
1164 (defvar gnus-summary-inhibit-highlight nil)
1165 (defvar gnus-newsgroup-selected-overlay nil)
1166 (defvar gnus-inhibit-limiting nil)
1167 (defvar gnus-newsgroup-adaptive-score-file nil)
1168 (defvar gnus-current-score-file nil)
1169 (defvar gnus-current-move-group nil)
1170 (defvar gnus-current-copy-group nil)
1171 (defvar gnus-current-crosspost-group nil)
1172 (defvar gnus-newsgroup-display nil)
1173
1174 (defvar gnus-newsgroup-dependencies nil)
1175 (defvar gnus-newsgroup-adaptive nil)
1176 (defvar gnus-summary-display-article-function nil)
1177 (defvar gnus-summary-highlight-line-function nil
1178   "Function called after highlighting a summary line.")
1179
1180 (defvar gnus-summary-line-format-alist
1181   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1182     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1183     (?s gnus-tmp-subject-or-nil ?s)
1184     (?n gnus-tmp-name ?s)
1185     (?A (std11-address-string
1186          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1187     (?a (or (std11-full-name-string
1188              (car (mime-entity-read-field gnus-tmp-header 'From)))
1189             gnus-tmp-from) ?s)
1190     (?F gnus-tmp-from ?s)
1191     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1192     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1193     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1194     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1195     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1196     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1197     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1198     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1199     (?L gnus-tmp-lines ?s)
1200     (?O gnus-tmp-downloaded ?c)
1201     (?I gnus-tmp-indentation ?s)
1202     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1203     (?R gnus-tmp-replied ?c)
1204     (?\[ gnus-tmp-opening-bracket ?c)
1205     (?\] gnus-tmp-closing-bracket ?c)
1206     (?\> (make-string gnus-tmp-level ? ) ?s)
1207     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1208     (?i gnus-tmp-score ?d)
1209     (?z gnus-tmp-score-char ?c)
1210     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1211     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1212     (?U gnus-tmp-unread ?c)
1213     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1214         ?s)
1215     (?t (gnus-summary-number-of-articles-in-thread
1216          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1217         ?d)
1218     (?e (gnus-summary-number-of-articles-in-thread
1219          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1220         ?c)
1221     (?u gnus-tmp-user-defined ?s)
1222     (?P (gnus-pick-line-number) ?d)
1223     (?B gnus-tmp-thread-tree-header-string ?s)
1224     (user-date (gnus-user-date
1225                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1226   "An alist of format specifications that can appear in summary lines.
1227 These are paired with what variables they correspond with, along with
1228 the type of the variable (string, integer, character, etc).")
1229
1230 (defvar gnus-summary-dummy-line-format-alist
1231   `((?S gnus-tmp-subject ?s)
1232     (?N gnus-tmp-number ?d)
1233     (?u gnus-tmp-user-defined ?s)))
1234
1235 (defvar gnus-summary-mode-line-format-alist
1236   `((?G gnus-tmp-group-name ?s)
1237     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1238     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1239     (?A gnus-tmp-article-number ?d)
1240     (?Z gnus-tmp-unread-and-unselected ?s)
1241     (?V gnus-version ?s)
1242     (?U gnus-tmp-unread-and-unticked ?d)
1243     (?S gnus-tmp-subject ?s)
1244     (?e gnus-tmp-unselected ?d)
1245     (?u gnus-tmp-user-defined ?s)
1246     (?d (length gnus-newsgroup-dormant) ?d)
1247     (?t (length gnus-newsgroup-marked) ?d)
1248     (?h (length gnus-newsgroup-spam-marked) ?d)
1249     (?r (length gnus-newsgroup-reads) ?d)
1250     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1251     (?E gnus-newsgroup-expunged-tally ?d)
1252     (?s (gnus-current-score-file-nondirectory) ?s)))
1253
1254 (defvar gnus-last-search-regexp nil
1255   "Default regexp for article search command.")
1256
1257 (defvar gnus-summary-search-article-matched-data nil
1258   "Last matched data of article search command.  It is the local variable
1259 in `gnus-article-buffer' which consists of the list of start position,
1260 end position and text.")
1261
1262 (defvar gnus-last-shell-command nil
1263   "Default shell command on article.")
1264
1265 (defvar gnus-newsgroup-agentized nil
1266   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1267 (defvar gnus-newsgroup-begin nil)
1268 (defvar gnus-newsgroup-end nil)
1269 (defvar gnus-newsgroup-last-rmail nil)
1270 (defvar gnus-newsgroup-last-mail nil)
1271 (defvar gnus-newsgroup-last-folder nil)
1272 (defvar gnus-newsgroup-last-file nil)
1273 (defvar gnus-newsgroup-auto-expire nil)
1274 (defvar gnus-newsgroup-active nil)
1275
1276 (defvar gnus-newsgroup-data nil)
1277 (defvar gnus-newsgroup-data-reverse nil)
1278 (defvar gnus-newsgroup-limit nil)
1279 (defvar gnus-newsgroup-limits nil)
1280
1281 (defvar gnus-newsgroup-unreads nil
1282   "Sorted list of unread articles in the current newsgroup.")
1283
1284 (defvar gnus-newsgroup-unselected nil
1285   "Sorted list of unselected unread articles in the current newsgroup.")
1286
1287 (defvar gnus-newsgroup-reads nil
1288   "Alist of read articles and article marks in the current newsgroup.")
1289
1290 (defvar gnus-newsgroup-expunged-tally nil)
1291
1292 (defvar gnus-newsgroup-marked nil
1293   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1294
1295 (defvar gnus-newsgroup-spam-marked nil
1296   "List of ranges of articles that have been marked as spam.")
1297
1298 (defvar gnus-newsgroup-killed nil
1299   "List of ranges of articles that have been through the scoring process.")
1300
1301 (defvar gnus-newsgroup-cached nil
1302   "Sorted list of articles that come from the article cache.")
1303
1304 (defvar gnus-newsgroup-saved nil
1305   "List of articles that have been saved.")
1306
1307 (defvar gnus-newsgroup-kill-headers nil)
1308
1309 (defvar gnus-newsgroup-replied nil
1310   "List of articles that have been replied to in the current newsgroup.")
1311
1312 (defvar gnus-newsgroup-forwarded nil
1313   "List of articles that have been forwarded in the current newsgroup.")
1314
1315 (defvar gnus-newsgroup-recent nil
1316   "List of articles that have are recent in the current newsgroup.")
1317
1318 (defvar gnus-newsgroup-expirable nil
1319   "Sorted list of articles in the current newsgroup that can be expired.")
1320
1321 (defvar gnus-newsgroup-processable nil
1322   "List of articles in the current newsgroup that can be processed.")
1323
1324 (defvar gnus-newsgroup-downloadable nil
1325   "Sorted list of articles in the current newsgroup that can be processed.")
1326
1327 (defvar gnus-newsgroup-unfetched nil
1328   "Sorted list of articles in the current newsgroup whose headers have
1329 not been fetched into the agent.
1330
1331 This list will always be a subset of gnus-newsgroup-undownloaded.")
1332
1333 (defvar gnus-newsgroup-undownloaded nil
1334   "List of articles in the current newsgroup that haven't been downloaded.")
1335
1336 (defvar gnus-newsgroup-unsendable nil
1337   "List of articles in the current newsgroup that won't be sent.")
1338
1339 (defvar gnus-newsgroup-bookmarks nil
1340   "List of articles in the current newsgroup that have bookmarks.")
1341
1342 (defvar gnus-newsgroup-dormant nil
1343   "Sorted list of dormant articles in the current newsgroup.")
1344
1345 (defvar gnus-newsgroup-unseen nil
1346   "List of unseen articles in the current newsgroup.")
1347
1348 (defvar gnus-newsgroup-seen nil
1349   "Range of seen articles in the current newsgroup.")
1350
1351 (defvar gnus-newsgroup-articles nil
1352   "List of articles in the current newsgroup.")
1353
1354 (defvar gnus-newsgroup-scored nil
1355   "List of scored articles in the current newsgroup.")
1356
1357 (defvar gnus-newsgroup-incorporated nil
1358   "List of incorporated articles in the current newsgroup.")
1359
1360 (defvar gnus-newsgroup-headers nil
1361   "List of article headers in the current newsgroup.")
1362
1363 (defvar gnus-newsgroup-threads nil)
1364
1365 (defvar gnus-newsgroup-prepared nil
1366   "Whether the current group has been prepared properly.")
1367
1368 (defvar gnus-newsgroup-ancient nil
1369   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1370
1371 (defvar gnus-newsgroup-sparse nil)
1372
1373 (defvar gnus-current-article nil)
1374 (defvar gnus-article-current nil)
1375 (defvar gnus-current-headers nil)
1376 (defvar gnus-have-all-headers nil)
1377 (defvar gnus-last-article nil)
1378 (defvar gnus-newsgroup-history nil)
1379 (defvar gnus-newsgroup-charset nil)
1380 (defvar gnus-newsgroup-ephemeral-charset nil)
1381 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1382
1383 (defvar gnus-article-before-search nil)
1384
1385 (defvar gnus-summary-local-variables
1386   '(gnus-newsgroup-name
1387     gnus-newsgroup-begin gnus-newsgroup-end
1388     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1389     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1390     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1391     gnus-newsgroup-unselected gnus-newsgroup-marked
1392     gnus-newsgroup-spam-marked
1393     gnus-newsgroup-reads gnus-newsgroup-saved
1394     gnus-newsgroup-replied gnus-newsgroup-forwarded
1395     gnus-newsgroup-recent
1396     gnus-newsgroup-expirable
1397     gnus-newsgroup-processable gnus-newsgroup-killed
1398     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1399     gnus-newsgroup-unfetched
1400     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1401     gnus-newsgroup-seen gnus-newsgroup-articles
1402     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1403     gnus-newsgroup-headers gnus-newsgroup-threads
1404     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1405     gnus-current-article gnus-current-headers gnus-have-all-headers
1406     gnus-last-article gnus-article-internal-prepare-hook
1407     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1408     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1409     gnus-thread-expunge-below
1410     gnus-score-alist gnus-current-score-file
1411     (gnus-summary-expunge-below . global)
1412     (gnus-summary-mark-below . global)
1413     (gnus-orphan-score . global)
1414     gnus-newsgroup-active gnus-scores-exclude-files
1415     gnus-newsgroup-history gnus-newsgroup-ancient
1416     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1417     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1418     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1419     (gnus-newsgroup-expunged-tally . 0)
1420     gnus-cache-removable-articles gnus-newsgroup-cached
1421     gnus-newsgroup-data gnus-newsgroup-data-reverse
1422     gnus-newsgroup-limit gnus-newsgroup-limits
1423     gnus-newsgroup-charset gnus-newsgroup-display
1424     gnus-newsgroup-incorporated)
1425   "Variables that are buffer-local to the summary buffers.")
1426
1427 (defvar gnus-newsgroup-variables nil
1428   "A list of variables that have separate values in different newsgroups.
1429 A list of newsgroup (summary buffer) local variables, or cons of
1430 variables and their default values (when the default values are not
1431 nil), that should be made global while the summary buffer is active.
1432 These variables can be used to set variables in the group parameters
1433 while still allowing them to affect operations done in other
1434 buffers. For example:
1435
1436 \(setq gnus-newsgroup-variables
1437      '(message-use-followup-to
1438        (gnus-visible-headers .
1439          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1440 ")
1441
1442 ;; Byte-compiler warning.
1443 (eval-when-compile
1444   ;; Bind features so that require will believe that gnus-sum has
1445   ;; already been loaded (avoids infinite recursion)
1446   (let ((features (cons 'gnus-sum features)))
1447     ;; Several of the declarations in gnus-sum are needed to load the
1448     ;; following files. Right now, these definitions have been
1449     ;; compiled but not defined (evaluated).  We could either do a
1450     ;; eval-and-compile about all of the declarations or evaluate the
1451     ;; source file.
1452     (if (boundp 'gnus-newsgroup-variables)
1453         nil
1454       (load "gnus-sum.el" t t t))
1455     (require 'gnus)
1456     (require 'gnus-agent)
1457     (require 'gnus-art)))
1458
1459 ;; Subject simplification.
1460
1461 (defun gnus-simplify-whitespace (str)
1462   "Remove excessive whitespace from STR."
1463   ;; Multiple spaces.
1464   (while (string-match "[ \t][ \t]+" str)
1465     (setq str (concat (substring str 0 (match-beginning 0))
1466                         " "
1467                         (substring str (match-end 0)))))
1468   ;; Leading spaces.
1469   (when (string-match "^[ \t]+" str)
1470     (setq str (substring str (match-end 0))))
1471   ;; Trailing spaces.
1472   (when (string-match "[ \t]+$" str)
1473     (setq str (substring str 0 (match-beginning 0))))
1474   str)
1475
1476 (defun gnus-simplify-all-whitespace (str)
1477   "Remove all whitespace from STR."
1478   (while (string-match "[ \t\n]+" str)
1479     (setq str (replace-match "" nil nil str)))
1480   str)
1481
1482 (defsubst gnus-simplify-subject-re (subject)
1483   "Remove \"Re:\" from subject lines."
1484   (if (string-match message-subject-re-regexp subject)
1485       (substring subject (match-end 0))
1486     subject))
1487
1488 (defun gnus-simplify-subject (subject &optional re-only)
1489   "Remove `Re:' and words in parentheses.
1490 If RE-ONLY is non-nil, strip leading `Re:'s only."
1491   (let ((case-fold-search t))           ;Ignore case.
1492     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1493     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1494       (setq subject (substring subject (match-end 0))))
1495     ;; Remove uninteresting prefixes.
1496     (when (and (not re-only)
1497                gnus-simplify-ignored-prefixes
1498                (string-match gnus-simplify-ignored-prefixes subject))
1499       (setq subject (substring subject (match-end 0))))
1500     ;; Remove words in parentheses from end.
1501     (unless re-only
1502       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1503         (setq subject (substring subject 0 (match-beginning 0)))))
1504     ;; Return subject string.
1505     subject))
1506
1507 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1508 ;; all whitespace.
1509 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1510   (goto-char (point-min))
1511   (while (re-search-forward regexp nil t)
1512     (replace-match (or newtext ""))))
1513
1514 (defun gnus-simplify-buffer-fuzzy ()
1515   "Simplify string in the buffer fuzzily.
1516 The string in the accessible portion of the current buffer is simplified.
1517 It is assumed to be a single-line subject.
1518 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1519 matter is removed.  Additional things can be deleted by setting
1520 `gnus-simplify-subject-fuzzy-regexp'."
1521   (let ((case-fold-search t)
1522         (modified-tick))
1523     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1524
1525     (while (not (eq modified-tick (buffer-modified-tick)))
1526       (setq modified-tick (buffer-modified-tick))
1527       (cond
1528        ((listp gnus-simplify-subject-fuzzy-regexp)
1529         (mapcar 'gnus-simplify-buffer-fuzzy-step
1530                 gnus-simplify-subject-fuzzy-regexp))
1531        (gnus-simplify-subject-fuzzy-regexp
1532         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1533       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1534       (gnus-simplify-buffer-fuzzy-step
1535        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1536       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1537
1538     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1539     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1540     (gnus-simplify-buffer-fuzzy-step " $")
1541     (gnus-simplify-buffer-fuzzy-step "^ +")))
1542
1543 (defun gnus-simplify-subject-fuzzy (subject)
1544   "Simplify a subject string fuzzily.
1545 See `gnus-simplify-buffer-fuzzy' for details."
1546   (save-excursion
1547     (gnus-set-work-buffer)
1548     (let ((case-fold-search t))
1549       ;; Remove uninteresting prefixes.
1550       (when (and gnus-simplify-ignored-prefixes
1551                  (string-match gnus-simplify-ignored-prefixes subject))
1552         (setq subject (substring subject (match-end 0))))
1553       (insert subject)
1554       (inline (gnus-simplify-buffer-fuzzy))
1555       (buffer-string))))
1556
1557 (defsubst gnus-simplify-subject-fully (subject)
1558   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1559   (cond
1560    (gnus-simplify-subject-functions
1561     (gnus-map-function gnus-simplify-subject-functions subject))
1562    ((null gnus-summary-gather-subject-limit)
1563     (gnus-simplify-subject-re subject))
1564    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1565     (gnus-simplify-subject-fuzzy subject))
1566    ((numberp gnus-summary-gather-subject-limit)
1567     (gnus-limit-string (gnus-simplify-subject-re subject)
1568                        gnus-summary-gather-subject-limit))
1569    (t
1570     subject)))
1571
1572 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1573   "Check whether two subjects are equal.
1574 If optional argument SIMPLE-FIRST is t, first argument is already
1575 simplified."
1576   (cond
1577    ((null simple-first)
1578     (equal (gnus-simplify-subject-fully s1)
1579            (gnus-simplify-subject-fully s2)))
1580    (t
1581     (equal s1
1582            (gnus-simplify-subject-fully s2)))))
1583
1584 (defun gnus-summary-bubble-group ()
1585   "Increase the score of the current group.
1586 This is a handy function to add to `gnus-summary-exit-hook' to
1587 increase the score of each group you read."
1588   (gnus-group-add-score gnus-newsgroup-name))
1589
1590 \f
1591 ;;;
1592 ;;; Gnus summary mode
1593 ;;;
1594
1595 (put 'gnus-summary-mode 'mode-class 'special)
1596
1597 (defvar gnus-article-commands-menu)
1598
1599 ;; Non-orthogonal keys
1600
1601 (gnus-define-keys gnus-summary-mode-map
1602   " " gnus-summary-next-page
1603   "\177" gnus-summary-prev-page
1604   [delete] gnus-summary-prev-page
1605   [backspace] gnus-summary-prev-page
1606   "\r" gnus-summary-scroll-up
1607   "\M-\r" gnus-summary-scroll-down
1608   "n" gnus-summary-next-unread-article
1609   "p" gnus-summary-prev-unread-article
1610   "N" gnus-summary-next-article
1611   "P" gnus-summary-prev-article
1612   "\M-\C-n" gnus-summary-next-same-subject
1613   "\M-\C-p" gnus-summary-prev-same-subject
1614   "\M-n" gnus-summary-next-unread-subject
1615   "\M-p" gnus-summary-prev-unread-subject
1616   "." gnus-summary-first-unread-article
1617   "," gnus-summary-best-unread-article
1618   "\M-s" gnus-summary-search-article-forward
1619   "\M-r" gnus-summary-search-article-backward
1620   "<" gnus-summary-beginning-of-article
1621   ">" gnus-summary-end-of-article
1622   "j" gnus-summary-goto-article
1623   "^" gnus-summary-refer-parent-article
1624   "\M-^" gnus-summary-refer-article
1625   "u" gnus-summary-tick-article-forward
1626   "!" gnus-summary-tick-article-forward
1627   "U" gnus-summary-tick-article-backward
1628   "d" gnus-summary-mark-as-read-forward
1629   "D" gnus-summary-mark-as-read-backward
1630   "E" gnus-summary-mark-as-expirable
1631   "\M-u" gnus-summary-clear-mark-forward
1632   "\M-U" gnus-summary-clear-mark-backward
1633   "k" gnus-summary-kill-same-subject-and-select
1634   "\C-k" gnus-summary-kill-same-subject
1635   "\M-\C-k" gnus-summary-kill-thread
1636   "\M-\C-l" gnus-summary-lower-thread
1637   "e" gnus-summary-edit-article
1638   "#" gnus-summary-mark-as-processable
1639   "\M-#" gnus-summary-unmark-as-processable
1640   "\M-\C-t" gnus-summary-toggle-threads
1641   "\M-\C-s" gnus-summary-show-thread
1642   "\M-\C-h" gnus-summary-hide-thread
1643   "\M-\C-f" gnus-summary-next-thread
1644   "\M-\C-b" gnus-summary-prev-thread
1645   [(meta down)] gnus-summary-next-thread
1646   [(meta up)] gnus-summary-prev-thread
1647   "\M-\C-u" gnus-summary-up-thread
1648   "\M-\C-d" gnus-summary-down-thread
1649   "&" gnus-summary-execute-command
1650   "c" gnus-summary-catchup-and-exit
1651   "\C-w" gnus-summary-mark-region-as-read
1652   "\C-t" gnus-summary-toggle-truncation
1653   "?" gnus-summary-mark-as-dormant
1654   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1655   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1656   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1657   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1658   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1659   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1660   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1661   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1662   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1663   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1664   "=" gnus-summary-expand-window
1665   "\C-x\C-s" gnus-summary-reselect-current-group
1666   "\M-g" gnus-summary-rescan-group
1667   "w" gnus-summary-stop-page-breaking
1668   "\C-c\C-r" gnus-summary-caesar-message
1669   "\M-t" gnus-summary-toggle-mime
1670   "f" gnus-summary-followup
1671   "F" gnus-summary-followup-with-original
1672   "C" gnus-summary-cancel-article
1673   "r" gnus-summary-reply
1674   "R" gnus-summary-reply-with-original
1675   "\C-c\C-f" gnus-summary-mail-forward
1676   "o" gnus-summary-save-article
1677   "\C-o" gnus-summary-save-article-mail
1678   "|" gnus-summary-pipe-output
1679   "\M-k" gnus-summary-edit-local-kill
1680   "\M-K" gnus-summary-edit-global-kill
1681   ;; "V" gnus-version
1682   "\C-c\C-d" gnus-summary-describe-group
1683   "q" gnus-summary-exit
1684   "Q" gnus-summary-exit-no-update
1685   "\C-c\C-i" gnus-info-find-node
1686   gnus-mouse-2 gnus-mouse-pick-article
1687   "m" gnus-summary-mail-other-window
1688   "a" gnus-summary-post-news
1689   "i" gnus-summary-news-other-window
1690   "x" gnus-summary-limit-to-unread
1691   "s" gnus-summary-isearch-article
1692   "t" gnus-summary-toggle-header
1693   "g" gnus-summary-show-article
1694   "l" gnus-summary-goto-last-article
1695   "v" gnus-summary-preview-mime-message
1696   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1697   "\C-d" gnus-summary-enter-digest-group
1698   "\M-\C-d" gnus-summary-read-document
1699   "\M-\C-e" gnus-summary-edit-parameters
1700   "\M-\C-a" gnus-summary-customize-parameters
1701   "\C-c\C-b" gnus-bug
1702   "\C-c\C-n" gnus-namazu-search
1703   "*" gnus-cache-enter-article
1704   "\M-*" gnus-cache-remove-article
1705   "\M-&" gnus-summary-universal-argument
1706   "\C-l" gnus-recenter
1707   "I" gnus-summary-increase-score
1708   "L" gnus-summary-lower-score
1709   "\M-i" gnus-symbolic-argument
1710   "h" gnus-summary-select-article-buffer
1711
1712   "V" gnus-summary-score-map
1713   "X" gnus-uu-extract-map
1714   "S" gnus-summary-send-map)
1715
1716   ;; Sort of orthogonal keymap
1717 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1718   "t" gnus-summary-tick-article-forward
1719   "!" gnus-summary-tick-article-forward
1720   "d" gnus-summary-mark-as-read-forward
1721   "r" gnus-summary-mark-as-read-forward
1722   "c" gnus-summary-clear-mark-forward
1723   " " gnus-summary-clear-mark-forward
1724   "e" gnus-summary-mark-as-expirable
1725   "x" gnus-summary-mark-as-expirable
1726   "?" gnus-summary-mark-as-dormant
1727   "b" gnus-summary-set-bookmark
1728   "B" gnus-summary-remove-bookmark
1729   "#" gnus-summary-mark-as-processable
1730   "\M-#" gnus-summary-unmark-as-processable
1731   "S" gnus-summary-limit-include-expunged
1732   "C" gnus-summary-catchup
1733   "H" gnus-summary-catchup-to-here
1734   "h" gnus-summary-catchup-from-here
1735   "\C-c" gnus-summary-catchup-all
1736   "k" gnus-summary-kill-same-subject-and-select
1737   "K" gnus-summary-kill-same-subject
1738   "P" gnus-uu-mark-map)
1739
1740 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1741   "c" gnus-summary-clear-above
1742   "u" gnus-summary-tick-above
1743   "m" gnus-summary-mark-above
1744   "k" gnus-summary-kill-below)
1745
1746 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1747   "/" gnus-summary-limit-to-subject
1748   "n" gnus-summary-limit-to-articles
1749   "w" gnus-summary-pop-limit
1750   "s" gnus-summary-limit-to-subject
1751   "a" gnus-summary-limit-to-author
1752   "u" gnus-summary-limit-to-unread
1753   "m" gnus-summary-limit-to-marks
1754   "M" gnus-summary-limit-exclude-marks
1755   "v" gnus-summary-limit-to-score
1756   "*" gnus-summary-limit-include-cached
1757   "D" gnus-summary-limit-include-dormant
1758   "T" gnus-summary-limit-include-thread
1759   "d" gnus-summary-limit-exclude-dormant
1760   "t" gnus-summary-limit-to-age
1761   "." gnus-summary-limit-to-unseen
1762   "x" gnus-summary-limit-to-extra
1763   "p" gnus-summary-limit-to-display-predicate
1764   "E" gnus-summary-limit-include-expunged
1765   "c" gnus-summary-limit-exclude-childless-dormant
1766   "C" gnus-summary-limit-mark-excluded-as-read
1767   "o" gnus-summary-insert-old-articles
1768   "N" gnus-summary-insert-new-articles)
1769
1770 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1771   "n" gnus-summary-next-unread-article
1772   "p" gnus-summary-prev-unread-article
1773   "N" gnus-summary-next-article
1774   "P" gnus-summary-prev-article
1775   "\C-n" gnus-summary-next-same-subject
1776   "\C-p" gnus-summary-prev-same-subject
1777   "\M-n" gnus-summary-next-unread-subject
1778   "\M-p" gnus-summary-prev-unread-subject
1779   "f" gnus-summary-first-unread-article
1780   "b" gnus-summary-best-unread-article
1781   "j" gnus-summary-goto-article
1782   "g" gnus-summary-goto-subject
1783   "l" gnus-summary-goto-last-article
1784   "o" gnus-summary-pop-article)
1785
1786 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1787   "k" gnus-summary-kill-thread
1788   "l" gnus-summary-lower-thread
1789   "i" gnus-summary-raise-thread
1790   "T" gnus-summary-toggle-threads
1791   "t" gnus-summary-rethread-current
1792   "^" gnus-summary-reparent-thread
1793   "s" gnus-summary-show-thread
1794   "S" gnus-summary-show-all-threads
1795   "h" gnus-summary-hide-thread
1796   "H" gnus-summary-hide-all-threads
1797   "n" gnus-summary-next-thread
1798   "p" gnus-summary-prev-thread
1799   "u" gnus-summary-up-thread
1800   "o" gnus-summary-top-thread
1801   "d" gnus-summary-down-thread
1802   "#" gnus-uu-mark-thread
1803   "\M-#" gnus-uu-unmark-thread)
1804
1805 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1806   "g" gnus-summary-prepare
1807   "c" gnus-summary-insert-cached-articles
1808   "d" gnus-summary-insert-dormant-articles)
1809
1810 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1811   "c" gnus-summary-catchup-and-exit
1812   "C" gnus-summary-catchup-all-and-exit
1813   "E" gnus-summary-exit-no-update
1814   "J" gnus-summary-jump-to-other-group
1815   "Q" gnus-summary-exit
1816   "Z" gnus-summary-exit
1817   "n" gnus-summary-catchup-and-goto-next-group
1818   "R" gnus-summary-reselect-current-group
1819   "G" gnus-summary-rescan-group
1820   "N" gnus-summary-next-group
1821   "s" gnus-summary-save-newsrc
1822   "P" gnus-summary-prev-group)
1823
1824 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1825   " " gnus-summary-next-page
1826   "n" gnus-summary-next-page
1827   "\177" gnus-summary-prev-page
1828   [delete] gnus-summary-prev-page
1829   "p" gnus-summary-prev-page
1830   "\r" gnus-summary-scroll-up
1831   "\M-\r" gnus-summary-scroll-down
1832   "<" gnus-summary-beginning-of-article
1833   ">" gnus-summary-end-of-article
1834   "b" gnus-summary-beginning-of-article
1835   "e" gnus-summary-end-of-article
1836   "^" gnus-summary-refer-parent-article
1837   "r" gnus-summary-refer-parent-article
1838   "D" gnus-summary-enter-digest-group
1839   "R" gnus-summary-refer-references
1840   "T" gnus-summary-refer-thread
1841   "g" gnus-summary-show-article
1842   "s" gnus-summary-isearch-article
1843   "P" gnus-summary-print-article
1844   "M" gnus-mailing-list-insinuate
1845   "t" gnus-article-babel)
1846
1847 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1848   "b" gnus-article-add-buttons
1849   "B" gnus-article-add-buttons-to-head
1850   "o" gnus-article-treat-overstrike
1851   "e" gnus-article-emphasize
1852   "w" gnus-article-fill-cited-article
1853   "Q" gnus-article-fill-long-lines
1854   "C" gnus-article-capitalize-sentences
1855   "c" gnus-article-remove-cr
1856   "Z" gnus-article-decode-HZ
1857   "h" gnus-article-wash-html
1858   "u" gnus-article-unsplit-urls
1859   "f" gnus-article-display-x-face
1860   "l" gnus-summary-stop-page-breaking
1861   "r" gnus-summary-caesar-message
1862   "m" gnus-summary-morse-message
1863   "t" gnus-summary-toggle-header
1864   "g" gnus-treat-smiley
1865   "v" gnus-summary-verbose-headers
1866   "m" gnus-summary-toggle-mime
1867   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1868   "p" gnus-article-verify-x-pgp-sig
1869   "d" gnus-article-treat-dumbquotes)
1870
1871 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1872   ;; mnemonic: deuglif*Y*
1873   "u" gnus-article-outlook-unwrap-lines
1874   "a" gnus-article-outlook-repair-attribution
1875   "c" gnus-article-outlook-rearrange-citation
1876   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1877
1878 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1879   "a" gnus-article-hide
1880   "h" gnus-article-hide-headers
1881   "b" gnus-article-hide-boring-headers
1882   "s" gnus-article-hide-signature
1883   "c" gnus-article-hide-citation
1884   "C" gnus-article-hide-citation-in-followups
1885   "l" gnus-article-hide-list-identifiers
1886   "B" gnus-article-strip-banner
1887   "P" gnus-article-hide-pem
1888   "\C-c" gnus-article-hide-citation-maybe)
1889
1890 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1891   "a" gnus-article-highlight
1892   "h" gnus-article-highlight-headers
1893   "c" gnus-article-highlight-citation
1894   "s" gnus-article-highlight-signature)
1895
1896 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1897   "f" gnus-article-treat-fold-headers
1898   "u" gnus-article-treat-unfold-headers
1899   "n" gnus-article-treat-fold-newsgroups)
1900
1901 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1902   "x" gnus-article-display-x-face
1903   "d" gnus-article-display-face
1904   "s" gnus-treat-smiley
1905   "D" gnus-article-remove-images
1906   "f" gnus-treat-from-picon
1907   "m" gnus-treat-mail-picon
1908   "n" gnus-treat-newsgroups-picon)
1909
1910 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1911   "z" gnus-article-date-ut
1912   "u" gnus-article-date-ut
1913   "l" gnus-article-date-local
1914   "p" gnus-article-date-english
1915   "e" gnus-article-date-lapsed
1916   "o" gnus-article-date-original
1917   "i" gnus-article-date-iso8601
1918   "s" gnus-article-date-user)
1919
1920 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1921   "t" gnus-article-remove-trailing-blank-lines
1922   "l" gnus-article-strip-leading-blank-lines
1923   "m" gnus-article-strip-multiple-blank-lines
1924   "a" gnus-article-strip-blank-lines
1925   "A" gnus-article-strip-all-blank-lines
1926   "s" gnus-article-strip-leading-space
1927   "e" gnus-article-strip-trailing-space
1928   "w" gnus-article-remove-leading-whitespace)
1929
1930 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1931   "v" gnus-version
1932   "f" gnus-summary-fetch-faq
1933   "d" gnus-summary-describe-group
1934   "h" gnus-summary-describe-briefly
1935   "i" gnus-info-find-node
1936   "c" gnus-group-fetch-charter
1937   "C" gnus-group-fetch-control)
1938
1939 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1940   "e" gnus-summary-expire-articles
1941   "\M-\C-e" gnus-summary-expire-articles-now
1942   "\177" gnus-summary-delete-article
1943   [delete] gnus-summary-delete-article
1944   [backspace] gnus-summary-delete-article
1945   "m" gnus-summary-move-article
1946   "r" gnus-summary-respool-article
1947   "w" gnus-summary-edit-article
1948   "c" gnus-summary-copy-article
1949   "B" gnus-summary-crosspost-article
1950   "q" gnus-summary-respool-query
1951   "t" gnus-summary-respool-trace
1952   "i" gnus-summary-import-article
1953   "I" gnus-summary-create-article
1954   "p" gnus-summary-article-posted-p)
1955
1956 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1957   "o" gnus-summary-save-article
1958   "m" gnus-summary-save-article-mail
1959   "F" gnus-summary-write-article-file
1960   "r" gnus-summary-save-article-rmail
1961   "f" gnus-summary-save-article-file
1962   "b" gnus-summary-save-article-body-file
1963   "h" gnus-summary-save-article-folder
1964   "v" gnus-summary-save-article-vm
1965   "p" gnus-summary-pipe-output
1966   "P" gnus-summary-muttprint
1967   "s" gnus-soup-add-article)
1968
1969 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1970   "b" gnus-summary-display-buttonized
1971   "m" gnus-summary-repair-multipart
1972   "v" gnus-article-view-part
1973   "o" gnus-article-save-part
1974   "c" gnus-article-copy-part
1975   "C" gnus-article-view-part-as-charset
1976   "e" gnus-article-view-part-externally
1977   "E" gnus-article-encrypt-body
1978   "i" gnus-article-inline-part
1979   "|" gnus-article-pipe-part)
1980
1981 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1982   "p" gnus-summary-mark-as-processable
1983   "u" gnus-summary-unmark-as-processable
1984   "U" gnus-summary-unmark-all-processable
1985   "v" gnus-uu-mark-over
1986   "s" gnus-uu-mark-series
1987   "r" gnus-uu-mark-region
1988   "g" gnus-uu-unmark-region
1989   "R" gnus-uu-mark-by-regexp
1990   "G" gnus-uu-unmark-by-regexp
1991   "t" gnus-uu-mark-thread
1992   "T" gnus-uu-unmark-thread
1993   "a" gnus-uu-mark-all
1994   "b" gnus-uu-mark-buffer
1995   "S" gnus-uu-mark-sparse
1996   "k" gnus-summary-kill-process-mark
1997   "y" gnus-summary-yank-process-mark
1998   "w" gnus-summary-save-process-mark
1999   "i" gnus-uu-invert-processable)
2000
2001 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2002   ;;"x" gnus-uu-extract-any
2003   "m" gnus-summary-save-parts
2004   "u" gnus-uu-decode-uu
2005   "U" gnus-uu-decode-uu-and-save
2006   "s" gnus-uu-decode-unshar
2007   "S" gnus-uu-decode-unshar-and-save
2008   "o" gnus-uu-decode-save
2009   "O" gnus-uu-decode-save
2010   "b" gnus-uu-decode-binhex
2011   "B" gnus-uu-decode-binhex
2012   "p" gnus-uu-decode-postscript
2013   "P" gnus-uu-decode-postscript-and-save)
2014
2015 (gnus-define-keys
2016     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2017   "u" gnus-uu-decode-uu-view
2018   "U" gnus-uu-decode-uu-and-save-view
2019   "s" gnus-uu-decode-unshar-view
2020   "S" gnus-uu-decode-unshar-and-save-view
2021   "o" gnus-uu-decode-save-view
2022   "O" gnus-uu-decode-save-view
2023   "b" gnus-uu-decode-binhex-view
2024   "B" gnus-uu-decode-binhex-view
2025   "p" gnus-uu-decode-postscript-view
2026   "P" gnus-uu-decode-postscript-and-save-view)
2027
2028 (defvar gnus-article-post-menu nil)
2029
2030 (defconst gnus-summary-menu-maxlen 20)
2031
2032 (defun gnus-summary-menu-split (menu)
2033   ;; If we have lots of elements, divide them into groups of 20
2034   ;; and make a pane (or submenu) for each one.
2035   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2036       (let ((menu menu) sublists next
2037             (i 1))
2038         (while menu
2039           ;; Pull off the next gnus-summary-menu-maxlen elements
2040           ;; and make them the next element of sublist.
2041           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2042           (if next
2043               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2044                       nil))
2045           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2046                                              (aref (car (last menu)) 0)) menu)
2047                                sublists))
2048           (setq i (1+ i))
2049           (setq menu next))
2050         (nreverse sublists))
2051     ;; Few elements--put them all in one pane.
2052     menu))
2053
2054 (defun gnus-summary-make-menu-bar ()
2055   (gnus-turn-off-edit-menu 'summary)
2056
2057   (unless (boundp 'gnus-summary-misc-menu)
2058
2059     (easy-menu-define
2060      gnus-summary-kill-menu gnus-summary-mode-map ""
2061      (cons
2062       "Score"
2063       (nconc
2064        (list
2065         ["Customize" gnus-score-customize t])
2066        (gnus-make-score-map 'increase)
2067        (gnus-make-score-map 'lower)
2068        '(("Mark"
2069           ["Kill below" gnus-summary-kill-below t]
2070           ["Mark above" gnus-summary-mark-above t]
2071           ["Tick above" gnus-summary-tick-above t]
2072           ["Clear above" gnus-summary-clear-above t])
2073          ["Current score" gnus-summary-current-score t]
2074          ["Set score" gnus-summary-set-score t]
2075          ["Switch current score file..." gnus-score-change-score-file t]
2076          ["Set mark below..." gnus-score-set-mark-below t]
2077          ["Set expunge below..." gnus-score-set-expunge-below t]
2078          ["Edit current score file" gnus-score-edit-current-scores t]
2079          ["Edit score file" gnus-score-edit-file t]
2080          ["Trace score" gnus-score-find-trace t]
2081          ["Find words" gnus-score-find-favourite-words t]
2082          ["Rescore buffer" gnus-summary-rescore t]
2083          ["Increase score..." gnus-summary-increase-score t]
2084          ["Lower score..." gnus-summary-lower-score t]))))
2085
2086     ;; Define both the Article menu in the summary buffer and the
2087     ;; equivalent Commands menu in the article buffer here for
2088     ;; consistency.
2089     (let ((innards
2090            `(("Hide"
2091               ["All" gnus-article-hide t]
2092               ["Headers" gnus-article-hide-headers t]
2093               ["Signature" gnus-article-hide-signature t]
2094               ["Citation" gnus-article-hide-citation t]
2095               ["List identifiers" gnus-article-hide-list-identifiers t]
2096               ["Banner" gnus-article-strip-banner t]
2097               ["Boring headers" gnus-article-hide-boring-headers t])
2098              ("Highlight"
2099               ["All" gnus-article-highlight t]
2100               ["Headers" gnus-article-highlight-headers t]
2101               ["Signature" gnus-article-highlight-signature t]
2102               ["Citation" gnus-article-highlight-citation t])
2103              ("Date"
2104               ["Local" gnus-article-date-local t]
2105               ["ISO8601" gnus-article-date-iso8601 t]
2106               ["UT" gnus-article-date-ut t]
2107               ["Original" gnus-article-date-original t]
2108               ["Lapsed" gnus-article-date-lapsed t]
2109               ["User-defined" gnus-article-date-user t])
2110              ("Display"
2111               ["Remove images" gnus-article-remove-images t]
2112               ["Toggle smiley" gnus-treat-smiley t]
2113               ["Show X-Face" gnus-article-display-x-face t]
2114               ["Show picons in From" gnus-treat-from-picon t]
2115               ["Show picons in mail headers" gnus-treat-mail-picon t]
2116               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2117               ("View as different encoding"
2118                ,@(gnus-summary-menu-split
2119                   (mapcar
2120                    (lambda (cs)
2121                      ;; Since easymenu under Emacs doesn't allow
2122                      ;; lambda forms for menu commands, we should
2123                      ;; provide intern'ed function symbols.
2124                      (let ((command (intern (format "\
2125 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2126                        (fset command
2127                              `(lambda ()
2128                                 (interactive)
2129                                 (let ((gnus-summary-show-article-charset-alist
2130                                        '((1 . ,cs))))
2131                                   (gnus-summary-show-article 1))))
2132                        `[,(symbol-name cs) ,command t]))
2133                    (sort (if (fboundp 'coding-system-list)
2134                              (coding-system-list)
2135                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2136                            )
2137                          'string<)))))
2138              ("Washing"
2139               ("Remove Blanks"
2140                ["Leading" gnus-article-strip-leading-blank-lines t]
2141                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2142                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2143                ["All of the above" gnus-article-strip-blank-lines t]
2144                ["All" gnus-article-strip-all-blank-lines t]
2145                ["Leading space" gnus-article-strip-leading-space t]
2146                ["Trailing space" gnus-article-strip-trailing-space t]
2147                ["Leading space in headers"
2148                 gnus-article-remove-leading-whitespace t])
2149               ["Overstrike" gnus-article-treat-overstrike t]
2150               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2151               ["Emphasis" gnus-article-emphasize t]
2152               ["Word wrap" gnus-article-fill-cited-article t]
2153               ["Fill long lines" gnus-article-fill-long-lines t]
2154               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2155               ["CR" gnus-article-remove-cr t]
2156               ["Rot 13" gnus-summary-caesar-message
2157                ,@(if (featurep 'xemacs) '(t)
2158                    '(:help "\"Caesar rotate\" article by 13"))]
2159               ["Morse decode" gnus-summary-morse-message t]
2160               ["Unix pipe..." gnus-summary-pipe-message t]
2161               ["Add buttons" gnus-article-add-buttons t]
2162               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2163               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2164               ["Toggle MIME" gnus-summary-toggle-mime t]
2165               ["Verbose header" gnus-summary-verbose-headers t]
2166               ["Toggle header" gnus-summary-toggle-header t]
2167               ["Unfold headers" gnus-article-treat-unfold-headers t]
2168               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2169               ["Html" gnus-article-wash-html t]
2170               ["URLs" gnus-article-unsplit-urls t]
2171               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2172               ["HZ" gnus-article-decode-HZ t]
2173               ("(Outlook) Deuglify"
2174                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2175                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2176                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2177                ["Full (Outlook) deuglify"
2178                 gnus-article-outlook-deuglify-article t])
2179               )
2180              ("Output"
2181               ["Save in default format" gnus-summary-save-article
2182                ,@(if (featurep 'xemacs) '(t)
2183                    '(:help "Save article using default method"))]
2184               ["Save in file" gnus-summary-save-article-file
2185                ,@(if (featurep 'xemacs) '(t)
2186                    '(:help "Save article in file"))]
2187               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2188               ["Save in MH folder" gnus-summary-save-article-folder t]
2189               ["Save in VM folder" gnus-summary-save-article-vm t]
2190               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2191               ["Save body in file" gnus-summary-save-article-body-file t]
2192               ["Pipe through a filter" gnus-summary-pipe-output t]
2193               ["Add to SOUP packet" gnus-soup-add-article t]
2194               ["Print with Muttprint" gnus-summary-muttprint t]
2195               ["Print" gnus-summary-print-article t])
2196              ("Backend"
2197               ["Respool article..." gnus-summary-respool-article t]
2198               ["Move article..." gnus-summary-move-article
2199                (gnus-check-backend-function
2200                 'request-move-article gnus-newsgroup-name)]
2201               ["Copy article..." gnus-summary-copy-article t]
2202               ["Crosspost article..." gnus-summary-crosspost-article
2203                (gnus-check-backend-function
2204                 'request-replace-article gnus-newsgroup-name)]
2205               ["Import file..." gnus-summary-import-article
2206                (gnus-check-backend-function
2207                 'request-accept-article gnus-newsgroup-name)]
2208               ["Create article..." gnus-summary-create-article
2209                (gnus-check-backend-function
2210                 'request-accept-article gnus-newsgroup-name)]
2211               ["Check if posted" gnus-summary-article-posted-p t]
2212               ["Edit article" gnus-summary-edit-article
2213                (not (gnus-group-read-only-p))]
2214               ["Delete article" gnus-summary-delete-article
2215                (gnus-check-backend-function
2216                 'request-expire-articles gnus-newsgroup-name)]
2217               ["Query respool" gnus-summary-respool-query t]
2218               ["Trace respool" gnus-summary-respool-trace t]
2219               ["Delete expirable articles" gnus-summary-expire-articles-now
2220                (gnus-check-backend-function
2221                 'request-expire-articles gnus-newsgroup-name)])
2222              ("Extract"
2223               ["Uudecode" gnus-uu-decode-uu
2224                ,@(if (featurep 'xemacs) '(t)
2225                    '(:help "Decode uuencoded article(s)"))]
2226               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2227               ["Unshar" gnus-uu-decode-unshar t]
2228               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2229               ["Save" gnus-uu-decode-save t]
2230               ["Binhex" gnus-uu-decode-binhex t]
2231               ["Postscript" gnus-uu-decode-postscript t])
2232              ("Cache"
2233               ["Enter article" gnus-cache-enter-article t]
2234               ["Remove article" gnus-cache-remove-article t])
2235              ["Translate" gnus-article-babel t]
2236              ["Select article buffer" gnus-summary-select-article-buffer t]
2237              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2238              ["Isearch article..." gnus-summary-isearch-article t]
2239              ["Beginning of the article" gnus-summary-beginning-of-article t]
2240              ["End of the article" gnus-summary-end-of-article t]
2241              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2242              ["Fetch referenced articles" gnus-summary-refer-references t]
2243              ["Fetch current thread" gnus-summary-refer-thread t]
2244              ["Fetch article with id..." gnus-summary-refer-article t]
2245              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2246              ["Redisplay" gnus-summary-show-article t]
2247              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2248       (easy-menu-define
2249        gnus-summary-article-menu gnus-summary-mode-map ""
2250        (cons "Article" innards))
2251
2252       (if (not (keymapp gnus-summary-article-menu))
2253           (easy-menu-define
2254            gnus-article-commands-menu gnus-article-mode-map ""
2255            (cons "Commands" innards))
2256         ;; in Emacs, don't share menu.
2257         (setq gnus-article-commands-menu
2258               (copy-keymap gnus-summary-article-menu))
2259         (define-key gnus-article-mode-map [menu-bar commands]
2260           (cons "Commands" gnus-article-commands-menu))))
2261
2262     (easy-menu-define
2263      gnus-summary-thread-menu gnus-summary-mode-map ""
2264      '("Threads"
2265        ["Find all messages in thread" gnus-summary-refer-thread t]
2266        ["Toggle threading" gnus-summary-toggle-threads t]
2267        ["Hide threads" gnus-summary-hide-all-threads t]
2268        ["Show threads" gnus-summary-show-all-threads t]
2269        ["Hide thread" gnus-summary-hide-thread t]
2270        ["Show thread" gnus-summary-show-thread t]
2271        ["Go to next thread" gnus-summary-next-thread t]
2272        ["Go to previous thread" gnus-summary-prev-thread t]
2273        ["Go down thread" gnus-summary-down-thread t]
2274        ["Go up thread" gnus-summary-up-thread t]
2275        ["Top of thread" gnus-summary-top-thread t]
2276        ["Mark thread as read" gnus-summary-kill-thread t]
2277        ["Lower thread score" gnus-summary-lower-thread t]
2278        ["Raise thread score" gnus-summary-raise-thread t]
2279        ["Rethread current" gnus-summary-rethread-current t]))
2280
2281     (easy-menu-define
2282      gnus-summary-post-menu gnus-summary-mode-map ""
2283      `("Post"
2284        ["Send a message (mail or news)" gnus-summary-post-news
2285         ,@(if (featurep 'xemacs) '(t)
2286             '(:help "Post an article"))]
2287        ["Followup" gnus-summary-followup
2288         ,@(if (featurep 'xemacs) '(t)
2289             '(:help "Post followup to this article"))]
2290        ["Followup and yank" gnus-summary-followup-with-original
2291         ,@(if (featurep 'xemacs) '(t)
2292             '(:help "Post followup to this article, quoting its contents"))]
2293        ["Supersede article" gnus-summary-supersede-article t]
2294        ["Cancel article" gnus-summary-cancel-article
2295         ,@(if (featurep 'xemacs) '(t)
2296             '(:help "Cancel an article you posted"))]
2297        ["Reply" gnus-summary-reply t]
2298        ["Reply and yank" gnus-summary-reply-with-original t]
2299        ["Wide reply" gnus-summary-wide-reply t]
2300        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2301         ,@(if (featurep 'xemacs) '(t)
2302             '(:help "Mail a reply, quoting this article"))]
2303        ["Very wide reply" gnus-summary-very-wide-reply t]
2304        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2305         ,@(if (featurep 'xemacs) '(t)
2306             '(:help "Mail a very wide reply, quoting this article"))]
2307        ["Mail forward" gnus-summary-mail-forward t]
2308        ["Post forward" gnus-summary-post-forward t]
2309        ["Digest and mail" gnus-summary-digest-mail-forward t]
2310        ["Digest and post" gnus-summary-digest-post-forward t]
2311        ["Resend message" gnus-summary-resend-message t]
2312        ["Resend message edit" gnus-summary-resend-message-edit t]
2313        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2314        ["Send a mail" gnus-summary-mail-other-window t]
2315        ["Create a local message" gnus-summary-news-other-window t]
2316        ["Uuencode and post" gnus-uu-post-news
2317         ,@(if (featurep 'xemacs) '(t)
2318             '(:help "Post a uuencoded article"))]
2319        ["Followup via news" gnus-summary-followup-to-mail t]
2320        ["Followup via news and yank"
2321         gnus-summary-followup-to-mail-with-original t]
2322        ;;("Draft"
2323        ;;["Send" gnus-summary-send-draft t]
2324        ;;["Send bounced" gnus-resend-bounced-mail t])
2325        ))
2326
2327     (cond
2328      ((not (keymapp gnus-summary-post-menu))
2329       (setq gnus-article-post-menu gnus-summary-post-menu))
2330      ((not gnus-article-post-menu)
2331       ;; Don't share post menu.
2332       (setq gnus-article-post-menu
2333             (copy-keymap gnus-summary-post-menu))))
2334     (define-key gnus-article-mode-map [menu-bar post]
2335       (cons "Post" gnus-article-post-menu))
2336
2337     (easy-menu-define
2338      gnus-summary-misc-menu gnus-summary-mode-map ""
2339      `("Gnus"
2340        ("Mark Read"
2341         ["Mark as read" gnus-summary-mark-as-read-forward t]
2342         ["Mark same subject and select"
2343          gnus-summary-kill-same-subject-and-select t]
2344         ["Mark same subject" gnus-summary-kill-same-subject t]
2345         ["Catchup" gnus-summary-catchup
2346          ,@(if (featurep 'xemacs) '(t)
2347              '(:help "Mark unread articles in this group as read"))]
2348         ["Catchup all" gnus-summary-catchup-all t]
2349         ["Catchup to here" gnus-summary-catchup-to-here t]
2350         ["Catchup from here" gnus-summary-catchup-from-here t]
2351         ["Catchup region" gnus-summary-mark-region-as-read
2352          (gnus-mark-active-p)]
2353         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2354        ("Mark Various"
2355         ["Tick" gnus-summary-tick-article-forward t]
2356         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2357         ["Remove marks" gnus-summary-clear-mark-forward t]
2358         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2359         ["Set bookmark" gnus-summary-set-bookmark t]
2360         ["Remove bookmark" gnus-summary-remove-bookmark t])
2361        ("Limit to"
2362         ["Marks..." gnus-summary-limit-to-marks t]
2363         ["Subject..." gnus-summary-limit-to-subject t]
2364         ["Author..." gnus-summary-limit-to-author t]
2365         ["Age..." gnus-summary-limit-to-age t]
2366         ["Extra..." gnus-summary-limit-to-extra t]
2367         ["Score..." gnus-summary-limit-to-score t]
2368         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2369         ["Unread" gnus-summary-limit-to-unread t]
2370         ["Unseen" gnus-summary-limit-to-unseen t]
2371         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2372         ["Next articles" gnus-summary-limit-to-articles t]
2373         ["Pop limit" gnus-summary-pop-limit t]
2374         ["Show dormant" gnus-summary-limit-include-dormant t]
2375         ["Hide childless dormant"
2376          gnus-summary-limit-exclude-childless-dormant t]
2377         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2378         ["Hide marked" gnus-summary-limit-exclude-marks t]
2379         ["Show expunged" gnus-summary-limit-include-expunged t])
2380        ("Process Mark"
2381         ["Set mark" gnus-summary-mark-as-processable t]
2382         ["Remove mark" gnus-summary-unmark-as-processable t]
2383         ["Remove all marks" gnus-summary-unmark-all-processable t]
2384         ["Mark above" gnus-uu-mark-over t]
2385         ["Mark series" gnus-uu-mark-series t]
2386         ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2387         ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2388         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2389         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2390         ["Mark all" gnus-uu-mark-all t]
2391         ["Mark buffer" gnus-uu-mark-buffer t]
2392         ["Mark sparse" gnus-uu-mark-sparse t]
2393         ["Mark thread" gnus-uu-mark-thread t]
2394         ["Unmark thread" gnus-uu-unmark-thread t]
2395         ("Process Mark Sets"
2396          ["Kill" gnus-summary-kill-process-mark t]
2397          ["Yank" gnus-summary-yank-process-mark
2398           gnus-newsgroup-process-stack]
2399          ["Save" gnus-summary-save-process-mark t]
2400          ["Run command on marked..." gnus-summary-universal-argument t]))
2401        ("Scroll article"
2402         ["Page forward" gnus-summary-next-page
2403          ,@(if (featurep 'xemacs) '(t)
2404              '(:help "Show next page of article"))]
2405         ["Page backward" gnus-summary-prev-page
2406          ,@(if (featurep 'xemacs) '(t)
2407              '(:help "Show previous page of article"))]
2408         ["Line forward" gnus-summary-scroll-up t])
2409        ("Move"
2410         ["Next unread article" gnus-summary-next-unread-article t]
2411         ["Previous unread article" gnus-summary-prev-unread-article t]
2412         ["Next article" gnus-summary-next-article t]
2413         ["Previous article" gnus-summary-prev-article t]
2414         ["Next unread subject" gnus-summary-next-unread-subject t]
2415         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2416         ["Next article same subject" gnus-summary-next-same-subject t]
2417         ["Previous article same subject" gnus-summary-prev-same-subject t]
2418         ["First unread article" gnus-summary-first-unread-article t]
2419         ["Best unread article" gnus-summary-best-unread-article t]
2420         ["Go to subject number..." gnus-summary-goto-subject t]
2421         ["Go to article number..." gnus-summary-goto-article t]
2422         ["Go to the last article" gnus-summary-goto-last-article t]
2423         ["Pop article off history" gnus-summary-pop-article t])
2424        ("Sort"
2425         ["Sort by number" gnus-summary-sort-by-number t]
2426         ["Sort by author" gnus-summary-sort-by-author t]
2427         ["Sort by subject" gnus-summary-sort-by-subject t]
2428         ["Sort by date" gnus-summary-sort-by-date t]
2429         ["Sort by score" gnus-summary-sort-by-score t]
2430         ["Sort by lines" gnus-summary-sort-by-lines t]
2431         ["Sort by characters" gnus-summary-sort-by-chars t]
2432         ["Randomize" gnus-summary-sort-by-random t]
2433         ["Original sort" gnus-summary-sort-by-original t])
2434        ("Help"
2435         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2436         ["Describe group" gnus-summary-describe-group t]
2437         ["Fetch charter" gnus-group-fetch-charter
2438          ,@(if (featurep 'xemacs) nil
2439              '(:help "Display the charter of the current group"))]
2440         ["Fetch control message" gnus-group-fetch-control
2441          ,@(if (featurep 'xemacs) nil
2442              '(:help "Display the archived control message for the current group"))]
2443         ["Read manual" gnus-info-find-node t])
2444        ("Modes"
2445         ["Pick and read" gnus-pick-mode t]
2446         ["Binary" gnus-binary-mode t])
2447        ("Regeneration"
2448         ["Regenerate" gnus-summary-prepare t]
2449         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2450         ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2451         ["Toggle threading" gnus-summary-toggle-threads t])
2452        ["See old articles" gnus-summary-insert-old-articles t]
2453        ["See new articles" gnus-summary-insert-new-articles t]
2454        ["Filter articles..." gnus-summary-execute-command t]
2455        ["Run command on articles..." gnus-summary-universal-argument t]
2456        ["Search articles forward..." gnus-summary-search-article-forward t]
2457        ["Search articles backward..." gnus-summary-search-article-backward t]
2458        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2459        ["Expand window" gnus-summary-expand-window t]
2460        ["Expire expirable articles" gnus-summary-expire-articles
2461         (gnus-check-backend-function
2462          'request-expire-articles gnus-newsgroup-name)]
2463        ["Edit local kill file" gnus-summary-edit-local-kill t]
2464        ["Edit main kill file" gnus-summary-edit-global-kill t]
2465        ["Edit group parameters" gnus-summary-edit-parameters t]
2466        ["Customize group parameters" gnus-summary-customize-parameters t]
2467        ["Send a bug report" gnus-bug t]
2468        ("Exit"
2469         ["Catchup and exit" gnus-summary-catchup-and-exit
2470          ,@(if (featurep 'xemacs) '(t)
2471              '(:help "Mark unread articles in this group as read, then exit"))]
2472         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2473         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2474         ["Exit group" gnus-summary-exit
2475          ,@(if (featurep 'xemacs) '(t)
2476              '(:help "Exit current group, return to group selection mode"))]
2477         ["Exit group without updating" gnus-summary-exit-no-update t]
2478         ["Exit and goto next group" gnus-summary-next-group t]
2479         ["Exit and goto prev group" gnus-summary-prev-group t]
2480         ["Reselect group" gnus-summary-reselect-current-group t]
2481         ["Rescan group" gnus-summary-rescan-group t]
2482         ["Update dribble" gnus-summary-save-newsrc t])))
2483
2484     (gnus-run-hooks 'gnus-summary-menu-hook)))
2485
2486 (defvar gnus-summary-tool-bar-map nil)
2487
2488 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2489 (defun gnus-summary-make-tool-bar ()
2490   (if (and (fboundp 'tool-bar-add-item-from-menu)
2491            (default-value 'tool-bar-mode)
2492            (not gnus-summary-tool-bar-map))
2493       (setq gnus-summary-tool-bar-map
2494             (let ((tool-bar-map (make-sparse-keymap))
2495                   (load-path (mm-image-load-path)))
2496               (tool-bar-add-item-from-menu
2497                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2498               (tool-bar-add-item-from-menu
2499                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2500               (tool-bar-add-item-from-menu
2501                'gnus-summary-post-news "post" gnus-summary-mode-map)
2502               (tool-bar-add-item-from-menu
2503                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2504               (tool-bar-add-item-from-menu
2505                'gnus-summary-followup "followup" gnus-summary-mode-map)
2506               (tool-bar-add-item-from-menu
2507                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2508               (tool-bar-add-item-from-menu
2509                'gnus-summary-reply "reply" gnus-summary-mode-map)
2510               (tool-bar-add-item-from-menu
2511                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2512               (tool-bar-add-item-from-menu
2513                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2514               (tool-bar-add-item-from-menu
2515                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2516               (tool-bar-add-item-from-menu
2517                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2518               (tool-bar-add-item-from-menu
2519                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2520               (tool-bar-add-item-from-menu
2521                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2522               (tool-bar-add-item-from-menu
2523                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2524               (tool-bar-add-item-from-menu
2525                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2526               tool-bar-map)))
2527   (if gnus-summary-tool-bar-map
2528       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2529
2530 (defun gnus-score-set-default (var value)
2531   "A version of set that updates the GNU Emacs menu-bar."
2532   (set var value)
2533   ;; It is the message that forces the active status to be updated.
2534   (message ""))
2535
2536 (defun gnus-make-score-map (type)
2537   "Make a summary score map of type TYPE."
2538   (if t
2539       nil
2540     (let ((headers '(("author" "from" string)
2541                      ("subject" "subject" string)
2542                      ("article body" "body" string)
2543                      ("article head" "head" string)
2544                      ("xref" "xref" string)
2545                      ("extra header" "extra" string)
2546                      ("lines" "lines" number)
2547                      ("followups to author" "followup" string)))
2548           (types '((number ("less than" <)
2549                            ("greater than" >)
2550                            ("equal" =))
2551                    (string ("substring" s)
2552                            ("exact string" e)
2553                            ("fuzzy string" f)
2554                            ("regexp" r))))
2555           (perms '(("temporary" (current-time-string))
2556                    ("permanent" nil)
2557                    ("immediate" now)))
2558           header)
2559       (list
2560        (apply
2561         'nconc
2562         (list
2563          (if (eq type 'lower)
2564              "Lower score"
2565            "Increase score"))
2566         (let (outh)
2567           (while headers
2568             (setq header (car headers))
2569             (setq outh
2570                   (cons
2571                    (apply
2572                     'nconc
2573                     (list (car header))
2574                     (let ((ts (cdr (assoc (nth 2 header) types)))
2575                           outt)
2576                       (while ts
2577                         (setq outt
2578                               (cons
2579                                (apply
2580                                 'nconc
2581                                 (list (caar ts))
2582                                 (let ((ps perms)
2583                                       outp)
2584                                   (while ps
2585                                     (setq outp
2586                                           (cons
2587                                            (vector
2588                                             (caar ps)
2589                                             (list
2590                                              'gnus-summary-score-entry
2591                                              (nth 1 header)
2592                                              (if (or (string= (nth 1 header)
2593                                                               "head")
2594                                                      (string= (nth 1 header)
2595                                                               "body"))
2596                                                  ""
2597                                                (list 'gnus-summary-header
2598                                                      (nth 1 header)))
2599                                              (list 'quote (nth 1 (car ts)))
2600                                              (list 'gnus-score-delta-default
2601                                                    nil)
2602                                              (nth 1 (car ps))
2603                                              t)
2604                                             t)
2605                                            outp))
2606                                     (setq ps (cdr ps)))
2607                                   (list (nreverse outp))))
2608                                outt))
2609                         (setq ts (cdr ts)))
2610                       (list (nreverse outt))))
2611                    outh))
2612             (setq headers (cdr headers)))
2613           (list (nreverse outh))))))))
2614
2615 \f
2616
2617 (defun gnus-summary-mode (&optional group)
2618   "Major mode for reading articles.
2619
2620 All normal editing commands are switched off.
2621 \\<gnus-summary-mode-map>
2622 Each line in this buffer represents one article.  To read an
2623 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2624 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2625 respectively.
2626
2627 You can also post articles and send mail from this buffer.  To
2628 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2629 of an article, type `\\[gnus-summary-reply]'.
2630
2631 There are approx. one gazillion commands you can execute in this
2632 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2633
2634 The following commands are available:
2635
2636 \\{gnus-summary-mode-map}"
2637   (interactive)
2638   (kill-all-local-variables)
2639   (when (gnus-visual-p 'summary-menu 'menu)
2640     (gnus-summary-make-menu-bar)
2641     (gnus-summary-make-tool-bar))
2642   (gnus-summary-make-local-variables)
2643   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2644     (gnus-summary-make-local-variables))
2645   (gnus-make-thread-indent-array)
2646   (gnus-simplify-mode-line)
2647   (setq major-mode 'gnus-summary-mode)
2648   (setq mode-name "Summary")
2649   (make-local-variable 'minor-mode-alist)
2650   (use-local-map gnus-summary-mode-map)
2651   (buffer-disable-undo)
2652   (setq buffer-read-only t)             ;Disable modification
2653   (setq truncate-lines t)
2654   (setq selective-display t)
2655   (setq selective-display-ellipses t)   ;Display `...'
2656   (gnus-summary-set-display-table)
2657   (gnus-set-default-directory)
2658   (setq gnus-newsgroup-name group)
2659   (unless (gnus-news-group-p group)
2660     (setq gnus-newsgroup-incorporated
2661           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2662   (make-local-variable 'gnus-summary-line-format)
2663   (make-local-variable 'gnus-summary-line-format-spec)
2664   (make-local-variable 'gnus-summary-dummy-line-format)
2665   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2666   (make-local-variable 'gnus-summary-mark-positions)
2667   (gnus-make-local-hook 'pre-command-hook)
2668   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2669   (gnus-run-hooks 'gnus-summary-mode-hook)
2670   (turn-on-gnus-mailing-list-mode)
2671   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2672   (gnus-update-summary-mark-positions))
2673
2674 (defun gnus-summary-make-local-variables ()
2675   "Make all the local summary buffer variables."
2676   (let (global)
2677     (dolist (local gnus-summary-local-variables)
2678       (if (consp local)
2679           (progn
2680             (if (eq (cdr local) 'global)
2681                 ;; Copy the global value of the variable.
2682                 (setq global (symbol-value (car local)))
2683               ;; Use the value from the list.
2684               (setq global (eval (cdr local))))
2685             (set (make-local-variable (car local)) global))
2686         ;; Simple nil-valued local variable.
2687         (set (make-local-variable local) nil)))))
2688
2689 (defun gnus-summary-clear-local-variables ()
2690   (let ((locals gnus-summary-local-variables))
2691     (while locals
2692       (if (consp (car locals))
2693           (and (vectorp (caar locals))
2694                (set (caar locals) nil))
2695         (and (vectorp (car locals))
2696              (set (car locals) nil)))
2697       (setq locals (cdr locals)))))
2698
2699 ;; Summary data functions.
2700
2701 (defmacro gnus-data-number (data)
2702   `(car ,data))
2703
2704 (defmacro gnus-data-set-number (data number)
2705   `(setcar ,data ,number))
2706
2707 (defmacro gnus-data-mark (data)
2708   `(nth 1 ,data))
2709
2710 (defmacro gnus-data-set-mark (data mark)
2711   `(setcar (nthcdr 1 ,data) ,mark))
2712
2713 (defmacro gnus-data-pos (data)
2714   `(nth 2 ,data))
2715
2716 (defmacro gnus-data-set-pos (data pos)
2717   `(setcar (nthcdr 2 ,data) ,pos))
2718
2719 (defmacro gnus-data-header (data)
2720   `(nth 3 ,data))
2721
2722 (defmacro gnus-data-set-header (data header)
2723   `(setcar (nthcdr 3 ,data) ,header))
2724
2725 (defmacro gnus-data-level (data)
2726   `(nth 4 ,data))
2727
2728 (defmacro gnus-data-unread-p (data)
2729   `(= (nth 1 ,data) gnus-unread-mark))
2730
2731 (defmacro gnus-data-read-p (data)
2732   `(/= (nth 1 ,data) gnus-unread-mark))
2733
2734 (defmacro gnus-data-pseudo-p (data)
2735   `(consp (nth 3 ,data)))
2736
2737 (defmacro gnus-data-find (number)
2738   `(assq ,number gnus-newsgroup-data))
2739
2740 (defmacro gnus-data-find-list (number &optional data)
2741   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2742      (memq (assq ,number bdata)
2743            bdata)))
2744
2745 (defmacro gnus-data-make (number mark pos header level)
2746   `(list ,number ,mark ,pos ,header ,level))
2747
2748 (defun gnus-data-enter (after-article number mark pos header level offset)
2749   (let ((data (gnus-data-find-list after-article)))
2750     (unless data
2751       (error "No such article: %d" after-article))
2752     (setcdr data (cons (gnus-data-make number mark pos header level)
2753                        (cdr data)))
2754     (setq gnus-newsgroup-data-reverse nil)
2755     (gnus-data-update-list (cddr data) offset)))
2756
2757 (defun gnus-data-enter-list (after-article list &optional offset)
2758   (when list
2759     (let ((data (and after-article (gnus-data-find-list after-article)))
2760           (ilist list))
2761       (if (not (or data
2762                    after-article))
2763           (let ((odata gnus-newsgroup-data))
2764             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2765             (when offset
2766               (gnus-data-update-list odata offset)))
2767         ;; Find the last element in the list to be spliced into the main
2768         ;; list.
2769         (while (cdr list)
2770           (setq list (cdr list)))
2771         (if (not data)
2772             (progn
2773               (setcdr list gnus-newsgroup-data)
2774               (setq gnus-newsgroup-data ilist)
2775               (when offset
2776                 (gnus-data-update-list (cdr list) offset)))
2777           (setcdr list (cdr data))
2778           (setcdr data ilist)
2779           (when offset
2780             (gnus-data-update-list (cdr list) offset))))
2781       (setq gnus-newsgroup-data-reverse nil))))
2782
2783 (defun gnus-data-remove (article &optional offset)
2784   (let ((data gnus-newsgroup-data))
2785     (if (= (gnus-data-number (car data)) article)
2786         (progn
2787           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2788                 gnus-newsgroup-data-reverse nil)
2789           (when offset
2790             (gnus-data-update-list gnus-newsgroup-data offset)))
2791       (while (cdr data)
2792         (when (= (gnus-data-number (cadr data)) article)
2793           (setcdr data (cddr data))
2794           (when offset
2795             (gnus-data-update-list (cdr data) offset))
2796           (setq data nil
2797                 gnus-newsgroup-data-reverse nil))
2798         (setq data (cdr data))))))
2799
2800 (defmacro gnus-data-list (backward)
2801   `(if ,backward
2802        (or gnus-newsgroup-data-reverse
2803            (setq gnus-newsgroup-data-reverse
2804                  (reverse gnus-newsgroup-data)))
2805      gnus-newsgroup-data))
2806
2807 (defun gnus-data-update-list (data offset)
2808   "Add OFFSET to the POS of all data entries in DATA."
2809   (setq gnus-newsgroup-data-reverse nil)
2810   (while data
2811     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2812     (setq data (cdr data))))
2813
2814 (defun gnus-summary-article-pseudo-p (article)
2815   "Say whether this article is a pseudo article or not."
2816   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2817
2818 (defmacro gnus-summary-article-sparse-p (article)
2819   "Say whether this article is a sparse article or not."
2820   `(memq ,article gnus-newsgroup-sparse))
2821
2822 (defmacro gnus-summary-article-ancient-p (article)
2823   "Say whether this article is a sparse article or not."
2824   `(memq ,article gnus-newsgroup-ancient))
2825
2826 (defun gnus-article-parent-p (number)
2827   "Say whether this article is a parent or not."
2828   (let ((data (gnus-data-find-list number)))
2829     (and (cdr data)                     ; There has to be an article after...
2830          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2831             (gnus-data-level (nth 1 data))))))
2832
2833 (defun gnus-article-children (number)
2834   "Return a list of all children to NUMBER."
2835   (let* ((data (gnus-data-find-list number))
2836          (level (gnus-data-level (car data)))
2837          children)
2838     (setq data (cdr data))
2839     (while (and data
2840                 (= (gnus-data-level (car data)) (1+ level)))
2841       (push (gnus-data-number (car data)) children)
2842       (setq data (cdr data)))
2843     children))
2844
2845 (defmacro gnus-summary-skip-intangible ()
2846   "If the current article is intangible, then jump to a different article."
2847   '(let ((to (get-text-property (point) 'gnus-intangible)))
2848      (and to (gnus-summary-goto-subject to))))
2849
2850 (defmacro gnus-summary-article-intangible-p ()
2851   "Say whether this article is intangible or not."
2852   '(get-text-property (point) 'gnus-intangible))
2853
2854 (defun gnus-article-read-p (article)
2855   "Say whether ARTICLE is read or not."
2856   (not (or (memq article gnus-newsgroup-marked)
2857            (memq article gnus-newsgroup-spam-marked)
2858            (memq article gnus-newsgroup-unreads)
2859            (memq article gnus-newsgroup-unselected)
2860            (memq article gnus-newsgroup-dormant))))
2861
2862 ;; Some summary mode macros.
2863
2864 (defmacro gnus-summary-article-number ()
2865   "The article number of the article on the current line.
2866 If there isn't an article number here, then we return the current
2867 article number."
2868   '(progn
2869      (gnus-summary-skip-intangible)
2870      (or (get-text-property (point) 'gnus-number)
2871          (gnus-summary-last-subject))))
2872
2873 (defmacro gnus-summary-article-header (&optional number)
2874   "Return the header of article NUMBER."
2875   `(gnus-data-header (gnus-data-find
2876                       ,(or number '(gnus-summary-article-number)))))
2877
2878 (defmacro gnus-summary-thread-level (&optional number)
2879   "Return the level of thread that starts with article NUMBER."
2880   `(if (and (eq gnus-summary-make-false-root 'dummy)
2881             (get-text-property (point) 'gnus-intangible))
2882        0
2883      (gnus-data-level (gnus-data-find
2884                        ,(or number '(gnus-summary-article-number))))))
2885
2886 (defmacro gnus-summary-article-mark (&optional number)
2887   "Return the mark of article NUMBER."
2888   `(gnus-data-mark (gnus-data-find
2889                     ,(or number '(gnus-summary-article-number)))))
2890
2891 (defmacro gnus-summary-article-pos (&optional number)
2892   "Return the position of the line of article NUMBER."
2893   `(gnus-data-pos (gnus-data-find
2894                    ,(or number '(gnus-summary-article-number)))))
2895
2896 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2897 (defmacro gnus-summary-article-subject (&optional number)
2898   "Return current subject string or nil if nothing."
2899   `(let ((headers
2900           ,(if number
2901                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2902              '(gnus-data-header (assq (gnus-summary-article-number)
2903                                       gnus-newsgroup-data)))))
2904      (and headers
2905           (vectorp headers)
2906           (mail-header-subject headers))))
2907
2908 (defmacro gnus-summary-article-score (&optional number)
2909   "Return current article score."
2910   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2911                   gnus-newsgroup-scored))
2912        gnus-summary-default-score 0))
2913
2914 (defun gnus-summary-article-children (&optional number)
2915   "Return a list of article numbers that are children of article NUMBER."
2916   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2917          (level (gnus-data-level (car data)))
2918          l children)
2919     (while (and (setq data (cdr data))
2920                 (> (setq l (gnus-data-level (car data))) level))
2921       (and (= (1+ level) l)
2922            (push (gnus-data-number (car data))
2923                  children)))
2924     (nreverse children)))
2925
2926 (defun gnus-summary-article-parent (&optional number)
2927   "Return the article number of the parent of article NUMBER."
2928   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2929                                     (gnus-data-list t)))
2930          (level (gnus-data-level (car data))))
2931     (if (zerop level)
2932         ()                              ; This is a root.
2933       ;; We search until we find an article with a level less than
2934       ;; this one.  That function has to be the parent.
2935       (while (and (setq data (cdr data))
2936                   (not (< (gnus-data-level (car data)) level))))
2937       (and data (gnus-data-number (car data))))))
2938
2939 (defun gnus-unread-mark-p (mark)
2940   "Say whether MARK is the unread mark."
2941   (= mark gnus-unread-mark))
2942
2943 (defun gnus-read-mark-p (mark)
2944   "Say whether MARK is one of the marks that mark as read.
2945 This is all marks except unread, ticked, dormant, and expirable."
2946   (not (or (= mark gnus-unread-mark)
2947            (= mark gnus-ticked-mark)
2948            (= mark gnus-spam-mark)
2949            (= mark gnus-dormant-mark)
2950            (= mark gnus-expirable-mark))))
2951
2952 (defmacro gnus-article-mark (number)
2953   "Return the MARK of article NUMBER.
2954 This macro should only be used when computing the mark the \"first\"
2955 time; i.e., when generating the summary lines.  After that,
2956 `gnus-summary-article-mark' should be used to examine the
2957 marks of articles."
2958   `(cond
2959     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2960     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2961     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2962     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2963     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2964     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2965     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2966     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2967            gnus-ancient-mark))))
2968
2969 ;; Saving hidden threads.
2970
2971 (defmacro gnus-save-hidden-threads (&rest forms)
2972   "Save hidden threads, eval FORMS, and restore the hidden threads."
2973   (let ((config (make-symbol "config")))
2974     `(let ((,config (gnus-hidden-threads-configuration)))
2975        (unwind-protect
2976            (save-excursion
2977              ,@forms)
2978          (gnus-restore-hidden-threads-configuration ,config)))))
2979 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2980 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2981
2982 (defun gnus-data-compute-positions ()
2983   "Compute the positions of all articles."
2984   (setq gnus-newsgroup-data-reverse nil)
2985   (let ((data gnus-newsgroup-data))
2986     (save-excursion
2987       (gnus-save-hidden-threads
2988         (gnus-summary-show-all-threads)
2989         (goto-char (point-min))
2990         (while data
2991           (while (get-text-property (point) 'gnus-intangible)
2992             (forward-line 1))
2993           (gnus-data-set-pos (car data) (+ (point) 3))
2994           (setq data (cdr data))
2995           (forward-line 1))))))
2996
2997 (defun gnus-hidden-threads-configuration ()
2998   "Return the current hidden threads configuration."
2999   (save-excursion
3000     (let (config)
3001       (goto-char (point-min))
3002       (while (search-forward "\r" nil t)
3003         (push (1- (point)) config))
3004       config)))
3005
3006 (defun gnus-restore-hidden-threads-configuration (config)
3007   "Restore hidden threads configuration from CONFIG."
3008   (save-excursion
3009     (let (point buffer-read-only)
3010       (while (setq point (pop config))
3011         (when (and (< point (point-max))
3012                    (goto-char point)
3013                    (eq (char-after) ?\n))
3014           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3015
3016 ;; Various summary mode internalish functions.
3017
3018 (defun gnus-mouse-pick-article (e)
3019   (interactive "e")
3020   (mouse-set-point e)
3021   (gnus-summary-next-page nil t))
3022
3023 (defun gnus-summary-set-display-table ()
3024   "Change the display table.
3025 Odd characters have a tendency to mess
3026 up nicely formatted displays - we make all possible glyphs
3027 display only a single character."
3028
3029   ;; We start from the standard display table, if any.
3030   (let ((table (or (copy-sequence standard-display-table)
3031                    (make-display-table)))
3032         (i 32))
3033     ;; Nix out all the control chars...
3034     (while (>= (setq i (1- i)) 0)
3035       (aset table i [??]))
3036     ;; ... but not newline and cr, of course.  (cr is necessary for the
3037     ;; selective display).
3038     (aset table ?\n nil)
3039     (aset table ?\r nil)
3040     ;; We keep TAB as well.
3041     (aset table ?\t nil)
3042     ;; We nix out any glyphs over 126 that are not set already.
3043     (let ((i 256))
3044       (while (>= (setq i (1- i)) 127)
3045         ;; Only modify if the entry is nil.
3046         (unless (aref table i)
3047           (aset table i [??]))))
3048     (setq buffer-display-table table)))
3049
3050 (defun gnus-summary-set-article-display-arrow (pos)
3051   "Update the overlay arrow to point to line at position POS."
3052   (when (and gnus-summary-display-arrow
3053              (boundp 'overlay-arrow-position)
3054              (boundp 'overlay-arrow-string))
3055     (save-excursion
3056       (goto-char pos)
3057       (beginning-of-line)
3058       (unless overlay-arrow-position
3059         (setq overlay-arrow-position (make-marker)))
3060       (setq overlay-arrow-string "=>"
3061             overlay-arrow-position (set-marker overlay-arrow-position
3062                                                (point)
3063                                                (current-buffer))))))
3064
3065 (defun gnus-summary-setup-buffer (group)
3066   "Initialize summary buffer."
3067   (let ((buffer (gnus-summary-buffer-name group))
3068         (dead-name (concat "*Dead Summary "
3069                            (gnus-group-decoded-name group) "*")))
3070     ;; If a dead summary buffer exists, we kill it.
3071     (when (gnus-buffer-live-p dead-name)
3072       (gnus-kill-buffer dead-name))
3073     (if (get-buffer buffer)
3074         (progn
3075           (set-buffer buffer)
3076           (setq gnus-summary-buffer (current-buffer))
3077           (not gnus-newsgroup-prepared))
3078       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3079       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3080       (gnus-summary-mode group)
3081       (when gnus-carpal
3082         (gnus-carpal-setup-buffer 'summary))
3083       (unless gnus-single-article-buffer
3084         (make-local-variable 'gnus-article-buffer)
3085         (make-local-variable 'gnus-article-current)
3086         (make-local-variable 'gnus-original-article-buffer))
3087       (setq gnus-newsgroup-name group)
3088       ;; Set any local variables in the group parameters.
3089       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3090       t)))
3091
3092 (defun gnus-set-global-variables ()
3093   "Set the global equivalents of the buffer-local variables.
3094 They are set to the latest values they had.  These reflect the summary
3095 buffer that was in action when the last article was fetched."
3096   (when (eq major-mode 'gnus-summary-mode)
3097     (setq gnus-summary-buffer (current-buffer))
3098     (let ((name gnus-newsgroup-name)
3099           (marked gnus-newsgroup-marked)
3100           (spam gnus-newsgroup-spam-marked)
3101           (unread gnus-newsgroup-unreads)
3102           (headers gnus-current-headers)
3103           (data gnus-newsgroup-data)
3104           (summary gnus-summary-buffer)
3105           (article-buffer gnus-article-buffer)
3106           (original gnus-original-article-buffer)
3107           (gac gnus-article-current)
3108           (reffed gnus-reffed-article-number)
3109           (score-file gnus-current-score-file)
3110           (default-charset gnus-newsgroup-charset)
3111           vlist)
3112       (let ((locals gnus-newsgroup-variables))
3113         (while locals
3114           (if (consp (car locals))
3115               (push (eval (caar locals)) vlist)
3116             (push (eval (car locals)) vlist))
3117           (setq locals (cdr locals)))
3118         (setq vlist (nreverse vlist)))
3119       (save-excursion
3120         (set-buffer gnus-group-buffer)
3121         (setq gnus-newsgroup-name name
3122               gnus-newsgroup-marked marked
3123               gnus-newsgroup-spam-marked spam
3124               gnus-newsgroup-unreads unread
3125               gnus-current-headers headers
3126               gnus-newsgroup-data data
3127               gnus-article-current gac
3128               gnus-summary-buffer summary
3129               gnus-article-buffer article-buffer
3130               gnus-original-article-buffer original
3131               gnus-reffed-article-number reffed
3132               gnus-current-score-file score-file
3133               gnus-newsgroup-charset default-charset)
3134         (let ((locals gnus-newsgroup-variables))
3135           (while locals
3136             (if (consp (car locals))
3137                 (set (caar locals) (pop vlist))
3138               (set (car locals) (pop vlist)))
3139             (setq locals (cdr locals))))
3140         ;; The article buffer also has local variables.
3141         (when (gnus-buffer-live-p gnus-article-buffer)
3142           (set-buffer gnus-article-buffer)
3143           (setq gnus-summary-buffer summary))))))
3144
3145 (defun gnus-summary-article-unread-p (article)
3146   "Say whether ARTICLE is unread or not."
3147   (memq article gnus-newsgroup-unreads))
3148
3149 (defun gnus-summary-first-article-p (&optional article)
3150   "Return whether ARTICLE is the first article in the buffer."
3151   (if (not (setq article (or article (gnus-summary-article-number))))
3152       nil
3153     (eq article (caar gnus-newsgroup-data))))
3154
3155 (defun gnus-summary-last-article-p (&optional article)
3156   "Return whether ARTICLE is the last article in the buffer."
3157   (if (not (setq article (or article (gnus-summary-article-number))))
3158       ;; All non-existent numbers are the last article.  :-)
3159       t
3160     (not (cdr (gnus-data-find-list article)))))
3161
3162 (defun gnus-make-thread-indent-array ()
3163   (let ((n 200))
3164     (unless (and gnus-thread-indent-array
3165                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3166       (setq gnus-thread-indent-array (make-vector 201 "")
3167             gnus-thread-indent-array-level gnus-thread-indent-level)
3168       (while (>= n 0)
3169         (aset gnus-thread-indent-array n
3170               (make-string (* n gnus-thread-indent-level) ? ))
3171         (setq n (1- n))))))
3172
3173 (defun gnus-update-summary-mark-positions ()
3174   "Compute where the summary marks are to go."
3175   (save-excursion
3176     (when (gnus-buffer-exists-p gnus-summary-buffer)
3177       (set-buffer gnus-summary-buffer))
3178     (let ((gnus-replied-mark 129)
3179           (gnus-score-below-mark 130)
3180           (gnus-score-over-mark 130)
3181           (gnus-undownloaded-mark 131)
3182           (spec gnus-summary-line-format-spec)
3183           gnus-visual pos)
3184       (save-excursion
3185         (gnus-set-work-buffer)
3186         (let ((gnus-summary-line-format-spec spec)
3187               (gnus-newsgroup-downloadable '(0)))
3188           (gnus-summary-insert-line
3189            (make-full-mail-header 0 "" "nobody"
3190                                   "05 Apr 2001 23:33:09 +0400"
3191                                   "" "" 0 0 "" nil)
3192            0 nil t 128 t nil "" nil 1)
3193           (goto-char (point-min))
3194           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3195                                              (- (point) (point-min) 1)))))
3196           (goto-char (point-min))
3197           (push (cons 'replied (and (search-forward "\201" nil t)
3198                                     (- (point) (point-min) 1)))
3199                 pos)
3200           (goto-char (point-min))
3201           (push (cons 'score (and (search-forward "\202" nil t)
3202                                   (- (point) (point-min) 1)))
3203                 pos)
3204           (goto-char (point-min))
3205           (push (cons 'download
3206                       (and (search-forward "\203" nil t)
3207                            (- (point) (point-min) 1)))
3208                 pos)))
3209       (setq gnus-summary-mark-positions pos))))
3210
3211 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3212   "Insert a dummy root in the summary buffer."
3213   (beginning-of-line)
3214   (gnus-add-text-properties
3215    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3216    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3217
3218 (defun gnus-summary-extract-address-component (from)
3219   (or (car (funcall gnus-extract-address-components from))
3220       from))
3221
3222 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3223   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3224                                 default-mime-charset)))
3225     ;; Is it really necessary to do this next part for each summary line?
3226     ;; Luckily, doesn't seem to slow things down much.
3227     (or
3228      (and gnus-ignored-from-addresses
3229           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3230           (let ((extra-headers (mail-header-extra header))
3231                 to
3232                 newsgroups)
3233             (cond
3234              ((setq to (cdr (assq 'To extra-headers)))
3235               (concat "-> "
3236                       (inline
3237                         (gnus-summary-extract-address-component
3238                          (funcall gnus-decode-encoded-word-function to)))))
3239              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3240               (concat "=> " newsgroups)))))
3241      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3242
3243 (defun gnus-summary-insert-line (gnus-tmp-header
3244                                  gnus-tmp-level gnus-tmp-current
3245                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3246                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3247                                  &optional gnus-tmp-dummy gnus-tmp-score
3248                                  gnus-tmp-process)
3249   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3250          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3251          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3252          (gnus-tmp-score-char
3253           (if (or (null gnus-summary-default-score)
3254                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3255                       gnus-summary-zcore-fuzz))
3256               ?\ ;;;Whitespace
3257             (if (< gnus-tmp-score gnus-summary-default-score)
3258                 gnus-score-below-mark gnus-score-over-mark)))
3259          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3260          (gnus-tmp-replied
3261           (cond (gnus-tmp-process gnus-process-mark)
3262                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3263                  gnus-cached-mark)
3264                 (gnus-tmp-replied gnus-replied-mark)
3265                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3266                  gnus-forwarded-mark)
3267                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3268                  gnus-saved-mark)
3269                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3270                  gnus-recent-mark)
3271                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3272                  gnus-unseen-mark)
3273                 (t gnus-no-mark)))
3274          (gnus-tmp-downloaded
3275           (cond (undownloaded 
3276                  gnus-undownloaded-mark)
3277                 (gnus-newsgroup-agentized
3278                  gnus-downloaded-mark)
3279                 (t
3280                  gnus-no-mark)))
3281          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3282          (gnus-tmp-name
3283           (cond
3284            ((string-match "<[^>]+> *$" gnus-tmp-from)
3285             (let ((beg (match-beginning 0)))
3286               (or (and (string-match "^\".+\"" gnus-tmp-from)
3287                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3288                   (substring gnus-tmp-from 0 beg))))
3289            ((string-match "(.+)" gnus-tmp-from)
3290             (substring gnus-tmp-from
3291                        (1+ (match-beginning 0)) (1- (match-end 0))))
3292            (t gnus-tmp-from)))
3293          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3294          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3295          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3296          (buffer-read-only nil))
3297     (when (string= gnus-tmp-name "")
3298       (setq gnus-tmp-name gnus-tmp-from))
3299     (unless (numberp gnus-tmp-lines)
3300       (setq gnus-tmp-lines -1))
3301     (if (= gnus-tmp-lines -1)
3302         (setq gnus-tmp-lines "?")
3303       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3304     (gnus-put-text-property-excluding-characters-with-faces
3305      (point)
3306      (progn (eval gnus-summary-line-format-spec) (point))
3307      'gnus-number gnus-tmp-number)
3308     (when (gnus-visual-p 'summary-highlight 'highlight)
3309       (forward-line -1)
3310       (gnus-run-hooks 'gnus-summary-update-hook)
3311       (forward-line 1))))
3312
3313 (defun gnus-summary-update-line (&optional dont-update)
3314   "Update summary line after change."
3315   (when (and gnus-summary-default-score
3316              (not gnus-summary-inhibit-highlight))
3317     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3318            (article (gnus-summary-article-number))
3319            (score (gnus-summary-article-score article)))
3320       (unless dont-update
3321         (if (and gnus-summary-mark-below
3322                  (< (gnus-summary-article-score)
3323                     gnus-summary-mark-below))
3324             ;; This article has a low score, so we mark it as read.
3325             (when (memq article gnus-newsgroup-unreads)
3326               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3327           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3328             ;; This article was previously marked as read on account
3329             ;; of a low score, but now it has risen, so we mark it as
3330             ;; unread.
3331             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3332         (gnus-summary-update-mark
3333          (if (or (null gnus-summary-default-score)
3334                  (<= (abs (- score gnus-summary-default-score))
3335                      gnus-summary-zcore-fuzz))
3336              ?\ ;;;Whitespace
3337            (if (< score gnus-summary-default-score)
3338                gnus-score-below-mark gnus-score-over-mark))
3339          'score))
3340       ;; Do visual highlighting.
3341       (when (gnus-visual-p 'summary-highlight 'highlight)
3342         (gnus-run-hooks 'gnus-summary-update-hook)))))
3343
3344 (defvar gnus-tmp-new-adopts nil)
3345
3346 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3347   "Return the number of articles in THREAD.
3348 This may be 0 in some cases -- if none of the articles in
3349 the thread are to be displayed."
3350   (let* ((number
3351           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3352           (cond
3353            ((not (listp thread))
3354             1)
3355            ((and (consp thread) (cdr thread))
3356             (apply
3357              '+ 1 (mapcar
3358                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3359            ((null thread)
3360             1)
3361            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3362             1)
3363            (t 0))))
3364     (when (and level (zerop level) gnus-tmp-new-adopts)
3365       (incf number
3366             (apply '+ (mapcar
3367                        'gnus-summary-number-of-articles-in-thread
3368                        gnus-tmp-new-adopts))))
3369     (if char
3370         (if (> number 1) gnus-not-empty-thread-mark
3371           gnus-empty-thread-mark)
3372       number)))
3373
3374 (defsubst gnus-summary-line-message-size (head)
3375   "Return pretty-printed version of message size.
3376 This function is intended to be used in
3377 `gnus-summary-line-format-alist'."
3378   (let ((c (or (mail-header-chars head) -1)))
3379     (cond ((< c 0) "n/a")               ; chars not available
3380           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3381           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3382           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3383           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3384
3385
3386 (defun gnus-summary-set-local-parameters (group)
3387   "Go through the local params of GROUP and set all variable specs in that list."
3388   (let ((params (gnus-group-find-parameter group))
3389         (vars '(quit-config))           ; Ignore quit-config.
3390         elem)
3391     (while params
3392       (setq elem (car params)
3393             params (cdr params))
3394       (and (consp elem)                 ; Has to be a cons.
3395            (consp (cdr elem))           ; The cdr has to be a list.
3396            (symbolp (car elem))         ; Has to be a symbol in there.
3397            (not (memq (car elem) vars))
3398            (ignore-errors               ; So we set it.
3399              (push (car elem) vars)
3400              (make-local-variable (car elem))
3401              (set (car elem) (eval (nth 1 elem))))))))
3402
3403 (defun gnus-summary-read-group (group &optional show-all no-article
3404                                       kill-buffer no-display backward
3405                                       select-articles)
3406   "Start reading news in newsgroup GROUP.
3407 If SHOW-ALL is non-nil, already read articles are also listed.
3408 If NO-ARTICLE is non-nil, no article is selected initially.
3409 If NO-DISPLAY, don't generate a summary buffer."
3410   (let (result)
3411     (while (and group
3412                 (null (setq result
3413                             (let ((gnus-auto-select-next nil))
3414                               (or (gnus-summary-read-group-1
3415                                    group show-all no-article
3416                                    kill-buffer no-display
3417                                    select-articles)
3418                                   (setq show-all nil
3419                                         select-articles nil)))))
3420                 (eq gnus-auto-select-next 'quietly))
3421       (set-buffer gnus-group-buffer)
3422       ;; The entry function called above goes to the next
3423       ;; group automatically, so we go two groups back
3424       ;; if we are searching for the previous group.
3425       (when backward
3426         (gnus-group-prev-unread-group 2))
3427       (if (not (equal group (gnus-group-group-name)))
3428           (setq group (gnus-group-group-name))
3429         (setq group nil)))
3430     result))
3431
3432 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3433   "Directly jump to the other GROUP from summary buffer.
3434 If SHOW-ALL is non-nil, already read articles are also listed."
3435   (interactive
3436    (if (eq gnus-summary-buffer (current-buffer))
3437        (list (completing-read
3438               "Group: " gnus-active-hashtb nil t
3439               (when (and gnus-newsgroup-name
3440                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3441                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3442               'gnus-group-history)
3443              current-prefix-arg)
3444      (error "%s must be invoked from a gnus summary buffer." this-command)))
3445   (unless (or (zerop (length group))
3446               (and gnus-newsgroup-name
3447                    (string-equal gnus-newsgroup-name group)))
3448     (gnus-summary-exit)
3449     (gnus-summary-read-group group show-all
3450                              gnus-dont-select-after-jump-to-other-group)))
3451
3452 (defun gnus-summary-read-group-1 (group show-all no-article
3453                                         kill-buffer no-display
3454                                         &optional select-articles)
3455   ;; Killed foreign groups can't be entered.
3456   ;;  (when (and (not (gnus-group-native-p group))
3457   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3458   ;;    (error "Dead non-native groups can't be entered"))
3459   (gnus-message 5 "Retrieving newsgroup: %s..."
3460                 (gnus-group-decoded-name group))
3461   (let* ((new-group (gnus-summary-setup-buffer group))
3462          (quit-config (gnus-group-quit-config group))
3463          (did-select (and new-group (gnus-select-newsgroup
3464                                      group show-all select-articles))))
3465     (cond
3466      ;; This summary buffer exists already, so we just select it.
3467      ((not new-group)
3468       (gnus-set-global-variables)
3469       (when kill-buffer
3470         (gnus-kill-or-deaden-summary kill-buffer))
3471       (gnus-configure-windows 'summary 'force)
3472       (gnus-set-mode-line 'summary)
3473       (gnus-summary-position-point)
3474       (message "")
3475       t)
3476      ;; We couldn't select this group.
3477      ((null did-select)
3478       (when (and (eq major-mode 'gnus-summary-mode)
3479                  (not (equal (current-buffer) kill-buffer)))
3480         (kill-buffer (current-buffer))
3481         (if (not quit-config)
3482             (progn
3483               ;; Update the info -- marks might need to be removed,
3484               ;; for instance.
3485               (gnus-summary-update-info)
3486               (set-buffer gnus-group-buffer)
3487               (gnus-group-jump-to-group group)
3488               (gnus-group-next-unread-group 1))
3489           (gnus-handle-ephemeral-exit quit-config)))
3490       (let ((grpinfo (gnus-get-info group)))
3491         (if (null (gnus-info-read grpinfo))
3492             (gnus-message 3 "Group %s contains no messages"
3493                           (gnus-group-decoded-name group))
3494           (gnus-message 3 "Can't select group")))
3495       nil)
3496      ;; The user did a `C-g' while prompting for number of articles,
3497      ;; so we exit this group.
3498      ((eq did-select 'quit)
3499       (and (eq major-mode 'gnus-summary-mode)
3500            (not (equal (current-buffer) kill-buffer))
3501            (kill-buffer (current-buffer)))
3502       (when kill-buffer
3503         (gnus-kill-or-deaden-summary kill-buffer))
3504       (if (not quit-config)
3505           (progn
3506             (set-buffer gnus-group-buffer)
3507             (gnus-group-jump-to-group group)
3508             (gnus-group-next-unread-group 1)
3509             (gnus-configure-windows 'group 'force))
3510         (gnus-handle-ephemeral-exit quit-config))
3511       ;; Finally signal the quit.
3512       (signal 'quit nil))
3513      ;; The group was successfully selected.
3514      (t
3515       (gnus-set-global-variables)
3516       ;; Save the active value in effect when the group was entered.
3517       (setq gnus-newsgroup-active
3518             (gnus-copy-sequence
3519              (gnus-active gnus-newsgroup-name)))
3520       ;; You can change the summary buffer in some way with this hook.
3521       (gnus-run-hooks 'gnus-select-group-hook)
3522       (gnus-update-format-specifications
3523        nil 'summary 'summary-mode 'summary-dummy)
3524       (gnus-update-summary-mark-positions)
3525       ;; Do score processing.
3526       (when gnus-use-scoring
3527         (gnus-possibly-score-headers))
3528       ;; Check whether to fill in the gaps in the threads.
3529       (when gnus-build-sparse-threads
3530         (gnus-build-sparse-threads))
3531       ;; Find the initial limit.
3532       (if gnus-show-threads
3533           (if show-all
3534               (let ((gnus-newsgroup-dormant nil))
3535                 (gnus-summary-initial-limit show-all))
3536             (gnus-summary-initial-limit show-all))
3537         ;; When unthreaded, all articles are always shown.
3538         (setq gnus-newsgroup-limit
3539               (mapcar
3540                (lambda (header) (mail-header-number header))
3541                gnus-newsgroup-headers)))
3542       ;; Generate the summary buffer.
3543       (unless no-display
3544         (gnus-summary-prepare))
3545       (when gnus-use-trees
3546         (gnus-tree-open group)
3547         (setq gnus-summary-highlight-line-function
3548               'gnus-tree-highlight-article))
3549       ;; If the summary buffer is empty, but there are some low-scored
3550       ;; articles or some excluded dormants, we include these in the
3551       ;; buffer.
3552       (when (and (zerop (buffer-size))
3553                  (not no-display))
3554         (cond (gnus-newsgroup-dormant
3555                (gnus-summary-limit-include-dormant))
3556               ((and gnus-newsgroup-scored show-all)
3557                (gnus-summary-limit-include-expunged t))))
3558       ;; Function `gnus-apply-kill-file' must be called in this hook.
3559       (gnus-run-hooks 'gnus-apply-kill-hook)
3560       (if (and (zerop (buffer-size))
3561                (not no-display))
3562           (progn
3563             ;; This newsgroup is empty.
3564             (gnus-summary-catchup-and-exit nil t)
3565             (gnus-message 6 "No unread news")
3566             (when kill-buffer
3567               (gnus-kill-or-deaden-summary kill-buffer))
3568             ;; Return nil from this function.
3569             nil)
3570         ;; Hide conversation thread subtrees.  We cannot do this in
3571         ;; gnus-summary-prepare-hook since kill processing may not
3572         ;; work with hidden articles.
3573         (gnus-summary-maybe-hide-threads)
3574         (when kill-buffer
3575           (gnus-kill-or-deaden-summary kill-buffer))
3576         (gnus-summary-auto-select-subject)
3577         ;; Show first unread article if requested.
3578         (if (and (not no-article)
3579                  (not no-display)
3580                  gnus-newsgroup-unreads
3581                  gnus-auto-select-first)
3582             (progn
3583               (gnus-configure-windows 'summary)
3584               (let ((art (gnus-summary-article-number)))
3585                 (unless (and (not gnus-plugged)
3586                              (or (memq art gnus-newsgroup-undownloaded)
3587                                  (memq art gnus-newsgroup-downloadable)))
3588                   (gnus-summary-goto-article art))))
3589           ;; Don't select any articles.
3590           (gnus-summary-position-point)
3591           (gnus-configure-windows 'summary 'force)
3592           (gnus-set-mode-line 'summary))
3593         (when (and gnus-auto-center-group
3594                    (get-buffer-window gnus-group-buffer t))
3595           ;; Gotta use windows, because recenter does weird stuff if
3596           ;; the current buffer ain't the displayed window.
3597           (let ((owin (selected-window)))
3598             (select-window (get-buffer-window gnus-group-buffer t))
3599             (when (gnus-group-goto-group group)
3600               (recenter))
3601             (select-window owin)))
3602         ;; Mark this buffer as "prepared".
3603         (setq gnus-newsgroup-prepared t)
3604         (gnus-run-hooks 'gnus-summary-prepared-hook)
3605         (unless (gnus-ephemeral-group-p group)
3606           (gnus-group-update-group group))
3607         t)))))
3608
3609 (defun gnus-summary-auto-select-subject ()
3610   "Select the subject line on initial group entry."
3611   (goto-char (point-min))
3612   (cond
3613    ((eq gnus-auto-select-subject 'best)
3614     (gnus-summary-best-unread-subject))
3615    ((eq gnus-auto-select-subject 'unread)
3616     (gnus-summary-first-unread-subject))
3617    ((eq gnus-auto-select-subject 'unseen)
3618     (gnus-summary-first-unseen-subject))
3619    ((eq gnus-auto-select-subject 'unseen-or-unread)
3620     (gnus-summary-first-unseen-or-unread-subject))
3621    ((eq gnus-auto-select-subject 'first)
3622     ;; Do nothing.
3623     )
3624    ((functionp gnus-auto-select-subject)
3625     (funcall gnus-auto-select-subject))))
3626
3627 (defun gnus-summary-prepare ()
3628   "Generate the summary buffer."
3629   (interactive)
3630   (let ((buffer-read-only nil))
3631     (erase-buffer)
3632     (setq gnus-newsgroup-data nil
3633           gnus-newsgroup-data-reverse nil)
3634     (gnus-run-hooks 'gnus-summary-generate-hook)
3635     ;; Generate the buffer, either with threads or without.
3636     (when gnus-newsgroup-headers
3637       (gnus-summary-prepare-threads
3638        (if gnus-show-threads
3639            (gnus-sort-gathered-threads
3640             (funcall gnus-summary-thread-gathering-function
3641                      (gnus-sort-threads
3642                       (gnus-cut-threads (gnus-make-threads)))))
3643          ;; Unthreaded display.
3644          (gnus-sort-articles gnus-newsgroup-headers))))
3645     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3646     ;; Call hooks for modifying summary buffer.
3647     (goto-char (point-min))
3648     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3649
3650 (defsubst gnus-general-simplify-subject (subject)
3651   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3652   (setq subject
3653         (cond
3654          ;; Truncate the subject.
3655          (gnus-simplify-subject-functions
3656           (gnus-map-function gnus-simplify-subject-functions subject))
3657          ((numberp gnus-summary-gather-subject-limit)
3658           (setq subject (gnus-simplify-subject-re subject))
3659           (if (> (length subject) gnus-summary-gather-subject-limit)
3660               (substring subject 0 gnus-summary-gather-subject-limit)
3661             subject))
3662          ;; Fuzzily simplify it.
3663          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3664           (gnus-simplify-subject-fuzzy subject))
3665          ;; Just remove the leading "Re:".
3666          (t
3667           (gnus-simplify-subject-re subject))))
3668
3669   (if (and gnus-summary-gather-exclude-subject
3670            (string-match gnus-summary-gather-exclude-subject subject))
3671       nil                               ; This article shouldn't be gathered
3672     subject))
3673
3674 (defun gnus-summary-simplify-subject-query ()
3675   "Query where the respool algorithm would put this article."
3676   (interactive)
3677   (gnus-summary-select-article)
3678   (message "%s"
3679            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3680
3681 (defun gnus-gather-threads-by-subject (threads)
3682   "Gather threads by looking at Subject headers."
3683   (if (not gnus-summary-make-false-root)
3684       threads
3685     (let ((hashtb (gnus-make-hashtable 1024))
3686           (prev threads)
3687           (result threads)
3688           subject hthread whole-subject)
3689       (while threads
3690         (setq subject (gnus-general-simplify-subject
3691                        (setq whole-subject (mail-header-subject
3692                                             (caar threads)))))
3693         (when subject
3694           (if (setq hthread (gnus-gethash subject hashtb))
3695               (progn
3696                 ;; We enter a dummy root into the thread, if we
3697                 ;; haven't done that already.
3698                 (unless (stringp (caar hthread))
3699                   (setcar hthread (list whole-subject (car hthread))))
3700                 ;; We add this new gathered thread to this gathered
3701                 ;; thread.
3702                 (setcdr (car hthread)
3703                         (nconc (cdar hthread) (list (car threads))))
3704                 ;; Remove it from the list of threads.
3705                 (setcdr prev (cdr threads))
3706                 (setq threads prev))
3707             ;; Enter this thread into the hash table.
3708             (gnus-sethash subject
3709                           (if gnus-summary-make-false-root-always
3710                               (progn
3711                                 ;; If you want a dummy root above all
3712                                 ;; threads...
3713                                 (setcar threads (list whole-subject
3714                                                       (car threads)))
3715                                 threads)
3716                             threads)
3717                           hashtb)))
3718         (setq prev threads)
3719         (setq threads (cdr threads)))
3720       result)))
3721
3722 (defun gnus-gather-threads-by-references (threads)
3723   "Gather threads by looking at References headers."
3724   (let ((idhashtb (gnus-make-hashtable 1024))
3725         (thhashtb (gnus-make-hashtable 1024))
3726         (prev threads)
3727         (result threads)
3728         ids references id gthread gid entered ref)
3729     (while threads
3730       (when (setq references (mail-header-references (caar threads)))
3731         (setq id (mail-header-id (caar threads))
3732               ids (inline (gnus-split-references references))
3733               entered nil)
3734         (while (setq ref (pop ids))
3735           (setq ids (delete ref ids))
3736           (if (not (setq gid (gnus-gethash ref idhashtb)))
3737               (progn
3738                 (gnus-sethash ref id idhashtb)
3739                 (gnus-sethash id threads thhashtb))
3740             (setq gthread (gnus-gethash gid thhashtb))
3741             (unless entered
3742               ;; We enter a dummy root into the thread, if we
3743               ;; haven't done that already.
3744               (unless (stringp (caar gthread))
3745                 (setcar gthread (list (mail-header-subject (caar gthread))
3746                                       (car gthread))))
3747               ;; We add this new gathered thread to this gathered
3748               ;; thread.
3749               (setcdr (car gthread)
3750                       (nconc (cdar gthread) (list (car threads)))))
3751             ;; Add it into the thread hash table.
3752             (gnus-sethash id gthread thhashtb)
3753             (setq entered t)
3754             ;; Remove it from the list of threads.
3755             (setcdr prev (cdr threads))
3756             (setq threads prev))))
3757       (setq prev threads)
3758       (setq threads (cdr threads)))
3759     result))
3760
3761 (defun gnus-sort-gathered-threads (threads)
3762   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3763   (let ((result threads))
3764     (while threads
3765       (when (stringp (caar threads))
3766         (setcdr (car threads)
3767                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3768       (setq threads (cdr threads)))
3769     result))
3770
3771 (defun gnus-thread-loop-p (root thread)
3772   "Say whether ROOT is in THREAD."
3773   (let ((stack (list thread))
3774         (infloop 0)
3775         th)
3776     (while (setq thread (pop stack))
3777       (setq th (cdr thread))
3778       (while (and th
3779                   (not (eq (caar th) root)))
3780         (pop th))
3781       (if th
3782           ;; We have found a loop.
3783           (let (ref-dep)
3784             (setcdr thread (delq (car th) (cdr thread)))
3785             (if (boundp (setq ref-dep (intern "none"
3786                                               gnus-newsgroup-dependencies)))
3787                 (setcdr (symbol-value ref-dep)
3788                         (nconc (cdr (symbol-value ref-dep))
3789                                (list (car th))))
3790               (set ref-dep (list nil (car th))))
3791             (setq infloop 1
3792                   stack nil))
3793         ;; Push all the subthreads onto the stack.
3794         (push (cdr thread) stack)))
3795     infloop))
3796
3797 (defun gnus-make-threads ()
3798   "Go through the dependency hashtb and find the roots.  Return all threads."
3799   (let (threads)
3800     (while (catch 'infloop
3801              (mapatoms
3802               (lambda (refs)
3803                 ;; Deal with self-referencing References loops.
3804                 (when (and (car (symbol-value refs))
3805                            (not (zerop
3806                                  (apply
3807                                   '+
3808                                   (mapcar
3809                                    (lambda (thread)
3810                                      (gnus-thread-loop-p
3811                                       (car (symbol-value refs)) thread))
3812                                    (cdr (symbol-value refs)))))))
3813                   (setq threads nil)
3814                   (throw 'infloop t))
3815                 (unless (car (symbol-value refs))
3816                   ;; These threads do not refer back to any other articles,
3817                   ;; so they're roots.
3818                   (setq threads (append (cdr (symbol-value refs)) threads))))
3819               gnus-newsgroup-dependencies)))
3820     threads))
3821
3822 ;; Build the thread tree.
3823 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3824   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3825
3826 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3827 if it was already present.
3828
3829 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3830 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3831 Message-IDs will be renamed to a unique Message-ID before being
3832 entered.
3833
3834 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3835   (let* ((id (mail-header-id header))
3836          (id-dep (and id (intern id dependencies)))
3837          parent-id ref ref-dep ref-header replaced)
3838     ;; Enter this `header' in the `dependencies' table.
3839     (cond
3840      ((not id-dep)
3841       (setq header nil))
3842      ;; The first two cases do the normal part: enter a new `header'
3843      ;; in the `dependencies' table.
3844      ((not (boundp id-dep))
3845       (set id-dep (list header)))
3846      ((null (car (symbol-value id-dep)))
3847       (setcar (symbol-value id-dep) header))
3848
3849      ;; From here the `header' was already present in the
3850      ;; `dependencies' table.
3851      (force-new
3852       ;; Overrides an existing entry;
3853       ;; just set the header part of the entry.
3854       (setcar (symbol-value id-dep) header)
3855       (setq replaced t))
3856
3857      ;; Renames the existing `header' to a unique Message-ID.
3858      ((not gnus-summary-ignore-duplicates)
3859       ;; An article with this Message-ID has already been seen.
3860       ;; We rename the Message-ID.
3861       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3862            (list header))
3863       (mail-header-set-id header id))
3864
3865      ;; The last case ignores an existing entry, except it adds any
3866      ;; additional Xrefs (in case the two articles came from different
3867      ;; servers.
3868      ;; Also sets `header' to `nil' meaning that the `dependencies'
3869      ;; table was *not* modified.
3870      (t
3871       (mail-header-set-xref
3872        (car (symbol-value id-dep))
3873        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3874                    "")
3875                (or (mail-header-xref header) "")))
3876       (setq header nil)))
3877
3878     (when (and header (not replaced))
3879       ;; First check that we are not creating a References loop.
3880       (setq parent-id (gnus-parent-id (mail-header-references header)))
3881       (setq ref parent-id)
3882       (while (and ref
3883                   (setq ref-dep (intern-soft ref dependencies))
3884                   (boundp ref-dep)
3885                   (setq ref-header (car (symbol-value ref-dep))))
3886         (if (string= id ref)
3887             ;; Yuk!  This is a reference loop.  Make the article be a
3888             ;; root article.
3889             (progn
3890               (mail-header-set-references (car (symbol-value id-dep)) "none")
3891               (setq ref nil)
3892               (setq parent-id nil))
3893           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3894       (setq ref-dep (intern (or parent-id "none") dependencies))
3895       (if (boundp ref-dep)
3896           (setcdr (symbol-value ref-dep)
3897                   (nconc (cdr (symbol-value ref-dep))
3898                          (list (symbol-value id-dep))))
3899         (set ref-dep (list nil (symbol-value id-dep)))))
3900     header))
3901
3902 (defun gnus-extract-message-id-from-in-reply-to (string)
3903   (if (string-match "<[^>]+>" string)
3904       (substring string (match-beginning 0) (match-end 0))
3905     nil))
3906
3907 (defun gnus-build-sparse-threads ()
3908   (let ((headers gnus-newsgroup-headers)
3909         (mail-parse-charset gnus-newsgroup-charset)
3910         (gnus-summary-ignore-duplicates t)
3911         header references generation relations
3912         subject child end new-child date)
3913     ;; First we create an alist of generations/relations, where
3914     ;; generations is how much we trust the relation, and the relation
3915     ;; is parent/child.
3916     (gnus-message 7 "Making sparse threads...")
3917     (save-excursion
3918       (nnheader-set-temp-buffer " *gnus sparse threads*")
3919       (while (setq header (pop headers))
3920         (when (and (setq references (mail-header-references header))
3921                    (not (string= references "")))
3922           (insert references)
3923           (setq child (mail-header-id header)
3924                 subject (mail-header-subject header)
3925                 date (mail-header-date header)
3926                 generation 0)
3927           (while (search-backward ">" nil t)
3928             (setq end (1+ (point)))
3929             (when (search-backward "<" nil t)
3930               (setq new-child (buffer-substring (point) end))
3931               (push (list (incf generation)
3932                           child (setq child new-child)
3933                           subject date)
3934                     relations)))
3935           (when child
3936             (push (list (1+ generation) child nil subject) relations))
3937           (erase-buffer)))
3938       (kill-buffer (current-buffer)))
3939     ;; Sort over trustworthiness.
3940     (mapcar
3941      (lambda (relation)
3942        (when (gnus-dependencies-add-header
3943               (make-full-mail-header-from-decoded-header
3944                gnus-reffed-article-number
3945                (nth 3 relation) "" (or (nth 4 relation) "")
3946                (nth 1 relation)
3947                (or (nth 2 relation) "") 0 0 "")
3948               gnus-newsgroup-dependencies nil)
3949          (push gnus-reffed-article-number gnus-newsgroup-limit)
3950          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3951          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3952                gnus-newsgroup-reads)
3953          (decf gnus-reffed-article-number)))
3954      (sort relations 'car-less-than-car))
3955     (gnus-message 7 "Making sparse threads...done")))
3956
3957 (defun gnus-build-old-threads ()
3958   ;; Look at all the articles that refer back to old articles, and
3959   ;; fetch the headers for the articles that aren't there.  This will
3960   ;; build complete threads - if the roots haven't been expired by the
3961   ;; server, that is.
3962   (let ((mail-parse-charset gnus-newsgroup-charset)
3963         id heads)
3964     (mapatoms
3965      (lambda (refs)
3966        (when (not (car (symbol-value refs)))
3967          (setq heads (cdr (symbol-value refs)))
3968          (while heads
3969            (if (memq (mail-header-number (caar heads))
3970                      gnus-newsgroup-dormant)
3971                (setq heads (cdr heads))
3972              (setq id (symbol-name refs))
3973              (while (and (setq id (gnus-build-get-header id))
3974                          (not (car (gnus-id-to-thread id)))))
3975              (setq heads nil)))))
3976      gnus-newsgroup-dependencies)))
3977
3978 ;; This function has to be called with point after the article number
3979 ;; on the beginning of the line.
3980 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3981   (let ((eol (gnus-point-at-eol))
3982         (buffer (current-buffer))
3983         header references in-reply-to)
3984
3985     ;; overview: [num subject from date id refs chars lines misc]
3986     (unwind-protect
3987         (progn
3988           (narrow-to-region (point) eol)
3989           (unless (eobp)
3990             (forward-char))
3991
3992           (setq header
3993                 (make-full-mail-header
3994                  number                         ; number
3995                  (nnheader-nov-field)           ; subject
3996                  (nnheader-nov-field)           ; from
3997                  (nnheader-nov-field)           ; date
3998                  (nnheader-nov-read-message-id) ; id
3999                  (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 ((addr (car (mime-entity-read-field h1 'From))))
4438      (or (std11-full-name-string addr)
4439          (std11-address-string addr)
4440          ""))
4441    (let ((addr (car (mime-entity-read-field h2 'From))))
4442      (or (std11-full-name-string addr)
4443          (std11-address-string addr)
4444          ""))
4445    ))
4446
4447 (defun gnus-thread-sort-by-author (h1 h2)
4448   "Sort threads by root author."
4449   (gnus-article-sort-by-author
4450    (gnus-thread-header h1)  (gnus-thread-header h2)))
4451
4452 (defsubst gnus-article-sort-by-subject (h1 h2)
4453   "Sort articles by root subject."
4454   (string-lessp
4455    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4456    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4457
4458 (defun gnus-thread-sort-by-subject (h1 h2)
4459   "Sort threads by root subject."
4460   (gnus-article-sort-by-subject
4461    (gnus-thread-header h1) (gnus-thread-header h2)))
4462
4463 (defsubst gnus-article-sort-by-date (h1 h2)
4464   "Sort articles by root article date."
4465   (time-less-p
4466    (gnus-date-get-time (mail-header-date h1))
4467    (gnus-date-get-time (mail-header-date h2))))
4468
4469 (defun gnus-thread-sort-by-date (h1 h2)
4470   "Sort threads by root article date."
4471   (gnus-article-sort-by-date
4472    (gnus-thread-header h1) (gnus-thread-header h2)))
4473
4474 (defsubst gnus-article-sort-by-score (h1 h2)
4475   "Sort articles by root article score.
4476 Unscored articles will be counted as having a score of zero."
4477   (> (or (cdr (assq (mail-header-number h1)
4478                     gnus-newsgroup-scored))
4479          gnus-summary-default-score 0)
4480      (or (cdr (assq (mail-header-number h2)
4481                     gnus-newsgroup-scored))
4482          gnus-summary-default-score 0)))
4483
4484 (defun gnus-thread-sort-by-score (h1 h2)
4485   "Sort threads by root article score."
4486   (gnus-article-sort-by-score
4487    (gnus-thread-header h1) (gnus-thread-header h2)))
4488
4489 (defun gnus-thread-sort-by-total-score (h1 h2)
4490   "Sort threads by the sum of all scores in the thread.
4491 Unscored articles will be counted as having a score of zero."
4492   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4493
4494 (defun gnus-thread-total-score (thread)
4495   ;; This function find the total score of THREAD.
4496   (cond
4497    ((null thread)
4498     0)
4499    ((consp thread)
4500     (if (stringp (car thread))
4501         (apply gnus-thread-score-function 0
4502                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4503       (gnus-thread-total-score-1 thread)))
4504    (t
4505     (gnus-thread-total-score-1 (list thread)))))
4506
4507 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4508   "Sort threads such that the thread with the most recently arrived article comes first."
4509   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4510
4511 (defun gnus-thread-highest-number (thread)
4512   "Return the highest article number in THREAD."
4513   (apply 'max (mapcar (lambda (header)
4514                         (mail-header-number header))
4515                       (message-flatten-list thread))))
4516
4517 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4518   "Sort threads such that the thread with the most recently dated article comes first."
4519   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4520
4521 (defun gnus-thread-latest-date (thread)
4522   "Return the highest article date in THREAD."
4523   (let ((previous-time 0))
4524     (apply 'max
4525            (mapcar
4526             (lambda (header)
4527               (setq previous-time
4528                     (time-to-seconds
4529                      (condition-case ()
4530                          (mail-header-parse-date (mail-header-date header))
4531                        (error previous-time)))))
4532             (sort
4533              (message-flatten-list thread)
4534              (lambda (h1 h2)
4535                (< (mail-header-number h1)
4536                   (mail-header-number h2))))))))
4537
4538 (defun gnus-thread-total-score-1 (root)
4539   ;; This function find the total score of the thread below ROOT.
4540   (setq root (car root))
4541   (apply gnus-thread-score-function
4542          (or (append
4543               (mapcar 'gnus-thread-total-score
4544                       (cdr (gnus-id-to-thread (mail-header-id root))))
4545               (when (> (mail-header-number root) 0)
4546                 (list (or (cdr (assq (mail-header-number root)
4547                                      gnus-newsgroup-scored))
4548                           gnus-summary-default-score 0))))
4549              (list gnus-summary-default-score)
4550              '(0))))
4551
4552 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4553 (defvar gnus-tmp-prev-subject nil)
4554 (defvar gnus-tmp-false-parent nil)
4555 (defvar gnus-tmp-root-expunged nil)
4556 (defvar gnus-tmp-dummy-line nil)
4557
4558 (eval-when-compile (defvar gnus-tmp-header))
4559 (defun gnus-extra-header (type &optional header)
4560   "Return the extra header of TYPE."
4561   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4562       ""))
4563
4564 (defvar gnus-tmp-thread-tree-header-string "")
4565
4566 (defcustom gnus-sum-thread-tree-root "> "
4567   "With %B spec, used for the root of a thread.
4568 If nil, use subject instead."
4569   :type 'string
4570   :group 'gnus-thread)
4571 (defcustom gnus-sum-thread-tree-false-root "> "
4572   "With %B spec, used for a false root of a thread.
4573 If nil, use subject instead."
4574   :type 'string
4575   :group 'gnus-thread)
4576 (defcustom gnus-sum-thread-tree-single-indent ""
4577   "With %B spec, used for a thread with just one message.
4578 If nil, use subject instead."
4579   :type 'string
4580   :group 'gnus-thread)
4581 (defcustom gnus-sum-thread-tree-vertical "| "
4582   "With %B spec, used for drawing a vertical line."
4583   :type 'string
4584   :group 'gnus-thread)
4585 (defcustom gnus-sum-thread-tree-indent "  "
4586   "With %B spec, used for indenting."
4587   :type 'string
4588   :group 'gnus-thread)
4589 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4590   "With %B spec, used for a leaf with brothers."
4591   :type 'string
4592   :group 'gnus-thread)
4593 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4594   "With %B spec, used for a leaf without brothers."
4595   :type 'string
4596   :group 'gnus-thread)
4597
4598 (defun gnus-summary-prepare-threads (threads)
4599   "Prepare summary buffer from THREADS and indentation LEVEL.
4600 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4601 or a straight list of headers."
4602   (gnus-message 7 "Generating summary...")
4603
4604   (setq gnus-newsgroup-threads threads)
4605   (beginning-of-line)
4606
4607   (let ((gnus-tmp-level 0)
4608         (default-score (or gnus-summary-default-score 0))
4609         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4610         (building-line-count gnus-summary-display-while-building)
4611         (building-count (integerp gnus-summary-display-while-building))
4612         thread number subject stack state gnus-tmp-gathered beg-match
4613         new-roots gnus-tmp-new-adopts thread-end simp-subject
4614         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4615         gnus-tmp-replied gnus-tmp-subject-or-nil
4616         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4617         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4618         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4619         tree-stack)
4620
4621     (setq gnus-tmp-prev-subject nil
4622           gnus-tmp-thread-tree-header-string "")
4623
4624     (if (vectorp (car threads))
4625         ;; If this is a straight (sic) list of headers, then a
4626         ;; threaded summary display isn't required, so we just create
4627         ;; an unthreaded one.
4628         (gnus-summary-prepare-unthreaded threads)
4629
4630       ;; Do the threaded display.
4631
4632       (if gnus-summary-display-while-building
4633           (switch-to-buffer (buffer-name)))
4634       (while (or threads stack gnus-tmp-new-adopts new-roots)
4635
4636         (if (and (= gnus-tmp-level 0)
4637                  (or (not stack)
4638                      (= (caar stack) 0))
4639                  (not gnus-tmp-false-parent)
4640                  (or gnus-tmp-new-adopts new-roots))
4641             (if gnus-tmp-new-adopts
4642                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4643                       thread (list (car gnus-tmp-new-adopts))
4644                       gnus-tmp-header (caar thread)
4645                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4646               (when new-roots
4647                 (setq thread (list (car new-roots))
4648                       gnus-tmp-header (caar thread)
4649                       new-roots (cdr new-roots))))
4650
4651           (if threads
4652               ;; If there are some threads, we do them before the
4653               ;; threads on the stack.
4654               (setq thread threads
4655                     gnus-tmp-header (caar thread))
4656             ;; There were no current threads, so we pop something off
4657             ;; the stack.
4658             (setq state (car stack)
4659                   gnus-tmp-level (car state)
4660                   tree-stack (cadr state)
4661                   thread (caddr state)
4662                   stack (cdr stack)
4663                   gnus-tmp-header (caar thread))))
4664
4665         (setq gnus-tmp-false-parent nil)
4666         (setq gnus-tmp-root-expunged nil)
4667         (setq thread-end nil)
4668
4669         (if (stringp gnus-tmp-header)
4670             ;; The header is a dummy root.
4671             (cond
4672              ((eq gnus-summary-make-false-root 'adopt)
4673               ;; We let the first article adopt the rest.
4674               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4675                                                (cddar thread)))
4676               (setq gnus-tmp-gathered
4677                     (nconc (mapcar
4678                             (lambda (h) (mail-header-number (car h)))
4679                             (cddar thread))
4680                            gnus-tmp-gathered))
4681               (setq thread (cons (list (caar thread)
4682                                        (cadar thread))
4683                                  (cdr thread)))
4684               (setq gnus-tmp-level -1
4685                     gnus-tmp-false-parent t))
4686              ((eq gnus-summary-make-false-root 'empty)
4687               ;; We print adopted articles with empty subject fields.
4688               (setq gnus-tmp-gathered
4689                     (nconc (mapcar
4690                             (lambda (h) (mail-header-number (car h)))
4691                             (cddar thread))
4692                            gnus-tmp-gathered))
4693               (setq gnus-tmp-level -1))
4694              ((eq gnus-summary-make-false-root 'dummy)
4695               ;; We remember that we probably want to output a dummy
4696               ;; root.
4697               (setq gnus-tmp-dummy-line gnus-tmp-header)
4698               (setq gnus-tmp-prev-subject gnus-tmp-header))
4699              (t
4700               ;; We do not make a root for the gathered
4701               ;; sub-threads at all.
4702               (setq gnus-tmp-level -1)))
4703
4704           (setq number (mail-header-number gnus-tmp-header)
4705                 subject (mail-header-subject gnus-tmp-header)
4706                 simp-subject (gnus-simplify-subject-fully subject))
4707
4708           (cond
4709            ;; If the thread has changed subject, we might want to make
4710            ;; this subthread into a root.
4711            ((and (null gnus-thread-ignore-subject)
4712                  (not (zerop gnus-tmp-level))
4713                  gnus-tmp-prev-subject
4714                  (not (string= gnus-tmp-prev-subject simp-subject)))
4715             (setq new-roots (nconc new-roots (list (car thread)))
4716                   thread-end t
4717                   gnus-tmp-header nil))
4718            ;; If the article lies outside the current limit,
4719            ;; then we do not display it.
4720            ((not (memq number gnus-newsgroup-limit))
4721             (setq gnus-tmp-gathered
4722                   (nconc (mapcar
4723                           (lambda (h) (mail-header-number (car h)))
4724                           (cdar thread))
4725                          gnus-tmp-gathered))
4726             (setq gnus-tmp-new-adopts (if (cdar thread)
4727                                           (append gnus-tmp-new-adopts
4728                                                   (cdar thread))
4729                                         gnus-tmp-new-adopts)
4730                   thread-end t
4731                   gnus-tmp-header nil)
4732             (when (zerop gnus-tmp-level)
4733               (setq gnus-tmp-root-expunged t)))
4734            ;; Perhaps this article is to be marked as read?
4735            ((and gnus-summary-mark-below
4736                  (< (or (cdr (assq number gnus-newsgroup-scored))
4737                         default-score)
4738                     gnus-summary-mark-below)
4739                  ;; Don't touch sparse articles.
4740                  (not (gnus-summary-article-sparse-p number))
4741                  (not (gnus-summary-article-ancient-p number)))
4742             (setq gnus-newsgroup-unreads
4743                   (delq number gnus-newsgroup-unreads))
4744             (if gnus-newsgroup-auto-expire
4745                 (setq gnus-newsgroup-expirable
4746                       (gnus-add-to-sorted-list
4747                        gnus-newsgroup-expirable number))
4748               (push (cons number gnus-low-score-mark)
4749                     gnus-newsgroup-reads))))
4750
4751           (when gnus-tmp-header
4752             ;; We may have an old dummy line to output before this
4753             ;; article.
4754             (when (and gnus-tmp-dummy-line
4755                        (gnus-subject-equal
4756                         gnus-tmp-dummy-line
4757                         (mail-header-subject gnus-tmp-header)))
4758               (gnus-summary-insert-dummy-line
4759                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4760               (setq gnus-tmp-dummy-line nil))
4761
4762             ;; Compute the mark.
4763             (setq gnus-tmp-unread (gnus-article-mark number))
4764
4765             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4766                                   gnus-tmp-header gnus-tmp-level)
4767                   gnus-newsgroup-data)
4768
4769             ;; Actually insert the line.
4770             (setq
4771              gnus-tmp-subject-or-nil
4772              (cond
4773               ((and gnus-thread-ignore-subject
4774                     gnus-tmp-prev-subject
4775                     (not (string= gnus-tmp-prev-subject simp-subject)))
4776                subject)
4777               ((zerop gnus-tmp-level)
4778                (if (and (eq gnus-summary-make-false-root 'empty)
4779                         (memq number gnus-tmp-gathered)
4780                         gnus-tmp-prev-subject
4781                         (string= gnus-tmp-prev-subject simp-subject))
4782                    gnus-summary-same-subject
4783                  subject))
4784               (t gnus-summary-same-subject)))
4785             (if (and (eq gnus-summary-make-false-root 'adopt)
4786                      (= gnus-tmp-level 1)
4787                      (memq number gnus-tmp-gathered))
4788                 (setq gnus-tmp-opening-bracket ?\<
4789                       gnus-tmp-closing-bracket ?\>)
4790               (setq gnus-tmp-opening-bracket ?\[
4791                     gnus-tmp-closing-bracket ?\]))
4792             (setq
4793              gnus-tmp-indentation
4794              (aref gnus-thread-indent-array gnus-tmp-level)
4795              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4796              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4797                                 gnus-summary-default-score 0)
4798              gnus-tmp-score-char
4799              (if (or (null gnus-summary-default-score)
4800                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4801                          gnus-summary-zcore-fuzz))
4802                  ?\ ;;;Whitespace
4803                (if (< gnus-tmp-score gnus-summary-default-score)
4804                    gnus-score-below-mark gnus-score-over-mark))
4805              gnus-tmp-replied
4806              (cond ((memq number gnus-newsgroup-processable)
4807                     gnus-process-mark)
4808                    ((memq number gnus-newsgroup-cached)
4809                     gnus-cached-mark)
4810                    ((memq number gnus-newsgroup-replied)
4811                     gnus-replied-mark)
4812                    ((memq number gnus-newsgroup-forwarded)
4813                     gnus-forwarded-mark)
4814                    ((memq number gnus-newsgroup-saved)
4815                     gnus-saved-mark)
4816                    ((memq number gnus-newsgroup-recent)
4817                     gnus-recent-mark)
4818                    ((memq number gnus-newsgroup-unseen)
4819                     gnus-unseen-mark)
4820                    (t gnus-no-mark))
4821              gnus-tmp-downloaded
4822              (cond ((memq number gnus-newsgroup-undownloaded) 
4823                     gnus-undownloaded-mark)
4824                    (gnus-newsgroup-agentized
4825                     gnus-downloaded-mark)
4826                    (t
4827                     gnus-no-mark))
4828              gnus-tmp-from (mail-header-from gnus-tmp-header)
4829              gnus-tmp-name
4830              (cond
4831               ((string-match "<[^>]+> *$" gnus-tmp-from)
4832                (setq beg-match (match-beginning 0))
4833                (or (and (string-match "^\".+\"" gnus-tmp-from)
4834                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4835                    (substring gnus-tmp-from 0 beg-match)))
4836               ((string-match "(.+)" gnus-tmp-from)
4837                (substring gnus-tmp-from
4838                           (1+ (match-beginning 0)) (1- (match-end 0))))
4839               (t gnus-tmp-from))
4840
4841              ;; Do the %B string
4842              gnus-tmp-thread-tree-header-string
4843              (cond
4844               ((not gnus-show-threads) "")
4845               ((zerop gnus-tmp-level)
4846                (cond ((cdar thread)
4847                       (or gnus-sum-thread-tree-root subject))
4848                      (gnus-tmp-new-adopts
4849                       (or gnus-sum-thread-tree-false-root subject))
4850                      (t
4851                       (or gnus-sum-thread-tree-single-indent subject))))
4852               (t
4853                (concat (apply 'concat
4854                               (mapcar (lambda (item)
4855                                         (if (= item 1)
4856                                             gnus-sum-thread-tree-vertical
4857                                           gnus-sum-thread-tree-indent))
4858                                       (cdr (reverse tree-stack))))
4859                        (if (nth 1 thread)
4860                            gnus-sum-thread-tree-leaf-with-other
4861                          gnus-sum-thread-tree-single-leaf)))))
4862             (when (string= gnus-tmp-name "")
4863               (setq gnus-tmp-name gnus-tmp-from))
4864             (unless (numberp gnus-tmp-lines)
4865               (setq gnus-tmp-lines -1))
4866             (if (= gnus-tmp-lines -1)
4867                 (setq gnus-tmp-lines "?")
4868               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4869               (gnus-put-text-property
4870              (point)
4871              (progn (eval gnus-summary-line-format-spec) (point))
4872                'gnus-number number)
4873             (when gnus-visual-p
4874               (forward-line -1)
4875               (gnus-run-hooks 'gnus-summary-update-hook)
4876               (forward-line 1))
4877
4878             (setq gnus-tmp-prev-subject simp-subject)))
4879
4880         (when (nth 1 thread)
4881           (push (list (max 0 gnus-tmp-level)
4882                       (copy-sequence tree-stack)
4883                       (nthcdr 1 thread))
4884                 stack))
4885         (push (if (nth 1 thread) 1 0) tree-stack)
4886         (incf gnus-tmp-level)
4887         (setq threads (if thread-end nil (cdar thread)))
4888         (if gnus-summary-display-while-building
4889             (if building-count
4890                 (progn
4891                   ;; use a set frequency
4892                   (setq building-line-count (1- building-line-count))
4893                   (when (= building-line-count 0)
4894                     (sit-for 0)
4895                     (setq building-line-count
4896                           gnus-summary-display-while-building)))
4897               ;; always
4898               (sit-for 0)))
4899         (unless threads
4900           (setq gnus-tmp-level 0)))))
4901   (gnus-message 7 "Generating summary...done"))
4902
4903 (defun gnus-summary-prepare-unthreaded (headers)
4904   "Generate an unthreaded summary buffer based on HEADERS."
4905   (let (header number mark)
4906
4907     (beginning-of-line)
4908
4909     (while headers
4910       ;; We may have to root out some bad articles...
4911       (when (memq (setq number (mail-header-number
4912                                 (setq header (pop headers))))
4913                   gnus-newsgroup-limit)
4914         ;; Mark article as read when it has a low score.
4915         (when (and gnus-summary-mark-below
4916                    (< (or (cdr (assq number gnus-newsgroup-scored))
4917                           gnus-summary-default-score 0)
4918                       gnus-summary-mark-below)
4919                    (not (gnus-summary-article-ancient-p number)))
4920           (setq gnus-newsgroup-unreads
4921                 (delq number gnus-newsgroup-unreads))
4922           (if gnus-newsgroup-auto-expire
4923               (push number gnus-newsgroup-expirable)
4924             (push (cons number gnus-low-score-mark)
4925                   gnus-newsgroup-reads)))
4926
4927         (setq mark (gnus-article-mark number))
4928         (push (gnus-data-make number mark (1+ (point)) header 0)
4929               gnus-newsgroup-data)
4930         (gnus-summary-insert-line
4931          header 0 number
4932          (memq number gnus-newsgroup-undownloaded)
4933          mark (memq number gnus-newsgroup-replied)
4934          (memq number gnus-newsgroup-expirable)
4935          (mail-header-subject header) nil
4936          (cdr (assq number gnus-newsgroup-scored))
4937          (memq number gnus-newsgroup-processable))))))
4938
4939 (defun gnus-summary-remove-list-identifiers ()
4940   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4941   (let ((regexp (if (consp gnus-list-identifiers)
4942                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4943                   gnus-list-identifiers))
4944         changed subject)
4945     (when regexp
4946       (dolist (header gnus-newsgroup-headers)
4947         (setq subject (mail-header-subject header)
4948               changed nil)
4949         (while (string-match
4950                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4951                 subject)
4952           (setq subject
4953                 (concat (substring subject 0 (match-beginning 2))
4954                         (substring subject (match-end 0)))
4955                 changed t))
4956         (when (and changed
4957                    (string-match
4958                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4959           (setq subject
4960                 (concat (substring subject 0 (match-beginning 1))
4961                         (substring subject (match-end 1)))))
4962         (when changed
4963           (mail-header-set-subject header subject))))))
4964
4965 (defun gnus-fetch-headers (articles)
4966   "Fetch headers of ARTICLES."
4967   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4968     (gnus-message 5 "Fetching headers for %s..." name)
4969     (prog1
4970         (if (eq 'nov
4971                 (setq gnus-headers-retrieved-by
4972                       (gnus-retrieve-headers
4973                        articles gnus-newsgroup-name
4974                        ;; We might want to fetch old headers, but
4975                        ;; not if there is only 1 article.
4976                        (and (or (and
4977                                  (not (eq gnus-fetch-old-headers 'some))
4978                                  (not (numberp gnus-fetch-old-headers)))
4979                                 (> (length articles) 1))
4980                             gnus-fetch-old-headers))))
4981             (gnus-get-newsgroup-headers-xover
4982              articles nil nil gnus-newsgroup-name t)
4983           (gnus-get-newsgroup-headers))
4984       (gnus-message 5 "Fetching headers for %s...done" name))))
4985
4986 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4987   "Select newsgroup GROUP.
4988 If READ-ALL is non-nil, all articles in the group are selected.
4989 If SELECT-ARTICLES, only select those articles from GROUP."
4990   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4991          ;;!!! Dirty hack; should be removed.
4992          (gnus-summary-ignore-duplicates
4993           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4994               t
4995             gnus-summary-ignore-duplicates))
4996          (info (nth 2 entry))
4997          articles fetched-articles cached)
4998
4999     (unless (gnus-check-server
5000              (set (make-local-variable 'gnus-current-select-method)
5001                   (gnus-find-method-for-group group)))
5002       (error "Couldn't open server"))
5003
5004     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5005         (gnus-activate-group group)     ; Or we can activate it...
5006         (progn                          ; Or we bug out.
5007           (when (equal major-mode 'gnus-summary-mode)
5008             (gnus-kill-buffer (current-buffer)))
5009           (error "Couldn't activate group %s: %s"
5010                  group (gnus-status-message group))))
5011
5012     (unless (gnus-request-group group t)
5013       (when (equal major-mode 'gnus-summary-mode)
5014         (gnus-kill-buffer (current-buffer)))
5015       (error "Couldn't request group %s: %s"
5016              group (gnus-status-message group)))
5017
5018     (when gnus-agent
5019       ;; The agent may be storing articles that are no longer in the
5020       ;; server's active range.  If that is the case, the active range
5021       ;; needs to be expanded such that the agent's articles can be
5022       ;; included in the summary.
5023       (let* ((gnus-command-method (gnus-find-method-for-group group))
5024              (alist (gnus-agent-load-alist group))
5025              (active (gnus-active group)))
5026         (if (and (car alist)
5027                  (< (caar alist) (car active)))
5028             (gnus-set-active group (cons (caar alist) (cdr active))))))
5029
5030     (setq gnus-newsgroup-name group
5031           gnus-newsgroup-unselected nil
5032           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5033
5034     (let ((display (gnus-group-find-parameter group 'display)))
5035       (setq gnus-newsgroup-display
5036             (cond
5037              ((not (zerop (or (car-safe read-all) 0)))
5038               ;; The user entered the group with C-u SPC/RET, let's show
5039               ;; all articles.
5040               'gnus-not-ignore)
5041              ((eq display 'all)
5042               'gnus-not-ignore)
5043              ((arrayp display)
5044               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5045              ((numberp display)
5046               ;; The following is probably the "correct" solution, but
5047               ;; it makes Gnus fetch all headers and then limit the
5048               ;; articles (which is slow), so instead we hack the
5049               ;; select-articles parameter instead. -- Simon Josefsson
5050               ;; <jas@kth.se>
5051               ;;
5052               ;; (gnus-byte-compile
5053               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5054               ;;                         display)))))
5055               (setq select-articles
5056                     (gnus-uncompress-range
5057                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5058                              (if (> tmp 0)
5059                                  tmp
5060                                1))
5061                            (cdr (gnus-active group)))))
5062               nil)
5063              (t
5064               nil))))
5065
5066     (gnus-summary-setup-default-charset)
5067
5068     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5069     (when (gnus-virtual-group-p group)
5070       (setq cached gnus-newsgroup-cached))
5071
5072     (setq gnus-newsgroup-unreads
5073           (gnus-sorted-ndifference
5074            (gnus-sorted-ndifference gnus-newsgroup-unreads
5075                                     gnus-newsgroup-marked)
5076            gnus-newsgroup-dormant))
5077
5078     (setq gnus-newsgroup-processable nil)
5079
5080     (gnus-update-read-articles group gnus-newsgroup-unreads)
5081
5082     ;; Adjust and set lists of article marks.
5083     (when info
5084       (gnus-adjust-marked-articles info))
5085     (if (setq articles select-articles)
5086         (setq gnus-newsgroup-unselected
5087               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5088       (setq articles (gnus-articles-to-read group read-all)))
5089
5090     (cond
5091      ((null articles)
5092       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5093       'quit)
5094      ((eq articles 0) nil)
5095      (t
5096       ;; Init the dependencies hash table.
5097       (setq gnus-newsgroup-dependencies
5098             (gnus-make-hashtable (length articles)))
5099       (gnus-set-global-variables)
5100       ;; Retrieve the headers and read them in.
5101
5102       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5103
5104       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5105       (when cached
5106         (setq gnus-newsgroup-cached cached))
5107
5108       ;; Suppress duplicates?
5109       (when gnus-suppress-duplicates
5110         (gnus-dup-suppress-articles))
5111
5112       ;; Set the initial limit.
5113       (setq gnus-newsgroup-limit (copy-sequence articles))
5114       ;; Remove canceled articles from the list of unread articles.
5115       (setq fetched-articles
5116             (mapcar (lambda (headers) (mail-header-number headers))
5117                     gnus-newsgroup-headers))
5118       (setq gnus-newsgroup-articles fetched-articles)
5119       (setq gnus-newsgroup-unreads
5120             (gnus-sorted-nintersection
5121              gnus-newsgroup-unreads fetched-articles))
5122       (gnus-compute-unseen-list)
5123
5124       ;; Removed marked articles that do not exist.
5125       (gnus-update-missing-marks
5126        (gnus-sorted-difference articles fetched-articles))
5127       ;; We might want to build some more threads first.
5128       (when (and gnus-fetch-old-headers
5129                  (eq gnus-headers-retrieved-by 'nov))
5130         (if (eq gnus-fetch-old-headers 'invisible)
5131             (gnus-build-all-threads)
5132           (gnus-build-old-threads)))
5133       ;; Let the Gnus agent mark articles as read.
5134       (when gnus-agent
5135         (gnus-agent-get-undownloaded-list))
5136       ;; Remove list identifiers from subject
5137       (when gnus-list-identifiers
5138         (gnus-summary-remove-list-identifiers))
5139       ;; Check whether auto-expire is to be done in this group.
5140       (setq gnus-newsgroup-auto-expire
5141             (gnus-group-auto-expirable-p group))
5142       ;; Set up the article buffer now, if necessary.
5143       (unless gnus-single-article-buffer
5144         (gnus-article-setup-buffer))
5145       ;; First and last article in this newsgroup.
5146       (when gnus-newsgroup-headers
5147         (setq gnus-newsgroup-begin
5148               (mail-header-number (car gnus-newsgroup-headers))
5149               gnus-newsgroup-end
5150               (mail-header-number
5151                (gnus-last-element gnus-newsgroup-headers))))
5152       ;; GROUP is successfully selected.
5153       (or gnus-newsgroup-headers t)))))
5154
5155 (defun gnus-compute-unseen-list ()
5156   ;; The `seen' marks are treated specially.
5157   (if (not gnus-newsgroup-seen)
5158       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5159     (setq gnus-newsgroup-unseen
5160           (gnus-inverse-list-range-intersection
5161            gnus-newsgroup-articles gnus-newsgroup-seen))))
5162
5163 (defun gnus-summary-display-make-predicate (display)
5164   (require 'gnus-agent)
5165   (when (= (length display) 1)
5166     (setq display (car display)))
5167   (unless gnus-summary-display-cache
5168     (dolist (elem (append '((unread . unread)
5169                             (read . read)
5170                             (unseen . unseen))
5171                           gnus-article-mark-lists))
5172       (push (cons (cdr elem)
5173                   (gnus-byte-compile
5174                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5175             gnus-summary-display-cache)))
5176   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5177         (gnus-category-predicate-cache gnus-summary-display-cache))
5178     (gnus-get-predicate display)))
5179
5180 ;; Uses the dynamically bound `number' variable.
5181 (eval-when-compile
5182   (defvar number))
5183 (defun gnus-article-marked-p (type &optional article)
5184   (let ((article (or article number)))
5185     (cond
5186      ((eq type 'tick)
5187       (memq article gnus-newsgroup-marked))
5188      ((eq type 'spam)
5189       (memq article gnus-newsgroup-spam-marked))
5190      ((eq type 'unsend)
5191       (memq article gnus-newsgroup-unsendable))
5192      ((eq type 'undownload)
5193       (memq article gnus-newsgroup-undownloaded))
5194      ((eq type 'download)
5195       (memq article gnus-newsgroup-downloadable))
5196      ((eq type 'unread)
5197       (memq article gnus-newsgroup-unreads))
5198      ((eq type 'read)
5199       (memq article gnus-newsgroup-reads))
5200      ((eq type 'dormant)
5201       (memq article gnus-newsgroup-dormant) )
5202      ((eq type 'expire)
5203       (memq article gnus-newsgroup-expirable))
5204      ((eq type 'reply)
5205       (memq article gnus-newsgroup-replied))
5206      ((eq type 'killed)
5207       (memq article gnus-newsgroup-killed))
5208      ((eq type 'bookmark)
5209       (assq article gnus-newsgroup-bookmarks))
5210      ((eq type 'score)
5211       (assq article gnus-newsgroup-scored))
5212      ((eq type 'save)
5213       (memq article gnus-newsgroup-saved))
5214      ((eq type 'cache)
5215       (memq article gnus-newsgroup-cached))
5216      ((eq type 'forward)
5217       (memq article gnus-newsgroup-forwarded))
5218      ((eq type 'seen)
5219       (not (memq article gnus-newsgroup-unseen)))
5220      ((eq type 'recent)
5221       (memq article gnus-newsgroup-recent))
5222      (t t))))
5223
5224 (defun gnus-articles-to-read (group &optional read-all)
5225   "Find out what articles the user wants to read."
5226   (let* ((display (gnus-group-find-parameter group 'display))
5227          (articles
5228           ;; Select all articles if `read-all' is non-nil, or if there
5229           ;; are no unread articles.
5230           (if (or read-all
5231                   (and (zerop (length gnus-newsgroup-marked))
5232                        (zerop (length gnus-newsgroup-unreads)))
5233                   ;; Fetch all if the predicate is non-nil.
5234                   gnus-newsgroup-display)
5235               ;; We want to select the headers for all the articles in
5236               ;; the group, so we select either all the active
5237               ;; articles in the group, or (if that's nil), the
5238               ;; articles in the cache.
5239               (or
5240                (gnus-uncompress-range (gnus-active group))
5241                (gnus-cache-articles-in-group group))
5242             ;; Select only the "normal" subset of articles.
5243             (gnus-sorted-nunion
5244              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5245              gnus-newsgroup-unreads)))
5246          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5247          (scored (length scored-list))
5248          (number (length articles))
5249          (marked (+ (length gnus-newsgroup-marked)
5250                     (length gnus-newsgroup-dormant)))
5251          (select
5252           (cond
5253            ((numberp read-all)
5254             read-all)
5255            ((numberp gnus-newsgroup-display)
5256             gnus-newsgroup-display)
5257            (t
5258             (condition-case ()
5259                 (cond
5260                  ((and (or (<= scored marked) (= scored number))
5261                        (numberp gnus-large-newsgroup)
5262                        (> number gnus-large-newsgroup))
5263                   (let* ((cursor-in-echo-area nil)
5264                          (initial (gnus-parameter-large-newsgroup-initial
5265                                    gnus-newsgroup-name))
5266                          (input
5267                           (read-string
5268                            (format
5269                             "How many articles from %s (%s %d): "
5270                             (gnus-limit-string
5271                              (gnus-group-decoded-name gnus-newsgroup-name)
5272                              35)
5273                             (if initial "max" "default")
5274                             number)
5275                            (if initial
5276                                (cons (number-to-string initial)
5277                                      0)))))
5278                     (if (string-match "^[ \t]*$" input) number input)))
5279                  ((and (> scored marked) (< scored number)
5280                        (> (- scored number) 20))
5281                   (let ((input
5282                          (read-string
5283                           (format "%s %s (%d scored, %d total): "
5284                                   "How many articles from"
5285                                   (gnus-group-decoded-name group)
5286                                   scored number))))
5287                     (if (string-match "^[ \t]*$" input)
5288                         number input)))
5289                  (t number))
5290               (quit
5291                (message "Quit getting the articles to read")
5292                nil))))))
5293     (setq select (if (stringp select) (string-to-number select) select))
5294     (if (or (null select) (zerop select))
5295         select
5296       (if (and (not (zerop scored)) (<= (abs select) scored))
5297           (progn
5298             (setq articles (sort scored-list '<))
5299             (setq number (length articles)))
5300         (setq articles (copy-sequence articles)))
5301
5302       (when (< (abs select) number)
5303         (if (< select 0)
5304             ;; Select the N oldest articles.
5305             (setcdr (nthcdr (1- (abs select)) articles) nil)
5306           ;; Select the N most recent articles.
5307           (setq articles (nthcdr (- number select) articles))))
5308       (setq gnus-newsgroup-unselected
5309             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5310       (when gnus-alter-articles-to-read-function
5311         (setq articles
5312               (sort
5313                (funcall gnus-alter-articles-to-read-function
5314                         gnus-newsgroup-name articles)
5315                '<)))
5316       articles)))
5317
5318 (defun gnus-killed-articles (killed articles)
5319   (let (out)
5320     (while articles
5321       (when (inline (gnus-member-of-range (car articles) killed))
5322         (push (car articles) out))
5323       (setq articles (cdr articles)))
5324     out))
5325
5326 (defun gnus-uncompress-marks (marks)
5327   "Uncompress the mark ranges in MARKS."
5328   (let ((uncompressed '(score bookmark))
5329         out)
5330     (while marks
5331       (if (memq (caar marks) uncompressed)
5332           (push (car marks) out)
5333         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5334       (setq marks (cdr marks)))
5335     out))
5336
5337 (defun gnus-article-mark-to-type (mark)
5338   "Return the type of MARK."
5339   (or (cadr (assq mark gnus-article-special-mark-lists))
5340       'list))
5341
5342 (defun gnus-article-unpropagatable-p (mark)
5343   "Return whether MARK should be propagated to backend."
5344   (memq mark gnus-article-unpropagated-mark-lists))
5345
5346 (defun gnus-adjust-marked-articles (info)
5347   "Set all article lists and remove all marks that are no longer valid."
5348   (let* ((marked-lists (gnus-info-marks info))
5349          (active (gnus-active (gnus-info-group info)))
5350          (min (car active))
5351          (max (cdr active))
5352          (types gnus-article-mark-lists)
5353          marks var articles article mark mark-type)
5354
5355     (dolist (marks marked-lists)
5356       (setq mark (car marks)
5357             mark-type (gnus-article-mark-to-type mark)
5358             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5359
5360       ;; We set the variable according to the type of the marks list,
5361       ;; and then adjust the marks to a subset of the active articles.
5362       (cond
5363        ;; Adjust "simple" lists.
5364        ((eq mark-type 'list)
5365         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5366         (when (memq mark '(tick dormant expire reply save))
5367           (while articles
5368             (when (or (< (setq article (pop articles)) min) (> article max))
5369               (set var (delq article (symbol-value var)))))))
5370        ;; Adjust assocs.
5371        ((eq mark-type 'tuple)
5372         (set var (setq articles (cdr marks)))
5373         (when (not (listp (cdr (symbol-value var))))
5374           (set var (list (symbol-value var))))
5375         (when (not (listp (cdr articles)))
5376           (setq articles (list articles)))
5377         (while articles
5378           (when (or (not (consp (setq article (pop articles))))
5379                     (< (car article) min)
5380                     (> (car article) max))
5381             (set var (delq article (symbol-value var))))))
5382        ;; Adjust ranges (sloppily).
5383        ((eq mark-type 'range)
5384         (cond
5385          ((eq mark 'seen)
5386           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5387           ;; It should be (seen (NUM1 . NUM2)).
5388           (when (numberp (cddr marks))
5389             (setcdr marks (list (cdr marks))))
5390           (setq articles (cdr marks))
5391           (while (and articles
5392                       (or (and (consp (car articles))
5393                                (> min (cdar articles)))
5394                           (and (numberp (car articles))
5395                                (> min (car articles)))))
5396             (pop articles))
5397           (set var articles))))))))
5398
5399 (defun gnus-update-missing-marks (missing)
5400   "Go through the list of MISSING articles and remove them from the mark lists."
5401   (when missing
5402     (let (var m)
5403       ;; Go through all types.
5404       (dolist (elem gnus-article-mark-lists)
5405         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5406           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5407           (when (symbol-value var)
5408             ;; This list has articles.  So we delete all missing
5409             ;; articles from it.
5410             (setq m missing)
5411             (while m
5412               (set var (delq (pop m) (symbol-value var))))))))))
5413
5414 (defun gnus-update-marks ()
5415   "Enter the various lists of marked articles into the newsgroup info list."
5416   (let ((types gnus-article-mark-lists)
5417         (info (gnus-get-info gnus-newsgroup-name))
5418         type list newmarked symbol delta-marks)
5419     (when info
5420       ;; Add all marks lists to the list of marks lists.
5421       (while (setq type (pop types))
5422         (setq list (symbol-value
5423                     (setq symbol
5424                           (intern (format "gnus-newsgroup-%s" (car type))))))
5425
5426         (when list
5427           ;; Get rid of the entries of the articles that have the
5428           ;; default score.
5429           (when (and (eq (cdr type) 'score)
5430                      gnus-save-score
5431                      list)
5432             (let* ((arts list)
5433                    (prev (cons nil list))
5434                    (all prev))
5435               (while arts
5436                 (if (or (not (consp (car arts)))
5437                         (= (cdar arts) gnus-summary-default-score))
5438                     (setcdr prev (cdr arts))
5439                   (setq prev arts))
5440                 (setq arts (cdr arts)))
5441               (setq list (cdr all)))))
5442
5443         (when (eq (cdr type) 'seen)
5444           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5445
5446         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5447           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5448
5449         (when (and (gnus-check-backend-function
5450                     'request-set-mark gnus-newsgroup-name)
5451                    (not (gnus-article-unpropagatable-p (cdr type))))
5452           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5453                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5454                  (add (gnus-remove-from-range
5455                        (gnus-copy-sequence list) old)))
5456             (when add
5457               (push (list add 'add (list (cdr type))) delta-marks))
5458             (when del
5459               (push (list del 'del (list (cdr type))) delta-marks))))
5460
5461         (when list
5462           (push (cons (cdr type) list) newmarked)))
5463
5464       (when delta-marks
5465         (unless (gnus-check-group gnus-newsgroup-name)
5466           (error "Can't open server for %s" gnus-newsgroup-name))
5467         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5468
5469       ;; Enter these new marks into the info of the group.
5470       (if (nthcdr 3 info)
5471           (setcar (nthcdr 3 info) newmarked)
5472         ;; Add the marks lists to the end of the info.
5473         (when newmarked
5474           (setcdr (nthcdr 2 info) (list newmarked))))
5475
5476       ;; Cut off the end of the info if there's nothing else there.
5477       (let ((i 5))
5478         (while (and (> i 2)
5479                     (not (nth i info)))
5480           (when (nthcdr (decf i) info)
5481             (setcdr (nthcdr i info) nil)))))))
5482
5483 (defun gnus-set-mode-line (where)
5484   "Set the mode line of the article or summary buffers.
5485 If WHERE is `summary', the summary mode line format will be used."
5486   ;; Is this mode line one we keep updated?
5487   (when (and (memq where gnus-updated-mode-lines)
5488              (symbol-value
5489               (intern (format "gnus-%s-mode-line-format-spec" where))))
5490     (let (mode-string)
5491       (save-excursion
5492         ;; We evaluate this in the summary buffer since these
5493         ;; variables are buffer-local to that buffer.
5494         (set-buffer gnus-summary-buffer)
5495         ;; We bind all these variables that are used in the `eval' form
5496         ;; below.
5497         (let* ((mformat (symbol-value
5498                          (intern
5499                           (format "gnus-%s-mode-line-format-spec" where))))
5500                (gnus-tmp-group-name (gnus-group-decoded-name
5501                                      gnus-newsgroup-name))
5502                (gnus-tmp-article-number (or gnus-current-article 0))
5503                (gnus-tmp-unread gnus-newsgroup-unreads)
5504                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5505                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5506                (gnus-tmp-unread-and-unselected
5507                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5508                             (zerop gnus-tmp-unselected))
5509                        "")
5510                       ((zerop gnus-tmp-unselected)
5511                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5512                       (t (format "{%d(+%d) more}"
5513                                  gnus-tmp-unread-and-unticked
5514                                  gnus-tmp-unselected))))
5515                (gnus-tmp-subject
5516                 (if (and gnus-current-headers
5517                          (vectorp gnus-current-headers))
5518                     (gnus-mode-string-quote
5519                      (mail-header-subject gnus-current-headers))
5520                   ""))
5521                bufname-length max-len
5522                gnus-tmp-header);; passed as argument to any user-format-funcs
5523           (setq mode-string (eval mformat))
5524           (setq bufname-length (if (string-match "%b" mode-string)
5525                                    (- (length
5526                                        (buffer-name
5527                                         (if (eq where 'summary)
5528                                             nil
5529                                           (get-buffer gnus-article-buffer))))
5530                                       2)
5531                                  0))
5532           (setq max-len (max 4 (if gnus-mode-non-string-length
5533                                    (- (window-width)
5534                                       gnus-mode-non-string-length
5535                                       bufname-length)
5536                                  (length mode-string))))
5537           ;; We might have to chop a bit of the string off...
5538           (when (> (length mode-string) max-len)
5539             (setq mode-string
5540                   (concat (gnus-truncate-string mode-string (- max-len 3))
5541                           "...")))
5542           ;; Pad the mode string a bit.
5543           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5544       ;; Update the mode line.
5545       (setq mode-line-buffer-identification
5546             (gnus-mode-line-buffer-identification (list mode-string)))
5547       (set-buffer-modified-p t))))
5548
5549 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5550   "Go through the HEADERS list and add all Xrefs to a hash table.
5551 The resulting hash table is returned, or nil if no Xrefs were found."
5552   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5553          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5554          (xref-hashtb (gnus-make-hashtable))
5555          start group entry number xrefs header)
5556     (while headers
5557       (setq header (pop headers))
5558       (when (and (setq xrefs (mail-header-xref header))
5559                  (not (memq (setq number (mail-header-number header))
5560                             unreads)))
5561         (setq start 0)
5562         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5563           (setq start (match-end 0))
5564           (setq group (if prefix
5565                           (concat prefix (substring xrefs (match-beginning 1)
5566                                                     (match-end 1)))
5567                         (substring xrefs (match-beginning 1) (match-end 1))))
5568           (setq number
5569                 (string-to-int (substring xrefs (match-beginning 2)
5570                                           (match-end 2))))
5571           (if (setq entry (gnus-gethash group xref-hashtb))
5572               (setcdr entry (cons number (cdr entry)))
5573             (gnus-sethash group (cons number nil) xref-hashtb)))))
5574     (and start xref-hashtb)))
5575
5576 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5577   "Look through all the headers and mark the Xrefs as read."
5578   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5579         name entry info xref-hashtb idlist method nth4)
5580     (save-excursion
5581       (set-buffer gnus-group-buffer)
5582       (when (setq xref-hashtb
5583                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5584         (mapatoms
5585          (lambda (group)
5586            (unless (string= from-newsgroup (setq name (symbol-name group)))
5587              (setq idlist (symbol-value group))
5588              ;; Dead groups are not updated.
5589              (and (prog1
5590                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5591                             info (nth 2 entry))
5592                     (when (stringp (setq nth4 (gnus-info-method info)))
5593                       (setq nth4 (gnus-server-to-method nth4))))
5594                   ;; Only do the xrefs if the group has the same
5595                   ;; select method as the group we have just read.
5596                   (or (gnus-methods-equal-p
5597                        nth4 (gnus-find-method-for-group from-newsgroup))
5598                       virtual
5599                       (equal nth4 (setq method (gnus-find-method-for-group
5600                                                 from-newsgroup)))
5601                       (and (equal (car nth4) (car method))
5602                            (equal (nth 1 nth4) (nth 1 method))))
5603                   gnus-use-cross-reference
5604                   (or (not (eq gnus-use-cross-reference t))
5605                       virtual
5606                       ;; Only do cross-references on subscribed
5607                       ;; groups, if that is what is wanted.
5608                       (<= (gnus-info-level info) gnus-level-subscribed))
5609                   (gnus-group-make-articles-read name idlist))))
5610          xref-hashtb)))))
5611
5612 (defun gnus-compute-read-articles (group articles)
5613   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5614          (info (nth 2 entry))
5615          (active (gnus-active group))
5616          ninfo)
5617     (when entry
5618       ;; First peel off all invalid article numbers.
5619       (when active
5620         (let ((ids articles)
5621               id first)
5622           (while (setq id (pop ids))
5623             (when (and first (> id (cdr active)))
5624               ;; We'll end up in this situation in one particular
5625               ;; obscure situation.  If you re-scan a group and get
5626               ;; a new article that is cross-posted to a different
5627               ;; group that has not been re-scanned, you might get
5628               ;; crossposted article that has a higher number than
5629               ;; Gnus believes possible.  So we re-activate this
5630               ;; group as well.  This might mean doing the
5631               ;; crossposting thingy will *increase* the number
5632               ;; of articles in some groups.  Tsk, tsk.
5633               (setq active (or (gnus-activate-group group) active)))
5634             (when (or (> id (cdr active))
5635                       (< id (car active)))
5636               (setq articles (delq id articles))))))
5637       ;; If the read list is nil, we init it.
5638       (if (and active
5639                (null (gnus-info-read info))
5640                (> (car active) 1))
5641           (setq ninfo (cons 1 (1- (car active))))
5642         (setq ninfo (gnus-info-read info)))
5643       ;; Then we add the read articles to the range.
5644       (gnus-add-to-range
5645        ninfo (setq articles (sort articles '<))))))
5646
5647 (defun gnus-group-make-articles-read (group articles)
5648   "Update the info of GROUP to say that ARTICLES are read."
5649   (let* ((num 0)
5650          (entry (gnus-gethash group gnus-newsrc-hashtb))
5651          (info (nth 2 entry))
5652          (active (gnus-active group))
5653          range)
5654     (when entry
5655       (setq range (gnus-compute-read-articles group articles))
5656       (save-excursion
5657         (set-buffer gnus-group-buffer)
5658         (gnus-undo-register
5659           `(progn
5660              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5661              (gnus-info-set-read ',info ',(gnus-info-read info))
5662              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5663              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5664              (gnus-group-update-group ,group t))))
5665       ;; Add the read articles to the range.
5666       (gnus-info-set-read info range)
5667       (gnus-request-set-mark group (list (list range 'add '(read))))
5668       ;; Then we have to re-compute how many unread
5669       ;; articles there are in this group.
5670       (when active
5671         (cond
5672          ((not range)
5673           (setq num (- (1+ (cdr active)) (car active))))
5674          ((not (listp (cdr range)))
5675           (setq num (- (cdr active) (- (1+ (cdr range))
5676                                        (car range)))))
5677          (t
5678           (while range
5679             (if (numberp (car range))
5680                 (setq num (1+ num))
5681               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5682             (setq range (cdr range)))
5683           (setq num (- (cdr active) num))))
5684         ;; Update the number of unread articles.
5685         (setcar entry num)
5686         ;; Update the group buffer.
5687         (unless (gnus-ephemeral-group-p group)
5688           (gnus-group-update-group group t))))))
5689
5690 (defvar gnus-newsgroup-none-id 0)
5691
5692 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5693   (let ((cur nntp-server-buffer)
5694         (dependencies
5695          (or dependencies
5696              (save-excursion (set-buffer gnus-summary-buffer)
5697                              gnus-newsgroup-dependencies)))
5698         headers id end ref
5699         (mail-parse-charset gnus-newsgroup-charset)
5700         (mail-parse-ignored-charsets
5701          (save-excursion (condition-case nil
5702                              (set-buffer gnus-summary-buffer)
5703                            (error))
5704                          gnus-newsgroup-ignored-charsets)))
5705     (save-excursion
5706       (set-buffer nntp-server-buffer)
5707       ;; Translate all TAB characters into SPACE characters.
5708       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5709       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5710       (gnus-run-hooks 'gnus-parse-headers-hook)
5711       (let ((case-fold-search t)
5712             in-reply-to header p lines chars ctype)
5713         (goto-char (point-min))
5714         ;; Search to the beginning of the next header.  Error messages
5715         ;; do not begin with 2 or 3.
5716         (while (re-search-forward "^[23][0-9]+ " nil t)
5717           (setq id nil
5718                 ref nil)
5719           ;; This implementation of this function, with nine
5720           ;; search-forwards instead of the one re-search-forward and
5721           ;; a case (which basically was the old function) is actually
5722           ;; about twice as fast, even though it looks messier.  You
5723           ;; can't have everything, I guess.  Speed and elegance
5724           ;; doesn't always go hand in hand.
5725           (setq
5726            header
5727            (make-full-mail-header
5728             ;; Number.
5729             (prog1
5730                 (read cur)
5731               (end-of-line)
5732               (setq p (point))
5733               (narrow-to-region (point)
5734                                 (or (and (search-forward "\n.\n" nil t)
5735                                          (- (point) 2))
5736                                     (point))))
5737             ;; Subject.
5738             (progn
5739               (goto-char p)
5740               (if (search-forward "\nsubject:" nil t)
5741                   (nnheader-header-value)
5742                 "(none)"))
5743             ;; From.
5744             (progn
5745               (goto-char p)
5746               (if (search-forward "\nfrom:" nil t)
5747                   (nnheader-header-value)
5748                 "(nobody)"))
5749             ;; Date.
5750             (progn
5751               (goto-char p)
5752               (if (search-forward "\ndate:" nil t)
5753                   (nnheader-header-value) ""))
5754             ;; Message-ID.
5755             (progn
5756               (goto-char p)
5757               (setq id (if (re-search-forward
5758                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5759                            ;; We do it this way to make sure the Message-ID
5760                            ;; is (somewhat) syntactically valid.
5761                            (buffer-substring (match-beginning 1)
5762                                              (match-end 1))
5763                          ;; If there was no message-id, we just fake one
5764                          ;; to make subsequent routines simpler.
5765                          (nnheader-generate-fake-message-id))))
5766             ;; References.
5767             (progn
5768               (goto-char p)
5769               (if (search-forward "\nreferences:" nil t)
5770                   (progn
5771                     (setq end (point))
5772                     (prog1
5773                         (nnheader-header-value)
5774                       (setq ref
5775                             (buffer-substring
5776                              (progn
5777                                ;; (end-of-line)
5778                                (search-backward ">" end t)
5779                                (1+ (point)))
5780                              (progn
5781                                (search-backward "<" end t)
5782                                (point))))))
5783                 ;; Get the references from the in-reply-to header if there
5784                 ;; were no references and the in-reply-to header looks
5785                 ;; promising.
5786                 (if (and (search-forward "\nin-reply-to:" nil t)
5787                          (setq in-reply-to (nnheader-header-value))
5788                          (string-match "<[^>]+>" in-reply-to))
5789                     (let (ref2)
5790                       (setq ref (substring in-reply-to (match-beginning 0)
5791                                            (match-end 0)))
5792                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5793                         (setq ref2 (substring in-reply-to (match-beginning 0)
5794                                               (match-end 0)))
5795                         (when (> (length ref2) (length ref))
5796                           (setq ref ref2)))
5797                       ref)
5798                   (setq ref nil))))
5799             ;; Chars.
5800             (progn
5801               (goto-char p)
5802               (if (search-forward "\nchars: " nil t)
5803                   (if (numberp (setq chars (ignore-errors (read cur))))
5804                       chars -1)
5805                 -1))
5806             ;; Lines.
5807             (progn
5808               (goto-char p)
5809               (if (search-forward "\nlines: " nil t)
5810                   (if (numberp (setq lines (ignore-errors (read cur))))
5811                       lines -1)
5812                 -1))
5813             ;; Xref.
5814             (progn
5815               (goto-char p)
5816               (and (search-forward "\nxref:" nil t)
5817                    (nnheader-header-value)))
5818             ;; Extra.
5819             (when gnus-extra-headers
5820               (let ((extra gnus-extra-headers)
5821                     out)
5822                 (while extra
5823                   (goto-char p)
5824                   (when (search-forward
5825                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5826                     (push (cons (car extra) (nnheader-header-value)) out))
5827                   (pop extra))
5828                 out))))
5829           (goto-char p)
5830           (if (and (search-forward "\ncontent-type: " nil t)
5831                    (setq ctype (nnheader-header-value)))
5832               (mime-entity-set-content-type-internal
5833                header (mime-parse-Content-Type ctype)))
5834           (when (equal id ref)
5835             (setq ref nil))
5836
5837           (when gnus-alter-header-function
5838             (funcall gnus-alter-header-function header)
5839             (setq id (mail-header-id header)
5840                   ref (gnus-parent-id (mail-header-references header))))
5841
5842           (when (setq header
5843                       (gnus-dependencies-add-header
5844                        header dependencies force-new))
5845             (push header headers))
5846           (goto-char (point-max))
5847           (widen))
5848         (nreverse headers)))))
5849
5850 ;; Goes through the xover lines and returns a list of vectors
5851 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5852                                                   force-new dependencies
5853                                                   group also-fetch-heads)
5854   "Parse the news overview data in the server buffer.
5855 Return a list of headers that match SEQUENCE (see
5856 `nntp-retrieve-headers')."
5857   ;; Get the Xref when the users reads the articles since most/some
5858   ;; NNTP servers do not include Xrefs when using XOVER.
5859   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5860   (let ((mail-parse-charset gnus-newsgroup-charset)
5861         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5862         (cur nntp-server-buffer)
5863         (dependencies (or dependencies gnus-newsgroup-dependencies))
5864         (allp (cond
5865                ((eq gnus-read-all-available-headers t)
5866                 t)
5867                ((stringp gnus-read-all-available-headers)
5868                 (string-match gnus-read-all-available-headers group))
5869                (t
5870                 nil)))
5871         number headers header)
5872     (save-excursion
5873       (set-buffer nntp-server-buffer)
5874       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5875       ;; Allow the user to mangle the headers before parsing them.
5876       (gnus-run-hooks 'gnus-parse-headers-hook)
5877       (goto-char (point-min))
5878       (gnus-parse-without-error
5879         (while (and (or sequence allp)
5880                     (not (eobp)))
5881           (setq number (read cur))
5882           (when (not allp)
5883             (while (and sequence
5884                         (< (car sequence) number))
5885               (setq sequence (cdr sequence))))
5886           (when (and (or allp
5887                          (and sequence
5888                               (eq number (car sequence))))
5889                      (progn
5890                        (setq sequence (cdr sequence))
5891                        (setq header (inline
5892                                       (gnus-nov-parse-line
5893                                        number dependencies force-new)))))
5894             (push header headers))
5895           (forward-line 1)))
5896       ;; A common bug in inn is that if you have posted an article and
5897       ;; then retrieves the active file, it will answer correctly --
5898       ;; the new article is included.  However, a NOV entry for the
5899       ;; article may not have been generated yet, so this may fail.
5900       ;; We work around this problem by retrieving the last few
5901       ;; headers using HEAD.
5902       (if (or (not also-fetch-heads)
5903               (not sequence))
5904           ;; We (probably) got all the headers.
5905           (nreverse headers)
5906         (let ((gnus-nov-is-evil t))
5907           (nconc
5908            (nreverse headers)
5909            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5910              (gnus-get-newsgroup-headers))))))))
5911
5912 (defun gnus-article-get-xrefs ()
5913   "Fill in the Xref value in `gnus-current-headers', if necessary.
5914 This is meant to be called in `gnus-article-internal-prepare-hook'."
5915   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5916                                  gnus-current-headers)))
5917     (or (not gnus-use-cross-reference)
5918         (not headers)
5919         (and (mail-header-xref headers)
5920              (not (string= (mail-header-xref headers) "")))
5921         (let ((case-fold-search t)
5922               xref)
5923           (save-restriction
5924             (nnheader-narrow-to-headers)
5925             (goto-char (point-min))
5926             (when (or (and (not (eobp))
5927                            (eq (downcase (char-after)) ?x)
5928                            (looking-at "Xref:"))
5929                       (search-forward "\nXref:" nil t))
5930               (goto-char (1+ (match-end 0)))
5931               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5932               (mail-header-set-xref headers xref)))))))
5933
5934 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5935   "Find article ID and insert the summary line for that article.
5936 OLD-HEADER can either be a header or a line number to insert
5937 the subject line on."
5938   (let* ((line (and (numberp old-header) old-header))
5939          (old-header (and (vectorp old-header) old-header))
5940          (header (cond ((and old-header use-old-header)
5941                         old-header)
5942                        ((and (numberp id)
5943                              (gnus-number-to-header id))
5944                         (gnus-number-to-header id))
5945                        (t
5946                         (gnus-read-header id))))
5947          (number (and (numberp id) id))
5948          d)
5949     (when header
5950       ;; Rebuild the thread that this article is part of and go to the
5951       ;; article we have fetched.
5952       (when (and (not gnus-show-threads)
5953                  old-header)
5954         (when (and number
5955                    (setq d (gnus-data-find (mail-header-number old-header))))
5956           (goto-char (gnus-data-pos d))
5957           (gnus-data-remove
5958            number
5959            (- (gnus-point-at-bol)
5960               (prog1
5961                   (1+ (gnus-point-at-eol))
5962                 (gnus-delete-line))))))
5963       (when old-header
5964         (mail-header-set-number header (mail-header-number old-header)))
5965       (setq gnus-newsgroup-sparse
5966             (delq (setq number (mail-header-number header))
5967                   gnus-newsgroup-sparse))
5968       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5969       (push number gnus-newsgroup-limit)
5970       (gnus-rebuild-thread (mail-header-id header) line)
5971       (gnus-summary-goto-subject number nil t))
5972     (when (and (numberp number)
5973                (> number 0))
5974       ;; We have to update the boundaries even if we can't fetch the
5975       ;; article if ID is a number -- so that the next `P' or `N'
5976       ;; command will fetch the previous (or next) article even
5977       ;; if the one we tried to fetch this time has been canceled.
5978       (when (> number gnus-newsgroup-end)
5979         (setq gnus-newsgroup-end number))
5980       (when (< number gnus-newsgroup-begin)
5981         (setq gnus-newsgroup-begin number))
5982       (setq gnus-newsgroup-unselected
5983             (delq number gnus-newsgroup-unselected)))
5984     ;; Report back a success?
5985     (and header (mail-header-number header))))
5986
5987 ;;; Process/prefix in the summary buffer
5988
5989 (defun gnus-summary-work-articles (n)
5990   "Return a list of articles to be worked upon.
5991 The prefix argument, the list of process marked articles, and the
5992 current article will be taken into consideration."
5993   (save-excursion
5994     (set-buffer gnus-summary-buffer)
5995     (cond
5996      (n
5997       ;; A numerical prefix has been given.
5998       (setq n (prefix-numeric-value n))
5999       (let ((backward (< n 0))
6000             (n (abs (prefix-numeric-value n)))
6001             articles article)
6002         (save-excursion
6003           (while
6004               (and (> n 0)
6005                    (push (setq article (gnus-summary-article-number))
6006                          articles)
6007                    (if backward
6008                        (gnus-summary-find-prev nil article)
6009                      (gnus-summary-find-next nil article)))
6010             (decf n)))
6011         (nreverse articles)))
6012      ((and (gnus-region-active-p) (mark))
6013       (message "region active")
6014       ;; Work on the region between point and mark.
6015       (let ((max (max (point) (mark)))
6016             articles article)
6017         (save-excursion
6018           (goto-char (min (point) (mark)))
6019           (while
6020               (and
6021                (push (setq article (gnus-summary-article-number)) articles)
6022                (gnus-summary-find-next nil article)
6023                (< (point) max)))
6024           (nreverse articles))))
6025      (gnus-newsgroup-processable
6026       ;; There are process-marked articles present.
6027       ;; Save current state.
6028       (gnus-summary-save-process-mark)
6029       ;; Return the list.
6030       (reverse gnus-newsgroup-processable))
6031      (t
6032       ;; Just return the current article.
6033       (list (gnus-summary-article-number))))))
6034
6035 (defmacro gnus-summary-iterate (arg &rest forms)
6036   "Iterate over the process/prefixed articles and do FORMS.
6037 ARG is the interactive prefix given to the command.  FORMS will be
6038 executed with point over the summary line of the articles."
6039   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6040     `(let ((,articles (gnus-summary-work-articles ,arg)))
6041        (while ,articles
6042          (gnus-summary-goto-subject (car ,articles))
6043          ,@forms
6044          (pop ,articles)))))
6045
6046 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6047 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6048
6049 (defun gnus-summary-save-process-mark ()
6050   "Push the current set of process marked articles on the stack."
6051   (interactive)
6052   (push (copy-sequence gnus-newsgroup-processable)
6053         gnus-newsgroup-process-stack))
6054
6055 (defun gnus-summary-kill-process-mark ()
6056   "Push the current set of process marked articles on the stack and unmark."
6057   (interactive)
6058   (gnus-summary-save-process-mark)
6059   (gnus-summary-unmark-all-processable))
6060
6061 (defun gnus-summary-yank-process-mark ()
6062   "Pop the last process mark state off the stack and restore it."
6063   (interactive)
6064   (unless gnus-newsgroup-process-stack
6065     (error "Empty mark stack"))
6066   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6067
6068 (defun gnus-summary-process-mark-set (set)
6069   "Make SET into the current process marked articles."
6070   (gnus-summary-unmark-all-processable)
6071   (while set
6072     (gnus-summary-set-process-mark (pop set))))
6073
6074 ;;; Searching and stuff
6075
6076 (defun gnus-summary-search-group (&optional backward use-level)
6077   "Search for next unread newsgroup.
6078 If optional argument BACKWARD is non-nil, search backward instead."
6079   (save-excursion
6080     (set-buffer gnus-group-buffer)
6081     (when (gnus-group-search-forward
6082            backward nil (if use-level (gnus-group-group-level) nil))
6083       (gnus-group-group-name))))
6084
6085 (defun gnus-summary-best-group (&optional exclude-group)
6086   "Find the name of the best unread group.
6087 If EXCLUDE-GROUP, do not go to this group."
6088   (save-excursion
6089     (set-buffer gnus-group-buffer)
6090     (save-excursion
6091       (gnus-group-best-unread-group exclude-group))))
6092
6093 (defun gnus-summary-find-next (&optional unread article backward)
6094   (if backward (gnus-summary-find-prev unread article)
6095     (let* ((dummy (gnus-summary-article-intangible-p))
6096            (article (or article (gnus-summary-article-number)))
6097            (data (gnus-data-find-list article))
6098            result)
6099       (when (and (not dummy)
6100                  (or (not gnus-summary-check-current)
6101                      (not unread)
6102                      (not (gnus-data-unread-p (car data)))))
6103         (setq data (cdr data)))
6104       (when (setq result
6105                   (if unread
6106                       (progn
6107                         (while data
6108                           (unless (memq (gnus-data-number (car data)) 
6109                                         (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6110                                                gnus-newsgroup-undownloaded)
6111                                               (gnus-plugged
6112                                                nil)
6113                                               ((eq gnus-auto-goto-ignores 'unfetched)
6114                                                gnus-newsgroup-unfetched)
6115                                               ((eq gnus-auto-goto-ignores 'undownloaded)
6116                                                gnus-newsgroup-undownloaded)))
6117                             (when (gnus-data-unread-p (car data))
6118                               (setq result (car data)
6119                                     data nil)))
6120                           (setq data (cdr data)))
6121                         result)
6122                     (car data)))
6123         (goto-char (gnus-data-pos result))
6124         (gnus-data-number result)))))
6125
6126 (defun gnus-summary-find-prev (&optional unread article)
6127   (let* ((eobp (eobp))
6128          (article (or article (gnus-summary-article-number)))
6129          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6130          result)
6131     (when (and (not eobp)
6132                (or (not gnus-summary-check-current)
6133                    (not unread)
6134                    (not (gnus-data-unread-p (car data)))))
6135       (setq data (cdr data)))
6136     (when (setq result
6137                 (if unread
6138                     (progn
6139                       (while data
6140                         (unless (memq (gnus-data-number (car data))
6141                                       (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6142                                              gnus-newsgroup-undownloaded)
6143                                             (gnus-plugged
6144                                              nil)
6145                                             ((eq gnus-auto-goto-ignores 'unfetched)
6146                                              gnus-newsgroup-unfetched)
6147                                             ((eq gnus-auto-goto-ignores 'undownloaded)
6148                                              gnus-newsgroup-undownloaded)))
6149                           (when (gnus-data-unread-p (car data))
6150                             (setq result (car data)
6151                                   data nil)))
6152                         (setq data (cdr data)))
6153                       result)
6154                   (car data)))
6155       (goto-char (gnus-data-pos result))
6156       (gnus-data-number result))))
6157
6158 (defun gnus-summary-find-subject (subject &optional unread backward article)
6159   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6160          (article (or article (gnus-summary-article-number)))
6161          (articles (gnus-data-list backward))
6162          (arts (gnus-data-find-list article articles))
6163          result)
6164     (when (or (not gnus-summary-check-current)
6165               (not unread)
6166               (not (gnus-data-unread-p (car arts))))
6167       (setq arts (cdr arts)))
6168     (while arts
6169       (and (or (not unread)
6170                (gnus-data-unread-p (car arts)))
6171            (vectorp (gnus-data-header (car arts)))
6172            (gnus-subject-equal
6173             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6174            (setq result (car arts)
6175                  arts nil))
6176       (setq arts (cdr arts)))
6177     (and result
6178          (goto-char (gnus-data-pos result))
6179          (gnus-data-number result))))
6180
6181 (defun gnus-summary-search-forward (&optional unread subject backward)
6182   "Search forward for an article.
6183 If UNREAD, look for unread articles.  If SUBJECT, look for
6184 articles with that subject.  If BACKWARD, search backward instead."
6185   (cond (subject (gnus-summary-find-subject subject unread backward))
6186         (backward (gnus-summary-find-prev unread))
6187         (t (gnus-summary-find-next unread))))
6188
6189 (defun gnus-recenter (&optional n)
6190   "Center point in window and redisplay frame.
6191 Also do horizontal recentering."
6192   (interactive "P")
6193   (when (and gnus-auto-center-summary
6194              (not (eq gnus-auto-center-summary 'vertical)))
6195     (gnus-horizontal-recenter))
6196   (recenter n))
6197
6198 (defun gnus-summary-recenter ()
6199   "Center point in the summary window.
6200 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6201 displayed, no centering will be performed."
6202   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6203   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6204   (interactive)
6205   ;; The user has to want it.
6206   (when gnus-auto-center-summary
6207     (let* ((top (cond ((< (window-height) 4) 0)
6208                       ((< (window-height) 7) 1)
6209                       (t (if (numberp gnus-auto-center-summary)
6210                              gnus-auto-center-summary
6211                            2))))
6212            (height (1- (window-height)))
6213            (bottom (save-excursion (goto-char (point-max))
6214                                    (forward-line (- height))
6215                                    (point)))
6216            (window (get-buffer-window (current-buffer))))
6217       (when (get-buffer-window gnus-article-buffer)
6218         ;; Only do recentering when the article buffer is displayed,
6219         ;; Set the window start to either `bottom', which is the biggest
6220         ;; possible valid number, or the second line from the top,
6221         ;; whichever is the least.
6222         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6223           (if (> bottom top-pos)
6224               ;; Keep the second line from the top visible
6225               (set-window-start window top-pos t)
6226             ;; Try to keep the bottom line visible; if it's partially
6227             ;; obscured, either scroll one more line to make it fully
6228             ;; visible, or revert to using TOP-POS.
6229             (save-excursion
6230               (goto-char (point-max))
6231               (forward-line -1)
6232               (let ((last-line-start (point)))
6233                 (goto-char bottom)
6234                 (set-window-start window (point) t)
6235                 (when (not (pos-visible-in-window-p last-line-start window))
6236                   (forward-line 1)
6237                   (set-window-start window (min (point) top-pos) t)))))))
6238       ;; Do horizontal recentering while we're at it.
6239       (when (and (get-buffer-window (current-buffer) t)
6240                  (not (eq gnus-auto-center-summary 'vertical)))
6241         (let ((selected (selected-window)))
6242           (select-window (get-buffer-window (current-buffer) t))
6243           (gnus-summary-position-point)
6244           (gnus-horizontal-recenter)
6245           (select-window selected))))))
6246
6247 (defun gnus-summary-jump-to-group (newsgroup)
6248   "Move point to NEWSGROUP in group mode buffer."
6249   ;; Keep update point of group mode buffer if visible.
6250   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6251       (save-window-excursion
6252         ;; Take care of tree window mode.
6253         (when (get-buffer-window gnus-group-buffer)
6254           (pop-to-buffer gnus-group-buffer))
6255         (gnus-group-jump-to-group newsgroup))
6256     (save-excursion
6257       ;; Take care of tree window mode.
6258       (if (get-buffer-window gnus-group-buffer)
6259           (pop-to-buffer gnus-group-buffer)
6260         (set-buffer gnus-group-buffer))
6261       (gnus-group-jump-to-group newsgroup))))
6262
6263 ;; This function returns a list of article numbers based on the
6264 ;; difference between the ranges of read articles in this group and
6265 ;; the range of active articles.
6266 (defun gnus-list-of-unread-articles (group)
6267   (let* ((read (gnus-info-read (gnus-get-info group)))
6268          (active (or (gnus-active group) (gnus-activate-group group)))
6269          (last (cdr active))
6270          first nlast unread)
6271     ;; If none are read, then all are unread.
6272     (if (not read)
6273         (setq first (car active))
6274       ;; If the range of read articles is a single range, then the
6275       ;; first unread article is the article after the last read
6276       ;; article.  Sounds logical, doesn't it?
6277       (if (and (not (listp (cdr read)))
6278                (or (< (car read) (car active))
6279                    (progn (setq read (list read))
6280                           nil)))
6281           (setq first (max (car active) (1+ (cdr read))))
6282         ;; `read' is a list of ranges.
6283         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6284                                   (caar read)))
6285                   1)
6286           (setq first (car active)))
6287         (while read
6288           (when first
6289             (while (< first nlast)
6290               (push first unread)
6291               (setq first (1+ first))))
6292           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6293           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6294           (setq read (cdr read)))))
6295     ;; And add the last unread articles.
6296     (while (<= first last)
6297       (push first unread)
6298       (setq first (1+ first)))
6299     ;; Return the list of unread articles.
6300     (delq 0 (nreverse unread))))
6301
6302 (defun gnus-list-of-read-articles (group)
6303   "Return a list of unread, unticked and non-dormant articles."
6304   (let* ((info (gnus-get-info group))
6305          (marked (gnus-info-marks info))
6306          (active (gnus-active group)))
6307     (and info active
6308          (gnus-list-range-difference
6309           (gnus-list-range-difference
6310            (gnus-sorted-complement
6311             (gnus-uncompress-range active)
6312             (gnus-list-of-unread-articles group))
6313            (cdr (assq 'dormant marked)))
6314           (cdr (assq 'tick marked))))))
6315
6316 ;; Various summary commands
6317
6318 (defun gnus-summary-select-article-buffer ()
6319   "Reconfigure windows to show article buffer."
6320   (interactive)
6321   (if (not (gnus-buffer-live-p gnus-article-buffer))
6322       (error "There is no article buffer for this summary buffer")
6323     (gnus-configure-windows 'article)
6324     (select-window (get-buffer-window gnus-article-buffer))))
6325
6326 (defun gnus-summary-universal-argument (arg)
6327   "Perform any operation on all articles that are process/prefixed."
6328   (interactive "P")
6329   (let ((articles (gnus-summary-work-articles arg))
6330         func article)
6331     (if (eq
6332          (setq
6333           func
6334           (key-binding
6335            (read-key-sequence
6336             (substitute-command-keys
6337              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6338          'undefined)
6339         (gnus-error 1 "Undefined key")
6340       (save-excursion
6341         (while articles
6342           (gnus-summary-goto-subject (setq article (pop articles)))
6343           (let (gnus-newsgroup-processable)
6344             (command-execute func))
6345           (gnus-summary-remove-process-mark article)))))
6346   (gnus-summary-position-point))
6347
6348 (defun gnus-summary-toggle-truncation (&optional arg)
6349   "Toggle truncation of summary lines.
6350 With arg, turn line truncation on if arg is positive."
6351   (interactive "P")
6352   (setq truncate-lines
6353         (if (null arg) (not truncate-lines)
6354           (> (prefix-numeric-value arg) 0)))
6355   (redraw-display))
6356
6357 (defun gnus-summary-find-for-reselect ()
6358   "Return the number of an article to stay on across a reselect.
6359 The current article is considered, then following articles, then previous
6360 articles.  An article is sought which is not cancelled and isn't a temporary
6361 insertion from another group.  If there's no such then return a dummy 0."
6362   (let (found)
6363     (dolist (rev '(nil t))
6364       (unless found      ; don't demand the reverse list if we don't need it
6365         (let ((data (gnus-data-find-list
6366                      (gnus-summary-article-number) (gnus-data-list rev))))
6367           (while (and data (not found))
6368             (if (and (< 0 (gnus-data-number (car data)))
6369                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6370                 (setq found (gnus-data-number (car data))))
6371             (setq data (cdr data))))))
6372     (or found 0)))
6373
6374 (defun gnus-summary-reselect-current-group (&optional all rescan)
6375   "Exit and then reselect the current newsgroup.
6376 The prefix argument ALL means to select all articles."
6377   (interactive "P")
6378   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6379     (error "Ephemeral groups can't be reselected"))
6380   (let ((current-subject (gnus-summary-find-for-reselect))
6381         (group gnus-newsgroup-name))
6382     (setq gnus-newsgroup-begin nil)
6383     (gnus-summary-exit)
6384     ;; We have to adjust the point of group mode buffer because
6385     ;; point was moved to the next unread newsgroup by exiting.
6386     (gnus-summary-jump-to-group group)
6387     (when rescan
6388       (save-excursion
6389         (save-window-excursion
6390           ;; Don't show group contents.
6391           (set-window-start (selected-window) (point-max))
6392           (gnus-group-get-new-news-this-group 1))))
6393     (gnus-group-read-group all t)
6394     (gnus-summary-goto-subject current-subject nil t)))
6395
6396 (defun gnus-summary-rescan-group (&optional all)
6397   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6398   (interactive "P")
6399   (gnus-summary-reselect-current-group all t))
6400
6401 (defun gnus-summary-update-info (&optional non-destructive)
6402   (save-excursion
6403     (let ((group gnus-newsgroup-name))
6404       (when group
6405         (when gnus-newsgroup-kill-headers
6406           (setq gnus-newsgroup-killed
6407                 (gnus-compress-sequence
6408                  (gnus-sorted-union
6409                   (gnus-list-range-intersection
6410                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6411                   gnus-newsgroup-unreads)
6412                  t)))
6413         (unless (listp (cdr gnus-newsgroup-killed))
6414           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6415         (let ((headers gnus-newsgroup-headers))
6416           ;; Set the new ranges of read articles.
6417           (save-excursion
6418             (set-buffer gnus-group-buffer)
6419             (gnus-undo-force-boundary))
6420           (gnus-update-read-articles
6421            group (gnus-sorted-union
6422                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6423           ;; Set the current article marks.
6424           (let ((gnus-newsgroup-scored
6425                  (if (and (not gnus-save-score)
6426                           (not non-destructive))
6427                      nil
6428                    gnus-newsgroup-scored)))
6429             (save-excursion
6430               (gnus-update-marks)))
6431           ;; Do the cross-ref thing.
6432           (when gnus-use-cross-reference
6433             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6434           ;; Do not switch windows but change the buffer to work.
6435           (set-buffer gnus-group-buffer)
6436           (unless (gnus-ephemeral-group-p group)
6437             (gnus-group-update-group group)))))))
6438
6439 (defun gnus-summary-save-newsrc (&optional force)
6440   "Save the current number of read/marked articles in the dribble buffer.
6441 The dribble buffer will then be saved.
6442 If FORCE (the prefix), also save the .newsrc file(s)."
6443   (interactive "P")
6444   (gnus-summary-update-info t)
6445   (if force
6446       (gnus-save-newsrc-file)
6447     (gnus-dribble-save)))
6448
6449 (defun gnus-summary-exit (&optional temporary)
6450   "Exit reading current newsgroup, and then return to group selection mode.
6451 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6452   (interactive)
6453   (gnus-set-global-variables)
6454   (gnus-kill-save-kill-buffer)
6455   (gnus-async-halt-prefetch)
6456   (let* ((group gnus-newsgroup-name)
6457          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6458          (gnus-group-is-exiting-p t)
6459          (mode major-mode)
6460          (group-point nil)
6461          (buf (current-buffer)))
6462     (unless quit-config
6463       ;; Do adaptive scoring, and possibly save score files.
6464       (when gnus-newsgroup-adaptive
6465         (gnus-score-adaptive))
6466       (when gnus-use-scoring
6467         (gnus-score-save)))
6468     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6469     ;; If we have several article buffers, we kill them at exit.
6470     (unless gnus-single-article-buffer
6471       (gnus-kill-buffer gnus-original-article-buffer)
6472       (setq gnus-article-current nil))
6473     (when gnus-use-cache
6474       (gnus-cache-possibly-remove-articles)
6475       (gnus-cache-save-buffers))
6476     (gnus-async-prefetch-remove-group group)
6477     (when gnus-suppress-duplicates
6478       (gnus-dup-enter-articles))
6479     (when gnus-use-trees
6480       (gnus-tree-close group))
6481     (when gnus-use-cache
6482       (gnus-cache-write-active))
6483     ;; Remove entries for this group.
6484     (nnmail-purge-split-history (gnus-group-real-name group))
6485     ;; Make all changes in this group permanent.
6486     (unless quit-config
6487       (gnus-run-hooks 'gnus-exit-group-hook)
6488       (gnus-summary-update-info))
6489     (gnus-close-group group)
6490     ;; Make sure where we were, and go to next newsgroup.
6491     (set-buffer gnus-group-buffer)
6492     (unless quit-config
6493       (gnus-group-jump-to-group group))
6494     (gnus-run-hooks 'gnus-summary-exit-hook)
6495     (unless (or quit-config
6496                 ;; If this group has disappeared from the summary
6497                 ;; buffer, don't skip forwards.
6498                 (not (string= group (gnus-group-group-name))))
6499       (gnus-group-next-unread-group 1))
6500     (setq group-point (point))
6501     (if temporary
6502         nil                             ;Nothing to do.
6503       ;; If we have several article buffers, we kill them at exit.
6504       (unless gnus-single-article-buffer
6505         (gnus-kill-buffer gnus-article-buffer)
6506         (gnus-kill-buffer gnus-original-article-buffer)
6507         (setq gnus-article-current nil))
6508       (set-buffer buf)
6509       (if (not gnus-kill-summary-on-exit)
6510           (progn
6511             (gnus-deaden-summary)
6512             (setq mode nil))
6513         ;; We set all buffer-local variables to nil.  It is unclear why
6514         ;; this is needed, but if we don't, buffer-local variables are
6515         ;; not garbage-collected, it seems.  This would the lead to en
6516         ;; ever-growing Emacs.
6517         (gnus-summary-clear-local-variables)
6518         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6519           (gnus-summary-clear-local-variables))
6520         (when (get-buffer gnus-article-buffer)
6521           (bury-buffer gnus-article-buffer))
6522         ;; We clear the global counterparts of the buffer-local
6523         ;; variables as well, just to be on the safe side.
6524         (set-buffer gnus-group-buffer)
6525         (gnus-summary-clear-local-variables)
6526         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6527           (gnus-summary-clear-local-variables))
6528         ;; Return to group mode buffer.
6529         (when (eq mode 'gnus-summary-mode)
6530           (gnus-kill-buffer buf)))
6531       (setq gnus-current-select-method gnus-select-method)
6532       (pop-to-buffer gnus-group-buffer)
6533       (if (not quit-config)
6534           (progn
6535             (goto-char group-point)
6536             (gnus-configure-windows 'group 'force)
6537             (unless (pos-visible-in-window-p)
6538               (forward-line (/ (static-if (featurep 'xemacs)
6539                                    (window-displayed-height)
6540                                  (1- (window-height)))
6541                                -2))
6542               (set-window-start (selected-window) (point))
6543               (goto-char group-point)))
6544         (gnus-handle-ephemeral-exit quit-config))
6545       ;; Clear the current group name.
6546       (unless quit-config
6547         (setq gnus-newsgroup-name nil)))))
6548
6549 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6550 (defun gnus-summary-exit-no-update (&optional no-questions)
6551   "Quit reading current newsgroup without updating read article info."
6552   (interactive)
6553   (let* ((group gnus-newsgroup-name)
6554          (gnus-group-is-exiting-p t)
6555          (gnus-group-is-exiting-without-update-p t)
6556          (quit-config (gnus-group-quit-config group)))
6557     (when (or no-questions
6558               gnus-expert-user
6559               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6560       (gnus-async-halt-prefetch)
6561       (run-hooks 'gnus-summary-prepare-exit-hook)
6562       ;; If we have several article buffers, we kill them at exit.
6563       (unless gnus-single-article-buffer
6564         (gnus-kill-buffer gnus-article-buffer)
6565         (gnus-kill-buffer gnus-original-article-buffer)
6566         (setq gnus-article-current nil))
6567       (if (not gnus-kill-summary-on-exit)
6568           (gnus-deaden-summary)
6569         (gnus-close-group group)
6570         (gnus-summary-clear-local-variables)
6571         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6572           (gnus-summary-clear-local-variables))
6573         (set-buffer gnus-group-buffer)
6574         (gnus-summary-clear-local-variables)
6575         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6576           (gnus-summary-clear-local-variables))
6577         (gnus-kill-buffer gnus-summary-buffer))
6578       (unless gnus-single-article-buffer
6579         (setq gnus-article-current nil))
6580       (when gnus-use-trees
6581         (gnus-tree-close group))
6582       (gnus-async-prefetch-remove-group group)
6583       (when (get-buffer gnus-article-buffer)
6584         (bury-buffer gnus-article-buffer))
6585       ;; Return to the group buffer.
6586       (gnus-configure-windows 'group 'force)
6587       ;; Clear the current group name.
6588       (setq gnus-newsgroup-name nil)
6589       (unless (gnus-ephemeral-group-p group)
6590         (gnus-group-update-group group))
6591       (when (equal (gnus-group-group-name) group)
6592         (gnus-group-next-unread-group 1))
6593       (when quit-config
6594         (gnus-handle-ephemeral-exit quit-config)))))
6595
6596 (defun gnus-handle-ephemeral-exit (quit-config)
6597   "Handle movement when leaving an ephemeral group.
6598 The state which existed when entering the ephemeral is reset."
6599   (if (not (buffer-name (car quit-config)))
6600       (gnus-configure-windows 'group 'force)
6601     (set-buffer (car quit-config))
6602     (cond ((eq major-mode 'gnus-summary-mode)
6603            (gnus-set-global-variables))
6604           ((eq major-mode 'gnus-article-mode)
6605            (save-excursion
6606              ;; The `gnus-summary-buffer' variable may point
6607              ;; to the old summary buffer when using a single
6608              ;; article buffer.
6609              (unless (gnus-buffer-live-p gnus-summary-buffer)
6610                (set-buffer gnus-group-buffer))
6611              (set-buffer gnus-summary-buffer)
6612              (gnus-set-global-variables))))
6613     (if (or (eq (cdr quit-config) 'article)
6614             (eq (cdr quit-config) 'pick))
6615         (progn
6616           ;; The current article may be from the ephemeral group
6617           ;; thus it is best that we reload this article
6618           (gnus-summary-show-article)
6619           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6620               (gnus-configure-windows 'pick 'force)
6621             (gnus-configure-windows (cdr quit-config) 'force)))
6622       (gnus-configure-windows (cdr quit-config) 'force))
6623     (when (eq major-mode 'gnus-summary-mode)
6624       (gnus-summary-next-subject 1 nil t)
6625       (gnus-summary-recenter)
6626       (gnus-summary-position-point))))
6627
6628 (defun gnus-summary-preview-mime-message ()
6629   "MIME decode and play this message."
6630   (interactive)
6631   (let ((gnus-break-pages nil)
6632         (gnus-show-mime t))
6633     (gnus-summary-select-article gnus-show-all-headers t))
6634   (let ((w (get-buffer-window gnus-article-buffer)))
6635     (when w
6636       (select-window (get-buffer-window gnus-article-buffer)))))
6637
6638 ;;; Dead summaries.
6639
6640 (defvar gnus-dead-summary-mode-map nil)
6641
6642 (unless gnus-dead-summary-mode-map
6643   (setq gnus-dead-summary-mode-map (make-keymap))
6644   (suppress-keymap gnus-dead-summary-mode-map)
6645   (substitute-key-definition
6646    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6647   (dolist (key '("\C-d" "\r" "\177" [delete]))
6648     (define-key gnus-dead-summary-mode-map
6649       key 'gnus-summary-wake-up-the-dead))
6650   (dolist (key '("q" "Q"))
6651     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6652
6653 (defvar gnus-dead-summary-mode nil
6654   "Minor mode for Gnus summary buffers.")
6655
6656 (defun gnus-dead-summary-mode (&optional arg)
6657   "Minor mode for Gnus summary buffers."
6658   (interactive "P")
6659   (when (eq major-mode 'gnus-summary-mode)
6660     (make-local-variable 'gnus-dead-summary-mode)
6661     (setq gnus-dead-summary-mode
6662           (if (null arg) (not gnus-dead-summary-mode)
6663             (> (prefix-numeric-value arg) 0)))
6664     (when gnus-dead-summary-mode
6665       (gnus-add-minor-mode
6666        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6667
6668 (defun gnus-deaden-summary ()
6669   "Make the current summary buffer into a dead summary buffer."
6670   ;; Kill any previous dead summary buffer.
6671   (when (and gnus-dead-summary
6672              (buffer-name gnus-dead-summary))
6673     (save-excursion
6674       (set-buffer gnus-dead-summary)
6675       (when gnus-dead-summary-mode
6676         (kill-buffer (current-buffer)))))
6677   ;; Make this the current dead summary.
6678   (setq gnus-dead-summary (current-buffer))
6679   (gnus-dead-summary-mode 1)
6680   (let ((name (buffer-name)))
6681     (when (string-match "Summary" name)
6682       (rename-buffer
6683        (concat (substring name 0 (match-beginning 0)) "Dead "
6684                (substring name (match-beginning 0)))
6685        t)
6686       (bury-buffer))))
6687
6688 (defun gnus-kill-or-deaden-summary (buffer)
6689   "Kill or deaden the summary BUFFER."
6690   (save-excursion
6691     (when (and (buffer-name buffer)
6692                (not gnus-single-article-buffer))
6693       (save-excursion
6694         (set-buffer buffer)
6695         (gnus-kill-buffer gnus-article-buffer)
6696         (gnus-kill-buffer gnus-original-article-buffer)))
6697     (cond
6698      ;; Kill the buffer.
6699      (gnus-kill-summary-on-exit
6700       (when (and gnus-use-trees
6701                  (gnus-buffer-exists-p buffer))
6702         (save-excursion
6703           (set-buffer buffer)
6704           (gnus-tree-close gnus-newsgroup-name)))
6705       (gnus-kill-buffer buffer))
6706      ;; Deaden the buffer.
6707      ((gnus-buffer-exists-p buffer)
6708       (save-excursion
6709         (set-buffer buffer)
6710         (gnus-deaden-summary))))))
6711
6712 (defun gnus-summary-wake-up-the-dead (&rest args)
6713   "Wake up the dead summary buffer."
6714   (interactive)
6715   (gnus-dead-summary-mode -1)
6716   (let ((name (buffer-name)))
6717     (when (string-match "Dead " name)
6718       (rename-buffer
6719        (concat (substring name 0 (match-beginning 0))
6720                (substring name (match-end 0)))
6721        t)))
6722   (gnus-message 3 "This dead summary is now alive again"))
6723
6724 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6725 (defun gnus-summary-fetch-faq (&optional faq-dir)
6726   "Fetch the FAQ for the current group.
6727 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6728 in."
6729   (interactive
6730    (list
6731     (when current-prefix-arg
6732       (completing-read
6733        "FAQ dir: " (and (listp gnus-group-faq-directory)
6734                         (mapcar (lambda (file) (list file))
6735                                 gnus-group-faq-directory))))))
6736   (let (gnus-faq-buffer)
6737     (when (setq gnus-faq-buffer
6738                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6739       (gnus-configure-windows 'summary-faq))))
6740
6741 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6742 (defun gnus-summary-describe-group (&optional force)
6743   "Describe the current newsgroup."
6744   (interactive "P")
6745   (gnus-group-describe-group force gnus-newsgroup-name))
6746
6747 (defun gnus-summary-describe-briefly ()
6748   "Describe summary mode commands briefly."
6749   (interactive)
6750   (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")))
6751
6752 ;; Walking around group mode buffer from summary mode.
6753
6754 (defun gnus-summary-next-group (&optional no-article target-group backward)
6755   "Exit current newsgroup and then select next unread newsgroup.
6756 If prefix argument NO-ARTICLE is non-nil, no article is selected
6757 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6758 previous group instead."
6759   (interactive "P")
6760   ;; Stop pre-fetching.
6761   (gnus-async-halt-prefetch)
6762   (let ((current-group gnus-newsgroup-name)
6763         (current-buffer (current-buffer))
6764         entered)
6765     ;; First we semi-exit this group to update Xrefs and all variables.
6766     ;; We can't do a real exit, because the window conf must remain
6767     ;; the same in case the user is prompted for info, and we don't
6768     ;; want the window conf to change before that...
6769     (gnus-summary-exit t)
6770     (while (not entered)
6771       ;; Then we find what group we are supposed to enter.
6772       (set-buffer gnus-group-buffer)
6773       (gnus-group-jump-to-group current-group)
6774       (setq target-group
6775             (or target-group
6776                 (if (eq gnus-keep-same-level 'best)
6777                     (gnus-summary-best-group gnus-newsgroup-name)
6778                   (gnus-summary-search-group backward gnus-keep-same-level))))
6779       (if (not target-group)
6780           ;; There are no further groups, so we return to the group
6781           ;; buffer.
6782           (progn
6783             (gnus-message 5 "Returning to the group buffer")
6784             (setq entered t)
6785             (when (gnus-buffer-live-p current-buffer)
6786               (set-buffer current-buffer)
6787               (gnus-summary-exit))
6788             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6789         ;; We try to enter the target group.
6790         (gnus-group-jump-to-group target-group)
6791         (let ((unreads (gnus-group-group-unread)))
6792           (if (and (or (eq t unreads)
6793                        (and unreads (not (zerop unreads))))
6794                    (gnus-summary-read-group
6795                     target-group nil no-article
6796                     (and (buffer-name current-buffer) current-buffer)
6797                     nil backward))
6798               (setq entered t)
6799             (setq current-group target-group
6800                   target-group nil)))))))
6801
6802 (defun gnus-summary-prev-group (&optional no-article)
6803   "Exit current newsgroup and then select previous unread newsgroup.
6804 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6805   (interactive "P")
6806   (gnus-summary-next-group no-article nil t))
6807
6808 ;; Walking around summary lines.
6809
6810 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6811   "Go to the first subject satisfying any non-nil constraint.
6812 If UNREAD is non-nil, the article should be unread.
6813 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6814 If UNSEED is non-nil, the article should be unseen.
6815 Returns the article selected or nil if there are no matching articles."
6816   (interactive "P")
6817   (cond
6818    ;; Empty summary.
6819    ((null gnus-newsgroup-data)
6820     (gnus-message 3 "No articles in the group")
6821     nil)
6822    ;; Pick the first article.
6823    ((not (or unread undownloaded unseen))
6824     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6825     (gnus-data-number (car gnus-newsgroup-data)))
6826    ;; Find the first unread article.
6827    (t
6828     (let ((data gnus-newsgroup-data))
6829       (while (and data
6830                   (let ((num (gnus-data-number (car data))))
6831                     (or (memq num gnus-newsgroup-unfetched)
6832                         (not (or (and unread
6833                                       (memq num gnus-newsgroup-unreads))
6834                                  (and undownloaded
6835                                       (memq num gnus-newsgroup-undownloaded))
6836                                  (and unseen
6837                                       (memq num gnus-newsgroup-unseen)))))))
6838         (setq data (cdr data)))
6839       (prog1 
6840           (if data
6841               (progn
6842                 (goto-char (gnus-data-pos (car data)))
6843                 (gnus-data-number (car data)))
6844             (gnus-message 3 "No more%s articles"
6845                           (let* ((r (when unread " unread"))
6846                                  (d (when undownloaded " undownloaded"))
6847                                  (s (when unseen " unseen"))
6848                                  (l (delq nil (list r d s))))
6849                             (cond ((= 3 (length l))
6850                                    (concat r "," d ", or" s))
6851                                   ((= 2 (length l))
6852                                    (concat (car l) ", or" (cadr l)))
6853                                   ((= 1 (length l))
6854                                    (car l))
6855                                   (t
6856                                    ""))))
6857             nil
6858             )
6859         (gnus-summary-position-point))))))
6860
6861 (defun gnus-summary-next-subject (n &optional unread dont-display)
6862   "Go to next N'th summary line.
6863 If N is negative, go to the previous N'th subject line.
6864 If UNREAD is non-nil, only unread articles are selected.
6865 The difference between N and the actual number of steps taken is
6866 returned."
6867   (interactive "p")
6868   (let ((backward (< n 0))
6869         (n (abs n)))
6870     (while (and (> n 0)
6871                 (if backward
6872                     (gnus-summary-find-prev unread)
6873                   (gnus-summary-find-next unread)))
6874       (unless (zerop (setq n (1- n)))
6875         (gnus-summary-show-thread)))
6876     (when (/= 0 n)
6877       (gnus-message 7 "No more%s articles"
6878                     (if unread " unread" "")))
6879     (unless dont-display
6880       (gnus-summary-recenter)
6881       (gnus-summary-position-point))
6882     n))
6883
6884 (defun gnus-summary-next-unread-subject (n)
6885   "Go to next N'th unread summary line."
6886   (interactive "p")
6887   (gnus-summary-next-subject n t))
6888
6889 (defun gnus-summary-prev-subject (n &optional unread)
6890   "Go to previous N'th summary line.
6891 If optional argument UNREAD is non-nil, only unread article is selected."
6892   (interactive "p")
6893   (gnus-summary-next-subject (- n) unread))
6894
6895 (defun gnus-summary-prev-unread-subject (n)
6896   "Go to previous N'th unread summary line."
6897   (interactive "p")
6898   (gnus-summary-next-subject (- n) t))
6899
6900 (defun gnus-summary-goto-subjects (articles)
6901   "Insert the subject header for ARTICLES in the current buffer."
6902   (save-excursion
6903     (dolist (article articles)
6904       (gnus-summary-goto-subject article t)))
6905   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6906   (gnus-summary-position-point))
6907  
6908 (defun gnus-summary-goto-subject (article &optional force silent)
6909   "Go the subject line of ARTICLE.
6910 If FORCE, also allow jumping to articles not currently shown."
6911   (interactive "nArticle number: ")
6912   (unless (numberp article)
6913     (error "Article %s is not a number" article))
6914   (let ((b (point))
6915         (data (gnus-data-find article)))
6916     ;; We read in the article if we have to.
6917     (and (not data)
6918          force
6919          (gnus-summary-insert-subject
6920           article
6921           (if (or (numberp force) (vectorp force)) force)
6922           t)
6923          (setq data (gnus-data-find article)))
6924     (goto-char b)
6925     (if (not data)
6926         (progn
6927           (unless silent
6928             (gnus-message 3 "Can't find article %d" article))
6929           nil)
6930       (let ((pt (gnus-data-pos data)))
6931         (goto-char pt)
6932         (gnus-summary-set-article-display-arrow pt))
6933       (gnus-summary-position-point)
6934       article)))
6935
6936 ;; Walking around summary lines with displaying articles.
6937
6938 (defun gnus-summary-expand-window (&optional arg)
6939   "Make the summary buffer take up the entire Emacs frame.
6940 Given a prefix, will force an `article' buffer configuration."
6941   (interactive "P")
6942   (if arg
6943       (gnus-configure-windows 'article 'force)
6944     (gnus-configure-windows 'summary 'force)))
6945
6946 (defun gnus-summary-display-article (article &optional all-header)
6947   "Display ARTICLE in article buffer."
6948   (when (gnus-buffer-live-p gnus-article-buffer)
6949     (with-current-buffer gnus-article-buffer
6950       (set-buffer-multibyte t)))
6951   (gnus-set-global-variables)
6952   (when (gnus-buffer-live-p gnus-article-buffer)
6953     (with-current-buffer gnus-article-buffer
6954       (setq gnus-article-charset gnus-newsgroup-charset)
6955       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6956       (set-buffer-multibyte t)))
6957   (if (null article)
6958       nil
6959     (prog1
6960         (if gnus-summary-display-article-function
6961             (funcall gnus-summary-display-article-function article all-header)
6962           (gnus-article-prepare article all-header))
6963       (with-current-buffer gnus-article-buffer
6964         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6965              nil))
6966       (gnus-run-hooks 'gnus-select-article-hook)
6967       (when (and gnus-current-article
6968                  (not (zerop gnus-current-article)))
6969         (gnus-summary-goto-subject gnus-current-article))
6970       (gnus-summary-recenter)
6971       (when (and gnus-use-trees gnus-show-threads)
6972         (gnus-possibly-generate-tree article)
6973         (gnus-highlight-selected-tree article))
6974       ;; Successfully display article.
6975       (gnus-article-set-window-start
6976        (cdr (assq article gnus-newsgroup-bookmarks))))))
6977
6978 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6979   "Select the current article.
6980 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6981 non-nil, the article will be re-fetched even if it already present in
6982 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6983 be displayed."
6984   ;; Make sure we are in the summary buffer to work around bbdb bug.
6985   (unless (eq major-mode 'gnus-summary-mode)
6986     (set-buffer gnus-summary-buffer))
6987   (let ((article (or article (gnus-summary-article-number)))
6988         (all-headers (not (not all-headers))) ;Must be T or NIL.
6989         gnus-summary-display-article-function)
6990     (and (not pseudo)
6991          (gnus-summary-article-pseudo-p article)
6992          (error "This is a pseudo-article"))
6993     (save-excursion
6994       (set-buffer gnus-summary-buffer)
6995       (if (or (and gnus-single-article-buffer
6996                    (or (null gnus-current-article)
6997                        (null gnus-article-current)
6998                        (null (get-buffer gnus-article-buffer))
6999                        (not (eq article (cdr gnus-article-current)))
7000                        (not (equal (car gnus-article-current)
7001                                    gnus-newsgroup-name))))
7002               (and (not gnus-single-article-buffer)
7003                    (or (null gnus-current-article)
7004                        (not (eq gnus-current-article article))))
7005               force)
7006           ;; The requested article is different from the current article.
7007           (progn
7008             (gnus-summary-display-article article all-headers)
7009             (gnus-article-set-window-start
7010              (cdr (assq article gnus-newsgroup-bookmarks)))
7011             article)
7012         'old))))
7013
7014 (defun gnus-summary-force-verify-and-decrypt ()
7015   "Display buttons for signed/encrypted parts and verify/decrypt them."
7016   (interactive)
7017   (let ((mm-verify-option 'known)
7018         (mm-decrypt-option 'known)
7019         (gnus-buttonized-mime-types (append (list "multipart/signed"
7020                                                   "multipart/encrypted")
7021                                             gnus-buttonized-mime-types)))
7022     (gnus-summary-select-article nil 'force)))
7023
7024 (defun gnus-summary-set-current-mark (&optional current-mark)
7025   "Obsolete function."
7026   nil)
7027
7028 (defun gnus-summary-next-article (&optional unread subject backward push)
7029   "Select the next article.
7030 If UNREAD, only unread articles are selected.
7031 If SUBJECT, only articles with SUBJECT are selected.
7032 If BACKWARD, the previous article is selected instead of the next."
7033   (interactive "P")
7034   (cond
7035    ;; Is there such an article?
7036    ((and (gnus-summary-search-forward unread subject backward)
7037          (or (gnus-summary-display-article (gnus-summary-article-number))
7038              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7039     (gnus-summary-position-point))
7040    ;; If not, we try the first unread, if that is wanted.
7041    ((and subject
7042          gnus-auto-select-same
7043          (gnus-summary-first-unread-article))
7044     (gnus-summary-position-point)
7045     (gnus-message 6 "Wrapped"))
7046    ;; Try to get next/previous article not displayed in this group.
7047    ((and gnus-auto-extend-newsgroup
7048          (not unread) (not subject))
7049     (gnus-summary-goto-article
7050      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7051      nil (count-lines (point-min) (point))))
7052    ;; Go to next/previous group.
7053    (t
7054     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7055       (gnus-summary-jump-to-group gnus-newsgroup-name))
7056     (let ((cmd last-command-char)
7057           (point
7058            (save-excursion
7059              (set-buffer gnus-group-buffer)
7060              (point)))
7061           (group
7062            (if (eq gnus-keep-same-level 'best)
7063                (gnus-summary-best-group gnus-newsgroup-name)
7064              (gnus-summary-search-group backward gnus-keep-same-level))))
7065       ;; For some reason, the group window gets selected.  We change
7066       ;; it back.
7067       (select-window (get-buffer-window (current-buffer)))
7068       ;; Select next unread newsgroup automagically.
7069       (cond
7070        ((or (not gnus-auto-select-next)
7071             (not cmd))
7072         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7073        ((or (eq gnus-auto-select-next 'quietly)
7074             (and (eq gnus-auto-select-next 'slightly-quietly)
7075                  push)
7076             (and (eq gnus-auto-select-next 'almost-quietly)
7077                  (gnus-summary-last-article-p)))
7078         ;; Select quietly.
7079         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7080             (gnus-summary-exit)
7081           (gnus-message 7 "No more%s articles (%s)..."
7082                         (if unread " unread" "")
7083                         (if group (concat "selecting " group)
7084                           "exiting"))
7085           (gnus-summary-next-group nil group backward)))
7086        (t
7087         (when (gnus-key-press-event-p last-input-event)
7088           (gnus-summary-walk-group-buffer
7089            gnus-newsgroup-name cmd unread backward point))))))))
7090
7091 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7092   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7093                       (?\C-p (gnus-group-prev-unread-group 1))))
7094         (cursor-in-echo-area t)
7095         keve key group ended prompt)
7096     (save-excursion
7097       (set-buffer gnus-group-buffer)
7098       (goto-char start)
7099       (setq group
7100             (if (eq gnus-keep-same-level 'best)
7101                 (gnus-summary-best-group gnus-newsgroup-name)
7102               (gnus-summary-search-group backward gnus-keep-same-level))))
7103     (while (not ended)
7104       (setq prompt
7105             (format
7106              "No more%s articles%s " (if unread " unread" "")
7107              (if (and group
7108                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7109                  (format " (Type %s for %s [%s])"
7110                          (single-key-description cmd) group
7111                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7112                (format " (Type %s to exit %s)"
7113                        (single-key-description cmd)
7114                        gnus-newsgroup-name))))
7115       ;; Confirm auto selection.
7116       (setq key (car (setq keve (gnus-read-event-char prompt)))
7117             ended t)
7118       (cond
7119        ((assq key keystrokes)
7120         (let ((obuf (current-buffer)))
7121           (switch-to-buffer gnus-group-buffer)
7122           (when group
7123             (gnus-group-jump-to-group group))
7124           (eval (cadr (assq key keystrokes)))
7125           (setq group (gnus-group-group-name))
7126           (switch-to-buffer obuf))
7127         (setq ended nil))
7128        ((equal key cmd)
7129         (if (or (not group)
7130                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7131             (gnus-summary-exit)
7132           (gnus-summary-next-group nil group backward)))
7133        (t
7134         (push (cdr keve) unread-command-events))))))
7135
7136 (defun gnus-summary-next-unread-article ()
7137   "Select unread article after current one."
7138   (interactive)
7139   (gnus-summary-next-article
7140    (or (not (eq gnus-summary-goto-unread 'never))
7141        (gnus-summary-last-article-p (gnus-summary-article-number)))
7142    (and gnus-auto-select-same
7143         (gnus-summary-article-subject))))
7144
7145 (defun gnus-summary-prev-article (&optional unread subject)
7146   "Select the article after the current one.
7147 If UNREAD is non-nil, only unread articles are selected."
7148   (interactive "P")
7149   (gnus-summary-next-article unread subject t))
7150
7151 (defun gnus-summary-prev-unread-article ()
7152   "Select unread article before current one."
7153   (interactive)
7154   (gnus-summary-prev-article
7155    (or (not (eq gnus-summary-goto-unread 'never))
7156        (gnus-summary-first-article-p (gnus-summary-article-number)))
7157    (and gnus-auto-select-same
7158         (gnus-summary-article-subject))))
7159
7160 (defun gnus-summary-next-page (&optional lines circular stop)
7161   "Show next page of the selected article.
7162 If at the end of the current article, select the next article.
7163 LINES says how many lines should be scrolled up.
7164
7165 If CIRCULAR is non-nil, go to the start of the article instead of
7166 selecting the next article when reaching the end of the current
7167 article.
7168
7169 If STOP is non-nil, just stop when reaching the end of the message."
7170   (interactive "P")
7171   (setq gnus-summary-buffer (current-buffer))
7172   (gnus-set-global-variables)
7173   (let ((article (gnus-summary-article-number))
7174         (article-window (get-buffer-window gnus-article-buffer t))
7175         endp)
7176     ;; If the buffer is empty, we have no article.
7177     (unless article
7178       (error "No article to select"))
7179     (gnus-configure-windows 'article)
7180     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7181         (if (and (eq gnus-summary-goto-unread 'never)
7182                  (not (gnus-summary-last-article-p article)))
7183             (gnus-summary-next-article)
7184           (gnus-summary-next-unread-article))
7185       (if (or (null gnus-current-article)
7186               (null gnus-article-current)
7187               (/= article (cdr gnus-article-current))
7188               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7189           ;; Selected subject is different from current article's.
7190           (gnus-summary-display-article article)
7191         (when article-window
7192           (gnus-eval-in-buffer-window gnus-article-buffer
7193             (setq endp (or (gnus-article-next-page lines)
7194                            (gnus-article-only-boring-p))))
7195           (when endp
7196             (cond (stop
7197                    (gnus-message 3 "End of message"))
7198                   (circular
7199                    (gnus-summary-beginning-of-article))
7200                   (lines
7201                    (gnus-message 3 "End of message"))
7202                   ((null lines)
7203                    (if (and (eq gnus-summary-goto-unread 'never)
7204                             (not (gnus-summary-last-article-p article)))
7205                        (gnus-summary-next-article)
7206                      (gnus-summary-next-unread-article))))))))
7207     (gnus-summary-recenter)
7208     (gnus-summary-position-point)))
7209
7210 (defun gnus-summary-prev-page (&optional lines move)
7211   "Show previous page of selected article.
7212 Argument LINES specifies lines to be scrolled down.
7213 If MOVE, move to the previous unread article if point is at
7214 the beginning of the buffer."
7215   (interactive "P")
7216   (let ((article (gnus-summary-article-number))
7217         (article-window (get-buffer-window gnus-article-buffer t))
7218         endp)
7219     (gnus-configure-windows 'article)
7220     (if (or (null gnus-current-article)
7221             (null gnus-article-current)
7222             (/= article (cdr gnus-article-current))
7223             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7224         ;; Selected subject is different from current article's.
7225         (gnus-summary-display-article article)
7226       (gnus-summary-recenter)
7227       (when article-window
7228         (gnus-eval-in-buffer-window gnus-article-buffer
7229           (setq endp (gnus-article-prev-page lines)))
7230         (when (and move endp)
7231           (cond (lines
7232                  (gnus-message 3 "Beginning of message"))
7233                 ((null lines)
7234                  (if (and (eq gnus-summary-goto-unread 'never)
7235                           (not (gnus-summary-first-article-p article)))
7236                      (gnus-summary-prev-article)
7237                    (gnus-summary-prev-unread-article))))))))
7238   (gnus-summary-position-point))
7239
7240 (defun gnus-summary-prev-page-or-article (&optional lines)
7241   "Show previous page of selected article.
7242 Argument LINES specifies lines to be scrolled down.
7243 If at the beginning of the article, go to the next article."
7244   (interactive "P")
7245   (gnus-summary-prev-page lines t))
7246
7247 (defun gnus-summary-scroll-up (lines)
7248   "Scroll up (or down) one line current article.
7249 Argument LINES specifies lines to be scrolled up (or down if negative)."
7250   (interactive "p")
7251   (gnus-configure-windows 'article)
7252   (gnus-summary-show-thread)
7253   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7254     (gnus-eval-in-buffer-window gnus-article-buffer
7255       (cond ((> lines 0)
7256              (when (gnus-article-next-page lines)
7257                (gnus-message 3 "End of message")))
7258             ((< lines 0)
7259              (gnus-article-prev-page (- lines))))))
7260   (gnus-summary-recenter)
7261   (gnus-summary-position-point))
7262
7263 (defun gnus-summary-scroll-down (lines)
7264   "Scroll down (or up) one line current article.
7265 Argument LINES specifies lines to be scrolled down (or up if negative)."
7266   (interactive "p")
7267   (gnus-summary-scroll-up (- lines)))
7268
7269 (defun gnus-summary-next-same-subject ()
7270   "Select next article which has the same subject as current one."
7271   (interactive)
7272   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7273
7274 (defun gnus-summary-prev-same-subject ()
7275   "Select previous article which has the same subject as current one."
7276   (interactive)
7277   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7278
7279 (defun gnus-summary-next-unread-same-subject ()
7280   "Select next unread article which has the same subject as current one."
7281   (interactive)
7282   (gnus-summary-next-article t (gnus-summary-article-subject)))
7283
7284 (defun gnus-summary-prev-unread-same-subject ()
7285   "Select previous unread article which has the same subject as current one."
7286   (interactive)
7287   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7288
7289 (defun gnus-summary-first-unread-article ()
7290   "Select the first unread article.
7291 Return nil if there are no unread articles."
7292   (interactive)
7293   (prog1
7294       (when (gnus-summary-first-subject t)
7295         (gnus-summary-show-thread)
7296         (gnus-summary-first-subject t)
7297         (gnus-summary-display-article (gnus-summary-article-number)))
7298     (gnus-summary-position-point)))
7299
7300 (defun gnus-summary-first-unread-subject ()
7301   "Place the point on the subject line of the first unread article.
7302 Return nil if there are no unread articles."
7303   (interactive)
7304   (prog1
7305       (when (gnus-summary-first-subject t)
7306         (gnus-summary-show-thread)
7307         (gnus-summary-first-subject t))
7308     (gnus-summary-position-point)))
7309
7310 (defun gnus-summary-first-unseen-subject ()
7311   "Place the point on the subject line of the first unseen article.
7312 Return nil if there are no unseen articles."
7313   (interactive)
7314   (prog1
7315       (when (gnus-summary-first-subject nil nil t)
7316         (gnus-summary-show-thread)
7317         (gnus-summary-first-subject nil nil t))
7318     (gnus-summary-position-point)))
7319
7320 (defun gnus-summary-first-unseen-or-unread-subject ()
7321   "Place the point on the subject line of the first unseen article or,
7322 if all article have been seen, on the subject line of the first unread
7323 article."
7324   (interactive)
7325   (prog1
7326       (unless (when (gnus-summary-first-subject nil nil t)
7327                 (gnus-summary-show-thread)
7328                 (gnus-summary-first-subject nil nil t))
7329         (when (gnus-summary-first-subject t)
7330           (gnus-summary-show-thread)
7331           (gnus-summary-first-subject t)))
7332     (gnus-summary-position-point)))
7333
7334 (defun gnus-summary-first-article ()
7335   "Select the first article.
7336 Return nil if there are no articles."
7337   (interactive)
7338   (prog1
7339       (when (gnus-summary-first-subject)
7340         (gnus-summary-show-thread)
7341         (gnus-summary-first-subject)
7342         (gnus-summary-display-article (gnus-summary-article-number)))
7343     (gnus-summary-position-point)))
7344
7345 (defun gnus-summary-best-unread-article (&optional arg)
7346   "Select the unread article with the highest score.
7347 If given a prefix argument, select the next unread article that has a
7348 score higher than the default score."
7349   (interactive "P")
7350   (let ((article (if arg
7351                      (gnus-summary-better-unread-subject)
7352                    (gnus-summary-best-unread-subject))))
7353     (if article
7354         (gnus-summary-goto-article article)
7355       (error "No unread articles"))))
7356
7357 (defun gnus-summary-best-unread-subject ()
7358   "Select the unread subject with the highest score."
7359   (interactive)
7360   (let ((best -1000000)
7361         (data gnus-newsgroup-data)
7362         article score)
7363     (while data
7364       (and (gnus-data-unread-p (car data))
7365            (> (setq score
7366                     (gnus-summary-article-score (gnus-data-number (car data))))
7367               best)
7368            (setq best score
7369                  article (gnus-data-number (car data))))
7370       (setq data (cdr data)))
7371     (when article
7372       (gnus-summary-goto-subject article))
7373     (gnus-summary-position-point)
7374     article))
7375
7376 (defun gnus-summary-better-unread-subject ()
7377   "Select the first unread subject that has a score over the default score."
7378   (interactive)
7379   (let ((data gnus-newsgroup-data)
7380         article score)
7381     (while (and (setq article (gnus-data-number (car data)))
7382                 (or (gnus-data-read-p (car data))
7383                     (not (> (gnus-summary-article-score article)
7384                             gnus-summary-default-score))))
7385       (setq data (cdr data)))
7386     (when article
7387       (gnus-summary-goto-subject article))
7388     (gnus-summary-position-point)
7389     article))
7390
7391 (defun gnus-summary-last-subject ()
7392   "Go to the last displayed subject line in the group."
7393   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7394     (when article
7395       (gnus-summary-goto-subject article))))
7396
7397 (defun gnus-summary-goto-article (article &optional all-headers force)
7398   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7399 If ALL-HEADERS is non-nil, no header lines are hidden.
7400 If FORCE, go to the article even if it isn't displayed.  If FORCE
7401 is a number, it is the line the article is to be displayed on."
7402   (interactive
7403    (list
7404     (completing-read
7405      "Article number or Message-ID: "
7406      (mapcar (lambda (number) (list (int-to-string number)))
7407              gnus-newsgroup-limit))
7408     current-prefix-arg
7409     t))
7410   (prog1
7411       (if (and (stringp article)
7412                (string-match "@" article))
7413           (gnus-summary-refer-article article)
7414         (when (stringp article)
7415           (setq article (string-to-number article)))
7416         (if (gnus-summary-goto-subject article force)
7417             (gnus-summary-display-article article all-headers)
7418           (gnus-message 4 "Couldn't go to article %s" article) nil))
7419     (gnus-summary-position-point)))
7420
7421 (defun gnus-summary-goto-last-article ()
7422   "Go to the previously read article."
7423   (interactive)
7424   (prog1
7425       (when gnus-last-article
7426         (gnus-summary-goto-article gnus-last-article nil t))
7427     (gnus-summary-position-point)))
7428
7429 (defun gnus-summary-pop-article (number)
7430   "Pop one article off the history and go to the previous.
7431 NUMBER articles will be popped off."
7432   (interactive "p")
7433   (let (to)
7434     (setq gnus-newsgroup-history
7435           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7436     (if to
7437         (gnus-summary-goto-article (car to) nil t)
7438       (error "Article history empty")))
7439   (gnus-summary-position-point))
7440
7441 ;; Summary commands and functions for limiting the summary buffer.
7442
7443 (defun gnus-summary-limit-to-articles (n)
7444   "Limit the summary buffer to the next N articles.
7445 If not given a prefix, use the process marked articles instead."
7446   (interactive "P")
7447   (prog1
7448       (let ((articles (gnus-summary-work-articles n)))
7449         (setq gnus-newsgroup-processable nil)
7450         (gnus-summary-limit articles))
7451     (gnus-summary-position-point)))
7452
7453 (defun gnus-summary-pop-limit (&optional total)
7454   "Restore the previous limit.
7455 If given a prefix, remove all limits."
7456   (interactive "P")
7457   (when total
7458     (setq gnus-newsgroup-limits
7459           (list (mapcar (lambda (h) (mail-header-number h))
7460                         gnus-newsgroup-headers))))
7461   (unless gnus-newsgroup-limits
7462     (error "No limit to pop"))
7463   (prog1
7464       (gnus-summary-limit nil 'pop)
7465     (gnus-summary-position-point)))
7466
7467 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7468   "Limit the summary buffer to articles that have subjects that match a regexp.
7469 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7470   (interactive
7471    (list (read-string (if current-prefix-arg
7472                           "Exclude subject (regexp): "
7473                         "Limit to subject (regexp): "))
7474          nil current-prefix-arg))
7475   (unless header
7476     (setq header "subject"))
7477   (when (not (equal "" subject))
7478     (prog1
7479         (let ((articles (gnus-summary-find-matching
7480                          (or header "subject") subject 'all nil nil
7481                          not-matching)))
7482           (unless articles
7483             (error "Found no matches for \"%s\"" subject))
7484           (gnus-summary-limit articles))
7485       (gnus-summary-position-point))))
7486
7487 (defun gnus-summary-limit-to-author (from &optional not-matching)
7488   "Limit the summary buffer to articles that have authors that match a regexp.
7489 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7490   (interactive
7491    (list (read-string (if current-prefix-arg
7492                           "Exclude author (regexp): "
7493                         "Limit to author (regexp): "))
7494          current-prefix-arg))
7495   (gnus-summary-limit-to-subject from "from" not-matching))
7496
7497 (defun gnus-summary-limit-to-age (age &optional younger-p)
7498   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7499 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7500 articles that are younger than AGE days."
7501   (interactive
7502    (let ((younger current-prefix-arg)
7503          (days-got nil)
7504          days)
7505      (while (not days-got)
7506        (setq days (if younger
7507                       (read-string "Limit to articles younger than (in days, older when negative): ")
7508                     (read-string
7509                      "Limit to articles older than (in days, younger when negative): ")))
7510        (when (> (length days) 0)
7511          (setq days (read days)))
7512        (if (numberp days)
7513            (progn
7514              (setq days-got t)
7515              (if (< days 0)
7516                  (progn
7517                    (setq younger (not younger))
7518                    (setq days (* days -1)))))
7519          (message "Please enter a number.")
7520          (sleep-for 1)))
7521      (list days younger)))
7522   (prog1
7523       (let ((data gnus-newsgroup-data)
7524             (cutoff (days-to-time age))
7525             articles d date is-younger)
7526         (while (setq d (pop data))
7527           (when (and (vectorp (gnus-data-header d))
7528                      (setq date (mail-header-date (gnus-data-header d))))
7529             (setq is-younger (time-less-p
7530                               (time-since (condition-case ()
7531                                               (date-to-time date)
7532                                             (error '(0 0))))
7533                               cutoff))
7534             (when (if younger-p
7535                       is-younger
7536                     (not is-younger))
7537               (push (gnus-data-number d) articles))))
7538         (gnus-summary-limit (nreverse articles)))
7539     (gnus-summary-position-point)))
7540
7541 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7542   "Limit the summary buffer to articles that match an 'extra' header."
7543   (interactive
7544    (let ((header
7545           (intern
7546            (gnus-completing-read-with-default
7547             (symbol-name (car gnus-extra-headers))
7548             (if current-prefix-arg
7549                 "Exclude extra header:"
7550               "Limit extra header:")
7551             (mapcar (lambda (x)
7552                       (cons (symbol-name x) x))
7553                     gnus-extra-headers)
7554             nil
7555             t))))
7556      (list header
7557            (read-string (format "%s header %s (regexp): "
7558                                 (if current-prefix-arg "Exclude" "Limit to")
7559                                 header))
7560            current-prefix-arg)))
7561   (when (not (equal "" regexp))
7562     (prog1
7563         (let ((articles (gnus-summary-find-matching
7564                          (cons 'extra header) regexp 'all nil nil
7565                          not-matching)))
7566           (unless articles
7567             (error "Found no matches for \"%s\"" regexp))
7568           (gnus-summary-limit articles))
7569       (gnus-summary-position-point))))
7570
7571 (defun gnus-summary-limit-to-display-predicate ()
7572   "Limit the summary buffer to the predicated in the `display' group parameter."
7573   (interactive)
7574   (unless gnus-newsgroup-display
7575     (error "There is no `display' group parameter"))
7576   (let (articles)
7577     (dolist (number gnus-newsgroup-articles)
7578       (when (funcall gnus-newsgroup-display)
7579         (push number articles)))
7580     (gnus-summary-limit articles))
7581   (gnus-summary-position-point))
7582
7583 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7584 (make-obsolete
7585  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7586
7587 (defun gnus-summary-limit-to-unread (&optional all)
7588   "Limit the summary buffer to articles that are not marked as read.
7589 If ALL is non-nil, limit strictly to unread articles."
7590   (interactive "P")
7591   (if all
7592       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7593     (gnus-summary-limit-to-marks
7594      ;; Concat all the marks that say that an article is read and have
7595      ;; those removed.
7596      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7597            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7598            gnus-low-score-mark gnus-expirable-mark
7599            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7600            gnus-duplicate-mark gnus-souped-mark)
7601      'reverse)))
7602
7603 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7604 (make-obsolete 'gnus-summary-delete-marked-with
7605                'gnus-summary-limit-exclude-marks)
7606
7607 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7608   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7609 If REVERSE, limit the summary buffer to articles that are marked
7610 with MARKS.  MARKS can either be a string of marks or a list of marks.
7611 Returns how many articles were removed."
7612   (interactive "sMarks: ")
7613   (gnus-summary-limit-to-marks marks t))
7614
7615 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7616   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7617 If REVERSE (the prefix), limit the summary buffer to articles that are
7618 not marked with MARKS.  MARKS can either be a string of marks or a
7619 list of marks.
7620 Returns how many articles were removed."
7621   (interactive "sMarks: \nP")
7622   (prog1
7623       (let ((data gnus-newsgroup-data)
7624             (marks (if (listp marks) marks
7625                      (append marks nil))) ; Transform to list.
7626             articles)
7627         (while data
7628           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7629                   (memq (gnus-data-mark (car data)) marks))
7630             (push (gnus-data-number (car data)) articles))
7631           (setq data (cdr data)))
7632         (gnus-summary-limit articles))
7633     (gnus-summary-position-point)))
7634
7635 (defun gnus-summary-limit-to-score (score)
7636   "Limit to articles with score at or above SCORE."
7637   (interactive "NLimit to articles with score of at least: ")
7638   (let ((data gnus-newsgroup-data)
7639         articles)
7640     (while data
7641       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7642                 score)
7643         (push (gnus-data-number (car data)) articles))
7644       (setq data (cdr data)))
7645     (prog1
7646         (gnus-summary-limit articles)
7647       (gnus-summary-position-point))))
7648
7649 (defun gnus-summary-limit-to-unseen ()
7650   "Limit to unseen articles."
7651   (interactive)
7652   (prog1
7653       (gnus-summary-limit gnus-newsgroup-unseen)
7654     (gnus-summary-position-point)))
7655
7656 (defun gnus-summary-limit-include-thread (id)
7657   "Display all the hidden articles that is in the thread with ID in it.
7658 When called interactively, ID is the Message-ID of the current
7659 article."
7660   (interactive (list (mail-header-id (gnus-summary-article-header))))
7661   (let ((articles (gnus-articles-in-thread
7662                    (gnus-id-to-thread (gnus-root-id id)))))
7663     (prog1
7664         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7665       (gnus-summary-limit-include-matching-articles
7666        "subject"
7667        (regexp-quote (gnus-simplify-subject-re
7668                       (mail-header-subject (gnus-id-to-header id)))))
7669       (gnus-summary-position-point))))
7670
7671 (defun gnus-summary-limit-include-matching-articles (header regexp)
7672   "Display all the hidden articles that have HEADERs that match REGEXP."
7673   (interactive (list (read-string "Match on header: ")
7674                      (read-string "Regexp: ")))
7675   (let ((articles (gnus-find-matching-articles header regexp)))
7676     (prog1
7677         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7678       (gnus-summary-position-point))))
7679
7680 (defun gnus-summary-insert-dormant-articles ()
7681   "Insert all the dormat articles for this group into the current buffer."
7682   (interactive)
7683   (let ((gnus-verbose (max 6 gnus-verbose)))
7684     (if (not gnus-newsgroup-dormant)
7685         (gnus-message 3 "No cached articles for this group")
7686       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7687
7688 (defun gnus-summary-limit-include-dormant ()
7689   "Display all the hidden articles that are marked as dormant.
7690 Note that this command only works on a subset of the articles currently
7691 fetched for this group."
7692   (interactive)
7693   (unless gnus-newsgroup-dormant
7694     (error "There are no dormant articles in this group"))
7695   (prog1
7696       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7697     (gnus-summary-position-point)))
7698
7699 (defun gnus-summary-limit-exclude-dormant ()
7700   "Hide all dormant articles."
7701   (interactive)
7702   (prog1
7703       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7704     (gnus-summary-position-point)))
7705
7706 (defun gnus-summary-limit-exclude-childless-dormant ()
7707   "Hide all dormant articles that have no children."
7708   (interactive)
7709   (let ((data (gnus-data-list t))
7710         articles d children)
7711     ;; Find all articles that are either not dormant or have
7712     ;; children.
7713     (while (setq d (pop data))
7714       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7715                 (and (setq children
7716                            (gnus-article-children (gnus-data-number d)))
7717                      (let (found)
7718                        (while children
7719                          (when (memq (car children) articles)
7720                            (setq children nil
7721                                  found t))
7722                          (pop children))
7723                        found)))
7724         (push (gnus-data-number d) articles)))
7725     ;; Do the limiting.
7726     (prog1
7727         (gnus-summary-limit articles)
7728       (gnus-summary-position-point))))
7729
7730 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7731   "Mark all unread excluded articles as read.
7732 If ALL, mark even excluded ticked and dormants as read."
7733   (interactive "P")
7734   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7735   (let ((articles (gnus-sorted-ndifference
7736                    (sort
7737                     (mapcar (lambda (h) (mail-header-number h))
7738                             gnus-newsgroup-headers)
7739                     '<)
7740                    gnus-newsgroup-limit))
7741         article)
7742     (setq gnus-newsgroup-unreads
7743           (gnus-sorted-intersection gnus-newsgroup-unreads
7744                                     gnus-newsgroup-limit))
7745     (if all
7746         (setq gnus-newsgroup-dormant nil
7747               gnus-newsgroup-marked nil
7748               gnus-newsgroup-reads
7749               (nconc
7750                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7751                gnus-newsgroup-reads))
7752       (while (setq article (pop articles))
7753         (unless (or (memq article gnus-newsgroup-dormant)
7754                     (memq article gnus-newsgroup-marked))
7755           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7756
7757 (defun gnus-summary-limit (articles &optional pop)
7758   (if pop
7759       ;; We pop the previous limit off the stack and use that.
7760       (setq articles (car gnus-newsgroup-limits)
7761             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7762     ;; We use the new limit, so we push the old limit on the stack.
7763     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7764   ;; Set the limit.
7765   (setq gnus-newsgroup-limit articles)
7766   (let ((total (length gnus-newsgroup-data))
7767         (data (gnus-data-find-list (gnus-summary-article-number)))
7768         (gnus-summary-mark-below nil)   ; Inhibit this.
7769         found)
7770     ;; This will do all the work of generating the new summary buffer
7771     ;; according to the new limit.
7772     (gnus-summary-prepare)
7773     ;; Hide any threads, possibly.
7774     (gnus-summary-maybe-hide-threads)
7775     ;; Try to return to the article you were at, or one in the
7776     ;; neighborhood.
7777     (when data
7778       ;; We try to find some article after the current one.
7779       (while data
7780         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7781           (setq data nil
7782                 found t))
7783         (setq data (cdr data))))
7784     (unless found
7785       ;; If there is no data, that means that we were after the last
7786       ;; article.  The same goes when we can't find any articles
7787       ;; after the current one.
7788       (goto-char (point-max))
7789       (gnus-summary-find-prev))
7790     (gnus-set-mode-line 'summary)
7791     ;; We return how many articles were removed from the summary
7792     ;; buffer as a result of the new limit.
7793     (- total (length gnus-newsgroup-data))))
7794
7795 (defsubst gnus-invisible-cut-children (threads)
7796   (let ((num 0))
7797     (while threads
7798       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7799         (incf num))
7800       (pop threads))
7801     (< num 2)))
7802
7803 (defsubst gnus-cut-thread (thread)
7804   "Go forwards in the thread until we find an article that we want to display."
7805   (when (or (eq gnus-fetch-old-headers 'some)
7806             (eq gnus-fetch-old-headers 'invisible)
7807             (numberp gnus-fetch-old-headers)
7808             (eq gnus-build-sparse-threads 'some)
7809             (eq gnus-build-sparse-threads 'more))
7810     ;; Deal with old-fetched headers and sparse threads.
7811     (while (and
7812             thread
7813             (or
7814              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7815              (gnus-summary-article-ancient-p
7816               (mail-header-number (car thread))))
7817             (if (or (<= (length (cdr thread)) 1)
7818                     (eq gnus-fetch-old-headers 'invisible))
7819                 (setq gnus-newsgroup-limit
7820                       (delq (mail-header-number (car thread))
7821                             gnus-newsgroup-limit)
7822                       thread (cadr thread))
7823               (when (gnus-invisible-cut-children (cdr thread))
7824                 (let ((th (cdr thread)))
7825                   (while th
7826                     (if (memq (mail-header-number (caar th))
7827                               gnus-newsgroup-limit)
7828                         (setq thread (car th)
7829                               th nil)
7830                       (setq th (cdr th))))))))))
7831   thread)
7832
7833 (defun gnus-cut-threads (threads)
7834   "Cut off all uninteresting articles from the beginning of threads."
7835   (when (or (eq gnus-fetch-old-headers 'some)
7836             (eq gnus-fetch-old-headers 'invisible)
7837             (numberp gnus-fetch-old-headers)
7838             (eq gnus-build-sparse-threads 'some)
7839             (eq gnus-build-sparse-threads 'more))
7840     (let ((th threads))
7841       (while th
7842         (setcar th (gnus-cut-thread (car th)))
7843         (setq th (cdr th)))))
7844   ;; Remove nixed out threads.
7845   (delq nil threads))
7846
7847 (defun gnus-summary-initial-limit (&optional show-if-empty)
7848   "Figure out what the initial limit is supposed to be on group entry.
7849 This entails weeding out unwanted dormants, low-scored articles,
7850 fetch-old-headers verbiage, and so on."
7851   ;; Most groups have nothing to remove.
7852   (if (or gnus-inhibit-limiting
7853           (and (null gnus-newsgroup-dormant)
7854                (eq gnus-newsgroup-display 'gnus-not-ignore)
7855                (not (eq gnus-fetch-old-headers 'some))
7856                (not (numberp gnus-fetch-old-headers))
7857                (not (eq gnus-fetch-old-headers 'invisible))
7858                (null gnus-summary-expunge-below)
7859                (not (eq gnus-build-sparse-threads 'some))
7860                (not (eq gnus-build-sparse-threads 'more))
7861                (null gnus-thread-expunge-below)
7862                (not gnus-use-nocem)))
7863       ()                                ; Do nothing.
7864     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7865     (setq gnus-newsgroup-limit nil)
7866     (mapatoms
7867      (lambda (node)
7868        (unless (car (symbol-value node))
7869          ;; These threads have no parents -- they are roots.
7870          (let ((nodes (cdr (symbol-value node)))
7871                thread)
7872            (while nodes
7873              (if (and gnus-thread-expunge-below
7874                       (< (gnus-thread-total-score (car nodes))
7875                          gnus-thread-expunge-below))
7876                  (gnus-expunge-thread (pop nodes))
7877                (setq thread (pop nodes))
7878                (gnus-summary-limit-children thread))))))
7879      gnus-newsgroup-dependencies)
7880     ;; If this limitation resulted in an empty group, we might
7881     ;; pop the previous limit and use it instead.
7882     (when (and (not gnus-newsgroup-limit)
7883                show-if-empty)
7884       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7885     gnus-newsgroup-limit))
7886
7887 (defun gnus-summary-limit-children (thread)
7888   "Return 1 if this subthread is visible and 0 if it is not."
7889   ;; First we get the number of visible children to this thread.  This
7890   ;; is done by recursing down the thread using this function, so this
7891   ;; will really go down to a leaf article first, before slowly
7892   ;; working its way up towards the root.
7893   (when thread
7894     (let* ((max-lisp-eval-depth 5000)
7895            (children
7896            (if (cdr thread)
7897                (apply '+ (mapcar 'gnus-summary-limit-children
7898                                  (cdr thread)))
7899              0))
7900           (number (mail-header-number (car thread)))
7901           score)
7902       (if (and
7903            (not (memq number gnus-newsgroup-marked))
7904            (or
7905             ;; If this article is dormant and has absolutely no visible
7906             ;; children, then this article isn't visible.
7907             (and (memq number gnus-newsgroup-dormant)
7908                  (zerop children))
7909             ;; If this is "fetch-old-headered" and there is no
7910             ;; visible children, then we don't want this article.
7911             (and (or (eq gnus-fetch-old-headers 'some)
7912                      (numberp gnus-fetch-old-headers))
7913                  (gnus-summary-article-ancient-p number)
7914                  (zerop children))
7915             ;; If this is "fetch-old-headered" and `invisible', then
7916             ;; we don't want this article.
7917             (and (eq gnus-fetch-old-headers 'invisible)
7918                  (gnus-summary-article-ancient-p number))
7919             ;; If this is a sparsely inserted article with no children,
7920             ;; we don't want it.
7921             (and (eq gnus-build-sparse-threads 'some)
7922                  (gnus-summary-article-sparse-p number)
7923                  (zerop children))
7924             ;; If we use expunging, and this article is really
7925             ;; low-scored, then we don't want this article.
7926             (when (and gnus-summary-expunge-below
7927                        (< (setq score
7928                                 (or (cdr (assq number gnus-newsgroup-scored))
7929                                     gnus-summary-default-score))
7930                           gnus-summary-expunge-below))
7931               ;; We increase the expunge-tally here, but that has
7932               ;; nothing to do with the limits, really.
7933               (incf gnus-newsgroup-expunged-tally)
7934               ;; We also mark as read here, if that's wanted.
7935               (when (and gnus-summary-mark-below
7936                          (< score gnus-summary-mark-below))
7937                 (setq gnus-newsgroup-unreads
7938                       (delq number gnus-newsgroup-unreads))
7939                 (if gnus-newsgroup-auto-expire
7940                     (push number gnus-newsgroup-expirable)
7941                   (push (cons number gnus-low-score-mark)
7942                         gnus-newsgroup-reads)))
7943               t)
7944             ;; Do the `display' group parameter.
7945             (and gnus-newsgroup-display
7946                  (not (funcall gnus-newsgroup-display)))
7947             ;; Check NoCeM things.
7948             (if (and gnus-use-nocem
7949                      (gnus-nocem-unwanted-article-p
7950                       (mail-header-id (car thread))))
7951                 (progn
7952                   (setq gnus-newsgroup-unreads
7953                         (delq number gnus-newsgroup-unreads))
7954                   t))))
7955           ;; Nope, invisible article.
7956           0
7957         ;; Ok, this article is to be visible, so we add it to the limit
7958         ;; and return 1.
7959         (push number gnus-newsgroup-limit)
7960         1))))
7961
7962 (defun gnus-expunge-thread (thread)
7963   "Mark all articles in THREAD as read."
7964   (let* ((number (mail-header-number (car thread))))
7965     (incf gnus-newsgroup-expunged-tally)
7966     ;; We also mark as read here, if that's wanted.
7967     (setq gnus-newsgroup-unreads
7968           (delq number gnus-newsgroup-unreads))
7969     (if gnus-newsgroup-auto-expire
7970         (push number gnus-newsgroup-expirable)
7971       (push (cons number gnus-low-score-mark)
7972             gnus-newsgroup-reads)))
7973   ;; Go recursively through all subthreads.
7974   (mapcar 'gnus-expunge-thread (cdr thread)))
7975
7976 ;; Summary article oriented commands
7977
7978 (defun gnus-summary-refer-parent-article (n)
7979   "Refer parent article N times.
7980 If N is negative, go to ancestor -N instead.
7981 The difference between N and the number of articles fetched is returned."
7982   (interactive "p")
7983   (let ((skip 1)
7984         error header ref)
7985     (when (not (natnump n))
7986       (setq skip (abs n)
7987             n 1))
7988     (while (and (> n 0)
7989                 (not error))
7990       (setq header (gnus-summary-article-header))
7991       (if (and (eq (mail-header-number header)
7992                    (cdr gnus-article-current))
7993                (equal gnus-newsgroup-name
7994                       (car gnus-article-current)))
7995           ;; If we try to find the parent of the currently
7996           ;; displayed article, then we take a look at the actual
7997           ;; References header, since this is slightly more
7998           ;; reliable than the References field we got from the
7999           ;; server.
8000           (save-excursion
8001             (set-buffer gnus-original-article-buffer)
8002             (nnheader-narrow-to-headers)
8003             (unless (setq ref (message-fetch-field "references"))
8004               (setq ref (message-fetch-field "in-reply-to")))
8005             (widen))
8006         (setq ref
8007               ;; It's not the current article, so we take a bet on
8008               ;; the value we got from the server.
8009               (mail-header-references header)))
8010       (if (and ref
8011                (not (equal ref "")))
8012           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8013             (gnus-message 1 "Couldn't find parent"))
8014         (gnus-message 1 "No references in article %d"
8015                       (gnus-summary-article-number))
8016         (setq error t))
8017       (decf n))
8018     (gnus-summary-position-point)
8019     n))
8020
8021 (defun gnus-summary-refer-references ()
8022   "Fetch all articles mentioned in the References header.
8023 Return the number of articles fetched."
8024   (interactive)
8025   (let ((ref (mail-header-references (gnus-summary-article-header)))
8026         (current (gnus-summary-article-number))
8027         (n 0))
8028     (if (or (not ref)
8029             (equal ref ""))
8030         (error "No References in the current article")
8031       ;; For each Message-ID in the References header...
8032       (while (string-match "<[^>]*>" ref)
8033         (incf n)
8034         ;; ... fetch that article.
8035         (gnus-summary-refer-article
8036          (prog1 (match-string 0 ref)
8037            (setq ref (substring ref (match-end 0))))))
8038       (gnus-summary-goto-subject current)
8039       (gnus-summary-position-point)
8040       n)))
8041
8042 (defun gnus-summary-refer-thread (&optional limit)
8043   "Fetch all articles in the current thread.
8044 If LIMIT (the numerical prefix), fetch that many old headers instead
8045 of what's specified by the `gnus-refer-thread-limit' variable."
8046   (interactive "P")
8047   (let ((id (mail-header-id (gnus-summary-article-header)))
8048         (limit (if limit (prefix-numeric-value limit)
8049                  gnus-refer-thread-limit)))
8050     (unless (eq gnus-fetch-old-headers 'invisible)
8051       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8052       ;; Retrieve the headers and read them in.
8053       (if (eq (if (numberp limit)
8054                   (gnus-retrieve-headers
8055                    (list (min
8056                           (+ (mail-header-number
8057                               (gnus-summary-article-header))
8058                              limit)
8059                           gnus-newsgroup-end))
8060                    gnus-newsgroup-name (* limit 2))
8061                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8062                 ;; headers.
8063                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8064                                        gnus-newsgroup-name limit))
8065               'nov)
8066           (gnus-build-all-threads)
8067         (error "Can't fetch thread from backends that don't support NOV"))
8068       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8069     (gnus-summary-limit-include-thread id)))
8070
8071 (defun gnus-summary-refer-article (message-id)
8072   "Fetch an article specified by MESSAGE-ID."
8073   (interactive "sMessage-ID: ")
8074   (when (and (stringp message-id)
8075              (not (zerop (length message-id))))
8076     ;; Construct the correct Message-ID if necessary.
8077     ;; Suggested by tale@pawl.rpi.edu.
8078     (unless (string-match "^<" message-id)
8079       (setq message-id (concat "<" message-id)))
8080     (unless (string-match ">$" message-id)
8081       (setq message-id (concat message-id ">")))
8082     (let* ((header (gnus-id-to-header message-id))
8083            (sparse (and header
8084                         (gnus-summary-article-sparse-p
8085                          (mail-header-number header))
8086                         (memq (mail-header-number header)
8087                               gnus-newsgroup-limit)))
8088            number)
8089       (cond
8090        ;; If the article is present in the buffer we just go to it.
8091        ((and header
8092              (or (not (gnus-summary-article-sparse-p
8093                        (mail-header-number header)))
8094                  sparse))
8095         (prog1
8096             (gnus-summary-goto-article
8097              (mail-header-number header) nil t)
8098           (when sparse
8099             (gnus-summary-update-article (mail-header-number header)))))
8100        (t
8101         ;; We fetch the article.
8102         (catch 'found
8103           (dolist (gnus-override-method (gnus-refer-article-methods))
8104             (when (and (gnus-check-server gnus-override-method)
8105                        ;; Fetch the header,
8106                        (setq number (gnus-summary-insert-subject message-id)))
8107               ;; and display the article.
8108               (gnus-summary-select-article nil nil nil number)
8109               (throw 'found t)))
8110           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8111
8112 (defun gnus-refer-article-methods ()
8113   "Return a list of referable methods."
8114   (cond
8115    ;; No method, so we default to current and native.
8116    ((null gnus-refer-article-method)
8117     (list gnus-current-select-method gnus-select-method))
8118    ;; Current.
8119    ((eq 'current gnus-refer-article-method)
8120     (list gnus-current-select-method))
8121    ;; List of select methods.
8122    ((not (and (symbolp (car gnus-refer-article-method))
8123               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8124     (let (out)
8125       (dolist (method gnus-refer-article-method)
8126         (push (if (eq 'current method)
8127                   gnus-current-select-method
8128                 method)
8129               out))
8130       (nreverse out)))
8131    ;; One single select method.
8132    (t
8133     (list gnus-refer-article-method))))
8134
8135 (defun gnus-summary-edit-parameters ()
8136   "Edit the group parameters of the current group."
8137   (interactive)
8138   (gnus-group-edit-group gnus-newsgroup-name 'params))
8139
8140 (defun gnus-summary-customize-parameters ()
8141   "Customize the group parameters of the current group."
8142   (interactive)
8143   (gnus-group-customize gnus-newsgroup-name))
8144
8145 (defun gnus-summary-enter-digest-group (&optional force)
8146   "Enter an nndoc group based on the current article.
8147 If FORCE, force a digest interpretation.  If not, try
8148 to guess what the document format is."
8149   (interactive "P")
8150   (let ((conf gnus-current-window-configuration))
8151     (save-excursion
8152       (gnus-summary-select-article))
8153     (setq gnus-current-window-configuration conf)
8154     (let* ((name (format "%s-%d"
8155                          (gnus-group-prefixed-name
8156                           gnus-newsgroup-name (list 'nndoc ""))
8157                          (save-excursion
8158                            (set-buffer gnus-summary-buffer)
8159                            gnus-current-article)))
8160            (ogroup gnus-newsgroup-name)
8161            (params (append (gnus-info-params (gnus-get-info ogroup))
8162                            (list (cons 'to-group ogroup))
8163                            (list (cons 'save-article-group ogroup))))
8164            (case-fold-search t)
8165            (buf (current-buffer))
8166            dig to-address)
8167       (save-excursion
8168         (set-buffer gnus-original-article-buffer)
8169         ;; Have the digest group inherit the main mail address of
8170         ;; the parent article.
8171         (when (setq to-address (or (gnus-fetch-field "reply-to")
8172                                    (gnus-fetch-field "from")))
8173           (setq params (append
8174                         (list (cons 'to-address
8175                                     (funcall gnus-decode-encoded-word-function
8176                                              to-address))))))
8177         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8178         (insert-buffer-substring gnus-original-article-buffer)
8179         ;; Remove lines that may lead nndoc to misinterpret the
8180         ;; document type.
8181         (narrow-to-region
8182          (goto-char (point-min))
8183          (or (search-forward "\n\n" nil t) (point)))
8184         (goto-char (point-min))
8185         (delete-matching-lines "^Path:\\|^From ")
8186         (widen))
8187       (unwind-protect
8188           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8189                     (gnus-newsgroup-ephemeral-ignored-charsets
8190                      gnus-newsgroup-ignored-charsets))
8191                 (gnus-group-read-ephemeral-group
8192                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8193                               (nndoc-article-type
8194                                ,(if force 'mbox 'guess)))
8195                  t nil nil nil
8196                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8197                                                         "ADAPT")))))
8198               ;; Make all postings to this group go to the parent group.
8199               (nconc (gnus-info-params (gnus-get-info name))
8200                      params)
8201             ;; Couldn't select this doc group.
8202             (switch-to-buffer buf)
8203             (gnus-set-global-variables)
8204             (gnus-configure-windows 'summary)
8205             (gnus-message 3 "Article couldn't be entered?"))
8206         (kill-buffer dig)))))
8207
8208 (defun gnus-summary-read-document (n)
8209   "Open a new group based on the current article(s).
8210 This will allow you to read digests and other similar
8211 documents as newsgroups.
8212 Obeys the standard process/prefix convention."
8213   (interactive "P")
8214   (let* ((articles (gnus-summary-work-articles n))
8215          (ogroup gnus-newsgroup-name)
8216          (params (append (gnus-info-params (gnus-get-info ogroup))
8217                          (list (cons 'to-group ogroup))))
8218          article group egroup groups vgroup)
8219     (while (setq article (pop articles))
8220       (setq group (format "%s-%d" gnus-newsgroup-name article))
8221       (gnus-summary-remove-process-mark article)
8222       (when (gnus-summary-display-article article)
8223         (save-excursion
8224           (with-temp-buffer
8225             (insert-buffer-substring gnus-original-article-buffer)
8226             ;; Remove some headers that may lead nndoc to make
8227             ;; the wrong guess.
8228             (message-narrow-to-head)
8229             (goto-char (point-min))
8230             (delete-matching-lines "^\\(Path\\):\\|^From ")
8231             (widen)
8232             (if (setq egroup
8233                       (gnus-group-read-ephemeral-group
8234                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8235                                      (nndoc-article-type guess))
8236                        t nil t))
8237                 (progn
8238                   ;; Make all postings to this group go to the parent group.
8239                   (nconc (gnus-info-params (gnus-get-info egroup))
8240                          params)
8241                   (push egroup groups))
8242               ;; Couldn't select this doc group.
8243               (gnus-error 3 "Article couldn't be entered"))))))
8244     ;; Now we have selected all the documents.
8245     (cond
8246      ((not groups)
8247       (error "None of the articles could be interpreted as documents"))
8248      ((gnus-group-read-ephemeral-group
8249        (setq vgroup (format
8250                      "nnvirtual:%s-%s" gnus-newsgroup-name
8251                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8252        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8253        t
8254        (cons (current-buffer) 'summary)))
8255      (t
8256       (error "Couldn't select virtual nndoc group")))))
8257
8258 (defun gnus-summary-isearch-article (&optional regexp-p)
8259   "Do incremental search forward on the current article.
8260 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8261   (interactive "P")
8262   (let* ((gnus-inhibit-treatment t)
8263          (old (gnus-summary-select-article)))
8264     (gnus-configure-windows 'article)
8265     (gnus-eval-in-buffer-window gnus-article-buffer
8266       (save-restriction
8267         (widen)
8268         (when (eq 'old old)
8269           (gnus-article-show-all-headers))
8270         (goto-char (point-min))
8271         (isearch-forward regexp-p)))))
8272
8273 (defun gnus-summary-search-article-forward (regexp &optional backward)
8274   "Search for an article containing REGEXP forward.
8275 If BACKWARD, search backward instead."
8276   (interactive
8277    (list (read-string
8278           (format "Search article %s (regexp%s): "
8279                   (if current-prefix-arg "backward" "forward")
8280                   (if gnus-last-search-regexp
8281                       (concat ", default " gnus-last-search-regexp)
8282                     "")))
8283          current-prefix-arg))
8284   (if (string-equal regexp "")
8285       (setq regexp (or gnus-last-search-regexp ""))
8286     (setq gnus-last-search-regexp regexp)
8287     (setq gnus-article-before-search gnus-current-article))
8288   ;; Intentionally set gnus-last-article.
8289   (setq gnus-last-article gnus-article-before-search)
8290   (let ((gnus-last-article gnus-last-article))
8291     (if (gnus-summary-search-article regexp backward)
8292         (gnus-summary-show-thread)
8293       (error "Search failed: \"%s\"" regexp))))
8294
8295 (defun gnus-summary-search-article-backward (regexp)
8296   "Search for an article containing REGEXP backward."
8297   (interactive
8298    (list (read-string
8299           (format "Search article backward (regexp%s): "
8300                   (if gnus-last-search-regexp
8301                       (concat ", default " gnus-last-search-regexp)
8302                     "")))))
8303   (gnus-summary-search-article-forward regexp 'backward))
8304
8305 (eval-when-compile
8306   (defmacro gnus-summary-search-article-position-point (regexp backward)
8307     "Dehighlight the last matched text and goto the beginning position."
8308     (` (if (and gnus-summary-search-article-matched-data
8309                 (let ((text (caddr gnus-summary-search-article-matched-data))
8310                       (inhibit-read-only t)
8311                       buffer-read-only)
8312                   (delete-region
8313                    (goto-char (car gnus-summary-search-article-matched-data))
8314                    (cadr gnus-summary-search-article-matched-data))
8315                   (insert text)
8316                   (string-match (, regexp) text)))
8317            (if (, backward) (beginning-of-line) (end-of-line))
8318          (goto-char (if (, backward) (point-max) (point-min))))))
8319
8320   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8321     "Place point where X-Face image is displayed."
8322     (if (featurep 'xemacs)
8323         (` (let ((end (if (search-forward "\n\n" nil t)
8324                           (goto-char (1- (point)))
8325                         (point-min)))
8326                  extent)
8327              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8328              (unless (and (re-search-forward "^From:" end t)
8329                           (setq extent (extent-at (point)))
8330                           (extent-begin-glyph extent))
8331                (goto-char (, opoint)))))
8332       (` (let ((end (if (search-forward "\n\n" nil t)
8333                         (goto-char (1- (point)))
8334                       (point-min)))
8335                (start (or (search-backward "\n\n" nil t) (point-min))))
8336            (goto-char
8337             (or (text-property-any start end 'x-face-image t);; x-face-e21
8338                 (text-property-any start end 'x-face-mule-bitmap-image t)
8339                 (, opoint)))))))
8340
8341   (defmacro gnus-summary-search-article-highlight-matched-text
8342     (backward treated x-face)
8343     "Highlight matched text in the function `gnus-summary-search-article'."
8344     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8345              (end (set-marker (make-marker) (match-end 0)))
8346              (inhibit-read-only t)
8347              buffer-read-only)
8348          (unless treated
8349            (let ((,@
8350                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8351                     (mapcar
8352                      (lambda (item) (setq items (delq item items)))
8353                      '(gnus-treat-buttonize
8354                        gnus-treat-fill-article
8355                        gnus-treat-fill-long-lines
8356                        gnus-treat-emphasize
8357                        gnus-treat-highlight-headers
8358                        gnus-treat-highlight-citation
8359                        gnus-treat-highlight-signature
8360                        gnus-treat-overstrike
8361                        gnus-treat-display-xface
8362                        gnus-treat-buttonize-head
8363                        gnus-treat-decode-article-as-default-mime-charset))
8364                     (static-if (featurep 'xemacs)
8365                         items
8366                       (cons '(x-face-mule-delete-x-face-field
8367                               (quote never))
8368                             items))))
8369                  (gnus-treat-display-xface
8370                   (when (, x-face) gnus-treat-display-xface)))
8371              (gnus-article-prepare-mime-display)))
8372          (goto-char (if (, backward) start end))
8373          (when (, x-face)
8374            (gnus-summary-search-article-highlight-goto-x-face (point)))
8375          (setq gnus-summary-search-article-matched-data
8376                (list start end (buffer-substring start end)))
8377          (unless (eq start end);; matched text has been deleted. :-<
8378            (put-text-property start end 'face
8379                               (or (find-face 'isearch)
8380                                   'secondary-selection))))))
8381   )
8382
8383 (defun gnus-summary-search-article (regexp &optional backward)
8384   "Search for an article containing REGEXP.
8385 Optional argument BACKWARD means do search for backward.
8386 `gnus-select-article-hook' is not called during the search."
8387   ;; We have to require this here to make sure that the following
8388   ;; dynamic binding isn't shadowed by autoloading.
8389   (require 'gnus-async)
8390   (require 'gnus-art)
8391   (let ((gnus-select-article-hook nil)  ;Disable hook.
8392         (gnus-article-prepare-hook nil)
8393         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8394         (gnus-use-article-prefetch nil)
8395         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8396         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8397         (gnus-visual nil)
8398         (gnus-keep-backlog nil)
8399         (gnus-break-pages nil)
8400         (gnus-summary-display-arrow nil)
8401         (gnus-updated-mode-lines nil)
8402         (gnus-auto-center-summary nil)
8403         (sum (current-buffer))
8404         (found nil)
8405         point treated)
8406     (gnus-save-hidden-threads
8407       (static-if (featurep 'xemacs)
8408           (let ((gnus-inhibit-treatment t))
8409             (setq treated (eq 'old (gnus-summary-select-article)))
8410             (when (and treated
8411                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8412                                  (window-live-p (get-buffer-window
8413                                                  gnus-article-buffer t)))))
8414               (gnus-summary-select-article nil t)
8415               (setq treated nil)))
8416         (let ((gnus-inhibit-treatment t)
8417               (x-face-mule-delete-x-face-field 'never))
8418           (setq treated (eq 'old (gnus-summary-select-article)))
8419           (when (and treated
8420                      (not
8421                       (and (gnus-buffer-live-p gnus-article-buffer)
8422                            (window-live-p (get-buffer-window
8423                                            gnus-article-buffer t))
8424                            (or (not (string-match "^\\^X-Face:" regexp))
8425                                (with-current-buffer gnus-article-buffer
8426                                  gnus-summary-search-article-matched-data)))))
8427             (gnus-summary-select-article nil t)
8428             (setq treated nil))))
8429       (set-buffer gnus-article-buffer)
8430       (widen)
8431       (if treated
8432           (progn
8433             (gnus-article-show-all-headers)
8434             (gnus-summary-search-article-position-point regexp backward))
8435         (goto-char (if backward (point-max) (point-min))))
8436       (while (not found)
8437         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8438         (if (if backward
8439                 (re-search-backward regexp nil t)
8440               (re-search-forward regexp nil t))
8441             ;; We found the regexp.
8442             (progn
8443               (gnus-summary-search-article-highlight-matched-text
8444                backward treated (string-match "^\\^X-Face:" regexp))
8445               (setq found 'found)
8446               (forward-line
8447                (/ (- 2 (window-height
8448                         (get-buffer-window gnus-article-buffer t)))
8449                   2))
8450               (set-window-start
8451                (get-buffer-window (current-buffer))
8452                (point))
8453               (set-buffer sum)
8454               (setq point (point)))
8455           ;; We didn't find it, so we go to the next article.
8456           (set-buffer sum)
8457           (setq found 'not)
8458           (while (eq found 'not)
8459             (if (not (if backward (gnus-summary-find-prev)
8460                        (gnus-summary-find-next)))
8461                 ;; No more articles.
8462                 (setq found t)
8463               ;; Select the next article and adjust point.
8464               (unless (gnus-summary-article-sparse-p
8465                        (gnus-summary-article-number))
8466                 (setq found nil)
8467                 (let ((gnus-inhibit-treatment t))
8468                   (gnus-summary-select-article))
8469                 (setq treated nil)
8470                 (set-buffer gnus-article-buffer)
8471                 (widen)
8472                 (goto-char (if backward (point-max) (point-min))))))))
8473       (gnus-message 7 ""))
8474     ;; Return whether we found the regexp.
8475     (when (eq found 'found)
8476       (goto-char point)
8477       (gnus-summary-show-thread)
8478       (gnus-summary-goto-subject gnus-current-article)
8479       (gnus-summary-position-point)
8480       t)))
8481
8482 (defun gnus-find-matching-articles (header regexp)
8483   "Return a list of all articles that match REGEXP on HEADER.
8484 This search includes all articles in the current group that Gnus has
8485 fetched headers for, whether they are displayed or not."
8486   (let ((articles nil)
8487         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8488         (case-fold-search t))
8489     (dolist (header gnus-newsgroup-headers)
8490       (when (string-match regexp (funcall func header))
8491         (push (mail-header-number header) articles)))
8492     (nreverse articles)))
8493
8494 (defun gnus-summary-find-matching (header regexp &optional backward unread
8495                                           not-case-fold not-matching)
8496   "Return a list of all articles that match REGEXP on HEADER.
8497 The search stars on the current article and goes forwards unless
8498 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8499 If UNREAD is non-nil, only unread articles will
8500 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8501 in the comparisons. If NOT-MATCHING, return a list of all articles that
8502 not match REGEXP on HEADER."
8503   (let ((case-fold-search (not not-case-fold))
8504         articles d func)
8505     (if (consp header)
8506         (if (eq (car header) 'extra)
8507             (setq func
8508                   `(lambda (h)
8509                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8510                          "")))
8511           (error "%s is an invalid header" header))
8512       (unless (fboundp (intern (concat "mail-header-" header)))
8513         (error "%s is not a valid header" header))
8514       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8515     (dolist (d (if (eq backward 'all)
8516                    gnus-newsgroup-data
8517                  (gnus-data-find-list
8518                   (gnus-summary-article-number)
8519                   (gnus-data-list backward))))
8520       (when (and (or (not unread)       ; We want all articles...
8521                      (gnus-data-unread-p d)) ; Or just unreads.
8522                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8523                  (if not-matching
8524                      (not (string-match
8525                            regexp
8526                            (funcall func (gnus-data-header d))))
8527                    (string-match regexp
8528                                  (funcall func (gnus-data-header d)))))
8529         (push (gnus-data-number d) articles))) ; Success!
8530     (nreverse articles)))
8531
8532 (defun gnus-summary-execute-command (header regexp command &optional backward)
8533   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8534 If HEADER is an empty string (or nil), the match is done on the entire
8535 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8536   (interactive
8537    (list (let ((completion-ignore-case t))
8538            (completing-read
8539             "Header name: "
8540             (mapcar (lambda (header) (list (format "%s" header)))
8541                     (append
8542                      '("Number" "Subject" "From" "Lines" "Date"
8543                        "Message-ID" "Xref" "References" "Body")
8544                      gnus-extra-headers))
8545             nil 'require-match))
8546          (read-string "Regexp: ")
8547          (read-key-sequence "Command: ")
8548          current-prefix-arg))
8549   (when (equal header "Body")
8550     (setq header ""))
8551   ;; Hidden thread subtrees must be searched as well.
8552   (gnus-summary-show-all-threads)
8553   ;; We don't want to change current point nor window configuration.
8554   (save-excursion
8555     (save-window-excursion
8556       (let (gnus-visual
8557             gnus-treat-strip-trailing-blank-lines
8558             gnus-treat-strip-leading-blank-lines
8559             gnus-treat-strip-multiple-blank-lines
8560             gnus-treat-hide-boring-headers
8561             gnus-treat-fold-newsgroups
8562             gnus-article-prepare-hook)
8563         (gnus-message 6 "Executing %s..." (key-description command))
8564         ;; We'd like to execute COMMAND interactively so as to give arguments.
8565         (gnus-execute header regexp
8566                       `(call-interactively ',(key-binding command))
8567                       backward)
8568         (gnus-message 6 "Executing %s...done" (key-description command))))))
8569
8570 (defun gnus-summary-beginning-of-article ()
8571   "Scroll the article back to the beginning."
8572   (interactive)
8573   (gnus-summary-select-article)
8574   (gnus-configure-windows 'article)
8575   (gnus-eval-in-buffer-window gnus-article-buffer
8576     (widen)
8577     (goto-char (point-min))
8578     (when gnus-page-broken
8579       (gnus-narrow-to-page))))
8580
8581 (defun gnus-summary-end-of-article ()
8582   "Scroll to the end of the article."
8583   (interactive)
8584   (gnus-summary-select-article)
8585   (gnus-configure-windows 'article)
8586   (gnus-eval-in-buffer-window gnus-article-buffer
8587     (widen)
8588     (goto-char (point-max))
8589     (recenter -3)
8590     (when gnus-page-broken
8591       (gnus-narrow-to-page))))
8592
8593 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8594   "Truncate to LEN and quote all \"(\"'s in STRING."
8595   (gnus-replace-in-string (if (and len (> (length string) len))
8596                               (substring string 0 len)
8597                             string)
8598                           "[()]" "\\\\\\&"))
8599
8600 (defun gnus-summary-print-article (&optional filename n)
8601   "Generate and print a PostScript image of the N next (mail) articles.
8602
8603 If N is negative, print the N previous articles.  If N is nil and articles
8604 have been marked with the process mark, print these instead.
8605
8606 If the optional first argument FILENAME is nil, send the image to the
8607 printer.  If FILENAME is a string, save the PostScript image in a file with
8608 that name.  If FILENAME is a number, prompt the user for the name of the file
8609 to save in."
8610   (interactive (list (ps-print-preprint current-prefix-arg)))
8611   (dolist (article (gnus-summary-work-articles n))
8612     (gnus-summary-select-article nil nil 'pseudo article)
8613     (gnus-eval-in-buffer-window gnus-article-buffer
8614       (gnus-print-buffer))
8615     (gnus-summary-remove-process-mark article))
8616   (ps-despool filename))
8617
8618 (defun gnus-print-buffer ()
8619   (let ((buffer (generate-new-buffer " *print*")))
8620     (unwind-protect
8621         (progn
8622           (copy-to-buffer buffer (point-min) (point-max))
8623           (set-buffer buffer)
8624           (gnus-article-delete-invisible-text)
8625           (gnus-remove-text-with-property 'gnus-decoration)
8626           (when (gnus-visual-p 'article-highlight 'highlight)
8627             ;; Copy-to-buffer doesn't copy overlay.  So redo
8628             ;; highlight.
8629             (let ((gnus-article-buffer buffer))
8630               (gnus-article-highlight-citation t)
8631               (gnus-article-highlight-signature)))
8632           (let ((ps-left-header
8633                  (list
8634                   (concat "("
8635                           (gnus-summary-print-truncate-and-quote
8636                            (mail-header-subject gnus-current-headers)
8637                            66) ")")
8638                   (concat "("
8639                           (gnus-summary-print-truncate-and-quote
8640                            (mail-header-from gnus-current-headers)
8641                            45) ")")))
8642                 (ps-right-header
8643                  (list
8644                   "/pagenumberstring load"
8645                   (concat "("
8646                           (mail-header-date gnus-current-headers) ")"))))
8647             (gnus-run-hooks 'gnus-ps-print-hook)
8648             (save-excursion
8649               (if window-system
8650                   (ps-spool-buffer-with-faces)
8651                 (ps-spool-buffer)))))
8652       (kill-buffer buffer))))
8653
8654 (defun gnus-summary-show-article (&optional arg)
8655   "Force redisplaying of the current article.
8656 If ARG (the prefix) is a number, show the article with the charset
8657 defined in `gnus-summary-show-article-charset-alist', or the charset
8658 input.
8659 If ARG (the prefix) is non-nil and not a number, show the raw article
8660 without any article massaging functions being run.  Normally, the key
8661 strokes are `C-u g'."
8662   (interactive "P")
8663   (cond
8664    ((numberp arg)
8665     (gnus-summary-show-article t)
8666     (let* ((gnus-newsgroup-charset
8667             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8668                 (mm-read-coding-system
8669                  "View as charset: " ;; actually it is coding system.
8670                  (save-excursion
8671                    (set-buffer gnus-article-buffer)
8672                    (mm-detect-coding-region (point) (point-max))))))
8673            (default-mime-charset gnus-newsgroup-charset)
8674            (gnus-newsgroup-ignored-charsets 'gnus-all))
8675       (gnus-summary-select-article nil 'force)
8676       (let ((deps gnus-newsgroup-dependencies)
8677             head header lines)
8678         (save-excursion
8679           (set-buffer gnus-original-article-buffer)
8680           (save-restriction
8681             (message-narrow-to-head)
8682             (setq head (buffer-string))
8683             (goto-char (point-min))
8684             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8685               (goto-char (point-max))
8686               (widen)
8687               (setq lines (1- (count-lines (point) (point-max))))))
8688           (with-temp-buffer
8689             (insert (format "211 %d Article retrieved.\n"
8690                             (cdr gnus-article-current)))
8691             (insert head)
8692             (if lines (insert (format "Lines: %d\n" lines)))
8693             (insert ".\n")
8694             (let ((nntp-server-buffer (current-buffer)))
8695               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8696         (gnus-data-set-header
8697          (gnus-data-find (cdr gnus-article-current))
8698          header)
8699         (gnus-summary-update-article-line
8700          (cdr gnus-article-current) header)
8701         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8702           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8703    ((not arg)
8704     ;; Select the article the normal way.
8705     (gnus-summary-select-article nil 'force))
8706    (t
8707     ;; We have to require this here to make sure that the following
8708     ;; dynamic binding isn't shadowed by autoloading.
8709     (require 'gnus-async)
8710     (require 'gnus-art)
8711     ;; Bind the article treatment functions to nil.
8712     (let ((gnus-have-all-headers t)
8713           gnus-article-prepare-hook
8714           gnus-article-decode-hook
8715           gnus-break-pages
8716           gnus-show-mime
8717           (gnus-inhibit-treatment t))
8718       (gnus-summary-select-article nil 'force))))
8719   (gnus-summary-goto-subject gnus-current-article)
8720   (gnus-summary-position-point))
8721
8722 (defun gnus-summary-show-raw-article ()
8723   "Show the raw article without any article massaging functions being run."
8724   (interactive)
8725   (gnus-summary-show-article t))
8726
8727 (defun gnus-summary-verbose-headers (&optional arg)
8728   "Toggle permanent full header display.
8729 If ARG is a positive number, turn header display on.
8730 If ARG is a negative number, turn header display off."
8731   (interactive "P")
8732   (setq gnus-show-all-headers
8733         (cond ((or (not (numberp arg))
8734                    (zerop arg))
8735                (not gnus-show-all-headers))
8736               ((natnump arg)
8737                t)))
8738   (gnus-summary-show-article))
8739
8740 (defun gnus-summary-toggle-header (&optional arg)
8741   "Show the headers if they are hidden, or hide them if they are shown.
8742 If ARG is a positive number, show the entire header.
8743 If ARG is a negative number, hide the unwanted header lines."
8744   (interactive "P")
8745   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8746                      (get-buffer-window gnus-article-buffer t))))
8747     (with-current-buffer gnus-article-buffer
8748       (widen)
8749       (article-narrow-to-head)
8750       (let* ((buffer-read-only nil)
8751              (inhibit-point-motion-hooks t)
8752              (hidden (if (numberp arg)
8753                          (>= arg 0)
8754                        (gnus-article-hidden-text-p 'headers)))
8755              s e)
8756         (delete-region (point-min) (point-max))
8757         (with-current-buffer gnus-original-article-buffer
8758           (goto-char (setq s (point-min)))
8759           (setq e (if (search-forward "\n\n" nil t)
8760                       (1- (point))
8761                     (point-max))))
8762         (insert-buffer-substring gnus-original-article-buffer s e)
8763         (run-hooks 'gnus-article-decode-hook)
8764         (if hidden
8765             (let ((gnus-treat-hide-headers nil)
8766                   (gnus-treat-hide-boring-headers nil))
8767               (gnus-delete-wash-type 'headers)
8768               (gnus-treat-article 'head))
8769           (gnus-treat-article 'head))
8770         (widen)
8771         (if window
8772             (set-window-start window (goto-char (point-min))))
8773         (setq gnus-page-broken
8774               (when gnus-break-pages
8775                 (gnus-narrow-to-page)
8776                 t))
8777         (gnus-set-mode-line 'article)))))
8778
8779 (defun gnus-summary-show-all-headers ()
8780   "Make all header lines visible."
8781   (interactive)
8782   (gnus-summary-toggle-header 1))
8783
8784 (defun gnus-summary-toggle-mime (&optional arg)
8785   "Toggle MIME processing.
8786 If ARG is a positive number, turn MIME processing on."
8787   (interactive "P")
8788   (setq gnus-show-mime
8789         (if (null arg)
8790             (not gnus-show-mime)
8791           (> (prefix-numeric-value arg) 0)))
8792   (gnus-summary-select-article t 'force))
8793
8794 (defun gnus-summary-caesar-message (&optional arg)
8795   "Caesar rotate the current article by 13.
8796 The numerical prefix specifies how many places to rotate each letter
8797 forward."
8798   (interactive "P")
8799   (gnus-summary-select-article)
8800   (let ((mail-header-separator ""))
8801     (gnus-eval-in-buffer-window gnus-article-buffer
8802       (save-restriction
8803         (widen)
8804         (let ((start (window-start))
8805               buffer-read-only)
8806           (message-caesar-buffer-body arg)
8807           (set-window-start (get-buffer-window (current-buffer)) start))))))
8808
8809 (autoload 'unmorse-region "morse"
8810   "Convert morse coded text in region to ordinary ASCII text."
8811   t)
8812
8813 (defun gnus-summary-morse-message (&optional arg)
8814   "Morse decode the current article."
8815   (interactive "P")
8816   (gnus-summary-select-article)
8817   (let ((mail-header-separator ""))
8818     (gnus-eval-in-buffer-window gnus-article-buffer
8819       (save-excursion
8820         (save-restriction
8821           (widen)
8822           (let ((pos (window-start))
8823                 buffer-read-only)
8824             (goto-char (point-min))
8825             (when (message-goto-body)
8826               (gnus-narrow-to-body))
8827             (goto-char (point-min))
8828             (while (re-search-forward "·" (point-max) t)
8829               (replace-match "."))
8830             (unmorse-region (point-min) (point-max))
8831             (widen)
8832             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8833
8834 (defun gnus-summary-stop-page-breaking ()
8835   "Stop page breaking in the current article."
8836   (interactive)
8837   (gnus-summary-select-article)
8838   (gnus-eval-in-buffer-window gnus-article-buffer
8839     (widen)
8840     (when (gnus-visual-p 'page-marker)
8841       (let ((buffer-read-only nil))
8842         (gnus-remove-text-with-property 'gnus-prev)
8843         (gnus-remove-text-with-property 'gnus-next))
8844       (setq gnus-page-broken nil))))
8845
8846 (defun gnus-summary-move-article (&optional n to-newsgroup
8847                                             select-method action)
8848   "Move the current article to a different newsgroup.
8849 If N is a positive number, move the N next articles.
8850 If N is a negative number, move the N previous articles.
8851 If N is nil and any articles have been marked with the process mark,
8852 move those articles instead.
8853 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8854 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8855 re-spool using this method.
8856
8857 When called interactively with TO-NEWSGROUP being nil, the value of
8858 the variable `gnus-move-split-methods' is used for finding a default
8859 for the target newsgroup.
8860
8861 For this function to work, both the current newsgroup and the
8862 newsgroup that you want to move to have to support the `request-move'
8863 and `request-accept' functions.
8864
8865 ACTION can be either `move' (the default), `crosspost' or `copy'."
8866   (interactive "P")
8867   (unless action
8868     (setq action 'move))
8869   ;; Check whether the source group supports the required functions.
8870   (cond ((and (eq action 'move)
8871               (not (gnus-check-backend-function
8872                     'request-move-article gnus-newsgroup-name)))
8873          (error "The current group does not support article moving"))
8874         ((and (eq action 'crosspost)
8875               (not (gnus-check-backend-function
8876                     'request-replace-article gnus-newsgroup-name)))
8877          (error "The current group does not support article editing")))
8878   (let ((articles (gnus-summary-work-articles n))
8879         (prefix (if (gnus-check-backend-function
8880                      'request-move-article gnus-newsgroup-name)
8881                     (gnus-group-real-prefix gnus-newsgroup-name)
8882                   ""))
8883         (names '((move "Move" "Moving")
8884                  (copy "Copy" "Copying")
8885                  (crosspost "Crosspost" "Crossposting")))
8886         (copy-buf (save-excursion
8887                     (nnheader-set-temp-buffer " *copy article*")))
8888         (default-marks gnus-article-mark-lists)
8889         (no-expire-marks (delete '(expirable . expire)
8890                                  (copy-sequence gnus-article-mark-lists)))
8891         art-group to-method new-xref article to-groups)
8892     (unless (assq action names)
8893       (error "Unknown action %s" action))
8894     ;; Read the newsgroup name.
8895     (when (and (not to-newsgroup)
8896                (not select-method))
8897       (if (and gnus-move-split-methods
8898                (not
8899                 (and (memq gnus-current-article articles)
8900                      (gnus-buffer-live-p gnus-original-article-buffer))))
8901           ;; When `gnus-move-split-methods' is non-nil, we have to
8902           ;; select an article to give `gnus-read-move-group-name' an
8903           ;; opportunity to suggest an appropriate default.  However,
8904           ;; we needn't render or mark the article.
8905           (let ((gnus-display-mime-function nil)
8906                 (gnus-article-prepare-hook nil)
8907                 (gnus-mark-article-hook nil))
8908             (gnus-summary-select-article nil nil nil (car articles))))
8909       (setq to-newsgroup
8910             (gnus-read-move-group-name
8911              (cadr (assq action names))
8912              (symbol-value (intern (format "gnus-current-%s-group" action)))
8913              articles prefix))
8914       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8915     (setq to-method (or select-method
8916                         (gnus-server-to-method
8917                          (gnus-group-method to-newsgroup))))
8918     ;; Check the method we are to move this article to...
8919     (unless (gnus-check-backend-function
8920              'request-accept-article (car to-method))
8921       (error "%s does not support article copying" (car to-method)))
8922     (unless (gnus-check-server to-method)
8923       (error "Can't open server %s" (car to-method)))
8924     (gnus-message 6 "%s to %s: %s..."
8925                   (caddr (assq action names))
8926                   (or (car select-method) to-newsgroup) articles)
8927     (while articles
8928       (setq article (pop articles))
8929       (setq
8930        art-group
8931        (cond
8932         ;; Move the article.
8933         ((eq action 'move)
8934          ;; Remove this article from future suppression.
8935          (gnus-dup-unsuppress-article article)
8936          (gnus-request-move-article
8937           article                       ; Article to move
8938           gnus-newsgroup-name           ; From newsgroup
8939           (nth 1 (gnus-find-method-for-group
8940                   gnus-newsgroup-name)) ; Server
8941           (list 'gnus-request-accept-article
8942                 to-newsgroup (list 'quote select-method)
8943                 (not articles) t)       ; Accept form
8944           (not articles)))              ; Only save nov last time
8945         ;; Copy the article.
8946         ((eq action 'copy)
8947          (save-excursion
8948            (set-buffer copy-buf)
8949            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8950              (gnus-request-accept-article
8951               to-newsgroup select-method (not articles) t))))
8952         ;; Crosspost the article.
8953         ((eq action 'crosspost)
8954          (let ((xref (message-tokenize-header
8955                       (mail-header-xref (gnus-summary-article-header article))
8956                       " ")))
8957            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8958                                   ":" (number-to-string article)))
8959            (unless xref
8960              (setq xref (list (system-name))))
8961            (setq new-xref
8962                  (concat
8963                   (mapconcat 'identity
8964                              (delete "Xref:" (delete new-xref xref))
8965                              " ")
8966                   " " new-xref))
8967            (save-excursion
8968              (set-buffer copy-buf)
8969              ;; First put the article in the destination group.
8970              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8971              (when (consp (setq art-group
8972                                 (gnus-request-accept-article
8973                                  to-newsgroup select-method (not articles))))
8974                (setq new-xref (concat new-xref " " (car art-group)
8975                                       ":"
8976                                       (number-to-string (cdr art-group))))
8977                ;; Now we have the new Xrefs header, so we insert
8978                ;; it and replace the new article.
8979                (nnheader-replace-header "Xref" new-xref)
8980                (gnus-request-replace-article
8981                 (cdr art-group) to-newsgroup (current-buffer))
8982                art-group))))))
8983       (cond
8984        ((not art-group)
8985         (gnus-message 1 "Couldn't %s article %s: %s"
8986                       (cadr (assq action names)) article
8987                       (nnheader-get-report (car to-method))))
8988        ((eq art-group 'junk)
8989         (when (eq action 'move)
8990           (gnus-summary-mark-article article gnus-canceled-mark)
8991           (gnus-message 4 "Deleted article %s" article)
8992           ;; run the delete hook
8993           (run-hook-with-args 'gnus-summary-article-delete-hook
8994                               action
8995                               (gnus-data-header
8996                                (assoc article (gnus-data-list nil)))
8997                               gnus-newsgroup-name nil
8998                               select-method)))
8999        (t
9000         (let* ((pto-group (gnus-group-prefixed-name
9001                            (car art-group) to-method))
9002                (entry
9003                 (gnus-gethash pto-group gnus-newsrc-hashtb))
9004                (info (nth 2 entry))
9005                (to-group (gnus-info-group info))
9006                to-marks)
9007           ;; Update the group that has been moved to.
9008           (when (and info
9009                      (memq action '(move copy)))
9010             (unless (member to-group to-groups)
9011               (push to-group to-groups))
9012
9013             (unless (memq article gnus-newsgroup-unreads)
9014               (push 'read to-marks)
9015               (gnus-info-set-read
9016                info (gnus-add-to-range (gnus-info-read info)
9017                                        (list (cdr art-group)))))
9018
9019             ;; See whether the article is to be put in the cache.
9020             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9021                              default-marks
9022                            no-expire-marks))
9023                   (to-article (cdr art-group)))
9024
9025               ;; Enter the article into the cache in the new group,
9026               ;; if that is required.
9027               (when gnus-use-cache
9028                 (gnus-cache-possibly-enter-article
9029                  to-group to-article
9030                  (let ((header (copy-sequence
9031                                 (gnus-summary-article-header article))))
9032                    (mail-header-set-number header to-article)
9033                    header)
9034                  (memq article gnus-newsgroup-marked)
9035                  (memq article gnus-newsgroup-dormant)
9036                  (memq article gnus-newsgroup-unreads)))
9037
9038               (when gnus-preserve-marks
9039                 ;; Copy any marks over to the new group.
9040                 (when (and (equal to-group gnus-newsgroup-name)
9041                            (not (memq article gnus-newsgroup-unreads)))
9042                   ;; Mark this article as read in this group.
9043                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9044                   (setcdr (gnus-active to-group) to-article)
9045                   (setcdr gnus-newsgroup-active to-article))
9046
9047                 (while marks
9048                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9049                     (when (memq article (symbol-value
9050                                          (intern (format "gnus-newsgroup-%s"
9051                                                          (caar marks)))))
9052                       (push (cdar marks) to-marks)
9053                       ;; If the other group is the same as this group,
9054                       ;; then we have to add the mark to the list.
9055                       (when (equal to-group gnus-newsgroup-name)
9056                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9057                              (cons to-article
9058                                    (symbol-value
9059                                     (intern (format "gnus-newsgroup-%s"
9060                                                     (caar marks)))))))
9061                       ;; Copy the marks to other group.
9062                       (gnus-add-marked-articles
9063                        to-group (cdar marks) (list to-article) info)))
9064                   (setq marks (cdr marks)))
9065
9066                 (gnus-request-set-mark
9067                  to-group (list (list (list to-article) 'add to-marks))))
9068
9069               (gnus-dribble-enter
9070                (concat "(gnus-group-set-info '"
9071                        (gnus-prin1-to-string (gnus-get-info to-group))
9072                        ")"))))
9073
9074           ;; Update the Xref header in this article to point to
9075           ;; the new crossposted article we have just created.
9076           (when (eq action 'crosspost)
9077             (save-excursion
9078               (set-buffer copy-buf)
9079               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9080               (nnheader-replace-header "Xref" new-xref)
9081               (gnus-request-replace-article
9082                article gnus-newsgroup-name (current-buffer))))
9083
9084           ;; run the move/copy/crosspost/respool hook
9085           (run-hook-with-args 'gnus-summary-article-move-hook 
9086                               action
9087                               (gnus-data-header 
9088                                (assoc article (gnus-data-list nil)))
9089                               gnus-newsgroup-name
9090                               to-newsgroup
9091                               select-method))
9092
9093         ;;;!!!Why is this necessary?
9094         (set-buffer gnus-summary-buffer)
9095         
9096         (gnus-summary-goto-subject article)
9097         (when (eq action 'move)
9098           (gnus-summary-mark-article article gnus-canceled-mark))))
9099       (gnus-summary-remove-process-mark article))
9100     ;; Re-activate all groups that have been moved to.
9101     (save-excursion
9102       (set-buffer gnus-group-buffer)
9103       (let ((gnus-group-marked to-groups))
9104         (gnus-group-get-new-news-this-group nil t)))
9105
9106     (gnus-kill-buffer copy-buf)
9107     (gnus-summary-position-point)
9108     (gnus-set-mode-line 'summary)))
9109
9110 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9111   "Move the current article to a different newsgroup.
9112 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9113 When called interactively, if TO-NEWSGROUP is nil, use the value of
9114 the variable `gnus-move-split-methods' for finding a default target
9115 newsgroup.
9116 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9117 re-spool using this method."
9118   (interactive "P")
9119   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9120
9121 (defun gnus-summary-crosspost-article (&optional n)
9122   "Crosspost the current article to some other group."
9123   (interactive "P")
9124   (gnus-summary-move-article n nil nil 'crosspost))
9125
9126 (defcustom gnus-summary-respool-default-method nil
9127   "Default method type for respooling an article.
9128 If nil, use to the current newsgroup method."
9129   :type 'symbol
9130   :group 'gnus-summary-mail)
9131
9132 (defcustom gnus-summary-display-while-building nil
9133   "If not-nil, show and update the summary buffer as it's being built.
9134 If the value is t, update the buffer after every line is inserted.  If
9135 the value is an integer (N), update the display every N lines."
9136   :group 'gnus-thread
9137   :type '(choice (const :tag "off" nil)
9138                  number
9139                  (const :tag "frequently" t)))
9140
9141 (defun gnus-summary-respool-article (&optional n method)
9142   "Respool the current article.
9143 The article will be squeezed through the mail spooling process again,
9144 which means that it will be put in some mail newsgroup or other
9145 depending on `nnmail-split-methods'.
9146 If N is a positive number, respool the N next articles.
9147 If N is a negative number, respool the N previous articles.
9148 If N is nil and any articles have been marked with the process mark,
9149 respool those articles instead.
9150
9151 Respooling can be done both from mail groups and \"real\" newsgroups.
9152 In the former case, the articles in question will be moved from the
9153 current group into whatever groups they are destined to.  In the
9154 latter case, they will be copied into the relevant groups."
9155   (interactive
9156    (list current-prefix-arg
9157          (let* ((methods (gnus-methods-using 'respool))
9158                 (methname
9159                  (symbol-name (or gnus-summary-respool-default-method
9160                                   (car (gnus-find-method-for-group
9161                                         gnus-newsgroup-name)))))
9162                 (method
9163                  (gnus-completing-read-with-default
9164                   methname "What backend do you want to use when respooling?"
9165                   methods nil t nil 'gnus-mail-method-history))
9166                 ms)
9167            (cond
9168             ((zerop (length (setq ms (gnus-servers-using-backend
9169                                       (intern method)))))
9170              (list (intern method) ""))
9171             ((= 1 (length ms))
9172              (car ms))
9173             (t
9174              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9175                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9176                            ms-alist))))))))
9177   (unless method
9178     (error "No method given for respooling"))
9179   (if (assoc (symbol-name
9180               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9181              (gnus-methods-using 'respool))
9182       (gnus-summary-move-article n nil method)
9183     (gnus-summary-copy-article n nil method)))
9184
9185 (defun gnus-summary-import-article (file &optional edit)
9186   "Import an arbitrary file into a mail newsgroup."
9187   (interactive "fImport file: \nP")
9188   (let ((group gnus-newsgroup-name)
9189         (now (current-time))
9190         atts lines group-art)
9191     (unless (gnus-check-backend-function 'request-accept-article group)
9192       (error "%s does not support article importing" group))
9193     (or (file-readable-p file)
9194         (not (file-regular-p file))
9195         (error "Can't read %s" file))
9196     (save-excursion
9197       (set-buffer (gnus-get-buffer-create " *import file*"))
9198       (erase-buffer)
9199       (nnheader-insert-file-contents file)
9200       (goto-char (point-min))
9201       (if (nnheader-article-p)
9202           (save-restriction
9203             (goto-char (point-min))
9204             (search-forward "\n\n" nil t)
9205             (narrow-to-region (point-min) (1- (point)))
9206             (goto-char (point-min))
9207             (unless (re-search-forward "^date:" nil t)
9208               (goto-char (point-max))
9209               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9210         ;; This doesn't look like an article, so we fudge some headers.
9211         (setq atts (file-attributes file)
9212               lines (count-lines (point-min) (point-max)))
9213         (insert "From: " (read-string "From: ") "\n"
9214                 "Subject: " (read-string "Subject: ") "\n"
9215                 "Date: " (message-make-date (nth 5 atts)) "\n"
9216                 "Message-ID: " (message-make-message-id) "\n"
9217                 "Lines: " (int-to-string lines) "\n"
9218                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9219       (setq group-art (gnus-request-accept-article group nil t))
9220       (kill-buffer (current-buffer)))
9221     (setq gnus-newsgroup-active (gnus-activate-group group))
9222     (forward-line 1)
9223     (gnus-summary-goto-article (cdr group-art) nil t)
9224     (when edit
9225       (gnus-summary-edit-article))))
9226
9227 (defun gnus-summary-create-article ()
9228   "Create an article in a mail newsgroup."
9229   (interactive)
9230   (let ((group gnus-newsgroup-name)
9231         (now (current-time))
9232         group-art)
9233     (unless (gnus-check-backend-function 'request-accept-article group)
9234       (error "%s does not support article importing" group))
9235     (save-excursion
9236       (set-buffer (gnus-get-buffer-create " *import file*"))
9237       (erase-buffer)
9238       (goto-char (point-min))
9239       ;; This doesn't look like an article, so we fudge some headers.
9240       (insert "From: " (read-string "From: ") "\n"
9241               "Subject: " (read-string "Subject: ") "\n"
9242               "Date: " (message-make-date now) "\n"
9243               "Message-ID: " (message-make-message-id) "\n")
9244       (setq group-art (gnus-request-accept-article group nil t))
9245       (kill-buffer (current-buffer)))
9246     (setq gnus-newsgroup-active (gnus-activate-group group))
9247     (forward-line 1)
9248     (gnus-summary-goto-article (cdr group-art) nil t)
9249     (gnus-summary-edit-article)))
9250
9251 (defun gnus-summary-article-posted-p ()
9252   "Say whether the current (mail) article is available from news as well.
9253 This will be the case if the article has both been mailed and posted."
9254   (interactive)
9255   (let ((id (mail-header-references (gnus-summary-article-header)))
9256         (gnus-override-method (car (gnus-refer-article-methods))))
9257     (if (gnus-request-head id "")
9258         (gnus-message 2 "The current message was found on %s"
9259                       gnus-override-method)
9260       (gnus-message 2 "The current message couldn't be found on %s"
9261                     gnus-override-method)
9262       nil)))
9263
9264 (defun gnus-summary-expire-articles (&optional now)
9265   "Expire all articles that are marked as expirable in the current group."
9266   (interactive)
9267   (when (and (not gnus-group-is-exiting-without-update-p)
9268              (gnus-check-backend-function
9269               'request-expire-articles gnus-newsgroup-name))
9270     ;; This backend supports expiry.
9271     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9272            (expirable (if total
9273                           (progn
9274                             ;; We need to update the info for
9275                             ;; this group for `gnus-list-of-read-articles'
9276                             ;; to give us the right answer.
9277                             (gnus-run-hooks 'gnus-exit-group-hook)
9278                             (gnus-summary-update-info)
9279                             (gnus-list-of-read-articles gnus-newsgroup-name))
9280                         (setq gnus-newsgroup-expirable
9281                               (sort gnus-newsgroup-expirable '<))))
9282            (expiry-wait (if now 'immediate
9283                           (gnus-group-find-parameter
9284                            gnus-newsgroup-name 'expiry-wait)))
9285            (nnmail-expiry-target
9286             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9287                 nnmail-expiry-target))
9288            es)
9289       (when expirable
9290         ;; There are expirable articles in this group, so we run them
9291         ;; through the expiry process.
9292         (gnus-message 6 "Expiring articles...")
9293         (unless (gnus-check-group gnus-newsgroup-name)
9294           (error "Can't open server for %s" gnus-newsgroup-name))
9295         ;; The list of articles that weren't expired is returned.
9296         (save-excursion
9297           (if expiry-wait
9298               (let ((nnmail-expiry-wait-function nil)
9299                     (nnmail-expiry-wait expiry-wait))
9300                 (setq es (gnus-request-expire-articles
9301                           expirable gnus-newsgroup-name)))
9302             (setq es (gnus-request-expire-articles
9303                       expirable gnus-newsgroup-name)))
9304           (unless total
9305             (setq gnus-newsgroup-expirable es))
9306           ;; We go through the old list of expirable, and mark all
9307           ;; really expired articles as nonexistent.
9308           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9309             (let ((gnus-use-cache nil))
9310               (dolist (article expirable)
9311                 (when (and (not (memq article es))
9312                            (gnus-data-find article))
9313                   (gnus-summary-mark-article article gnus-canceled-mark)
9314                   (run-hook-with-args 'gnus-summary-article-expire-hook
9315                                       'delete
9316                                       (gnus-data-header
9317                                        (assoc article (gnus-data-list nil)))
9318                                       gnus-newsgroup-name
9319                                       nil
9320                                       nil))))))
9321         (gnus-message 6 "Expiring articles...done")))))
9322
9323 (defun gnus-summary-expire-articles-now ()
9324   "Expunge all expirable articles in the current group.
9325 This means that *all* articles that are marked as expirable will be
9326 deleted forever, right now."
9327   (interactive)
9328   (or gnus-expert-user
9329       (gnus-yes-or-no-p
9330        "Are you really, really, really sure you want to delete all these messages? ")
9331       (error "Phew!"))
9332   (gnus-summary-expire-articles t))
9333
9334 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9335 (defun gnus-summary-delete-article (&optional n)
9336   "Delete the N next (mail) articles.
9337 This command actually deletes articles.  This is not a marking
9338 command.  The article will disappear forever from your life, never to
9339 return.
9340
9341 If N is negative, delete backwards.
9342 If N is nil and articles have been marked with the process mark,
9343 delete these instead.
9344
9345 If `gnus-novice-user' is non-nil you will be asked for
9346 confirmation before the articles are deleted."
9347   (interactive "P")
9348   (unless (gnus-check-backend-function 'request-expire-articles
9349                                        gnus-newsgroup-name)
9350     (error "The current newsgroup does not support article deletion"))
9351   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9352     (error "Couldn't open server"))
9353   ;; Compute the list of articles to delete.
9354   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9355         (nnmail-expiry-target 'delete)
9356         not-deleted)
9357     (if (and gnus-novice-user
9358              (not (gnus-yes-or-no-p
9359                    (format "Do you really want to delete %s forever? "
9360                            (if (> (length articles) 1)
9361                                (format "these %s articles" (length articles))
9362                              "this article")))))
9363         ()
9364       ;; Delete the articles.
9365       (setq not-deleted (gnus-request-expire-articles
9366                          articles gnus-newsgroup-name 'force))
9367       (while articles
9368         (gnus-summary-remove-process-mark (car articles))
9369         ;; The backend might not have been able to delete the article
9370         ;; after all.
9371         (unless (memq (car articles) not-deleted)
9372           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9373         (let* ((article (car articles))
9374                (id (mail-header-id (gnus-data-header 
9375                                     (assoc article (gnus-data-list nil))))))
9376           (run-hook-with-args 'gnus-summary-article-delete-hook
9377                               'delete id gnus-newsgroup-name nil
9378                               nil))
9379         (setq articles (cdr articles)))
9380       (when not-deleted
9381         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9382     (gnus-summary-position-point)
9383     (gnus-set-mode-line 'summary)
9384     not-deleted))
9385
9386 (defun gnus-summary-edit-article (&optional force)
9387   "Edit the current article.
9388 This will have permanent effect only in mail groups.
9389 If FORCE is non-nil, allow editing of articles even in read-only
9390 groups."
9391   (interactive "P")
9392   (save-excursion
9393     (set-buffer gnus-summary-buffer)
9394     (let ((mail-parse-charset gnus-newsgroup-charset)
9395           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9396       (gnus-set-global-variables)
9397       (when (and (not force)
9398                  (gnus-group-read-only-p))
9399         (error "The current newsgroup does not support article editing"))
9400       (gnus-summary-show-article t)
9401       (gnus-article-edit-article
9402        'ignore
9403        `(lambda (no-highlight)
9404           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9405                 (message-options message-options)
9406                 (message-options-set-recipient)
9407                 (mail-parse-ignored-charsets
9408                  ',gnus-newsgroup-ignored-charsets))
9409             (gnus-summary-edit-article-done
9410              ,(or (mail-header-references gnus-current-headers) "")
9411              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9412
9413 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9414
9415 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9416                                                  no-highlight)
9417   "Make edits to the current article permanent."
9418   (interactive)
9419   (save-excursion
9420     ;; The buffer restriction contains the entire article if it exists.
9421     (when (article-goto-body)
9422       (let ((lines (count-lines (point) (point-max)))
9423             (length (- (point-max) (point)))
9424             (case-fold-search t)
9425             (body (copy-marker (point))))
9426         (goto-char (point-min))
9427         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9428           (delete-region (match-beginning 1) (match-end 1))
9429           (insert (number-to-string length)))
9430         (goto-char (point-min))
9431         (when (re-search-forward
9432                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9433           (delete-region (match-beginning 1) (match-end 1))
9434           (insert (number-to-string length)))
9435         (goto-char (point-min))
9436         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9437           (delete-region (match-beginning 1) (match-end 1))
9438           (insert (number-to-string lines))))))
9439   ;; Replace the article.
9440   (let ((buf (current-buffer)))
9441     (with-temp-buffer
9442       (insert-buffer-substring buf)
9443
9444       (if (and (not read-only)
9445                (not (gnus-request-replace-article
9446                      (cdr gnus-article-current) (car gnus-article-current)
9447                      (current-buffer) t)))
9448           (error "Couldn't replace article")
9449         ;; Update the summary buffer.
9450         (if (and references
9451                  (equal (message-tokenize-header references " ")
9452                         (message-tokenize-header
9453                          (or (message-fetch-field "references") "") " ")))
9454             ;; We only have to update this line.
9455             (save-excursion
9456               (save-restriction
9457                 (message-narrow-to-head)
9458                 (let ((head (buffer-string))
9459                       header)
9460                   (with-temp-buffer
9461                     (insert (format "211 %d Article retrieved.\n"
9462                                     (cdr gnus-article-current)))
9463                     (insert head)
9464                     (insert ".\n")
9465                     (let ((nntp-server-buffer (current-buffer)))
9466                       (setq header (car (gnus-get-newsgroup-headers
9467                                          nil t))))
9468                     (save-excursion
9469                       (set-buffer gnus-summary-buffer)
9470                       (gnus-data-set-header
9471                        (gnus-data-find (cdr gnus-article-current))
9472                        header)
9473                       (gnus-summary-update-article-line
9474                        (cdr gnus-article-current) header)
9475                       (if (gnus-summary-goto-subject
9476                            (cdr gnus-article-current) nil t)
9477                           (gnus-summary-update-secondary-mark
9478                            (cdr gnus-article-current))))))))
9479           ;; Update threads.
9480           (set-buffer (or buffer gnus-summary-buffer))
9481           (gnus-summary-update-article (cdr gnus-article-current))
9482           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9483               (gnus-summary-update-secondary-mark
9484                (cdr gnus-article-current))))
9485         ;; Prettify the article buffer again.
9486         (unless no-highlight
9487           (save-excursion
9488             (set-buffer gnus-article-buffer)
9489             ;;;!!! Fix this -- article should be rehighlighted.
9490             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9491             (set-buffer gnus-original-article-buffer)
9492             (gnus-request-article
9493              (cdr gnus-article-current)
9494              (car gnus-article-current) (current-buffer))))
9495         ;; Prettify the summary buffer line.
9496         (when (gnus-visual-p 'summary-highlight 'highlight)
9497           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9498
9499 (defun gnus-summary-edit-wash (key)
9500   "Perform editing command KEY in the article buffer."
9501   (interactive
9502    (list
9503     (progn
9504       (message "%s" (concat (this-command-keys) "- "))
9505       (read-char))))
9506   (message "")
9507   (gnus-summary-edit-article)
9508   (execute-kbd-macro (concat (this-command-keys) key))
9509   (gnus-article-edit-done))
9510
9511 ;;; Respooling
9512
9513 (defun gnus-summary-respool-query (&optional silent trace)
9514   "Query where the respool algorithm would put this article."
9515   (interactive)
9516   (let (gnus-mark-article-hook)
9517     (gnus-summary-select-article)
9518     (save-excursion
9519       (set-buffer gnus-original-article-buffer)
9520       (save-restriction
9521         (message-narrow-to-head)
9522         (let ((groups (nnmail-article-group 'identity trace)))
9523           (unless silent
9524             (if groups
9525                 (message "This message would go to %s"
9526                          (mapconcat 'car groups ", "))
9527               (message "This message would go to no groups"))
9528             groups))))))
9529
9530 (defun gnus-summary-respool-trace ()
9531   "Trace where the respool algorithm would put this article.
9532 Display a buffer showing all fancy splitting patterns which matched."
9533   (interactive)
9534   (gnus-summary-respool-query nil t))
9535
9536 ;; Summary marking commands.
9537
9538 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9539   "Mark articles which has the same subject as read, and then select the next.
9540 If UNMARK is positive, remove any kind of mark.
9541 If UNMARK is negative, tick articles."
9542   (interactive "P")
9543   (when unmark
9544     (setq unmark (prefix-numeric-value unmark)))
9545   (let ((count
9546          (gnus-summary-mark-same-subject
9547           (gnus-summary-article-subject) unmark)))
9548     ;; Select next unread article.  If auto-select-same mode, should
9549     ;; select the first unread article.
9550     (gnus-summary-next-article t (and gnus-auto-select-same
9551                                       (gnus-summary-article-subject)))
9552     (gnus-message 7 "%d article%s marked as %s"
9553                   count (if (= count 1) " is" "s are")
9554                   (if unmark "unread" "read"))))
9555
9556 (defun gnus-summary-kill-same-subject (&optional unmark)
9557   "Mark articles which has the same subject as read.
9558 If UNMARK is positive, remove any kind of mark.
9559 If UNMARK is negative, tick articles."
9560   (interactive "P")
9561   (when unmark
9562     (setq unmark (prefix-numeric-value unmark)))
9563   (let ((count
9564          (gnus-summary-mark-same-subject
9565           (gnus-summary-article-subject) unmark)))
9566     ;; If marked as read, go to next unread subject.
9567     (when (null unmark)
9568       ;; Go to next unread subject.
9569       (gnus-summary-next-subject 1 t))
9570     (gnus-message 7 "%d articles are marked as %s"
9571                   count (if unmark "unread" "read"))))
9572
9573 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9574   "Mark articles with same SUBJECT as read, and return marked number.
9575 If optional argument UNMARK is positive, remove any kinds of marks.
9576 If optional argument UNMARK is negative, mark articles as unread instead."
9577   (let ((count 1))
9578     (save-excursion
9579       (cond
9580        ((null unmark)                   ; Mark as read.
9581         (while (and
9582                 (progn
9583                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9584                   (gnus-summary-show-thread) t)
9585                 (gnus-summary-find-subject subject))
9586           (setq count (1+ count))))
9587        ((> unmark 0)                    ; Tick.
9588         (while (and
9589                 (progn
9590                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9591                   (gnus-summary-show-thread) t)
9592                 (gnus-summary-find-subject subject))
9593           (setq count (1+ count))))
9594        (t                               ; Mark as unread.
9595         (while (and
9596                 (progn
9597                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9598                   (gnus-summary-show-thread) t)
9599                 (gnus-summary-find-subject subject))
9600           (setq count (1+ count)))))
9601       (gnus-set-mode-line 'summary)
9602       ;; Return the number of marked articles.
9603       count)))
9604
9605 (defun gnus-summary-mark-as-processable (n &optional unmark)
9606   "Set the process mark on the next N articles.
9607 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9608 the process mark instead.  The difference between N and the actual
9609 number of articles marked is returned."
9610   (interactive "P")
9611   (if (and (null n) (gnus-region-active-p))
9612       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9613     (setq n (prefix-numeric-value n))
9614     (let ((backward (< n 0))
9615           (n (abs n)))
9616       (while (and
9617               (> n 0)
9618               (if unmark
9619                   (gnus-summary-remove-process-mark
9620                    (gnus-summary-article-number))
9621                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9622               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9623         (setq n (1- n)))
9624       (when (/= 0 n)
9625         (gnus-message 7 "No more articles"))
9626       (gnus-summary-recenter)
9627       (gnus-summary-position-point)
9628       n)))
9629
9630 (defun gnus-summary-unmark-as-processable (n)
9631   "Remove the process mark from the next N articles.
9632 If N is negative, unmark backward instead.  The difference between N and
9633 the actual number of articles unmarked is returned."
9634   (interactive "P")
9635   (gnus-summary-mark-as-processable n t))
9636
9637 (defun gnus-summary-unmark-all-processable ()
9638   "Remove the process mark from all articles."
9639   (interactive)
9640   (save-excursion
9641     (while gnus-newsgroup-processable
9642       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9643   (gnus-summary-position-point))
9644
9645 (defun gnus-summary-add-mark (article type)
9646   "Mark ARTICLE with a mark of TYPE."
9647   (let ((vtype (car (assq type gnus-article-mark-lists)))
9648         var)
9649     (if (not vtype)
9650         (error "No such mark type: %s" type)
9651       (setq var (intern (format "gnus-newsgroup-%s" type)))
9652       (set var (cons article (symbol-value var)))
9653       (if (memq type '(processable cached replied forwarded recent saved))
9654           (gnus-summary-update-secondary-mark article)
9655         ;;; !!! This is bogus.  We should find out what primary
9656         ;;; !!! mark we want to set.
9657         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9658
9659 (defun gnus-summary-mark-as-expirable (n)
9660   "Mark N articles forward as expirable.
9661 If N is negative, mark backward instead.  The difference between N and
9662 the actual number of articles marked is returned."
9663   (interactive "p")
9664   (gnus-summary-mark-forward n gnus-expirable-mark))
9665
9666 (defun gnus-summary-mark-as-spam (n)
9667   "Mark N articles forward as spam.
9668 If N is negative, mark backward instead.  The difference between N and
9669 the actual number of articles marked is returned."
9670   (interactive "p")
9671   (gnus-summary-mark-forward n gnus-spam-mark))
9672
9673 (defun gnus-summary-mark-article-as-replied (article)
9674   "Mark ARTICLE as replied to and update the summary line.
9675 ARTICLE can also be a list of articles."
9676   (interactive (list (gnus-summary-article-number)))
9677   (let ((articles (if (listp article) article (list article))))
9678     (dolist (article articles)
9679       (unless (numberp article)
9680         (error "%s is not a number" article))
9681       (push article gnus-newsgroup-replied)
9682       (let ((buffer-read-only nil))
9683         (when (gnus-summary-goto-subject article nil t)
9684           (gnus-summary-update-secondary-mark article))))))
9685
9686 (defun gnus-summary-mark-article-as-forwarded (article)
9687   "Mark ARTICLE as forwarded and update the summary line.
9688 ARTICLE can also be a list of articles."
9689   (let ((articles (if (listp article) article (list article))))
9690     (dolist (article articles)
9691       (push article gnus-newsgroup-forwarded)
9692       (let ((buffer-read-only nil))
9693         (when (gnus-summary-goto-subject article nil t)
9694           (gnus-summary-update-secondary-mark article))))))
9695
9696 (defun gnus-summary-set-bookmark (article)
9697   "Set a bookmark in current article."
9698   (interactive (list (gnus-summary-article-number)))
9699   (when (or (not (get-buffer gnus-article-buffer))
9700             (not gnus-current-article)
9701             (not gnus-article-current)
9702             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9703     (error "No current article selected"))
9704   ;; Remove old bookmark, if one exists.
9705   (gnus-pull article gnus-newsgroup-bookmarks)
9706   ;; Set the new bookmark, which is on the form
9707   ;; (article-number . line-number-in-body).
9708   (push
9709    (cons article
9710          (save-excursion
9711            (set-buffer gnus-article-buffer)
9712            (count-lines
9713             (min (point)
9714                  (save-excursion
9715                    (article-goto-body)
9716                    (point)))
9717             (point))))
9718    gnus-newsgroup-bookmarks)
9719   (gnus-message 6 "A bookmark has been added to the current article."))
9720
9721 (defun gnus-summary-remove-bookmark (article)
9722   "Remove the bookmark from the current article."
9723   (interactive (list (gnus-summary-article-number)))
9724   ;; Remove old bookmark, if one exists.
9725   (if (not (assq article gnus-newsgroup-bookmarks))
9726       (gnus-message 6 "No bookmark in current article.")
9727     (gnus-pull article gnus-newsgroup-bookmarks)
9728     (gnus-message 6 "Removed bookmark.")))
9729
9730 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9731 (defun gnus-summary-mark-as-dormant (n)
9732   "Mark N articles forward as dormant.
9733 If N is negative, mark backward instead.  The difference between N and
9734 the actual number of articles marked is returned."
9735   (interactive "p")
9736   (gnus-summary-mark-forward n gnus-dormant-mark))
9737
9738 (defun gnus-summary-set-process-mark (article)
9739   "Set the process mark on ARTICLE and update the summary line."
9740   (setq gnus-newsgroup-processable
9741         (cons article
9742               (delq article gnus-newsgroup-processable)))
9743   (when (gnus-summary-goto-subject article)
9744     (gnus-summary-show-thread)
9745     (gnus-summary-goto-subject article)
9746     (gnus-summary-update-secondary-mark article)))
9747
9748 (defun gnus-summary-remove-process-mark (article)
9749   "Remove the process mark from ARTICLE and update the summary line."
9750   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9751   (when (gnus-summary-goto-subject article)
9752     (gnus-summary-show-thread)
9753     (gnus-summary-goto-subject article)
9754     (gnus-summary-update-secondary-mark article)))
9755
9756 (defun gnus-summary-set-saved-mark (article)
9757   "Set the process mark on ARTICLE and update the summary line."
9758   (push article gnus-newsgroup-saved)
9759   (when (gnus-summary-goto-subject article)
9760     (gnus-summary-update-secondary-mark article)))
9761
9762 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9763   "Mark N articles as read forwards.
9764 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9765 The difference between N and the actual number of articles marked is
9766 returned.
9767 If NO-EXPIRE, auto-expiry will be inhibited."
9768   (interactive "p")
9769   (gnus-summary-show-thread)
9770   (let ((backward (< n 0))
9771         (gnus-summary-goto-unread
9772          (and gnus-summary-goto-unread
9773               (not (eq gnus-summary-goto-unread 'never))
9774               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9775                                     gnus-ticked-mark gnus-dormant-mark)))))
9776         (n (abs n))
9777         (mark (or mark gnus-del-mark)))
9778     (while (and (> n 0)
9779                 (gnus-summary-mark-article nil mark no-expire)
9780                 (zerop (gnus-summary-next-subject
9781                         (if backward -1 1)
9782                         (and gnus-summary-goto-unread
9783                              (not (eq gnus-summary-goto-unread 'never)))
9784                         t)))
9785       (setq n (1- n)))
9786     (when (/= 0 n)
9787       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9788     (gnus-summary-recenter)
9789     (gnus-summary-position-point)
9790     (gnus-set-mode-line 'summary)
9791     n))
9792
9793 (defun gnus-summary-mark-article-as-read (mark)
9794   "Mark the current article quickly as read with MARK."
9795   (let ((article (gnus-summary-article-number)))
9796     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9797     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9798     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9799     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9800     (push (cons article mark) gnus-newsgroup-reads)
9801     ;; Possibly remove from cache, if that is used.
9802     (when gnus-use-cache
9803       (gnus-cache-enter-remove-article article))
9804     ;; Allow the backend to change the mark.
9805     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9806     ;; Check for auto-expiry.
9807     (when (and gnus-newsgroup-auto-expire
9808                (memq mark gnus-auto-expirable-marks))
9809       (setq mark gnus-expirable-mark)
9810       ;; Let the backend know about the mark change.
9811       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9812       (push article gnus-newsgroup-expirable))
9813     ;; Set the mark in the buffer.
9814     (gnus-summary-update-mark mark 'unread)
9815     t))
9816
9817 (defun gnus-summary-mark-article-as-unread (mark)
9818   "Mark the current article quickly as unread with MARK."
9819   (let* ((article (gnus-summary-article-number))
9820          (old-mark (gnus-summary-article-mark article)))
9821     ;; Allow the backend to change the mark.
9822     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9823     (if (eq mark old-mark)
9824         t
9825       (if (<= article 0)
9826           (progn
9827             (gnus-error 1 "Can't mark negative article numbers")
9828             nil)
9829         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9830         (setq gnus-newsgroup-spam-marked
9831               (delq article gnus-newsgroup-spam-marked))
9832         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9833         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9834         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9835         (cond ((= mark gnus-ticked-mark)
9836                (setq gnus-newsgroup-marked
9837                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9838                                               article)))
9839               ((= mark gnus-spam-mark)
9840                (setq gnus-newsgroup-spam-marked
9841                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9842                                               article)))
9843               ((= mark gnus-dormant-mark)
9844                (setq gnus-newsgroup-dormant
9845                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9846                                               article)))
9847               (t
9848                (setq gnus-newsgroup-unreads
9849                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9850                                               article))))
9851         (gnus-pull article gnus-newsgroup-reads)
9852
9853         ;; See whether the article is to be put in the cache.
9854         (and gnus-use-cache
9855              (vectorp (gnus-summary-article-header article))
9856              (save-excursion
9857                (gnus-cache-possibly-enter-article
9858                 gnus-newsgroup-name article
9859                 (gnus-summary-article-header article)
9860                 (= mark gnus-ticked-mark)
9861                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9862
9863         ;; Fix the mark.
9864         (gnus-summary-update-mark mark 'unread)
9865         t))))
9866
9867 (defun gnus-summary-mark-article (&optional article mark no-expire)
9868   "Mark ARTICLE with MARK.  MARK can be any character.
9869 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9870 `??' (dormant) and `?E' (expirable).
9871 If MARK is nil, then the default character `?r' is used.
9872 If ARTICLE is nil, then the article on the current line will be
9873 marked.
9874 If NO-EXPIRE, auto-expiry will be inhibited."
9875   ;; The mark might be a string.
9876   (when (stringp mark)
9877     (setq mark (aref mark 0)))
9878   ;; If no mark is given, then we check auto-expiring.
9879   (when (null mark)
9880     (setq mark gnus-del-mark))
9881   (when (and (not no-expire)
9882              gnus-newsgroup-auto-expire
9883              (memq mark gnus-auto-expirable-marks))
9884     (setq mark gnus-expirable-mark))
9885   (let ((article (or article (gnus-summary-article-number)))
9886         (old-mark (gnus-summary-article-mark article)))
9887     ;; Allow the backend to change the mark.
9888     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9889     (if (eq mark old-mark)
9890         t
9891       (unless article
9892         (error "No article on current line"))
9893       (if (not (if (or (= mark gnus-unread-mark)
9894                        (= mark gnus-ticked-mark)
9895                        (= mark gnus-spam-mark)
9896                        (= mark gnus-dormant-mark))
9897                    (gnus-mark-article-as-unread article mark)
9898                  (gnus-mark-article-as-read article mark)))
9899           t
9900         ;; See whether the article is to be put in the cache.
9901         (and gnus-use-cache
9902              (not (= mark gnus-canceled-mark))
9903              (vectorp (gnus-summary-article-header article))
9904              (save-excursion
9905                (gnus-cache-possibly-enter-article
9906                 gnus-newsgroup-name article
9907                 (gnus-summary-article-header article)
9908                 (= mark gnus-ticked-mark)
9909                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9910
9911         (when (gnus-summary-goto-subject article nil t)
9912           (let ((buffer-read-only nil))
9913             (gnus-summary-show-thread)
9914             ;; Fix the mark.
9915             (gnus-summary-update-mark mark 'unread)
9916             t))))))
9917
9918 (defun gnus-summary-update-secondary-mark (article)
9919   "Update the secondary (read, process, cache) mark."
9920   (gnus-summary-update-mark
9921    (cond ((memq article gnus-newsgroup-processable)
9922           gnus-process-mark)
9923          ((memq article gnus-newsgroup-cached)
9924           gnus-cached-mark)
9925          ((memq article gnus-newsgroup-replied)
9926           gnus-replied-mark)
9927          ((memq article gnus-newsgroup-forwarded)
9928           gnus-forwarded-mark)
9929          ((memq article gnus-newsgroup-saved)
9930           gnus-saved-mark)
9931          ((memq article gnus-newsgroup-recent)
9932           gnus-recent-mark)
9933          ((memq article gnus-newsgroup-unseen)
9934           gnus-unseen-mark)
9935          (t gnus-no-mark))
9936    'replied)
9937   (when (gnus-visual-p 'summary-highlight 'highlight)
9938     (gnus-run-hooks 'gnus-summary-update-hook))
9939   t)
9940
9941 (defun gnus-summary-update-download-mark (article)
9942   "Update the download mark."
9943   (gnus-summary-update-mark
9944    (cond ((memq article gnus-newsgroup-undownloaded) 
9945           gnus-undownloaded-mark)
9946          (gnus-newsgroup-agentized
9947           gnus-downloaded-mark)
9948          (t
9949           gnus-no-mark))
9950    'download)
9951   (gnus-summary-update-line t)
9952   t)
9953
9954 (defun gnus-summary-update-mark (mark type)
9955   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9956         (buffer-read-only nil))
9957     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9958     (when forward
9959       (when (looking-at "\r")
9960         (incf forward))
9961       (when (<= (+ forward (point)) (point-max))
9962         ;; Go to the right position on the line.
9963         (goto-char (+ forward (point)))
9964         ;; Replace the old mark with the new mark.
9965         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9966         ;; Optionally update the marks by some user rule.
9967         (when (eq type 'unread)
9968           (gnus-data-set-mark
9969            (gnus-data-find (gnus-summary-article-number)) mark)
9970           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9971
9972 (defun gnus-mark-article-as-read (article &optional mark)
9973   "Enter ARTICLE in the pertinent lists and remove it from others."
9974   ;; Make the article expirable.
9975   (let ((mark (or mark gnus-del-mark)))
9976     (setq gnus-newsgroup-expirable
9977           (if (= mark gnus-expirable-mark)
9978               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9979             (delq article gnus-newsgroup-expirable)))
9980     ;; Remove from unread and marked lists.
9981     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9982     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9983     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9984     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9985     (push (cons article mark) gnus-newsgroup-reads)
9986     ;; Possibly remove from cache, if that is used.
9987     (when gnus-use-cache
9988       (gnus-cache-enter-remove-article article))
9989     t))
9990
9991 (defun gnus-mark-article-as-unread (article &optional mark)
9992   "Enter ARTICLE in the pertinent lists and remove it from others."
9993   (let ((mark (or mark gnus-ticked-mark)))
9994     (if (<= article 0)
9995         (progn
9996           (gnus-error 1 "Can't mark negative article numbers")
9997           nil)
9998       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9999             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10000             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10001             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10002             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10003
10004       ;; Unsuppress duplicates?
10005       (when gnus-suppress-duplicates
10006         (gnus-dup-unsuppress-article article))
10007
10008       (cond ((= mark gnus-ticked-mark)
10009              (setq gnus-newsgroup-marked
10010                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10011             ((= mark gnus-spam-mark)
10012              (setq gnus-newsgroup-spam-marked
10013                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10014                                             article)))
10015             ((= mark gnus-dormant-mark)
10016              (setq gnus-newsgroup-dormant
10017                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10018             (t
10019              (setq gnus-newsgroup-unreads
10020                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10021       (gnus-pull article gnus-newsgroup-reads)
10022       t)))
10023
10024 (defalias 'gnus-summary-mark-as-unread-forward
10025   'gnus-summary-tick-article-forward)
10026 (make-obsolete 'gnus-summary-mark-as-unread-forward
10027                'gnus-summary-tick-article-forward)
10028 (defun gnus-summary-tick-article-forward (n)
10029   "Tick N articles forwards.
10030 If N is negative, tick backwards instead.
10031 The difference between N and the number of articles ticked is returned."
10032   (interactive "p")
10033   (gnus-summary-mark-forward n gnus-ticked-mark))
10034
10035 (defalias 'gnus-summary-mark-as-unread-backward
10036   'gnus-summary-tick-article-backward)
10037 (make-obsolete 'gnus-summary-mark-as-unread-backward
10038                'gnus-summary-tick-article-backward)
10039 (defun gnus-summary-tick-article-backward (n)
10040   "Tick N articles backwards.
10041 The difference between N and the number of articles ticked is returned."
10042   (interactive "p")
10043   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10044
10045 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10046 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10047 (defun gnus-summary-tick-article (&optional article clear-mark)
10048   "Mark current article as unread.
10049 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10050 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10051   (interactive)
10052   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10053                                        gnus-ticked-mark)))
10054
10055 (defun gnus-summary-mark-as-read-forward (n)
10056   "Mark N articles as read forwards.
10057 If N is negative, mark backwards instead.
10058 The difference between N and the actual number of articles marked is
10059 returned."
10060   (interactive "p")
10061   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10062
10063 (defun gnus-summary-mark-as-read-backward (n)
10064   "Mark the N articles as read backwards.
10065 The difference between N and the actual number of articles marked is
10066 returned."
10067   (interactive "p")
10068   (gnus-summary-mark-forward
10069    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10070
10071 (defun gnus-summary-mark-as-read (&optional article mark)
10072   "Mark current article as read.
10073 ARTICLE specifies the article to be marked as read.
10074 MARK specifies a string to be inserted at the beginning of the line."
10075   (gnus-summary-mark-article article mark))
10076
10077 (defun gnus-summary-clear-mark-forward (n)
10078   "Clear marks from N articles forward.
10079 If N is negative, clear backward instead.
10080 The difference between N and the number of marks cleared is returned."
10081   (interactive "p")
10082   (gnus-summary-mark-forward n gnus-unread-mark))
10083
10084 (defun gnus-summary-clear-mark-backward (n)
10085   "Clear marks from N articles backward.
10086 The difference between N and the number of marks cleared is returned."
10087   (interactive "p")
10088   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10089
10090 (defun gnus-summary-mark-unread-as-read ()
10091   "Intended to be used by `gnus-summary-mark-article-hook'."
10092   (when (memq gnus-current-article gnus-newsgroup-unreads)
10093     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10094
10095 (defun gnus-summary-mark-read-and-unread-as-read ()
10096   "Intended to be used by `gnus-summary-mark-article-hook'."
10097   (let ((mark (gnus-summary-article-mark)))
10098     (when (or (gnus-unread-mark-p mark)
10099               (gnus-read-mark-p mark))
10100       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
10101
10102 (defun gnus-summary-mark-unread-as-ticked ()
10103   "Intended to be used by `gnus-summary-mark-article-hook'."
10104   (when (memq gnus-current-article gnus-newsgroup-unreads)
10105     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10106
10107 (defun gnus-summary-mark-region-as-read (point mark all)
10108   "Mark all unread articles between point and mark as read.
10109 If given a prefix, mark all articles between point and mark as read,
10110 even ticked and dormant ones."
10111   (interactive "r\nP")
10112   (save-excursion
10113     (let (article)
10114       (goto-char point)
10115       (beginning-of-line)
10116       (while (and
10117               (< (point) mark)
10118               (progn
10119                 (when (or all
10120                           (memq (setq article (gnus-summary-article-number))
10121                                 gnus-newsgroup-unreads))
10122                   (gnus-summary-mark-article article gnus-del-mark))
10123                 t)
10124               (gnus-summary-find-next))))))
10125
10126 (defun gnus-summary-mark-below (score mark)
10127   "Mark articles with score less than SCORE with MARK."
10128   (interactive "P\ncMark: ")
10129   (setq score (if score
10130                   (prefix-numeric-value score)
10131                 (or gnus-summary-default-score 0)))
10132   (save-excursion
10133     (set-buffer gnus-summary-buffer)
10134     (goto-char (point-min))
10135     (while
10136         (progn
10137           (and (< (gnus-summary-article-score) score)
10138                (gnus-summary-mark-article nil mark))
10139           (gnus-summary-find-next)))))
10140
10141 (defun gnus-summary-kill-below (&optional score)
10142   "Mark articles with score below SCORE as read."
10143   (interactive "P")
10144   (gnus-summary-mark-below score gnus-killed-mark))
10145
10146 (defun gnus-summary-clear-above (&optional score)
10147   "Clear all marks from articles with score above SCORE."
10148   (interactive "P")
10149   (gnus-summary-mark-above score gnus-unread-mark))
10150
10151 (defun gnus-summary-tick-above (&optional score)
10152   "Tick all articles with score above SCORE."
10153   (interactive "P")
10154   (gnus-summary-mark-above score gnus-ticked-mark))
10155
10156 (defun gnus-summary-mark-above (score mark)
10157   "Mark articles with score over SCORE with MARK."
10158   (interactive "P\ncMark: ")
10159   (setq score (if score
10160                   (prefix-numeric-value score)
10161                 (or gnus-summary-default-score 0)))
10162   (save-excursion
10163     (set-buffer gnus-summary-buffer)
10164     (goto-char (point-min))
10165     (while (and (progn
10166                   (when (> (gnus-summary-article-score) score)
10167                     (gnus-summary-mark-article nil mark))
10168                   t)
10169                 (gnus-summary-find-next)))))
10170
10171 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10172 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10173 (defun gnus-summary-limit-include-expunged (&optional no-error)
10174   "Display all the hidden articles that were expunged for low scores."
10175   (interactive)
10176   (let ((buffer-read-only nil))
10177     (let ((scored gnus-newsgroup-scored)
10178           headers h)
10179       (while scored
10180         (unless (gnus-summary-article-header (caar scored))
10181           (and (setq h (gnus-number-to-header (caar scored)))
10182                (< (cdar scored) gnus-summary-expunge-below)
10183                (push h headers)))
10184         (setq scored (cdr scored)))
10185       (if (not headers)
10186           (when (not no-error)
10187             (error "No expunged articles hidden"))
10188         (goto-char (point-min))
10189         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10190         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10191         (mapcar (lambda (x) (push (mail-header-number x)
10192                                   gnus-newsgroup-limit))
10193                 headers)
10194         (gnus-summary-prepare-unthreaded (nreverse headers))
10195         (goto-char (point-min))
10196         (gnus-summary-position-point)
10197         t))))
10198
10199 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10200   "Mark all unread articles in this newsgroup as read.
10201 If prefix argument ALL is non-nil, ticked and dormant articles will
10202 also be marked as read.
10203 If QUIETLY is non-nil, no questions will be asked.
10204 If TO-HERE is non-nil, it should be a point in the buffer.  All
10205 articles before (after, if REVERSE is set) this point will be marked as read.
10206 Note that this function will only catch up the unread article
10207 in the current summary buffer limitation.
10208 The number of articles marked as read is returned."
10209   (interactive "P")
10210   (prog1
10211       (save-excursion
10212         (when (or quietly
10213                   (not gnus-interactive-catchup) ;Without confirmation?
10214                   gnus-expert-user
10215                   (gnus-y-or-n-p
10216                    (if all
10217                        "Mark absolutely all articles as read? "
10218                      "Mark all unread articles as read? ")))
10219           (if (and not-mark
10220                    (not gnus-newsgroup-adaptive)
10221                    (not gnus-newsgroup-auto-expire)
10222                    (not gnus-suppress-duplicates)
10223                    (or (not gnus-use-cache)
10224                        (eq gnus-use-cache 'passive)))
10225               (progn
10226                 (when all
10227                   (setq gnus-newsgroup-marked nil
10228                         gnus-newsgroup-spam-marked nil
10229                         gnus-newsgroup-dormant nil))
10230                 (setq gnus-newsgroup-unreads
10231                       (gnus-sorted-nunion
10232                        (gnus-intersection gnus-newsgroup-unreads
10233                                           gnus-newsgroup-downloadable)
10234                        gnus-newsgroup-unfetched)))
10235             ;; We actually mark all articles as canceled, which we
10236             ;; have to do when using auto-expiry or adaptive scoring.
10237             (gnus-summary-show-all-threads)
10238             (if (and to-here reverse)
10239                 (progn
10240                   (goto-char to-here)
10241                   (while (and
10242                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10243                           (gnus-summary-find-next (not all)))))
10244               (when (gnus-summary-first-subject (not all))
10245                 (while (and
10246                         (if to-here (< (point) to-here) t)
10247                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10248                         (gnus-summary-find-next (not all))))))
10249             (gnus-set-mode-line 'summary))
10250           t))
10251     (gnus-summary-position-point)))
10252
10253 (defun gnus-summary-catchup-to-here (&optional all)
10254   "Mark all unticked articles before the current one as read.
10255 If ALL is non-nil, also mark ticked and dormant articles as read."
10256   (interactive "P")
10257   (save-excursion
10258     (gnus-save-hidden-threads
10259       (let ((beg (point)))
10260         ;; We check that there are unread articles.
10261         (when (or all (gnus-summary-find-prev))
10262           (gnus-summary-catchup all t beg)))))
10263   (gnus-summary-position-point))
10264
10265 (defun gnus-summary-catchup-from-here (&optional all)
10266   "Mark all unticked articles after (and including) the current one as read.
10267 If ALL is non-nil, also mark ticked and dormant articles as read."
10268   (interactive "P")
10269   (save-excursion
10270     (gnus-save-hidden-threads
10271       (let ((beg (point)))
10272         ;; We check that there are unread articles.
10273         (when (or all (gnus-summary-find-next))
10274           (gnus-summary-catchup all t beg nil t)))))
10275   (gnus-summary-position-point))
10276
10277 (defun gnus-summary-catchup-all (&optional quietly)
10278   "Mark all articles in this newsgroup as read.
10279 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10280 instead, which marks only unread articles as read."
10281   (interactive "P")
10282   (gnus-summary-catchup t quietly))
10283
10284 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10285   "Mark all unread articles in this group as read, then exit.
10286 If prefix argument ALL is non-nil, all articles are marked as read.
10287 If QUIETLY is non-nil, no questions will be asked."
10288   (interactive "P")
10289   (when (gnus-summary-catchup all quietly nil 'fast)
10290     ;; Select next newsgroup or exit.
10291     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10292              (eq gnus-auto-select-next 'quietly))
10293         (gnus-summary-next-group nil)
10294       (gnus-summary-exit))))
10295
10296 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10297   "Mark all articles in this newsgroup as read, and then exit.
10298 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10299 instead, which marks only unread articles as read."
10300   (interactive "P")
10301   (gnus-summary-catchup-and-exit t quietly))
10302
10303 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10304   "Mark all articles in this group as read and select the next group.
10305 If given a prefix, mark all articles, unread as well as ticked, as
10306 read."
10307   (interactive "P")
10308   (save-excursion
10309     (gnus-summary-catchup all))
10310   (gnus-summary-next-group))
10311
10312 ;;;
10313 ;;; with article
10314 ;;;
10315
10316 (defmacro gnus-with-article (article &rest forms)
10317   "Select ARTICLE and perform FORMS in the original article buffer.
10318 Then replace the article with the result."
10319   `(progn
10320      ;; We don't want the article to be marked as read.
10321      (let (gnus-mark-article-hook)
10322        (gnus-summary-select-article t t nil ,article))
10323      (set-buffer gnus-original-article-buffer)
10324      ,@forms
10325      (if (not (gnus-check-backend-function
10326                'request-replace-article (car gnus-article-current)))
10327          (gnus-message 5 "Read-only group; not replacing")
10328        (unless (gnus-request-replace-article
10329                 ,article (car gnus-article-current)
10330                 (current-buffer) t)
10331          (error "Couldn't replace article")))
10332      ;; The cache and backlog have to be flushed somewhat.
10333      (when gnus-keep-backlog
10334        (gnus-backlog-remove-article
10335         (car gnus-article-current) (cdr gnus-article-current)))
10336      (when gnus-use-cache
10337        (gnus-cache-update-article
10338         (car gnus-article-current) (cdr gnus-article-current)))))
10339
10340 (put 'gnus-with-article 'lisp-indent-function 1)
10341 (put 'gnus-with-article 'edebug-form-spec '(form body))
10342
10343 ;; Thread-based commands.
10344
10345 (defun gnus-summary-articles-in-thread (&optional article)
10346   "Return a list of all articles in the current thread.
10347 If ARTICLE is non-nil, return all articles in the thread that starts
10348 with that article."
10349   (let* ((article (or article (gnus-summary-article-number)))
10350          (data (gnus-data-find-list article))
10351          (top-level (gnus-data-level (car data)))
10352          (top-subject
10353           (cond ((null gnus-thread-operation-ignore-subject)
10354                  (gnus-simplify-subject-re
10355                   (mail-header-subject (gnus-data-header (car data)))))
10356                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10357                  (gnus-simplify-subject-fuzzy
10358                   (mail-header-subject (gnus-data-header (car data)))))
10359                 (t nil)))
10360          (end-point (save-excursion
10361                       (if (gnus-summary-go-to-next-thread)
10362                           (point) (point-max))))
10363          articles)
10364     (while (and data
10365                 (< (gnus-data-pos (car data)) end-point))
10366       (when (or (not top-subject)
10367                 (string= top-subject
10368                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10369                              (gnus-simplify-subject-fuzzy
10370                               (mail-header-subject
10371                                (gnus-data-header (car data))))
10372                            (gnus-simplify-subject-re
10373                             (mail-header-subject
10374                              (gnus-data-header (car data)))))))
10375         (push (gnus-data-number (car data)) articles))
10376       (unless (and (setq data (cdr data))
10377                    (> (gnus-data-level (car data)) top-level))
10378         (setq data nil)))
10379     ;; Return the list of articles.
10380     (nreverse articles)))
10381
10382 (defun gnus-summary-rethread-current ()
10383   "Rethread the thread the current article is part of."
10384   (interactive)
10385   (let* ((gnus-show-threads t)
10386          (article (gnus-summary-article-number))
10387          (id (mail-header-id (gnus-summary-article-header)))
10388          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10389     (unless id
10390       (error "No article on the current line"))
10391     (gnus-rebuild-thread id)
10392     (gnus-summary-goto-subject article)))
10393
10394 (defun gnus-summary-reparent-thread ()
10395   "Make the current article child of the marked (or previous) article.
10396
10397 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10398 is non-nil or the Subject: of both articles are the same."
10399   (interactive)
10400   (unless (not (gnus-group-read-only-p))
10401     (error "The current newsgroup does not support article editing"))
10402   (unless (<= (length gnus-newsgroup-processable) 1)
10403     (error "No more than one article may be marked"))
10404   (save-window-excursion
10405     (let ((gnus-article-buffer " *reparent*")
10406           (current-article (gnus-summary-article-number))
10407           ;; First grab the marked article, otherwise one line up.
10408           (parent-article (if (not (null gnus-newsgroup-processable))
10409                               (car gnus-newsgroup-processable)
10410                             (save-excursion
10411                               (if (eq (forward-line -1) 0)
10412                                   (gnus-summary-article-number)
10413                                 (error "Beginning of summary buffer"))))))
10414       (unless (not (eq current-article parent-article))
10415         (error "An article may not be self-referential"))
10416       (let ((message-id (mail-header-id
10417                          (gnus-summary-article-header parent-article))))
10418         (unless (and message-id (not (equal message-id "")))
10419           (error "No message-id in desired parent"))
10420         (gnus-with-article current-article
10421           (save-restriction
10422             (goto-char (point-min))
10423             (message-narrow-to-head)
10424             (if (re-search-forward "^References: " nil t)
10425                 (progn
10426                   (re-search-forward "^[^ \t]" nil t)
10427                   (forward-line -1)
10428                   (end-of-line)
10429                   (insert " " message-id))
10430               (insert "References: " message-id "\n"))))
10431         (set-buffer gnus-summary-buffer)
10432         (gnus-summary-unmark-all-processable)
10433         (gnus-summary-update-article current-article)
10434         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10435             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10436         (gnus-summary-rethread-current)
10437         (gnus-message 3 "Article %d is now the child of article %d"
10438                       current-article parent-article)))))
10439
10440 (defun gnus-summary-toggle-threads (&optional arg)
10441   "Toggle showing conversation threads.
10442 If ARG is positive number, turn showing conversation threads on."
10443   (interactive "P")
10444   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10445     (setq gnus-show-threads
10446           (if (null arg) (not gnus-show-threads)
10447             (> (prefix-numeric-value arg) 0)))
10448     (gnus-summary-prepare)
10449     (gnus-summary-goto-subject current)
10450     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10451     (gnus-summary-position-point)))
10452
10453 (defun gnus-summary-show-all-threads ()
10454   "Show all threads."
10455   (interactive)
10456   (save-excursion
10457     (let ((buffer-read-only nil))
10458       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10459   (gnus-summary-position-point))
10460
10461 (defun gnus-summary-show-thread ()
10462   "Show thread subtrees.
10463 Returns nil if no thread was there to be shown."
10464   (interactive)
10465   (let ((buffer-read-only nil)
10466         (orig (point))
10467         (end (gnus-point-at-eol))
10468         ;; Leave point at bol
10469         (beg (progn (beginning-of-line) (point))))
10470     (prog1
10471         ;; Any hidden lines here?
10472         (search-forward "\r" end t)
10473       (subst-char-in-region beg end ?\^M ?\n t)
10474       (goto-char orig)
10475       (gnus-summary-position-point))))
10476
10477 (defun gnus-summary-maybe-hide-threads ()
10478   "If requested, hide the threads that should be hidden."
10479   (when (and gnus-show-threads
10480              gnus-thread-hide-subtree)
10481     (gnus-summary-hide-all-threads
10482      (if (or (consp gnus-thread-hide-subtree)
10483              (functionp gnus-thread-hide-subtree))
10484          (gnus-make-predicate gnus-thread-hide-subtree)
10485        nil))))
10486
10487 ;;; Hiding predicates.
10488
10489 (defun gnus-article-unread-p (header)
10490   (memq (mail-header-number header) gnus-newsgroup-unreads))
10491
10492 (defun gnus-article-unseen-p (header)
10493   (memq (mail-header-number header) gnus-newsgroup-unseen))
10494
10495 (defun gnus-map-articles (predicate articles)
10496   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10497   (apply 'gnus-or (mapcar predicate
10498                           (mapcar 'gnus-summary-article-header articles))))
10499
10500 (defun gnus-summary-hide-all-threads (&optional predicate)
10501   "Hide all thread subtrees.
10502 If PREDICATE is supplied, threads that satisfy this predicate
10503 will not be hidden."
10504   (interactive)
10505   (save-excursion
10506     (goto-char (point-min))
10507     (let ((end nil))
10508       (while (not end)
10509         (when (or (not predicate)
10510                   (gnus-map-articles
10511                    predicate (gnus-summary-article-children)))
10512             (gnus-summary-hide-thread))
10513         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10514   (gnus-summary-position-point))
10515
10516 (defun gnus-summary-hide-thread ()
10517   "Hide thread subtrees.
10518 If PREDICATE is supplied, threads that satisfy this predicate
10519 will not be hidden.
10520 Returns nil if no threads were there to be hidden."
10521   (interactive)
10522   (let ((buffer-read-only nil)
10523         (start (point))
10524         (article (gnus-summary-article-number)))
10525     (goto-char start)
10526     ;; Go forward until either the buffer ends or the subthread
10527     ;; ends.
10528     (when (and (not (eobp))
10529                (or (zerop (gnus-summary-next-thread 1 t))
10530                    (goto-char (point-max))))
10531       (prog1
10532           (if (and (> (point) start)
10533                    (search-backward "\n" start t))
10534               (progn
10535                 (subst-char-in-region start (point) ?\n ?\^M)
10536                 (gnus-summary-goto-subject article))
10537             (goto-char start)
10538             nil)))))
10539
10540 (defun gnus-summary-go-to-next-thread (&optional previous)
10541   "Go to the same level (or less) next thread.
10542 If PREVIOUS is non-nil, go to previous thread instead.
10543 Return the article number moved to, or nil if moving was impossible."
10544   (let ((level (gnus-summary-thread-level))
10545         (way (if previous -1 1))
10546         (beg (point)))
10547     (forward-line way)
10548     (while (and (not (eobp))
10549                 (< level (gnus-summary-thread-level)))
10550       (forward-line way))
10551     (if (eobp)
10552         (progn
10553           (goto-char beg)
10554           nil)
10555       (setq beg (point))
10556       (prog1
10557           (gnus-summary-article-number)
10558         (goto-char beg)))))
10559
10560 (defun gnus-summary-next-thread (n &optional silent)
10561   "Go to the same level next N'th thread.
10562 If N is negative, search backward instead.
10563 Returns the difference between N and the number of skips actually
10564 done.
10565
10566 If SILENT, don't output messages."
10567   (interactive "p")
10568   (let ((backward (< n 0))
10569         (n (abs n)))
10570     (while (and (> n 0)
10571                 (gnus-summary-go-to-next-thread backward))
10572       (decf n))
10573     (unless silent
10574       (gnus-summary-position-point))
10575     (when (and (not silent) (/= 0 n))
10576       (gnus-message 7 "No more threads"))
10577     n))
10578
10579 (defun gnus-summary-prev-thread (n)
10580   "Go to the same level previous N'th thread.
10581 Returns the difference between N and the number of skips actually
10582 done."
10583   (interactive "p")
10584   (gnus-summary-next-thread (- n)))
10585
10586 (defun gnus-summary-go-down-thread ()
10587   "Go down one level in the current thread."
10588   (let ((children (gnus-summary-article-children)))
10589     (when children
10590       (gnus-summary-goto-subject (car children)))))
10591
10592 (defun gnus-summary-go-up-thread ()
10593   "Go up one level in the current thread."
10594   (let ((parent (gnus-summary-article-parent)))
10595     (when parent
10596       (gnus-summary-goto-subject parent))))
10597
10598 (defun gnus-summary-down-thread (n)
10599   "Go down thread N steps.
10600 If N is negative, go up instead.
10601 Returns the difference between N and how many steps down that were
10602 taken."
10603   (interactive "p")
10604   (let ((up (< n 0))
10605         (n (abs n)))
10606     (while (and (> n 0)
10607                 (if up (gnus-summary-go-up-thread)
10608                   (gnus-summary-go-down-thread)))
10609       (setq n (1- n)))
10610     (gnus-summary-position-point)
10611     (when (/= 0 n)
10612       (gnus-message 7 "Can't go further"))
10613     n))
10614
10615 (defun gnus-summary-up-thread (n)
10616   "Go up thread N steps.
10617 If N is negative, go down instead.
10618 Returns the difference between N and how many steps down that were
10619 taken."
10620   (interactive "p")
10621   (gnus-summary-down-thread (- n)))
10622
10623 (defun gnus-summary-top-thread ()
10624   "Go to the top of the thread."
10625   (interactive)
10626   (while (gnus-summary-go-up-thread))
10627   (gnus-summary-article-number))
10628
10629 (defun gnus-summary-kill-thread (&optional unmark)
10630   "Mark articles under current thread as read.
10631 If the prefix argument is positive, remove any kinds of marks.
10632 If the prefix argument is negative, tick articles instead."
10633   (interactive "P")
10634   (when unmark
10635     (setq unmark (prefix-numeric-value unmark)))
10636   (let ((articles (gnus-summary-articles-in-thread)))
10637     (save-excursion
10638       ;; Expand the thread.
10639       (gnus-summary-show-thread)
10640       ;; Mark all the articles.
10641       (while articles
10642         (gnus-summary-goto-subject (car articles))
10643         (cond ((null unmark)
10644                (gnus-summary-mark-article-as-read gnus-killed-mark))
10645               ((> unmark 0)
10646                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10647               (t
10648                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10649         (setq articles (cdr articles))))
10650     ;; Hide killed subtrees.
10651     (and (null unmark)
10652          gnus-thread-hide-killed
10653          (gnus-summary-hide-thread))
10654     ;; If marked as read, go to next unread subject.
10655     (when (null unmark)
10656       ;; Go to next unread subject.
10657       (gnus-summary-next-subject 1 t)))
10658   (gnus-set-mode-line 'summary))
10659
10660 ;; Summary sorting commands
10661
10662 (defun gnus-summary-sort-by-number (&optional reverse)
10663   "Sort the summary buffer by article number.
10664 Argument REVERSE means reverse order."
10665   (interactive "P")
10666   (gnus-summary-sort 'number reverse))
10667
10668 (defun gnus-summary-sort-by-random (&optional reverse)
10669   "Randomize the order in the summary buffer.
10670 Argument REVERSE means to randomize in reverse order."
10671   (interactive "P")
10672   (gnus-summary-sort 'random reverse))
10673
10674 (defun gnus-summary-sort-by-author (&optional reverse)
10675   "Sort the summary buffer by author name alphabetically.
10676 If `case-fold-search' is non-nil, case of letters is ignored.
10677 Argument REVERSE means reverse order."
10678   (interactive "P")
10679   (gnus-summary-sort 'author reverse))
10680
10681 (defun gnus-summary-sort-by-subject (&optional reverse)
10682   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10683 If `case-fold-search' is non-nil, case of letters is ignored.
10684 Argument REVERSE means reverse order."
10685   (interactive "P")
10686   (gnus-summary-sort 'subject reverse))
10687
10688 (defun gnus-summary-sort-by-date (&optional reverse)
10689   "Sort the summary buffer by date.
10690 Argument REVERSE means reverse order."
10691   (interactive "P")
10692   (gnus-summary-sort 'date reverse))
10693
10694 (defun gnus-summary-sort-by-score (&optional reverse)
10695   "Sort the summary buffer by score.
10696 Argument REVERSE means reverse order."
10697   (interactive "P")
10698   (gnus-summary-sort 'score reverse))
10699
10700 (defun gnus-summary-sort-by-lines (&optional reverse)
10701   "Sort the summary buffer by the number of lines.
10702 Argument REVERSE means reverse order."
10703   (interactive "P")
10704   (gnus-summary-sort 'lines reverse))
10705
10706 (defun gnus-summary-sort-by-chars (&optional reverse)
10707   "Sort the summary buffer by article length.
10708 Argument REVERSE means reverse order."
10709   (interactive "P")
10710   (gnus-summary-sort 'chars reverse))
10711
10712 (defun gnus-summary-sort-by-original (&optional reverse)
10713   "Sort the summary buffer using the default sorting method.
10714 Argument REVERSE means reverse order."
10715   (interactive "P")
10716   (let* ((buffer-read-only)
10717          (gnus-summary-prepare-hook nil))
10718     ;; We do the sorting by regenerating the threads.
10719     (gnus-summary-prepare)
10720     ;; Hide subthreads if needed.
10721     (gnus-summary-maybe-hide-threads)))
10722
10723 (defun gnus-summary-sort (predicate reverse)
10724   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10725   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10726          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10727          (gnus-thread-sort-functions
10728           (if (not reverse)
10729               thread
10730             `(lambda (t1 t2)
10731                (,thread t2 t1))))
10732          (gnus-sort-gathered-threads-function
10733           gnus-thread-sort-functions)
10734          (gnus-article-sort-functions
10735           (if (not reverse)
10736               article
10737             `(lambda (t1 t2)
10738                (,article t2 t1))))
10739          (buffer-read-only)
10740          (gnus-summary-prepare-hook nil))
10741     ;; We do the sorting by regenerating the threads.
10742     (gnus-summary-prepare)
10743     ;; Hide subthreads if needed.
10744     (gnus-summary-maybe-hide-threads)))
10745
10746 ;; Summary saving commands.
10747
10748 (defun gnus-summary-save-article (&optional n not-saved)
10749   "Save the current article using the default saver function.
10750 If N is a positive number, save the N next articles.
10751 If N is a negative number, save the N previous articles.
10752 If N is nil and any articles have been marked with the process mark,
10753 save those articles instead.
10754 The variable `gnus-default-article-saver' specifies the saver function."
10755   (interactive "P")
10756   (let* ((articles (gnus-summary-work-articles n))
10757          (save-buffer (save-excursion
10758                         (nnheader-set-temp-buffer " *Gnus Save*")))
10759          (num (length articles))
10760          header file)
10761     (dolist (article articles)
10762       (setq header (gnus-summary-article-header article))
10763       (if (not (vectorp header))
10764           ;; This is a pseudo-article.
10765           (if (assq 'name header)
10766               (gnus-copy-file (cdr (assq 'name header)))
10767             (gnus-message 1 "Article %d is unsaveable" article))
10768         ;; This is a real article.
10769         (save-window-excursion
10770           (let ((gnus-display-mime-function nil)
10771                 (gnus-article-prepare-hook nil))
10772             (gnus-summary-select-article t nil nil article)))
10773         (save-excursion
10774           (set-buffer save-buffer)
10775           (erase-buffer)
10776           (insert-buffer-substring gnus-original-article-buffer))
10777         (setq file (gnus-article-save save-buffer file num))
10778         (gnus-summary-remove-process-mark article)
10779         (unless not-saved
10780           (gnus-summary-set-saved-mark article))))
10781     (gnus-kill-buffer save-buffer)
10782     (gnus-summary-position-point)
10783     (gnus-set-mode-line 'summary)
10784     n))
10785
10786 (defun gnus-summary-pipe-output (&optional arg headers)
10787   "Pipe the current article to a subprocess.
10788 If N is a positive number, pipe the N next articles.
10789 If N is a negative number, pipe the N previous articles.
10790 If N is nil and any articles have been marked with the process mark,
10791 pipe those articles instead.
10792 If HEADERS (the symbolic prefix), include the headers, too."
10793   (interactive (gnus-interactive "P\ny"))
10794   (require 'gnus-art)
10795   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10796         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10797     (gnus-summary-save-article arg t))
10798   (let ((buffer (get-buffer "*Shell Command Output*")))
10799     (when (and buffer
10800                (not (zerop (buffer-size buffer))))
10801       (gnus-configure-windows 'pipe))))
10802
10803 (defun gnus-summary-save-article-mail (&optional arg)
10804   "Append the current article to an mail file.
10805 If N is a positive number, save the N next articles.
10806 If N is a negative number, save the N previous articles.
10807 If N is nil and any articles have been marked with the process mark,
10808 save those articles instead."
10809   (interactive "P")
10810   (require 'gnus-art)
10811   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10812     (gnus-summary-save-article arg)))
10813
10814 (defun gnus-summary-save-article-rmail (&optional arg)
10815   "Append the current article to an rmail file.
10816 If N is a positive number, save the N next articles.
10817 If N is a negative number, save the N previous articles.
10818 If N is nil and any articles have been marked with the process mark,
10819 save those articles instead."
10820   (interactive "P")
10821   (require 'gnus-art)
10822   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10823     (gnus-summary-save-article arg)))
10824
10825 (defun gnus-summary-save-article-file (&optional arg)
10826   "Append the current article to a file.
10827 If N is a positive number, save the N next articles.
10828 If N is a negative number, save the N previous articles.
10829 If N is nil and any articles have been marked with the process mark,
10830 save those articles instead."
10831   (interactive "P")
10832   (require 'gnus-art)
10833   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10834     (gnus-summary-save-article arg)))
10835
10836 (defun gnus-summary-write-article-file (&optional arg)
10837   "Write the current article to a file, deleting the previous file.
10838 If N is a positive number, save the N next articles.
10839 If N is a negative number, save the N previous articles.
10840 If N is nil and any articles have been marked with the process mark,
10841 save those articles instead."
10842   (interactive "P")
10843   (require 'gnus-art)
10844   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10845     (gnus-summary-save-article arg)))
10846
10847 (defun gnus-summary-save-article-body-file (&optional arg)
10848   "Append the current article body to a file.
10849 If N is a positive number, save the N next articles.
10850 If N is a negative number, save the N previous articles.
10851 If N is nil and any articles have been marked with the process mark,
10852 save those articles instead."
10853   (interactive "P")
10854   (require 'gnus-art)
10855   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10856     (gnus-summary-save-article arg)))
10857
10858 (defun gnus-summary-muttprint (&optional arg)
10859   "Print the current article using Muttprint.
10860 If N is a positive number, save the N next articles.
10861 If N is a negative number, save the N previous articles.
10862 If N is nil and any articles have been marked with the process mark,
10863 save those articles instead."
10864   (interactive "P")
10865   (require 'gnus-art)
10866   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10867     (gnus-summary-save-article arg t)))
10868
10869 (defun gnus-summary-pipe-message (program)
10870   "Pipe the current article through PROGRAM."
10871   (interactive "sProgram: ")
10872   (gnus-summary-select-article)
10873   (let ((mail-header-separator ""))
10874     (gnus-eval-in-buffer-window gnus-article-buffer
10875       (save-restriction
10876         (widen)
10877         (let ((start (window-start))
10878               buffer-read-only)
10879           (message-pipe-buffer-body program)
10880           (set-window-start (get-buffer-window (current-buffer)) start))))))
10881
10882 (defun gnus-get-split-value (methods)
10883   "Return a value based on the split METHODS."
10884   (let (split-name method result match)
10885     (when methods
10886       (save-excursion
10887         (set-buffer gnus-original-article-buffer)
10888         (save-restriction
10889           (nnheader-narrow-to-headers)
10890           (while (and methods (not split-name))
10891             (goto-char (point-min))
10892             (setq method (pop methods))
10893             (setq match (car method))
10894             (when (cond
10895                    ((stringp match)
10896                     ;; Regular expression.
10897                     (ignore-errors
10898                       (re-search-forward match nil t)))
10899                    ((functionp match)
10900                     ;; Function.
10901                     (save-restriction
10902                       (widen)
10903                       (setq result (funcall match gnus-newsgroup-name))))
10904                    ((consp match)
10905                     ;; Form.
10906                     (save-restriction
10907                       (widen)
10908                       (setq result (eval match)))))
10909               (setq split-name (cdr method))
10910               (cond ((stringp result)
10911                      (push (expand-file-name
10912                             result gnus-article-save-directory)
10913                            split-name))
10914                     ((consp result)
10915                      (setq split-name (append result split-name)))))))))
10916     (nreverse split-name)))
10917
10918 (defun gnus-valid-move-group-p (group)
10919   (and (boundp group)
10920        (symbol-name group)
10921        (symbol-value group)
10922        (gnus-get-function (gnus-find-method-for-group
10923                            (symbol-name group)) 'request-accept-article t)))
10924
10925 (defun gnus-read-move-group-name (prompt default articles prefix)
10926   "Read a group name."
10927   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10928          (minibuffer-confirm-incomplete nil) ; XEmacs
10929          (prom
10930           (format "%s %s to:"
10931                   prompt
10932                   (if (> (length articles) 1)
10933                       (format "these %d articles" (length articles))
10934                     "this article")))
10935          (to-newsgroup
10936           (cond
10937            ((null split-name)
10938             (gnus-completing-read-with-default
10939              default prom
10940              gnus-active-hashtb
10941              'gnus-valid-move-group-p
10942              nil prefix
10943              'gnus-group-history))
10944            ((= 1 (length split-name))
10945             (gnus-completing-read-with-default
10946              (car split-name) prom
10947              gnus-active-hashtb
10948              'gnus-valid-move-group-p
10949              nil nil
10950              'gnus-group-history))
10951            (t
10952             (gnus-completing-read-with-default
10953              nil prom
10954              (mapcar (lambda (el) (list el))
10955                      (nreverse split-name))
10956              nil nil nil
10957              'gnus-group-history))))
10958          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10959     (when to-newsgroup
10960       (if (or (string= to-newsgroup "")
10961               (string= to-newsgroup prefix))
10962           (setq to-newsgroup default))
10963       (unless to-newsgroup
10964         (error "No group name entered"))
10965       (or (gnus-active to-newsgroup)
10966           (gnus-activate-group to-newsgroup nil nil to-method)
10967           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10968                                      to-newsgroup))
10969               (or (and (gnus-request-create-group to-newsgroup to-method)
10970                        (gnus-activate-group
10971                         to-newsgroup nil nil to-method)
10972                        (gnus-subscribe-group to-newsgroup))
10973                   (error "Couldn't create group %s" to-newsgroup)))
10974           (error "No such group: %s" to-newsgroup)))
10975     to-newsgroup))
10976
10977 (defun gnus-summary-save-parts (type dir n &optional reverse)
10978   "Save parts matching TYPE to DIR.
10979 If REVERSE, save parts that do not match TYPE."
10980   (interactive
10981    (list (read-string "Save parts of type: "
10982                       (or (car gnus-summary-save-parts-type-history)
10983                           gnus-summary-save-parts-default-mime)
10984                       'gnus-summary-save-parts-type-history)
10985          (setq gnus-summary-save-parts-last-directory
10986                (read-file-name "Save to directory: "
10987                                gnus-summary-save-parts-last-directory
10988                                nil t))
10989          current-prefix-arg))
10990   (gnus-summary-iterate n
10991     (let ((gnus-display-mime-function nil)
10992           (gnus-inhibit-treatment t))
10993       (gnus-summary-select-article))
10994     (save-excursion
10995       (set-buffer gnus-article-buffer)
10996       (let ((handles (or gnus-article-mime-handles
10997                          (mm-dissect-buffer nil gnus-article-loose-mime)
10998                          (and gnus-article-emulate-mime
10999                               (mm-uu-dissect)))))
11000         (when handles
11001           (gnus-summary-save-parts-1 type dir handles reverse)
11002           (unless gnus-article-mime-handles ;; Don't destroy this case.
11003             (mm-destroy-parts handles)))))))
11004
11005 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11006   (if (stringp (car handle))
11007       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11008               (cdr handle))
11009     (when (if reverse
11010               (not (string-match type (mm-handle-media-type handle)))
11011             (string-match type (mm-handle-media-type handle)))
11012       (let ((file (expand-file-name
11013                    (file-name-nondirectory
11014                     (or
11015                      (mail-content-type-get
11016                       (mm-handle-disposition handle) 'filename)
11017                      (concat gnus-newsgroup-name
11018                              "." (number-to-string
11019                                   (cdr gnus-article-current)))))
11020                    dir)))
11021         (unless (file-exists-p file)
11022           (mm-save-part-to-file handle file))))))
11023
11024 ;; Summary extract commands
11025
11026 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11027   (let ((buffer-read-only nil)
11028         (article (gnus-summary-article-number))
11029         after-article b e)
11030     (unless (gnus-summary-goto-subject article)
11031       (error "No such article: %d" article))
11032     (gnus-summary-position-point)
11033     ;; If all commands are to be bunched up on one line, we collect
11034     ;; them here.
11035     (unless gnus-view-pseudos-separately
11036       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11037             files action)
11038         (while ps
11039           (setq action (cdr (assq 'action (car ps))))
11040           (setq files (list (cdr (assq 'name (car ps)))))
11041           (while (and ps (cdr ps)
11042                       (string= (or action "1")
11043                                (or (cdr (assq 'action (cadr ps))) "2")))
11044             (push (cdr (assq 'name (cadr ps))) files)
11045             (setcdr ps (cddr ps)))
11046           (when files
11047             (when (not (string-match "%s" action))
11048               (push " " files))
11049             (push " " files)
11050             (when (assq 'execute (car ps))
11051               (setcdr (assq 'execute (car ps))
11052                       (funcall (if (string-match "%s" action)
11053                                    'format 'concat)
11054                                action
11055                                (mapconcat
11056                                 (lambda (f)
11057                                   (if (equal f " ")
11058                                       f
11059                                     (gnus-quote-arg-for-sh-or-csh f)))
11060                                 files " ")))))
11061           (setq ps (cdr ps)))))
11062     (if (and gnus-view-pseudos (not not-view))
11063         (while pslist
11064           (when (assq 'execute (car pslist))
11065             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11066                                   (eq gnus-view-pseudos 'not-confirm)))
11067           (setq pslist (cdr pslist)))
11068       (save-excursion
11069         (while pslist
11070           (setq after-article (or (cdr (assq 'article (car pslist)))
11071                                   (gnus-summary-article-number)))
11072           (gnus-summary-goto-subject after-article)
11073           (forward-line 1)
11074           (setq b (point))
11075           (insert "    " (file-name-nondirectory
11076                           (cdr (assq 'name (car pslist))))
11077                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11078           (setq e (point))
11079           (forward-line -1)             ; back to `b'
11080           (gnus-add-text-properties
11081            b (1- e) (list 'gnus-number gnus-reffed-article-number
11082                           gnus-mouse-face-prop gnus-mouse-face))
11083           (gnus-data-enter
11084            after-article gnus-reffed-article-number
11085            gnus-unread-mark b (car pslist) 0 (- e b))
11086           (setq gnus-newsgroup-unreads
11087                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11088                                          gnus-reffed-article-number))
11089           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11090           (setq pslist (cdr pslist)))))))
11091
11092 (defun gnus-pseudos< (p1 p2)
11093   (let ((c1 (cdr (assq 'action p1)))
11094         (c2 (cdr (assq 'action p2))))
11095     (and c1 c2 (string< c1 c2))))
11096
11097 (defun gnus-request-pseudo-article (props)
11098   (cond ((assq 'execute props)
11099          (gnus-execute-command (cdr (assq 'execute props)))))
11100   (let ((gnus-current-article (gnus-summary-article-number)))
11101     (gnus-run-hooks 'gnus-mark-article-hook)))
11102
11103 (defun gnus-execute-command (command &optional automatic)
11104   (save-excursion
11105     (gnus-article-setup-buffer)
11106     (set-buffer gnus-article-buffer)
11107     (setq buffer-read-only nil)
11108     (let ((command (if automatic command
11109                      (read-string "Command: " (cons command 0)))))
11110       (erase-buffer)
11111       (insert "$ " command "\n\n")
11112       (if gnus-view-pseudo-asynchronously
11113           (start-process "gnus-execute" (current-buffer) shell-file-name
11114                          shell-command-switch command)
11115         (call-process shell-file-name nil t nil
11116                       shell-command-switch command)))))
11117
11118 ;; Summary kill commands.
11119
11120 (defun gnus-summary-edit-global-kill (article)
11121   "Edit the \"global\" kill file."
11122   (interactive (list (gnus-summary-article-number)))
11123   (gnus-group-edit-global-kill article))
11124
11125 (defun gnus-summary-edit-local-kill ()
11126   "Edit a local kill file applied to the current newsgroup."
11127   (interactive)
11128   (setq gnus-current-headers (gnus-summary-article-header))
11129   (gnus-group-edit-local-kill
11130    (gnus-summary-article-number) gnus-newsgroup-name))
11131
11132 ;;; Header reading.
11133
11134 (defun gnus-read-header (id &optional header)
11135   "Read the headers of article ID and enter them into the Gnus system."
11136   (let ((group gnus-newsgroup-name)
11137         (gnus-override-method
11138          (or
11139           gnus-override-method
11140           (and (gnus-news-group-p gnus-newsgroup-name)
11141                (car (gnus-refer-article-methods)))))
11142         where)
11143     ;; First we check to see whether the header in question is already
11144     ;; fetched.
11145     (if (stringp id)
11146         ;; This is a Message-ID.
11147         (setq header (or header (gnus-id-to-header id)))
11148       ;; This is an article number.
11149       (setq header (or header (gnus-summary-article-header id))))
11150     (if (and header
11151              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11152         ;; We have found the header.
11153         header
11154       ;; If this is a sparse article, we have to nix out its
11155       ;; previous entry in the thread hashtb.
11156       (when (and header
11157                  (gnus-summary-article-sparse-p (mail-header-number header)))
11158         (let* ((parent (gnus-parent-id (mail-header-references header)))
11159                (thread (and parent (gnus-id-to-thread parent))))
11160           (when thread
11161             (delq (assq header thread) thread))))
11162       ;; We have to really fetch the header to this article.
11163       (save-excursion
11164         (set-buffer nntp-server-buffer)
11165         (when (setq where (gnus-request-head id group))
11166           (nnheader-fold-continuation-lines)
11167           (goto-char (point-max))
11168           (insert ".\n")
11169           (goto-char (point-min))
11170           (insert "211 ")
11171           (princ (cond
11172                   ((numberp id) id)
11173                   ((cdr where) (cdr where))
11174                   (header (mail-header-number header))
11175                   (t gnus-reffed-article-number))
11176                  (current-buffer))
11177           (insert " Article retrieved.\n"))
11178         (if (or (not where)
11179                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11180             ()                          ; Malformed head.
11181           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11182             (when (and (stringp id)
11183                        (not (string= (gnus-group-real-name group)
11184                                      (car where))))
11185               ;; If we fetched by Message-ID and the article came
11186               ;; from a different group, we fudge some bogus article
11187               ;; numbers for this article.
11188               (mail-header-set-number header gnus-reffed-article-number))
11189             (save-excursion
11190               (set-buffer gnus-summary-buffer)
11191               (decf gnus-reffed-article-number)
11192               (gnus-remove-header (mail-header-number header))
11193               (push header gnus-newsgroup-headers)
11194               (setq gnus-current-headers header)
11195               (push (mail-header-number header) gnus-newsgroup-limit)))
11196           header)))))
11197
11198 (defun gnus-remove-header (number)
11199   "Remove header NUMBER from `gnus-newsgroup-headers'."
11200   (if (and gnus-newsgroup-headers
11201            (= number (mail-header-number (car gnus-newsgroup-headers))))
11202       (pop gnus-newsgroup-headers)
11203     (let ((headers gnus-newsgroup-headers))
11204       (while (and (cdr headers)
11205                   (not (= number (mail-header-number (cadr headers)))))
11206         (pop headers))
11207       (when (cdr headers)
11208         (setcdr headers (cddr headers))))))
11209
11210 ;;;
11211 ;;; summary highlights
11212 ;;;
11213
11214 (defun gnus-highlight-selected-summary ()
11215   "Highlight selected article in summary buffer."
11216   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11217   (when gnus-summary-selected-face
11218     (save-excursion
11219       (let* ((beg (gnus-point-at-bol))
11220              (end (gnus-point-at-eol))
11221              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11222              (from (if (get-text-property beg gnus-mouse-face-prop)
11223                        beg
11224                      (or (next-single-property-change
11225                           beg gnus-mouse-face-prop nil end)
11226                          beg)))
11227              (to
11228               (if (= from end)
11229                   (- from 2)
11230                 (or (next-single-property-change
11231                      from gnus-mouse-face-prop nil end)
11232                     end))))
11233         ;; If no mouse-face prop on line we will have to = from = end,
11234         ;; so we highlight the entire line instead.
11235         (when (= (+ to 2) from)
11236           (setq from beg)
11237           (setq to end))
11238         (if gnus-newsgroup-selected-overlay
11239             ;; Move old overlay.
11240             (gnus-move-overlay
11241              gnus-newsgroup-selected-overlay from to (current-buffer))
11242           ;; Create new overlay.
11243           (gnus-overlay-put
11244            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11245            'face gnus-summary-selected-face))))))
11246
11247 (defvar gnus-summary-highlight-line-cached nil)
11248 (defvar gnus-summary-highlight-line-trigger nil)
11249
11250 (defun gnus-summary-highlight-line-0 ()
11251   (if (and (eq gnus-summary-highlight-line-trigger 
11252                gnus-summary-highlight)
11253            gnus-summary-highlight-line-cached)
11254       gnus-summary-highlight-line-cached
11255     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11256           gnus-summary-highlight-line-cached
11257           (let* ((cond (list 'cond))
11258                  (c cond)
11259                  (list gnus-summary-highlight))
11260             (while list
11261               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11262                               nil))
11263               (setq c (cdr c)
11264                     list (cdr list)))
11265             (gnus-byte-compile (list 'lambda nil cond))))))
11266
11267 (defun gnus-summary-highlight-line ()
11268   "Highlight current line according to `gnus-summary-highlight'."
11269   (let* ((beg (gnus-point-at-bol))
11270          (article (or (gnus-summary-article-number) gnus-current-article))
11271          (score (or (cdr (assq article
11272                                gnus-newsgroup-scored))
11273                     gnus-summary-default-score 0))
11274          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11275          (inhibit-read-only t)
11276          (default gnus-summary-default-score)
11277          (default-high gnus-summary-default-high-score)
11278          (default-low gnus-summary-default-low-score)
11279          (uncached (memq article gnus-newsgroup-undownloaded))
11280          (downloaded (not uncached)))
11281     (let ((face (funcall (gnus-summary-highlight-line-0))))
11282       (unless (eq face (get-text-property beg 'face))
11283         (gnus-put-text-property-excluding-characters-with-faces
11284          beg (gnus-point-at-eol) 'face
11285          (setq face (if (boundp face) (symbol-value face) face)))
11286         (when gnus-summary-highlight-line-function
11287           (funcall gnus-summary-highlight-line-function article face))))))
11288
11289 (defun gnus-update-read-articles (group unread &optional compute)
11290   "Update the list of read articles in GROUP.
11291 UNREAD is a sorted list."
11292   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11293          (entry (gnus-gethash group gnus-newsrc-hashtb))
11294          (info (nth 2 entry))
11295          (prev 1)
11296          read)
11297     (if (or (not info) (not active))
11298         ;; There is no info on this group if it was, in fact,
11299         ;; killed.  Gnus stores no information on killed groups, so
11300         ;; there's nothing to be done.
11301         ;; One could store the information somewhere temporarily,
11302         ;; perhaps...  Hmmm...
11303         ()
11304       ;; Remove any negative articles numbers.
11305       (while (and unread (< (car unread) 0))
11306         (setq unread (cdr unread)))
11307       ;; Remove any expired article numbers
11308       (while (and unread (< (car unread) (car active)))
11309         (setq unread (cdr unread)))
11310       ;; Compute the ranges of read articles by looking at the list of
11311       ;; unread articles.
11312       (while unread
11313         (when (/= (car unread) prev)
11314           (push (if (= prev (1- (car unread))) prev
11315                   (cons prev (1- (car unread))))
11316                 read))
11317         (setq prev (1+ (car unread)))
11318         (setq unread (cdr unread)))
11319       (when (<= prev (cdr active))
11320         (push (cons prev (cdr active)) read))
11321       (setq read (if (> (length read) 1) (nreverse read) read))
11322       (if compute
11323           read
11324         (save-excursion
11325           (let (setmarkundo)
11326             ;; Propagate the read marks to the backend.
11327             (when (gnus-check-backend-function 'request-set-mark group)
11328               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11329                     (add (gnus-remove-from-range read (gnus-info-read info))))
11330                 (when (or add del)
11331                   (unless (gnus-check-group group)
11332                     (error "Can't open server for %s" group))
11333                   (gnus-request-set-mark
11334                    group (delq nil (list (if add (list add 'add '(read)))
11335                                          (if del (list del 'del '(read))))))
11336                   (setq setmarkundo
11337                         `(gnus-request-set-mark
11338                           ,group
11339                           ',(delq nil (list
11340                                        (if del (list del 'add '(read)))
11341                                        (if add (list add 'del '(read))))))))))
11342             (set-buffer gnus-group-buffer)
11343             (gnus-undo-register
11344               `(progn
11345                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11346                  (gnus-info-set-read ',info ',(gnus-info-read info))
11347                  (gnus-get-unread-articles-in-group ',info
11348                                                     (gnus-active ,group))
11349                  (gnus-group-update-group ,group t)
11350                  ,setmarkundo))))
11351         ;; Enter this list into the group info.
11352         (gnus-info-set-read info read)
11353         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11354         (gnus-get-unread-articles-in-group info (gnus-active group))
11355         t))))
11356
11357 (defun gnus-offer-save-summaries ()
11358   "Offer to save all active summary buffers."
11359   (let (buffers)
11360     ;; Go through all buffers and find all summaries.
11361     (dolist (buffer (buffer-list))
11362       (when (and (setq buffer (buffer-name buffer))
11363                  (string-match "Summary" buffer)
11364                  (save-excursion
11365                    (set-buffer buffer)
11366                    ;; We check that this is, indeed, a summary buffer.
11367                    (and (eq major-mode 'gnus-summary-mode)
11368                         ;; Also make sure this isn't bogus.
11369                         gnus-newsgroup-prepared
11370                         ;; Also make sure that this isn't a
11371                         ;; dead summary buffer.
11372                         (not gnus-dead-summary-mode))))
11373         (push buffer buffers)))
11374     ;; Go through all these summary buffers and offer to save them.
11375     (when buffers
11376       (save-excursion
11377         (map-y-or-n-p
11378          "Update summary buffer %s? "
11379          (lambda (buf)
11380            (switch-to-buffer buf)
11381            (gnus-summary-exit))
11382          buffers)))))
11383
11384
11385 ;;; @ for mime-partial
11386 ;;;
11387
11388 (defun gnus-request-partial-message ()
11389   (save-excursion
11390     (let ((number (gnus-summary-article-number))
11391           (group gnus-newsgroup-name)
11392           (mother gnus-article-buffer))
11393       (set-buffer (get-buffer-create " *Partial Article*"))
11394       (erase-buffer)
11395       (setq mime-preview-buffer mother)
11396       (gnus-request-article-this-buffer number group)
11397       (mime-parse-buffer)
11398       )))
11399
11400 (autoload 'mime-combine-message/partial-pieces-automatically
11401   "mime-partial"
11402   "Internal method to combine message/partial messages automatically.")
11403
11404 (mime-add-condition
11405  'action '((type . message)(subtype . partial)
11406            (major-mode . gnus-original-article-mode)
11407            (method . mime-combine-message/partial-pieces-automatically)
11408            (summary-buffer-exp . gnus-summary-buffer)
11409            (request-partial-message-method . gnus-request-partial-message)
11410            ))
11411
11412
11413 ;;; @ for message/rfc822
11414 ;;;
11415
11416 (defun gnus-mime-extract-message/rfc822 (entity situation)
11417   "Burst a forwarded article."
11418   (save-excursion
11419     (set-buffer gnus-summary-buffer)
11420     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11421                                    gnus-newsgroup-name 'gnus-group-history))
11422            (gnus-group-marked (list group))
11423            article info)
11424       (with-temp-buffer
11425         (mime-insert-entity-content entity)
11426         (setq article (gnus-request-accept-article group)))
11427       (when (and (consp article)
11428                  (numberp (setq article (cdr article))))
11429         (setq info (gnus-get-info group))
11430         (gnus-info-set-read info
11431                             (gnus-remove-from-range (gnus-info-read info)
11432                                                     (list article)))
11433         (when (string-equal group gnus-newsgroup-name)
11434           (forward-line 1)
11435           (let (gnus-show-threads)
11436             (gnus-summary-goto-subject article t))
11437           (gnus-summary-clear-mark-forward 1))
11438         (set-buffer gnus-group-buffer)
11439         (gnus-group-get-new-news-this-group nil t)))))
11440
11441 (mime-add-condition
11442  'action '((type . message)(subtype . rfc822)
11443            (major-mode . gnus-original-article-mode)
11444            (method . gnus-mime-extract-message/rfc822)
11445            (mode . "extract")
11446            ))
11447
11448 (mime-add-condition
11449  'action '((type . message)(subtype . news)
11450            (major-mode . gnus-original-article-mode)
11451            (method . gnus-mime-extract-message/rfc822)
11452            (mode . "extract")
11453            ))
11454
11455 (defun gnus-mime-extract-multipart (entity situation)
11456   (let ((children (mime-entity-children entity))
11457         mime-acting-situation-to-override
11458         f)
11459     (while children
11460       (mime-play-entity (car children)
11461                         (cons (assq 'mode situation)
11462                               mime-acting-situation-to-override))
11463       (setq children (cdr children)))
11464     (if (setq f (cdr (assq 'after-method
11465                            mime-acting-situation-to-override)))
11466         (eval f)
11467       )))
11468
11469 (mime-add-condition
11470  'action '((type . multipart)
11471            (method . gnus-mime-extract-multipart)
11472            (mode . "extract")
11473            )
11474  'with-default)
11475
11476
11477 ;;; @ end
11478 ;;;
11479
11480 (defun gnus-summary-inherit-default-charset ()
11481   "Import `default-mime-charset' from summary buffer.
11482 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11483 of FLIM is used."
11484   (if (buffer-live-p gnus-summary-buffer)
11485       (let (d-m-c d-m-c-u)
11486         (with-current-buffer gnus-summary-buffer
11487           (setq d-m-c (if (local-variable-p 'default-mime-charset
11488                                             gnus-summary-buffer)
11489                           default-mime-charset
11490                         t)
11491                 ;; LIMIT
11492                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11493                                               gnus-summary-buffer)
11494                             (symbol-value 'default-mime-charset-unlimited)
11495                           t)))
11496         (if (eq t d-m-c)
11497             (kill-local-variable 'default-mime-charset)
11498           (set (make-local-variable 'default-mime-charset) d-m-c))
11499         (if (eq t d-m-c-u)
11500             (kill-local-variable 'default-mime-charset-unlimited)
11501           (set (make-local-variable 'default-mime-charset-unlimited)
11502                d-m-c-u)))))
11503
11504 (defun gnus-summary-setup-default-charset ()
11505   "Setup newsgroup default charset."
11506   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11507       (progn
11508         (setq gnus-newsgroup-charset nil)
11509         (set (make-local-variable 'default-mime-charset) nil)
11510         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11511           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11512     (let ((ignored-charsets
11513            (or gnus-newsgroup-ephemeral-ignored-charsets
11514                (append
11515                 (and gnus-newsgroup-name
11516                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11517                 gnus-newsgroup-ignored-charsets)))
11518           charset)
11519       (setq gnus-newsgroup-charset
11520             (or gnus-newsgroup-ephemeral-charset
11521                 (when (and gnus-newsgroup-name
11522                            (setq charset (gnus-parameter-charset
11523                                           gnus-newsgroup-name)))
11524                   (make-local-variable 'default-mime-charset)
11525                   (setq default-mime-charset charset))
11526                 gnus-default-charset))
11527       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11528            ignored-charsets))))
11529
11530 ;;;
11531 ;;; Mime Commands
11532 ;;;
11533
11534 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11535   "Display the current article buffer fully MIME-buttonized.
11536 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11537 treated as multipart/mixed."
11538   (interactive "P")
11539   (require 'gnus-art)
11540   (let ((gnus-unbuttonized-mime-types nil)
11541         (gnus-mime-display-multipart-as-mixed show-all-parts))
11542     (gnus-summary-show-article)))
11543
11544 (defun gnus-summary-repair-multipart (article)
11545   "Add a Content-Type header to a multipart article without one."
11546   (interactive (list (gnus-summary-article-number)))
11547   (gnus-with-article article
11548     (message-narrow-to-head)
11549     (message-remove-header "Mime-Version")
11550     (goto-char (point-max))
11551     (insert "Mime-Version: 1.0\n")
11552     (widen)
11553     (when (search-forward "\n--" nil t)
11554       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11555         (message-narrow-to-head)
11556         (message-remove-header "Content-Type")
11557         (goto-char (point-max))
11558         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11559                         separator))
11560         (widen))))
11561   (let (gnus-mark-article-hook)
11562     (gnus-summary-select-article t t nil article)))
11563
11564 (defun gnus-summary-toggle-display-buttonized ()
11565   "Toggle the buttonizing of the article buffer."
11566   (interactive)
11567   (require 'gnus-art)
11568   (if (setq gnus-inhibit-mime-unbuttonizing
11569             (not gnus-inhibit-mime-unbuttonizing))
11570       (let ((gnus-unbuttonized-mime-types nil))
11571         (gnus-summary-show-article))
11572     (gnus-summary-show-article)))
11573
11574 ;;;
11575 ;;; Intelli-mouse commmands
11576 ;;;
11577
11578 (defun gnus-wheel-summary-scroll (event)
11579   (interactive "e")
11580   (let ((amount (if (memq 'shift (event-modifiers event))
11581                     (car gnus-wheel-scroll-amount)
11582                   (cdr gnus-wheel-scroll-amount)))
11583         (direction (- (* (static-if (featurep 'xemacs)
11584                              (event-button event)
11585                            (cond ((eq 'mouse-4 (event-basic-type event))
11586                                   4)
11587                                  ((eq 'mouse-5 (event-basic-type event))
11588                                   5)))
11589                          2) 9))
11590         edge)
11591     (gnus-summary-scroll-up (* amount direction))
11592     (when (gnus-eval-in-buffer-window gnus-article-buffer
11593             (save-restriction
11594               (widen)
11595               (and (if (< 0 direction)
11596                        (gnus-article-next-page 0)
11597                      (gnus-article-prev-page 0)
11598                      (bobp))
11599                    (if (setq edge (get-text-property
11600                                    (point-min) 'gnus-wheel-edge))
11601                        (setq edge (* edge direction))
11602                      (setq edge -1))
11603                    (or (plusp edge)
11604                        (let ((buffer-read-only nil)
11605                              (inhibit-read-only t))
11606                          (put-text-property (point-min) (point-max)
11607                                             'gnus-wheel-edge direction)
11608                          nil))
11609                    (or (> edge gnus-wheel-edge-resistance)
11610                        (let ((buffer-read-only nil)
11611                              (inhibit-read-only t))
11612                          (put-text-property (point-min) (point-max)
11613                                             'gnus-wheel-edge
11614                                             (* (1+ edge) direction))
11615                          nil))
11616                    (eq last-command 'gnus-wheel-summary-scroll))))
11617       (gnus-summary-next-article nil nil (minusp direction)))))
11618
11619 (defun gnus-wheel-install ()
11620   "Enable mouse wheel support on summary window."
11621   (when gnus-use-wheel
11622     (let ((keys
11623            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11624       (dolist (key keys)
11625         (define-key gnus-summary-mode-map key
11626           'gnus-wheel-summary-scroll)))))
11627
11628 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11629
11630 ;;;
11631 ;;; Traditional PGP commmands
11632 ;;;
11633
11634 (defun gnus-summary-decrypt-article (&optional force)
11635   "Decrypt the current article in traditional PGP way.
11636 This will have permanent effect only in mail groups.
11637 If FORCE is non-nil, allow editing of articles even in read-only
11638 groups."
11639   (interactive "P")
11640   (gnus-summary-select-article t)
11641   (gnus-eval-in-buffer-window gnus-article-buffer
11642     (save-excursion
11643       (save-restriction
11644         (widen)
11645         (goto-char (point-min))
11646         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11647           (error "Not a traditional PGP message!"))
11648         (let ((armor-start (match-beginning 0)))
11649           (if (and (pgg-decrypt-region armor-start (point-max))
11650                    (or force (not (gnus-group-read-only-p))))
11651               (let ((inhibit-read-only t)
11652                     buffer-read-only)
11653                 (delete-region armor-start
11654                                (progn
11655                                  (re-search-forward "^-+END PGP" nil t)
11656                                  (beginning-of-line 2)
11657                                  (point)))
11658                 (insert-buffer-substring pgg-output-buffer))))))))
11659
11660 (defun gnus-summary-verify-article ()
11661   "Verify the current article in traditional PGP way."
11662   (interactive)
11663   (save-excursion
11664     (set-buffer gnus-original-article-buffer)
11665     (goto-char (point-min))
11666     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11667       (error "Not a traditional PGP message!"))
11668     (re-search-forward "^-+END PGP" nil t)
11669     (beginning-of-line 2)
11670     (call-interactively (function pgg-verify-region))))
11671
11672 ;;;
11673 ;;; Generic summary marking commands
11674 ;;;
11675
11676 (defvar gnus-summary-marking-alist
11677   '((read gnus-del-mark "d")
11678     (unread gnus-unread-mark "u")
11679     (ticked gnus-ticked-mark "!")
11680     (dormant gnus-dormant-mark "?")
11681     (expirable gnus-expirable-mark "e"))
11682   "An alist of names/marks/keystrokes.")
11683
11684 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11685 (defvar gnus-summary-mark-map)
11686
11687 (defun gnus-summary-make-all-marking-commands ()
11688   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11689   (dolist (elem gnus-summary-marking-alist)
11690     (apply 'gnus-summary-make-marking-command elem)))
11691
11692 (defun gnus-summary-make-marking-command (name mark keystroke)
11693   (let ((map (make-sparse-keymap)))
11694     (define-key gnus-summary-generic-mark-map keystroke map)
11695     (dolist (lway `((next "next" next nil "n")
11696                     (next-unread "next unread" next t "N")
11697                     (prev "previous" prev nil "p")
11698                     (prev-unread "previous unread" prev t "P")
11699                     (nomove "" nil nil ,keystroke)))
11700       (let ((func (gnus-summary-make-marking-command-1
11701                    mark (car lway) lway name)))
11702         (setq func (eval func))
11703         (define-key map (nth 4 lway) func)))))
11704
11705 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11706   `(defun ,(intern
11707             (format "gnus-summary-put-mark-as-%s%s"
11708                     name (if (eq way 'nomove)
11709                              ""
11710                            (concat "-" (symbol-name way)))))
11711      (n)
11712      ,(format
11713        "Mark the current article as %s%s.
11714 If N, the prefix, then repeat N times.
11715 If N is negative, move in reverse order.
11716 The difference between N and the actual number of articles marked is
11717 returned."
11718        name (car (cdr lway)))
11719      (interactive "p")
11720      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11721
11722 (defun gnus-summary-generic-mark (n mark move unread)
11723   "Mark N articles with MARK."
11724   (unless (eq major-mode 'gnus-summary-mode)
11725     (error "This command can only be used in the summary buffer"))
11726   (gnus-summary-show-thread)
11727   (let ((nummove
11728          (cond
11729           ((eq move 'next) 1)
11730           ((eq move 'prev) -1)
11731           (t 0))))
11732     (if (zerop nummove)
11733         (setq n 1)
11734       (when (< n 0)
11735         (setq n (abs n)
11736               nummove (* -1 nummove))))
11737     (while (and (> n 0)
11738                 (gnus-summary-mark-article nil mark)
11739                 (zerop (gnus-summary-next-subject nummove unread t)))
11740       (setq n (1- n)))
11741     (when (/= 0 n)
11742       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11743     (gnus-summary-recenter)
11744     (gnus-summary-position-point)
11745     (gnus-set-mode-line 'summary)
11746     n))
11747
11748 (defun gnus-summary-insert-articles (articles)
11749   (when (setq articles
11750               (gnus-sorted-difference articles
11751                                       (mapcar (lambda (h)
11752                                                 (mail-header-number h))
11753                                               gnus-newsgroup-headers)))
11754     (setq gnus-newsgroup-headers
11755           (gnus-merge 'list
11756                       gnus-newsgroup-headers
11757                       (gnus-fetch-headers articles)
11758                       'gnus-article-sort-by-number))
11759     ;; Suppress duplicates?
11760     (when gnus-suppress-duplicates
11761       (gnus-dup-suppress-articles))
11762
11763     ;; We might want to build some more threads first.
11764     (when (and gnus-fetch-old-headers
11765                (eq gnus-headers-retrieved-by 'nov))
11766       (if (eq gnus-fetch-old-headers 'invisible)
11767           (gnus-build-all-threads)
11768         (gnus-build-old-threads)))
11769     ;; Let the Gnus agent mark articles as read.
11770     (when gnus-agent
11771       (gnus-agent-get-undownloaded-list))
11772     ;; Remove list identifiers from subject
11773     (when gnus-list-identifiers
11774       (gnus-summary-remove-list-identifiers))
11775     ;; First and last article in this newsgroup.
11776     (when gnus-newsgroup-headers
11777       (setq gnus-newsgroup-begin
11778             (mail-header-number (car gnus-newsgroup-headers))
11779             gnus-newsgroup-end
11780             (mail-header-number
11781              (gnus-last-element gnus-newsgroup-headers))))
11782     (when gnus-use-scoring
11783       (gnus-possibly-score-headers))))
11784
11785 (defun gnus-summary-insert-old-articles (&optional all)
11786   "Insert all old articles in this group.
11787 If ALL is non-nil, already read articles become readable.
11788 If ALL is a number, fetch this number of articles."
11789   (interactive "P")
11790   (prog1
11791       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11792             older len)
11793         (setq older
11794               ;; Some nntp servers lie about their active range.  When
11795               ;; this happens, the active range can be in the millions.
11796               ;; Use a compressed range to avoid creating a huge list.
11797               (gnus-range-difference (list gnus-newsgroup-active) old))
11798         (setq len (gnus-range-length older))
11799         (cond
11800          ((null older) nil)
11801          ((numberp all)
11802           (if (< all len)
11803               (let ((older-range (nreverse older)))
11804                 (setq older nil)
11805
11806                 (while (> all 0)
11807                   (let* ((r (pop older-range))
11808                          (min (if (numberp r) r (car r)))
11809                          (max (if (numberp r) r (cdr r))))
11810                     (while (and (<= min max)
11811                                 (> all 0))
11812                       (push max older)
11813                       (setq all (1- all)
11814                             max (1- max))))))
11815             (setq older (gnus-uncompress-range older))))
11816          (all
11817           (setq older (gnus-uncompress-range older)))
11818          (t
11819           (when (and (numberp gnus-large-newsgroup)
11820                    (> len gnus-large-newsgroup))
11821               (let* ((cursor-in-echo-area nil)
11822                      (initial (gnus-parameter-large-newsgroup-initial
11823                                gnus-newsgroup-name))
11824                      (input
11825                       (read-string
11826                        (format
11827                         "How many articles from %s (%s %d): "
11828                         (gnus-limit-string
11829                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11830                         (if initial "max" "default")
11831                         len)
11832                        (if initial
11833                            (cons (number-to-string initial)
11834                                  0)))))
11835                 (unless (string-match "^[ \t]*$" input)
11836                   (setq all (string-to-number input))
11837                   (if (< all len)
11838                       (let ((older-range (nreverse older)))
11839                         (setq older nil)
11840
11841                         (while (> all 0)
11842                           (let* ((r (pop older-range))
11843                                  (min (if (numberp r) r (car r)))
11844                                  (max (if (numberp r) r (cdr r))))
11845                             (while (and (<= min max)
11846                                         (> all 0))
11847                               (push max older)
11848                               (setq all (1- all)
11849                                     max (1- max))))))))))
11850           (setq older (gnus-uncompress-range older))))
11851         (if (not older)
11852             (message "No old news.")
11853           (gnus-summary-insert-articles older)
11854           (gnus-summary-limit (gnus-sorted-nunion old older))))
11855     (gnus-summary-position-point)))
11856
11857 (defun gnus-summary-insert-new-articles ()
11858   "Insert all new articles in this group."
11859   (interactive)
11860   (prog1
11861       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11862             (old-active gnus-newsgroup-active)
11863             (nnmail-fetched-sources (list t))
11864             i new)
11865         (setq gnus-newsgroup-active
11866               (gnus-activate-group gnus-newsgroup-name 'scan))
11867         (setq i (cdr gnus-newsgroup-active))
11868         (while (> i (cdr old-active))
11869           (push i new)
11870           (decf i))
11871         (if (not new)
11872             (message "No gnus is bad news.")
11873           (gnus-summary-insert-articles new)
11874           (setq gnus-newsgroup-unreads
11875                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11876           (gnus-summary-limit (gnus-sorted-nunion old new))))
11877     (gnus-summary-position-point)))
11878
11879 (gnus-summary-make-all-marking-commands)
11880
11881 (gnus-ems-redefine)
11882
11883 (provide 'gnus-sum)
11884
11885 (run-hooks 'gnus-sum-load-hook)
11886
11887 ;; Local Variables:
11888 ;; coding: iso-8859-1
11889 ;; End:
11890
11891 ;;; gnus-sum.el ends here