Synch to No Gnus 200401230204.
[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, 2004
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   (defvar tool-bar-map))
34
35 (require 'gnus)
36 (require 'gnus-group)
37 (require 'gnus-spec)
38 (require 'gnus-range)
39 (require 'gnus-int)
40 (require 'gnus-undo)
41 (require 'gnus-util)
42 (require 'nnoo)
43
44 (eval-when-compile
45   (require 'mime-play)
46   (require 'static))
47
48 (eval-and-compile
49   (autoload 'pgg-decrypt-region "pgg" nil t)
50   (autoload 'pgg-verify-region "pgg" nil t))
51
52 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
53 (autoload 'gnus-cache-write-active "gnus-cache")
54 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
55 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
56 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
57 (autoload 'mm-uu-dissect "mm-uu")
58 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
59   "Deuglify broken Outlook (Express) articles and redisplay."
60   t)
61 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
62 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
63 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
64
65 (defcustom gnus-kill-summary-on-exit t
66   "*If non-nil, kill the summary buffer when you exit from it.
67 If nil, the summary will become a \"*Dead Summary*\" buffer, and
68 it will be killed sometime later."
69   :group 'gnus-summary-exit
70   :type 'boolean)
71
72 (defcustom gnus-fetch-old-headers nil
73   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
74 If an unread article in the group refers to an older, already read (or
75 just marked as read) article, the old article will not normally be
76 displayed in the Summary buffer.  If this variable is non-nil, Gnus
77 will attempt to grab the headers to the old articles, and thereby
78 build complete threads.  If it has the value `some', only enough
79 headers to connect otherwise loose threads will be displayed.  This
80 variable can also be a number.  In that case, no more than that number
81 of old headers will be fetched.  If it has the value `invisible', all
82 old headers will be fetched, but none will be displayed.
83
84 The server has to support NOV for any of this to work."
85   :group 'gnus-thread
86   :type '(choice (const :tag "off" nil)
87                  (const some)
88                  number
89                  (sexp :menu-tag "other" t)))
90
91 (defcustom gnus-refer-thread-limit 200
92   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
93 If t, fetch all the available old headers."
94   :group 'gnus-thread
95   :type '(choice number
96                  (sexp :menu-tag "other" t)))
97
98 (defcustom gnus-summary-make-false-root 'adopt
99   "*nil means that Gnus won't gather loose threads.
100 If the root of a thread has expired or been read in a previous
101 session, the information necessary to build a complete thread has been
102 lost.  Instead of having many small sub-threads from this original thread
103 scattered all over the summary buffer, Gnus can gather them.
104
105 If non-nil, Gnus will try to gather all loose sub-threads from an
106 original thread into one large thread.
107
108 If this variable is non-nil, it should be one of `none', `adopt',
109 `dummy' or `empty'.
110
111 If this variable is `none', Gnus will not make a false root, but just
112 present the sub-threads after another.
113 If this variable is `dummy', Gnus will create a dummy root that will
114 have all the sub-threads as children.
115 If this variable is `adopt', Gnus will make one of the \"children\"
116 the parent and mark all the step-children as such.
117 If this variable is `empty', the \"children\" are printed with empty
118 subject fields.  (Or rather, they will be printed with a string
119 given by the `gnus-summary-same-subject' variable.)"
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const none)
123                  (const dummy)
124                  (const adopt)
125                  (const empty)))
126
127 (defcustom gnus-summary-make-false-root-always nil
128   "Always make a false dummy root."
129   :group 'gnus-thread
130   :type 'boolean)
131
132 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
133   "*A regexp to match subjects to be excluded from loose thread gathering.
134 As loose thread gathering is done on subjects only, that means that
135 there can be many false gatherings performed.  By rooting out certain
136 common subjects, gathering might become saner."
137   :group 'gnus-thread
138   :type 'regexp)
139
140 (defcustom gnus-summary-gather-subject-limit nil
141   "*Maximum length of subject comparisons when gathering loose threads.
142 Use nil to compare full subjects.  Setting this variable to a low
143 number will help gather threads that have been corrupted by
144 newsreaders chopping off subject lines, but it might also mean that
145 unrelated articles that have subject that happen to begin with the
146 same few characters will be incorrectly gathered.
147
148 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
149 comparing subjects."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  (const fuzzy)
153                  (sexp :menu-tag "on" t)))
154
155 (defcustom gnus-simplify-subject-functions nil
156   "List of functions taking a string argument that simplify subjects.
157 The functions are applied recursively.
158
159 Useful functions to put in this list include:
160 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
161 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
162   :group 'gnus-thread
163   :type '(repeat function))
164
165 (defcustom gnus-simplify-ignored-prefixes nil
166   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  regexp))
170
171 (defcustom gnus-build-sparse-threads nil
172   "*If non-nil, fill in the gaps in threads.
173 If `some', only fill in the gaps that are needed to tie loose threads
174 together.  If `more', fill in all leaf nodes that Gnus can find.  If
175 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
176   :group 'gnus-thread
177   :type '(choice (const :tag "off" nil)
178                  (const some)
179                  (const more)
180                  (sexp :menu-tag "all" t)))
181
182 (defcustom gnus-summary-thread-gathering-function
183   'gnus-gather-threads-by-subject
184   "*Function used for gathering loose threads.
185 There are two pre-defined functions: `gnus-gather-threads-by-subject',
186 which only takes Subjects into consideration; and
187 `gnus-gather-threads-by-references', which compared the References
188 headers of the articles to find matches."
189   :group 'gnus-thread
190   :type '(radio (function-item gnus-gather-threads-by-subject)
191                 (function-item gnus-gather-threads-by-references)
192                 (function :tag "other")))
193
194 (defcustom gnus-summary-same-subject ""
195   "*String indicating that the current article has the same subject as the previous.
196 This variable will only be used if the value of
197 `gnus-summary-make-false-root' is `empty'."
198   :group 'gnus-summary-format
199   :type 'string)
200
201 (defcustom gnus-summary-goto-unread t
202   "*If t, many commands will go to the next unread article.
203 This applies to marking commands as well as other commands that
204 \"naturally\" select the next article, like, for instance, `SPC' at
205 the end of an article.
206
207 If nil, the marking commands do NOT go to the next unread article
208 \(they go to the next article instead).  If `never', commands that
209 usually go to the next unread article, will go to the next article,
210 whether it is read or not."
211   :group 'gnus-summary-marks
212   :link '(custom-manual "(gnus)Setting Marks")
213   :type '(choice (const :tag "off" nil)
214                  (const never)
215                  (sexp :menu-tag "on" t)))
216
217 (defcustom gnus-summary-default-score 0
218   "*Default article score level.
219 All scores generated by the score files will be added to this score.
220 If this variable is nil, scoring will be disabled."
221   :group 'gnus-score-default
222   :type '(choice (const :tag "disable")
223                  integer))
224
225 (defcustom gnus-summary-default-high-score 0
226   "*Default threshold for a high scored article.
227 An article will be highlighted as high scored if its score is greater
228 than this score."
229   :group 'gnus-score-default
230   :type 'integer)
231
232 (defcustom gnus-summary-default-low-score 0
233   "*Default threshold for a low scored article.
234 An article will be highlighted as low scored if its score is smaller
235 than this score."
236   :group 'gnus-score-default
237   :type 'integer)
238
239 (defcustom gnus-summary-zcore-fuzz 0
240   "*Fuzziness factor for the zcore in the summary buffer.
241 Articles with scores closer than this to `gnus-summary-default-score'
242 will not be marked."
243   :group 'gnus-summary-format
244   :type 'integer)
245
246 (defcustom gnus-simplify-subject-fuzzy-regexp nil
247   "*Strings to be removed when doing fuzzy matches.
248 This can either be a regular expression or list of regular expressions
249 that will be removed from subject strings if fuzzy subject
250 simplification is selected."
251   :group 'gnus-thread
252   :type '(repeat regexp))
253
254 (defcustom gnus-show-threads t
255   "*If non-nil, display threads in summary mode."
256   :group 'gnus-thread
257   :type 'boolean)
258
259 (defcustom gnus-thread-hide-subtree nil
260   "*If non-nil, hide all threads initially.
261 This can be a predicate specifier which says which threads to hide.
262 If threads are hidden, you have to run the command
263 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
264 to expose hidden threads."
265   :group 'gnus-thread
266   :type '(radio (sexp :format "Non-nil\n"
267                       :match (lambda (widget value)
268                                (not (or (consp value) (functionp value))))
269                       :value t)
270                 (const nil)
271                 (sexp :tag "Predicate specifier" :size 0)))
272
273 (defcustom gnus-thread-hide-killed t
274   "*If non-nil, hide killed threads automatically."
275   :group 'gnus-thread
276   :type 'boolean)
277
278 (defcustom gnus-thread-ignore-subject t
279   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
280 If nil, articles that have different subjects from their parents will
281 start separate threads."
282   :group 'gnus-thread
283   :type 'boolean)
284
285 (defcustom gnus-thread-operation-ignore-subject t
286   "*If non-nil, subjects will be ignored when doing thread commands.
287 This affects commands like `gnus-summary-kill-thread' and
288 `gnus-summary-lower-thread'.
289
290 If this variable is nil, articles in the same thread with different
291 subjects will not be included in the operation in question.  If this
292 variable is `fuzzy', only articles that have subjects that are fuzzily
293 equal will be included."
294   :group 'gnus-thread
295   :type '(choice (const :tag "off" nil)
296                  (const fuzzy)
297                  (sexp :tag "on" t)))
298
299 (defcustom gnus-thread-indent-level 4
300   "*Number that says how much each sub-thread should be indented."
301   :group 'gnus-thread
302   :type 'integer)
303
304 (defcustom gnus-auto-extend-newsgroup t
305   "*If non-nil, extend newsgroup forward and backward when requested."
306   :group 'gnus-summary-choose
307   :type 'boolean)
308
309 (defcustom gnus-auto-select-first t
310   "*If non-nil, select the article under point.
311 Which article this is is controlled by the `gnus-auto-select-subject'
312 variable.
313
314 If you want to prevent automatic selection of articles in some
315 newsgroups, set the variable to nil in `gnus-select-group-hook'."
316   :group 'gnus-group-select
317   :type '(choice (const :tag "none" nil)
318                  (sexp :menu-tag "first" t)))
319
320 (defcustom gnus-auto-select-subject 'unread
321   "*Says what subject to place under point when entering a group.
322
323 This variable can either be the symbols `first' (place point on the
324 first subject), `unread' (place point on the subject line of the first
325 unread article), `best' (place point on the subject line of the
326 higest-scored article), `unseen' (place point on the subject line of
327 the first unseen article), 'unseen-or-unread' (place point on the subject
328 line of the first unseen article or, if all article have been seen, on the
329 subject line of the first unread article), or a function to be called to
330 place point on some subject line."
331   :group 'gnus-group-select
332   :type '(choice (const best)
333                  (const unread)
334                  (const first)
335                  (const unseen)
336                  (const unseen-or-unread)))
337
338 (defcustom gnus-dont-select-after-jump-to-other-group nil
339   "If non-nil, don't select the first unread article after entering the
340 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
341 it is depend on the value of `gnus-auto-select-first' whether to select
342 or not."
343   :group 'gnus-group-select
344   :type 'boolean)
345
346 (defcustom gnus-auto-select-next t
347   "*If non-nil, offer to go to the next group from the end of the previous.
348 If the value is t and the next newsgroup is empty, Gnus will exit
349 summary mode and go back to group mode.  If the value is neither nil
350 nor t, Gnus will select the following unread newsgroup.  In
351 particular, if the value is the symbol `quietly', the next unread
352 newsgroup will be selected without any confirmation, and if it is
353 `almost-quietly', the next group will be selected without any
354 confirmation if you are located on the last article in the group.
355 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
356 will go to the next group without confirmation."
357   :group 'gnus-summary-maneuvering
358   :type '(choice (const :tag "off" nil)
359                  (const quietly)
360                  (const almost-quietly)
361                  (const slightly-quietly)
362                  (sexp :menu-tag "on" t)))
363
364 (defcustom gnus-auto-select-same nil
365   "*If non-nil, select the next article with the same subject.
366 If there are no more articles with the same subject, go to
367 the first unread article."
368   :group 'gnus-summary-maneuvering
369   :type 'boolean)
370
371 (defcustom gnus-auto-goto-ignores 'unfetched
372   "*Says how to handle unfetched articles when maneuvering.
373
374 This variable can either be the symbols nil (maneuver to any
375 article), `undownloaded' (maneuvering while unplugged ignores articles
376 that have not been fetched), `always-undownloaded' (maneuvering always
377 ignores articles that have not been fetched), `unfetched' (maneuvering
378 ignores articles whose headers have not been fetched).
379
380 NOTE: The list of unfetched articles will always be nil when plugged
381 and, when unplugged, a subset of the undownloaded article list."
382   :group 'gnus-summary-maneuvering
383   :type '(choice (const :tag "None" nil)
384                  (const :tag "Undownloaded when unplugged" undownloaded)
385                  (const :tag "Undownloaded" always-undownloaded)
386                  (const :tag "Unfetched" unfetched)))
387
388 (defcustom gnus-summary-check-current nil
389   "*If non-nil, consider the current article when moving.
390 The \"unread\" movement commands will stay on the same line if the
391 current article is unread."
392   :group 'gnus-summary-maneuvering
393   :type 'boolean)
394
395 (defcustom gnus-auto-center-summary t
396   "*If non-nil, always center the current summary buffer.
397 In particular, if `vertical' do only vertical recentering.  If non-nil
398 and non-`vertical', do both horizontal and vertical recentering."
399   :group 'gnus-summary-maneuvering
400   :type '(choice (const :tag "none" nil)
401                  (const vertical)
402                  (integer :tag "height")
403                  (sexp :menu-tag "both" t)))
404
405 (defvar gnus-auto-center-group t
406   "*If non-nil, always center the group buffer.")
407
408 (defcustom gnus-show-all-headers nil
409   "*If non-nil, don't hide any headers."
410   :group 'gnus-article-hiding
411   :group 'gnus-article-headers
412   :type 'boolean)
413
414 (defcustom gnus-summary-ignore-duplicates nil
415   "*If non-nil, ignore articles with identical Message-ID headers."
416   :group 'gnus-summary
417   :type 'boolean)
418
419 (defcustom gnus-single-article-buffer t
420   "*If non-nil, display all articles in the same buffer.
421 If nil, each group will get its own article buffer."
422   :group 'gnus-article-various
423   :type 'boolean)
424
425 (defcustom gnus-break-pages t
426   "*If non-nil, do page breaking on articles.
427 The page delimiter is specified by the `gnus-page-delimiter'
428 variable."
429   :group 'gnus-article-various
430   :type 'boolean)
431
432 (defcustom gnus-show-mime t
433   "*If non-nil, do mime processing of articles.
434 The articles will simply be fed to the function given by
435 `gnus-article-display-method-for-mime'."
436   :group 'gnus-article-mime
437   :type 'boolean)
438
439 (defcustom gnus-move-split-methods nil
440   "*Variable used to suggest where articles are to be moved to.
441 It uses the same syntax as the `gnus-split-methods' variable.
442 However, whereas `gnus-split-methods' specifies file names as targets,
443 this variable specifies group names."
444   :group 'gnus-summary-mail
445   :type '(repeat (choice (list :value (fun) function)
446                          (cons :value ("" "") regexp (repeat string))
447                          (sexp :value nil))))
448
449 (defcustom gnus-unread-mark ?\ ;;;Whitespace
450   "*Mark used for unread articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-ticked-mark ?!
455   "*Mark used for ticked articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-dormant-mark ??
460   "*Mark used for dormant articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-del-mark ?r
465   "*Mark used for del'd articles."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-read-mark ?R
470   "*Mark used for read articles."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-expirable-mark ?E
475   "*Mark used for expirable articles."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-killed-mark ?K
480   "*Mark used for killed articles."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-spam-mark ?$
485   "*Mark used for spam articles."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-souped-mark ?F
490   "*Mark used for souped articles."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-kill-file-mark ?X
495   "*Mark used for articles killed by kill files."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-low-score-mark ?Y
500   "*Mark used for articles with a low score."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-catchup-mark ?C
505   "*Mark used for articles that are caught up."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-replied-mark ?A
510   "*Mark used for articles that have been replied to."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-forwarded-mark ?F
515   "*Mark used for articles that have been forwarded."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-recent-mark ?N
520   "*Mark used for articles that are recent."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-cached-mark ?*
525   "*Mark used for articles that are in the cache."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-saved-mark ?S
530   "*Mark used for articles that have been saved."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-unseen-mark ?.
535   "*Mark used for articles that haven't been seen."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-no-mark ?\ ;;;Whitespace
540   "*Mark used for articles that have no other secondary mark."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-ancient-mark ?O
545   "*Mark used for ancient articles."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-sparse-mark ?Q
550   "*Mark used for sparsely reffed articles."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-canceled-mark ?G
555   "*Mark used for canceled articles."
556   :group 'gnus-summary-marks
557   :type 'character)
558
559 (defcustom gnus-duplicate-mark ?M
560   "*Mark used for duplicate articles."
561   :group 'gnus-summary-marks
562   :type 'character)
563
564 (defcustom gnus-undownloaded-mark ?-
565   "*Mark used for articles that weren't downloaded."
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-downloaded-mark ?+
570   "*Mark used for articles that were downloaded."
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-downloadable-mark ?%
575   "*Mark used for articles that are to be downloaded."
576   :group 'gnus-summary-marks
577   :type 'character)
578
579 (defcustom gnus-unsendable-mark ?=
580   "*Mark used for articles that won't be sent."
581   :group 'gnus-summary-marks
582   :type 'character)
583
584 (defcustom gnus-score-over-mark ?+
585   "*Score mark used for articles with high scores."
586   :group 'gnus-summary-marks
587   :type 'character)
588
589 (defcustom gnus-score-below-mark ?-
590   "*Score mark used for articles with low scores."
591   :group 'gnus-summary-marks
592   :type 'character)
593
594 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
595   "*There is no thread under the article."
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-not-empty-thread-mark ?=
600   "*There is a thread under the article."
601   :group 'gnus-summary-marks
602   :type 'character)
603
604 (defcustom gnus-view-pseudo-asynchronously nil
605   "*If non-nil, Gnus will view pseudo-articles asynchronously."
606   :group 'gnus-extract-view
607   :type 'boolean)
608
609 (defcustom gnus-auto-expirable-marks
610   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
611         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
612         gnus-souped-mark gnus-duplicate-mark)
613   "*The list of marks converted into expiration if a group is auto-expirable."
614   :version "21.1"
615   :group 'gnus-summary
616   :type '(repeat character))
617
618 (defcustom gnus-inhibit-user-auto-expire t
619   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
620   :version "21.1"
621   :group 'gnus-summary
622   :type 'boolean)
623
624 (defcustom gnus-view-pseudos nil
625   "*If `automatic', pseudo-articles will be viewed automatically.
626 If `not-confirm', pseudos will be viewed automatically, and the user
627 will not be asked to confirm the command."
628   :group 'gnus-extract-view
629   :type '(choice (const :tag "off" nil)
630                  (const automatic)
631                  (const not-confirm)))
632
633 (defcustom gnus-view-pseudos-separately t
634   "*If non-nil, one pseudo-article will be created for each file to be viewed.
635 If nil, all files that use the same viewing command will be given as a
636 list of parameters to that command."
637   :group 'gnus-extract-view
638   :type 'boolean)
639
640 (defcustom gnus-insert-pseudo-articles t
641   "*If non-nil, insert pseudo-articles when decoding articles."
642   :group 'gnus-extract-view
643   :type 'boolean)
644
645 (defcustom gnus-summary-dummy-line-format
646   "   %(:                             :%) %S\n"
647   "*The format specification for the dummy roots in the summary buffer.
648 It works along the same lines as a normal formatting string,
649 with some simple extensions.
650
651 %S  The subject
652
653 General format specifiers can also be used.
654 See `(gnus)Formatting Variables'."
655   :link '(custom-manual "(gnus)Formatting Variables")
656   :group 'gnus-threading
657   :type 'string)
658
659 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
660   "*The format specification for the summary mode line.
661 It works along the same lines as a normal formatting string,
662 with some simple extensions:
663
664 %G  Group name
665 %p  Unprefixed group name
666 %A  Current article number
667 %z  Current article score
668 %V  Gnus version
669 %U  Number of unread articles in the group
670 %e  Number of unselected articles in the group
671 %Z  A string with unread/unselected article counts
672 %g  Shortish group name
673 %S  Subject of the current article
674 %u  User-defined spec
675 %s  Current score file name
676 %d  Number of dormant articles
677 %r  Number of articles that have been marked as read in this session
678 %E  Number of articles expunged by the score files"
679   :group 'gnus-summary-format
680   :type 'string)
681
682 (defcustom gnus-list-identifiers nil
683   "Regexp that matches list identifiers to be removed from subject.
684 This can also be a list of regexps."
685   :version "21.1"
686   :group 'gnus-summary-format
687   :group 'gnus-article-hiding
688   :type '(choice (const :tag "none" nil)
689                  (regexp :value ".*")
690                  (repeat :value (".*") regexp)))
691
692 (defcustom gnus-summary-mark-below 0
693   "*Mark all articles with a score below this variable as read.
694 This variable is local to each summary buffer and usually set by the
695 score file."
696   :group 'gnus-score-default
697   :type 'integer)
698
699 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
700   "*List of functions used for sorting articles in the summary buffer.
701
702 Each function takes two articles and returns non-nil if the first
703 article should be sorted before the other.  If you use more than one
704 function, the primary sort function should be the last.  You should
705 probably always include `gnus-article-sort-by-number' in the list of
706 sorting functions -- preferably first.  Also note that sorting by date
707 is often much slower than sorting by number, and the sorting order is
708 very similar.  (Sorting by date means sorting by the time the message
709 was sent, sorting by number means sorting by arrival time.)
710
711 Ready-made functions include `gnus-article-sort-by-number',
712 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
713 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
714 and `gnus-article-sort-by-score'.
715
716 When threading is turned on, the variable `gnus-thread-sort-functions'
717 controls how articles are sorted."
718   :group 'gnus-summary-sort
719   :type '(repeat (choice (function-item gnus-article-sort-by-number)
720                          (function-item gnus-article-sort-by-author)
721                          (function-item gnus-article-sort-by-subject)
722                          (function-item gnus-article-sort-by-date)
723                          (function-item gnus-article-sort-by-score)
724                          (function-item gnus-article-sort-by-random)
725                          (function :tag "other"))))
726
727 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
728   "*List of functions used for sorting threads in the summary buffer.
729 By default, threads are sorted by article number.
730
731 Each function takes two threads and returns non-nil if the first
732 thread should be sorted before the other.  If you use more than one
733 function, the primary sort function should be the last.  You should
734 probably always include `gnus-thread-sort-by-number' in the list of
735 sorting functions -- preferably first.  Also note that sorting by date
736 is often much slower than sorting by number, and the sorting order is
737 very similar.  (Sorting by date means sorting by the time the message
738 was sent, sorting by number means sorting by arrival time.)
739
740 Ready-made functions include `gnus-thread-sort-by-number',
741 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
742 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
743 `gnus-thread-sort-by-most-recent-number',
744 `gnus-thread-sort-by-most-recent-date',
745 `gnus-thread-sort-by-random', and
746 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
747
748 When threading is turned off, the variable
749 `gnus-article-sort-functions' controls how articles are sorted."
750   :group 'gnus-summary-sort
751   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
752                          (function-item gnus-thread-sort-by-author)
753                          (function-item gnus-thread-sort-by-subject)
754                          (function-item gnus-thread-sort-by-date)
755                          (function-item gnus-thread-sort-by-score)
756                          (function-item gnus-thread-sort-by-total-score)
757                          (function-item gnus-thread-sort-by-random)
758                          (function :tag "other"))))
759
760 (defcustom gnus-thread-score-function '+
761   "*Function used for calculating the total score of a thread.
762
763 The function is called with the scores of the article and each
764 subthread and should then return the score of the thread.
765
766 Some functions you can use are `+', `max', or `min'."
767   :group 'gnus-summary-sort
768   :type 'function)
769
770 (defcustom gnus-summary-expunge-below nil
771   "All articles that have a score less than this variable will be expunged.
772 This variable is local to the summary buffers."
773   :group 'gnus-score-default
774   :type '(choice (const :tag "off" nil)
775                  integer))
776
777 (defcustom gnus-thread-expunge-below nil
778   "All threads that have a total score less than this variable will be expunged.
779 See `gnus-thread-score-function' for en explanation of what a
780 \"thread score\" is.
781
782 This variable is local to the summary buffers."
783   :group 'gnus-threading
784   :group 'gnus-score-default
785   :type '(choice (const :tag "off" nil)
786                  integer))
787
788 (defcustom gnus-summary-mode-hook nil
789   "*A hook for Gnus summary mode.
790 This hook is run before any variables are set in the summary buffer."
791   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
792   :group 'gnus-summary-various
793   :type 'hook)
794
795 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
796 (when (featurep 'xemacs)
797   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
798   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
799   (add-hook 'gnus-summary-mode-hook
800             'gnus-xmas-switch-horizontal-scrollbar-off))
801
802 (defcustom gnus-summary-menu-hook nil
803   "*Hook run after the creation of the summary mode menu."
804   :group 'gnus-summary-visual
805   :type 'hook)
806
807 (defcustom gnus-summary-exit-hook nil
808   "*A hook called on exit from the summary buffer.
809 It will be called with point in the group buffer."
810   :group 'gnus-summary-exit
811   :type 'hook)
812
813 (defcustom gnus-summary-prepare-hook nil
814   "*A hook called after the summary buffer has been generated.
815 If you want to modify the summary buffer, you can use this hook."
816   :group 'gnus-summary-various
817   :type 'hook)
818
819 (defcustom gnus-summary-prepared-hook nil
820   "*A hook called as the last thing after the summary buffer has been generated."
821   :group 'gnus-summary-various
822   :type 'hook)
823
824 (defcustom gnus-summary-generate-hook nil
825   "*A hook run just before generating the summary buffer.
826 This hook is commonly used to customize threading variables and the
827 like."
828   :group 'gnus-summary-various
829   :type 'hook)
830
831 (defcustom gnus-select-group-hook nil
832   "*A hook called when a newsgroup is selected.
833
834 If you'd like to simplify subjects like the
835 `gnus-summary-next-same-subject' command does, you can use the
836 following hook:
837
838  (add-hook gnus-select-group-hook
839            (lambda ()
840              (mapcar (lambda (header)
841                        (mail-header-set-subject
842                         header
843                         (gnus-simplify-subject
844                          (mail-header-subject header) 're-only)))
845                      gnus-newsgroup-headers)))"
846   :group 'gnus-group-select
847   :type 'hook)
848
849 (defcustom gnus-select-article-hook nil
850   "*A hook called when an article is selected."
851   :group 'gnus-summary-choose
852   :options '(gnus-agent-fetch-selected-article)
853   :type 'hook)
854
855 (defcustom gnus-visual-mark-article-hook
856   (list 'gnus-highlight-selected-summary)
857   "*Hook run after selecting an article in the summary buffer.
858 It is meant to be used for highlighting the article in some way.  It
859 is not run if `gnus-visual' is nil."
860   :group 'gnus-summary-visual
861   :type 'hook)
862
863 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
864   "*A hook called before parsing the headers."
865   :group 'gnus-various
866   :type 'hook)
867
868 (defcustom gnus-exit-group-hook nil
869   "*A hook called when exiting summary mode.
870 This hook is not called from the non-updating exit commands like `Q'."
871   :group 'gnus-various
872   :type 'hook)
873
874 (defcustom gnus-summary-update-hook
875   (list 'gnus-summary-highlight-line)
876   "*A hook called when a summary line is changed.
877 The hook will not be called if `gnus-visual' is nil.
878
879 The default function `gnus-summary-highlight-line' will
880 highlight the line according to the `gnus-summary-highlight'
881 variable."
882   :group 'gnus-summary-visual
883   :type 'hook)
884
885 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
886   "*A hook called when an article is selected for the first time.
887 The hook is intended to mark an article as read (or unread)
888 automatically when it is selected."
889   :group 'gnus-summary-choose
890   :type 'hook)
891
892 (defcustom gnus-group-no-more-groups-hook nil
893   "*A hook run when returning to group mode having no more (unread) groups."
894   :group 'gnus-group-select
895   :type 'hook)
896
897 (defcustom gnus-ps-print-hook nil
898   "*A hook run before ps-printing something from Gnus."
899   :group 'gnus-summary
900   :type 'hook)
901
902 (defcustom gnus-summary-article-move-hook nil
903   "*A hook called after an article is moved, copied, respooled, or crossposted."
904   :group 'gnus-summary
905   :type 'hook)
906
907 (defcustom gnus-summary-article-delete-hook nil
908   "*A hook called after an article is deleted."
909   :group 'gnus-summary
910   :type 'hook)
911
912 (defcustom gnus-summary-article-expire-hook nil
913   "*A hook called after an article is expired."
914   :group 'gnus-summary
915   :type 'hook)
916
917 (defcustom gnus-summary-display-arrow
918   (and (fboundp 'display-graphic-p)
919        (display-graphic-p))
920   "*If non-nil, display an arrow highlighting the current article."
921   :version "21.1"
922   :group 'gnus-summary
923   :type 'boolean)
924
925 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
926   "Face used for highlighting the current article in the summary buffer."
927   :group 'gnus-summary-visual
928   :type 'face)
929
930 (defvar gnus-tmp-downloaded nil)
931
932 (defcustom gnus-summary-highlight
933   '(((eq mark gnus-canceled-mark)
934      . gnus-summary-cancelled-face)
935     ((and uncached (> score default-high))
936      . gnus-summary-high-undownloaded-face)
937     ((and uncached (< score default-low))
938      . gnus-summary-low-undownloaded-face)
939     (uncached
940      . gnus-summary-normal-undownloaded-face)
941     ((and (> score default-high)
942           (or (eq mark gnus-dormant-mark)
943               (eq mark gnus-ticked-mark)))
944      . gnus-summary-high-ticked-face)
945     ((and (< score default-low)
946           (or (eq mark gnus-dormant-mark)
947               (eq mark gnus-ticked-mark)))
948      . gnus-summary-low-ticked-face)
949     ((or (eq mark gnus-dormant-mark)
950          (eq mark gnus-ticked-mark))
951      . gnus-summary-normal-ticked-face)
952     ((and (> score default-high) (eq mark gnus-ancient-mark))
953      . gnus-summary-high-ancient-face)
954     ((and (< score default-low) (eq mark gnus-ancient-mark))
955      . gnus-summary-low-ancient-face)
956     ((eq mark gnus-ancient-mark)
957      . gnus-summary-normal-ancient-face)
958     ((and (> score default-high) (eq mark gnus-unread-mark))
959      . gnus-summary-high-unread-face)
960     ((and (< score default-low) (eq mark gnus-unread-mark))
961      . gnus-summary-low-unread-face)
962     ((eq mark gnus-unread-mark)
963      . gnus-summary-normal-unread-face)
964     ((> score default-high)
965      . gnus-summary-high-read-face)
966     ((< score default-low)
967      . gnus-summary-low-read-face)
968     (t
969      . gnus-summary-normal-read-face))
970   "*Controls the highlighting of summary buffer lines.
971
972 A list of (FORM . FACE) pairs.  When deciding how a a particular
973 summary line should be displayed, each form is evaluated.  The content
974 of the face field after the first true form is used.  You can change
975 how those summary lines are displayed, by editing the face field.
976
977 You can use the following variables in the FORM field.
978
979 score:        The article's score
980 default:      The default article score.
981 default-high: The default score for high scored articles.
982 default-low:  The default score for low scored articles.
983 below:        The score below which articles are automatically marked as read.
984 mark:         The article's mark.
985 uncached:     Non-nil if the article is uncached."
986   :group 'gnus-summary-visual
987   :type '(repeat (cons (sexp :tag "Form" nil)
988                        face)))
989
990 (defcustom gnus-alter-header-function nil
991   "Function called to allow alteration of article header structures.
992 The function is called with one parameter, the article header vector,
993 which it may alter in any way."
994   :type '(choice (const :tag "None" nil)
995                  function)
996   :group 'gnus-summary)
997
998 (defvar gnus-decode-encoded-word-function
999   (mime-find-field-decoder 'From 'nov)
1000   "Variable that says which function should be used to decode a string with encoded words.")
1001
1002 (defcustom gnus-extra-headers '(To Newsgroups)
1003   "*Extra headers to parse."
1004   :version "21.1"
1005   :group 'gnus-summary
1006   :type '(repeat symbol))
1007
1008 (defcustom gnus-ignored-from-addresses
1009   (and user-mail-address (regexp-quote user-mail-address))
1010   "*Regexp of From headers that may be suppressed in favor of To headers."
1011   :version "21.1"
1012   :group 'gnus-summary
1013   :type 'regexp)
1014
1015 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1016   "List of charsets that should be ignored.
1017 When these charsets are used in the \"charset\" parameter, the
1018 default charset will be used instead."
1019   :version "21.1"
1020   :type '(repeat symbol)
1021   :group 'gnus-charset)
1022
1023 (gnus-define-group-parameter
1024  ignored-charsets
1025  :type list
1026  :function-document
1027  "Return the ignored charsets of GROUP."
1028  :variable gnus-group-ignored-charsets-alist
1029  :variable-default
1030  '(("alt\\.chinese\\.text" iso-8859-1))
1031  :variable-document
1032  "Alist of regexps (to match group names) and charsets that should be ignored.
1033 When these charsets are used in the \"charset\" parameter, the
1034 default charset will be used instead."
1035  :variable-group gnus-charset
1036  :variable-type '(repeat (cons (regexp :tag "Group")
1037                                (repeat symbol)))
1038  :parameter-type '(choice :tag "Ignored charsets"
1039                           :value nil
1040                           (repeat (symbol)))
1041  :parameter-document       "\
1042 List of charsets that should be ignored.
1043
1044 When these charsets are used in the \"charset\" parameter, the
1045 default charset will be used instead.")
1046
1047 (defcustom gnus-group-highlight-words-alist nil
1048   "Alist of group regexps and highlight regexps.
1049 This variable uses the same syntax as `gnus-emphasis-alist'."
1050   :version "21.1"
1051   :type '(repeat (cons (regexp :tag "Group")
1052                        (repeat (list (regexp :tag "Highlight regexp")
1053                                      (number :tag "Group for entire word" 0)
1054                                      (number :tag "Group for displayed part" 0)
1055                                      (symbol :tag "Face"
1056                                              gnus-emphasis-highlight-words)))))
1057   :group 'gnus-summary-visual)
1058
1059 (defcustom gnus-use-wheel nil
1060   "Use Intelli-mouse on summary movement"
1061   :type 'boolean
1062   :group 'gnus-summary-maneuvering)
1063
1064 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1065   "Amount to scroll messages by spinning the mouse wheel.
1066 This is actually a cons cell, where the first item is the amount to scroll
1067 on a normal wheel event, and the second is the amount to scroll when the
1068 wheel is moved with the shift key depressed."
1069   :type '(cons (integer :tag "Shift") integer)
1070   :group 'gnus-summary-maneuvering)
1071
1072 (defcustom gnus-wheel-edge-resistance 2
1073   "How hard it should be to change the current article
1074 by moving the mouse over the edge of the article window."
1075   :type 'integer
1076   :group 'gnus-summary-maneuvering)
1077
1078 (defcustom gnus-summary-show-article-charset-alist
1079   nil
1080   "Alist of number and charset.
1081 The article will be shown with the charset corresponding to the
1082 numbered argument.
1083 For example: ((1 . cn-gb-2312) (2 . big5))."
1084   :version "21.1"
1085   :type '(repeat (cons (number :tag "Argument" 1)
1086                        (symbol :tag "Charset")))
1087   :group 'gnus-charset)
1088
1089 (defcustom gnus-preserve-marks t
1090   "Whether marks are preserved when moving, copying and respooling messages."
1091   :version "21.1"
1092   :type 'boolean
1093   :group 'gnus-summary-marks)
1094
1095 (defcustom gnus-alter-articles-to-read-function nil
1096   "Function to be called to alter the list of articles to be selected."
1097   :type '(choice (const nil) function)
1098   :group 'gnus-summary)
1099
1100 (defcustom gnus-orphan-score nil
1101   "*All orphans get this score added.  Set in the score file."
1102   :group 'gnus-score-default
1103   :type '(choice (const nil)
1104                  integer))
1105
1106 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1107   "*A regexp to match MIME parts when saving multiple parts of a
1108 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1109 This regexp will be used by default when prompting the user for which
1110 type of files to save."
1111   :group 'gnus-summary
1112   :type 'regexp)
1113
1114 (defcustom gnus-read-all-available-headers nil
1115   "Whether Gnus should parse all headers made available to it.
1116 This is mostly relevant for slow back ends where the user may
1117 wish to widen the summary buffer to include all headers
1118 that were fetched.  Say, for nnultimate groups."
1119   :group 'gnus-summary
1120   :type '(choice boolean regexp))
1121
1122 (defcustom gnus-summary-muttprint-program "muttprint"
1123   "Command (and optional arguments) used to run Muttprint."
1124   :version "21.3"
1125   :group 'gnus-summary
1126   :type 'string)
1127
1128 (defcustom gnus-article-loose-mime nil
1129   "If non-nil, don't require MIME-Version header.
1130 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1131 supply the MIME-Version header or deliberately strip it From the mail.
1132 Set it to non-nil, Gnus will treat some articles as MIME even if
1133 the MIME-Version header is missed."
1134   :version "21.3"
1135   :type 'boolean
1136   :group 'gnus-article-mime)
1137
1138 (defcustom gnus-article-emulate-mime t
1139   "If non-nil, use MIME emulation for uuencode and the like.
1140 This means that Gnus will search message bodies for text that look
1141 like uuencoded bits, yEncoded bits, and so on, and present that using
1142 the normal Gnus MIME machinery."
1143   :type 'boolean
1144   :group 'gnus-article-mime)
1145
1146 ;;; Internal variables
1147
1148 (defvar gnus-summary-display-cache nil)
1149 (defvar gnus-article-mime-handles nil)
1150 (defvar gnus-article-decoded-p nil)
1151 (defvar gnus-article-charset nil)
1152 (defvar gnus-article-ignored-charsets nil)
1153 (defvar gnus-scores-exclude-files nil)
1154 (defvar gnus-page-broken nil)
1155
1156 (defvar gnus-original-article nil)
1157 (defvar gnus-article-internal-prepare-hook nil)
1158 (defvar gnus-newsgroup-process-stack nil)
1159
1160 (defvar gnus-thread-indent-array nil)
1161 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1162 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1163   "Function called to sort the articles within a thread after it has been gathered together.")
1164
1165 (defvar gnus-summary-save-parts-type-history nil)
1166 (defvar gnus-summary-save-parts-last-directory nil)
1167
1168 ;; Avoid highlighting in kill files.
1169 (defvar gnus-summary-inhibit-highlight nil)
1170 (defvar gnus-newsgroup-selected-overlay nil)
1171 (defvar gnus-inhibit-limiting nil)
1172 (defvar gnus-newsgroup-adaptive-score-file nil)
1173 (defvar gnus-current-score-file nil)
1174 (defvar gnus-current-move-group nil)
1175 (defvar gnus-current-copy-group nil)
1176 (defvar gnus-current-crosspost-group nil)
1177 (defvar gnus-newsgroup-display nil)
1178
1179 (defvar gnus-newsgroup-dependencies nil)
1180 (defvar gnus-newsgroup-adaptive nil)
1181 (defvar gnus-summary-display-article-function nil)
1182 (defvar gnus-summary-highlight-line-function nil
1183   "Function called after highlighting a summary line.")
1184
1185 (defvar gnus-summary-line-format-alist
1186   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1187     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1188     (?s gnus-tmp-subject-or-nil ?s)
1189     (?n gnus-tmp-name ?s)
1190     (?A (std11-address-string
1191          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1192     (?a (or (std11-full-name-string
1193              (car (mime-entity-read-field gnus-tmp-header 'From)))
1194             gnus-tmp-from) ?s)
1195     (?F gnus-tmp-from ?s)
1196     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1197     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1198     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1199     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1200     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1201     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1202     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1203     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1204     (?L gnus-tmp-lines ?s)
1205     (?O gnus-tmp-downloaded ?c)
1206     (?I gnus-tmp-indentation ?s)
1207     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1208     (?R gnus-tmp-replied ?c)
1209     (?\[ gnus-tmp-opening-bracket ?c)
1210     (?\] gnus-tmp-closing-bracket ?c)
1211     (?\> (make-string gnus-tmp-level ? ) ?s)
1212     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1213     (?i gnus-tmp-score ?d)
1214     (?z gnus-tmp-score-char ?c)
1215     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1216     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1217     (?U gnus-tmp-unread ?c)
1218     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1219         ?s)
1220     (?t (gnus-summary-number-of-articles-in-thread
1221          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1222         ?d)
1223     (?e (gnus-summary-number-of-articles-in-thread
1224          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1225         ?c)
1226     (?u gnus-tmp-user-defined ?s)
1227     (?P (gnus-pick-line-number) ?d)
1228     (?B gnus-tmp-thread-tree-header-string ?s)
1229     (user-date (gnus-user-date
1230                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1231   "An alist of format specifications that can appear in summary lines.
1232 These are paired with what variables they correspond with, along with
1233 the type of the variable (string, integer, character, etc).")
1234
1235 (defvar gnus-summary-dummy-line-format-alist
1236   `((?S gnus-tmp-subject ?s)
1237     (?N gnus-tmp-number ?d)
1238     (?u gnus-tmp-user-defined ?s)))
1239
1240 (defvar gnus-summary-mode-line-format-alist
1241   `((?G gnus-tmp-group-name ?s)
1242     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1243     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1244     (?A gnus-tmp-article-number ?d)
1245     (?Z gnus-tmp-unread-and-unselected ?s)
1246     (?V gnus-version ?s)
1247     (?U gnus-tmp-unread-and-unticked ?d)
1248     (?S gnus-tmp-subject ?s)
1249     (?e gnus-tmp-unselected ?d)
1250     (?u gnus-tmp-user-defined ?s)
1251     (?d (length gnus-newsgroup-dormant) ?d)
1252     (?t (length gnus-newsgroup-marked) ?d)
1253     (?h (length gnus-newsgroup-spam-marked) ?d)
1254     (?r (length gnus-newsgroup-reads) ?d)
1255     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1256     (?E gnus-newsgroup-expunged-tally ?d)
1257     (?s (gnus-current-score-file-nondirectory) ?s)))
1258
1259 (defvar gnus-last-search-regexp nil
1260   "Default regexp for article search command.")
1261
1262 (defvar gnus-summary-search-article-matched-data nil
1263   "Last matched data of article search command.  It is the local variable
1264 in `gnus-article-buffer' which consists of the list of start position,
1265 end position and text.")
1266
1267 (defvar gnus-last-shell-command nil
1268   "Default shell command on article.")
1269
1270 (defvar gnus-newsgroup-agentized nil
1271   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1272 (defvar gnus-newsgroup-begin nil)
1273 (defvar gnus-newsgroup-end nil)
1274 (defvar gnus-newsgroup-last-rmail nil)
1275 (defvar gnus-newsgroup-last-mail nil)
1276 (defvar gnus-newsgroup-last-folder nil)
1277 (defvar gnus-newsgroup-last-file nil)
1278 (defvar gnus-newsgroup-auto-expire nil)
1279 (defvar gnus-newsgroup-active nil)
1280
1281 (defvar gnus-newsgroup-data nil)
1282 (defvar gnus-newsgroup-data-reverse nil)
1283 (defvar gnus-newsgroup-limit nil)
1284 (defvar gnus-newsgroup-limits nil)
1285 (defvar gnus-summary-use-undownloaded-faces nil)
1286
1287 (defvar gnus-newsgroup-unreads nil
1288   "Sorted list of unread articles in the current newsgroup.")
1289
1290 (defvar gnus-newsgroup-unselected nil
1291   "Sorted list of unselected unread articles in the current newsgroup.")
1292
1293 (defvar gnus-newsgroup-reads nil
1294   "Alist of read articles and article marks in the current newsgroup.")
1295
1296 (defvar gnus-newsgroup-expunged-tally nil)
1297
1298 (defvar gnus-newsgroup-marked nil
1299   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1300
1301 (defvar gnus-newsgroup-spam-marked nil
1302   "List of ranges of articles that have been marked as spam.")
1303
1304 (defvar gnus-newsgroup-killed nil
1305   "List of ranges of articles that have been through the scoring process.")
1306
1307 (defvar gnus-newsgroup-cached nil
1308   "Sorted list of articles that come from the article cache.")
1309
1310 (defvar gnus-newsgroup-saved nil
1311   "List of articles that have been saved.")
1312
1313 (defvar gnus-newsgroup-kill-headers nil)
1314
1315 (defvar gnus-newsgroup-replied nil
1316   "List of articles that have been replied to in the current newsgroup.")
1317
1318 (defvar gnus-newsgroup-forwarded nil
1319   "List of articles that have been forwarded in the current newsgroup.")
1320
1321 (defvar gnus-newsgroup-recent nil
1322   "List of articles that have are recent in the current newsgroup.")
1323
1324 (defvar gnus-newsgroup-expirable nil
1325   "Sorted list of articles in the current newsgroup that can be expired.")
1326
1327 (defvar gnus-newsgroup-processable nil
1328   "List of articles in the current newsgroup that can be processed.")
1329
1330 (defvar gnus-newsgroup-downloadable nil
1331   "Sorted list of articles in the current newsgroup that can be processed.")
1332
1333 (defvar gnus-newsgroup-unfetched nil
1334   "Sorted list of articles in the current newsgroup whose headers have
1335 not been fetched into the agent.
1336
1337 This list will always be a subset of gnus-newsgroup-undownloaded.")
1338
1339 (defvar gnus-newsgroup-undownloaded nil
1340   "List of articles in the current newsgroup that haven't been downloaded.")
1341
1342 (defvar gnus-newsgroup-unsendable nil
1343   "List of articles in the current newsgroup that won't be sent.")
1344
1345 (defvar gnus-newsgroup-bookmarks nil
1346   "List of articles in the current newsgroup that have bookmarks.")
1347
1348 (defvar gnus-newsgroup-dormant nil
1349   "Sorted list of dormant articles in the current newsgroup.")
1350
1351 (defvar gnus-newsgroup-unseen nil
1352   "List of unseen articles in the current newsgroup.")
1353
1354 (defvar gnus-newsgroup-seen nil
1355   "Range of seen articles in the current newsgroup.")
1356
1357 (defvar gnus-newsgroup-articles nil
1358   "List of articles in the current newsgroup.")
1359
1360 (defvar gnus-newsgroup-scored nil
1361   "List of scored articles in the current newsgroup.")
1362
1363 (defvar gnus-newsgroup-incorporated nil
1364   "List of incorporated articles in the current newsgroup.")
1365
1366 (defvar gnus-newsgroup-headers nil
1367   "List of article headers in the current newsgroup.")
1368
1369 (defvar gnus-newsgroup-threads nil)
1370
1371 (defvar gnus-newsgroup-prepared nil
1372   "Whether the current group has been prepared properly.")
1373
1374 (defvar gnus-newsgroup-ancient nil
1375   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1376
1377 (defvar gnus-newsgroup-sparse nil)
1378
1379 (defvar gnus-current-article nil)
1380 (defvar gnus-article-current nil)
1381 (defvar gnus-current-headers nil)
1382 (defvar gnus-have-all-headers nil)
1383 (defvar gnus-last-article nil)
1384 (defvar gnus-newsgroup-history nil)
1385 (defvar gnus-newsgroup-charset nil)
1386 (defvar gnus-newsgroup-ephemeral-charset nil)
1387 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1388
1389 (defvar gnus-article-before-search nil)
1390
1391 (defvar gnus-summary-local-variables
1392   '(gnus-newsgroup-name
1393     gnus-newsgroup-begin gnus-newsgroup-end
1394     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1395     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1396     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1397     gnus-newsgroup-unselected gnus-newsgroup-marked
1398     gnus-newsgroup-spam-marked
1399     gnus-newsgroup-reads gnus-newsgroup-saved
1400     gnus-newsgroup-replied gnus-newsgroup-forwarded
1401     gnus-newsgroup-recent
1402     gnus-newsgroup-expirable
1403     gnus-newsgroup-processable gnus-newsgroup-killed
1404     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1405     gnus-newsgroup-unfetched
1406     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1407     gnus-newsgroup-seen gnus-newsgroup-articles
1408     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1409     gnus-newsgroup-headers gnus-newsgroup-threads
1410     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1411     gnus-current-article gnus-current-headers gnus-have-all-headers
1412     gnus-last-article gnus-article-internal-prepare-hook
1413     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1414     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1415     gnus-thread-expunge-below
1416     gnus-score-alist gnus-current-score-file
1417     (gnus-summary-expunge-below . global)
1418     (gnus-summary-mark-below . global)
1419     (gnus-orphan-score . global)
1420     gnus-newsgroup-active gnus-scores-exclude-files
1421     gnus-newsgroup-history gnus-newsgroup-ancient
1422     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1423     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1424     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1425     (gnus-newsgroup-expunged-tally . 0)
1426     gnus-cache-removable-articles gnus-newsgroup-cached
1427     gnus-newsgroup-data gnus-newsgroup-data-reverse
1428     gnus-newsgroup-limit gnus-newsgroup-limits
1429     gnus-newsgroup-charset gnus-newsgroup-display
1430     gnus-summary-use-undownloaded-faces
1431     gnus-newsgroup-incorporated)
1432   "Variables that are buffer-local to the summary buffers.")
1433
1434 (defvar gnus-newsgroup-variables nil
1435   "A list of variables that have separate values in different newsgroups.
1436 A list of newsgroup (summary buffer) local variables, or cons of
1437 variables and their default values (when the default values are not
1438 nil), that should be made global while the summary buffer is active.
1439 These variables can be used to set variables in the group parameters
1440 while still allowing them to affect operations done in other
1441 buffers. For example:
1442
1443 \(setq gnus-newsgroup-variables
1444      '(message-use-followup-to
1445        (gnus-visible-headers .
1446          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1447 ")
1448
1449 ;; Byte-compiler warning.
1450 (eval-when-compile
1451   ;; Bind features so that require will believe that gnus-sum has
1452   ;; already been loaded (avoids infinite recursion)
1453   (let ((features (cons 'gnus-sum features)))
1454     ;; Several of the declarations in gnus-sum are needed to load the
1455     ;; following files. Right now, these definitions have been
1456     ;; compiled but not defined (evaluated).  We could either do a
1457     ;; eval-and-compile about all of the declarations or evaluate the
1458     ;; source file.
1459     (if (boundp 'gnus-newsgroup-variables)
1460         nil
1461       (load "gnus-sum.el" t t t))
1462     (require 'gnus)
1463     (require 'gnus-agent)
1464     (require 'gnus-art)))
1465
1466 ;; Subject simplification.
1467
1468 (defun gnus-simplify-whitespace (str)
1469   "Remove excessive whitespace from STR."
1470   ;; Multiple spaces.
1471   (while (string-match "[ \t][ \t]+" str)
1472     (setq str (concat (substring str 0 (match-beginning 0))
1473                         " "
1474                         (substring str (match-end 0)))))
1475   ;; Leading spaces.
1476   (when (string-match "^[ \t]+" str)
1477     (setq str (substring str (match-end 0))))
1478   ;; Trailing spaces.
1479   (when (string-match "[ \t]+$" str)
1480     (setq str (substring str 0 (match-beginning 0))))
1481   str)
1482
1483 (defun gnus-simplify-all-whitespace (str)
1484   "Remove all whitespace from STR."
1485   (while (string-match "[ \t\n]+" str)
1486     (setq str (replace-match "" nil nil str)))
1487   str)
1488
1489 (defsubst gnus-simplify-subject-re (subject)
1490   "Remove \"Re:\" from subject lines."
1491   (if (string-match message-subject-re-regexp subject)
1492       (substring subject (match-end 0))
1493     subject))
1494
1495 (defun gnus-simplify-subject (subject &optional re-only)
1496   "Remove `Re:' and words in parentheses.
1497 If RE-ONLY is non-nil, strip leading `Re:'s only."
1498   (let ((case-fold-search t))           ;Ignore case.
1499     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1500     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1501       (setq subject (substring subject (match-end 0))))
1502     ;; Remove uninteresting prefixes.
1503     (when (and (not re-only)
1504                gnus-simplify-ignored-prefixes
1505                (string-match gnus-simplify-ignored-prefixes subject))
1506       (setq subject (substring subject (match-end 0))))
1507     ;; Remove words in parentheses from end.
1508     (unless re-only
1509       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1510         (setq subject (substring subject 0 (match-beginning 0)))))
1511     ;; Return subject string.
1512     subject))
1513
1514 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1515 ;; all whitespace.
1516 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1517   (goto-char (point-min))
1518   (while (re-search-forward regexp nil t)
1519     (replace-match (or newtext ""))))
1520
1521 (defun gnus-simplify-buffer-fuzzy ()
1522   "Simplify string in the buffer fuzzily.
1523 The string in the accessible portion of the current buffer is simplified.
1524 It is assumed to be a single-line subject.
1525 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1526 matter is removed.  Additional things can be deleted by setting
1527 `gnus-simplify-subject-fuzzy-regexp'."
1528   (let ((case-fold-search t)
1529         (modified-tick))
1530     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1531
1532     (while (not (eq modified-tick (buffer-modified-tick)))
1533       (setq modified-tick (buffer-modified-tick))
1534       (cond
1535        ((listp gnus-simplify-subject-fuzzy-regexp)
1536         (mapcar 'gnus-simplify-buffer-fuzzy-step
1537                 gnus-simplify-subject-fuzzy-regexp))
1538        (gnus-simplify-subject-fuzzy-regexp
1539         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1540       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1541       (gnus-simplify-buffer-fuzzy-step
1542        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1543       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1544
1545     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1546     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1547     (gnus-simplify-buffer-fuzzy-step " $")
1548     (gnus-simplify-buffer-fuzzy-step "^ +")))
1549
1550 (defun gnus-simplify-subject-fuzzy (subject)
1551   "Simplify a subject string fuzzily.
1552 See `gnus-simplify-buffer-fuzzy' for details."
1553   (save-excursion
1554     (gnus-set-work-buffer)
1555     (let ((case-fold-search t))
1556       ;; Remove uninteresting prefixes.
1557       (when (and gnus-simplify-ignored-prefixes
1558                  (string-match gnus-simplify-ignored-prefixes subject))
1559         (setq subject (substring subject (match-end 0))))
1560       (insert subject)
1561       (inline (gnus-simplify-buffer-fuzzy))
1562       (buffer-string))))
1563
1564 (defsubst gnus-simplify-subject-fully (subject)
1565   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1566   (cond
1567    (gnus-simplify-subject-functions
1568     (gnus-map-function gnus-simplify-subject-functions subject))
1569    ((null gnus-summary-gather-subject-limit)
1570     (gnus-simplify-subject-re subject))
1571    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1572     (gnus-simplify-subject-fuzzy subject))
1573    ((numberp gnus-summary-gather-subject-limit)
1574     (gnus-limit-string (gnus-simplify-subject-re subject)
1575                        gnus-summary-gather-subject-limit))
1576    (t
1577     subject)))
1578
1579 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1580   "Check whether two subjects are equal.
1581 If optional argument SIMPLE-FIRST is t, first argument is already
1582 simplified."
1583   (cond
1584    ((null simple-first)
1585     (equal (gnus-simplify-subject-fully s1)
1586            (gnus-simplify-subject-fully s2)))
1587    (t
1588     (equal s1
1589            (gnus-simplify-subject-fully s2)))))
1590
1591 (defun gnus-summary-bubble-group ()
1592   "Increase the score of the current group.
1593 This is a handy function to add to `gnus-summary-exit-hook' to
1594 increase the score of each group you read."
1595   (gnus-group-add-score gnus-newsgroup-name))
1596
1597 \f
1598 ;;;
1599 ;;; Gnus summary mode
1600 ;;;
1601
1602 (put 'gnus-summary-mode 'mode-class 'special)
1603
1604 (defvar gnus-article-commands-menu)
1605
1606 ;; Non-orthogonal keys
1607
1608 (gnus-define-keys gnus-summary-mode-map
1609   " " gnus-summary-next-page
1610   "\177" gnus-summary-prev-page
1611   [delete] gnus-summary-prev-page
1612   [backspace] gnus-summary-prev-page
1613   "\r" gnus-summary-scroll-up
1614   "\M-\r" gnus-summary-scroll-down
1615   "n" gnus-summary-next-unread-article
1616   "p" gnus-summary-prev-unread-article
1617   "N" gnus-summary-next-article
1618   "P" gnus-summary-prev-article
1619   "\M-\C-n" gnus-summary-next-same-subject
1620   "\M-\C-p" gnus-summary-prev-same-subject
1621   "\M-n" gnus-summary-next-unread-subject
1622   "\M-p" gnus-summary-prev-unread-subject
1623   "." gnus-summary-first-unread-article
1624   "," gnus-summary-best-unread-article
1625   "\M-s" gnus-summary-search-article-forward
1626   "\M-r" gnus-summary-search-article-backward
1627   "<" gnus-summary-beginning-of-article
1628   ">" gnus-summary-end-of-article
1629   "j" gnus-summary-goto-article
1630   "^" gnus-summary-refer-parent-article
1631   "\M-^" gnus-summary-refer-article
1632   "u" gnus-summary-tick-article-forward
1633   "!" gnus-summary-tick-article-forward
1634   "U" gnus-summary-tick-article-backward
1635   "d" gnus-summary-mark-as-read-forward
1636   "D" gnus-summary-mark-as-read-backward
1637   "E" gnus-summary-mark-as-expirable
1638   "\M-u" gnus-summary-clear-mark-forward
1639   "\M-U" gnus-summary-clear-mark-backward
1640   "k" gnus-summary-kill-same-subject-and-select
1641   "\C-k" gnus-summary-kill-same-subject
1642   "\M-\C-k" gnus-summary-kill-thread
1643   "\M-\C-l" gnus-summary-lower-thread
1644   "e" gnus-summary-edit-article
1645   "#" gnus-summary-mark-as-processable
1646   "\M-#" gnus-summary-unmark-as-processable
1647   "\M-\C-t" gnus-summary-toggle-threads
1648   "\M-\C-s" gnus-summary-show-thread
1649   "\M-\C-h" gnus-summary-hide-thread
1650   "\M-\C-f" gnus-summary-next-thread
1651   "\M-\C-b" gnus-summary-prev-thread
1652   [(meta down)] gnus-summary-next-thread
1653   [(meta up)] gnus-summary-prev-thread
1654   "\M-\C-u" gnus-summary-up-thread
1655   "\M-\C-d" gnus-summary-down-thread
1656   "&" gnus-summary-execute-command
1657   "c" gnus-summary-catchup-and-exit
1658   "\C-w" gnus-summary-mark-region-as-read
1659   "\C-t" gnus-summary-toggle-truncation
1660   "?" gnus-summary-mark-as-dormant
1661   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1662   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1663   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1664   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1665   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1666   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1667   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1668   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1669   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1670   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1671   "=" gnus-summary-expand-window
1672   "\C-x\C-s" gnus-summary-reselect-current-group
1673   "\M-g" gnus-summary-rescan-group
1674   "w" gnus-summary-stop-page-breaking
1675   "\C-c\C-r" gnus-summary-caesar-message
1676   "\M-t" gnus-summary-toggle-mime
1677   "f" gnus-summary-followup
1678   "F" gnus-summary-followup-with-original
1679   "C" gnus-summary-cancel-article
1680   "r" gnus-summary-reply
1681   "R" gnus-summary-reply-with-original
1682   "\C-c\C-f" gnus-summary-mail-forward
1683   "o" gnus-summary-save-article
1684   "\C-o" gnus-summary-save-article-mail
1685   "|" gnus-summary-pipe-output
1686   "\M-k" gnus-summary-edit-local-kill
1687   "\M-K" gnus-summary-edit-global-kill
1688   ;; "V" gnus-version
1689   "\C-c\C-d" gnus-summary-describe-group
1690   "q" gnus-summary-exit
1691   "Q" gnus-summary-exit-no-update
1692   "\C-c\C-i" gnus-info-find-node
1693   gnus-mouse-2 gnus-mouse-pick-article
1694   "m" gnus-summary-mail-other-window
1695   "a" gnus-summary-post-news
1696   "i" gnus-summary-news-other-window
1697   "x" gnus-summary-limit-to-unread
1698   "s" gnus-summary-isearch-article
1699   "t" gnus-summary-toggle-header
1700   "g" gnus-summary-show-article
1701   "l" gnus-summary-goto-last-article
1702   "v" gnus-summary-preview-mime-message
1703   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1704   "\C-d" gnus-summary-enter-digest-group
1705   "\M-\C-d" gnus-summary-read-document
1706   "\M-\C-e" gnus-summary-edit-parameters
1707   "\M-\C-a" gnus-summary-customize-parameters
1708   "\C-c\C-b" gnus-bug
1709   "\C-c\C-n" gnus-namazu-search
1710   "*" gnus-cache-enter-article
1711   "\M-*" gnus-cache-remove-article
1712   "\M-&" gnus-summary-universal-argument
1713   "\C-l" gnus-recenter
1714   "I" gnus-summary-increase-score
1715   "L" gnus-summary-lower-score
1716   "\M-i" gnus-symbolic-argument
1717   "h" gnus-summary-select-article-buffer
1718
1719   "V" gnus-summary-score-map
1720   "X" gnus-uu-extract-map
1721   "S" gnus-summary-send-map)
1722
1723   ;; Sort of orthogonal keymap
1724 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1725   "t" gnus-summary-tick-article-forward
1726   "!" gnus-summary-tick-article-forward
1727   "d" gnus-summary-mark-as-read-forward
1728   "r" gnus-summary-mark-as-read-forward
1729   "c" gnus-summary-clear-mark-forward
1730   " " gnus-summary-clear-mark-forward
1731   "e" gnus-summary-mark-as-expirable
1732   "x" gnus-summary-mark-as-expirable
1733   "?" gnus-summary-mark-as-dormant
1734   "b" gnus-summary-set-bookmark
1735   "B" gnus-summary-remove-bookmark
1736   "#" gnus-summary-mark-as-processable
1737   "\M-#" gnus-summary-unmark-as-processable
1738   "S" gnus-summary-limit-include-expunged
1739   "C" gnus-summary-catchup
1740   "H" gnus-summary-catchup-to-here
1741   "h" gnus-summary-catchup-from-here
1742   "\C-c" gnus-summary-catchup-all
1743   "k" gnus-summary-kill-same-subject-and-select
1744   "K" gnus-summary-kill-same-subject
1745   "P" gnus-uu-mark-map)
1746
1747 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1748   "c" gnus-summary-clear-above
1749   "u" gnus-summary-tick-above
1750   "m" gnus-summary-mark-above
1751   "k" gnus-summary-kill-below)
1752
1753 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1754   "/" gnus-summary-limit-to-subject
1755   "n" gnus-summary-limit-to-articles
1756   "w" gnus-summary-pop-limit
1757   "s" gnus-summary-limit-to-subject
1758   "a" gnus-summary-limit-to-author
1759   "u" gnus-summary-limit-to-unread
1760   "m" gnus-summary-limit-to-marks
1761   "M" gnus-summary-limit-exclude-marks
1762   "v" gnus-summary-limit-to-score
1763   "*" gnus-summary-limit-include-cached
1764   "D" gnus-summary-limit-include-dormant
1765   "T" gnus-summary-limit-include-thread
1766   "d" gnus-summary-limit-exclude-dormant
1767   "t" gnus-summary-limit-to-age
1768   "." gnus-summary-limit-to-unseen
1769   "x" gnus-summary-limit-to-extra
1770   "p" gnus-summary-limit-to-display-predicate
1771   "E" gnus-summary-limit-include-expunged
1772   "c" gnus-summary-limit-exclude-childless-dormant
1773   "C" gnus-summary-limit-mark-excluded-as-read
1774   "o" gnus-summary-insert-old-articles
1775   "N" gnus-summary-insert-new-articles
1776   "r" gnus-summary-limit-to-replied)
1777
1778 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1779   "n" gnus-summary-next-unread-article
1780   "p" gnus-summary-prev-unread-article
1781   "N" gnus-summary-next-article
1782   "P" gnus-summary-prev-article
1783   "\C-n" gnus-summary-next-same-subject
1784   "\C-p" gnus-summary-prev-same-subject
1785   "\M-n" gnus-summary-next-unread-subject
1786   "\M-p" gnus-summary-prev-unread-subject
1787   "f" gnus-summary-first-unread-article
1788   "b" gnus-summary-best-unread-article
1789   "j" gnus-summary-goto-article
1790   "g" gnus-summary-goto-subject
1791   "l" gnus-summary-goto-last-article
1792   "o" gnus-summary-pop-article)
1793
1794 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1795   "k" gnus-summary-kill-thread
1796   "l" gnus-summary-lower-thread
1797   "i" gnus-summary-raise-thread
1798   "T" gnus-summary-toggle-threads
1799   "t" gnus-summary-rethread-current
1800   "^" gnus-summary-reparent-thread
1801   "s" gnus-summary-show-thread
1802   "S" gnus-summary-show-all-threads
1803   "h" gnus-summary-hide-thread
1804   "H" gnus-summary-hide-all-threads
1805   "n" gnus-summary-next-thread
1806   "p" gnus-summary-prev-thread
1807   "u" gnus-summary-up-thread
1808   "o" gnus-summary-top-thread
1809   "d" gnus-summary-down-thread
1810   "#" gnus-uu-mark-thread
1811   "\M-#" gnus-uu-unmark-thread)
1812
1813 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1814   "g" gnus-summary-prepare
1815   "c" gnus-summary-insert-cached-articles
1816   "d" gnus-summary-insert-dormant-articles)
1817
1818 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1819   "c" gnus-summary-catchup-and-exit
1820   "C" gnus-summary-catchup-all-and-exit
1821   "E" gnus-summary-exit-no-update
1822   "J" gnus-summary-jump-to-other-group
1823   "Q" gnus-summary-exit
1824   "Z" gnus-summary-exit
1825   "n" gnus-summary-catchup-and-goto-next-group
1826   "R" gnus-summary-reselect-current-group
1827   "G" gnus-summary-rescan-group
1828   "N" gnus-summary-next-group
1829   "s" gnus-summary-save-newsrc
1830   "P" gnus-summary-prev-group)
1831
1832 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1833   " " gnus-summary-next-page
1834   "n" gnus-summary-next-page
1835   "\177" gnus-summary-prev-page
1836   [delete] gnus-summary-prev-page
1837   "p" gnus-summary-prev-page
1838   "\r" gnus-summary-scroll-up
1839   "\M-\r" gnus-summary-scroll-down
1840   "<" gnus-summary-beginning-of-article
1841   ">" gnus-summary-end-of-article
1842   "b" gnus-summary-beginning-of-article
1843   "e" gnus-summary-end-of-article
1844   "^" gnus-summary-refer-parent-article
1845   "r" gnus-summary-refer-parent-article
1846   "D" gnus-summary-enter-digest-group
1847   "R" gnus-summary-refer-references
1848   "T" gnus-summary-refer-thread
1849   "g" gnus-summary-show-article
1850   "s" gnus-summary-isearch-article
1851   "P" gnus-summary-print-article
1852   "M" gnus-mailing-list-insinuate
1853   "t" gnus-article-babel)
1854
1855 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1856   "b" gnus-article-add-buttons
1857   "B" gnus-article-add-buttons-to-head
1858   "o" gnus-article-treat-overstrike
1859   "e" gnus-article-emphasize
1860   "w" gnus-article-fill-cited-article
1861   "Q" gnus-article-fill-long-lines
1862   "C" gnus-article-capitalize-sentences
1863   "c" gnus-article-remove-cr
1864   "Z" gnus-article-decode-HZ
1865   "A" gnus-article-treat-ansi-sequences
1866   "h" gnus-article-wash-html
1867   "u" gnus-article-unsplit-urls
1868   "f" gnus-article-display-x-face
1869   "l" gnus-summary-stop-page-breaking
1870   "r" gnus-summary-caesar-message
1871   "m" gnus-summary-morse-message
1872   "t" gnus-summary-toggle-header
1873   "g" gnus-treat-smiley
1874   "v" gnus-summary-verbose-headers
1875   "m" gnus-summary-toggle-mime
1876   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1877   "p" gnus-article-verify-x-pgp-sig
1878   "d" gnus-article-treat-dumbquotes)
1879
1880 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1881   ;; mnemonic: deuglif*Y*
1882   "u" gnus-article-outlook-unwrap-lines
1883   "a" gnus-article-outlook-repair-attribution
1884   "c" gnus-article-outlook-rearrange-citation
1885   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1886
1887 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1888   "a" gnus-article-hide
1889   "h" gnus-article-hide-headers
1890   "b" gnus-article-hide-boring-headers
1891   "s" gnus-article-hide-signature
1892   "c" gnus-article-hide-citation
1893   "C" gnus-article-hide-citation-in-followups
1894   "l" gnus-article-hide-list-identifiers
1895   "B" gnus-article-strip-banner
1896   "P" gnus-article-hide-pem
1897   "\C-c" gnus-article-hide-citation-maybe)
1898
1899 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1900   "a" gnus-article-highlight
1901   "h" gnus-article-highlight-headers
1902   "c" gnus-article-highlight-citation
1903   "s" gnus-article-highlight-signature)
1904
1905 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1906   "f" gnus-article-treat-fold-headers
1907   "u" gnus-article-treat-unfold-headers
1908   "n" gnus-article-treat-fold-newsgroups)
1909
1910 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1911   "x" gnus-article-display-x-face
1912   "d" gnus-article-display-face
1913   "s" gnus-treat-smiley
1914   "D" gnus-article-remove-images
1915   "f" gnus-treat-from-picon
1916   "m" gnus-treat-mail-picon
1917   "n" gnus-treat-newsgroups-picon)
1918
1919 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1920   "z" gnus-article-date-ut
1921   "u" gnus-article-date-ut
1922   "l" gnus-article-date-local
1923   "p" gnus-article-date-english
1924   "e" gnus-article-date-lapsed
1925   "o" gnus-article-date-original
1926   "i" gnus-article-date-iso8601
1927   "s" gnus-article-date-user)
1928
1929 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1930   "t" gnus-article-remove-trailing-blank-lines
1931   "l" gnus-article-strip-leading-blank-lines
1932   "m" gnus-article-strip-multiple-blank-lines
1933   "a" gnus-article-strip-blank-lines
1934   "A" gnus-article-strip-all-blank-lines
1935   "s" gnus-article-strip-leading-space
1936   "e" gnus-article-strip-trailing-space
1937   "w" gnus-article-remove-leading-whitespace)
1938
1939 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1940   "v" gnus-version
1941   "f" gnus-summary-fetch-faq
1942   "d" gnus-summary-describe-group
1943   "h" gnus-summary-describe-briefly
1944   "i" gnus-info-find-node
1945   "c" gnus-group-fetch-charter
1946   "C" gnus-group-fetch-control)
1947
1948 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1949   "e" gnus-summary-expire-articles
1950   "\M-\C-e" gnus-summary-expire-articles-now
1951   "\177" gnus-summary-delete-article
1952   [delete] gnus-summary-delete-article
1953   [backspace] gnus-summary-delete-article
1954   "m" gnus-summary-move-article
1955   "r" gnus-summary-respool-article
1956   "w" gnus-summary-edit-article
1957   "c" gnus-summary-copy-article
1958   "B" gnus-summary-crosspost-article
1959   "q" gnus-summary-respool-query
1960   "t" gnus-summary-respool-trace
1961   "i" gnus-summary-import-article
1962   "I" gnus-summary-create-article
1963   "p" gnus-summary-article-posted-p)
1964
1965 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1966   "o" gnus-summary-save-article
1967   "m" gnus-summary-save-article-mail
1968   "F" gnus-summary-write-article-file
1969   "r" gnus-summary-save-article-rmail
1970   "f" gnus-summary-save-article-file
1971   "b" gnus-summary-save-article-body-file
1972   "h" gnus-summary-save-article-folder
1973   "v" gnus-summary-save-article-vm
1974   "p" gnus-summary-pipe-output
1975   "P" gnus-summary-muttprint
1976   "s" gnus-soup-add-article)
1977
1978 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1979   "b" gnus-summary-display-buttonized
1980   "m" gnus-summary-repair-multipart
1981   "v" gnus-article-view-part
1982   "o" gnus-article-save-part
1983   "c" gnus-article-copy-part
1984   "C" gnus-article-view-part-as-charset
1985   "e" gnus-article-view-part-externally
1986   "E" gnus-article-encrypt-body
1987   "i" gnus-article-inline-part
1988   "|" gnus-article-pipe-part)
1989
1990 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1991   "p" gnus-summary-mark-as-processable
1992   "u" gnus-summary-unmark-as-processable
1993   "U" gnus-summary-unmark-all-processable
1994   "v" gnus-uu-mark-over
1995   "s" gnus-uu-mark-series
1996   "r" gnus-uu-mark-region
1997   "g" gnus-uu-unmark-region
1998   "R" gnus-uu-mark-by-regexp
1999   "G" gnus-uu-unmark-by-regexp
2000   "t" gnus-uu-mark-thread
2001   "T" gnus-uu-unmark-thread
2002   "a" gnus-uu-mark-all
2003   "b" gnus-uu-mark-buffer
2004   "S" gnus-uu-mark-sparse
2005   "k" gnus-summary-kill-process-mark
2006   "y" gnus-summary-yank-process-mark
2007   "w" gnus-summary-save-process-mark
2008   "i" gnus-uu-invert-processable)
2009
2010 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2011   ;;"x" gnus-uu-extract-any
2012   "m" gnus-summary-save-parts
2013   "u" gnus-uu-decode-uu
2014   "U" gnus-uu-decode-uu-and-save
2015   "s" gnus-uu-decode-unshar
2016   "S" gnus-uu-decode-unshar-and-save
2017   "o" gnus-uu-decode-save
2018   "O" gnus-uu-decode-save
2019   "b" gnus-uu-decode-binhex
2020   "B" gnus-uu-decode-binhex
2021   "p" gnus-uu-decode-postscript
2022   "P" gnus-uu-decode-postscript-and-save)
2023
2024 (gnus-define-keys
2025     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2026   "u" gnus-uu-decode-uu-view
2027   "U" gnus-uu-decode-uu-and-save-view
2028   "s" gnus-uu-decode-unshar-view
2029   "S" gnus-uu-decode-unshar-and-save-view
2030   "o" gnus-uu-decode-save-view
2031   "O" gnus-uu-decode-save-view
2032   "b" gnus-uu-decode-binhex-view
2033   "B" gnus-uu-decode-binhex-view
2034   "p" gnus-uu-decode-postscript-view
2035   "P" gnus-uu-decode-postscript-and-save-view)
2036
2037 (defvar gnus-article-post-menu nil)
2038
2039 (defconst gnus-summary-menu-maxlen 20)
2040
2041 (defun gnus-summary-menu-split (menu)
2042   ;; If we have lots of elements, divide them into groups of 20
2043   ;; and make a pane (or submenu) for each one.
2044   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2045       (let ((menu menu) sublists next
2046             (i 1))
2047         (while menu
2048           ;; Pull off the next gnus-summary-menu-maxlen elements
2049           ;; and make them the next element of sublist.
2050           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2051           (if next
2052               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2053                       nil))
2054           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2055                                              (aref (car (last menu)) 0)) menu)
2056                                sublists))
2057           (setq i (1+ i))
2058           (setq menu next))
2059         (nreverse sublists))
2060     ;; Few elements--put them all in one pane.
2061     menu))
2062
2063 (defun gnus-summary-make-menu-bar ()
2064   (gnus-turn-off-edit-menu 'summary)
2065
2066   (unless (boundp 'gnus-summary-misc-menu)
2067
2068     (easy-menu-define
2069      gnus-summary-kill-menu gnus-summary-mode-map ""
2070      (cons
2071       "Score"
2072       (nconc
2073        (list
2074         ["Customize" gnus-score-customize t])
2075        (gnus-make-score-map 'increase)
2076        (gnus-make-score-map 'lower)
2077        '(("Mark"
2078           ["Kill below" gnus-summary-kill-below t]
2079           ["Mark above" gnus-summary-mark-above t]
2080           ["Tick above" gnus-summary-tick-above t]
2081           ["Clear above" gnus-summary-clear-above t])
2082          ["Current score" gnus-summary-current-score t]
2083          ["Set score" gnus-summary-set-score t]
2084          ["Switch current score file..." gnus-score-change-score-file t]
2085          ["Set mark below..." gnus-score-set-mark-below t]
2086          ["Set expunge below..." gnus-score-set-expunge-below t]
2087          ["Edit current score file" gnus-score-edit-current-scores t]
2088          ["Edit score file" gnus-score-edit-file t]
2089          ["Trace score" gnus-score-find-trace t]
2090          ["Find words" gnus-score-find-favourite-words t]
2091          ["Rescore buffer" gnus-summary-rescore t]
2092          ["Increase score..." gnus-summary-increase-score t]
2093          ["Lower score..." gnus-summary-lower-score t]))))
2094
2095     ;; Define both the Article menu in the summary buffer and the
2096     ;; equivalent Commands menu in the article buffer here for
2097     ;; consistency.
2098     (let ((innards
2099            `(("Hide"
2100               ["All" gnus-article-hide t]
2101               ["Headers" gnus-article-hide-headers t]
2102               ["Signature" gnus-article-hide-signature t]
2103               ["Citation" gnus-article-hide-citation t]
2104               ["List identifiers" gnus-article-hide-list-identifiers t]
2105               ["Banner" gnus-article-strip-banner t]
2106               ["Boring headers" gnus-article-hide-boring-headers t])
2107              ("Highlight"
2108               ["All" gnus-article-highlight t]
2109               ["Headers" gnus-article-highlight-headers t]
2110               ["Signature" gnus-article-highlight-signature t]
2111               ["Citation" gnus-article-highlight-citation t])
2112              ("Date"
2113               ["Local" gnus-article-date-local t]
2114               ["ISO8601" gnus-article-date-iso8601 t]
2115               ["UT" gnus-article-date-ut t]
2116               ["Original" gnus-article-date-original t]
2117               ["Lapsed" gnus-article-date-lapsed t]
2118               ["User-defined" gnus-article-date-user t])
2119              ("Display"
2120               ["Remove images" gnus-article-remove-images t]
2121               ["Toggle smiley" gnus-treat-smiley t]
2122               ["Show X-Face" gnus-article-display-x-face t]
2123               ["Show picons in From" gnus-treat-from-picon t]
2124               ["Show picons in mail headers" gnus-treat-mail-picon t]
2125               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2126               ("View as different encoding"
2127                ,@(gnus-summary-menu-split
2128                   (mapcar
2129                    (lambda (cs)
2130                      ;; Since easymenu under Emacs doesn't allow
2131                      ;; lambda forms for menu commands, we should
2132                      ;; provide intern'ed function symbols.
2133                      (let ((command (intern (format "\
2134 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2135                        (fset command
2136                              `(lambda ()
2137                                 (interactive)
2138                                 (let ((gnus-summary-show-article-charset-alist
2139                                        '((1 . ,cs))))
2140                                   (gnus-summary-show-article 1))))
2141                        `[,(symbol-name cs) ,command t]))
2142                    (sort (if (fboundp 'coding-system-list)
2143                              (coding-system-list)
2144                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2145                            )
2146                          'string<)))))
2147              ("Washing"
2148               ("Remove Blanks"
2149                ["Leading" gnus-article-strip-leading-blank-lines t]
2150                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2151                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2152                ["All of the above" gnus-article-strip-blank-lines t]
2153                ["All" gnus-article-strip-all-blank-lines t]
2154                ["Leading space" gnus-article-strip-leading-space t]
2155                ["Trailing space" gnus-article-strip-trailing-space t]
2156                ["Leading space in headers"
2157                 gnus-article-remove-leading-whitespace t])
2158               ["Overstrike" gnus-article-treat-overstrike t]
2159               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2160               ["Emphasis" gnus-article-emphasize t]
2161               ["Word wrap" gnus-article-fill-cited-article t]
2162               ["Fill long lines" gnus-article-fill-long-lines t]
2163               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2164               ["Remove CR" gnus-article-remove-cr t]
2165               ["Rot 13" gnus-summary-caesar-message
2166                ,@(if (featurep 'xemacs) '(t)
2167                    '(:help "\"Caesar rotate\" article by 13"))]
2168               ["Morse decode" gnus-summary-morse-message t]
2169               ["Unix pipe..." gnus-summary-pipe-message t]
2170               ["Add buttons" gnus-article-add-buttons t]
2171               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2172               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2173               ["Toggle MIME" gnus-summary-toggle-mime t]
2174               ["Verbose header" gnus-summary-verbose-headers t]
2175               ["Toggle header" gnus-summary-toggle-header t]
2176               ["Unfold headers" gnus-article-treat-unfold-headers t]
2177               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2178               ["Html" gnus-article-wash-html t]
2179               ["Unsplit URLs" gnus-article-unsplit-urls t]
2180               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2181               ["Decode HZ" gnus-article-decode-HZ t]
2182               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2183               ("(Outlook) Deuglify"
2184                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2185                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2186                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2187                ["Full (Outlook) deuglify"
2188                 gnus-article-outlook-deuglify-article t])
2189               )
2190              ("Output"
2191               ["Save in default format..." gnus-summary-save-article
2192                ,@(if (featurep 'xemacs) '(t)
2193                    '(:help "Save article using default method"))]
2194               ["Save in file..." gnus-summary-save-article-file
2195                ,@(if (featurep 'xemacs) '(t)
2196                    '(:help "Save article in file"))]
2197               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2198               ["Save in MH folder..." gnus-summary-save-article-folder t]
2199               ["Save in VM folder..." gnus-summary-save-article-vm t]
2200               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2201               ["Save body in file..." gnus-summary-save-article-body-file t]
2202               ["Pipe through a filter..." gnus-summary-pipe-output t]
2203               ["Add to SOUP packet" gnus-soup-add-article t]
2204               ["Print with Muttprint..." gnus-summary-muttprint t]
2205               ["Print" gnus-summary-print-article t])
2206              ("Backend"
2207               ["Respool article..." gnus-summary-respool-article t]
2208               ["Move article..." gnus-summary-move-article
2209                (gnus-check-backend-function
2210                 'request-move-article gnus-newsgroup-name)]
2211               ["Copy article..." gnus-summary-copy-article t]
2212               ["Crosspost article..." gnus-summary-crosspost-article
2213                (gnus-check-backend-function
2214                 'request-replace-article gnus-newsgroup-name)]
2215               ["Import file..." gnus-summary-import-article
2216                (gnus-check-backend-function
2217                 'request-accept-article gnus-newsgroup-name)]
2218               ["Create article..." gnus-summary-create-article
2219                (gnus-check-backend-function
2220                 'request-accept-article gnus-newsgroup-name)]
2221               ["Check if posted" gnus-summary-article-posted-p t]
2222               ["Edit article" gnus-summary-edit-article
2223                (not (gnus-group-read-only-p))]
2224               ["Delete article" gnus-summary-delete-article
2225                (gnus-check-backend-function
2226                 'request-expire-articles gnus-newsgroup-name)]
2227               ["Query respool" gnus-summary-respool-query t]
2228               ["Trace respool" gnus-summary-respool-trace t]
2229               ["Delete expirable articles" gnus-summary-expire-articles-now
2230                (gnus-check-backend-function
2231                 'request-expire-articles gnus-newsgroup-name)])
2232              ("Extract"
2233               ["Uudecode" gnus-uu-decode-uu
2234                ,@(if (featurep 'xemacs) '(t)
2235                    '(:help "Decode uuencoded article(s)"))]
2236               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2237               ["Unshar" gnus-uu-decode-unshar t]
2238               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2239               ["Save" gnus-uu-decode-save t]
2240               ["Binhex" gnus-uu-decode-binhex t]
2241               ["Postscript" gnus-uu-decode-postscript t])
2242              ("Cache"
2243               ["Enter article" gnus-cache-enter-article t]
2244               ["Remove article" gnus-cache-remove-article t])
2245              ["Translate" gnus-article-babel t]
2246              ["Select article buffer" gnus-summary-select-article-buffer t]
2247              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2248              ["Isearch article..." gnus-summary-isearch-article t]
2249              ["Beginning of the article" gnus-summary-beginning-of-article t]
2250              ["End of the article" gnus-summary-end-of-article t]
2251              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2252              ["Fetch referenced articles" gnus-summary-refer-references t]
2253              ["Fetch current thread" gnus-summary-refer-thread t]
2254              ["Fetch article with id..." gnus-summary-refer-article t]
2255              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2256              ["Redisplay" gnus-summary-show-article t]
2257              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2258       (easy-menu-define
2259        gnus-summary-article-menu gnus-summary-mode-map ""
2260        (cons "Article" innards))
2261
2262       (if (not (keymapp gnus-summary-article-menu))
2263           (easy-menu-define
2264            gnus-article-commands-menu gnus-article-mode-map ""
2265            (cons "Commands" innards))
2266         ;; in Emacs, don't share menu.
2267         (setq gnus-article-commands-menu
2268               (copy-keymap gnus-summary-article-menu))
2269         (define-key gnus-article-mode-map [menu-bar commands]
2270           (cons "Commands" gnus-article-commands-menu))))
2271
2272     (easy-menu-define
2273      gnus-summary-thread-menu gnus-summary-mode-map ""
2274      '("Threads"
2275        ["Find all messages in thread" gnus-summary-refer-thread t]
2276        ["Toggle threading" gnus-summary-toggle-threads t]
2277        ["Hide threads" gnus-summary-hide-all-threads t]
2278        ["Show threads" gnus-summary-show-all-threads t]
2279        ["Hide thread" gnus-summary-hide-thread t]
2280        ["Show thread" gnus-summary-show-thread t]
2281        ["Go to next thread" gnus-summary-next-thread t]
2282        ["Go to previous thread" gnus-summary-prev-thread t]
2283        ["Go down thread" gnus-summary-down-thread t]
2284        ["Go up thread" gnus-summary-up-thread t]
2285        ["Top of thread" gnus-summary-top-thread t]
2286        ["Mark thread as read" gnus-summary-kill-thread t]
2287        ["Lower thread score" gnus-summary-lower-thread t]
2288        ["Raise thread score" gnus-summary-raise-thread t]
2289        ["Rethread current" gnus-summary-rethread-current t]))
2290
2291     (easy-menu-define
2292      gnus-summary-post-menu gnus-summary-mode-map ""
2293      `("Post"
2294        ["Send a message (mail or news)" gnus-summary-post-news
2295         ,@(if (featurep 'xemacs) '(t)
2296             '(:help "Post an article"))]
2297        ["Followup" gnus-summary-followup
2298         ,@(if (featurep 'xemacs) '(t)
2299             '(:help "Post followup to this article"))]
2300        ["Followup and yank" gnus-summary-followup-with-original
2301         ,@(if (featurep 'xemacs) '(t)
2302             '(:help "Post followup to this article, quoting its contents"))]
2303        ["Supersede article" gnus-summary-supersede-article t]
2304        ["Cancel article" gnus-summary-cancel-article
2305         ,@(if (featurep 'xemacs) '(t)
2306             '(:help "Cancel an article you posted"))]
2307        ["Reply" gnus-summary-reply t]
2308        ["Reply and yank" gnus-summary-reply-with-original t]
2309        ["Wide reply" gnus-summary-wide-reply t]
2310        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2311         ,@(if (featurep 'xemacs) '(t)
2312             '(:help "Mail a reply, quoting this article"))]
2313        ["Very wide reply" gnus-summary-very-wide-reply t]
2314        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2315         ,@(if (featurep 'xemacs) '(t)
2316             '(:help "Mail a very wide reply, quoting this article"))]
2317        ["Mail forward" gnus-summary-mail-forward t]
2318        ["Post forward" gnus-summary-post-forward t]
2319        ["Digest and mail" gnus-summary-digest-mail-forward t]
2320        ["Digest and post" gnus-summary-digest-post-forward t]
2321        ["Resend message" gnus-summary-resend-message t]
2322        ["Resend message edit" gnus-summary-resend-message-edit t]
2323        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2324        ["Send a mail" gnus-summary-mail-other-window t]
2325        ["Create a local message" gnus-summary-news-other-window t]
2326        ["Uuencode and post" gnus-uu-post-news
2327         ,@(if (featurep 'xemacs) '(t)
2328             '(:help "Post a uuencoded article"))]
2329        ["Followup via news" gnus-summary-followup-to-mail t]
2330        ["Followup via news and yank"
2331         gnus-summary-followup-to-mail-with-original t]
2332        ;;("Draft"
2333        ;;["Send" gnus-summary-send-draft t]
2334        ;;["Send bounced" gnus-resend-bounced-mail t])
2335        ))
2336
2337     (cond
2338      ((not (keymapp gnus-summary-post-menu))
2339       (setq gnus-article-post-menu gnus-summary-post-menu))
2340      ((not gnus-article-post-menu)
2341       ;; Don't share post menu.
2342       (setq gnus-article-post-menu
2343             (copy-keymap gnus-summary-post-menu))))
2344     (define-key gnus-article-mode-map [menu-bar post]
2345       (cons "Post" gnus-article-post-menu))
2346
2347     (easy-menu-define
2348      gnus-summary-misc-menu gnus-summary-mode-map ""
2349      `("Gnus"
2350        ("Mark Read"
2351         ["Mark as read" gnus-summary-mark-as-read-forward t]
2352         ["Mark same subject and select"
2353          gnus-summary-kill-same-subject-and-select t]
2354         ["Mark same subject" gnus-summary-kill-same-subject t]
2355         ["Catchup" gnus-summary-catchup
2356          ,@(if (featurep 'xemacs) '(t)
2357              '(:help "Mark unread articles in this group as read"))]
2358         ["Catchup all" gnus-summary-catchup-all t]
2359         ["Catchup to here" gnus-summary-catchup-to-here t]
2360         ["Catchup from here" gnus-summary-catchup-from-here t]
2361         ["Catchup region" gnus-summary-mark-region-as-read
2362          (gnus-mark-active-p)]
2363         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2364        ("Mark Various"
2365         ["Tick" gnus-summary-tick-article-forward t]
2366         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2367         ["Remove marks" gnus-summary-clear-mark-forward t]
2368         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2369         ["Set bookmark" gnus-summary-set-bookmark t]
2370         ["Remove bookmark" gnus-summary-remove-bookmark t])
2371        ("Limit to"
2372         ["Marks..." gnus-summary-limit-to-marks t]
2373         ["Subject..." gnus-summary-limit-to-subject t]
2374         ["Author..." gnus-summary-limit-to-author t]
2375         ["Age..." gnus-summary-limit-to-age t]
2376         ["Extra..." gnus-summary-limit-to-extra t]
2377         ["Score..." gnus-summary-limit-to-score t]
2378         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2379         ["Unread" gnus-summary-limit-to-unread t]
2380         ["Unseen" gnus-summary-limit-to-unseen t]
2381         ["Replied" gnus-summary-limit-to-replied t]
2382         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2383         ["Next articles" gnus-summary-limit-to-articles t]
2384         ["Pop limit" gnus-summary-pop-limit t]
2385         ["Show dormant" gnus-summary-limit-include-dormant t]
2386         ["Hide childless dormant"
2387          gnus-summary-limit-exclude-childless-dormant t]
2388         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2389         ["Hide marked" gnus-summary-limit-exclude-marks t]
2390         ["Show expunged" gnus-summary-limit-include-expunged t])
2391        ("Process Mark"
2392         ["Set mark" gnus-summary-mark-as-processable t]
2393         ["Remove mark" gnus-summary-unmark-as-processable t]
2394         ["Remove all marks" gnus-summary-unmark-all-processable t]
2395         ["Mark above" gnus-uu-mark-over t]
2396         ["Mark series" gnus-uu-mark-series t]
2397         ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2398         ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2399         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2400         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2401         ["Mark all" gnus-uu-mark-all t]
2402         ["Mark buffer" gnus-uu-mark-buffer t]
2403         ["Mark sparse" gnus-uu-mark-sparse t]
2404         ["Mark thread" gnus-uu-mark-thread t]
2405         ["Unmark thread" gnus-uu-unmark-thread t]
2406         ("Process Mark Sets"
2407          ["Kill" gnus-summary-kill-process-mark t]
2408          ["Yank" gnus-summary-yank-process-mark
2409           gnus-newsgroup-process-stack]
2410          ["Save" gnus-summary-save-process-mark t]
2411          ["Run command on marked..." gnus-summary-universal-argument t]))
2412        ("Scroll article"
2413         ["Page forward" gnus-summary-next-page
2414          ,@(if (featurep 'xemacs) '(t)
2415              '(:help "Show next page of article"))]
2416         ["Page backward" gnus-summary-prev-page
2417          ,@(if (featurep 'xemacs) '(t)
2418              '(:help "Show previous page of article"))]
2419         ["Line forward" gnus-summary-scroll-up t])
2420        ("Move"
2421         ["Next unread article" gnus-summary-next-unread-article t]
2422         ["Previous unread article" gnus-summary-prev-unread-article t]
2423         ["Next article" gnus-summary-next-article t]
2424         ["Previous article" gnus-summary-prev-article t]
2425         ["Next unread subject" gnus-summary-next-unread-subject t]
2426         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2427         ["Next article same subject" gnus-summary-next-same-subject t]
2428         ["Previous article same subject" gnus-summary-prev-same-subject t]
2429         ["First unread article" gnus-summary-first-unread-article t]
2430         ["Best unread article" gnus-summary-best-unread-article t]
2431         ["Go to subject number..." gnus-summary-goto-subject t]
2432         ["Go to article number..." gnus-summary-goto-article t]
2433         ["Go to the last article" gnus-summary-goto-last-article t]
2434         ["Pop article off history" gnus-summary-pop-article t])
2435        ("Sort"
2436         ["Sort by number" gnus-summary-sort-by-number t]
2437         ["Sort by author" gnus-summary-sort-by-author t]
2438         ["Sort by subject" gnus-summary-sort-by-subject t]
2439         ["Sort by date" gnus-summary-sort-by-date t]
2440         ["Sort by score" gnus-summary-sort-by-score t]
2441         ["Sort by lines" gnus-summary-sort-by-lines t]
2442         ["Sort by characters" gnus-summary-sort-by-chars t]
2443         ["Randomize" gnus-summary-sort-by-random t]
2444         ["Original sort" gnus-summary-sort-by-original t])
2445        ("Help"
2446         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2447         ["Describe group" gnus-summary-describe-group t]
2448         ["Fetch charter" gnus-group-fetch-charter
2449          ,@(if (featurep 'xemacs) nil
2450              '(:help "Display the charter of the current group"))]
2451         ["Fetch control message" gnus-group-fetch-control
2452          ,@(if (featurep 'xemacs) nil
2453              '(:help "Display the archived control message for the current group"))]
2454         ["Read manual" gnus-info-find-node t])
2455        ("Modes"
2456         ["Pick and read" gnus-pick-mode t]
2457         ["Binary" gnus-binary-mode t])
2458        ("Regeneration"
2459         ["Regenerate" gnus-summary-prepare t]
2460         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2461         ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2462         ["Toggle threading" gnus-summary-toggle-threads t])
2463        ["See old articles" gnus-summary-insert-old-articles t]
2464        ["See new articles" gnus-summary-insert-new-articles t]
2465        ["Filter articles..." gnus-summary-execute-command t]
2466        ["Run command on articles..." gnus-summary-universal-argument t]
2467        ["Search articles forward..." gnus-summary-search-article-forward t]
2468        ["Search articles backward..." gnus-summary-search-article-backward t]
2469        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2470        ["Expand window" gnus-summary-expand-window t]
2471        ["Expire expirable articles" gnus-summary-expire-articles
2472         (gnus-check-backend-function
2473          'request-expire-articles gnus-newsgroup-name)]
2474        ["Edit local kill file" gnus-summary-edit-local-kill t]
2475        ["Edit main kill file" gnus-summary-edit-global-kill t]
2476        ["Edit group parameters" gnus-summary-edit-parameters t]
2477        ["Customize group parameters" gnus-summary-customize-parameters t]
2478        ["Send a bug report" gnus-bug t]
2479        ("Exit"
2480         ["Catchup and exit" gnus-summary-catchup-and-exit
2481          ,@(if (featurep 'xemacs) '(t)
2482              '(:help "Mark unread articles in this group as read, then exit"))]
2483         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2484         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2485         ["Exit group" gnus-summary-exit
2486          ,@(if (featurep 'xemacs) '(t)
2487              '(:help "Exit current group, return to group selection mode"))]
2488         ["Exit group without updating" gnus-summary-exit-no-update t]
2489         ["Exit and goto next group" gnus-summary-next-group t]
2490         ["Exit and goto prev group" gnus-summary-prev-group t]
2491         ["Reselect group" gnus-summary-reselect-current-group t]
2492         ["Rescan group" gnus-summary-rescan-group t]
2493         ["Update dribble" gnus-summary-save-newsrc t])))
2494
2495     (gnus-run-hooks 'gnus-summary-menu-hook)))
2496
2497 (defvar gnus-summary-tool-bar-map nil)
2498
2499 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2500 (defun gnus-summary-make-tool-bar ()
2501   (if (and (fboundp 'tool-bar-add-item-from-menu)
2502            (default-value 'tool-bar-mode)
2503            (not gnus-summary-tool-bar-map))
2504       (setq gnus-summary-tool-bar-map
2505             (let ((tool-bar-map (make-sparse-keymap))
2506                   (load-path (mm-image-load-path)))
2507               (tool-bar-add-item-from-menu
2508                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2509               (tool-bar-add-item-from-menu
2510                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2511               (tool-bar-add-item-from-menu
2512                'gnus-summary-post-news "post" gnus-summary-mode-map)
2513               (tool-bar-add-item-from-menu
2514                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2515               (tool-bar-add-item-from-menu
2516                'gnus-summary-followup "followup" gnus-summary-mode-map)
2517               (tool-bar-add-item-from-menu
2518                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2519               (tool-bar-add-item-from-menu
2520                'gnus-summary-reply "reply" gnus-summary-mode-map)
2521               (tool-bar-add-item-from-menu
2522                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2523               (tool-bar-add-item-from-menu
2524                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2525               (tool-bar-add-item-from-menu
2526                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2527               (tool-bar-add-item-from-menu
2528                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2529               (tool-bar-add-item-from-menu
2530                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2531               (tool-bar-add-item-from-menu
2532                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2533               (tool-bar-add-item-from-menu
2534                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2535               (tool-bar-add-item-from-menu
2536                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2537               tool-bar-map)))
2538   (if gnus-summary-tool-bar-map
2539       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2540
2541 (defun gnus-score-set-default (var value)
2542   "A version of set that updates the GNU Emacs menu-bar."
2543   (set var value)
2544   ;; It is the message that forces the active status to be updated.
2545   (message ""))
2546
2547 (defun gnus-make-score-map (type)
2548   "Make a summary score map of type TYPE."
2549   (if t
2550       nil
2551     (let ((headers '(("author" "from" string)
2552                      ("subject" "subject" string)
2553                      ("article body" "body" string)
2554                      ("article head" "head" string)
2555                      ("xref" "xref" string)
2556                      ("extra header" "extra" string)
2557                      ("lines" "lines" number)
2558                      ("followups to author" "followup" string)))
2559           (types '((number ("less than" <)
2560                            ("greater than" >)
2561                            ("equal" =))
2562                    (string ("substring" s)
2563                            ("exact string" e)
2564                            ("fuzzy string" f)
2565                            ("regexp" r))))
2566           (perms '(("temporary" (current-time-string))
2567                    ("permanent" nil)
2568                    ("immediate" now)))
2569           header)
2570       (list
2571        (apply
2572         'nconc
2573         (list
2574          (if (eq type 'lower)
2575              "Lower score"
2576            "Increase score"))
2577         (let (outh)
2578           (while headers
2579             (setq header (car headers))
2580             (setq outh
2581                   (cons
2582                    (apply
2583                     'nconc
2584                     (list (car header))
2585                     (let ((ts (cdr (assoc (nth 2 header) types)))
2586                           outt)
2587                       (while ts
2588                         (setq outt
2589                               (cons
2590                                (apply
2591                                 'nconc
2592                                 (list (caar ts))
2593                                 (let ((ps perms)
2594                                       outp)
2595                                   (while ps
2596                                     (setq outp
2597                                           (cons
2598                                            (vector
2599                                             (caar ps)
2600                                             (list
2601                                              'gnus-summary-score-entry
2602                                              (nth 1 header)
2603                                              (if (or (string= (nth 1 header)
2604                                                               "head")
2605                                                      (string= (nth 1 header)
2606                                                               "body"))
2607                                                  ""
2608                                                (list 'gnus-summary-header
2609                                                      (nth 1 header)))
2610                                              (list 'quote (nth 1 (car ts)))
2611                                              (list 'gnus-score-delta-default
2612                                                    nil)
2613                                              (nth 1 (car ps))
2614                                              t)
2615                                             t)
2616                                            outp))
2617                                     (setq ps (cdr ps)))
2618                                   (list (nreverse outp))))
2619                                outt))
2620                         (setq ts (cdr ts)))
2621                       (list (nreverse outt))))
2622                    outh))
2623             (setq headers (cdr headers)))
2624           (list (nreverse outh))))))))
2625
2626 \f
2627
2628 (defun gnus-summary-mode (&optional group)
2629   "Major mode for reading articles.
2630
2631 All normal editing commands are switched off.
2632 \\<gnus-summary-mode-map>
2633 Each line in this buffer represents one article.  To read an
2634 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2635 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2636 respectively.
2637
2638 You can also post articles and send mail from this buffer.  To
2639 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2640 of an article, type `\\[gnus-summary-reply]'.
2641
2642 There are approx. one gazillion commands you can execute in this
2643 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2644
2645 The following commands are available:
2646
2647 \\{gnus-summary-mode-map}"
2648   (interactive)
2649   (kill-all-local-variables)
2650   (when (gnus-visual-p 'summary-menu 'menu)
2651     (gnus-summary-make-menu-bar)
2652     (gnus-summary-make-tool-bar))
2653   (gnus-summary-make-local-variables)
2654   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2655     (gnus-summary-make-local-variables))
2656   (gnus-make-thread-indent-array)
2657   (gnus-simplify-mode-line)
2658   (setq major-mode 'gnus-summary-mode)
2659   (setq mode-name "Summary")
2660   (make-local-variable 'minor-mode-alist)
2661   (use-local-map gnus-summary-mode-map)
2662   (buffer-disable-undo)
2663   (setq buffer-read-only t)             ;Disable modification
2664   (setq truncate-lines t)
2665   (setq selective-display t)
2666   (setq selective-display-ellipses t)   ;Display `...'
2667   (gnus-summary-set-display-table)
2668   (gnus-set-default-directory)
2669   (setq gnus-newsgroup-name group)
2670   (unless (gnus-news-group-p group)
2671     (setq gnus-newsgroup-incorporated
2672           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2673   (make-local-variable 'gnus-summary-line-format)
2674   (make-local-variable 'gnus-summary-line-format-spec)
2675   (make-local-variable 'gnus-summary-dummy-line-format)
2676   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2677   (make-local-variable 'gnus-summary-mark-positions)
2678   (gnus-make-local-hook 'pre-command-hook)
2679   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2680   (gnus-run-hooks 'gnus-summary-mode-hook)
2681   (turn-on-gnus-mailing-list-mode)
2682   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2683   (gnus-update-summary-mark-positions))
2684
2685 (defun gnus-summary-make-local-variables ()
2686   "Make all the local summary buffer variables."
2687   (let (global)
2688     (dolist (local gnus-summary-local-variables)
2689       (if (consp local)
2690           (progn
2691             (if (eq (cdr local) 'global)
2692                 ;; Copy the global value of the variable.
2693                 (setq global (symbol-value (car local)))
2694               ;; Use the value from the list.
2695               (setq global (eval (cdr local))))
2696             (set (make-local-variable (car local)) global))
2697         ;; Simple nil-valued local variable.
2698         (set (make-local-variable local) nil)))))
2699
2700 (defun gnus-summary-clear-local-variables ()
2701   (let ((locals gnus-summary-local-variables))
2702     (while locals
2703       (if (consp (car locals))
2704           (and (vectorp (caar locals))
2705                (set (caar locals) nil))
2706         (and (vectorp (car locals))
2707              (set (car locals) nil)))
2708       (setq locals (cdr locals)))))
2709
2710 ;; Summary data functions.
2711
2712 (defmacro gnus-data-number (data)
2713   `(car ,data))
2714
2715 (defmacro gnus-data-set-number (data number)
2716   `(setcar ,data ,number))
2717
2718 (defmacro gnus-data-mark (data)
2719   `(nth 1 ,data))
2720
2721 (defmacro gnus-data-set-mark (data mark)
2722   `(setcar (nthcdr 1 ,data) ,mark))
2723
2724 (defmacro gnus-data-pos (data)
2725   `(nth 2 ,data))
2726
2727 (defmacro gnus-data-set-pos (data pos)
2728   `(setcar (nthcdr 2 ,data) ,pos))
2729
2730 (defmacro gnus-data-header (data)
2731   `(nth 3 ,data))
2732
2733 (defmacro gnus-data-set-header (data header)
2734   `(setcar (nthcdr 3 ,data) ,header))
2735
2736 (defmacro gnus-data-level (data)
2737   `(nth 4 ,data))
2738
2739 (defmacro gnus-data-unread-p (data)
2740   `(= (nth 1 ,data) gnus-unread-mark))
2741
2742 (defmacro gnus-data-read-p (data)
2743   `(/= (nth 1 ,data) gnus-unread-mark))
2744
2745 (defmacro gnus-data-pseudo-p (data)
2746   `(consp (nth 3 ,data)))
2747
2748 (defmacro gnus-data-find (number)
2749   `(assq ,number gnus-newsgroup-data))
2750
2751 (defmacro gnus-data-find-list (number &optional data)
2752   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2753      (memq (assq ,number bdata)
2754            bdata)))
2755
2756 (defmacro gnus-data-make (number mark pos header level)
2757   `(list ,number ,mark ,pos ,header ,level))
2758
2759 (defun gnus-data-enter (after-article number mark pos header level offset)
2760   (let ((data (gnus-data-find-list after-article)))
2761     (unless data
2762       (error "No such article: %d" after-article))
2763     (setcdr data (cons (gnus-data-make number mark pos header level)
2764                        (cdr data)))
2765     (setq gnus-newsgroup-data-reverse nil)
2766     (gnus-data-update-list (cddr data) offset)))
2767
2768 (defun gnus-data-enter-list (after-article list &optional offset)
2769   (when list
2770     (let ((data (and after-article (gnus-data-find-list after-article)))
2771           (ilist list))
2772       (if (not (or data
2773                    after-article))
2774           (let ((odata gnus-newsgroup-data))
2775             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2776             (when offset
2777               (gnus-data-update-list odata offset)))
2778         ;; Find the last element in the list to be spliced into the main
2779         ;; list.
2780         (while (cdr list)
2781           (setq list (cdr list)))
2782         (if (not data)
2783             (progn
2784               (setcdr list gnus-newsgroup-data)
2785               (setq gnus-newsgroup-data ilist)
2786               (when offset
2787                 (gnus-data-update-list (cdr list) offset)))
2788           (setcdr list (cdr data))
2789           (setcdr data ilist)
2790           (when offset
2791             (gnus-data-update-list (cdr list) offset))))
2792       (setq gnus-newsgroup-data-reverse nil))))
2793
2794 (defun gnus-data-remove (article &optional offset)
2795   (let ((data gnus-newsgroup-data))
2796     (if (= (gnus-data-number (car data)) article)
2797         (progn
2798           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2799                 gnus-newsgroup-data-reverse nil)
2800           (when offset
2801             (gnus-data-update-list gnus-newsgroup-data offset)))
2802       (while (cdr data)
2803         (when (= (gnus-data-number (cadr data)) article)
2804           (setcdr data (cddr data))
2805           (when offset
2806             (gnus-data-update-list (cdr data) offset))
2807           (setq data nil
2808                 gnus-newsgroup-data-reverse nil))
2809         (setq data (cdr data))))))
2810
2811 (defmacro gnus-data-list (backward)
2812   `(if ,backward
2813        (or gnus-newsgroup-data-reverse
2814            (setq gnus-newsgroup-data-reverse
2815                  (reverse gnus-newsgroup-data)))
2816      gnus-newsgroup-data))
2817
2818 (defun gnus-data-update-list (data offset)
2819   "Add OFFSET to the POS of all data entries in DATA."
2820   (setq gnus-newsgroup-data-reverse nil)
2821   (while data
2822     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2823     (setq data (cdr data))))
2824
2825 (defun gnus-summary-article-pseudo-p (article)
2826   "Say whether this article is a pseudo article or not."
2827   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2828
2829 (defmacro gnus-summary-article-sparse-p (article)
2830   "Say whether this article is a sparse article or not."
2831   `(memq ,article gnus-newsgroup-sparse))
2832
2833 (defmacro gnus-summary-article-ancient-p (article)
2834   "Say whether this article is a sparse article or not."
2835   `(memq ,article gnus-newsgroup-ancient))
2836
2837 (defun gnus-article-parent-p (number)
2838   "Say whether this article is a parent or not."
2839   (let ((data (gnus-data-find-list number)))
2840     (and (cdr data)                     ; There has to be an article after...
2841          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2842             (gnus-data-level (nth 1 data))))))
2843
2844 (defun gnus-article-children (number)
2845   "Return a list of all children to NUMBER."
2846   (let* ((data (gnus-data-find-list number))
2847          (level (gnus-data-level (car data)))
2848          children)
2849     (setq data (cdr data))
2850     (while (and data
2851                 (= (gnus-data-level (car data)) (1+ level)))
2852       (push (gnus-data-number (car data)) children)
2853       (setq data (cdr data)))
2854     children))
2855
2856 (defmacro gnus-summary-skip-intangible ()
2857   "If the current article is intangible, then jump to a different article."
2858   '(let ((to (get-text-property (point) 'gnus-intangible)))
2859      (and to (gnus-summary-goto-subject to))))
2860
2861 (defmacro gnus-summary-article-intangible-p ()
2862   "Say whether this article is intangible or not."
2863   '(get-text-property (point) 'gnus-intangible))
2864
2865 (defun gnus-article-read-p (article)
2866   "Say whether ARTICLE is read or not."
2867   (not (or (memq article gnus-newsgroup-marked)
2868            (memq article gnus-newsgroup-spam-marked)
2869            (memq article gnus-newsgroup-unreads)
2870            (memq article gnus-newsgroup-unselected)
2871            (memq article gnus-newsgroup-dormant))))
2872
2873 ;; Some summary mode macros.
2874
2875 (defmacro gnus-summary-article-number ()
2876   "The article number of the article on the current line.
2877 If there isn't an article number here, then we return the current
2878 article number."
2879   '(progn
2880      (gnus-summary-skip-intangible)
2881      (or (get-text-property (point) 'gnus-number)
2882          (gnus-summary-last-subject))))
2883
2884 (defmacro gnus-summary-article-header (&optional number)
2885   "Return the header of article NUMBER."
2886   `(gnus-data-header (gnus-data-find
2887                       ,(or number '(gnus-summary-article-number)))))
2888
2889 (defmacro gnus-summary-thread-level (&optional number)
2890   "Return the level of thread that starts with article NUMBER."
2891   `(if (and (eq gnus-summary-make-false-root 'dummy)
2892             (get-text-property (point) 'gnus-intangible))
2893        0
2894      (gnus-data-level (gnus-data-find
2895                        ,(or number '(gnus-summary-article-number))))))
2896
2897 (defmacro gnus-summary-article-mark (&optional number)
2898   "Return the mark of article NUMBER."
2899   `(gnus-data-mark (gnus-data-find
2900                     ,(or number '(gnus-summary-article-number)))))
2901
2902 (defmacro gnus-summary-article-pos (&optional number)
2903   "Return the position of the line of article NUMBER."
2904   `(gnus-data-pos (gnus-data-find
2905                    ,(or number '(gnus-summary-article-number)))))
2906
2907 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2908 (defmacro gnus-summary-article-subject (&optional number)
2909   "Return current subject string or nil if nothing."
2910   `(let ((headers
2911           ,(if number
2912                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2913              '(gnus-data-header (assq (gnus-summary-article-number)
2914                                       gnus-newsgroup-data)))))
2915      (and headers
2916           (vectorp headers)
2917           (mail-header-subject headers))))
2918
2919 (defmacro gnus-summary-article-score (&optional number)
2920   "Return current article score."
2921   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2922                   gnus-newsgroup-scored))
2923        gnus-summary-default-score 0))
2924
2925 (defun gnus-summary-article-children (&optional number)
2926   "Return a list of article numbers that are children of article NUMBER."
2927   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2928          (level (gnus-data-level (car data)))
2929          l children)
2930     (while (and (setq data (cdr data))
2931                 (> (setq l (gnus-data-level (car data))) level))
2932       (and (= (1+ level) l)
2933            (push (gnus-data-number (car data))
2934                  children)))
2935     (nreverse children)))
2936
2937 (defun gnus-summary-article-parent (&optional number)
2938   "Return the article number of the parent of article NUMBER."
2939   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2940                                     (gnus-data-list t)))
2941          (level (gnus-data-level (car data))))
2942     (if (zerop level)
2943         ()                              ; This is a root.
2944       ;; We search until we find an article with a level less than
2945       ;; this one.  That function has to be the parent.
2946       (while (and (setq data (cdr data))
2947                   (not (< (gnus-data-level (car data)) level))))
2948       (and data (gnus-data-number (car data))))))
2949
2950 (defun gnus-unread-mark-p (mark)
2951   "Say whether MARK is the unread mark."
2952   (= mark gnus-unread-mark))
2953
2954 (defun gnus-read-mark-p (mark)
2955   "Say whether MARK is one of the marks that mark as read.
2956 This is all marks except unread, ticked, dormant, and expirable."
2957   (not (or (= mark gnus-unread-mark)
2958            (= mark gnus-ticked-mark)
2959            (= mark gnus-spam-mark)
2960            (= mark gnus-dormant-mark)
2961            (= mark gnus-expirable-mark))))
2962
2963 (defmacro gnus-article-mark (number)
2964   "Return the MARK of article NUMBER.
2965 This macro should only be used when computing the mark the \"first\"
2966 time; i.e., when generating the summary lines.  After that,
2967 `gnus-summary-article-mark' should be used to examine the
2968 marks of articles."
2969   `(cond
2970     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2971     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2972     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2973     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2974     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2975     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2976     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2977     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2978            gnus-ancient-mark))))
2979
2980 ;; Saving hidden threads.
2981
2982 (defmacro gnus-save-hidden-threads (&rest forms)
2983   "Save hidden threads, eval FORMS, and restore the hidden threads."
2984   (let ((config (make-symbol "config")))
2985     `(let ((,config (gnus-hidden-threads-configuration)))
2986        (unwind-protect
2987            (save-excursion
2988              ,@forms)
2989          (gnus-restore-hidden-threads-configuration ,config)))))
2990 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2991 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2992
2993 (defun gnus-data-compute-positions ()
2994   "Compute the positions of all articles."
2995   (setq gnus-newsgroup-data-reverse nil)
2996   (let ((data gnus-newsgroup-data))
2997     (save-excursion
2998       (gnus-save-hidden-threads
2999         (gnus-summary-show-all-threads)
3000         (goto-char (point-min))
3001         (while data
3002           (while (get-text-property (point) 'gnus-intangible)
3003             (forward-line 1))
3004           (gnus-data-set-pos (car data) (+ (point) 3))
3005           (setq data (cdr data))
3006           (forward-line 1))))))
3007
3008 (defun gnus-hidden-threads-configuration ()
3009   "Return the current hidden threads configuration."
3010   (save-excursion
3011     (let (config)
3012       (goto-char (point-min))
3013       (while (search-forward "\r" nil t)
3014         (push (1- (point)) config))
3015       config)))
3016
3017 (defun gnus-restore-hidden-threads-configuration (config)
3018   "Restore hidden threads configuration from CONFIG."
3019   (save-excursion
3020     (let (point buffer-read-only)
3021       (while (setq point (pop config))
3022         (when (and (< point (point-max))
3023                    (goto-char point)
3024                    (eq (char-after) ?\n))
3025           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3026
3027 ;; Various summary mode internalish functions.
3028
3029 (defun gnus-mouse-pick-article (e)
3030   (interactive "e")
3031   (mouse-set-point e)
3032   (gnus-summary-next-page nil t))
3033
3034 (defun gnus-summary-set-display-table ()
3035   "Change the display table.
3036 Odd characters have a tendency to mess
3037 up nicely formatted displays - we make all possible glyphs
3038 display only a single character."
3039
3040   ;; We start from the standard display table, if any.
3041   (let ((table (or (copy-sequence standard-display-table)
3042                    (make-display-table)))
3043         (i 32))
3044     ;; Nix out all the control chars...
3045     (while (>= (setq i (1- i)) 0)
3046       (aset table i [??]))
3047     ;; ... but not newline and cr, of course.  (cr is necessary for the
3048     ;; selective display).
3049     (aset table ?\n nil)
3050     (aset table ?\r nil)
3051     ;; We keep TAB as well.
3052     (aset table ?\t nil)
3053     ;; We nix out any glyphs over 126 that are not set already.
3054     (let ((i 256))
3055       (while (>= (setq i (1- i)) 127)
3056         ;; Only modify if the entry is nil.
3057         (unless (aref table i)
3058           (aset table i [??]))))
3059     (setq buffer-display-table table)))
3060
3061 (defun gnus-summary-set-article-display-arrow (pos)
3062   "Update the overlay arrow to point to line at position POS."
3063   (when (and gnus-summary-display-arrow
3064              (boundp 'overlay-arrow-position)
3065              (boundp 'overlay-arrow-string))
3066     (save-excursion
3067       (goto-char pos)
3068       (beginning-of-line)
3069       (unless overlay-arrow-position
3070         (setq overlay-arrow-position (make-marker)))
3071       (setq overlay-arrow-string "=>"
3072             overlay-arrow-position (set-marker overlay-arrow-position
3073                                                (point)
3074                                                (current-buffer))))))
3075
3076 (defun gnus-summary-setup-buffer (group)
3077   "Initialize summary buffer."
3078   (let ((buffer (gnus-summary-buffer-name group))
3079         (dead-name (concat "*Dead Summary "
3080                            (gnus-group-decoded-name group) "*")))
3081     ;; If a dead summary buffer exists, we kill it.
3082     (when (gnus-buffer-live-p dead-name)
3083       (gnus-kill-buffer dead-name))
3084     (if (get-buffer buffer)
3085         (progn
3086           (set-buffer buffer)
3087           (setq gnus-summary-buffer (current-buffer))
3088           (not gnus-newsgroup-prepared))
3089       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3090       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3091       (gnus-summary-mode group)
3092       (when gnus-carpal
3093         (gnus-carpal-setup-buffer 'summary))
3094       (unless gnus-single-article-buffer
3095         (make-local-variable 'gnus-article-buffer)
3096         (make-local-variable 'gnus-article-current)
3097         (make-local-variable 'gnus-original-article-buffer))
3098       (setq gnus-newsgroup-name group)
3099       ;; Set any local variables in the group parameters.
3100       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3101       t)))
3102
3103 (defun gnus-set-global-variables ()
3104   "Set the global equivalents of the buffer-local variables.
3105 They are set to the latest values they had.  These reflect the summary
3106 buffer that was in action when the last article was fetched."
3107   (when (eq major-mode 'gnus-summary-mode)
3108     (setq gnus-summary-buffer (current-buffer))
3109     (let ((name gnus-newsgroup-name)
3110           (marked gnus-newsgroup-marked)
3111           (spam gnus-newsgroup-spam-marked)
3112           (unread gnus-newsgroup-unreads)
3113           (headers gnus-current-headers)
3114           (data gnus-newsgroup-data)
3115           (summary gnus-summary-buffer)
3116           (article-buffer gnus-article-buffer)
3117           (original gnus-original-article-buffer)
3118           (gac gnus-article-current)
3119           (reffed gnus-reffed-article-number)
3120           (score-file gnus-current-score-file)
3121           (default-charset gnus-newsgroup-charset)
3122           vlist)
3123       (let ((locals gnus-newsgroup-variables))
3124         (while locals
3125           (if (consp (car locals))
3126               (push (eval (caar locals)) vlist)
3127             (push (eval (car locals)) vlist))
3128           (setq locals (cdr locals)))
3129         (setq vlist (nreverse vlist)))
3130       (save-excursion
3131         (set-buffer gnus-group-buffer)
3132         (setq gnus-newsgroup-name name
3133               gnus-newsgroup-marked marked
3134               gnus-newsgroup-spam-marked spam
3135               gnus-newsgroup-unreads unread
3136               gnus-current-headers headers
3137               gnus-newsgroup-data data
3138               gnus-article-current gac
3139               gnus-summary-buffer summary
3140               gnus-article-buffer article-buffer
3141               gnus-original-article-buffer original
3142               gnus-reffed-article-number reffed
3143               gnus-current-score-file score-file
3144               gnus-newsgroup-charset default-charset)
3145         (let ((locals gnus-newsgroup-variables))
3146           (while locals
3147             (if (consp (car locals))
3148                 (set (caar locals) (pop vlist))
3149               (set (car locals) (pop vlist)))
3150             (setq locals (cdr locals))))
3151         ;; The article buffer also has local variables.
3152         (when (gnus-buffer-live-p gnus-article-buffer)
3153           (set-buffer gnus-article-buffer)
3154           (setq gnus-summary-buffer summary))))))
3155
3156 (defun gnus-summary-article-unread-p (article)
3157   "Say whether ARTICLE is unread or not."
3158   (memq article gnus-newsgroup-unreads))
3159
3160 (defun gnus-summary-first-article-p (&optional article)
3161   "Return whether ARTICLE is the first article in the buffer."
3162   (if (not (setq article (or article (gnus-summary-article-number))))
3163       nil
3164     (eq article (caar gnus-newsgroup-data))))
3165
3166 (defun gnus-summary-last-article-p (&optional article)
3167   "Return whether ARTICLE is the last article in the buffer."
3168   (if (not (setq article (or article (gnus-summary-article-number))))
3169       ;; All non-existent numbers are the last article.  :-)
3170       t
3171     (not (cdr (gnus-data-find-list article)))))
3172
3173 (defun gnus-make-thread-indent-array ()
3174   (let ((n 200))
3175     (unless (and gnus-thread-indent-array
3176                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3177       (setq gnus-thread-indent-array (make-vector 201 "")
3178             gnus-thread-indent-array-level gnus-thread-indent-level)
3179       (while (>= n 0)
3180         (aset gnus-thread-indent-array n
3181               (make-string (* n gnus-thread-indent-level) ? ))
3182         (setq n (1- n))))))
3183
3184 (defun gnus-update-summary-mark-positions ()
3185   "Compute where the summary marks are to go."
3186   (save-excursion
3187     (when (gnus-buffer-exists-p gnus-summary-buffer)
3188       (set-buffer gnus-summary-buffer))
3189     (let ((gnus-replied-mark 129)
3190           (gnus-score-below-mark 130)
3191           (gnus-score-over-mark 130)
3192           (gnus-undownloaded-mark 131)
3193           (spec gnus-summary-line-format-spec)
3194           gnus-visual pos)
3195       (save-excursion
3196         (gnus-set-work-buffer)
3197         (let ((gnus-summary-line-format-spec spec)
3198               (gnus-newsgroup-downloadable '(0)))
3199           (gnus-summary-insert-line
3200            (make-full-mail-header 0 "" "nobody"
3201                                   "05 Apr 2001 23:33:09 +0400"
3202                                   "" "" 0 0 "" nil)
3203            0 nil t 128 t nil "" nil 1)
3204           (goto-char (point-min))
3205           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3206                                              (- (point) (point-min) 1)))))
3207           (goto-char (point-min))
3208           (push (cons 'replied (and (search-forward "\201" nil t)
3209                                     (- (point) (point-min) 1)))
3210                 pos)
3211           (goto-char (point-min))
3212           (push (cons 'score (and (search-forward "\202" nil t)
3213                                   (- (point) (point-min) 1)))
3214                 pos)
3215           (goto-char (point-min))
3216           (push (cons 'download
3217                       (and (search-forward "\203" nil t)
3218                            (- (point) (point-min) 1)))
3219                 pos)))
3220       (setq gnus-summary-mark-positions pos))))
3221
3222 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3223   "Insert a dummy root in the summary buffer."
3224   (beginning-of-line)
3225   (gnus-add-text-properties
3226    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3227    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3228
3229 (defun gnus-summary-extract-address-component (from)
3230   (or (car (funcall gnus-extract-address-components from))
3231       from))
3232
3233 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3234   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3235                                 default-mime-charset)))
3236     ;; Is it really necessary to do this next part for each summary line?
3237     ;; Luckily, doesn't seem to slow things down much.
3238     (or
3239      (and gnus-ignored-from-addresses
3240           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3241           (let ((extra-headers (mail-header-extra header))
3242                 to
3243                 newsgroups)
3244             (cond
3245              ((setq to (cdr (assq 'To extra-headers)))
3246               (concat "-> "
3247                       (inline
3248                         (gnus-summary-extract-address-component
3249                          (funcall gnus-decode-encoded-word-function to)))))
3250              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3251               (concat "=> " newsgroups)))))
3252      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3253
3254 (defun gnus-summary-insert-line (gnus-tmp-header
3255                                  gnus-tmp-level gnus-tmp-current
3256                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3257                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3258                                  &optional gnus-tmp-dummy gnus-tmp-score
3259                                  gnus-tmp-process)
3260   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3261          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3262          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3263          (gnus-tmp-score-char
3264           (if (or (null gnus-summary-default-score)
3265                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3266                       gnus-summary-zcore-fuzz))
3267               ?\ ;;;Whitespace
3268             (if (< gnus-tmp-score gnus-summary-default-score)
3269                 gnus-score-below-mark gnus-score-over-mark)))
3270          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3271          (gnus-tmp-replied
3272           (cond (gnus-tmp-process gnus-process-mark)
3273                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3274                  gnus-cached-mark)
3275                 (gnus-tmp-replied gnus-replied-mark)
3276                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3277                  gnus-forwarded-mark)
3278                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3279                  gnus-saved-mark)
3280                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3281                  gnus-recent-mark)
3282                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3283                  gnus-unseen-mark)
3284                 (t gnus-no-mark)))
3285          (gnus-tmp-downloaded
3286           (cond (undownloaded
3287                  gnus-undownloaded-mark)
3288                 (gnus-newsgroup-agentized
3289                  gnus-downloaded-mark)
3290                 (t
3291                  gnus-no-mark)))
3292          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3293          (gnus-tmp-name
3294           (cond
3295            ((string-match "<[^>]+> *$" gnus-tmp-from)
3296             (let ((beg (match-beginning 0)))
3297               (or (and (string-match "^\".+\"" gnus-tmp-from)
3298                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3299                   (substring gnus-tmp-from 0 beg))))
3300            ((string-match "(.+)" gnus-tmp-from)
3301             (substring gnus-tmp-from
3302                        (1+ (match-beginning 0)) (1- (match-end 0))))
3303            (t gnus-tmp-from)))
3304          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3305          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3306          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3307          (buffer-read-only nil))
3308     (when (string= gnus-tmp-name "")
3309       (setq gnus-tmp-name gnus-tmp-from))
3310     (unless (numberp gnus-tmp-lines)
3311       (setq gnus-tmp-lines -1))
3312     (if (= gnus-tmp-lines -1)
3313         (setq gnus-tmp-lines "?")
3314       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3315     (gnus-put-text-property-excluding-characters-with-faces
3316      (point)
3317      (progn (eval gnus-summary-line-format-spec) (point))
3318      'gnus-number gnus-tmp-number)
3319     (when (gnus-visual-p 'summary-highlight 'highlight)
3320       (forward-line -1)
3321       (gnus-run-hooks 'gnus-summary-update-hook)
3322       (forward-line 1))))
3323
3324 (defun gnus-summary-update-line (&optional dont-update)
3325   "Update summary line after change."
3326   (when (and gnus-summary-default-score
3327              (not gnus-summary-inhibit-highlight))
3328     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3329            (article (gnus-summary-article-number))
3330            (score (gnus-summary-article-score article)))
3331       (unless dont-update
3332         (if (and gnus-summary-mark-below
3333                  (< (gnus-summary-article-score)
3334                     gnus-summary-mark-below))
3335             ;; This article has a low score, so we mark it as read.
3336             (when (memq article gnus-newsgroup-unreads)
3337               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3338           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3339             ;; This article was previously marked as read on account
3340             ;; of a low score, but now it has risen, so we mark it as
3341             ;; unread.
3342             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3343         (gnus-summary-update-mark
3344          (if (or (null gnus-summary-default-score)
3345                  (<= (abs (- score gnus-summary-default-score))
3346                      gnus-summary-zcore-fuzz))
3347              ?\ ;;;Whitespace
3348            (if (< score gnus-summary-default-score)
3349                gnus-score-below-mark gnus-score-over-mark))
3350          'score))
3351       ;; Do visual highlighting.
3352       (when (gnus-visual-p 'summary-highlight 'highlight)
3353         (gnus-run-hooks 'gnus-summary-update-hook)))))
3354
3355 (defvar gnus-tmp-new-adopts nil)
3356
3357 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3358   "Return the number of articles in THREAD.
3359 This may be 0 in some cases -- if none of the articles in
3360 the thread are to be displayed."
3361   (let* ((number
3362           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3363           (cond
3364            ((not (listp thread))
3365             1)
3366            ((and (consp thread) (cdr thread))
3367             (apply
3368              '+ 1 (mapcar
3369                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3370            ((null thread)
3371             1)
3372            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3373             1)
3374            (t 0))))
3375     (when (and level (zerop level) gnus-tmp-new-adopts)
3376       (incf number
3377             (apply '+ (mapcar
3378                        'gnus-summary-number-of-articles-in-thread
3379                        gnus-tmp-new-adopts))))
3380     (if char
3381         (if (> number 1) gnus-not-empty-thread-mark
3382           gnus-empty-thread-mark)
3383       number)))
3384
3385 (defsubst gnus-summary-line-message-size (head)
3386   "Return pretty-printed version of message size.
3387 This function is intended to be used in
3388 `gnus-summary-line-format-alist'."
3389   (let ((c (or (mail-header-chars head) -1)))
3390     (cond ((< c 0) "n/a")               ; chars not available
3391           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3392           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3393           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3394           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3395
3396
3397 (defun gnus-summary-set-local-parameters (group)
3398   "Go through the local params of GROUP and set all variable specs in that list."
3399   (let ((params (gnus-group-find-parameter group))
3400         (vars '(quit-config))           ; Ignore quit-config.
3401         elem)
3402     (while params
3403       (setq elem (car params)
3404             params (cdr params))
3405       (and (consp elem)                 ; Has to be a cons.
3406            (consp (cdr elem))           ; The cdr has to be a list.
3407            (symbolp (car elem))         ; Has to be a symbol in there.
3408            (not (memq (car elem) vars))
3409            (ignore-errors               ; So we set it.
3410              (push (car elem) vars)
3411              (make-local-variable (car elem))
3412              (set (car elem) (eval (nth 1 elem))))))))
3413
3414 (defun gnus-summary-read-group (group &optional show-all no-article
3415                                       kill-buffer no-display backward
3416                                       select-articles)
3417   "Start reading news in newsgroup GROUP.
3418 If SHOW-ALL is non-nil, already read articles are also listed.
3419 If NO-ARTICLE is non-nil, no article is selected initially.
3420 If NO-DISPLAY, don't generate a summary buffer."
3421   (let (result)
3422     (while (and group
3423                 (null (setq result
3424                             (let ((gnus-auto-select-next nil))
3425                               (or (gnus-summary-read-group-1
3426                                    group show-all no-article
3427                                    kill-buffer no-display
3428                                    select-articles)
3429                                   (setq show-all nil
3430                                         select-articles nil)))))
3431                 (eq gnus-auto-select-next 'quietly))
3432       (set-buffer gnus-group-buffer)
3433       ;; The entry function called above goes to the next
3434       ;; group automatically, so we go two groups back
3435       ;; if we are searching for the previous group.
3436       (when backward
3437         (gnus-group-prev-unread-group 2))
3438       (if (not (equal group (gnus-group-group-name)))
3439           (setq group (gnus-group-group-name))
3440         (setq group nil)))
3441     result))
3442
3443 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3444   "Directly jump to the other GROUP from summary buffer.
3445 If SHOW-ALL is non-nil, already read articles are also listed."
3446   (interactive
3447    (if (eq gnus-summary-buffer (current-buffer))
3448        (list (completing-read
3449               "Group: " gnus-active-hashtb nil t
3450               (when (and gnus-newsgroup-name
3451                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3452                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3453               'gnus-group-history)
3454              current-prefix-arg)
3455      (error "%s must be invoked from a gnus summary buffer." this-command)))
3456   (unless (or (zerop (length group))
3457               (and gnus-newsgroup-name
3458                    (string-equal gnus-newsgroup-name group)))
3459     (gnus-summary-exit)
3460     (gnus-summary-read-group group show-all
3461                              gnus-dont-select-after-jump-to-other-group)))
3462
3463 (defun gnus-summary-read-group-1 (group show-all no-article
3464                                         kill-buffer no-display
3465                                         &optional select-articles)
3466   ;; Killed foreign groups can't be entered.
3467   ;;  (when (and (not (gnus-group-native-p group))
3468   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3469   ;;    (error "Dead non-native groups can't be entered"))
3470   (gnus-message 5 "Retrieving newsgroup: %s..."
3471                 (gnus-group-decoded-name group))
3472   (let* ((new-group (gnus-summary-setup-buffer group))
3473          (quit-config (gnus-group-quit-config group))
3474          (did-select (and new-group (gnus-select-newsgroup
3475                                      group show-all select-articles))))
3476     (cond
3477      ;; This summary buffer exists already, so we just select it.
3478      ((not new-group)
3479       (gnus-set-global-variables)
3480       (when kill-buffer
3481         (gnus-kill-or-deaden-summary kill-buffer))
3482       (gnus-configure-windows 'summary 'force)
3483       (gnus-set-mode-line 'summary)
3484       (gnus-summary-position-point)
3485       (message "")
3486       t)
3487      ;; We couldn't select this group.
3488      ((null did-select)
3489       (when (and (eq major-mode 'gnus-summary-mode)
3490                  (not (equal (current-buffer) kill-buffer)))
3491         (kill-buffer (current-buffer))
3492         (if (not quit-config)
3493             (progn
3494               ;; Update the info -- marks might need to be removed,
3495               ;; for instance.
3496               (gnus-summary-update-info)
3497               (set-buffer gnus-group-buffer)
3498               (gnus-group-jump-to-group group)
3499               (gnus-group-next-unread-group 1))
3500           (gnus-handle-ephemeral-exit quit-config)))
3501       (let ((grpinfo (gnus-get-info group)))
3502         (if (null (gnus-info-read grpinfo))
3503             (gnus-message 3 "Group %s contains no messages"
3504                           (gnus-group-decoded-name group))
3505           (gnus-message 3 "Can't select group")))
3506       nil)
3507      ;; The user did a `C-g' while prompting for number of articles,
3508      ;; so we exit this group.
3509      ((eq did-select 'quit)
3510       (and (eq major-mode 'gnus-summary-mode)
3511            (not (equal (current-buffer) kill-buffer))
3512            (kill-buffer (current-buffer)))
3513       (when kill-buffer
3514         (gnus-kill-or-deaden-summary kill-buffer))
3515       (if (not quit-config)
3516           (progn
3517             (set-buffer gnus-group-buffer)
3518             (gnus-group-jump-to-group group)
3519             (gnus-group-next-unread-group 1)
3520             (gnus-configure-windows 'group 'force))
3521         (gnus-handle-ephemeral-exit quit-config))
3522       ;; Finally signal the quit.
3523       (signal 'quit nil))
3524      ;; The group was successfully selected.
3525      (t
3526       (gnus-set-global-variables)
3527       ;; Save the active value in effect when the group was entered.
3528       (setq gnus-newsgroup-active
3529             (gnus-copy-sequence
3530              (gnus-active gnus-newsgroup-name)))
3531       ;; You can change the summary buffer in some way with this hook.
3532       (gnus-run-hooks 'gnus-select-group-hook)
3533       (gnus-update-format-specifications
3534        nil 'summary 'summary-mode 'summary-dummy)
3535       (gnus-update-summary-mark-positions)
3536       ;; Do score processing.
3537       (when gnus-use-scoring
3538         (gnus-possibly-score-headers))
3539       ;; Check whether to fill in the gaps in the threads.
3540       (when gnus-build-sparse-threads
3541         (gnus-build-sparse-threads))
3542       ;; Find the initial limit.
3543       (if gnus-show-threads
3544           (if show-all
3545               (let ((gnus-newsgroup-dormant nil))
3546                 (gnus-summary-initial-limit show-all))
3547             (gnus-summary-initial-limit show-all))
3548         ;; When unthreaded, all articles are always shown.
3549         (setq gnus-newsgroup-limit
3550               (mapcar
3551                (lambda (header) (mail-header-number header))
3552                gnus-newsgroup-headers)))
3553       ;; Generate the summary buffer.
3554       (unless no-display
3555         (gnus-summary-prepare))
3556       (when gnus-use-trees
3557         (gnus-tree-open group)
3558         (setq gnus-summary-highlight-line-function
3559               'gnus-tree-highlight-article))
3560       ;; If the summary buffer is empty, but there are some low-scored
3561       ;; articles or some excluded dormants, we include these in the
3562       ;; buffer.
3563       (when (and (zerop (buffer-size))
3564                  (not no-display))
3565         (cond (gnus-newsgroup-dormant
3566                (gnus-summary-limit-include-dormant))
3567               ((and gnus-newsgroup-scored show-all)
3568                (gnus-summary-limit-include-expunged t))))
3569       ;; Function `gnus-apply-kill-file' must be called in this hook.
3570       (gnus-run-hooks 'gnus-apply-kill-hook)
3571       (if (and (zerop (buffer-size))
3572                (not no-display))
3573           (progn
3574             ;; This newsgroup is empty.
3575             (gnus-summary-catchup-and-exit nil t)
3576             (gnus-message 6 "No unread news")
3577             (when kill-buffer
3578               (gnus-kill-or-deaden-summary kill-buffer))
3579             ;; Return nil from this function.
3580             nil)
3581         ;; Hide conversation thread subtrees.  We cannot do this in
3582         ;; gnus-summary-prepare-hook since kill processing may not
3583         ;; work with hidden articles.
3584         (gnus-summary-maybe-hide-threads)
3585         (when kill-buffer
3586           (gnus-kill-or-deaden-summary kill-buffer))
3587         (gnus-summary-auto-select-subject)
3588         ;; Show first unread article if requested.
3589         (if (and (not no-article)
3590                  (not no-display)
3591                  gnus-newsgroup-unreads
3592                  gnus-auto-select-first)
3593             (progn
3594               (gnus-configure-windows 'summary)
3595               (let ((art (gnus-summary-article-number)))
3596                 (unless (and (not gnus-plugged)
3597                              (or (memq art gnus-newsgroup-undownloaded)
3598                                  (memq art gnus-newsgroup-downloadable)))
3599                   (gnus-summary-goto-article art))))
3600           ;; Don't select any articles.
3601           (gnus-summary-position-point)
3602           (gnus-configure-windows 'summary 'force)
3603           (gnus-set-mode-line 'summary))
3604         (when (and gnus-auto-center-group
3605                    (get-buffer-window gnus-group-buffer t))
3606           ;; Gotta use windows, because recenter does weird stuff if
3607           ;; the current buffer ain't the displayed window.
3608           (let ((owin (selected-window)))
3609             (select-window (get-buffer-window gnus-group-buffer t))
3610             (when (gnus-group-goto-group group)
3611               (recenter))
3612             (select-window owin)))
3613         ;; Mark this buffer as "prepared".
3614         (setq gnus-newsgroup-prepared t)
3615         (gnus-run-hooks 'gnus-summary-prepared-hook)
3616         (unless (gnus-ephemeral-group-p group)
3617           (gnus-group-update-group group))
3618         t)))))
3619
3620 (defun gnus-summary-auto-select-subject ()
3621   "Select the subject line on initial group entry."
3622   (goto-char (point-min))
3623   (cond
3624    ((eq gnus-auto-select-subject 'best)
3625     (gnus-summary-best-unread-subject))
3626    ((eq gnus-auto-select-subject 'unread)
3627     (gnus-summary-first-unread-subject))
3628    ((eq gnus-auto-select-subject 'unseen)
3629     (gnus-summary-first-unseen-subject))
3630    ((eq gnus-auto-select-subject 'unseen-or-unread)
3631     (gnus-summary-first-unseen-or-unread-subject))
3632    ((eq gnus-auto-select-subject 'first)
3633     ;; Do nothing.
3634     )
3635    ((functionp gnus-auto-select-subject)
3636     (funcall gnus-auto-select-subject))))
3637
3638 (defun gnus-summary-prepare ()
3639   "Generate the summary buffer."
3640   (interactive)
3641   (let ((buffer-read-only nil))
3642     (erase-buffer)
3643     (setq gnus-newsgroup-data nil
3644           gnus-newsgroup-data-reverse nil)
3645     (gnus-run-hooks 'gnus-summary-generate-hook)
3646     ;; Generate the buffer, either with threads or without.
3647     (when gnus-newsgroup-headers
3648       (gnus-summary-prepare-threads
3649        (if gnus-show-threads
3650            (gnus-sort-gathered-threads
3651             (funcall gnus-summary-thread-gathering-function
3652                      (gnus-sort-threads
3653                       (gnus-cut-threads (gnus-make-threads)))))
3654          ;; Unthreaded display.
3655          (gnus-sort-articles gnus-newsgroup-headers))))
3656     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3657     ;; Call hooks for modifying summary buffer.
3658     (goto-char (point-min))
3659     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3660
3661 (defsubst gnus-general-simplify-subject (subject)
3662   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3663   (setq subject
3664         (cond
3665          ;; Truncate the subject.
3666          (gnus-simplify-subject-functions
3667           (gnus-map-function gnus-simplify-subject-functions subject))
3668          ((numberp gnus-summary-gather-subject-limit)
3669           (setq subject (gnus-simplify-subject-re subject))
3670           (if (> (length subject) gnus-summary-gather-subject-limit)
3671               (substring subject 0 gnus-summary-gather-subject-limit)
3672             subject))
3673          ;; Fuzzily simplify it.
3674          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3675           (gnus-simplify-subject-fuzzy subject))
3676          ;; Just remove the leading "Re:".
3677          (t
3678           (gnus-simplify-subject-re subject))))
3679
3680   (if (and gnus-summary-gather-exclude-subject
3681            (string-match gnus-summary-gather-exclude-subject subject))
3682       nil                               ; This article shouldn't be gathered
3683     subject))
3684
3685 (defun gnus-summary-simplify-subject-query ()
3686   "Query where the respool algorithm would put this article."
3687   (interactive)
3688   (gnus-summary-select-article)
3689   (message "%s"
3690            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3691
3692 (defun gnus-gather-threads-by-subject (threads)
3693   "Gather threads by looking at Subject headers."
3694   (if (not gnus-summary-make-false-root)
3695       threads
3696     (let ((hashtb (gnus-make-hashtable 1024))
3697           (prev threads)
3698           (result threads)
3699           subject hthread whole-subject)
3700       (while threads
3701         (setq subject (gnus-general-simplify-subject
3702                        (setq whole-subject (mail-header-subject
3703                                             (caar threads)))))
3704         (when subject
3705           (if (setq hthread (gnus-gethash subject hashtb))
3706               (progn
3707                 ;; We enter a dummy root into the thread, if we
3708                 ;; haven't done that already.
3709                 (unless (stringp (caar hthread))
3710                   (setcar hthread (list whole-subject (car hthread))))
3711                 ;; We add this new gathered thread to this gathered
3712                 ;; thread.
3713                 (setcdr (car hthread)
3714                         (nconc (cdar hthread) (list (car threads))))
3715                 ;; Remove it from the list of threads.
3716                 (setcdr prev (cdr threads))
3717                 (setq threads prev))
3718             ;; Enter this thread into the hash table.
3719             (gnus-sethash subject
3720                           (if gnus-summary-make-false-root-always
3721                               (progn
3722                                 ;; If you want a dummy root above all
3723                                 ;; threads...
3724                                 (setcar threads (list whole-subject
3725                                                       (car threads)))
3726                                 threads)
3727                             threads)
3728                           hashtb)))
3729         (setq prev threads)
3730         (setq threads (cdr threads)))
3731       result)))
3732
3733 (defun gnus-gather-threads-by-references (threads)
3734   "Gather threads by looking at References headers."
3735   (let ((idhashtb (gnus-make-hashtable 1024))
3736         (thhashtb (gnus-make-hashtable 1024))
3737         (prev threads)
3738         (result threads)
3739         ids references id gthread gid entered ref)
3740     (while threads
3741       (when (setq references (mail-header-references (caar threads)))
3742         (setq id (mail-header-id (caar threads))
3743               ids (inline (gnus-split-references references))
3744               entered nil)
3745         (while (setq ref (pop ids))
3746           (setq ids (delete ref ids))
3747           (if (not (setq gid (gnus-gethash ref idhashtb)))
3748               (progn
3749                 (gnus-sethash ref id idhashtb)
3750                 (gnus-sethash id threads thhashtb))
3751             (setq gthread (gnus-gethash gid thhashtb))
3752             (unless entered
3753               ;; We enter a dummy root into the thread, if we
3754               ;; haven't done that already.
3755               (unless (stringp (caar gthread))
3756                 (setcar gthread (list (mail-header-subject (caar gthread))
3757                                       (car gthread))))
3758               ;; We add this new gathered thread to this gathered
3759               ;; thread.
3760               (setcdr (car gthread)
3761                       (nconc (cdar gthread) (list (car threads)))))
3762             ;; Add it into the thread hash table.
3763             (gnus-sethash id gthread thhashtb)
3764             (setq entered t)
3765             ;; Remove it from the list of threads.
3766             (setcdr prev (cdr threads))
3767             (setq threads prev))))
3768       (setq prev threads)
3769       (setq threads (cdr threads)))
3770     result))
3771
3772 (defun gnus-sort-gathered-threads (threads)
3773   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3774   (let ((result threads))
3775     (while threads
3776       (when (stringp (caar threads))
3777         (setcdr (car threads)
3778                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3779       (setq threads (cdr threads)))
3780     result))
3781
3782 (defun gnus-thread-loop-p (root thread)
3783   "Say whether ROOT is in THREAD."
3784   (let ((stack (list thread))
3785         (infloop 0)
3786         th)
3787     (while (setq thread (pop stack))
3788       (setq th (cdr thread))
3789       (while (and th
3790                   (not (eq (caar th) root)))
3791         (pop th))
3792       (if th
3793           ;; We have found a loop.
3794           (let (ref-dep)
3795             (setcdr thread (delq (car th) (cdr thread)))
3796             (if (boundp (setq ref-dep (intern "none"
3797                                               gnus-newsgroup-dependencies)))
3798                 (setcdr (symbol-value ref-dep)
3799                         (nconc (cdr (symbol-value ref-dep))
3800                                (list (car th))))
3801               (set ref-dep (list nil (car th))))
3802             (setq infloop 1
3803                   stack nil))
3804         ;; Push all the subthreads onto the stack.
3805         (push (cdr thread) stack)))
3806     infloop))
3807
3808 (defun gnus-make-threads ()
3809   "Go through the dependency hashtb and find the roots.  Return all threads."
3810   (let (threads)
3811     (while (catch 'infloop
3812              (mapatoms
3813               (lambda (refs)
3814                 ;; Deal with self-referencing References loops.
3815                 (when (and (car (symbol-value refs))
3816                            (not (zerop
3817                                  (apply
3818                                   '+
3819                                   (mapcar
3820                                    (lambda (thread)
3821                                      (gnus-thread-loop-p
3822                                       (car (symbol-value refs)) thread))
3823                                    (cdr (symbol-value refs)))))))
3824                   (setq threads nil)
3825                   (throw 'infloop t))
3826                 (unless (car (symbol-value refs))
3827                   ;; These threads do not refer back to any other articles,
3828                   ;; so they're roots.
3829                   (setq threads (append (cdr (symbol-value refs)) threads))))
3830               gnus-newsgroup-dependencies)))
3831     threads))
3832
3833 ;; Build the thread tree.
3834 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3835   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3836
3837 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3838 if it was already present.
3839
3840 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3841 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3842 Message-IDs will be renamed to a unique Message-ID before being
3843 entered.
3844
3845 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3846   (let* ((id (mail-header-id header))
3847          (id-dep (and id (intern id dependencies)))
3848          parent-id ref ref-dep ref-header replaced)
3849     ;; Enter this `header' in the `dependencies' table.
3850     (cond
3851      ((not id-dep)
3852       (setq header nil))
3853      ;; The first two cases do the normal part: enter a new `header'
3854      ;; in the `dependencies' table.
3855      ((not (boundp id-dep))
3856       (set id-dep (list header)))
3857      ((null (car (symbol-value id-dep)))
3858       (setcar (symbol-value id-dep) header))
3859
3860      ;; From here the `header' was already present in the
3861      ;; `dependencies' table.
3862      (force-new
3863       ;; Overrides an existing entry;
3864       ;; just set the header part of the entry.
3865       (setcar (symbol-value id-dep) header)
3866       (setq replaced t))
3867
3868      ;; Renames the existing `header' to a unique Message-ID.
3869      ((not gnus-summary-ignore-duplicates)
3870       ;; An article with this Message-ID has already been seen.
3871       ;; We rename the Message-ID.
3872       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3873            (list header))
3874       (mail-header-set-id header id))
3875
3876      ;; The last case ignores an existing entry, except it adds any
3877      ;; additional Xrefs (in case the two articles came from different
3878      ;; servers.
3879      ;; Also sets `header' to `nil' meaning that the `dependencies'
3880      ;; table was *not* modified.
3881      (t
3882       (mail-header-set-xref
3883        (car (symbol-value id-dep))
3884        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3885                    "")
3886                (or (mail-header-xref header) "")))
3887       (setq header nil)))
3888
3889     (when (and header (not replaced))
3890       ;; First check that we are not creating a References loop.
3891       (setq parent-id (gnus-parent-id (mail-header-references header)))
3892       (setq ref parent-id)
3893       (while (and ref
3894                   (setq ref-dep (intern-soft ref dependencies))
3895                   (boundp ref-dep)
3896                   (setq ref-header (car (symbol-value ref-dep))))
3897         (if (string= id ref)
3898             ;; Yuk!  This is a reference loop.  Make the article be a
3899             ;; root article.
3900             (progn
3901               (mail-header-set-references (car (symbol-value id-dep)) "none")
3902               (setq ref nil)
3903               (setq parent-id nil))
3904           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3905       (setq ref-dep (intern (or parent-id "none") dependencies))
3906       (if (boundp ref-dep)
3907           (setcdr (symbol-value ref-dep)
3908                   (nconc (cdr (symbol-value ref-dep))
3909                          (list (symbol-value id-dep))))
3910         (set ref-dep (list nil (symbol-value id-dep)))))
3911     header))
3912
3913 (defun gnus-extract-message-id-from-in-reply-to (string)
3914   (if (string-match "<[^>]+>" string)
3915       (substring string (match-beginning 0) (match-end 0))
3916     nil))
3917
3918 (defun gnus-build-sparse-threads ()
3919   (let ((headers gnus-newsgroup-headers)
3920         (mail-parse-charset gnus-newsgroup-charset)
3921         (gnus-summary-ignore-duplicates t)
3922         header references generation relations
3923         subject child end new-child date)
3924     ;; First we create an alist of generations/relations, where
3925     ;; generations is how much we trust the relation, and the relation
3926     ;; is parent/child.
3927     (gnus-message 7 "Making sparse threads...")
3928     (save-excursion
3929       (nnheader-set-temp-buffer " *gnus sparse threads*")
3930       (while (setq header (pop headers))
3931         (when (and (setq references (mail-header-references header))
3932                    (not (string= references "")))
3933           (insert references)
3934           (setq child (mail-header-id header)
3935                 subject (mail-header-subject header)
3936                 date (mail-header-date header)
3937                 generation 0)
3938           (while (search-backward ">" nil t)
3939             (setq end (1+ (point)))
3940             (when (search-backward "<" nil t)
3941               (setq new-child (buffer-substring (point) end))
3942               (push (list (incf generation)
3943                           child (setq child new-child)
3944                           subject date)
3945                     relations)))
3946           (when child
3947             (push (list (1+ generation) child nil subject) relations))
3948           (erase-buffer)))
3949       (kill-buffer (current-buffer)))
3950     ;; Sort over trustworthiness.
3951     (mapcar
3952      (lambda (relation)
3953        (when (gnus-dependencies-add-header
3954               (make-full-mail-header-from-decoded-header
3955                gnus-reffed-article-number
3956                (nth 3 relation) "" (or (nth 4 relation) "")
3957                (nth 1 relation)
3958                (or (nth 2 relation) "") 0 0 "")
3959               gnus-newsgroup-dependencies nil)
3960          (push gnus-reffed-article-number gnus-newsgroup-limit)
3961          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3962          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3963                gnus-newsgroup-reads)
3964          (decf gnus-reffed-article-number)))
3965      (sort relations 'car-less-than-car))
3966     (gnus-message 7 "Making sparse threads...done")))
3967
3968 (defun gnus-build-old-threads ()
3969   ;; Look at all the articles that refer back to old articles, and
3970   ;; fetch the headers for the articles that aren't there.  This will
3971   ;; build complete threads - if the roots haven't been expired by the
3972   ;; server, that is.
3973   (let ((mail-parse-charset gnus-newsgroup-charset)
3974         id heads)
3975     (mapatoms
3976      (lambda (refs)
3977        (when (not (car (symbol-value refs)))
3978          (setq heads (cdr (symbol-value refs)))
3979          (while heads
3980            (if (memq (mail-header-number (caar heads))
3981                      gnus-newsgroup-dormant)
3982                (setq heads (cdr heads))
3983              (setq id (symbol-name refs))
3984              (while (and (setq id (gnus-build-get-header id))
3985                          (not (car (gnus-id-to-thread id)))))
3986              (setq heads nil)))))
3987      gnus-newsgroup-dependencies)))
3988
3989 (defsubst gnus-remove-odd-characters (string)
3990   "Translate STRING into something that doesn't contain weird characters."
3991   (mm-subst-char-in-string
3992    ?\r ?\-
3993    (mm-subst-char-in-string ?\n ?\- string t) t))
3994
3995 ;; This function has to be called with point after the article number
3996 ;; on the beginning of the line.
3997 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3998   (let ((eol (point-at-eol))
3999         (buffer (current-buffer))
4000         header references in-reply-to)
4001
4002     ;; overview: [num subject from date id refs chars lines misc]
4003     (unwind-protect
4004         (progn
4005           (narrow-to-region (point) eol)
4006           (unless (eobp)
4007             (forward-char))
4008
4009           (setq header
4010                 (make-full-mail-header
4011                  number                         ; number
4012                  (nnheader-nov-field)           ; subject
4013                  (nnheader-nov-field)           ; from
4014                  (nnheader-nov-field)           ; date
4015                  (nnheader-nov-read-message-id) ; id
4016                  (nnheader-nov-field)           ; refs
4017                  (nnheader-nov-read-integer)    ; chars
4018                  (nnheader-nov-read-integer)    ; lines
4019                  (unless (eobp)
4020                    (if (looking-at "Xref: ")
4021                        (goto-char (match-end 0)))
4022                    (nnheader-nov-field))        ; Xref
4023                  (nnheader-nov-parse-extra))))  ; extra
4024
4025       (widen))
4026
4027     (when (and (string= references "")
4028                (setq in-reply-to (mail-header-extra header))
4029                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4030       (mail-header-set-references
4031        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4032
4033     (when gnus-alter-header-function
4034       (funcall gnus-alter-header-function header))
4035     (gnus-dependencies-add-header header dependencies force-new)))
4036
4037 (defun gnus-build-get-header (id)
4038   "Look through the buffer of NOV lines and find the header to ID.
4039 Enter this line into the dependencies hash table, and return
4040 the id of the parent article (if any)."
4041   (let ((deps gnus-newsgroup-dependencies)
4042         found header)
4043     (prog1
4044         (save-excursion
4045           (set-buffer nntp-server-buffer)
4046           (let ((case-fold-search nil))
4047             (goto-char (point-min))
4048             (while (and (not found)
4049                         (search-forward id nil t))
4050               (beginning-of-line)
4051               (setq found (looking-at
4052                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4053                                    (regexp-quote id))))
4054               (or found (beginning-of-line 2)))
4055             (when found
4056               (beginning-of-line)
4057               (and
4058                (setq header (gnus-nov-parse-line
4059                              (read (current-buffer)) deps))
4060                (gnus-parent-id (mail-header-references header))))))
4061       (when header
4062         (let ((number (mail-header-number header)))
4063           (push number gnus-newsgroup-limit)
4064           (push header gnus-newsgroup-headers)
4065           (if (memq number gnus-newsgroup-unselected)
4066               (progn
4067                 (setq gnus-newsgroup-unreads
4068                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4069                                                number))
4070                 (setq gnus-newsgroup-unselected
4071                       (delq number gnus-newsgroup-unselected)))
4072             (push number gnus-newsgroup-ancient)))))))
4073
4074 (defun gnus-build-all-threads ()
4075   "Read all the headers."
4076   (let ((gnus-summary-ignore-duplicates t)
4077         (mail-parse-charset gnus-newsgroup-charset)
4078         (dependencies gnus-newsgroup-dependencies)
4079         header article)
4080     (save-excursion
4081       (set-buffer nntp-server-buffer)
4082       (let ((case-fold-search nil))
4083         (goto-char (point-min))
4084         (while (not (eobp))
4085           (ignore-errors
4086             (setq article (read (current-buffer))
4087                   header (gnus-nov-parse-line article dependencies)))
4088           (when header
4089             (save-excursion
4090               (set-buffer gnus-summary-buffer)
4091               (push header gnus-newsgroup-headers)
4092               (if (memq (setq article (mail-header-number header))
4093                         gnus-newsgroup-unselected)
4094                   (progn
4095                     (setq gnus-newsgroup-unreads
4096                           (gnus-add-to-sorted-list
4097                            gnus-newsgroup-unreads article))
4098                     (setq gnus-newsgroup-unselected
4099                           (delq article gnus-newsgroup-unselected)))
4100                 (push article gnus-newsgroup-ancient)))
4101             (forward-line 1)))))))
4102
4103 (defun gnus-summary-update-article-line (article header)
4104   "Update the line for ARTICLE using HEADER."
4105   (let* ((id (mail-header-id header))
4106          (thread (gnus-id-to-thread id)))
4107     (unless thread
4108       (error "Article in no thread"))
4109     ;; Update the thread.
4110     (setcar thread header)
4111     (gnus-summary-goto-subject article)
4112     (let* ((datal (gnus-data-find-list article))
4113            (data (car datal))
4114            (buffer-read-only nil)
4115            (level (gnus-summary-thread-level)))
4116       (gnus-delete-line)
4117       (let ((inserted (- (point)
4118                          (progn
4119                            (gnus-summary-insert-line
4120                             header level nil
4121                             (memq article gnus-newsgroup-undownloaded)
4122                             (gnus-article-mark article)
4123                             (memq article gnus-newsgroup-replied)
4124                             (memq article gnus-newsgroup-expirable)
4125                             ;; Only insert the Subject string when it's different
4126                             ;; from the previous Subject string.
4127                             (if (and
4128                                  gnus-show-threads
4129                                  (gnus-subject-equal
4130                                   (condition-case ()
4131                                       (mail-header-subject
4132                                        (gnus-data-header
4133                                         (cadr
4134                                          (gnus-data-find-list
4135                                           article
4136                                           (gnus-data-list t)))))
4137                                     ;; Error on the side of excessive subjects.
4138                                     (error ""))
4139                                   (mail-header-subject header)))
4140                                 ""
4141                               (mail-header-subject header))
4142                             nil (cdr (assq article gnus-newsgroup-scored))
4143                             (memq article gnus-newsgroup-processable))
4144                            (point)))))
4145         (when (cdr datal)
4146           (gnus-data-update-list
4147            (cdr datal)
4148            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4149
4150 (defun gnus-summary-update-article (article &optional iheader)
4151   "Update ARTICLE in the summary buffer."
4152   (set-buffer gnus-summary-buffer)
4153   (let* ((header (gnus-summary-article-header article))
4154          (id (mail-header-id header))
4155          (data (gnus-data-find article))
4156          (thread (gnus-id-to-thread id))
4157          (references (mail-header-references header))
4158          (parent
4159           (gnus-id-to-thread
4160            (or (gnus-parent-id
4161                 (when (and references
4162                            (not (equal "" references)))
4163                   references))
4164                "none")))
4165          (buffer-read-only nil)
4166          (old (car thread)))
4167     (when thread
4168       (unless iheader
4169         (setcar thread nil)
4170         (when parent
4171           (delq thread parent)))
4172       (if (gnus-summary-insert-subject id header)
4173           ;; Set the (possibly) new article number in the data structure.
4174           (gnus-data-set-number data (gnus-id-to-article id))
4175         (setcar thread old)
4176         nil))))
4177
4178 (defun gnus-rebuild-thread (id &optional line)
4179   "Rebuild the thread containing ID.
4180 If LINE, insert the rebuilt thread starting on line LINE."
4181   (let ((buffer-read-only nil)
4182         old-pos current thread data)
4183     (if (not gnus-show-threads)
4184         (setq thread (list (car (gnus-id-to-thread id))))
4185       ;; Get the thread this article is part of.
4186       (setq thread (gnus-remove-thread id)))
4187     (setq old-pos (point-at-bol))
4188     (setq current (save-excursion
4189                     (and (re-search-backward "[\r\n]" nil t)
4190                          (gnus-summary-article-number))))
4191     ;; If this is a gathered thread, we have to go some re-gathering.
4192     (when (stringp (car thread))
4193       (let ((subject (car thread))
4194             roots thr)
4195         (setq thread (cdr thread))
4196         (while thread
4197           (unless (memq (setq thr (gnus-id-to-thread
4198                                    (gnus-root-id
4199                                     (mail-header-id (caar thread)))))
4200                         roots)
4201             (push thr roots))
4202           (setq thread (cdr thread)))
4203         ;; We now have all (unique) roots.
4204         (if (= (length roots) 1)
4205             ;; All the loose roots are now one solid root.
4206             (setq thread (car roots))
4207           (setq thread (cons subject (gnus-sort-threads roots))))))
4208     (let (threads)
4209       ;; We then insert this thread into the summary buffer.
4210       (when line
4211         (goto-char (point-min))
4212         (forward-line (1- line)))
4213       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4214         (if gnus-show-threads
4215             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4216           (gnus-summary-prepare-unthreaded thread))
4217         (setq data (nreverse gnus-newsgroup-data))
4218         (setq threads gnus-newsgroup-threads))
4219       ;; We splice the new data into the data structure.
4220       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4221       ;;!!! then we want to insert at the beginning of the buffer.
4222       ;;!!! That happens to be true with Gnus now, but that may
4223       ;;!!! change in the future.  Perhaps.
4224       (gnus-data-enter-list
4225        (if line nil current) data (- (point) old-pos))
4226       (setq gnus-newsgroup-threads
4227             (nconc threads gnus-newsgroup-threads))
4228       (gnus-data-compute-positions))))
4229
4230 (defun gnus-number-to-header (number)
4231   "Return the header for article NUMBER."
4232   (let ((headers gnus-newsgroup-headers))
4233     (while (and headers
4234                 (not (= number (mail-header-number (car headers)))))
4235       (pop headers))
4236     (when headers
4237       (car headers))))
4238
4239 (defun gnus-parent-headers (in-headers &optional generation)
4240   "Return the headers of the GENERATIONeth parent of HEADERS."
4241   (unless generation
4242     (setq generation 1))
4243   (let ((parent t)
4244         (headers in-headers)
4245         references)
4246     (while (and parent
4247                 (not (zerop generation))
4248                 (setq references (mail-header-references headers)))
4249       (setq headers (if (and references
4250                              (setq parent (gnus-parent-id references)))
4251                         (car (gnus-id-to-thread parent))
4252                       nil))
4253       (decf generation))
4254     (and (not (eq headers in-headers))
4255          headers)))
4256
4257 (defun gnus-id-to-thread (id)
4258   "Return the (sub-)thread where ID appears."
4259   (gnus-gethash id gnus-newsgroup-dependencies))
4260
4261 (defun gnus-id-to-article (id)
4262   "Return the article number of ID."
4263   (let ((thread (gnus-id-to-thread id)))
4264     (when (and thread
4265                (car thread))
4266       (mail-header-number (car thread)))))
4267
4268 (defun gnus-id-to-header (id)
4269   "Return the article headers of ID."
4270   (car (gnus-id-to-thread id)))
4271
4272 (defun gnus-article-displayed-root-p (article)
4273   "Say whether ARTICLE is a root(ish) article."
4274   (let ((level (gnus-summary-thread-level article))
4275         (refs (mail-header-references  (gnus-summary-article-header article)))
4276         particle)
4277     (cond
4278      ((null level) nil)
4279      ((zerop level) t)
4280      ((null refs) t)
4281      ((null (gnus-parent-id refs)) t)
4282      ((and (= 1 level)
4283            (null (setq particle (gnus-id-to-article
4284                                  (gnus-parent-id refs))))
4285            (null (gnus-summary-thread-level particle)))))))
4286
4287 (defun gnus-root-id (id)
4288   "Return the id of the root of the thread where ID appears."
4289   (let (last-id prev)
4290     (while (and id (setq prev (car (gnus-id-to-thread id))))
4291       (setq last-id id
4292             id (gnus-parent-id (mail-header-references prev))))
4293     last-id))
4294
4295 (defun gnus-articles-in-thread (thread)
4296   "Return the list of articles in THREAD."
4297   (cons (mail-header-number (car thread))
4298         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4299
4300 (defun gnus-remove-thread (id &optional dont-remove)
4301   "Remove the thread that has ID in it."
4302   (let (headers thread last-id)
4303     ;; First go up in this thread until we find the root.
4304     (setq last-id (gnus-root-id id)
4305           headers (message-flatten-list (gnus-id-to-thread last-id)))
4306     ;; We have now found the real root of this thread.  It might have
4307     ;; been gathered into some loose thread, so we have to search
4308     ;; through the threads to find the thread we wanted.
4309     (let ((threads gnus-newsgroup-threads)
4310           sub)
4311       (while threads
4312         (setq sub (car threads))
4313         (if (stringp (car sub))
4314             ;; This is a gathered thread, so we look at the roots
4315             ;; below it to find whether this article is in this
4316             ;; gathered root.
4317             (progn
4318               (setq sub (cdr sub))
4319               (while sub
4320                 (when (member (caar sub) headers)
4321                   (setq thread (car threads)
4322                         threads nil
4323                         sub nil))
4324                 (setq sub (cdr sub))))
4325           ;; It's an ordinary thread, so we check it.
4326           (when (eq (car sub) (car headers))
4327             (setq thread sub
4328                   threads nil)))
4329         (setq threads (cdr threads)))
4330       ;; If this article is in no thread, then it's a root.
4331       (if thread
4332           (unless dont-remove
4333             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4334         (setq thread (gnus-id-to-thread last-id)))
4335       (when thread
4336         (prog1
4337             thread                      ; We return this thread.
4338           (unless dont-remove
4339             (if (stringp (car thread))
4340                 (progn
4341                   ;; If we use dummy roots, then we have to remove the
4342                   ;; dummy root as well.
4343                   (when (eq gnus-summary-make-false-root 'dummy)
4344                     ;; We go to the dummy root by going to
4345                     ;; the first sub-"thread", and then one line up.
4346                     (gnus-summary-goto-article
4347                      (mail-header-number (caadr thread)))
4348                     (forward-line -1)
4349                     (gnus-delete-line)
4350                     (gnus-data-compute-positions))
4351                   (setq thread (cdr thread))
4352                   (while thread
4353                     (gnus-remove-thread-1 (car thread))
4354                     (setq thread (cdr thread))))
4355               (gnus-remove-thread-1 thread))))))))
4356
4357 (defun gnus-remove-thread-1 (thread)
4358   "Remove the thread THREAD recursively."
4359   (let ((number (mail-header-number (pop thread)))
4360         d)
4361     (setq thread (reverse thread))
4362     (while thread
4363       (gnus-remove-thread-1 (pop thread)))
4364     (when (setq d (gnus-data-find number))
4365       (goto-char (gnus-data-pos d))
4366       (gnus-summary-show-thread)
4367       (gnus-data-remove
4368        number
4369        (- (point-at-bol)
4370           (prog1
4371               (1+ (point-at-eol))
4372             (gnus-delete-line)))))))
4373
4374 (defun gnus-sort-threads-1 (threads func)
4375   (sort (mapcar (lambda (thread)
4376                   (cons (car thread)
4377                         (and (cdr thread)
4378                              (gnus-sort-threads-1 (cdr thread) func))))
4379                 threads) func))
4380
4381 (defun gnus-sort-threads (threads)
4382   "Sort THREADS."
4383   (if (not gnus-thread-sort-functions)
4384       threads
4385     (gnus-message 8 "Sorting threads...")
4386     (let ((max-lisp-eval-depth 5000))
4387       (prog1 (gnus-sort-threads-1
4388          threads
4389          (gnus-make-sort-function gnus-thread-sort-functions))
4390         (gnus-message 8 "Sorting threads...done")))))
4391
4392 (defun gnus-sort-articles (articles)
4393   "Sort ARTICLES."
4394   (when gnus-article-sort-functions
4395     (gnus-message 7 "Sorting articles...")
4396     (prog1
4397         (setq gnus-newsgroup-headers
4398               (sort articles (gnus-make-sort-function
4399                               gnus-article-sort-functions)))
4400       (gnus-message 7 "Sorting articles...done"))))
4401
4402 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4403 (defmacro gnus-thread-header (thread)
4404   "Return header of first article in THREAD.
4405 Note that THREAD must never, ever be anything else than a variable -
4406 using some other form will lead to serious barfage."
4407   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4408   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4409   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4410         (vector thread) 2))
4411
4412 (defsubst gnus-article-sort-by-number (h1 h2)
4413   "Sort articles by article number."
4414   (< (mail-header-number h1)
4415      (mail-header-number h2)))
4416
4417 (defun gnus-thread-sort-by-number (h1 h2)
4418   "Sort threads by root article number."
4419   (gnus-article-sort-by-number
4420    (gnus-thread-header h1) (gnus-thread-header h2)))
4421
4422 (defsubst gnus-article-sort-by-random (h1 h2)
4423   "Sort articles by article number."
4424   (zerop (random 2)))
4425
4426 (defun gnus-thread-sort-by-random (h1 h2)
4427   "Sort threads by root article number."
4428   (gnus-article-sort-by-random
4429    (gnus-thread-header h1) (gnus-thread-header h2)))
4430
4431 (defsubst gnus-article-sort-by-lines (h1 h2)
4432   "Sort articles by article Lines header."
4433   (< (mail-header-lines h1)
4434      (mail-header-lines h2)))
4435
4436 (defun gnus-thread-sort-by-lines (h1 h2)
4437   "Sort threads by root article Lines header."
4438   (gnus-article-sort-by-lines
4439    (gnus-thread-header h1) (gnus-thread-header h2)))
4440
4441 (defsubst gnus-article-sort-by-chars (h1 h2)
4442   "Sort articles by octet length."
4443   (< (mail-header-chars h1)
4444      (mail-header-chars h2)))
4445
4446 (defun gnus-thread-sort-by-chars (h1 h2)
4447   "Sort threads by root article octet length."
4448   (gnus-article-sort-by-chars
4449    (gnus-thread-header h1) (gnus-thread-header h2)))
4450
4451 (defsubst gnus-article-sort-by-author (h1 h2)
4452   "Sort articles by root author."
4453   (string-lessp
4454    (let ((addr (car (mime-entity-read-field h1 'From))))
4455      (or (std11-full-name-string addr)
4456          (std11-address-string addr)
4457          ""))
4458    (let ((addr (car (mime-entity-read-field h2 'From))))
4459      (or (std11-full-name-string addr)
4460          (std11-address-string addr)
4461          ""))
4462    ))
4463
4464 (defun gnus-thread-sort-by-author (h1 h2)
4465   "Sort threads by root author."
4466   (gnus-article-sort-by-author
4467    (gnus-thread-header h1)  (gnus-thread-header h2)))
4468
4469 (defsubst gnus-article-sort-by-subject (h1 h2)
4470   "Sort articles by root subject."
4471   (string-lessp
4472    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4473    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4474
4475 (defun gnus-thread-sort-by-subject (h1 h2)
4476   "Sort threads by root subject."
4477   (gnus-article-sort-by-subject
4478    (gnus-thread-header h1) (gnus-thread-header h2)))
4479
4480 (defsubst gnus-article-sort-by-date (h1 h2)
4481   "Sort articles by root article date."
4482   (time-less-p
4483    (gnus-date-get-time (mail-header-date h1))
4484    (gnus-date-get-time (mail-header-date h2))))
4485
4486 (defun gnus-thread-sort-by-date (h1 h2)
4487   "Sort threads by root article date."
4488   (gnus-article-sort-by-date
4489    (gnus-thread-header h1) (gnus-thread-header h2)))
4490
4491 (defsubst gnus-article-sort-by-score (h1 h2)
4492   "Sort articles by root article score.
4493 Unscored articles will be counted as having a score of zero."
4494   (> (or (cdr (assq (mail-header-number h1)
4495                     gnus-newsgroup-scored))
4496          gnus-summary-default-score 0)
4497      (or (cdr (assq (mail-header-number h2)
4498                     gnus-newsgroup-scored))
4499          gnus-summary-default-score 0)))
4500
4501 (defun gnus-thread-sort-by-score (h1 h2)
4502   "Sort threads by root article score."
4503   (gnus-article-sort-by-score
4504    (gnus-thread-header h1) (gnus-thread-header h2)))
4505
4506 (defun gnus-thread-sort-by-total-score (h1 h2)
4507   "Sort threads by the sum of all scores in the thread.
4508 Unscored articles will be counted as having a score of zero."
4509   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4510
4511 (defun gnus-thread-total-score (thread)
4512   ;; This function find the total score of THREAD.
4513   (cond
4514    ((null thread)
4515     0)
4516    ((consp thread)
4517     (if (stringp (car thread))
4518         (apply gnus-thread-score-function 0
4519                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4520       (gnus-thread-total-score-1 thread)))
4521    (t
4522     (gnus-thread-total-score-1 (list thread)))))
4523
4524 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4525   "Sort threads such that the thread with the most recently arrived article comes first."
4526   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4527
4528 (defun gnus-thread-highest-number (thread)
4529   "Return the highest article number in THREAD."
4530   (apply 'max (mapcar (lambda (header)
4531                         (mail-header-number header))
4532                       (message-flatten-list thread))))
4533
4534 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4535   "Sort threads such that the thread with the most recently dated article comes first."
4536   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4537
4538 (defun gnus-thread-latest-date (thread)
4539   "Return the highest article date in THREAD."
4540   (let ((previous-time 0))
4541     (apply 'max
4542            (mapcar
4543             (lambda (header)
4544               (setq previous-time
4545                     (condition-case ()
4546                         (time-to-seconds (mail-header-parse-date
4547                                           (mail-header-date header)))
4548                       (error previous-time))))
4549             (sort
4550              (message-flatten-list thread)
4551              (lambda (h1 h2)
4552                (< (mail-header-number h1)
4553                   (mail-header-number h2))))))))
4554
4555 (defun gnus-thread-total-score-1 (root)
4556   ;; This function find the total score of the thread below ROOT.
4557   (setq root (car root))
4558   (apply gnus-thread-score-function
4559          (or (append
4560               (mapcar 'gnus-thread-total-score
4561                       (cdr (gnus-id-to-thread (mail-header-id root))))
4562               (when (> (mail-header-number root) 0)
4563                 (list (or (cdr (assq (mail-header-number root)
4564                                      gnus-newsgroup-scored))
4565                           gnus-summary-default-score 0))))
4566              (list gnus-summary-default-score)
4567              '(0))))
4568
4569 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4570 (defvar gnus-tmp-prev-subject nil)
4571 (defvar gnus-tmp-false-parent nil)
4572 (defvar gnus-tmp-root-expunged nil)
4573 (defvar gnus-tmp-dummy-line nil)
4574
4575 (eval-when-compile (defvar gnus-tmp-header))
4576 (defun gnus-extra-header (type &optional header)
4577   "Return the extra header of TYPE."
4578   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4579       ""))
4580
4581 (defvar gnus-tmp-thread-tree-header-string "")
4582
4583 (defcustom gnus-sum-thread-tree-root "> "
4584   "With %B spec, used for the root of a thread.
4585 If nil, use subject instead."
4586   :type '(radio (const :format "%v  " nil) (string :size 0))
4587   :group 'gnus-thread)
4588 (defcustom gnus-sum-thread-tree-false-root "> "
4589   "With %B spec, used for a false root of a thread.
4590 If nil, use subject instead."
4591   :type '(radio (const :format "%v  " nil) (string :size 0))
4592   :group 'gnus-thread)
4593 (defcustom gnus-sum-thread-tree-single-indent ""
4594   "With %B spec, used for a thread with just one message.
4595 If nil, use subject instead."
4596   :type '(radio (const :format "%v  " nil) (string :size 0))
4597   :group 'gnus-thread)
4598 (defcustom gnus-sum-thread-tree-vertical "| "
4599   "With %B spec, used for drawing a vertical line."
4600   :type 'string
4601   :group 'gnus-thread)
4602 (defcustom gnus-sum-thread-tree-indent "  "
4603   "With %B spec, used for indenting."
4604   :type 'string
4605   :group 'gnus-thread)
4606 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4607   "With %B spec, used for a leaf with brothers."
4608   :type 'string
4609   :group 'gnus-thread)
4610 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4611   "With %B spec, used for a leaf without brothers."
4612   :type 'string
4613   :group 'gnus-thread)
4614
4615 (defun gnus-summary-prepare-threads (threads)
4616   "Prepare summary buffer from THREADS and indentation LEVEL.
4617 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4618 or a straight list of headers."
4619   (gnus-message 7 "Generating summary...")
4620
4621   (setq gnus-newsgroup-threads threads)
4622   (beginning-of-line)
4623
4624   (let ((gnus-tmp-level 0)
4625         (default-score (or gnus-summary-default-score 0))
4626         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4627         (building-line-count gnus-summary-display-while-building)
4628         (building-count (integerp gnus-summary-display-while-building))
4629         thread number subject stack state gnus-tmp-gathered beg-match
4630         new-roots gnus-tmp-new-adopts thread-end simp-subject
4631         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4632         gnus-tmp-replied gnus-tmp-subject-or-nil
4633         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4634         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4635         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4636         tree-stack)
4637
4638     (setq gnus-tmp-prev-subject nil
4639           gnus-tmp-thread-tree-header-string "")
4640
4641     (if (vectorp (car threads))
4642         ;; If this is a straight (sic) list of headers, then a
4643         ;; threaded summary display isn't required, so we just create
4644         ;; an unthreaded one.
4645         (gnus-summary-prepare-unthreaded threads)
4646
4647       ;; Do the threaded display.
4648
4649       (if gnus-summary-display-while-building
4650           (switch-to-buffer (buffer-name)))
4651       (while (or threads stack gnus-tmp-new-adopts new-roots)
4652
4653         (if (and (= gnus-tmp-level 0)
4654                  (or (not stack)
4655                      (= (caar stack) 0))
4656                  (not gnus-tmp-false-parent)
4657                  (or gnus-tmp-new-adopts new-roots))
4658             (if gnus-tmp-new-adopts
4659                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4660                       thread (list (car gnus-tmp-new-adopts))
4661                       gnus-tmp-header (caar thread)
4662                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4663               (when new-roots
4664                 (setq thread (list (car new-roots))
4665                       gnus-tmp-header (caar thread)
4666                       new-roots (cdr new-roots))))
4667
4668           (if threads
4669               ;; If there are some threads, we do them before the
4670               ;; threads on the stack.
4671               (setq thread threads
4672                     gnus-tmp-header (caar thread))
4673             ;; There were no current threads, so we pop something off
4674             ;; the stack.
4675             (setq state (car stack)
4676                   gnus-tmp-level (car state)
4677                   tree-stack (cadr state)
4678                   thread (caddr state)
4679                   stack (cdr stack)
4680                   gnus-tmp-header (caar thread))))
4681
4682         (setq gnus-tmp-false-parent nil)
4683         (setq gnus-tmp-root-expunged nil)
4684         (setq thread-end nil)
4685
4686         (if (stringp gnus-tmp-header)
4687             ;; The header is a dummy root.
4688             (cond
4689              ((eq gnus-summary-make-false-root 'adopt)
4690               ;; We let the first article adopt the rest.
4691               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4692                                                (cddar thread)))
4693               (setq gnus-tmp-gathered
4694                     (nconc (mapcar
4695                             (lambda (h) (mail-header-number (car h)))
4696                             (cddar thread))
4697                            gnus-tmp-gathered))
4698               (setq thread (cons (list (caar thread)
4699                                        (cadar thread))
4700                                  (cdr thread)))
4701               (setq gnus-tmp-level -1
4702                     gnus-tmp-false-parent t))
4703              ((eq gnus-summary-make-false-root 'empty)
4704               ;; We print adopted articles with empty subject fields.
4705               (setq gnus-tmp-gathered
4706                     (nconc (mapcar
4707                             (lambda (h) (mail-header-number (car h)))
4708                             (cddar thread))
4709                            gnus-tmp-gathered))
4710               (setq gnus-tmp-level -1))
4711              ((eq gnus-summary-make-false-root 'dummy)
4712               ;; We remember that we probably want to output a dummy
4713               ;; root.
4714               (setq gnus-tmp-dummy-line gnus-tmp-header)
4715               (setq gnus-tmp-prev-subject gnus-tmp-header))
4716              (t
4717               ;; We do not make a root for the gathered
4718               ;; sub-threads at all.
4719               (setq gnus-tmp-level -1)))
4720
4721           (setq number (mail-header-number gnus-tmp-header)
4722                 subject (mail-header-subject gnus-tmp-header)
4723                 simp-subject (gnus-simplify-subject-fully subject))
4724
4725           (cond
4726            ;; If the thread has changed subject, we might want to make
4727            ;; this subthread into a root.
4728            ((and (null gnus-thread-ignore-subject)
4729                  (not (zerop gnus-tmp-level))
4730                  gnus-tmp-prev-subject
4731                  (not (string= gnus-tmp-prev-subject simp-subject)))
4732             (setq new-roots (nconc new-roots (list (car thread)))
4733                   thread-end t
4734                   gnus-tmp-header nil))
4735            ;; If the article lies outside the current limit,
4736            ;; then we do not display it.
4737            ((not (memq number gnus-newsgroup-limit))
4738             (setq gnus-tmp-gathered
4739                   (nconc (mapcar
4740                           (lambda (h) (mail-header-number (car h)))
4741                           (cdar thread))
4742                          gnus-tmp-gathered))
4743             (setq gnus-tmp-new-adopts (if (cdar thread)
4744                                           (append gnus-tmp-new-adopts
4745                                                   (cdar thread))
4746                                         gnus-tmp-new-adopts)
4747                   thread-end t
4748                   gnus-tmp-header nil)
4749             (when (zerop gnus-tmp-level)
4750               (setq gnus-tmp-root-expunged t)))
4751            ;; Perhaps this article is to be marked as read?
4752            ((and gnus-summary-mark-below
4753                  (< (or (cdr (assq number gnus-newsgroup-scored))
4754                         default-score)
4755                     gnus-summary-mark-below)
4756                  ;; Don't touch sparse articles.
4757                  (not (gnus-summary-article-sparse-p number))
4758                  (not (gnus-summary-article-ancient-p number)))
4759             (setq gnus-newsgroup-unreads
4760                   (delq number gnus-newsgroup-unreads))
4761             (if gnus-newsgroup-auto-expire
4762                 (setq gnus-newsgroup-expirable
4763                       (gnus-add-to-sorted-list
4764                        gnus-newsgroup-expirable number))
4765               (push (cons number gnus-low-score-mark)
4766                     gnus-newsgroup-reads))))
4767
4768           (when gnus-tmp-header
4769             ;; We may have an old dummy line to output before this
4770             ;; article.
4771             (when (and gnus-tmp-dummy-line
4772                        (gnus-subject-equal
4773                         gnus-tmp-dummy-line
4774                         (mail-header-subject gnus-tmp-header)))
4775               (gnus-summary-insert-dummy-line
4776                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4777               (setq gnus-tmp-dummy-line nil))
4778
4779             ;; Compute the mark.
4780             (setq gnus-tmp-unread (gnus-article-mark number))
4781
4782             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4783                                   gnus-tmp-header gnus-tmp-level)
4784                   gnus-newsgroup-data)
4785
4786             ;; Actually insert the line.
4787             (setq
4788              gnus-tmp-subject-or-nil
4789              (cond
4790               ((and gnus-thread-ignore-subject
4791                     gnus-tmp-prev-subject
4792                     (not (string= gnus-tmp-prev-subject simp-subject)))
4793                subject)
4794               ((zerop gnus-tmp-level)
4795                (if (and (eq gnus-summary-make-false-root 'empty)
4796                         (memq number gnus-tmp-gathered)
4797                         gnus-tmp-prev-subject
4798                         (string= gnus-tmp-prev-subject simp-subject))
4799                    gnus-summary-same-subject
4800                  subject))
4801               (t gnus-summary-same-subject)))
4802             (if (and (eq gnus-summary-make-false-root 'adopt)
4803                      (= gnus-tmp-level 1)
4804                      (memq number gnus-tmp-gathered))
4805                 (setq gnus-tmp-opening-bracket ?\<
4806                       gnus-tmp-closing-bracket ?\>)
4807               (setq gnus-tmp-opening-bracket ?\[
4808                     gnus-tmp-closing-bracket ?\]))
4809             (setq
4810              gnus-tmp-indentation
4811              (aref gnus-thread-indent-array gnus-tmp-level)
4812              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4813              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4814                                 gnus-summary-default-score 0)
4815              gnus-tmp-score-char
4816              (if (or (null gnus-summary-default-score)
4817                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4818                          gnus-summary-zcore-fuzz))
4819                  ?\ ;;;Whitespace
4820                (if (< gnus-tmp-score gnus-summary-default-score)
4821                    gnus-score-below-mark gnus-score-over-mark))
4822              gnus-tmp-replied
4823              (cond ((memq number gnus-newsgroup-processable)
4824                     gnus-process-mark)
4825                    ((memq number gnus-newsgroup-cached)
4826                     gnus-cached-mark)
4827                    ((memq number gnus-newsgroup-replied)
4828                     gnus-replied-mark)
4829                    ((memq number gnus-newsgroup-forwarded)
4830                     gnus-forwarded-mark)
4831                    ((memq number gnus-newsgroup-saved)
4832                     gnus-saved-mark)
4833                    ((memq number gnus-newsgroup-recent)
4834                     gnus-recent-mark)
4835                    ((memq number gnus-newsgroup-unseen)
4836                     gnus-unseen-mark)
4837                    (t gnus-no-mark))
4838              gnus-tmp-downloaded
4839              (cond ((memq number gnus-newsgroup-undownloaded)
4840                     gnus-undownloaded-mark)
4841                    (gnus-newsgroup-agentized
4842                     gnus-downloaded-mark)
4843                    (t
4844                     gnus-no-mark))
4845              gnus-tmp-from (mail-header-from gnus-tmp-header)
4846              gnus-tmp-name
4847              (cond
4848               ((string-match "<[^>]+> *$" gnus-tmp-from)
4849                (setq beg-match (match-beginning 0))
4850                (or (and (string-match "^\".+\"" gnus-tmp-from)
4851                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4852                    (substring gnus-tmp-from 0 beg-match)))
4853               ((string-match "(.+)" gnus-tmp-from)
4854                (substring gnus-tmp-from
4855                           (1+ (match-beginning 0)) (1- (match-end 0))))
4856               (t gnus-tmp-from))
4857
4858              ;; Do the %B string
4859              gnus-tmp-thread-tree-header-string
4860              (cond
4861               ((not gnus-show-threads) "")
4862               ((zerop gnus-tmp-level)
4863                (cond ((cdar thread)
4864                       (or gnus-sum-thread-tree-root subject))
4865                      (gnus-tmp-new-adopts
4866                       (or gnus-sum-thread-tree-false-root subject))
4867                      (t
4868                       (or gnus-sum-thread-tree-single-indent subject))))
4869               (t
4870                (concat (apply 'concat
4871                               (mapcar (lambda (item)
4872                                         (if (= item 1)
4873                                             gnus-sum-thread-tree-vertical
4874                                           gnus-sum-thread-tree-indent))
4875                                       (cdr (reverse tree-stack))))
4876                        (if (nth 1 thread)
4877                            gnus-sum-thread-tree-leaf-with-other
4878                          gnus-sum-thread-tree-single-leaf)))))
4879             (when (string= gnus-tmp-name "")
4880               (setq gnus-tmp-name gnus-tmp-from))
4881             (unless (numberp gnus-tmp-lines)
4882               (setq gnus-tmp-lines -1))
4883             (if (= gnus-tmp-lines -1)
4884                 (setq gnus-tmp-lines "?")
4885               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4886               (gnus-put-text-property
4887              (point)
4888              (progn (eval gnus-summary-line-format-spec) (point))
4889                'gnus-number number)
4890             (when gnus-visual-p
4891               (forward-line -1)
4892               (gnus-run-hooks 'gnus-summary-update-hook)
4893               (forward-line 1))
4894
4895             (setq gnus-tmp-prev-subject simp-subject)))
4896
4897         (when (nth 1 thread)
4898           (push (list (max 0 gnus-tmp-level)
4899                       (copy-sequence tree-stack)
4900                       (nthcdr 1 thread))
4901                 stack))
4902         (push (if (nth 1 thread) 1 0) tree-stack)
4903         (incf gnus-tmp-level)
4904         (setq threads (if thread-end nil (cdar thread)))
4905         (if gnus-summary-display-while-building
4906             (if building-count
4907                 (progn
4908                   ;; use a set frequency
4909                   (setq building-line-count (1- building-line-count))
4910                   (when (= building-line-count 0)
4911                     (sit-for 0)
4912                     (setq building-line-count
4913                           gnus-summary-display-while-building)))
4914               ;; always
4915               (sit-for 0)))
4916         (unless threads
4917           (setq gnus-tmp-level 0)))))
4918   (gnus-message 7 "Generating summary...done"))
4919
4920 (defun gnus-summary-prepare-unthreaded (headers)
4921   "Generate an unthreaded summary buffer based on HEADERS."
4922   (let (header number mark)
4923
4924     (beginning-of-line)
4925
4926     (while headers
4927       ;; We may have to root out some bad articles...
4928       (when (memq (setq number (mail-header-number
4929                                 (setq header (pop headers))))
4930                   gnus-newsgroup-limit)
4931         ;; Mark article as read when it has a low score.
4932         (when (and gnus-summary-mark-below
4933                    (< (or (cdr (assq number gnus-newsgroup-scored))
4934                           gnus-summary-default-score 0)
4935                       gnus-summary-mark-below)
4936                    (not (gnus-summary-article-ancient-p number)))
4937           (setq gnus-newsgroup-unreads
4938                 (delq number gnus-newsgroup-unreads))
4939           (if gnus-newsgroup-auto-expire
4940               (push number gnus-newsgroup-expirable)
4941             (push (cons number gnus-low-score-mark)
4942                   gnus-newsgroup-reads)))
4943
4944         (setq mark (gnus-article-mark number))
4945         (push (gnus-data-make number mark (1+ (point)) header 0)
4946               gnus-newsgroup-data)
4947         (gnus-summary-insert-line
4948          header 0 number
4949          (memq number gnus-newsgroup-undownloaded)
4950          mark (memq number gnus-newsgroup-replied)
4951          (memq number gnus-newsgroup-expirable)
4952          (mail-header-subject header) nil
4953          (cdr (assq number gnus-newsgroup-scored))
4954          (memq number gnus-newsgroup-processable))))))
4955
4956 (defun gnus-summary-remove-list-identifiers ()
4957   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4958   (let ((regexp (if (consp gnus-list-identifiers)
4959                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4960                   gnus-list-identifiers))
4961         changed subject)
4962     (when regexp
4963       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
4964       (dolist (header gnus-newsgroup-headers)
4965         (setq subject (mail-header-subject header)
4966               changed nil)
4967         (while (string-match regexp subject)
4968           (setq subject
4969                 (concat (substring subject 0 (match-beginning 1))
4970                         (substring subject (match-end 0)))
4971                 changed t))
4972         (when changed
4973           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
4974             (setq subject
4975                   (concat (substring subject 0 (match-beginning 1))
4976                           (substring subject (match-end 1)))))
4977           (mail-header-set-subject header subject))))))
4978
4979 (defun gnus-fetch-headers (articles)
4980   "Fetch headers of ARTICLES."
4981   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4982     (gnus-message 5 "Fetching headers for %s..." name)
4983     (prog1
4984         (if (eq 'nov
4985                 (setq gnus-headers-retrieved-by
4986                       (gnus-retrieve-headers
4987                        articles gnus-newsgroup-name
4988                        ;; We might want to fetch old headers, but
4989                        ;; not if there is only 1 article.
4990                        (and (or (and
4991                                  (not (eq gnus-fetch-old-headers 'some))
4992                                  (not (numberp gnus-fetch-old-headers)))
4993                                 (> (length articles) 1))
4994                             gnus-fetch-old-headers))))
4995             (gnus-get-newsgroup-headers-xover
4996              articles nil nil gnus-newsgroup-name t)
4997           (gnus-get-newsgroup-headers))
4998       (gnus-message 5 "Fetching headers for %s...done" name))))
4999
5000 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5001   "Select newsgroup GROUP.
5002 If READ-ALL is non-nil, all articles in the group are selected.
5003 If SELECT-ARTICLES, only select those articles from GROUP."
5004   (let* ((entry (gnus-group-entry group))
5005          ;;!!! Dirty hack; should be removed.
5006          (gnus-summary-ignore-duplicates
5007           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5008               t
5009             gnus-summary-ignore-duplicates))
5010          (info (nth 2 entry))
5011          articles fetched-articles cached)
5012
5013     (unless (gnus-check-server
5014              (set (make-local-variable 'gnus-current-select-method)
5015                   (gnus-find-method-for-group group)))
5016       (error "Couldn't open server"))
5017
5018     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5019         (gnus-activate-group group)     ; Or we can activate it...
5020         (progn                          ; Or we bug out.
5021           (when (equal major-mode 'gnus-summary-mode)
5022             (gnus-kill-buffer (current-buffer)))
5023           (error "Couldn't activate group %s: %s"
5024                  group (gnus-status-message group))))
5025
5026     (unless (gnus-request-group group t)
5027       (when (equal major-mode 'gnus-summary-mode)
5028         (gnus-kill-buffer (current-buffer)))
5029       (error "Couldn't request group %s: %s"
5030              group (gnus-status-message group)))
5031
5032     (when gnus-agent
5033       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5034       
5035       (setq gnus-summary-use-undownloaded-faces
5036             (gnus-agent-find-parameter
5037              group
5038              'agent-enable-undownloaded-faces)))
5039
5040     (setq gnus-newsgroup-name group
5041           gnus-newsgroup-unselected nil
5042           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5043
5044     (let ((display (gnus-group-find-parameter group 'display)))
5045       (setq gnus-newsgroup-display
5046             (cond
5047              ((not (zerop (or (car-safe read-all) 0)))
5048               ;; The user entered the group with C-u SPC/RET, let's show
5049               ;; all articles.
5050               'gnus-not-ignore)
5051              ((eq display 'all)
5052               'gnus-not-ignore)
5053              ((arrayp display)
5054               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5055              ((numberp display)
5056               ;; The following is probably the "correct" solution, but
5057               ;; it makes Gnus fetch all headers and then limit the
5058               ;; articles (which is slow), so instead we hack the
5059               ;; select-articles parameter instead. -- Simon Josefsson
5060               ;; <jas@kth.se>
5061               ;;
5062               ;; (gnus-byte-compile
5063               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5064               ;;                         display)))))
5065               (setq select-articles
5066                     (gnus-uncompress-range
5067                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5068                              (if (> tmp 0)
5069                                  tmp
5070                                1))
5071                            (cdr (gnus-active group)))))
5072               nil)
5073              (t
5074               nil))))
5075
5076     (gnus-summary-setup-default-charset)
5077
5078     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5079     (when (gnus-virtual-group-p group)
5080       (setq cached gnus-newsgroup-cached))
5081
5082     (setq gnus-newsgroup-unreads
5083           (gnus-sorted-ndifference
5084            (gnus-sorted-ndifference gnus-newsgroup-unreads
5085                                     gnus-newsgroup-marked)
5086            gnus-newsgroup-dormant))
5087
5088     (setq gnus-newsgroup-processable nil)
5089
5090     (gnus-update-read-articles group gnus-newsgroup-unreads)
5091
5092     ;; Adjust and set lists of article marks.
5093     (when info
5094       (gnus-adjust-marked-articles info))
5095     (if (setq articles select-articles)
5096         (setq gnus-newsgroup-unselected
5097               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5098       (setq articles (gnus-articles-to-read group read-all)))
5099
5100     (cond
5101      ((null articles)
5102       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5103       'quit)
5104      ((eq articles 0) nil)
5105      (t
5106       ;; Init the dependencies hash table.
5107       (setq gnus-newsgroup-dependencies
5108             (gnus-make-hashtable (length articles)))
5109       (gnus-set-global-variables)
5110       ;; Retrieve the headers and read them in.
5111
5112       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5113
5114       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5115       (when cached
5116         (setq gnus-newsgroup-cached cached))
5117
5118       ;; Suppress duplicates?
5119       (when gnus-suppress-duplicates
5120         (gnus-dup-suppress-articles))
5121
5122       ;; Set the initial limit.
5123       (setq gnus-newsgroup-limit (copy-sequence articles))
5124       ;; Remove canceled articles from the list of unread articles.
5125       (setq fetched-articles
5126             (mapcar (lambda (headers) (mail-header-number headers))
5127                     gnus-newsgroup-headers))
5128       (setq gnus-newsgroup-articles fetched-articles)
5129       (setq gnus-newsgroup-unreads
5130             (gnus-sorted-nintersection
5131              gnus-newsgroup-unreads fetched-articles))
5132       (gnus-compute-unseen-list)
5133
5134       ;; Removed marked articles that do not exist.
5135       (gnus-update-missing-marks
5136        (gnus-sorted-difference articles fetched-articles))
5137       ;; We might want to build some more threads first.
5138       (when (and gnus-fetch-old-headers
5139                  (eq gnus-headers-retrieved-by 'nov))
5140         (if (eq gnus-fetch-old-headers 'invisible)
5141             (gnus-build-all-threads)
5142           (gnus-build-old-threads)))
5143       ;; Let the Gnus agent mark articles as read.
5144       (when gnus-agent
5145         (gnus-agent-get-undownloaded-list))
5146       ;; Remove list identifiers from subject
5147       (when gnus-list-identifiers
5148         (gnus-summary-remove-list-identifiers))
5149       ;; Check whether auto-expire is to be done in this group.
5150       (setq gnus-newsgroup-auto-expire
5151             (gnus-group-auto-expirable-p group))
5152       ;; Set up the article buffer now, if necessary.
5153       (unless gnus-single-article-buffer
5154         (gnus-article-setup-buffer))
5155       ;; First and last article in this newsgroup.
5156       (when gnus-newsgroup-headers
5157         (setq gnus-newsgroup-begin
5158               (mail-header-number (car gnus-newsgroup-headers))
5159               gnus-newsgroup-end
5160               (mail-header-number
5161                (gnus-last-element gnus-newsgroup-headers))))
5162       ;; GROUP is successfully selected.
5163       (or gnus-newsgroup-headers t)))))
5164
5165 (defun gnus-compute-unseen-list ()
5166   ;; The `seen' marks are treated specially.
5167   (if (not gnus-newsgroup-seen)
5168       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5169     (setq gnus-newsgroup-unseen
5170           (gnus-inverse-list-range-intersection
5171            gnus-newsgroup-articles gnus-newsgroup-seen))))
5172
5173 (defun gnus-summary-display-make-predicate (display)
5174   (require 'gnus-agent)
5175   (when (= (length display) 1)
5176     (setq display (car display)))
5177   (unless gnus-summary-display-cache
5178     (dolist (elem (append '((unread . unread)
5179                             (read . read)
5180                             (unseen . unseen))
5181                           gnus-article-mark-lists))
5182       (push (cons (cdr elem)
5183                   (gnus-byte-compile
5184                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5185             gnus-summary-display-cache)))
5186   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5187         (gnus-category-predicate-cache gnus-summary-display-cache))
5188     (gnus-get-predicate display)))
5189
5190 ;; Uses the dynamically bound `number' variable.
5191 (eval-when-compile
5192   (defvar number))
5193 (defun gnus-article-marked-p (type &optional article)
5194   (let ((article (or article number)))
5195     (cond
5196      ((eq type 'tick)
5197       (memq article gnus-newsgroup-marked))
5198      ((eq type 'spam)
5199       (memq article gnus-newsgroup-spam-marked))
5200      ((eq type 'unsend)
5201       (memq article gnus-newsgroup-unsendable))
5202      ((eq type 'undownload)
5203       (memq article gnus-newsgroup-undownloaded))
5204      ((eq type 'download)
5205       (memq article gnus-newsgroup-downloadable))
5206      ((eq type 'unread)
5207       (memq article gnus-newsgroup-unreads))
5208      ((eq type 'read)
5209       (memq article gnus-newsgroup-reads))
5210      ((eq type 'dormant)
5211       (memq article gnus-newsgroup-dormant) )
5212      ((eq type 'expire)
5213       (memq article gnus-newsgroup-expirable))
5214      ((eq type 'reply)
5215       (memq article gnus-newsgroup-replied))
5216      ((eq type 'killed)
5217       (memq article gnus-newsgroup-killed))
5218      ((eq type 'bookmark)
5219       (assq article gnus-newsgroup-bookmarks))
5220      ((eq type 'score)
5221       (assq article gnus-newsgroup-scored))
5222      ((eq type 'save)
5223       (memq article gnus-newsgroup-saved))
5224      ((eq type 'cache)
5225       (memq article gnus-newsgroup-cached))
5226      ((eq type 'forward)
5227       (memq article gnus-newsgroup-forwarded))
5228      ((eq type 'seen)
5229       (not (memq article gnus-newsgroup-unseen)))
5230      ((eq type 'recent)
5231       (memq article gnus-newsgroup-recent))
5232      (t t))))
5233
5234 (defun gnus-articles-to-read (group &optional read-all)
5235   "Find out what articles the user wants to read."
5236   (let* ((display (gnus-group-find-parameter group 'display))
5237          (articles
5238           ;; Select all articles if `read-all' is non-nil, or if there
5239           ;; are no unread articles.
5240           (if (or read-all
5241                   (and (zerop (length gnus-newsgroup-marked))
5242                        (zerop (length gnus-newsgroup-unreads)))
5243                   ;; Fetch all if the predicate is non-nil.
5244                   gnus-newsgroup-display)
5245               ;; We want to select the headers for all the articles in
5246               ;; the group, so we select either all the active
5247               ;; articles in the group, or (if that's nil), the
5248               ;; articles in the cache.
5249               (or
5250                (gnus-uncompress-range (gnus-active group))
5251                (gnus-cache-articles-in-group group))
5252             ;; Select only the "normal" subset of articles.
5253             (gnus-sorted-nunion
5254              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5255              gnus-newsgroup-unreads)))
5256          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5257          (scored (length scored-list))
5258          (number (length articles))
5259          (marked (+ (length gnus-newsgroup-marked)
5260                     (length gnus-newsgroup-dormant)))
5261          (select
5262           (cond
5263            ((numberp read-all)
5264             read-all)
5265            ((numberp gnus-newsgroup-display)
5266             gnus-newsgroup-display)
5267            (t
5268             (condition-case ()
5269                 (cond
5270                  ((and (or (<= scored marked) (= scored number))
5271                        (numberp gnus-large-newsgroup)
5272                        (> number gnus-large-newsgroup))
5273                   (let* ((cursor-in-echo-area nil)
5274                          (initial (gnus-parameter-large-newsgroup-initial
5275                                    gnus-newsgroup-name))
5276                          (input
5277                           (read-string
5278                            (format
5279                             "How many articles from %s (%s %d): "
5280                             (gnus-limit-string
5281                              (gnus-group-decoded-name gnus-newsgroup-name)
5282                              35)
5283                             (if initial "max" "default")
5284                             number)
5285                            (if initial
5286                                (cons (number-to-string initial)
5287                                      0)))))
5288                     (if (string-match "^[ \t]*$" input) number input)))
5289                  ((and (> scored marked) (< scored number)
5290                        (> (- scored number) 20))
5291                   (let ((input
5292                          (read-string
5293                           (format "%s %s (%d scored, %d total): "
5294                                   "How many articles from"
5295                                   (gnus-group-decoded-name group)
5296                                   scored number))))
5297                     (if (string-match "^[ \t]*$" input)
5298                         number input)))
5299                  (t number))
5300               (quit
5301                (message "Quit getting the articles to read")
5302                nil))))))
5303     (setq select (if (stringp select) (string-to-number select) select))
5304     (if (or (null select) (zerop select))
5305         select
5306       (if (and (not (zerop scored)) (<= (abs select) scored))
5307           (progn
5308             (setq articles (sort scored-list '<))
5309             (setq number (length articles)))
5310         (setq articles (copy-sequence articles)))
5311
5312       (when (< (abs select) number)
5313         (if (< select 0)
5314             ;; Select the N oldest articles.
5315             (setcdr (nthcdr (1- (abs select)) articles) nil)
5316           ;; Select the N most recent articles.
5317           (setq articles (nthcdr (- number select) articles))))
5318       (setq gnus-newsgroup-unselected
5319             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5320       (when gnus-alter-articles-to-read-function
5321         (setq articles
5322               (sort
5323                (funcall gnus-alter-articles-to-read-function
5324                         gnus-newsgroup-name articles)
5325                '<)))
5326       articles)))
5327
5328 (defun gnus-killed-articles (killed articles)
5329   (let (out)
5330     (while articles
5331       (when (inline (gnus-member-of-range (car articles) killed))
5332         (push (car articles) out))
5333       (setq articles (cdr articles)))
5334     out))
5335
5336 (defun gnus-uncompress-marks (marks)
5337   "Uncompress the mark ranges in MARKS."
5338   (let ((uncompressed '(score bookmark))
5339         out)
5340     (while marks
5341       (if (memq (caar marks) uncompressed)
5342           (push (car marks) out)
5343         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5344       (setq marks (cdr marks)))
5345     out))
5346
5347 (defun gnus-article-mark-to-type (mark)
5348   "Return the type of MARK."
5349   (or (cadr (assq mark gnus-article-special-mark-lists))
5350       'list))
5351
5352 (defun gnus-article-unpropagatable-p (mark)
5353   "Return whether MARK should be propagated to back end."
5354   (memq mark gnus-article-unpropagated-mark-lists))
5355
5356 (defun gnus-adjust-marked-articles (info)
5357   "Set all article lists and remove all marks that are no longer valid."
5358   (let* ((marked-lists (gnus-info-marks info))
5359          (active (gnus-active (gnus-info-group info)))
5360          (min (car active))
5361          (max (cdr active))
5362          (types gnus-article-mark-lists)
5363          marks var articles article mark mark-type p-articles
5364          bgn end)
5365
5366     (dolist (marks marked-lists)
5367       (setq mark (car marks)
5368             mark-type (gnus-article-mark-to-type mark)
5369             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5370
5371       ;; We set the variable according to the type of the marks list,
5372       ;; and then adjust the marks to a subset of the active articles.
5373       (cond
5374        ;; Adjust "simple" lists - compressed yet unsorted
5375        ((eq mark-type 'list)
5376         ;; Simultaneously uncompress and clip to active range
5377         (setq p-articles marks
5378               articles (cdr p-articles))
5379         (while (setq article (car articles))
5380           (when (cond ((consp article)
5381                        (setq bgn (max (car article) min)
5382                              end (min (cdr article) max))
5383                        (if (> bgn end)
5384                            t    ; range excluded - splice out of marks
5385                          (setcar articles bgn) ; First value replaces range.
5386                          (setq bgn (1+ bgn))
5387                          (while (<= bgn end)
5388                            (setq articles (setcdr articles (cons bgn (cdr articles)))
5389                                  bgn (1+ bgn)))
5390                          (setq p-articles articles
5391                                articles (cdr articles))
5392                          nil))
5393                       ((or (< article min)
5394                            (> article max))
5395                        t        ; value excluded - splice out of marks
5396                        )
5397                       (t
5398                        (setq p-articles articles
5399                              articles (cdr articles))
5400                        nil))
5401             ;; perform slice to remove article
5402             (if (setcar articles (cadr articles))
5403                 (setcdr articles (cddr articles))
5404               (setcdr p-articles nil))))
5405         (set var (cdr marks)))
5406        ;; Adjust assocs.
5407        ((eq mark-type 'tuple)
5408         (set var (setq articles (cdr marks)))
5409         (when (not (listp (cdr (symbol-value var))))
5410           (set var (list (symbol-value var))))
5411         (when (not (listp (cdr articles)))
5412           (setq articles (list articles)))
5413         (while articles
5414           (when (or (not (consp (setq article (pop articles))))
5415                     (< (car article) min)
5416                     (> (car article) max))
5417             (set var (delq article (symbol-value var))))))
5418        ;; Adjust ranges (sloppily).
5419        ((eq mark-type 'range)
5420         (cond
5421          ((eq mark 'seen)
5422           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5423           ;; It should be (seen (NUM1 . NUM2)).
5424           (when (numberp (cddr marks))
5425             (setcdr marks (list (cdr marks))))
5426           (setq articles (cdr marks))
5427           (while (and articles
5428                       (or (and (consp (car articles))
5429                                (> min (cdar articles)))
5430                           (and (numberp (car articles))
5431                                (> min (car articles)))))
5432             (pop articles))
5433           (set var articles))))))))
5434
5435 (defun gnus-update-missing-marks (missing)
5436   "Go through the list of MISSING articles and remove them from the mark lists."
5437   (when missing
5438     (let (var m)
5439       ;; Go through all types.
5440       (dolist (elem gnus-article-mark-lists)
5441         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5442           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5443           (when (symbol-value var)
5444             ;; This list has articles.  So we delete all missing
5445             ;; articles from it.
5446             (setq m missing)
5447             (while m
5448               (set var (delq (pop m) (symbol-value var))))))))))
5449
5450 (defun gnus-update-marks ()
5451   "Enter the various lists of marked articles into the newsgroup info list."
5452   (let ((types gnus-article-mark-lists)
5453         (info (gnus-get-info gnus-newsgroup-name))
5454         type list newmarked symbol delta-marks)
5455     (when info
5456       ;; Add all marks lists to the list of marks lists.
5457       (while (setq type (pop types))
5458         (setq list (symbol-value
5459                     (setq symbol
5460                           (intern (format "gnus-newsgroup-%s" (car type))))))
5461
5462         (when list
5463           ;; Get rid of the entries of the articles that have the
5464           ;; default score.
5465           (when (and (eq (cdr type) 'score)
5466                      gnus-save-score
5467                      list)
5468             (let* ((arts list)
5469                    (prev (cons nil list))
5470                    (all prev))
5471               (while arts
5472                 (if (or (not (consp (car arts)))
5473                         (= (cdar arts) gnus-summary-default-score))
5474                     (setcdr prev (cdr arts))
5475                   (setq prev arts))
5476                 (setq arts (cdr arts)))
5477               (setq list (cdr all)))))
5478
5479         (when (eq (cdr type) 'seen)
5480           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5481
5482         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5483           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5484
5485         (when (and (gnus-check-backend-function
5486                     'request-set-mark gnus-newsgroup-name)
5487                    (not (gnus-article-unpropagatable-p (cdr type))))
5488           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5489                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5490                  (add (gnus-remove-from-range
5491                        (gnus-copy-sequence list) old)))
5492             (when add
5493               (push (list add 'add (list (cdr type))) delta-marks))
5494             (when del
5495               (push (list del 'del (list (cdr type))) delta-marks))))
5496
5497         (when list
5498           (push (cons (cdr type) list) newmarked)))
5499
5500       (when delta-marks
5501         (unless (gnus-check-group gnus-newsgroup-name)
5502           (error "Can't open server for %s" gnus-newsgroup-name))
5503         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5504
5505       ;; Enter these new marks into the info of the group.
5506       (if (nthcdr 3 info)
5507           (setcar (nthcdr 3 info) newmarked)
5508         ;; Add the marks lists to the end of the info.
5509         (when newmarked
5510           (setcdr (nthcdr 2 info) (list newmarked))))
5511
5512       ;; Cut off the end of the info if there's nothing else there.
5513       (let ((i 5))
5514         (while (and (> i 2)
5515                     (not (nth i info)))
5516           (when (nthcdr (decf i) info)
5517             (setcdr (nthcdr i info) nil)))))))
5518
5519 (defun gnus-set-mode-line (where)
5520   "Set the mode line of the article or summary buffers.
5521 If WHERE is `summary', the summary mode line format will be used."
5522   ;; Is this mode line one we keep updated?
5523   (when (and (memq where gnus-updated-mode-lines)
5524              (symbol-value
5525               (intern (format "gnus-%s-mode-line-format-spec" where))))
5526     (let (mode-string)
5527       (save-excursion
5528         ;; We evaluate this in the summary buffer since these
5529         ;; variables are buffer-local to that buffer.
5530         (set-buffer gnus-summary-buffer)
5531         ;; We bind all these variables that are used in the `eval' form
5532         ;; below.
5533         (let* ((mformat (symbol-value
5534                          (intern
5535                           (format "gnus-%s-mode-line-format-spec" where))))
5536                (gnus-tmp-group-name (gnus-group-decoded-name
5537                                      gnus-newsgroup-name))
5538                (gnus-tmp-article-number (or gnus-current-article 0))
5539                (gnus-tmp-unread gnus-newsgroup-unreads)
5540                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5541                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5542                (gnus-tmp-unread-and-unselected
5543                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5544                             (zerop gnus-tmp-unselected))
5545                        "")
5546                       ((zerop gnus-tmp-unselected)
5547                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5548                       (t (format "{%d(+%d) more}"
5549                                  gnus-tmp-unread-and-unticked
5550                                  gnus-tmp-unselected))))
5551                (gnus-tmp-subject
5552                 (if (and gnus-current-headers
5553                          (vectorp gnus-current-headers))
5554                     (gnus-mode-string-quote
5555                      (mail-header-subject gnus-current-headers))
5556                   ""))
5557                bufname-length max-len
5558                gnus-tmp-header);; passed as argument to any user-format-funcs
5559           (setq mode-string (eval mformat))
5560           (setq bufname-length (if (string-match "%b" mode-string)
5561                                    (- (length
5562                                        (buffer-name
5563                                         (if (eq where 'summary)
5564                                             nil
5565                                           (get-buffer gnus-article-buffer))))
5566                                       2)
5567                                  0))
5568           (setq max-len (max 4 (if gnus-mode-non-string-length
5569                                    (- (window-width)
5570                                       gnus-mode-non-string-length
5571                                       bufname-length)
5572                                  (length mode-string))))
5573           ;; We might have to chop a bit of the string off...
5574           (when (> (length mode-string) max-len)
5575             (setq mode-string
5576                   (concat (gnus-truncate-string mode-string (- max-len 3))
5577                           "...")))
5578           ;; Pad the mode string a bit.
5579           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5580       ;; Update the mode line.
5581       (setq mode-line-buffer-identification
5582             (gnus-mode-line-buffer-identification (list mode-string)))
5583       (set-buffer-modified-p t))))
5584
5585 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5586   "Go through the HEADERS list and add all Xrefs to a hash table.
5587 The resulting hash table is returned, or nil if no Xrefs were found."
5588   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5589          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5590          (xref-hashtb (gnus-make-hashtable))
5591          start group entry number xrefs header)
5592     (while headers
5593       (setq header (pop headers))
5594       (when (and (setq xrefs (mail-header-xref header))
5595                  (not (memq (setq number (mail-header-number header))
5596                             unreads)))
5597         (setq start 0)
5598         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5599           (setq start (match-end 0))
5600           (setq group (if prefix
5601                           (concat prefix (substring xrefs (match-beginning 1)
5602                                                     (match-end 1)))
5603                         (substring xrefs (match-beginning 1) (match-end 1))))
5604           (setq number
5605                 (string-to-int (substring xrefs (match-beginning 2)
5606                                           (match-end 2))))
5607           (if (setq entry (gnus-gethash group xref-hashtb))
5608               (setcdr entry (cons number (cdr entry)))
5609             (gnus-sethash group (cons number nil) xref-hashtb)))))
5610     (and start xref-hashtb)))
5611
5612 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5613   "Look through all the headers and mark the Xrefs as read."
5614   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5615         name info xref-hashtb idlist method nth4)
5616     (save-excursion
5617       (set-buffer gnus-group-buffer)
5618       (when (setq xref-hashtb
5619                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5620         (mapatoms
5621          (lambda (group)
5622            (unless (string= from-newsgroup (setq name (symbol-name group)))
5623              (setq idlist (symbol-value group))
5624              ;; Dead groups are not updated.
5625              (and (prog1
5626                       (setq info (gnus-get-info name))
5627                     (when (stringp (setq nth4 (gnus-info-method info)))
5628                       (setq nth4 (gnus-server-to-method nth4))))
5629                   ;; Only do the xrefs if the group has the same
5630                   ;; select method as the group we have just read.
5631                   (or (gnus-methods-equal-p
5632                        nth4 (gnus-find-method-for-group from-newsgroup))
5633                       virtual
5634                       (equal nth4 (setq method (gnus-find-method-for-group
5635                                                 from-newsgroup)))
5636                       (and (equal (car nth4) (car method))
5637                            (equal (nth 1 nth4) (nth 1 method))))
5638                   gnus-use-cross-reference
5639                   (or (not (eq gnus-use-cross-reference t))
5640                       virtual
5641                       ;; Only do cross-references on subscribed
5642                       ;; groups, if that is what is wanted.
5643                       (<= (gnus-info-level info) gnus-level-subscribed))
5644                   (gnus-group-make-articles-read name idlist))))
5645          xref-hashtb)))))
5646
5647 (defun gnus-compute-read-articles (group articles)
5648   (let* ((entry (gnus-group-entry group))
5649          (info (nth 2 entry))
5650          (active (gnus-active group))
5651          ninfo)
5652     (when entry
5653       ;; First peel off all invalid article numbers.
5654       (when active
5655         (let ((ids articles)
5656               id first)
5657           (while (setq id (pop ids))
5658             (when (and first (> id (cdr active)))
5659               ;; We'll end up in this situation in one particular
5660               ;; obscure situation.  If you re-scan a group and get
5661               ;; a new article that is cross-posted to a different
5662               ;; group that has not been re-scanned, you might get
5663               ;; crossposted article that has a higher number than
5664               ;; Gnus believes possible.  So we re-activate this
5665               ;; group as well.  This might mean doing the
5666               ;; crossposting thingy will *increase* the number
5667               ;; of articles in some groups.  Tsk, tsk.
5668               (setq active (or (gnus-activate-group group) active)))
5669             (when (or (> id (cdr active))
5670                       (< id (car active)))
5671               (setq articles (delq id articles))))))
5672       ;; If the read list is nil, we init it.
5673       (if (and active
5674                (null (gnus-info-read info))
5675                (> (car active) 1))
5676           (setq ninfo (cons 1 (1- (car active))))
5677         (setq ninfo (gnus-info-read info)))
5678       ;; Then we add the read articles to the range.
5679       (gnus-add-to-range
5680        ninfo (setq articles (sort articles '<))))))
5681
5682 (defun gnus-group-make-articles-read (group articles)
5683   "Update the info of GROUP to say that ARTICLES are read."
5684   (let* ((num 0)
5685          (entry (gnus-group-entry group))
5686          (info (nth 2 entry))
5687          (active (gnus-active group))
5688          range)
5689     (when entry
5690       (setq range (gnus-compute-read-articles group articles))
5691       (with-current-buffer gnus-group-buffer
5692         (gnus-undo-register
5693           `(progn
5694              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5695              (gnus-info-set-read ',info ',(gnus-info-read info))
5696              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5697              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5698              (gnus-group-update-group ,group t))))
5699       ;; Add the read articles to the range.
5700       (gnus-info-set-read info range)
5701       (gnus-request-set-mark group (list (list range 'add '(read))))
5702       ;; Then we have to re-compute how many unread
5703       ;; articles there are in this group.
5704       (when active
5705         (cond
5706          ((not range)
5707           (setq num (- (1+ (cdr active)) (car active))))
5708          ((not (listp (cdr range)))
5709           (setq num (- (cdr active) (- (1+ (cdr range))
5710                                        (car range)))))
5711          (t
5712           (while range
5713             (if (numberp (car range))
5714                 (setq num (1+ num))
5715               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5716             (setq range (cdr range)))
5717           (setq num (- (cdr active) num))))
5718         ;; Update the number of unread articles.
5719         (setcar entry num)
5720         ;; Update the group buffer.
5721         (unless (gnus-ephemeral-group-p group)
5722           (gnus-group-update-group group t))))))
5723
5724 (defvar gnus-newsgroup-none-id 0)
5725
5726 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5727   (let ((cur nntp-server-buffer)
5728         (dependencies
5729          (or dependencies
5730              (save-excursion (set-buffer gnus-summary-buffer)
5731                              gnus-newsgroup-dependencies)))
5732         headers id end ref
5733         (mail-parse-charset gnus-newsgroup-charset)
5734         (mail-parse-ignored-charsets
5735          (save-excursion (condition-case nil
5736                              (set-buffer gnus-summary-buffer)
5737                            (error))
5738                          gnus-newsgroup-ignored-charsets)))
5739     (save-excursion
5740       (set-buffer nntp-server-buffer)
5741       ;; Translate all TAB characters into SPACE characters.
5742       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5743       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5744       (ietf-drums-unfold-fws)
5745       (gnus-run-hooks 'gnus-parse-headers-hook)
5746       (let ((case-fold-search t)
5747             in-reply-to header p lines chars ctype)
5748         (goto-char (point-min))
5749         ;; Search to the beginning of the next header.  Error messages
5750         ;; do not begin with 2 or 3.
5751         (while (re-search-forward "^[23][0-9]+ " nil t)
5752           (setq id nil
5753                 ref nil)
5754           ;; This implementation of this function, with nine
5755           ;; search-forwards instead of the one re-search-forward and
5756           ;; a case (which basically was the old function) is actually
5757           ;; about twice as fast, even though it looks messier.  You
5758           ;; can't have everything, I guess.  Speed and elegance
5759           ;; doesn't always go hand in hand.
5760           (setq
5761            header
5762            (make-full-mail-header
5763             ;; Number.
5764             (prog1
5765                 (read cur)
5766               (end-of-line)
5767               (setq p (point))
5768               (narrow-to-region (point)
5769                                 (or (and (search-forward "\n.\n" nil t)
5770                                          (- (point) 2))
5771                                     (point))))
5772             ;; Subject.
5773             (progn
5774               (goto-char p)
5775               (if (search-forward "\nsubject:" nil t)
5776                   (nnheader-header-value)
5777                 "(none)"))
5778             ;; From.
5779             (progn
5780               (goto-char p)
5781               (if (search-forward "\nfrom:" nil t)
5782                   (nnheader-header-value)
5783                 "(nobody)"))
5784             ;; Date.
5785             (progn
5786               (goto-char p)
5787               (if (search-forward "\ndate:" nil t)
5788                   (nnheader-header-value) ""))
5789             ;; Message-ID.
5790             (progn
5791               (goto-char p)
5792               (setq id (if (re-search-forward
5793                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5794                            ;; We do it this way to make sure the Message-ID
5795                            ;; is (somewhat) syntactically valid.
5796                            (buffer-substring (match-beginning 1)
5797                                              (match-end 1))
5798                          ;; If there was no message-id, we just fake one
5799                          ;; to make subsequent routines simpler.
5800                          (nnheader-generate-fake-message-id))))
5801             ;; References.
5802             (progn
5803               (goto-char p)
5804               (if (search-forward "\nreferences:" nil t)
5805                   (progn
5806                     (setq end (point))
5807                     (prog1
5808                         (nnheader-header-value)
5809                       (setq ref
5810                             (buffer-substring
5811                              (progn
5812                                ;; (end-of-line)
5813                                (search-backward ">" end t)
5814                                (1+ (point)))
5815                              (progn
5816                                (search-backward "<" end t)
5817                                (point))))))
5818                 ;; Get the references from the in-reply-to header if there
5819                 ;; were no references and the in-reply-to header looks
5820                 ;; promising.
5821                 (if (and (search-forward "\nin-reply-to:" nil t)
5822                          (setq in-reply-to (nnheader-header-value))
5823                          (string-match "<[^>]+>" in-reply-to))
5824                     (let (ref2)
5825                       (setq ref (substring in-reply-to (match-beginning 0)
5826                                            (match-end 0)))
5827                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5828                         (setq ref2 (substring in-reply-to (match-beginning 0)
5829                                               (match-end 0)))
5830                         (when (> (length ref2) (length ref))
5831                           (setq ref ref2)))
5832                       ref)
5833                   (setq ref nil))))
5834             ;; Chars.
5835             (progn
5836               (goto-char p)
5837               (if (search-forward "\nchars: " nil t)
5838                   (if (numberp (setq chars (ignore-errors (read cur))))
5839                       chars -1)
5840                 -1))
5841             ;; Lines.
5842             (progn
5843               (goto-char p)
5844               (if (search-forward "\nlines: " nil t)
5845                   (if (numberp (setq lines (ignore-errors (read cur))))
5846                       lines -1)
5847                 -1))
5848             ;; Xref.
5849             (progn
5850               (goto-char p)
5851               (and (search-forward "\nxref:" nil t)
5852                    (nnheader-header-value)))
5853             ;; Extra.
5854             (when gnus-extra-headers
5855               (let ((extra gnus-extra-headers)
5856                     out)
5857                 (while extra
5858                   (goto-char p)
5859                   (when (search-forward
5860                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5861                     (push (cons (car extra) (nnheader-header-value)) out))
5862                   (pop extra))
5863                 out))))
5864           (goto-char p)
5865           (if (and (search-forward "\ncontent-type: " nil t)
5866                    (setq ctype (nnheader-header-value)))
5867               (mime-entity-set-content-type-internal
5868                header (mime-parse-Content-Type ctype)))
5869           (when (equal id ref)
5870             (setq ref nil))
5871
5872           (when gnus-alter-header-function
5873             (funcall gnus-alter-header-function header)
5874             (setq id (mail-header-id header)
5875                   ref (gnus-parent-id (mail-header-references header))))
5876
5877           (when (setq header
5878                       (gnus-dependencies-add-header
5879                        header dependencies force-new))
5880             (push header headers))
5881           (goto-char (point-max))
5882           (widen))
5883         (nreverse headers)))))
5884
5885 ;; Goes through the xover lines and returns a list of vectors
5886 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5887                                                   force-new dependencies
5888                                                   group also-fetch-heads)
5889   "Parse the news overview data in the server buffer.
5890 Return a list of headers that match SEQUENCE (see
5891 `nntp-retrieve-headers')."
5892   ;; Get the Xref when the users reads the articles since most/some
5893   ;; NNTP servers do not include Xrefs when using XOVER.
5894   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5895   (let ((mail-parse-charset gnus-newsgroup-charset)
5896         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5897         (cur nntp-server-buffer)
5898         (dependencies (or dependencies gnus-newsgroup-dependencies))
5899         (allp (cond
5900                ((eq gnus-read-all-available-headers t)
5901                 t)
5902                ((stringp gnus-read-all-available-headers)
5903                 (string-match gnus-read-all-available-headers group))
5904                (t
5905                 nil)))
5906         number headers header)
5907     (save-excursion
5908       (set-buffer nntp-server-buffer)
5909       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5910       ;; Allow the user to mangle the headers before parsing them.
5911       (gnus-run-hooks 'gnus-parse-headers-hook)
5912       (goto-char (point-min))
5913       (gnus-parse-without-error
5914         (while (and (or sequence allp)
5915                     (not (eobp)))
5916           (setq number (read cur))
5917           (when (not allp)
5918             (while (and sequence
5919                         (< (car sequence) number))
5920               (setq sequence (cdr sequence))))
5921           (when (and (or allp
5922                          (and sequence
5923                               (eq number (car sequence))))
5924                      (progn
5925                        (setq sequence (cdr sequence))
5926                        (setq header (inline
5927                                       (gnus-nov-parse-line
5928                                        number dependencies force-new)))))
5929             (push header headers))
5930           (forward-line 1)))
5931       ;; A common bug in inn is that if you have posted an article and
5932       ;; then retrieves the active file, it will answer correctly --
5933       ;; the new article is included.  However, a NOV entry for the
5934       ;; article may not have been generated yet, so this may fail.
5935       ;; We work around this problem by retrieving the last few
5936       ;; headers using HEAD.
5937       (if (or (not also-fetch-heads)
5938               (not sequence))
5939           ;; We (probably) got all the headers.
5940           (nreverse headers)
5941         (let ((gnus-nov-is-evil t))
5942           (nconc
5943            (nreverse headers)
5944            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5945              (gnus-get-newsgroup-headers))))))))
5946
5947 (defun gnus-article-get-xrefs ()
5948   "Fill in the Xref value in `gnus-current-headers', if necessary.
5949 This is meant to be called in `gnus-article-internal-prepare-hook'."
5950   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5951                                  gnus-current-headers)))
5952     (or (not gnus-use-cross-reference)
5953         (not headers)
5954         (and (mail-header-xref headers)
5955              (not (string= (mail-header-xref headers) "")))
5956         (let ((case-fold-search t)
5957               xref)
5958           (save-restriction
5959             (nnheader-narrow-to-headers)
5960             (goto-char (point-min))
5961             (when (or (and (not (eobp))
5962                            (eq (downcase (char-after)) ?x)
5963                            (looking-at "Xref:"))
5964                       (search-forward "\nXref:" nil t))
5965               (goto-char (1+ (match-end 0)))
5966               (setq xref (buffer-substring (point) (point-at-eol)))
5967               (mail-header-set-xref headers xref)))))))
5968
5969 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5970   "Find article ID and insert the summary line for that article.
5971 OLD-HEADER can either be a header or a line number to insert
5972 the subject line on."
5973   (let* ((line (and (numberp old-header) old-header))
5974          (old-header (and (vectorp old-header) old-header))
5975          (header (cond ((and old-header use-old-header)
5976                         old-header)
5977                        ((and (numberp id)
5978                              (gnus-number-to-header id))
5979                         (gnus-number-to-header id))
5980                        (t
5981                         (gnus-read-header id))))
5982          (number (and (numberp id) id))
5983          d)
5984     (when header
5985       ;; Rebuild the thread that this article is part of and go to the
5986       ;; article we have fetched.
5987       (when (and (not gnus-show-threads)
5988                  old-header)
5989         (when (and number
5990                    (setq d (gnus-data-find (mail-header-number old-header))))
5991           (goto-char (gnus-data-pos d))
5992           (gnus-data-remove
5993            number
5994            (- (point-at-bol)
5995               (prog1
5996                   (1+ (point-at-eol))
5997                 (gnus-delete-line))))))
5998       (when old-header
5999         (mail-header-set-number header (mail-header-number old-header)))
6000       (setq gnus-newsgroup-sparse
6001             (delq (setq number (mail-header-number header))
6002                   gnus-newsgroup-sparse))
6003       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6004       (push number gnus-newsgroup-limit)
6005       (gnus-rebuild-thread (mail-header-id header) line)
6006       (gnus-summary-goto-subject number nil t))
6007     (when (and (numberp number)
6008                (> number 0))
6009       ;; We have to update the boundaries even if we can't fetch the
6010       ;; article if ID is a number -- so that the next `P' or `N'
6011       ;; command will fetch the previous (or next) article even
6012       ;; if the one we tried to fetch this time has been canceled.
6013       (when (> number gnus-newsgroup-end)
6014         (setq gnus-newsgroup-end number))
6015       (when (< number gnus-newsgroup-begin)
6016         (setq gnus-newsgroup-begin number))
6017       (setq gnus-newsgroup-unselected
6018             (delq number gnus-newsgroup-unselected)))
6019     ;; Report back a success?
6020     (and header (mail-header-number header))))
6021
6022 ;;; Process/prefix in the summary buffer
6023
6024 (defun gnus-summary-work-articles (n)
6025   "Return a list of articles to be worked upon.
6026 The prefix argument, the list of process marked articles, and the
6027 current article will be taken into consideration."
6028   (save-excursion
6029     (set-buffer gnus-summary-buffer)
6030     (cond
6031      (n
6032       ;; A numerical prefix has been given.
6033       (setq n (prefix-numeric-value n))
6034       (let ((backward (< n 0))
6035             (n (abs (prefix-numeric-value n)))
6036             articles article)
6037         (save-excursion
6038           (while
6039               (and (> n 0)
6040                    (push (setq article (gnus-summary-article-number))
6041                          articles)
6042                    (if backward
6043                        (gnus-summary-find-prev nil article)
6044                      (gnus-summary-find-next nil article)))
6045             (decf n)))
6046         (nreverse articles)))
6047      ((and (gnus-region-active-p) (mark))
6048       (message "region active")
6049       ;; Work on the region between point and mark.
6050       (let ((max (max (point) (mark)))
6051             articles article)
6052         (save-excursion
6053           (goto-char (min (point) (mark)))
6054           (while
6055               (and
6056                (push (setq article (gnus-summary-article-number)) articles)
6057                (gnus-summary-find-next nil article)
6058                (< (point) max)))
6059           (nreverse articles))))
6060      (gnus-newsgroup-processable
6061       ;; There are process-marked articles present.
6062       ;; Save current state.
6063       (gnus-summary-save-process-mark)
6064       ;; Return the list.
6065       (reverse gnus-newsgroup-processable))
6066      (t
6067       ;; Just return the current article.
6068       (list (gnus-summary-article-number))))))
6069
6070 (defmacro gnus-summary-iterate (arg &rest forms)
6071   "Iterate over the process/prefixed articles and do FORMS.
6072 ARG is the interactive prefix given to the command.  FORMS will be
6073 executed with point over the summary line of the articles."
6074   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6075     `(let ((,articles (gnus-summary-work-articles ,arg)))
6076        (while ,articles
6077          (gnus-summary-goto-subject (car ,articles))
6078          ,@forms
6079          (pop ,articles)))))
6080
6081 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6082 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6083
6084 (defun gnus-summary-save-process-mark ()
6085   "Push the current set of process marked articles on the stack."
6086   (interactive)
6087   (push (copy-sequence gnus-newsgroup-processable)
6088         gnus-newsgroup-process-stack))
6089
6090 (defun gnus-summary-kill-process-mark ()
6091   "Push the current set of process marked articles on the stack and unmark."
6092   (interactive)
6093   (gnus-summary-save-process-mark)
6094   (gnus-summary-unmark-all-processable))
6095
6096 (defun gnus-summary-yank-process-mark ()
6097   "Pop the last process mark state off the stack and restore it."
6098   (interactive)
6099   (unless gnus-newsgroup-process-stack
6100     (error "Empty mark stack"))
6101   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6102
6103 (defun gnus-summary-process-mark-set (set)
6104   "Make SET into the current process marked articles."
6105   (gnus-summary-unmark-all-processable)
6106   (while set
6107     (gnus-summary-set-process-mark (pop set))))
6108
6109 ;;; Searching and stuff
6110
6111 (defun gnus-summary-search-group (&optional backward use-level)
6112   "Search for next unread newsgroup.
6113 If optional argument BACKWARD is non-nil, search backward instead."
6114   (save-excursion
6115     (set-buffer gnus-group-buffer)
6116     (when (gnus-group-search-forward
6117            backward nil (if use-level (gnus-group-group-level) nil))
6118       (gnus-group-group-name))))
6119
6120 (defun gnus-summary-best-group (&optional exclude-group)
6121   "Find the name of the best unread group.
6122 If EXCLUDE-GROUP, do not go to this group."
6123   (save-excursion
6124     (set-buffer gnus-group-buffer)
6125     (save-excursion
6126       (gnus-group-best-unread-group exclude-group))))
6127
6128 (defun gnus-summary-find-next (&optional unread article backward)
6129   (if backward
6130       (gnus-summary-find-prev unread article)
6131     (let* ((dummy (gnus-summary-article-intangible-p))
6132            (article (or article (gnus-summary-article-number)))
6133            (data (gnus-data-find-list article))
6134            result)
6135       (when (and (not dummy)
6136                  (or (not gnus-summary-check-current)
6137                      (not unread)
6138                      (not (gnus-data-unread-p (car data)))))
6139         (setq data (cdr data)))
6140       (when (setq result
6141                   (if unread
6142                       (progn
6143                         (while data
6144                           (unless (memq (gnus-data-number (car data))
6145                                         (cond
6146                                          ((eq gnus-auto-goto-ignores
6147                                               'always-undownloaded)
6148                                           gnus-newsgroup-undownloaded)
6149                                          (gnus-plugged
6150                                           nil)
6151                                          ((eq gnus-auto-goto-ignores
6152                                               'unfetched)
6153                                           gnus-newsgroup-unfetched)
6154                                          ((eq gnus-auto-goto-ignores
6155                                               'undownloaded)
6156                                           gnus-newsgroup-undownloaded)))
6157                             (when (gnus-data-unread-p (car data))
6158                               (setq result (car data)
6159                                     data nil)))
6160                           (setq data (cdr data)))
6161                         result)
6162                     (car data)))
6163         (goto-char (gnus-data-pos result))
6164         (gnus-data-number result)))))
6165
6166 (defun gnus-summary-find-prev (&optional unread article)
6167   (let* ((eobp (eobp))
6168          (article (or article (gnus-summary-article-number)))
6169          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6170          result)
6171     (when (and (not eobp)
6172                (or (not gnus-summary-check-current)
6173                    (not unread)
6174                    (not (gnus-data-unread-p (car data)))))
6175       (setq data (cdr data)))
6176     (when (setq result
6177                 (if unread
6178                     (progn
6179                       (while data
6180                         (unless (memq (gnus-data-number (car data))
6181                                       (cond
6182                                        ((eq gnus-auto-goto-ignores
6183                                             'always-undownloaded)
6184                                         gnus-newsgroup-undownloaded)
6185                                        (gnus-plugged
6186                                         nil)
6187                                        ((eq gnus-auto-goto-ignores
6188                                             'unfetched)
6189                                         gnus-newsgroup-unfetched)
6190                                        ((eq gnus-auto-goto-ignores
6191                                             'undownloaded)
6192                                         gnus-newsgroup-undownloaded)))
6193                           (when (gnus-data-unread-p (car data))
6194                             (setq result (car data)
6195                                   data nil)))
6196                         (setq data (cdr data)))
6197                       result)
6198                   (car data)))
6199       (goto-char (gnus-data-pos result))
6200       (gnus-data-number result))))
6201
6202 (defun gnus-summary-find-subject (subject &optional unread backward article)
6203   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6204          (article (or article (gnus-summary-article-number)))
6205          (articles (gnus-data-list backward))
6206          (arts (gnus-data-find-list article articles))
6207          result)
6208     (when (or (not gnus-summary-check-current)
6209               (not unread)
6210               (not (gnus-data-unread-p (car arts))))
6211       (setq arts (cdr arts)))
6212     (while arts
6213       (and (or (not unread)
6214                (gnus-data-unread-p (car arts)))
6215            (vectorp (gnus-data-header (car arts)))
6216            (gnus-subject-equal
6217             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6218            (setq result (car arts)
6219                  arts nil))
6220       (setq arts (cdr arts)))
6221     (and result
6222          (goto-char (gnus-data-pos result))
6223          (gnus-data-number result))))
6224
6225 (defun gnus-summary-search-forward (&optional unread subject backward)
6226   "Search forward for an article.
6227 If UNREAD, look for unread articles.  If SUBJECT, look for
6228 articles with that subject.  If BACKWARD, search backward instead."
6229   (cond (subject (gnus-summary-find-subject subject unread backward))
6230         (backward (gnus-summary-find-prev unread))
6231         (t (gnus-summary-find-next unread))))
6232
6233 (defun gnus-recenter (&optional n)
6234   "Center point in window and redisplay frame.
6235 Also do horizontal recentering."
6236   (interactive "P")
6237   (when (and gnus-auto-center-summary
6238              (not (eq gnus-auto-center-summary 'vertical)))
6239     (gnus-horizontal-recenter))
6240   (recenter n))
6241
6242 (defun gnus-summary-recenter ()
6243   "Center point in the summary window.
6244 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6245 displayed, no centering will be performed."
6246   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6247   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6248   (interactive)
6249   ;; The user has to want it.
6250   (when gnus-auto-center-summary
6251     (let* ((top (cond ((< (window-height) 4) 0)
6252                       ((< (window-height) 7) 1)
6253                       (t (if (numberp gnus-auto-center-summary)
6254                              gnus-auto-center-summary
6255                            2))))
6256            (height (1- (window-height)))
6257            (bottom (save-excursion (goto-char (point-max))
6258                                    (forward-line (- height))
6259                                    (point)))
6260            (window (get-buffer-window (current-buffer))))
6261       (when (get-buffer-window gnus-article-buffer)
6262         ;; Only do recentering when the article buffer is displayed,
6263         ;; Set the window start to either `bottom', which is the biggest
6264         ;; possible valid number, or the second line from the top,
6265         ;; whichever is the least.
6266         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6267           (if (> bottom top-pos)
6268               ;; Keep the second line from the top visible
6269               (set-window-start window top-pos t)
6270             ;; Try to keep the bottom line visible; if it's partially
6271             ;; obscured, either scroll one more line to make it fully
6272             ;; visible, or revert to using TOP-POS.
6273             (save-excursion
6274               (goto-char (point-max))
6275               (forward-line -1)
6276               (let ((last-line-start (point)))
6277                 (goto-char bottom)
6278                 (set-window-start window (point) t)
6279                 (when (not (pos-visible-in-window-p last-line-start window))
6280                   (forward-line 1)
6281                   (set-window-start window (min (point) top-pos) t)))))))
6282       ;; Do horizontal recentering while we're at it.
6283       (when (and (get-buffer-window (current-buffer) t)
6284                  (not (eq gnus-auto-center-summary 'vertical)))
6285         (let ((selected (selected-window)))
6286           (select-window (get-buffer-window (current-buffer) t))
6287           (gnus-summary-position-point)
6288           (gnus-horizontal-recenter)
6289           (select-window selected))))))
6290
6291 (defun gnus-summary-jump-to-group (newsgroup)
6292   "Move point to NEWSGROUP in group mode buffer."
6293   ;; Keep update point of group mode buffer if visible.
6294   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6295       (save-window-excursion
6296         ;; Take care of tree window mode.
6297         (when (get-buffer-window gnus-group-buffer)
6298           (pop-to-buffer gnus-group-buffer))
6299         (gnus-group-jump-to-group newsgroup))
6300     (save-excursion
6301       ;; Take care of tree window mode.
6302       (if (get-buffer-window gnus-group-buffer)
6303           (pop-to-buffer gnus-group-buffer)
6304         (set-buffer gnus-group-buffer))
6305       (gnus-group-jump-to-group newsgroup))))
6306
6307 ;; This function returns a list of article numbers based on the
6308 ;; difference between the ranges of read articles in this group and
6309 ;; the range of active articles.
6310 (defun gnus-list-of-unread-articles (group)
6311   (let* ((read (gnus-info-read (gnus-get-info group)))
6312          (active (or (gnus-active group) (gnus-activate-group group)))
6313          (last (cdr active))
6314          first nlast unread)
6315     ;; If none are read, then all are unread.
6316     (if (not read)
6317         (setq first (car active))
6318       ;; If the range of read articles is a single range, then the
6319       ;; first unread article is the article after the last read
6320       ;; article.  Sounds logical, doesn't it?
6321       (if (and (not (listp (cdr read)))
6322                (or (< (car read) (car active))
6323                    (progn (setq read (list read))
6324                           nil)))
6325           (setq first (max (car active) (1+ (cdr read))))
6326         ;; `read' is a list of ranges.
6327         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6328                                   (caar read)))
6329                   1)
6330           (setq first (car active)))
6331         (while read
6332           (when first
6333             (while (< first nlast)
6334               (setq unread (cons first unread)
6335                     first (1+ first))))
6336           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6337           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6338           (setq read (cdr read)))))
6339     ;; And add the last unread articles.
6340     (while (<= first last)
6341       (setq unread (cons first unread)
6342             first (1+ first)))
6343     ;; Return the list of unread articles.
6344     (delq 0 (nreverse unread))))
6345
6346 (defun gnus-list-of-read-articles (group)
6347   "Return a list of unread, unticked and non-dormant articles."
6348   (let* ((info (gnus-get-info group))
6349          (marked (gnus-info-marks info))
6350          (active (gnus-active group)))
6351     (and info active
6352          (gnus-list-range-difference
6353           (gnus-list-range-difference
6354            (gnus-sorted-complement
6355             (gnus-uncompress-range active)
6356             (gnus-list-of-unread-articles group))
6357            (cdr (assq 'dormant marked)))
6358           (cdr (assq 'tick marked))))))
6359
6360 ;; This function returns a sequence of article numbers based on the
6361 ;; difference between the ranges of read articles in this group and
6362 ;; the range of active articles.
6363 (defun gnus-sequence-of-unread-articles (group)
6364   (let* ((read (gnus-info-read (gnus-get-info group)))
6365          (active (or (gnus-active group) (gnus-activate-group group)))
6366          (last (cdr active))
6367          first nlast unread)
6368     ;; If none are read, then all are unread.
6369     (if (not read)
6370         (setq first (car active))
6371       ;; If the range of read articles is a single range, then the
6372       ;; first unread article is the article after the last read
6373       ;; article.  Sounds logical, doesn't it?
6374       (if (and (not (listp (cdr read)))
6375                (or (< (car read) (car active))
6376                    (progn (setq read (list read))
6377                           nil)))
6378           (setq first (max (car active) (1+ (cdr read))))
6379         ;; `read' is a list of ranges.
6380         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6381                                   (caar read)))
6382                   1)
6383           (setq first (car active)))
6384         (while read
6385           (when first
6386             (push (cons first nlast) unread))
6387           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6388           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6389           (setq read (cdr read)))))
6390     ;; And add the last unread articles.
6391     (cond ((< first last)
6392            (push (cons first last) unread))
6393           ((= first last)
6394            (push first unread)))
6395     ;; Return the sequence of unread articles.
6396     (delq 0 (nreverse unread))))
6397
6398 ;; Various summary commands
6399
6400 (defun gnus-summary-select-article-buffer ()
6401   "Reconfigure windows to show article buffer."
6402   (interactive)
6403   (if (not (gnus-buffer-live-p gnus-article-buffer))
6404       (error "There is no article buffer for this summary buffer")
6405     (gnus-configure-windows 'article)
6406     (select-window (get-buffer-window gnus-article-buffer))))
6407
6408 (defun gnus-summary-universal-argument (arg)
6409   "Perform any operation on all articles that are process/prefixed."
6410   (interactive "P")
6411   (let ((articles (gnus-summary-work-articles arg))
6412         func article)
6413     (if (eq
6414          (setq
6415           func
6416           (key-binding
6417            (read-key-sequence
6418             (substitute-command-keys
6419              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6420          'undefined)
6421         (gnus-error 1 "Undefined key")
6422       (save-excursion
6423         (while articles
6424           (gnus-summary-goto-subject (setq article (pop articles)))
6425           (let (gnus-newsgroup-processable)
6426             (command-execute func))
6427           (gnus-summary-remove-process-mark article)))))
6428   (gnus-summary-position-point))
6429
6430 (defun gnus-summary-toggle-truncation (&optional arg)
6431   "Toggle truncation of summary lines.
6432 With ARG, turn line truncation on if ARG is positive."
6433   (interactive "P")
6434   (setq truncate-lines
6435         (if (null arg) (not truncate-lines)
6436           (> (prefix-numeric-value arg) 0)))
6437   (redraw-display))
6438
6439 (defun gnus-summary-find-for-reselect ()
6440   "Return the number of an article to stay on across a reselect.
6441 The current article is considered, then following articles, then previous
6442 articles.  An article is sought which is not cancelled and isn't a temporary
6443 insertion from another group.  If there's no such then return a dummy 0."
6444   (let (found)
6445     (dolist (rev '(nil t))
6446       (unless found      ; don't demand the reverse list if we don't need it
6447         (let ((data (gnus-data-find-list
6448                      (gnus-summary-article-number) (gnus-data-list rev))))
6449           (while (and data (not found))
6450             (if (and (< 0 (gnus-data-number (car data)))
6451                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6452                 (setq found (gnus-data-number (car data))))
6453             (setq data (cdr data))))))
6454     (or found 0)))
6455
6456 (defun gnus-summary-reselect-current-group (&optional all rescan)
6457   "Exit and then reselect the current newsgroup.
6458 The prefix argument ALL means to select all articles."
6459   (interactive "P")
6460   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6461     (error "Ephemeral groups can't be reselected"))
6462   (let ((current-subject (gnus-summary-find-for-reselect))
6463         (group gnus-newsgroup-name))
6464     (setq gnus-newsgroup-begin nil)
6465     (gnus-summary-exit nil 'leave-hidden)
6466     ;; We have to adjust the point of group mode buffer because
6467     ;; point was moved to the next unread newsgroup by exiting.
6468     (gnus-summary-jump-to-group group)
6469     (when rescan
6470       (save-excursion
6471         (save-window-excursion
6472           ;; Don't show group contents.
6473           (set-window-start (selected-window) (point-max))
6474           (gnus-group-get-new-news-this-group 1))))
6475     (gnus-group-read-group all t)
6476     (gnus-summary-goto-subject current-subject nil t)))
6477
6478 (defun gnus-summary-rescan-group (&optional all)
6479   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6480   (interactive "P")
6481   (gnus-summary-reselect-current-group all t))
6482
6483 (defun gnus-summary-update-info (&optional non-destructive)
6484   (save-excursion
6485     (let ((group gnus-newsgroup-name))
6486       (when group
6487         (when gnus-newsgroup-kill-headers
6488           (setq gnus-newsgroup-killed
6489                 (gnus-compress-sequence
6490                  (gnus-sorted-union
6491                   (gnus-list-range-intersection
6492                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6493                   gnus-newsgroup-unreads)
6494                  t)))
6495         (unless (listp (cdr gnus-newsgroup-killed))
6496           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6497         (let ((headers gnus-newsgroup-headers))
6498           ;; Set the new ranges of read articles.
6499           (save-excursion
6500             (set-buffer gnus-group-buffer)
6501             (gnus-undo-force-boundary))
6502           (gnus-update-read-articles
6503            group (gnus-sorted-union
6504                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6505           ;; Set the current article marks.
6506           (let ((gnus-newsgroup-scored
6507                  (if (and (not gnus-save-score)
6508                           (not non-destructive))
6509                      nil
6510                    gnus-newsgroup-scored)))
6511             (save-excursion
6512               (gnus-update-marks)))
6513           ;; Do the cross-ref thing.
6514           (when gnus-use-cross-reference
6515             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6516           ;; Do not switch windows but change the buffer to work.
6517           (set-buffer gnus-group-buffer)
6518           (unless (gnus-ephemeral-group-p group)
6519             (gnus-group-update-group group)))))))
6520
6521 (defun gnus-summary-save-newsrc (&optional force)
6522   "Save the current number of read/marked articles in the dribble buffer.
6523 The dribble buffer will then be saved.
6524 If FORCE (the prefix), also save the .newsrc file(s)."
6525   (interactive "P")
6526   (gnus-summary-update-info t)
6527   (if force
6528       (gnus-save-newsrc-file)
6529     (gnus-dribble-save)))
6530
6531 (defun gnus-summary-exit (&optional temporary leave-hidden)
6532   "Exit reading current newsgroup, and then return to group selection mode.
6533 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6534   (interactive)
6535   (gnus-set-global-variables)
6536   (gnus-kill-save-kill-buffer)
6537   (gnus-async-halt-prefetch)
6538   (let* ((group gnus-newsgroup-name)
6539          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6540          (gnus-group-is-exiting-p t)
6541          (mode major-mode)
6542          (group-point nil)
6543          (buf (current-buffer)))
6544     (unless quit-config
6545       ;; Do adaptive scoring, and possibly save score files.
6546       (when gnus-newsgroup-adaptive
6547         (gnus-score-adaptive))
6548       (when gnus-use-scoring
6549         (gnus-score-save)))
6550     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6551     ;; If we have several article buffers, we kill them at exit.
6552     (unless gnus-single-article-buffer
6553       (gnus-kill-buffer gnus-original-article-buffer)
6554       (setq gnus-article-current nil))
6555     (when gnus-use-cache
6556       (gnus-cache-possibly-remove-articles)
6557       (gnus-cache-save-buffers))
6558     (gnus-async-prefetch-remove-group group)
6559     (when gnus-suppress-duplicates
6560       (gnus-dup-enter-articles))
6561     (when gnus-use-trees
6562       (gnus-tree-close group))
6563     (when gnus-use-cache
6564       (gnus-cache-write-active))
6565     ;; Remove entries for this group.
6566     (nnmail-purge-split-history (gnus-group-real-name group))
6567     ;; Make all changes in this group permanent.
6568     (unless quit-config
6569       (gnus-run-hooks 'gnus-exit-group-hook)
6570       (gnus-summary-update-info))
6571     (gnus-close-group group)
6572     ;; Make sure where we were, and go to next newsgroup.
6573     (set-buffer gnus-group-buffer)
6574     (unless quit-config
6575       (gnus-group-jump-to-group group))
6576     (gnus-run-hooks 'gnus-summary-exit-hook)
6577     (unless (or quit-config
6578                 ;; If this group has disappeared from the summary
6579                 ;; buffer, don't skip forwards.
6580                 (not (string= group (gnus-group-group-name))))
6581       (gnus-group-next-unread-group 1))
6582     (setq group-point (point))
6583     (if temporary
6584         nil                             ;Nothing to do.
6585       ;; If we have several article buffers, we kill them at exit.
6586       (unless gnus-single-article-buffer
6587         (gnus-kill-buffer gnus-article-buffer)
6588         (gnus-kill-buffer gnus-original-article-buffer)
6589         (setq gnus-article-current nil))
6590       (set-buffer buf)
6591       (if (not gnus-kill-summary-on-exit)
6592           (progn
6593             (gnus-deaden-summary)
6594             (setq mode nil))
6595         ;; We set all buffer-local variables to nil.  It is unclear why
6596         ;; this is needed, but if we don't, buffer-local variables are
6597         ;; not garbage-collected, it seems.  This would the lead to en
6598         ;; ever-growing Emacs.
6599         (gnus-summary-clear-local-variables)
6600         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6601           (gnus-summary-clear-local-variables))
6602         (when (get-buffer gnus-article-buffer)
6603           (bury-buffer gnus-article-buffer))
6604         ;; We clear the global counterparts of the buffer-local
6605         ;; variables as well, just to be on the safe side.
6606         (set-buffer gnus-group-buffer)
6607         (gnus-summary-clear-local-variables)
6608         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6609           (gnus-summary-clear-local-variables))
6610         ;; Return to group mode buffer.
6611         (when (eq mode 'gnus-summary-mode)
6612           (gnus-kill-buffer buf)))
6613       (setq gnus-current-select-method gnus-select-method)
6614       (if leave-hidden
6615           (set-buffer gnus-group-buffer)
6616         (pop-to-buffer gnus-group-buffer))
6617       (if (not quit-config)
6618           (progn
6619             (goto-char group-point)
6620             (unless leave-hidden
6621               (gnus-configure-windows 'group 'force))
6622             (unless (pos-visible-in-window-p)
6623               (forward-line (/ (static-if (featurep 'xemacs)
6624                                    (window-displayed-height)
6625                                  (1- (window-height)))
6626                                -2))
6627               (set-window-start (selected-window) (point))
6628               (goto-char group-point)))
6629         (gnus-handle-ephemeral-exit quit-config))
6630       ;; Clear the current group name.
6631       (unless quit-config
6632         (setq gnus-newsgroup-name nil)))))
6633
6634 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6635 (defun gnus-summary-exit-no-update (&optional no-questions)
6636   "Quit reading current newsgroup without updating read article info."
6637   (interactive)
6638   (let* ((group gnus-newsgroup-name)
6639          (gnus-group-is-exiting-p t)
6640          (gnus-group-is-exiting-without-update-p t)
6641          (quit-config (gnus-group-quit-config group)))
6642     (when (or no-questions
6643               gnus-expert-user
6644               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6645       (gnus-async-halt-prefetch)
6646       (run-hooks 'gnus-summary-prepare-exit-hook)
6647       ;; If we have several article buffers, we kill them at exit.
6648       (unless gnus-single-article-buffer
6649         (gnus-kill-buffer gnus-article-buffer)
6650         (gnus-kill-buffer gnus-original-article-buffer)
6651         (setq gnus-article-current nil))
6652       (if (not gnus-kill-summary-on-exit)
6653           (gnus-deaden-summary)
6654         (gnus-close-group group)
6655         (gnus-summary-clear-local-variables)
6656         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6657           (gnus-summary-clear-local-variables))
6658         (set-buffer gnus-group-buffer)
6659         (gnus-summary-clear-local-variables)
6660         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6661           (gnus-summary-clear-local-variables))
6662         (gnus-kill-buffer gnus-summary-buffer))
6663       (unless gnus-single-article-buffer
6664         (setq gnus-article-current nil))
6665       (when gnus-use-trees
6666         (gnus-tree-close group))
6667       (gnus-async-prefetch-remove-group group)
6668       (when (get-buffer gnus-article-buffer)
6669         (bury-buffer gnus-article-buffer))
6670       ;; Return to the group buffer.
6671       (gnus-configure-windows 'group 'force)
6672       ;; Clear the current group name.
6673       (setq gnus-newsgroup-name nil)
6674       (unless (gnus-ephemeral-group-p group)
6675         (gnus-group-update-group group))
6676       (when (equal (gnus-group-group-name) group)
6677         (gnus-group-next-unread-group 1))
6678       (when quit-config
6679         (gnus-handle-ephemeral-exit quit-config)))))
6680
6681 (defun gnus-handle-ephemeral-exit (quit-config)
6682   "Handle movement when leaving an ephemeral group.
6683 The state which existed when entering the ephemeral is reset."
6684   (if (not (buffer-name (car quit-config)))
6685       (gnus-configure-windows 'group 'force)
6686     (set-buffer (car quit-config))
6687     (cond ((eq major-mode 'gnus-summary-mode)
6688            (gnus-set-global-variables))
6689           ((eq major-mode 'gnus-article-mode)
6690            (save-excursion
6691              ;; The `gnus-summary-buffer' variable may point
6692              ;; to the old summary buffer when using a single
6693              ;; article buffer.
6694              (unless (gnus-buffer-live-p gnus-summary-buffer)
6695                (set-buffer gnus-group-buffer))
6696              (set-buffer gnus-summary-buffer)
6697              (gnus-set-global-variables))))
6698     (if (or (eq (cdr quit-config) 'article)
6699             (eq (cdr quit-config) 'pick))
6700         (progn
6701           ;; The current article may be from the ephemeral group
6702           ;; thus it is best that we reload this article
6703           ;;
6704           ;; If we're exiting from a large digest, this can be
6705           ;; extremely slow.  So, it's better not to reload it. -- jh.
6706           ;;(gnus-summary-show-article)
6707           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6708               (gnus-configure-windows 'pick 'force)
6709             (gnus-configure-windows (cdr quit-config) 'force)))
6710       (gnus-configure-windows (cdr quit-config) 'force))
6711     (when (eq major-mode 'gnus-summary-mode)
6712       (gnus-summary-next-subject 1 nil t)
6713       (gnus-summary-recenter)
6714       (gnus-summary-position-point))))
6715
6716 (defun gnus-summary-preview-mime-message ()
6717   "MIME decode and play this message."
6718   (interactive)
6719   (let ((gnus-break-pages nil)
6720         (gnus-show-mime t))
6721     (gnus-summary-select-article gnus-show-all-headers t))
6722   (let ((w (get-buffer-window gnus-article-buffer)))
6723     (when w
6724       (select-window (get-buffer-window gnus-article-buffer)))))
6725
6726 ;;; Dead summaries.
6727
6728 (defvar gnus-dead-summary-mode-map nil)
6729
6730 (unless gnus-dead-summary-mode-map
6731   (setq gnus-dead-summary-mode-map (make-keymap))
6732   (suppress-keymap gnus-dead-summary-mode-map)
6733   (substitute-key-definition
6734    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6735   (dolist (key '("\C-d" "\r" "\177" [delete]))
6736     (define-key gnus-dead-summary-mode-map
6737       key 'gnus-summary-wake-up-the-dead))
6738   (dolist (key '("q" "Q"))
6739     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6740
6741 (defvar gnus-dead-summary-mode nil
6742   "Minor mode for Gnus summary buffers.")
6743
6744 (defun gnus-dead-summary-mode (&optional arg)
6745   "Minor mode for Gnus summary buffers."
6746   (interactive "P")
6747   (when (eq major-mode 'gnus-summary-mode)
6748     (make-local-variable 'gnus-dead-summary-mode)
6749     (setq gnus-dead-summary-mode
6750           (if (null arg) (not gnus-dead-summary-mode)
6751             (> (prefix-numeric-value arg) 0)))
6752     (when gnus-dead-summary-mode
6753       (add-minor-mode
6754        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6755
6756 (defun gnus-deaden-summary ()
6757   "Make the current summary buffer into a dead summary buffer."
6758   ;; Kill any previous dead summary buffer.
6759   (when (and gnus-dead-summary
6760              (buffer-name gnus-dead-summary))
6761     (save-excursion
6762       (set-buffer gnus-dead-summary)
6763       (when gnus-dead-summary-mode
6764         (kill-buffer (current-buffer)))))
6765   ;; Make this the current dead summary.
6766   (setq gnus-dead-summary (current-buffer))
6767   (gnus-dead-summary-mode 1)
6768   (let ((name (buffer-name)))
6769     (when (string-match "Summary" name)
6770       (rename-buffer
6771        (concat (substring name 0 (match-beginning 0)) "Dead "
6772                (substring name (match-beginning 0)))
6773        t)
6774       (bury-buffer))))
6775
6776 (defun gnus-kill-or-deaden-summary (buffer)
6777   "Kill or deaden the summary BUFFER."
6778   (save-excursion
6779     (when (and (buffer-name buffer)
6780                (not gnus-single-article-buffer))
6781       (save-excursion
6782         (set-buffer buffer)
6783         (gnus-kill-buffer gnus-article-buffer)
6784         (gnus-kill-buffer gnus-original-article-buffer)))
6785     (cond
6786      ;; Kill the buffer.
6787      (gnus-kill-summary-on-exit
6788       (when (and gnus-use-trees
6789                  (gnus-buffer-exists-p buffer))
6790         (save-excursion
6791           (set-buffer buffer)
6792           (gnus-tree-close gnus-newsgroup-name)))
6793       (gnus-kill-buffer buffer))
6794      ;; Deaden the buffer.
6795      ((gnus-buffer-exists-p buffer)
6796       (save-excursion
6797         (set-buffer buffer)
6798         (gnus-deaden-summary))))))
6799
6800 (defun gnus-summary-wake-up-the-dead (&rest args)
6801   "Wake up the dead summary buffer."
6802   (interactive)
6803   (gnus-dead-summary-mode -1)
6804   (let ((name (buffer-name)))
6805     (when (string-match "Dead " name)
6806       (rename-buffer
6807        (concat (substring name 0 (match-beginning 0))
6808                (substring name (match-end 0)))
6809        t)))
6810   (gnus-message 3 "This dead summary is now alive again"))
6811
6812 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6813 (defun gnus-summary-fetch-faq (&optional faq-dir)
6814   "Fetch the FAQ for the current group.
6815 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6816 in."
6817   (interactive
6818    (list
6819     (when current-prefix-arg
6820       (completing-read
6821        "FAQ dir: " (and (listp gnus-group-faq-directory)
6822                         (mapcar (lambda (file) (list file))
6823                                 gnus-group-faq-directory))))))
6824   (let (gnus-faq-buffer)
6825     (when (setq gnus-faq-buffer
6826                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6827       (gnus-configure-windows 'summary-faq))))
6828
6829 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6830 (defun gnus-summary-describe-group (&optional force)
6831   "Describe the current newsgroup."
6832   (interactive "P")
6833   (gnus-group-describe-group force gnus-newsgroup-name))
6834
6835 (defun gnus-summary-describe-briefly ()
6836   "Describe summary mode commands briefly."
6837   (interactive)
6838   (gnus-message 6 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info        \\[gnus-summary-describe-briefly]:This help")))
6839
6840 ;; Walking around group mode buffer from summary mode.
6841
6842 (defun gnus-summary-next-group (&optional no-article target-group backward)
6843   "Exit current newsgroup and then select next unread newsgroup.
6844 If prefix argument NO-ARTICLE is non-nil, no article is selected
6845 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6846 previous group instead."
6847   (interactive "P")
6848   ;; Stop pre-fetching.
6849   (gnus-async-halt-prefetch)
6850   (let ((current-group gnus-newsgroup-name)
6851         (current-buffer (current-buffer))
6852         entered)
6853     ;; First we semi-exit this group to update Xrefs and all variables.
6854     ;; We can't do a real exit, because the window conf must remain
6855     ;; the same in case the user is prompted for info, and we don't
6856     ;; want the window conf to change before that...
6857     (gnus-summary-exit t)
6858     (while (not entered)
6859       ;; Then we find what group we are supposed to enter.
6860       (set-buffer gnus-group-buffer)
6861       (gnus-group-jump-to-group current-group)
6862       (setq target-group
6863             (or target-group
6864                 (if (eq gnus-keep-same-level 'best)
6865                     (gnus-summary-best-group gnus-newsgroup-name)
6866                   (gnus-summary-search-group backward gnus-keep-same-level))))
6867       (if (not target-group)
6868           ;; There are no further groups, so we return to the group
6869           ;; buffer.
6870           (progn
6871             (gnus-message 5 "Returning to the group buffer")
6872             (setq entered t)
6873             (when (gnus-buffer-live-p current-buffer)
6874               (set-buffer current-buffer)
6875               (gnus-summary-exit))
6876             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6877         ;; We try to enter the target group.
6878         (gnus-group-jump-to-group target-group)
6879         (let ((unreads (gnus-group-group-unread)))
6880           (if (and (or (eq t unreads)
6881                        (and unreads (not (zerop unreads))))
6882                    (gnus-summary-read-group
6883                     target-group nil no-article
6884                     (and (buffer-name current-buffer) current-buffer)
6885                     nil backward))
6886               (setq entered t)
6887             (setq current-group target-group
6888                   target-group nil)))))))
6889
6890 (defun gnus-summary-prev-group (&optional no-article)
6891   "Exit current newsgroup and then select previous unread newsgroup.
6892 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6893   (interactive "P")
6894   (gnus-summary-next-group no-article nil t))
6895
6896 ;; Walking around summary lines.
6897
6898 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6899   "Go to the first subject satisfying any non-nil constraint.
6900 If UNREAD is non-nil, the article should be unread.
6901 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6902 If UNSEEN is non-nil, the article should be unseen.
6903 Returns the article selected or nil if there are no matching articles."
6904   (interactive "P")
6905   (cond
6906    ;; Empty summary.
6907    ((null gnus-newsgroup-data)
6908     (gnus-message 3 "No articles in the group")
6909     nil)
6910    ;; Pick the first article.
6911    ((not (or unread undownloaded unseen))
6912     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6913     (gnus-data-number (car gnus-newsgroup-data)))
6914    ;; Find the first unread article.
6915    (t
6916     (let ((data gnus-newsgroup-data))
6917       (while (and data
6918                   (let ((num (gnus-data-number (car data))))
6919                     (or (memq num gnus-newsgroup-unfetched)
6920                         (not (or (and unread
6921                                       (memq num gnus-newsgroup-unreads))
6922                                  (and undownloaded
6923                                       (memq num gnus-newsgroup-undownloaded))
6924                                  (and unseen
6925                                       (memq num gnus-newsgroup-unseen)))))))
6926         (setq data (cdr data)))
6927       (prog1
6928           (if data
6929               (progn
6930                 (goto-char (gnus-data-pos (car data)))
6931                 (gnus-data-number (car data)))
6932             (gnus-message 3 "No more%s articles"
6933                           (let* ((r (when unread " unread"))
6934                                  (d (when undownloaded " undownloaded"))
6935                                  (s (when unseen " unseen"))
6936                                  (l (delq nil (list r d s))))
6937                             (cond ((= 3 (length l))
6938                                    (concat r "," d ", or" s))
6939                                   ((= 2 (length l))
6940                                    (concat (car l) ", or" (cadr l)))
6941                                   ((= 1 (length l))
6942                                    (car l))
6943                                   (t
6944                                    ""))))
6945             nil
6946             )
6947         (gnus-summary-position-point))))))
6948
6949 (defun gnus-summary-next-subject (n &optional unread dont-display)
6950   "Go to next N'th summary line.
6951 If N is negative, go to the previous N'th subject line.
6952 If UNREAD is non-nil, only unread articles are selected.
6953 The difference between N and the actual number of steps taken is
6954 returned."
6955   (interactive "p")
6956   (let ((backward (< n 0))
6957         (n (abs n)))
6958     (while (and (> n 0)
6959                 (if backward
6960                     (gnus-summary-find-prev unread)
6961                   (gnus-summary-find-next unread)))
6962       (unless (zerop (setq n (1- n)))
6963         (gnus-summary-show-thread)))
6964     (when (/= 0 n)
6965       (gnus-message 7 "No more%s articles"
6966                     (if unread " unread" "")))
6967     (unless dont-display
6968       (gnus-summary-recenter)
6969       (gnus-summary-position-point))
6970     n))
6971
6972 (defun gnus-summary-next-unread-subject (n)
6973   "Go to next N'th unread summary line."
6974   (interactive "p")
6975   (gnus-summary-next-subject n t))
6976
6977 (defun gnus-summary-prev-subject (n &optional unread)
6978   "Go to previous N'th summary line.
6979 If optional argument UNREAD is non-nil, only unread article is selected."
6980   (interactive "p")
6981   (gnus-summary-next-subject (- n) unread))
6982
6983 (defun gnus-summary-prev-unread-subject (n)
6984   "Go to previous N'th unread summary line."
6985   (interactive "p")
6986   (gnus-summary-next-subject (- n) t))
6987
6988 (defun gnus-summary-goto-subjects (articles)
6989   "Insert the subject header for ARTICLES in the current buffer."
6990   (save-excursion
6991     (dolist (article articles)
6992       (gnus-summary-goto-subject article t)))
6993   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6994   (gnus-summary-position-point))
6995  
6996 (defun gnus-summary-goto-subject (article &optional force silent)
6997   "Go the subject line of ARTICLE.
6998 If FORCE, also allow jumping to articles not currently shown."
6999   (interactive "nArticle number: ")
7000   (unless (numberp article)
7001     (error "Article %s is not a number" article))
7002   (let ((b (point))
7003         (data (gnus-data-find article)))
7004     ;; We read in the article if we have to.
7005     (and (not data)
7006          force
7007          (gnus-summary-insert-subject
7008           article
7009           (if (or (numberp force) (vectorp force)) force)
7010           t)
7011          (setq data (gnus-data-find article)))
7012     (goto-char b)
7013     (if (not data)
7014         (progn
7015           (unless silent
7016             (gnus-message 3 "Can't find article %d" article))
7017           nil)
7018       (let ((pt (gnus-data-pos data)))
7019         (goto-char pt)
7020         (gnus-summary-set-article-display-arrow pt))
7021       (gnus-summary-position-point)
7022       article)))
7023
7024 ;; Walking around summary lines with displaying articles.
7025
7026 (defun gnus-summary-expand-window (&optional arg)
7027   "Make the summary buffer take up the entire Emacs frame.
7028 Given a prefix, will force an `article' buffer configuration."
7029   (interactive "P")
7030   (if arg
7031       (gnus-configure-windows 'article 'force)
7032     (gnus-configure-windows 'summary 'force)))
7033
7034 (defun gnus-summary-display-article (article &optional all-header)
7035   "Display ARTICLE in article buffer."
7036   (when (gnus-buffer-live-p gnus-article-buffer)
7037     (with-current-buffer gnus-article-buffer
7038       (set-buffer-multibyte t)))
7039   (gnus-set-global-variables)
7040   (when (gnus-buffer-live-p gnus-article-buffer)
7041     (with-current-buffer gnus-article-buffer
7042       (setq gnus-article-charset gnus-newsgroup-charset)
7043       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7044       (set-buffer-multibyte t)))
7045   (if (null article)
7046       nil
7047     (prog1
7048         (if gnus-summary-display-article-function
7049             (funcall gnus-summary-display-article-function article all-header)
7050           (gnus-article-prepare article all-header))
7051       (with-current-buffer gnus-article-buffer
7052         (set (make-local-variable 'gnus-summary-search-article-matched-data)
7053              nil))
7054       (gnus-run-hooks 'gnus-select-article-hook)
7055       (when (and gnus-current-article
7056                  (not (zerop gnus-current-article)))
7057         (gnus-summary-goto-subject gnus-current-article))
7058       (gnus-summary-recenter)
7059       (when (and gnus-use-trees gnus-show-threads)
7060         (gnus-possibly-generate-tree article)
7061         (gnus-highlight-selected-tree article))
7062       ;; Successfully display article.
7063       (gnus-article-set-window-start
7064        (cdr (assq article gnus-newsgroup-bookmarks))))))
7065
7066 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7067   "Select the current article.
7068 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7069 non-nil, the article will be re-fetched even if it already present in
7070 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7071 be displayed."
7072   ;; Make sure we are in the summary buffer to work around bbdb bug.
7073   (unless (eq major-mode 'gnus-summary-mode)
7074     (set-buffer gnus-summary-buffer))
7075   (let ((article (or article (gnus-summary-article-number)))
7076         (all-headers (not (not all-headers))) ;Must be T or NIL.
7077         gnus-summary-display-article-function)
7078     (and (not pseudo)
7079          (gnus-summary-article-pseudo-p article)
7080          (error "This is a pseudo-article"))
7081     (save-excursion
7082       (set-buffer gnus-summary-buffer)
7083       (if (or (and gnus-single-article-buffer
7084                    (or (null gnus-current-article)
7085                        (null gnus-article-current)
7086                        (null (get-buffer gnus-article-buffer))
7087                        (not (eq article (cdr gnus-article-current)))
7088                        (not (equal (car gnus-article-current)
7089                                    gnus-newsgroup-name))))
7090               (and (not gnus-single-article-buffer)
7091                    (or (null gnus-current-article)
7092                        (not (eq gnus-current-article article))))
7093               force)
7094           ;; The requested article is different from the current article.
7095           (progn
7096             (gnus-summary-display-article article all-headers)
7097             (gnus-article-set-window-start
7098              (cdr (assq article gnus-newsgroup-bookmarks)))
7099             article)
7100         'old))))
7101
7102 (defun gnus-summary-force-verify-and-decrypt ()
7103   "Display buttons for signed/encrypted parts and verify/decrypt them."
7104   (interactive)
7105   (let ((mm-verify-option 'known)
7106         (mm-decrypt-option 'known)
7107         (gnus-article-emulate-mime t)
7108         (gnus-buttonized-mime-types (append (list "multipart/signed"
7109                                                   "multipart/encrypted")
7110                                             gnus-buttonized-mime-types)))
7111     (gnus-summary-select-article nil 'force)))
7112
7113 (defun gnus-summary-set-current-mark (&optional current-mark)
7114   "Obsolete function."
7115   nil)
7116
7117 (defun gnus-summary-next-article (&optional unread subject backward push)
7118   "Select the next article.
7119 If UNREAD, only unread articles are selected.
7120 If SUBJECT, only articles with SUBJECT are selected.
7121 If BACKWARD, the previous article is selected instead of the next."
7122   (interactive "P")
7123   (cond
7124    ;; Is there such an article?
7125    ((and (gnus-summary-search-forward unread subject backward)
7126          (or (gnus-summary-display-article (gnus-summary-article-number))
7127              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7128     (gnus-summary-position-point))
7129    ;; If not, we try the first unread, if that is wanted.
7130    ((and subject
7131          gnus-auto-select-same
7132          (gnus-summary-first-unread-article))
7133     (gnus-summary-position-point)
7134     (gnus-message 6 "Wrapped"))
7135    ;; Try to get next/previous article not displayed in this group.
7136    ((and gnus-auto-extend-newsgroup
7137          (not unread) (not subject))
7138     (gnus-summary-goto-article
7139      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7140      nil (count-lines (point-min) (point))))
7141    ;; Go to next/previous group.
7142    (t
7143     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7144       (gnus-summary-jump-to-group gnus-newsgroup-name))
7145     (let ((cmd last-command-char)
7146           (point
7147            (save-excursion
7148              (set-buffer gnus-group-buffer)
7149              (point)))
7150           (group
7151            (if (eq gnus-keep-same-level 'best)
7152                (gnus-summary-best-group gnus-newsgroup-name)
7153              (gnus-summary-search-group backward gnus-keep-same-level))))
7154       ;; For some reason, the group window gets selected.  We change
7155       ;; it back.
7156       (select-window (get-buffer-window (current-buffer)))
7157       ;; Select next unread newsgroup automagically.
7158       (cond
7159        ((or (not gnus-auto-select-next)
7160             (not cmd))
7161         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7162        ((or (eq gnus-auto-select-next 'quietly)
7163             (and (eq gnus-auto-select-next 'slightly-quietly)
7164                  push)
7165             (and (eq gnus-auto-select-next 'almost-quietly)
7166                  (gnus-summary-last-article-p)))
7167         ;; Select quietly.
7168         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7169             (gnus-summary-exit)
7170           (gnus-message 7 "No more%s articles (%s)..."
7171                         (if unread " unread" "")
7172                         (if group (concat "selecting " group)
7173                           "exiting"))
7174           (gnus-summary-next-group nil group backward)))
7175        (t
7176         (when (gnus-key-press-event-p last-input-event)
7177           (gnus-summary-walk-group-buffer
7178            gnus-newsgroup-name cmd unread backward point))))))))
7179
7180 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7181   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7182                       (?\C-p (gnus-group-prev-unread-group 1))))
7183         (cursor-in-echo-area t)
7184         keve key group ended prompt)
7185     (save-excursion
7186       (set-buffer gnus-group-buffer)
7187       (goto-char start)
7188       (setq group
7189             (if (eq gnus-keep-same-level 'best)
7190                 (gnus-summary-best-group gnus-newsgroup-name)
7191               (gnus-summary-search-group backward gnus-keep-same-level))))
7192     (while (not ended)
7193       (setq prompt
7194             (format
7195              "No more%s articles%s " (if unread " unread" "")
7196              (if (and group
7197                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7198                  (format " (Type %s for %s [%s])"
7199                          (single-key-description cmd) group
7200                          (gnus-group-unread group))
7201                (format " (Type %s to exit %s)"
7202                        (single-key-description cmd)
7203                        gnus-newsgroup-name))))
7204       ;; Confirm auto selection.
7205       (setq key (car (setq keve (gnus-read-event-char prompt)))
7206             ended t)
7207       (cond
7208        ((assq key keystrokes)
7209         (let ((obuf (current-buffer)))
7210           (switch-to-buffer gnus-group-buffer)
7211           (when group
7212             (gnus-group-jump-to-group group))
7213           (eval (cadr (assq key keystrokes)))
7214           (setq group (gnus-group-group-name))
7215           (switch-to-buffer obuf))
7216         (setq ended nil))
7217        ((equal key cmd)
7218         (if (or (not group)
7219                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7220             (gnus-summary-exit)
7221           (gnus-summary-next-group nil group backward)))
7222        (t
7223         (push (cdr keve) unread-command-events))))))
7224
7225 (defun gnus-summary-next-unread-article ()
7226   "Select unread article after current one."
7227   (interactive)
7228   (gnus-summary-next-article
7229    (or (not (eq gnus-summary-goto-unread 'never))
7230        (gnus-summary-last-article-p (gnus-summary-article-number)))
7231    (and gnus-auto-select-same
7232         (gnus-summary-article-subject))))
7233
7234 (defun gnus-summary-prev-article (&optional unread subject)
7235   "Select the article after the current one.
7236 If UNREAD is non-nil, only unread articles are selected."
7237   (interactive "P")
7238   (gnus-summary-next-article unread subject t))
7239
7240 (defun gnus-summary-prev-unread-article ()
7241   "Select unread article before current one."
7242   (interactive)
7243   (gnus-summary-prev-article
7244    (or (not (eq gnus-summary-goto-unread 'never))
7245        (gnus-summary-first-article-p (gnus-summary-article-number)))
7246    (and gnus-auto-select-same
7247         (gnus-summary-article-subject))))
7248
7249 (defun gnus-summary-next-page (&optional lines circular stop)
7250   "Show next page of the selected article.
7251 If at the end of the current article, select the next article.
7252 LINES says how many lines should be scrolled up.
7253
7254 If CIRCULAR is non-nil, go to the start of the article instead of
7255 selecting the next article when reaching the end of the current
7256 article.
7257
7258 If STOP is non-nil, just stop when reaching the end of the message.
7259
7260 Also see the variable `gnus-article-skip-boring'."
7261   (interactive "P")
7262   (setq gnus-summary-buffer (current-buffer))
7263   (gnus-set-global-variables)
7264   (let ((article (gnus-summary-article-number))
7265         (article-window (get-buffer-window gnus-article-buffer t))
7266         endp)
7267     ;; If the buffer is empty, we have no article.
7268     (unless article
7269       (error "No article to select"))
7270     (gnus-configure-windows 'article)
7271     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7272         (if (and (eq gnus-summary-goto-unread 'never)
7273                  (not (gnus-summary-last-article-p article)))
7274             (gnus-summary-next-article)
7275           (gnus-summary-next-unread-article))
7276       (if (or (null gnus-current-article)
7277               (null gnus-article-current)
7278               (/= article (cdr gnus-article-current))
7279               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7280           ;; Selected subject is different from current article's.
7281           (gnus-summary-display-article article)
7282         (when article-window
7283           (gnus-eval-in-buffer-window gnus-article-buffer
7284             (setq endp (or (gnus-article-next-page lines)
7285                            (gnus-article-only-boring-p))))
7286           (when endp
7287             (cond (stop
7288                    (gnus-message 3 "End of message"))
7289                   (circular
7290                    (gnus-summary-beginning-of-article))
7291                   (lines
7292                    (gnus-message 3 "End of message"))
7293                   ((null lines)
7294                    (if (and (eq gnus-summary-goto-unread 'never)
7295                             (not (gnus-summary-last-article-p article)))
7296                        (gnus-summary-next-article)
7297                      (gnus-summary-next-unread-article))))))))
7298     (gnus-summary-recenter)
7299     (gnus-summary-position-point)))
7300
7301 (defun gnus-summary-prev-page (&optional lines move)
7302   "Show previous page of selected article.
7303 Argument LINES specifies lines to be scrolled down.
7304 If MOVE, move to the previous unread article if point is at
7305 the beginning of the buffer."
7306   (interactive "P")
7307   (let ((article (gnus-summary-article-number))
7308         (article-window (get-buffer-window gnus-article-buffer t))
7309         endp)
7310     (gnus-configure-windows 'article)
7311     (if (or (null gnus-current-article)
7312             (null gnus-article-current)
7313             (/= article (cdr gnus-article-current))
7314             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7315         ;; Selected subject is different from current article's.
7316         (gnus-summary-display-article article)
7317       (gnus-summary-recenter)
7318       (when article-window
7319         (gnus-eval-in-buffer-window gnus-article-buffer
7320           (setq endp (gnus-article-prev-page lines)))
7321         (when (and move endp)
7322           (cond (lines
7323                  (gnus-message 3 "Beginning of message"))
7324                 ((null lines)
7325                  (if (and (eq gnus-summary-goto-unread 'never)
7326                           (not (gnus-summary-first-article-p article)))
7327                      (gnus-summary-prev-article)
7328                    (gnus-summary-prev-unread-article))))))))
7329   (gnus-summary-position-point))
7330
7331 (defun gnus-summary-prev-page-or-article (&optional lines)
7332   "Show previous page of selected article.
7333 Argument LINES specifies lines to be scrolled down.
7334 If at the beginning of the article, go to the next article."
7335   (interactive "P")
7336   (gnus-summary-prev-page lines t))
7337
7338 (defun gnus-summary-scroll-up (lines)
7339   "Scroll up (or down) one line current article.
7340 Argument LINES specifies lines to be scrolled up (or down if negative)."
7341   (interactive "p")
7342   (gnus-configure-windows 'article)
7343   (gnus-summary-show-thread)
7344   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7345     (gnus-eval-in-buffer-window gnus-article-buffer
7346       (cond ((> lines 0)
7347              (when (gnus-article-next-page lines)
7348                (gnus-message 3 "End of message")))
7349             ((< lines 0)
7350              (gnus-article-prev-page (- lines))))))
7351   (gnus-summary-recenter)
7352   (gnus-summary-position-point))
7353
7354 (defun gnus-summary-scroll-down (lines)
7355   "Scroll down (or up) one line current article.
7356 Argument LINES specifies lines to be scrolled down (or up if negative)."
7357   (interactive "p")
7358   (gnus-summary-scroll-up (- lines)))
7359
7360 (defun gnus-summary-next-same-subject ()
7361   "Select next article which has the same subject as current one."
7362   (interactive)
7363   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7364
7365 (defun gnus-summary-prev-same-subject ()
7366   "Select previous article which has the same subject as current one."
7367   (interactive)
7368   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7369
7370 (defun gnus-summary-next-unread-same-subject ()
7371   "Select next unread article which has the same subject as current one."
7372   (interactive)
7373   (gnus-summary-next-article t (gnus-summary-article-subject)))
7374
7375 (defun gnus-summary-prev-unread-same-subject ()
7376   "Select previous unread article which has the same subject as current one."
7377   (interactive)
7378   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7379
7380 (defun gnus-summary-first-unread-article ()
7381   "Select the first unread article.
7382 Return nil if there are no unread articles."
7383   (interactive)
7384   (prog1
7385       (when (gnus-summary-first-subject t)
7386         (gnus-summary-show-thread)
7387         (gnus-summary-first-subject t)
7388         (gnus-summary-display-article (gnus-summary-article-number)))
7389     (gnus-summary-position-point)))
7390
7391 (defun gnus-summary-first-unread-subject ()
7392   "Place the point on the subject line of the first unread article.
7393 Return nil if there are no unread articles."
7394   (interactive)
7395   (prog1
7396       (when (gnus-summary-first-subject t)
7397         (gnus-summary-show-thread)
7398         (gnus-summary-first-subject t))
7399     (gnus-summary-position-point)))
7400
7401 (defun gnus-summary-first-unseen-subject ()
7402   "Place the point on the subject line of the first unseen article.
7403 Return nil if there are no unseen articles."
7404   (interactive)
7405   (prog1
7406       (when (gnus-summary-first-subject nil nil t)
7407         (gnus-summary-show-thread)
7408         (gnus-summary-first-subject nil nil t))
7409     (gnus-summary-position-point)))
7410
7411 (defun gnus-summary-first-unseen-or-unread-subject ()
7412   "Place the point on the subject line of the first unseen article or,
7413 if all article have been seen, on the subject line of the first unread
7414 article."
7415   (interactive)
7416   (prog1
7417       (unless (when (gnus-summary-first-subject nil nil t)
7418                 (gnus-summary-show-thread)
7419                 (gnus-summary-first-subject nil nil t))
7420         (when (gnus-summary-first-subject t)
7421           (gnus-summary-show-thread)
7422           (gnus-summary-first-subject t)))
7423     (gnus-summary-position-point)))
7424
7425 (defun gnus-summary-first-article ()
7426   "Select the first article.
7427 Return nil if there are no articles."
7428   (interactive)
7429   (prog1
7430       (when (gnus-summary-first-subject)
7431         (gnus-summary-show-thread)
7432         (gnus-summary-first-subject)
7433         (gnus-summary-display-article (gnus-summary-article-number)))
7434     (gnus-summary-position-point)))
7435
7436 (defun gnus-summary-best-unread-article (&optional arg)
7437   "Select the unread article with the highest score.
7438 If given a prefix argument, select the next unread article that has a
7439 score higher than the default score."
7440   (interactive "P")
7441   (let ((article (if arg
7442                      (gnus-summary-better-unread-subject)
7443                    (gnus-summary-best-unread-subject))))
7444     (if article
7445         (gnus-summary-goto-article article)
7446       (error "No unread articles"))))
7447
7448 (defun gnus-summary-best-unread-subject ()
7449   "Select the unread subject with the highest score."
7450   (interactive)
7451   (let ((best -1000000)
7452         (data gnus-newsgroup-data)
7453         article score)
7454     (while data
7455       (and (gnus-data-unread-p (car data))
7456            (> (setq score
7457                     (gnus-summary-article-score (gnus-data-number (car data))))
7458               best)
7459            (setq best score
7460                  article (gnus-data-number (car data))))
7461       (setq data (cdr data)))
7462     (when article
7463       (gnus-summary-goto-subject article))
7464     (gnus-summary-position-point)
7465     article))
7466
7467 (defun gnus-summary-better-unread-subject ()
7468   "Select the first unread subject that has a score over the default score."
7469   (interactive)
7470   (let ((data gnus-newsgroup-data)
7471         article score)
7472     (while (and (setq article (gnus-data-number (car data)))
7473                 (or (gnus-data-read-p (car data))
7474                     (not (> (gnus-summary-article-score article)
7475                             gnus-summary-default-score))))
7476       (setq data (cdr data)))
7477     (when article
7478       (gnus-summary-goto-subject article))
7479     (gnus-summary-position-point)
7480     article))
7481
7482 (defun gnus-summary-last-subject ()
7483   "Go to the last displayed subject line in the group."
7484   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7485     (when article
7486       (gnus-summary-goto-subject article))))
7487
7488 (defun gnus-summary-goto-article (article &optional all-headers force)
7489   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7490 If ALL-HEADERS is non-nil, no header lines are hidden.
7491 If FORCE, go to the article even if it isn't displayed.  If FORCE
7492 is a number, it is the line the article is to be displayed on."
7493   (interactive
7494    (list
7495     (completing-read
7496      "Article number or Message-ID: "
7497      (mapcar (lambda (number) (list (int-to-string number)))
7498              gnus-newsgroup-limit))
7499     current-prefix-arg
7500     t))
7501   (prog1
7502       (if (and (stringp article)
7503                (string-match "@\\|%40" article))
7504           (gnus-summary-refer-article article)
7505         (when (stringp article)
7506           (setq article (string-to-number article)))
7507         (if (gnus-summary-goto-subject article force)
7508             (gnus-summary-display-article article all-headers)
7509           (gnus-message 4 "Couldn't go to article %s" article) nil))
7510     (gnus-summary-position-point)))
7511
7512 (defun gnus-summary-goto-last-article ()
7513   "Go to the previously read article."
7514   (interactive)
7515   (prog1
7516       (when gnus-last-article
7517         (gnus-summary-goto-article gnus-last-article nil t))
7518     (gnus-summary-position-point)))
7519
7520 (defun gnus-summary-pop-article (number)
7521   "Pop one article off the history and go to the previous.
7522 NUMBER articles will be popped off."
7523   (interactive "p")
7524   (let (to)
7525     (setq gnus-newsgroup-history
7526           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7527     (if to
7528         (gnus-summary-goto-article (car to) nil t)
7529       (error "Article history empty")))
7530   (gnus-summary-position-point))
7531
7532 ;; Summary commands and functions for limiting the summary buffer.
7533
7534 (defun gnus-summary-limit-to-articles (n)
7535   "Limit the summary buffer to the next N articles.
7536 If not given a prefix, use the process marked articles instead."
7537   (interactive "P")
7538   (prog1
7539       (let ((articles (gnus-summary-work-articles n)))
7540         (setq gnus-newsgroup-processable nil)
7541         (gnus-summary-limit articles))
7542     (gnus-summary-position-point)))
7543
7544 (defun gnus-summary-pop-limit (&optional total)
7545   "Restore the previous limit.
7546 If given a prefix, remove all limits."
7547   (interactive "P")
7548   (when total
7549     (setq gnus-newsgroup-limits
7550           (list (mapcar (lambda (h) (mail-header-number h))
7551                         gnus-newsgroup-headers))))
7552   (unless gnus-newsgroup-limits
7553     (error "No limit to pop"))
7554   (prog1
7555       (gnus-summary-limit nil 'pop)
7556     (gnus-summary-position-point)))
7557
7558 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7559   "Limit the summary buffer to articles that have subjects that match a regexp.
7560 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7561   (interactive
7562    (list (read-string (if current-prefix-arg
7563                           "Exclude subject (regexp): "
7564                         "Limit to subject (regexp): "))
7565          nil current-prefix-arg))
7566   (unless header
7567     (setq header "subject"))
7568   (when (not (equal "" subject))
7569     (prog1
7570         (let ((articles (gnus-summary-find-matching
7571                          (or header "subject") subject 'all nil nil
7572                          not-matching)))
7573           (unless articles
7574             (error "Found no matches for \"%s\"" subject))
7575           (gnus-summary-limit articles))
7576       (gnus-summary-position-point))))
7577
7578 (defun gnus-summary-limit-to-author (from &optional not-matching)
7579   "Limit the summary buffer to articles that have authors that match a regexp.
7580 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7581   (interactive
7582    (list (read-string (if current-prefix-arg
7583                           "Exclude author (regexp): "
7584                         "Limit to author (regexp): "))
7585          current-prefix-arg))
7586   (gnus-summary-limit-to-subject from "from" not-matching))
7587
7588 (defun gnus-summary-limit-to-age (age &optional younger-p)
7589   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7590 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7591 articles that are younger than AGE days."
7592   (interactive
7593    (let ((younger current-prefix-arg)
7594          (days-got nil)
7595          days)
7596      (while (not days-got)
7597        (setq days (if younger
7598                       (read-string "Limit to articles younger than (in days, older when negative): ")
7599                     (read-string
7600                      "Limit to articles older than (in days, younger when negative): ")))
7601        (when (> (length days) 0)
7602          (setq days (read days)))
7603        (if (numberp days)
7604            (progn
7605              (setq days-got t)
7606              (if (< days 0)
7607                  (progn
7608                    (setq younger (not younger))
7609                    (setq days (* days -1)))))
7610          (message "Please enter a number.")
7611          (sleep-for 1)))
7612      (list days younger)))
7613   (prog1
7614       (let ((data gnus-newsgroup-data)
7615             (cutoff (days-to-time age))
7616             articles d date is-younger)
7617         (while (setq d (pop data))
7618           (when (and (vectorp (gnus-data-header d))
7619                      (setq date (mail-header-date (gnus-data-header d))))
7620             (setq is-younger (time-less-p
7621                               (time-since (condition-case ()
7622                                               (date-to-time date)
7623                                             (error '(0 0))))
7624                               cutoff))
7625             (when (if younger-p
7626                       is-younger
7627                     (not is-younger))
7628               (push (gnus-data-number d) articles))))
7629         (gnus-summary-limit (nreverse articles)))
7630     (gnus-summary-position-point)))
7631
7632 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7633   "Limit the summary buffer to articles that match an 'extra' header."
7634   (interactive
7635    (let ((header
7636           (intern
7637            (gnus-completing-read-with-default
7638             (symbol-name (car gnus-extra-headers))
7639             (if current-prefix-arg
7640                 "Exclude extra header:"
7641               "Limit extra header:")
7642             (mapcar (lambda (x)
7643                       (cons (symbol-name x) x))
7644                     gnus-extra-headers)
7645             nil
7646             t))))
7647      (list header
7648            (read-string (format "%s header %s (regexp): "
7649                                 (if current-prefix-arg "Exclude" "Limit to")
7650                                 header))
7651            current-prefix-arg)))
7652   (when (not (equal "" regexp))
7653     (prog1
7654         (let ((articles (gnus-summary-find-matching
7655                          (cons 'extra header) regexp 'all nil nil
7656                          not-matching)))
7657           (unless articles
7658             (error "Found no matches for \"%s\"" regexp))
7659           (gnus-summary-limit articles))
7660       (gnus-summary-position-point))))
7661
7662 (defun gnus-summary-limit-to-display-predicate ()
7663   "Limit the summary buffer to the predicated in the `display' group parameter."
7664   (interactive)
7665   (unless gnus-newsgroup-display
7666     (error "There is no `display' group parameter"))
7667   (let (articles)
7668     (dolist (number gnus-newsgroup-articles)
7669       (when (funcall gnus-newsgroup-display)
7670         (push number articles)))
7671     (gnus-summary-limit articles))
7672   (gnus-summary-position-point))
7673
7674 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7675 (make-obsolete
7676  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7677
7678 (defun gnus-summary-limit-to-unread (&optional all)
7679   "Limit the summary buffer to articles that are not marked as read.
7680 If ALL is non-nil, limit strictly to unread articles."
7681   (interactive "P")
7682   (if all
7683       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7684     (gnus-summary-limit-to-marks
7685      ;; Concat all the marks that say that an article is read and have
7686      ;; those removed.
7687      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7688            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7689            gnus-low-score-mark gnus-expirable-mark
7690            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7691            gnus-duplicate-mark gnus-souped-mark)
7692      'reverse)))
7693
7694 (defun gnus-summary-limit-to-replied (&optional unreplied)
7695   "Limit the summary buffer to replied articles.
7696 If UNREPLIED (the prefix), limit to unreplied articles."
7697   (interactive "P")
7698   (if unreplied
7699       (gnus-summary-limit
7700        (gnus-set-difference gnus-newsgroup-articles
7701         gnus-newsgroup-replied))
7702     (gnus-summary-limit gnus-newsgroup-replied))
7703   (gnus-summary-position-point))
7704
7705 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7706 (make-obsolete 'gnus-summary-delete-marked-with
7707                'gnus-summary-limit-exclude-marks)
7708
7709 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7710   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7711 If REVERSE, limit the summary buffer to articles that are marked
7712 with MARKS.  MARKS can either be a string of marks or a list of marks.
7713 Returns how many articles were removed."
7714   (interactive "sMarks: ")
7715   (gnus-summary-limit-to-marks marks t))
7716
7717 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7718   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7719 If REVERSE (the prefix), limit the summary buffer to articles that are
7720 not marked with MARKS.  MARKS can either be a string of marks or a
7721 list of marks.
7722 Returns how many articles were removed."
7723   (interactive "sMarks: \nP")
7724   (prog1
7725       (let ((data gnus-newsgroup-data)
7726             (marks (if (listp marks) marks
7727                      (append marks nil))) ; Transform to list.
7728             articles)
7729         (while data
7730           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7731                   (memq (gnus-data-mark (car data)) marks))
7732             (push (gnus-data-number (car data)) articles))
7733           (setq data (cdr data)))
7734         (gnus-summary-limit articles))
7735     (gnus-summary-position-point)))
7736
7737 (defun gnus-summary-limit-to-score (score)
7738   "Limit to articles with score at or above SCORE."
7739   (interactive "NLimit to articles with score of at least: ")
7740   (let ((data gnus-newsgroup-data)
7741         articles)
7742     (while data
7743       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7744                 score)
7745         (push (gnus-data-number (car data)) articles))
7746       (setq data (cdr data)))
7747     (prog1
7748         (gnus-summary-limit articles)
7749       (gnus-summary-position-point))))
7750
7751 (defun gnus-summary-limit-to-unseen ()
7752   "Limit to unseen articles."
7753   (interactive)
7754   (prog1
7755       (gnus-summary-limit gnus-newsgroup-unseen)
7756     (gnus-summary-position-point)))
7757
7758 (defun gnus-summary-limit-include-thread (id)
7759   "Display all the hidden articles that is in the thread with ID in it.
7760 When called interactively, ID is the Message-ID of the current
7761 article."
7762   (interactive (list (mail-header-id (gnus-summary-article-header))))
7763   (let ((articles (gnus-articles-in-thread
7764                    (gnus-id-to-thread (gnus-root-id id)))))
7765     (prog1
7766         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7767       (gnus-summary-limit-include-matching-articles
7768        "subject"
7769        (regexp-quote (gnus-simplify-subject-re
7770                       (mail-header-subject (gnus-id-to-header id)))))
7771       (gnus-summary-position-point))))
7772
7773 (defun gnus-summary-limit-include-matching-articles (header regexp)
7774   "Display all the hidden articles that have HEADERs that match REGEXP."
7775   (interactive (list (read-string "Match on header: ")
7776                      (read-string "Regexp: ")))
7777   (let ((articles (gnus-find-matching-articles header regexp)))
7778     (prog1
7779         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7780       (gnus-summary-position-point))))
7781
7782 (defun gnus-summary-insert-dormant-articles ()
7783   "Insert all the dormant articles for this group into the current buffer."
7784   (interactive)
7785   (let ((gnus-verbose (max 6 gnus-verbose)))
7786     (if (not gnus-newsgroup-dormant)
7787         (gnus-message 3 "No cached articles for this group")
7788       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7789
7790 (defun gnus-summary-limit-include-dormant ()
7791   "Display all the hidden articles that are marked as dormant.
7792 Note that this command only works on a subset of the articles currently
7793 fetched for this group."
7794   (interactive)
7795   (unless gnus-newsgroup-dormant
7796     (error "There are no dormant articles in this group"))
7797   (prog1
7798       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7799     (gnus-summary-position-point)))
7800
7801 (defun gnus-summary-limit-exclude-dormant ()
7802   "Hide all dormant articles."
7803   (interactive)
7804   (prog1
7805       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7806     (gnus-summary-position-point)))
7807
7808 (defun gnus-summary-limit-exclude-childless-dormant ()
7809   "Hide all dormant articles that have no children."
7810   (interactive)
7811   (let ((data (gnus-data-list t))
7812         articles d children)
7813     ;; Find all articles that are either not dormant or have
7814     ;; children.
7815     (while (setq d (pop data))
7816       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7817                 (and (setq children
7818                            (gnus-article-children (gnus-data-number d)))
7819                      (let (found)
7820                        (while children
7821                          (when (memq (car children) articles)
7822                            (setq children nil
7823                                  found t))
7824                          (pop children))
7825                        found)))
7826         (push (gnus-data-number d) articles)))
7827     ;; Do the limiting.
7828     (prog1
7829         (gnus-summary-limit articles)
7830       (gnus-summary-position-point))))
7831
7832 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7833   "Mark all unread excluded articles as read.
7834 If ALL, mark even excluded ticked and dormants as read."
7835   (interactive "P")
7836   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7837   (let ((articles (gnus-sorted-ndifference
7838                    (sort
7839                     (mapcar (lambda (h) (mail-header-number h))
7840                             gnus-newsgroup-headers)
7841                     '<)
7842                    gnus-newsgroup-limit))
7843         article)
7844     (setq gnus-newsgroup-unreads
7845           (gnus-sorted-intersection gnus-newsgroup-unreads
7846                                     gnus-newsgroup-limit))
7847     (if all
7848         (setq gnus-newsgroup-dormant nil
7849               gnus-newsgroup-marked nil
7850               gnus-newsgroup-reads
7851               (nconc
7852                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7853                gnus-newsgroup-reads))
7854       (while (setq article (pop articles))
7855         (unless (or (memq article gnus-newsgroup-dormant)
7856                     (memq article gnus-newsgroup-marked))
7857           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7858
7859 (defun gnus-summary-limit (articles &optional pop)
7860   (if pop
7861       ;; We pop the previous limit off the stack and use that.
7862       (setq articles (car gnus-newsgroup-limits)
7863             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7864     ;; We use the new limit, so we push the old limit on the stack.
7865     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7866   ;; Set the limit.
7867   (setq gnus-newsgroup-limit articles)
7868   (let ((total (length gnus-newsgroup-data))
7869         (data (gnus-data-find-list (gnus-summary-article-number)))
7870         (gnus-summary-mark-below nil)   ; Inhibit this.
7871         found)
7872     ;; This will do all the work of generating the new summary buffer
7873     ;; according to the new limit.
7874     (gnus-summary-prepare)
7875     ;; Hide any threads, possibly.
7876     (gnus-summary-maybe-hide-threads)
7877     ;; Try to return to the article you were at, or one in the
7878     ;; neighborhood.
7879     (when data
7880       ;; We try to find some article after the current one.
7881       (while data
7882         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7883           (setq data nil
7884                 found t))
7885         (setq data (cdr data))))
7886     (unless found
7887       ;; If there is no data, that means that we were after the last
7888       ;; article.  The same goes when we can't find any articles
7889       ;; after the current one.
7890       (goto-char (point-max))
7891       (gnus-summary-find-prev))
7892     (gnus-set-mode-line 'summary)
7893     ;; We return how many articles were removed from the summary
7894     ;; buffer as a result of the new limit.
7895     (- total (length gnus-newsgroup-data))))
7896
7897 (defsubst gnus-invisible-cut-children (threads)
7898   (let ((num 0))
7899     (while threads
7900       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7901         (incf num))
7902       (pop threads))
7903     (< num 2)))
7904
7905 (defsubst gnus-cut-thread (thread)
7906   "Go forwards in the thread until we find an article that we want to display."
7907   (when (or (eq gnus-fetch-old-headers 'some)
7908             (eq gnus-fetch-old-headers 'invisible)
7909             (numberp gnus-fetch-old-headers)
7910             (eq gnus-build-sparse-threads 'some)
7911             (eq gnus-build-sparse-threads 'more))
7912     ;; Deal with old-fetched headers and sparse threads.
7913     (while (and
7914             thread
7915             (or
7916              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7917              (gnus-summary-article-ancient-p
7918               (mail-header-number (car thread))))
7919             (if (or (<= (length (cdr thread)) 1)
7920                     (eq gnus-fetch-old-headers 'invisible))
7921                 (setq gnus-newsgroup-limit
7922                       (delq (mail-header-number (car thread))
7923                             gnus-newsgroup-limit)
7924                       thread (cadr thread))
7925               (when (gnus-invisible-cut-children (cdr thread))
7926                 (let ((th (cdr thread)))
7927                   (while th
7928                     (if (memq (mail-header-number (caar th))
7929                               gnus-newsgroup-limit)
7930                         (setq thread (car th)
7931                               th nil)
7932                       (setq th (cdr th))))))))))
7933   thread)
7934
7935 (defun gnus-cut-threads (threads)
7936   "Cut off all uninteresting articles from the beginning of THREADS."
7937   (when (or (eq gnus-fetch-old-headers 'some)
7938             (eq gnus-fetch-old-headers 'invisible)
7939             (numberp gnus-fetch-old-headers)
7940             (eq gnus-build-sparse-threads 'some)
7941             (eq gnus-build-sparse-threads 'more))
7942     (let ((th threads))
7943       (while th
7944         (setcar th (gnus-cut-thread (car th)))
7945         (setq th (cdr th)))))
7946   ;; Remove nixed out threads.
7947   (delq nil threads))
7948
7949 (defun gnus-summary-initial-limit (&optional show-if-empty)
7950   "Figure out what the initial limit is supposed to be on group entry.
7951 This entails weeding out unwanted dormants, low-scored articles,
7952 fetch-old-headers verbiage, and so on."
7953   ;; Most groups have nothing to remove.
7954   (if (or gnus-inhibit-limiting
7955           (and (null gnus-newsgroup-dormant)
7956                (eq gnus-newsgroup-display 'gnus-not-ignore)
7957                (not (eq gnus-fetch-old-headers 'some))
7958                (not (numberp gnus-fetch-old-headers))
7959                (not (eq gnus-fetch-old-headers 'invisible))
7960                (null gnus-summary-expunge-below)
7961                (not (eq gnus-build-sparse-threads 'some))
7962                (not (eq gnus-build-sparse-threads 'more))
7963                (null gnus-thread-expunge-below)
7964                (not gnus-use-nocem)))
7965       ()                                ; Do nothing.
7966     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7967     (setq gnus-newsgroup-limit nil)
7968     (mapatoms
7969      (lambda (node)
7970        (unless (car (symbol-value node))
7971          ;; These threads have no parents -- they are roots.
7972          (let ((nodes (cdr (symbol-value node)))
7973                thread)
7974            (while nodes
7975              (if (and gnus-thread-expunge-below
7976                       (< (gnus-thread-total-score (car nodes))
7977                          gnus-thread-expunge-below))
7978                  (gnus-expunge-thread (pop nodes))
7979                (setq thread (pop nodes))
7980                (gnus-summary-limit-children thread))))))
7981      gnus-newsgroup-dependencies)
7982     ;; If this limitation resulted in an empty group, we might
7983     ;; pop the previous limit and use it instead.
7984     (when (and (not gnus-newsgroup-limit)
7985                show-if-empty)
7986       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7987     gnus-newsgroup-limit))
7988
7989 (defun gnus-summary-limit-children (thread)
7990   "Return 1 if this subthread is visible and 0 if it is not."
7991   ;; First we get the number of visible children to this thread.  This
7992   ;; is done by recursing down the thread using this function, so this
7993   ;; will really go down to a leaf article first, before slowly
7994   ;; working its way up towards the root.
7995   (when thread
7996     (let* ((max-lisp-eval-depth 5000)
7997            (children
7998            (if (cdr thread)
7999                (apply '+ (mapcar 'gnus-summary-limit-children
8000                                  (cdr thread)))
8001              0))
8002           (number (mail-header-number (car thread)))
8003           score)
8004       (if (and
8005            (not (memq number gnus-newsgroup-marked))
8006            (or
8007             ;; If this article is dormant and has absolutely no visible
8008             ;; children, then this article isn't visible.
8009             (and (memq number gnus-newsgroup-dormant)
8010                  (zerop children))
8011             ;; If this is "fetch-old-headered" and there is no
8012             ;; visible children, then we don't want this article.
8013             (and (or (eq gnus-fetch-old-headers 'some)
8014                      (numberp gnus-fetch-old-headers))
8015                  (gnus-summary-article-ancient-p number)
8016                  (zerop children))
8017             ;; If this is "fetch-old-headered" and `invisible', then
8018             ;; we don't want this article.
8019             (and (eq gnus-fetch-old-headers 'invisible)
8020                  (gnus-summary-article-ancient-p number))
8021             ;; If this is a sparsely inserted article with no children,
8022             ;; we don't want it.
8023             (and (eq gnus-build-sparse-threads 'some)
8024                  (gnus-summary-article-sparse-p number)
8025                  (zerop children))
8026             ;; If we use expunging, and this article is really
8027             ;; low-scored, then we don't want this article.
8028             (when (and gnus-summary-expunge-below
8029                        (< (setq score
8030                                 (or (cdr (assq number gnus-newsgroup-scored))
8031                                     gnus-summary-default-score))
8032                           gnus-summary-expunge-below))
8033               ;; We increase the expunge-tally here, but that has
8034               ;; nothing to do with the limits, really.
8035               (incf gnus-newsgroup-expunged-tally)
8036               ;; We also mark as read here, if that's wanted.
8037               (when (and gnus-summary-mark-below
8038                          (< score gnus-summary-mark-below))
8039                 (setq gnus-newsgroup-unreads
8040                       (delq number gnus-newsgroup-unreads))
8041                 (if gnus-newsgroup-auto-expire
8042                     (push number gnus-newsgroup-expirable)
8043                   (push (cons number gnus-low-score-mark)
8044                         gnus-newsgroup-reads)))
8045               t)
8046             ;; Do the `display' group parameter.
8047             (and gnus-newsgroup-display
8048                  (not (funcall gnus-newsgroup-display)))
8049             ;; Check NoCeM things.
8050             (if (and gnus-use-nocem
8051                      (gnus-nocem-unwanted-article-p
8052                       (mail-header-id (car thread))))
8053                 (progn
8054                   (setq gnus-newsgroup-unreads
8055                         (delq number gnus-newsgroup-unreads))
8056                   t))))
8057           ;; Nope, invisible article.
8058           0
8059         ;; Ok, this article is to be visible, so we add it to the limit
8060         ;; and return 1.
8061         (push number gnus-newsgroup-limit)
8062         1))))
8063
8064 (defun gnus-expunge-thread (thread)
8065   "Mark all articles in THREAD as read."
8066   (let* ((number (mail-header-number (car thread))))
8067     (incf gnus-newsgroup-expunged-tally)
8068     ;; We also mark as read here, if that's wanted.
8069     (setq gnus-newsgroup-unreads
8070           (delq number gnus-newsgroup-unreads))
8071     (if gnus-newsgroup-auto-expire
8072         (push number gnus-newsgroup-expirable)
8073       (push (cons number gnus-low-score-mark)
8074             gnus-newsgroup-reads)))
8075   ;; Go recursively through all subthreads.
8076   (mapcar 'gnus-expunge-thread (cdr thread)))
8077
8078 ;; Summary article oriented commands
8079
8080 (defun gnus-summary-refer-parent-article (n)
8081   "Refer parent article N times.
8082 If N is negative, go to ancestor -N instead.
8083 The difference between N and the number of articles fetched is returned."
8084   (interactive "p")
8085   (let ((skip 1)
8086         error header ref)
8087     (when (not (natnump n))
8088       (setq skip (abs n)
8089             n 1))
8090     (while (and (> n 0)
8091                 (not error))
8092       (setq header (gnus-summary-article-header))
8093       (if (and (eq (mail-header-number header)
8094                    (cdr gnus-article-current))
8095                (equal gnus-newsgroup-name
8096                       (car gnus-article-current)))
8097           ;; If we try to find the parent of the currently
8098           ;; displayed article, then we take a look at the actual
8099           ;; References header, since this is slightly more
8100           ;; reliable than the References field we got from the
8101           ;; server.
8102           (save-excursion
8103             (set-buffer gnus-original-article-buffer)
8104             (nnheader-narrow-to-headers)
8105             (unless (setq ref (message-fetch-field "references"))
8106               (when (setq ref (message-fetch-field "in-reply-to"))
8107                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8108             (widen))
8109         (setq ref
8110               ;; It's not the current article, so we take a bet on
8111               ;; the value we got from the server.
8112               (mail-header-references header)))
8113       (if (and ref
8114                (not (equal ref "")))
8115           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8116             (gnus-message 1 "Couldn't find parent"))
8117         (gnus-message 1 "No references in article %d"
8118                       (gnus-summary-article-number))
8119         (setq error t))
8120       (decf n))
8121     (gnus-summary-position-point)
8122     n))
8123
8124 (defun gnus-summary-refer-references ()
8125   "Fetch all articles mentioned in the References header.
8126 Return the number of articles fetched."
8127   (interactive)
8128   (let ((ref (mail-header-references (gnus-summary-article-header)))
8129         (current (gnus-summary-article-number))
8130         (n 0))
8131     (if (or (not ref)
8132             (equal ref ""))
8133         (error "No References in the current article")
8134       ;; For each Message-ID in the References header...
8135       (while (string-match "<[^>]*>" ref)
8136         (incf n)
8137         ;; ... fetch that article.
8138         (gnus-summary-refer-article
8139          (prog1 (match-string 0 ref)
8140            (setq ref (substring ref (match-end 0))))))
8141       (gnus-summary-goto-subject current)
8142       (gnus-summary-position-point)
8143       n)))
8144
8145 (defun gnus-summary-refer-thread (&optional limit)
8146   "Fetch all articles in the current thread.
8147 If LIMIT (the numerical prefix), fetch that many old headers instead
8148 of what's specified by the `gnus-refer-thread-limit' variable."
8149   (interactive "P")
8150   (let ((id (mail-header-id (gnus-summary-article-header)))
8151         (limit (if limit (prefix-numeric-value limit)
8152                  gnus-refer-thread-limit)))
8153     (unless (eq gnus-fetch-old-headers 'invisible)
8154       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8155       ;; Retrieve the headers and read them in.
8156       (if (eq (if (numberp limit)
8157                   (gnus-retrieve-headers
8158                    (list (min
8159                           (+ (mail-header-number
8160                               (gnus-summary-article-header))
8161                              limit)
8162                           gnus-newsgroup-end))
8163                    gnus-newsgroup-name (* limit 2))
8164                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8165                 ;; headers.
8166                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8167                                        gnus-newsgroup-name limit))
8168               'nov)
8169           (gnus-build-all-threads)
8170         (error "Can't fetch thread from back ends that don't support NOV"))
8171       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8172     (gnus-summary-limit-include-thread id)))
8173
8174 (defun gnus-summary-refer-article (message-id)
8175   "Fetch an article specified by MESSAGE-ID."
8176   (interactive "sMessage-ID: ")
8177   (when (and (stringp message-id)
8178              (not (zerop (length message-id))))
8179     (setq message-id (gnus-replace-in-string message-id " " ""))
8180     ;; Construct the correct Message-ID if necessary.
8181     ;; Suggested by tale@pawl.rpi.edu.
8182     (unless (string-match "^<" message-id)
8183       (setq message-id (concat "<" message-id)))
8184     (unless (string-match ">$" message-id)
8185       (setq message-id (concat message-id ">")))
8186     ;; People often post MIDs from URLs, so unhex it:
8187     (unless (string-match "@" message-id)
8188       (setq message-id (gnus-url-unhex-string message-id)))
8189     (let* ((header (gnus-id-to-header message-id))
8190            (sparse (and header
8191                         (gnus-summary-article-sparse-p
8192                          (mail-header-number header))
8193                         (memq (mail-header-number header)
8194                               gnus-newsgroup-limit)))
8195            number)
8196       (cond
8197        ;; If the article is present in the buffer we just go to it.
8198        ((and header
8199              (or (not (gnus-summary-article-sparse-p
8200                        (mail-header-number header)))
8201                  sparse))
8202         (prog1
8203             (gnus-summary-goto-article
8204              (mail-header-number header) nil t)
8205           (when sparse
8206             (gnus-summary-update-article (mail-header-number header)))))
8207        (t
8208         ;; We fetch the article.
8209         (catch 'found
8210           (dolist (gnus-override-method (gnus-refer-article-methods))
8211             (when (and (gnus-check-server gnus-override-method)
8212                        ;; Fetch the header,
8213                        (setq number (gnus-summary-insert-subject message-id)))
8214               ;; and display the article.
8215               (gnus-summary-select-article nil nil nil number)
8216               (throw 'found t)))
8217           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8218
8219 (defun gnus-refer-article-methods ()
8220   "Return a list of referable methods."
8221   (cond
8222    ;; No method, so we default to current and native.
8223    ((null gnus-refer-article-method)
8224     (list gnus-current-select-method gnus-select-method))
8225    ;; Current.
8226    ((eq 'current gnus-refer-article-method)
8227     (list gnus-current-select-method))
8228    ;; List of select methods.
8229    ((not (and (symbolp (car gnus-refer-article-method))
8230               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8231     (let (out)
8232       (dolist (method gnus-refer-article-method)
8233         (push (if (eq 'current method)
8234                   gnus-current-select-method
8235                 method)
8236               out))
8237       (nreverse out)))
8238    ;; One single select method.
8239    (t
8240     (list gnus-refer-article-method))))
8241
8242 (defun gnus-summary-edit-parameters ()
8243   "Edit the group parameters of the current group."
8244   (interactive)
8245   (gnus-group-edit-group gnus-newsgroup-name 'params))
8246
8247 (defun gnus-summary-customize-parameters ()
8248   "Customize the group parameters of the current group."
8249   (interactive)
8250   (gnus-group-customize gnus-newsgroup-name))
8251
8252 (defun gnus-summary-enter-digest-group (&optional force)
8253   "Enter an nndoc group based on the current article.
8254 If FORCE, force a digest interpretation.  If not, try
8255 to guess what the document format is."
8256   (interactive "P")
8257   (let ((conf gnus-current-window-configuration))
8258     (save-window-excursion
8259       (save-excursion
8260         (let (gnus-article-prepare-hook
8261               gnus-display-mime-function
8262               gnus-break-pages)
8263           (gnus-summary-select-article))))
8264     (setq gnus-current-window-configuration conf)
8265     (let* ((name (format "%s-%d"
8266                          (gnus-group-prefixed-name
8267                           gnus-newsgroup-name (list 'nndoc ""))
8268                          (save-excursion
8269                            (set-buffer gnus-summary-buffer)
8270                            gnus-current-article)))
8271            (ogroup gnus-newsgroup-name)
8272            (params (append (gnus-info-params (gnus-get-info ogroup))
8273                            (list (cons 'to-group ogroup))
8274                            (list (cons 'parent-group ogroup))
8275                            (list (cons 'save-article-group ogroup))))
8276            (case-fold-search t)
8277            (buf (current-buffer))
8278            dig to-address)
8279       (save-excursion
8280         (set-buffer gnus-original-article-buffer)
8281         ;; Have the digest group inherit the main mail address of
8282         ;; the parent article.
8283         (when (setq to-address (or (gnus-fetch-field "reply-to")
8284                                    (gnus-fetch-field "from")))
8285           (setq params (append
8286                         (list (cons 'to-address
8287                                     (funcall gnus-decode-encoded-word-function
8288                                              to-address))))))
8289         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8290         (insert-buffer-substring gnus-original-article-buffer)
8291         ;; Remove lines that may lead nndoc to misinterpret the
8292         ;; document type.
8293         (narrow-to-region
8294          (goto-char (point-min))
8295          (or (search-forward "\n\n" nil t) (point)))
8296         (goto-char (point-min))
8297         (delete-matching-lines "^Path:\\|^From ")
8298         (widen))
8299       (unwind-protect
8300           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8301                     (gnus-newsgroup-ephemeral-ignored-charsets
8302                      gnus-newsgroup-ignored-charsets))
8303                 (gnus-group-read-ephemeral-group
8304                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8305                               (nndoc-article-type
8306                                ,(if force 'mbox 'guess)))
8307                  t nil nil nil
8308                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8309                                                         "ADAPT")))))
8310               ;; Make all postings to this group go to the parent group.
8311               (nconc (gnus-info-params (gnus-get-info name))
8312                      params)
8313             ;; Couldn't select this doc group.
8314             (switch-to-buffer buf)
8315             (gnus-set-global-variables)
8316             (gnus-configure-windows 'summary)
8317             (gnus-message 3 "Article couldn't be entered?"))
8318         (kill-buffer dig)))))
8319
8320 (defun gnus-summary-read-document (n)
8321   "Open a new group based on the current article(s).
8322 This will allow you to read digests and other similar
8323 documents as newsgroups.
8324 Obeys the standard process/prefix convention."
8325   (interactive "P")
8326   (let* ((articles (gnus-summary-work-articles n))
8327          (ogroup gnus-newsgroup-name)
8328          (params (append (gnus-info-params (gnus-get-info ogroup))
8329                          (list (cons 'to-group ogroup))))
8330          article group egroup groups vgroup)
8331     (while (setq article (pop articles))
8332       (setq group (format "%s-%d" gnus-newsgroup-name article))
8333       (gnus-summary-remove-process-mark article)
8334       (when (gnus-summary-display-article article)
8335         (save-excursion
8336           (with-temp-buffer
8337             (insert-buffer-substring gnus-original-article-buffer)
8338             ;; Remove some headers that may lead nndoc to make
8339             ;; the wrong guess.
8340             (message-narrow-to-head)
8341             (goto-char (point-min))
8342             (delete-matching-lines "^Path:\\|^From ")
8343             (widen)
8344             (if (setq egroup
8345                       (gnus-group-read-ephemeral-group
8346                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8347                                      (nndoc-article-type guess))
8348                        t nil t))
8349                 (progn
8350                   ;; Make all postings to this group go to the parent group.
8351                   (nconc (gnus-info-params (gnus-get-info egroup))
8352                          params)
8353                   (push egroup groups))
8354               ;; Couldn't select this doc group.
8355               (gnus-error 3 "Article couldn't be entered"))))))
8356     ;; Now we have selected all the documents.
8357     (cond
8358      ((not groups)
8359       (error "None of the articles could be interpreted as documents"))
8360      ((gnus-group-read-ephemeral-group
8361        (setq vgroup (format
8362                      "nnvirtual:%s-%s" gnus-newsgroup-name
8363                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8364        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8365        t
8366        (cons (current-buffer) 'summary)))
8367      (t
8368       (error "Couldn't select virtual nndoc group")))))
8369
8370 (defun gnus-summary-isearch-article (&optional regexp-p)
8371   "Do incremental search forward on the current article.
8372 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8373   (interactive "P")
8374   (let* ((gnus-inhibit-treatment t)
8375          (old (gnus-summary-select-article)))
8376     (gnus-configure-windows 'article)
8377     (gnus-eval-in-buffer-window gnus-article-buffer
8378       (save-restriction
8379         (widen)
8380         (when (eq 'old old)
8381           (gnus-article-show-all-headers))
8382         (goto-char (point-min))
8383         (isearch-forward regexp-p)))))
8384
8385 (defun gnus-summary-search-article-forward (regexp &optional backward)
8386   "Search for an article containing REGEXP forward.
8387 If BACKWARD, search backward instead."
8388   (interactive
8389    (list (read-string
8390           (format "Search article %s (regexp%s): "
8391                   (if current-prefix-arg "backward" "forward")
8392                   (if gnus-last-search-regexp
8393                       (concat ", default " gnus-last-search-regexp)
8394                     "")))
8395          current-prefix-arg))
8396   (if (string-equal regexp "")
8397       (setq regexp (or gnus-last-search-regexp ""))
8398     (setq gnus-last-search-regexp regexp)
8399     (setq gnus-article-before-search gnus-current-article))
8400   ;; Intentionally set gnus-last-article.
8401   (setq gnus-last-article gnus-article-before-search)
8402   (let ((gnus-last-article gnus-last-article))
8403     (if (gnus-summary-search-article regexp backward)
8404         (gnus-summary-show-thread)
8405       (error "Search failed: \"%s\"" regexp))))
8406
8407 (defun gnus-summary-search-article-backward (regexp)
8408   "Search for an article containing REGEXP backward."
8409   (interactive
8410    (list (read-string
8411           (format "Search article backward (regexp%s): "
8412                   (if gnus-last-search-regexp
8413                       (concat ", default " gnus-last-search-regexp)
8414                     "")))))
8415   (gnus-summary-search-article-forward regexp 'backward))
8416
8417 (eval-when-compile
8418   (defmacro gnus-summary-search-article-position-point (regexp backward)
8419     "Dehighlight the last matched text and goto the beginning position."
8420     (` (if (and gnus-summary-search-article-matched-data
8421                 (let ((text (caddr gnus-summary-search-article-matched-data))
8422                       (inhibit-read-only t)
8423                       buffer-read-only)
8424                   (delete-region
8425                    (goto-char (car gnus-summary-search-article-matched-data))
8426                    (cadr gnus-summary-search-article-matched-data))
8427                   (insert text)
8428                   (string-match (, regexp) text)))
8429            (if (, backward) (beginning-of-line) (end-of-line))
8430          (goto-char (if (, backward) (point-max) (point-min))))))
8431
8432   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8433     "Place point where X-Face image is displayed."
8434     (if (featurep 'xemacs)
8435         (` (let ((end (if (search-forward "\n\n" nil t)
8436                           (goto-char (1- (point)))
8437                         (point-min)))
8438                  extent)
8439              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8440              (unless (and (re-search-forward "^From:" end t)
8441                           (setq extent (extent-at (point)))
8442                           (extent-begin-glyph extent))
8443                (goto-char (, opoint)))))
8444       (` (let ((end (if (search-forward "\n\n" nil t)
8445                         (goto-char (1- (point)))
8446                       (point-min)))
8447                (start (or (search-backward "\n\n" nil t) (point-min))))
8448            (goto-char
8449             (or (text-property-any start end 'x-face-image t);; x-face-e21
8450                 (text-property-any start end 'x-face-mule-bitmap-image t)
8451                 (, opoint)))))))
8452
8453   (defmacro gnus-summary-search-article-highlight-matched-text
8454     (backward treated x-face)
8455     "Highlight matched text in the function `gnus-summary-search-article'."
8456     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8457              (end (set-marker (make-marker) (match-end 0)))
8458              (inhibit-read-only t)
8459              buffer-read-only)
8460          (unless treated
8461            (let ((,@
8462                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8463                     (mapcar
8464                      (lambda (item) (setq items (delq item items)))
8465                      '(gnus-treat-buttonize
8466                        gnus-treat-fill-article
8467                        gnus-treat-fill-long-lines
8468                        gnus-treat-emphasize
8469                        gnus-treat-highlight-headers
8470                        gnus-treat-highlight-citation
8471                        gnus-treat-highlight-signature
8472                        gnus-treat-overstrike
8473                        gnus-treat-display-x-face
8474                        gnus-treat-buttonize-head
8475                        gnus-treat-decode-article-as-default-mime-charset))
8476                     (static-if (featurep 'xemacs)
8477                         items
8478                       (cons '(x-face-mule-delete-x-face-field
8479                               (quote never))
8480                             items))))
8481                  (gnus-treat-display-x-face
8482                   (when (, x-face) gnus-treat-display-x-face)))
8483              (gnus-article-prepare-mime-display)))
8484          (goto-char (if (, backward) start end))
8485          (when (, x-face)
8486            (gnus-summary-search-article-highlight-goto-x-face (point)))
8487          (setq gnus-summary-search-article-matched-data
8488                (list start end (buffer-substring start end)))
8489          (unless (eq start end);; matched text has been deleted. :-<
8490            (put-text-property start end 'face
8491                               (or (find-face 'isearch)
8492                                   'secondary-selection))))))
8493   )
8494
8495 (defun gnus-summary-search-article (regexp &optional backward)
8496   "Search for an article containing REGEXP.
8497 Optional argument BACKWARD means do search for backward.
8498 `gnus-select-article-hook' is not called during the search."
8499   ;; We have to require this here to make sure that the following
8500   ;; dynamic binding isn't shadowed by autoloading.
8501   (require 'gnus-async)
8502   (require 'gnus-art)
8503   (let ((gnus-select-article-hook nil)  ;Disable hook.
8504         (gnus-article-prepare-hook nil)
8505         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8506         (gnus-use-article-prefetch nil)
8507         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8508         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8509         (gnus-visual nil)
8510         (gnus-keep-backlog nil)
8511         (gnus-break-pages nil)
8512         (gnus-summary-display-arrow nil)
8513         (gnus-updated-mode-lines nil)
8514         (gnus-auto-center-summary nil)
8515         (sum (current-buffer))
8516         (found nil)
8517         point treated)
8518     (gnus-save-hidden-threads
8519       (static-if (featurep 'xemacs)
8520           (let ((gnus-inhibit-treatment t))
8521             (setq treated (eq 'old (gnus-summary-select-article)))
8522             (when (and treated
8523                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8524                                  (window-live-p (get-buffer-window
8525                                                  gnus-article-buffer t)))))
8526               (gnus-summary-select-article nil t)
8527               (setq treated nil)))
8528         (let ((gnus-inhibit-treatment t)
8529               (x-face-mule-delete-x-face-field 'never))
8530           (setq treated (eq 'old (gnus-summary-select-article)))
8531           (when (and treated
8532                      (not
8533                       (and (gnus-buffer-live-p gnus-article-buffer)
8534                            (window-live-p (get-buffer-window
8535                                            gnus-article-buffer t))
8536                            (or (not (string-match "^\\^X-Face:" regexp))
8537                                (with-current-buffer gnus-article-buffer
8538                                  gnus-summary-search-article-matched-data)))))
8539             (gnus-summary-select-article nil t)
8540             (setq treated nil))))
8541       (set-buffer gnus-article-buffer)
8542       (widen)
8543       (if treated
8544           (progn
8545             (gnus-article-show-all-headers)
8546             (gnus-summary-search-article-position-point regexp backward))
8547         (goto-char (if backward (point-max) (point-min))))
8548       (while (not found)
8549         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8550         (if (if backward
8551                 (re-search-backward regexp nil t)
8552               (re-search-forward regexp nil t))
8553             ;; We found the regexp.
8554             (progn
8555               (gnus-summary-search-article-highlight-matched-text
8556                backward treated (string-match "^\\^X-Face:" regexp))
8557               (setq found 'found)
8558               (forward-line
8559                (/ (- 2 (window-height
8560                         (get-buffer-window gnus-article-buffer t)))
8561                   2))
8562               (set-window-start
8563                (get-buffer-window (current-buffer))
8564                (point))
8565               (set-buffer sum)
8566               (setq point (point)))
8567           ;; We didn't find it, so we go to the next article.
8568           (set-buffer sum)
8569           (setq found 'not)
8570           (while (eq found 'not)
8571             (if (not (if backward (gnus-summary-find-prev)
8572                        (gnus-summary-find-next)))
8573                 ;; No more articles.
8574                 (setq found t)
8575               ;; Select the next article and adjust point.
8576               (unless (gnus-summary-article-sparse-p
8577                        (gnus-summary-article-number))
8578                 (setq found nil)
8579                 (let ((gnus-inhibit-treatment t))
8580                   (gnus-summary-select-article))
8581                 (setq treated nil)
8582                 (set-buffer gnus-article-buffer)
8583                 (widen)
8584                 (goto-char (if backward (point-max) (point-min))))))))
8585       (gnus-message 7 ""))
8586     ;; Return whether we found the regexp.
8587     (when (eq found 'found)
8588       (goto-char point)
8589       (gnus-summary-show-thread)
8590       (gnus-summary-goto-subject gnus-current-article)
8591       (gnus-summary-position-point)
8592       t)))
8593
8594 (defun gnus-find-matching-articles (header regexp)
8595   "Return a list of all articles that match REGEXP on HEADER.
8596 This search includes all articles in the current group that Gnus has
8597 fetched headers for, whether they are displayed or not."
8598   (let ((articles nil)
8599         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8600         (case-fold-search t))
8601     (dolist (header gnus-newsgroup-headers)
8602       (when (string-match regexp (funcall func header))
8603         (push (mail-header-number header) articles)))
8604     (nreverse articles)))
8605
8606 (defun gnus-summary-find-matching (header regexp &optional backward unread
8607                                           not-case-fold not-matching)
8608   "Return a list of all articles that match REGEXP on HEADER.
8609 The search stars on the current article and goes forwards unless
8610 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8611 If UNREAD is non-nil, only unread articles will
8612 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8613 in the comparisons. If NOT-MATCHING, return a list of all articles that
8614 not match REGEXP on HEADER."
8615   (let ((case-fold-search (not not-case-fold))
8616         articles d func)
8617     (if (consp header)
8618         (if (eq (car header) 'extra)
8619             (setq func
8620                   `(lambda (h)
8621                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8622                          "")))
8623           (error "%s is an invalid header" header))
8624       (unless (fboundp (intern (concat "mail-header-" header)))
8625         (error "%s is not a valid header" header))
8626       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8627     (dolist (d (if (eq backward 'all)
8628                    gnus-newsgroup-data
8629                  (gnus-data-find-list
8630                   (gnus-summary-article-number)
8631                   (gnus-data-list backward))))
8632       (when (and (or (not unread)       ; We want all articles...
8633                      (gnus-data-unread-p d)) ; Or just unreads.
8634                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8635                  (if not-matching
8636                      (not (string-match
8637                            regexp
8638                            (funcall func (gnus-data-header d))))
8639                    (string-match regexp
8640                                  (funcall func (gnus-data-header d)))))
8641         (push (gnus-data-number d) articles))) ; Success!
8642     (nreverse articles)))
8643
8644 (defun gnus-summary-execute-command (header regexp command &optional backward)
8645   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8646 If HEADER is an empty string (or nil), the match is done on the entire
8647 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8648   (interactive
8649    (list (let ((completion-ignore-case t))
8650            (completing-read
8651             "Header name: "
8652             (mapcar (lambda (header) (list (format "%s" header)))
8653                     (append
8654                      '("Number" "Subject" "From" "Lines" "Date"
8655                        "Message-ID" "Xref" "References" "Body")
8656                      gnus-extra-headers))
8657             nil 'require-match))
8658          (read-string "Regexp: ")
8659          (read-key-sequence "Command: ")
8660          current-prefix-arg))
8661   (when (equal header "Body")
8662     (setq header ""))
8663   ;; Hidden thread subtrees must be searched as well.
8664   (gnus-summary-show-all-threads)
8665   ;; We don't want to change current point nor window configuration.
8666   (save-excursion
8667     (save-window-excursion
8668       (let (gnus-visual
8669             gnus-treat-strip-trailing-blank-lines
8670             gnus-treat-strip-leading-blank-lines
8671             gnus-treat-strip-multiple-blank-lines
8672             gnus-treat-hide-boring-headers
8673             gnus-treat-fold-newsgroups
8674             gnus-article-prepare-hook)
8675         (gnus-message 6 "Executing %s..." (key-description command))
8676         ;; We'd like to execute COMMAND interactively so as to give arguments.
8677         (gnus-execute header regexp
8678                       `(call-interactively ',(key-binding command))
8679                       backward)
8680         (gnus-message 6 "Executing %s...done" (key-description command))))))
8681
8682 (defun gnus-summary-beginning-of-article ()
8683   "Scroll the article back to the beginning."
8684   (interactive)
8685   (gnus-summary-select-article)
8686   (gnus-configure-windows 'article)
8687   (gnus-eval-in-buffer-window gnus-article-buffer
8688     (widen)
8689     (goto-char (point-min))
8690     (when gnus-break-pages
8691       (gnus-narrow-to-page))))
8692
8693 (defun gnus-summary-end-of-article ()
8694   "Scroll to the end of the article."
8695   (interactive)
8696   (gnus-summary-select-article)
8697   (gnus-configure-windows 'article)
8698   (gnus-eval-in-buffer-window gnus-article-buffer
8699     (widen)
8700     (goto-char (point-max))
8701     (recenter -3)
8702     (when gnus-break-pages
8703       (when (re-search-backward page-delimiter nil t)
8704         (narrow-to-region (match-end 0) (point-max)))
8705       (gnus-narrow-to-page))))
8706
8707 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8708   "Truncate to LEN and quote all \"(\"'s in STRING."
8709   (gnus-replace-in-string (if (and len (> (length string) len))
8710                               (substring string 0 len)
8711                             string)
8712                           "[()]" "\\\\\\&"))
8713
8714 (defun gnus-summary-print-article (&optional filename n)
8715   "Generate and print a PostScript image of the process-marked (mail) articles.
8716
8717 If used interactively, print the current article if none are
8718 process-marked.  With prefix arg, prompt the user for the name of the
8719 file to save in.
8720
8721 When used from Lisp, accept two optional args FILENAME and N.  N means
8722 to print the next N articles.  If N is negative, print the N previous
8723 articles.  If N is nil and articles have been marked with the process
8724 mark, print these instead.
8725
8726 If the optional first argument FILENAME is nil, send the image to the
8727 printer.  If FILENAME is a string, save the PostScript image in a file with
8728 that name.  If FILENAME is a number, prompt the user for the name of the file
8729 to save in."
8730   (interactive (list (ps-print-preprint current-prefix-arg)))
8731   (dolist (article (gnus-summary-work-articles n))
8732     (gnus-summary-select-article nil nil 'pseudo article)
8733     (gnus-eval-in-buffer-window gnus-article-buffer
8734       (gnus-print-buffer))
8735     (gnus-summary-remove-process-mark article))
8736   (ps-despool filename))
8737
8738 (defun gnus-print-buffer ()
8739   (let ((buffer (generate-new-buffer " *print*")))
8740     (unwind-protect
8741         (progn
8742           (copy-to-buffer buffer (point-min) (point-max))
8743           (set-buffer buffer)
8744           (gnus-remove-text-with-property 'gnus-decoration)
8745           (when (gnus-visual-p 'article-highlight 'highlight)
8746             ;; Copy-to-buffer doesn't copy overlay.  So redo
8747             ;; highlight.
8748             (let ((gnus-article-buffer buffer))
8749               (gnus-article-highlight-citation t)
8750               (gnus-article-highlight-signature)
8751               (gnus-article-emphasize)
8752               (gnus-article-delete-invisible-text)))
8753           (let ((ps-left-header
8754                  (list
8755                   (concat "("
8756                           (gnus-summary-print-truncate-and-quote
8757                            (mail-header-subject gnus-current-headers)
8758                            66) ")")
8759                   (concat "("
8760                           (gnus-summary-print-truncate-and-quote
8761                            (mail-header-from gnus-current-headers)
8762                            45) ")")))
8763                 (ps-right-header
8764                  (list
8765                   "/pagenumberstring load"
8766                   (concat "("
8767                           (mail-header-date gnus-current-headers) ")"))))
8768             (gnus-run-hooks 'gnus-ps-print-hook)
8769             (save-excursion
8770               (if window-system
8771                   (ps-spool-buffer-with-faces)
8772                 (ps-spool-buffer)))))
8773       (kill-buffer buffer))))
8774
8775 (defun gnus-summary-show-article (&optional arg)
8776   "Force redisplaying of the current article.
8777 If ARG (the prefix) is a number, show the article with the charset
8778 defined in `gnus-summary-show-article-charset-alist', or the charset
8779 input.
8780 If ARG (the prefix) is non-nil and not a number, show the raw article
8781 without any article massaging functions being run.  Normally, the key
8782 strokes are `C-u g'."
8783   (interactive "P")
8784   (cond
8785    ((numberp arg)
8786     (gnus-summary-show-article t)
8787     (let* ((gnus-newsgroup-charset
8788             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8789                 (mm-read-coding-system
8790                  "View as charset: " ;; actually it is coding system.
8791                  (save-excursion
8792                    (set-buffer gnus-article-buffer)
8793                    (mm-detect-coding-region (point) (point-max))))))
8794            (default-mime-charset gnus-newsgroup-charset)
8795            (gnus-newsgroup-ignored-charsets 'gnus-all))
8796       (gnus-summary-select-article nil 'force)
8797       (let ((deps gnus-newsgroup-dependencies)
8798             head header lines)
8799         (save-excursion
8800           (set-buffer gnus-original-article-buffer)
8801           (save-restriction
8802             (message-narrow-to-head)
8803             (setq head (buffer-string))
8804             (goto-char (point-min))
8805             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8806               (goto-char (point-max))
8807               (widen)
8808               (setq lines (1- (count-lines (point) (point-max))))))
8809           (with-temp-buffer
8810             (insert (format "211 %d Article retrieved.\n"
8811                             (cdr gnus-article-current)))
8812             (insert head)
8813             (if lines (insert (format "Lines: %d\n" lines)))
8814             (insert ".\n")
8815             (let ((nntp-server-buffer (current-buffer)))
8816               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8817         (gnus-data-set-header
8818          (gnus-data-find (cdr gnus-article-current))
8819          header)
8820         (gnus-summary-update-article-line
8821          (cdr gnus-article-current) header)
8822         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8823           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8824    ((not arg)
8825     ;; Select the article the normal way.
8826     (gnus-summary-select-article nil 'force))
8827    (t
8828     ;; We have to require this here to make sure that the following
8829     ;; dynamic binding isn't shadowed by autoloading.
8830     (require 'gnus-async)
8831     (require 'gnus-art)
8832     ;; Bind the article treatment functions to nil.
8833     (let ((gnus-have-all-headers t)
8834           gnus-article-prepare-hook
8835           gnus-article-decode-hook
8836           gnus-break-pages
8837           gnus-show-mime
8838           (gnus-inhibit-treatment t))
8839       (gnus-summary-select-article nil 'force))))
8840   (gnus-summary-goto-subject gnus-current-article)
8841   (gnus-summary-position-point))
8842
8843 (defun gnus-summary-show-raw-article ()
8844   "Show the raw article without any article massaging functions being run."
8845   (interactive)
8846   (gnus-summary-show-article t))
8847
8848 (defun gnus-summary-verbose-headers (&optional arg)
8849   "Toggle permanent full header display.
8850 If ARG is a positive number, turn header display on.
8851 If ARG is a negative number, turn header display off."
8852   (interactive "P")
8853   (setq gnus-show-all-headers
8854         (cond ((or (not (numberp arg))
8855                    (zerop arg))
8856                (not gnus-show-all-headers))
8857               ((natnump arg)
8858                t)))
8859   (gnus-summary-show-article))
8860
8861 (defun gnus-summary-toggle-header (&optional arg)
8862   "Show the headers if they are hidden, or hide them if they are shown.
8863 If ARG is a positive number, show the entire header.
8864 If ARG is a negative number, hide the unwanted header lines."
8865   (interactive "P")
8866   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8867                      (get-buffer-window gnus-article-buffer t))))
8868     (with-current-buffer gnus-article-buffer
8869       (widen)
8870       (article-narrow-to-head)
8871       (let* ((buffer-read-only nil)
8872              (inhibit-point-motion-hooks t)
8873              (hidden (if (numberp arg)
8874                          (>= arg 0)
8875                        (gnus-article-hidden-text-p 'headers)))
8876              s e)
8877         (delete-region (point-min) (point-max))
8878         (with-current-buffer gnus-original-article-buffer
8879           (goto-char (setq s (point-min)))
8880           (setq e (if (search-forward "\n\n" nil t)
8881                       (1- (point))
8882                     (point-max))))
8883         (insert-buffer-substring gnus-original-article-buffer s e)
8884         ;; In T-gnus, gnus-article-decode-hook doesn't contain
8885         ;; article-decode-encoded-words by default.
8886         (article-decode-encoded-words)
8887         (run-hooks 'gnus-article-decode-hook)
8888         (if hidden
8889             (let ((gnus-treat-hide-headers nil)
8890                   (gnus-treat-hide-boring-headers nil))
8891               (gnus-delete-wash-type 'headers)
8892               (gnus-treat-article 'head))
8893           (gnus-treat-article 'head))
8894         (widen)
8895         (if window
8896             (set-window-start window (goto-char (point-min))))
8897         (if gnus-break-pages
8898             (gnus-narrow-to-page)
8899           (when (gnus-visual-p 'page-marker)
8900             (let ((buffer-read-only nil))
8901               (gnus-remove-text-with-property 'gnus-prev)
8902               (gnus-remove-text-with-property 'gnus-next))))
8903         (gnus-set-mode-line 'article)))))
8904
8905 (defun gnus-summary-show-all-headers ()
8906   "Make all header lines visible."
8907   (interactive)
8908   (gnus-summary-toggle-header 1))
8909
8910 (defun gnus-summary-toggle-mime (&optional arg)
8911   "Toggle MIME processing.
8912 If ARG is a positive number, turn MIME processing on."
8913   (interactive "P")
8914   (setq gnus-show-mime
8915         (if (null arg)
8916             (not gnus-show-mime)
8917           (> (prefix-numeric-value arg) 0)))
8918   (gnus-summary-select-article t 'force))
8919
8920 (defun gnus-summary-caesar-message (&optional arg)
8921   "Caesar rotate the current article by 13.
8922 The numerical prefix specifies how many places to rotate each letter
8923 forward."
8924   (interactive "P")
8925   (gnus-summary-select-article)
8926   (let ((mail-header-separator ""))
8927     (gnus-eval-in-buffer-window gnus-article-buffer
8928       (save-restriction
8929         (widen)
8930         (let ((start (window-start))
8931               buffer-read-only)
8932           (message-caesar-buffer-body arg)
8933           (set-window-start (get-buffer-window (current-buffer)) start))))))
8934
8935 (autoload 'unmorse-region "morse"
8936   "Convert morse coded text in region to ordinary ASCII text."
8937   t)
8938
8939 (defun gnus-summary-morse-message (&optional arg)
8940   "Morse decode the current article."
8941   (interactive "P")
8942   (gnus-summary-select-article)
8943   (let ((mail-header-separator ""))
8944     (gnus-eval-in-buffer-window gnus-article-buffer
8945       (save-excursion
8946         (save-restriction
8947           (widen)
8948           (let ((pos (window-start))
8949                 buffer-read-only)
8950             (goto-char (point-min))
8951             (when (message-goto-body)
8952               (gnus-narrow-to-body))
8953             (goto-char (point-min))
8954             (while (re-search-forward "·" (point-max) t)
8955               (replace-match "."))
8956             (unmorse-region (point-min) (point-max))
8957             (widen)
8958             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8959
8960 (defun gnus-summary-stop-page-breaking ()
8961   "Stop page breaking in the current article."
8962   (interactive)
8963   (gnus-summary-select-article)
8964   (gnus-eval-in-buffer-window gnus-article-buffer
8965     (widen)
8966     (when (gnus-visual-p 'page-marker)
8967       (let ((buffer-read-only nil))
8968         (gnus-remove-text-with-property 'gnus-prev)
8969         (gnus-remove-text-with-property 'gnus-next))
8970       (setq gnus-page-broken nil))))
8971
8972 (defun gnus-summary-move-article (&optional n to-newsgroup
8973                                             select-method action)
8974   "Move the current article to a different newsgroup.
8975 If N is a positive number, move the N next articles.
8976 If N is a negative number, move the N previous articles.
8977 If N is nil and any articles have been marked with the process mark,
8978 move those articles instead.
8979 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8980 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8981 re-spool using this method.
8982
8983 When called interactively with TO-NEWSGROUP being nil, the value of
8984 the variable `gnus-move-split-methods' is used for finding a default
8985 for the target newsgroup.
8986
8987 For this function to work, both the current newsgroup and the
8988 newsgroup that you want to move to have to support the `request-move'
8989 and `request-accept' functions.
8990
8991 ACTION can be either `move' (the default), `crosspost' or `copy'."
8992   (interactive "P")
8993   (unless action
8994     (setq action 'move))
8995   ;; Check whether the source group supports the required functions.
8996   (cond ((and (eq action 'move)
8997               (not (gnus-check-backend-function
8998                     'request-move-article gnus-newsgroup-name)))
8999          (error "The current group does not support article moving"))
9000         ((and (eq action 'crosspost)
9001               (not (gnus-check-backend-function
9002                     'request-replace-article gnus-newsgroup-name)))
9003          (error "The current group does not support article editing")))
9004   (let ((articles (gnus-summary-work-articles n))
9005         (prefix (if (gnus-check-backend-function
9006                      'request-move-article gnus-newsgroup-name)
9007                     (gnus-group-real-prefix gnus-newsgroup-name)
9008                   ""))
9009         (names '((move "Move" "Moving")
9010                  (copy "Copy" "Copying")
9011                  (crosspost "Crosspost" "Crossposting")))
9012         (copy-buf (save-excursion
9013                     (nnheader-set-temp-buffer " *copy article*")))
9014         art-group to-method new-xref article to-groups)
9015     (unless (assq action names)
9016       (error "Unknown action %s" action))
9017     ;; Read the newsgroup name.
9018     (when (and (not to-newsgroup)
9019                (not select-method))
9020       (if (and gnus-move-split-methods
9021                (not
9022                 (and (memq gnus-current-article articles)
9023                      (gnus-buffer-live-p gnus-original-article-buffer))))
9024           ;; When `gnus-move-split-methods' is non-nil, we have to
9025           ;; select an article to give `gnus-read-move-group-name' an
9026           ;; opportunity to suggest an appropriate default.  However,
9027           ;; we needn't render or mark the article.
9028           (let ((gnus-display-mime-function nil)
9029                 (gnus-article-prepare-hook nil)
9030                 (gnus-mark-article-hook nil))
9031             (gnus-summary-select-article nil nil nil (car articles))))
9032       (setq to-newsgroup
9033             (gnus-read-move-group-name
9034              (cadr (assq action names))
9035              (symbol-value (intern (format "gnus-current-%s-group" action)))
9036              articles prefix))
9037       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9038     (setq to-method (or select-method
9039                         (gnus-server-to-method
9040                          (gnus-group-method to-newsgroup))))
9041     ;; Check the method we are to move this article to...
9042     (unless (gnus-check-backend-function
9043              'request-accept-article (car to-method))
9044       (error "%s does not support article copying" (car to-method)))
9045     (unless (gnus-check-server to-method)
9046       (error "Can't open server %s" (car to-method)))
9047     (gnus-message 6 "%s to %s: %s..."
9048                   (caddr (assq action names))
9049                   (or (car select-method) to-newsgroup) articles)
9050     (while articles
9051       (setq article (pop articles))
9052       (setq
9053        art-group
9054        (cond
9055         ;; Move the article.
9056         ((eq action 'move)
9057          ;; Remove this article from future suppression.
9058          (gnus-dup-unsuppress-article article)
9059          (gnus-request-move-article
9060           article                       ; Article to move
9061           gnus-newsgroup-name           ; From newsgroup
9062           (nth 1 (gnus-find-method-for-group
9063                   gnus-newsgroup-name)) ; Server
9064           (list 'gnus-request-accept-article
9065                 to-newsgroup (list 'quote select-method)
9066                 (not articles) t)       ; Accept form
9067           (not articles)))              ; Only save nov last time
9068         ;; Copy the article.
9069         ((eq action 'copy)
9070          (save-excursion
9071            (set-buffer copy-buf)
9072            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9073              (gnus-request-accept-article
9074               to-newsgroup select-method (not articles) t))))
9075         ;; Crosspost the article.
9076         ((eq action 'crosspost)
9077          (let ((xref (message-tokenize-header
9078                       (mail-header-xref (gnus-summary-article-header article))
9079                       " ")))
9080            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9081                                   ":" (number-to-string article)))
9082            (unless xref
9083              (setq xref (list (system-name))))
9084            (setq new-xref
9085                  (concat
9086                   (mapconcat 'identity
9087                              (delete "Xref:" (delete new-xref xref))
9088                              " ")
9089                   " " new-xref))
9090            (save-excursion
9091              (set-buffer copy-buf)
9092              ;; First put the article in the destination group.
9093              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9094              (when (consp (setq art-group
9095                                 (gnus-request-accept-article
9096                                  to-newsgroup select-method (not articles))))
9097                (setq new-xref (concat new-xref " " (car art-group)
9098                                       ":"
9099                                       (number-to-string (cdr art-group))))
9100                ;; Now we have the new Xrefs header, so we insert
9101                ;; it and replace the new article.
9102                (nnheader-replace-header "Xref" new-xref)
9103                (gnus-request-replace-article
9104                 (cdr art-group) to-newsgroup (current-buffer))
9105                art-group))))))
9106       (cond
9107        ((not art-group)
9108         (gnus-message 1 "Couldn't %s article %s: %s"
9109                       (cadr (assq action names)) article
9110                       (nnheader-get-report (car to-method))))
9111        ((eq art-group 'junk)
9112         (when (eq action 'move)
9113           (gnus-summary-mark-article article gnus-canceled-mark)
9114           (gnus-message 4 "Deleted article %s" article)
9115           ;; run the delete hook
9116           (run-hook-with-args 'gnus-summary-article-delete-hook
9117                               action
9118                               (gnus-data-header
9119                                (assoc article (gnus-data-list nil)))
9120                               gnus-newsgroup-name nil
9121                               select-method)))
9122        (t
9123         (let* ((pto-group (gnus-group-prefixed-name
9124                            (car art-group) to-method))
9125                (info (gnus-get-info pto-group))
9126                (to-group (gnus-info-group info))
9127                to-marks)
9128           ;; Update the group that has been moved to.
9129           (when (and info
9130                      (memq action '(move copy)))
9131             (unless (member to-group to-groups)
9132               (push to-group to-groups))
9133
9134             (unless (memq article gnus-newsgroup-unreads)
9135               (push 'read to-marks)
9136               (gnus-info-set-read
9137                info (gnus-add-to-range (gnus-info-read info)
9138                                        (list (cdr art-group)))))
9139
9140             ;; See whether the article is to be put in the cache.
9141             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9142                              gnus-article-mark-lists
9143                            (delete '(expirable . expire)
9144                                    (copy-sequence gnus-article-mark-lists))))
9145                   (to-article (cdr art-group)))
9146
9147               ;; Enter the article into the cache in the new group,
9148               ;; if that is required.
9149               (when gnus-use-cache
9150                 (gnus-cache-possibly-enter-article
9151                  to-group to-article
9152                  (let ((header (copy-sequence
9153                                 (gnus-summary-article-header article))))
9154                    (mail-header-set-number header to-article)
9155                    header)
9156                  (memq article gnus-newsgroup-marked)
9157                  (memq article gnus-newsgroup-dormant)
9158                  (memq article gnus-newsgroup-unreads)))
9159
9160               (when gnus-preserve-marks
9161                 ;; Copy any marks over to the new group.
9162                 (when (and (equal to-group gnus-newsgroup-name)
9163                            (not (memq article gnus-newsgroup-unreads)))
9164                   ;; Mark this article as read in this group.
9165                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9166                   (setcdr (gnus-active to-group) to-article)
9167                   (setcdr gnus-newsgroup-active to-article))
9168
9169                 (while marks
9170                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9171                     (when (memq article (symbol-value
9172                                          (intern (format "gnus-newsgroup-%s"
9173                                                          (caar marks)))))
9174                       (push (cdar marks) to-marks)
9175                       ;; If the other group is the same as this group,
9176                       ;; then we have to add the mark to the list.
9177                       (when (equal to-group gnus-newsgroup-name)
9178                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9179                              (cons to-article
9180                                    (symbol-value
9181                                     (intern (format "gnus-newsgroup-%s"
9182                                                     (caar marks)))))))
9183                       ;; Copy the marks to other group.
9184                       (gnus-add-marked-articles
9185                        to-group (cdar marks) (list to-article) info)))
9186                   (setq marks (cdr marks)))
9187
9188                 (gnus-request-set-mark
9189                  to-group (list (list (list to-article) 'add to-marks))))
9190
9191               (gnus-dribble-enter
9192                (concat "(gnus-group-set-info '"
9193                        (gnus-prin1-to-string (gnus-get-info to-group))
9194                        ")"))))
9195
9196           ;; Update the Xref header in this article to point to
9197           ;; the new crossposted article we have just created.
9198           (when (eq action 'crosspost)
9199             (save-excursion
9200               (set-buffer copy-buf)
9201               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9202               (nnheader-replace-header "Xref" new-xref)
9203               (gnus-request-replace-article
9204                article gnus-newsgroup-name (current-buffer))))
9205
9206           ;; run the move/copy/crosspost/respool hook
9207           (run-hook-with-args 'gnus-summary-article-move-hook
9208                               action
9209                               (gnus-data-header
9210                                (assoc article (gnus-data-list nil)))
9211                               gnus-newsgroup-name
9212                               to-newsgroup
9213                               select-method))
9214
9215         ;;;!!!Why is this necessary?
9216         (set-buffer gnus-summary-buffer)
9217         
9218         (gnus-summary-goto-subject article)
9219         (when (eq action 'move)
9220           (gnus-summary-mark-article article gnus-canceled-mark))))
9221       (gnus-summary-remove-process-mark article))
9222     ;; Re-activate all groups that have been moved to.
9223     (save-excursion
9224       (set-buffer gnus-group-buffer)
9225       (let ((gnus-group-marked to-groups))
9226         (gnus-group-get-new-news-this-group nil t)))
9227
9228     (gnus-kill-buffer copy-buf)
9229     (gnus-summary-position-point)
9230     (gnus-set-mode-line 'summary)))
9231
9232 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9233   "Move the current article to a different newsgroup.
9234 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9235 When called interactively, if TO-NEWSGROUP is nil, use the value of
9236 the variable `gnus-move-split-methods' for finding a default target
9237 newsgroup.
9238 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9239 re-spool using this method."
9240   (interactive "P")
9241   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9242
9243 (defun gnus-summary-crosspost-article (&optional n)
9244   "Crosspost the current article to some other group."
9245   (interactive "P")
9246   (gnus-summary-move-article n nil nil 'crosspost))
9247
9248 (defcustom gnus-summary-respool-default-method nil
9249   "Default method type for respooling an article.
9250 If nil, use to the current newsgroup method."
9251   :type 'symbol
9252   :group 'gnus-summary-mail)
9253
9254 (defcustom gnus-summary-display-while-building nil
9255   "If non-nil, show and update the summary buffer as it's being built.
9256 If the value is t, update the buffer after every line is inserted.  If
9257 the value is an integer (N), update the display every N lines."
9258   :group 'gnus-thread
9259   :type '(choice (const :tag "off" nil)
9260                  number
9261                  (const :tag "frequently" t)))
9262
9263 (defun gnus-summary-respool-article (&optional n method)
9264   "Respool the current article.
9265 The article will be squeezed through the mail spooling process again,
9266 which means that it will be put in some mail newsgroup or other
9267 depending on `nnmail-split-methods'.
9268 If N is a positive number, respool the N next articles.
9269 If N is a negative number, respool the N previous articles.
9270 If N is nil and any articles have been marked with the process mark,
9271 respool those articles instead.
9272
9273 Respooling can be done both from mail groups and \"real\" newsgroups.
9274 In the former case, the articles in question will be moved from the
9275 current group into whatever groups they are destined to.  In the
9276 latter case, they will be copied into the relevant groups."
9277   (interactive
9278    (list current-prefix-arg
9279          (let* ((methods (gnus-methods-using 'respool))
9280                 (methname
9281                  (symbol-name (or gnus-summary-respool-default-method
9282                                   (car (gnus-find-method-for-group
9283                                         gnus-newsgroup-name)))))
9284                 (method
9285                  (gnus-completing-read-with-default
9286                   methname "What backend do you want to use when respooling?"
9287                   methods nil t nil 'gnus-mail-method-history))
9288                 ms)
9289            (cond
9290             ((zerop (length (setq ms (gnus-servers-using-backend
9291                                       (intern method)))))
9292              (list (intern method) ""))
9293             ((= 1 (length ms))
9294              (car ms))
9295             (t
9296              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9297                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9298                            ms-alist))))))))
9299   (unless method
9300     (error "No method given for respooling"))
9301   (if (assoc (symbol-name
9302               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9303              (gnus-methods-using 'respool))
9304       (gnus-summary-move-article n nil method)
9305     (gnus-summary-copy-article n nil method)))
9306
9307 (defun gnus-summary-import-article (file &optional edit)
9308   "Import an arbitrary file into a mail newsgroup."
9309   (interactive "fImport file: \nP")
9310   (let ((group gnus-newsgroup-name)
9311         (now (current-time))
9312         atts lines group-art)
9313     (unless (gnus-check-backend-function 'request-accept-article group)
9314       (error "%s does not support article importing" group))
9315     (or (file-readable-p file)
9316         (not (file-regular-p file))
9317         (error "Can't read %s" file))
9318     (save-excursion
9319       (set-buffer (gnus-get-buffer-create " *import file*"))
9320       (erase-buffer)
9321       (nnheader-insert-file-contents file)
9322       (goto-char (point-min))
9323       (if (nnheader-article-p)
9324           (save-restriction
9325             (goto-char (point-min))
9326             (search-forward "\n\n" nil t)
9327             (narrow-to-region (point-min) (1- (point)))
9328             (goto-char (point-min))
9329             (unless (re-search-forward "^date:" nil t)
9330               (goto-char (point-max))
9331               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9332         ;; This doesn't look like an article, so we fudge some headers.
9333         (setq atts (file-attributes file)
9334               lines (count-lines (point-min) (point-max)))
9335         (insert "From: " (read-string "From: ") "\n"
9336                 "Subject: " (read-string "Subject: ") "\n"
9337                 "Date: " (message-make-date (nth 5 atts)) "\n"
9338                 "Message-ID: " (message-make-message-id) "\n"
9339                 "Lines: " (int-to-string lines) "\n"
9340                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9341       (setq group-art (gnus-request-accept-article group nil t))
9342       (kill-buffer (current-buffer)))
9343     (setq gnus-newsgroup-active (gnus-activate-group group))
9344     (forward-line 1)
9345     (gnus-summary-goto-article (cdr group-art) nil t)
9346     (when edit
9347       (gnus-summary-edit-article))))
9348
9349 (defun gnus-summary-create-article ()
9350   "Create an article in a mail newsgroup."
9351   (interactive)
9352   (let ((group gnus-newsgroup-name)
9353         (now (current-time))
9354         group-art)
9355     (unless (gnus-check-backend-function 'request-accept-article group)
9356       (error "%s does not support article importing" group))
9357     (save-excursion
9358       (set-buffer (gnus-get-buffer-create " *import file*"))
9359       (erase-buffer)
9360       (goto-char (point-min))
9361       ;; This doesn't look like an article, so we fudge some headers.
9362       (insert "From: " (read-string "From: ") "\n"
9363               "Subject: " (read-string "Subject: ") "\n"
9364               "Date: " (message-make-date now) "\n"
9365               "Message-ID: " (message-make-message-id) "\n")
9366       (setq group-art (gnus-request-accept-article group nil t))
9367       (kill-buffer (current-buffer)))
9368     (setq gnus-newsgroup-active (gnus-activate-group group))
9369     (forward-line 1)
9370     (gnus-summary-goto-article (cdr group-art) nil t)
9371     (gnus-summary-edit-article)))
9372
9373 (defun gnus-summary-article-posted-p ()
9374   "Say whether the current (mail) article is available from news as well.
9375 This will be the case if the article has both been mailed and posted."
9376   (interactive)
9377   (let ((id (mail-header-references (gnus-summary-article-header)))
9378         (gnus-override-method (car (gnus-refer-article-methods))))
9379     (if (gnus-request-head id "")
9380         (gnus-message 2 "The current message was found on %s"
9381                       gnus-override-method)
9382       (gnus-message 2 "The current message couldn't be found on %s"
9383                     gnus-override-method)
9384       nil)))
9385
9386 (defun gnus-summary-expire-articles (&optional now)
9387   "Expire all articles that are marked as expirable in the current group."
9388   (interactive)
9389   (when (and (not gnus-group-is-exiting-without-update-p)
9390              (gnus-check-backend-function
9391               'request-expire-articles gnus-newsgroup-name))
9392     ;; This backend supports expiry.
9393     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9394            (expirable (if total
9395                           (progn
9396                             ;; We need to update the info for
9397                             ;; this group for `gnus-list-of-read-articles'
9398                             ;; to give us the right answer.
9399                             (gnus-run-hooks 'gnus-exit-group-hook)
9400                             (gnus-summary-update-info)
9401                             (gnus-list-of-read-articles gnus-newsgroup-name))
9402                         (setq gnus-newsgroup-expirable
9403                               (sort gnus-newsgroup-expirable '<))))
9404            (expiry-wait (if now 'immediate
9405                           (gnus-group-find-parameter
9406                            gnus-newsgroup-name 'expiry-wait)))
9407            (nnmail-expiry-target
9408             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9409                 nnmail-expiry-target))
9410            es)
9411       (when expirable
9412         ;; There are expirable articles in this group, so we run them
9413         ;; through the expiry process.
9414         (gnus-message 6 "Expiring articles...")
9415         (unless (gnus-check-group gnus-newsgroup-name)
9416           (error "Can't open server for %s" gnus-newsgroup-name))
9417         ;; The list of articles that weren't expired is returned.
9418         (save-excursion
9419           (if expiry-wait
9420               (let ((nnmail-expiry-wait-function nil)
9421                     (nnmail-expiry-wait expiry-wait))
9422                 (setq es (gnus-request-expire-articles
9423                           expirable gnus-newsgroup-name)))
9424             (setq es (gnus-request-expire-articles
9425                       expirable gnus-newsgroup-name)))
9426           (unless total
9427             (setq gnus-newsgroup-expirable es))
9428           ;; We go through the old list of expirable, and mark all
9429           ;; really expired articles as nonexistent.
9430           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9431             (let ((gnus-use-cache nil))
9432               (dolist (article expirable)
9433                 (when (and (not (memq article es))
9434                            (gnus-data-find article))
9435                   (gnus-summary-mark-article article gnus-canceled-mark)
9436                   (run-hook-with-args 'gnus-summary-article-expire-hook
9437                                       'delete
9438                                       (gnus-data-header
9439                                        (assoc article (gnus-data-list nil)))
9440                                       gnus-newsgroup-name
9441                                       nil
9442                                       nil))))))
9443         (gnus-message 6 "Expiring articles...done")))))
9444
9445 (defun gnus-summary-expire-articles-now ()
9446   "Expunge all expirable articles in the current group.
9447 This means that *all* articles that are marked as expirable will be
9448 deleted forever, right now."
9449   (interactive)
9450   (or gnus-expert-user
9451       (gnus-yes-or-no-p
9452        "Are you really, really, really sure you want to delete all these messages? ")
9453       (error "Phew!"))
9454   (gnus-summary-expire-articles t))
9455
9456 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9457 (defun gnus-summary-delete-article (&optional n)
9458   "Delete the N next (mail) articles.
9459 This command actually deletes articles.  This is not a marking
9460 command.  The article will disappear forever from your life, never to
9461 return.
9462
9463 If N is negative, delete backwards.
9464 If N is nil and articles have been marked with the process mark,
9465 delete these instead.
9466
9467 If `gnus-novice-user' is non-nil you will be asked for
9468 confirmation before the articles are deleted."
9469   (interactive "P")
9470   (unless (gnus-check-backend-function 'request-expire-articles
9471                                        gnus-newsgroup-name)
9472     (error "The current newsgroup does not support article deletion"))
9473   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9474     (error "Couldn't open server"))
9475   ;; Compute the list of articles to delete.
9476   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9477         (nnmail-expiry-target 'delete)
9478         not-deleted)
9479     (if (and gnus-novice-user
9480              (not (gnus-yes-or-no-p
9481                    (format "Do you really want to delete %s forever? "
9482                            (if (> (length articles) 1)
9483                                (format "these %s articles" (length articles))
9484                              "this article")))))
9485         ()
9486       ;; Delete the articles.
9487       (setq not-deleted (gnus-request-expire-articles
9488                          articles gnus-newsgroup-name 'force))
9489       (while articles
9490         (gnus-summary-remove-process-mark (car articles))
9491         ;; The backend might not have been able to delete the article
9492         ;; after all.
9493         (unless (memq (car articles) not-deleted)
9494           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9495         (let* ((article (car articles))
9496                (id (mail-header-id (gnus-data-header
9497                                     (assoc article (gnus-data-list nil))))))
9498           (run-hook-with-args 'gnus-summary-article-delete-hook
9499                               'delete id gnus-newsgroup-name nil
9500                               nil))
9501         (setq articles (cdr articles)))
9502       (when not-deleted
9503         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9504     (gnus-summary-position-point)
9505     (gnus-set-mode-line 'summary)
9506     not-deleted))
9507
9508 (defun gnus-summary-edit-article (&optional force)
9509   "Edit the current article.
9510 This will have permanent effect only in mail groups.
9511 If FORCE is non-nil, allow editing of articles even in read-only
9512 groups."
9513   (interactive "P")
9514   (save-excursion
9515     (set-buffer gnus-summary-buffer)
9516     (let ((mail-parse-charset gnus-newsgroup-charset)
9517           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9518       (gnus-set-global-variables)
9519       (when (and (not force)
9520                  (gnus-group-read-only-p))
9521         (error "The current newsgroup does not support article editing"))
9522       (gnus-summary-show-article t)
9523       (gnus-article-edit-article
9524        'ignore
9525        `(lambda (no-highlight)
9526           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9527                 (message-options message-options)
9528                 (message-options-set-recipient)
9529                 (mail-parse-ignored-charsets
9530                  ',gnus-newsgroup-ignored-charsets))
9531             (gnus-summary-edit-article-done
9532              ,(or (mail-header-references gnus-current-headers) "")
9533              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9534
9535 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9536
9537 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9538                                                  no-highlight)
9539   "Make edits to the current article permanent."
9540   (interactive)
9541   (save-excursion
9542     ;; The buffer restriction contains the entire article if it exists.
9543     (when (article-goto-body)
9544       (let ((lines (count-lines (point) (point-max)))
9545             (length (- (point-max) (point)))
9546             (case-fold-search t)
9547             (body (copy-marker (point))))
9548         (goto-char (point-min))
9549         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9550           (delete-region (match-beginning 1) (match-end 1))
9551           (insert (number-to-string length)))
9552         (goto-char (point-min))
9553         (when (re-search-forward
9554                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9555           (delete-region (match-beginning 1) (match-end 1))
9556           (insert (number-to-string length)))
9557         (goto-char (point-min))
9558         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9559           (delete-region (match-beginning 1) (match-end 1))
9560           (insert (number-to-string lines))))))
9561   ;; Replace the article.
9562   (let ((buf (current-buffer)))
9563     (with-temp-buffer
9564       (insert-buffer-substring buf)
9565
9566       (if (and (not read-only)
9567                (not (gnus-request-replace-article
9568                      (cdr gnus-article-current) (car gnus-article-current)
9569                      (current-buffer) t)))
9570           (error "Couldn't replace article")
9571         ;; Update the summary buffer.
9572         (if (and references
9573                  (equal (message-tokenize-header references " ")
9574                         (message-tokenize-header
9575                          (or (message-fetch-field "references") "") " ")))
9576             ;; We only have to update this line.
9577             (save-excursion
9578               (save-restriction
9579                 (message-narrow-to-head)
9580                 (let ((head (buffer-string))
9581                       header)
9582                   (with-temp-buffer
9583                     (insert (format "211 %d Article retrieved.\n"
9584                                     (cdr gnus-article-current)))
9585                     (insert head)
9586                     (insert ".\n")
9587                     (let ((nntp-server-buffer (current-buffer)))
9588                       (setq header (car (gnus-get-newsgroup-headers
9589                                          nil t))))
9590                     (save-excursion
9591                       (set-buffer gnus-summary-buffer)
9592                       (gnus-data-set-header
9593                        (gnus-data-find (cdr gnus-article-current))
9594                        header)
9595                       (gnus-summary-update-article-line
9596                        (cdr gnus-article-current) header)
9597                       (if (gnus-summary-goto-subject
9598                            (cdr gnus-article-current) nil t)
9599                           (gnus-summary-update-secondary-mark
9600                            (cdr gnus-article-current))))))))
9601           ;; Update threads.
9602           (set-buffer (or buffer gnus-summary-buffer))
9603           (gnus-summary-update-article (cdr gnus-article-current))
9604           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9605               (gnus-summary-update-secondary-mark
9606                (cdr gnus-article-current))))
9607         ;; Prettify the article buffer again.
9608         (unless no-highlight
9609           (save-excursion
9610             (set-buffer gnus-article-buffer)
9611             ;;;!!! Fix this -- article should be rehighlighted.
9612             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9613             (set-buffer gnus-original-article-buffer)
9614             (gnus-request-article
9615              (cdr gnus-article-current)
9616              (car gnus-article-current) (current-buffer))))
9617         ;; Prettify the summary buffer line.
9618         (when (gnus-visual-p 'summary-highlight 'highlight)
9619           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9620
9621 (defun gnus-summary-edit-wash (key)
9622   "Perform editing command KEY in the article buffer."
9623   (interactive
9624    (list
9625     (progn
9626       (message "%s" (concat (this-command-keys) "- "))
9627       (read-char))))
9628   (message "")
9629   (gnus-summary-edit-article)
9630   (execute-kbd-macro (concat (this-command-keys) key))
9631   (gnus-article-edit-done))
9632
9633 ;;; Respooling
9634
9635 (defun gnus-summary-respool-query (&optional silent trace)
9636   "Query where the respool algorithm would put this article."
9637   (interactive)
9638   (let (gnus-mark-article-hook)
9639     (gnus-summary-select-article)
9640     (save-excursion
9641       (set-buffer gnus-original-article-buffer)
9642       (let ((groups (nnmail-article-group 'identity trace)))
9643         (unless silent
9644           (if groups
9645               (message "This message would go to %s"
9646                        (mapconcat 'car groups ", "))
9647             (message "This message would go to no groups"))
9648           groups)))))
9649
9650 (defun gnus-summary-respool-trace ()
9651   "Trace where the respool algorithm would put this article.
9652 Display a buffer showing all fancy splitting patterns which matched."
9653   (interactive)
9654   (gnus-summary-respool-query nil t))
9655
9656 ;; Summary marking commands.
9657
9658 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9659   "Mark articles which has the same subject as read, and then select the next.
9660 If UNMARK is positive, remove any kind of mark.
9661 If UNMARK is negative, tick articles."
9662   (interactive "P")
9663   (when unmark
9664     (setq unmark (prefix-numeric-value unmark)))
9665   (let ((count
9666          (gnus-summary-mark-same-subject
9667           (gnus-summary-article-subject) unmark)))
9668     ;; Select next unread article.  If auto-select-same mode, should
9669     ;; select the first unread article.
9670     (gnus-summary-next-article t (and gnus-auto-select-same
9671                                       (gnus-summary-article-subject)))
9672     (gnus-message 7 "%d article%s marked as %s"
9673                   count (if (= count 1) " is" "s are")
9674                   (if unmark "unread" "read"))))
9675
9676 (defun gnus-summary-kill-same-subject (&optional unmark)
9677   "Mark articles which has the same subject as read.
9678 If UNMARK is positive, remove any kind of mark.
9679 If UNMARK is negative, tick articles."
9680   (interactive "P")
9681   (when unmark
9682     (setq unmark (prefix-numeric-value unmark)))
9683   (let ((count
9684          (gnus-summary-mark-same-subject
9685           (gnus-summary-article-subject) unmark)))
9686     ;; If marked as read, go to next unread subject.
9687     (when (null unmark)
9688       ;; Go to next unread subject.
9689       (gnus-summary-next-subject 1 t))
9690     (gnus-message 7 "%d articles are marked as %s"
9691                   count (if unmark "unread" "read"))))
9692
9693 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9694   "Mark articles with same SUBJECT as read, and return marked number.
9695 If optional argument UNMARK is positive, remove any kinds of marks.
9696 If optional argument UNMARK is negative, mark articles as unread instead."
9697   (let ((count 1))
9698     (save-excursion
9699       (cond
9700        ((null unmark)                   ; Mark as read.
9701         (while (and
9702                 (progn
9703                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9704                   (gnus-summary-show-thread) t)
9705                 (gnus-summary-find-subject subject))
9706           (setq count (1+ count))))
9707        ((> unmark 0)                    ; Tick.
9708         (while (and
9709                 (progn
9710                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9711                   (gnus-summary-show-thread) t)
9712                 (gnus-summary-find-subject subject))
9713           (setq count (1+ count))))
9714        (t                               ; Mark as unread.
9715         (while (and
9716                 (progn
9717                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9718                   (gnus-summary-show-thread) t)
9719                 (gnus-summary-find-subject subject))
9720           (setq count (1+ count)))))
9721       (gnus-set-mode-line 'summary)
9722       ;; Return the number of marked articles.
9723       count)))
9724
9725 (defun gnus-summary-mark-as-processable (n &optional unmark)
9726   "Set the process mark on the next N articles.
9727 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9728 the process mark instead.  The difference between N and the actual
9729 number of articles marked is returned."
9730   (interactive "P")
9731   (if (and (null n) (gnus-region-active-p))
9732       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9733     (setq n (prefix-numeric-value n))
9734     (let ((backward (< n 0))
9735           (n (abs n)))
9736       (while (and
9737               (> n 0)
9738               (if unmark
9739                   (gnus-summary-remove-process-mark
9740                    (gnus-summary-article-number))
9741                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9742               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9743         (setq n (1- n)))
9744       (when (/= 0 n)
9745         (gnus-message 7 "No more articles"))
9746       (gnus-summary-recenter)
9747       (gnus-summary-position-point)
9748       n)))
9749
9750 (defun gnus-summary-unmark-as-processable (n)
9751   "Remove the process mark from the next N articles.
9752 If N is negative, unmark backward instead.  The difference between N and
9753 the actual number of articles unmarked is returned."
9754   (interactive "P")
9755   (gnus-summary-mark-as-processable n t))
9756
9757 (defun gnus-summary-unmark-all-processable ()
9758   "Remove the process mark from all articles."
9759   (interactive)
9760   (save-excursion
9761     (while gnus-newsgroup-processable
9762       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9763   (gnus-summary-position-point))
9764
9765 (defun gnus-summary-add-mark (article type)
9766   "Mark ARTICLE with a mark of TYPE."
9767   (let ((vtype (car (assq type gnus-article-mark-lists)))
9768         var)
9769     (if (not vtype)
9770         (error "No such mark type: %s" type)
9771       (setq var (intern (format "gnus-newsgroup-%s" type)))
9772       (set var (cons article (symbol-value var)))
9773       (if (memq type '(processable cached replied forwarded recent saved))
9774           (gnus-summary-update-secondary-mark article)
9775         ;;; !!! This is bogus.  We should find out what primary
9776         ;;; !!! mark we want to set.
9777         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9778
9779 (defun gnus-summary-mark-as-expirable (n)
9780   "Mark N articles forward as expirable.
9781 If N is negative, mark backward instead.  The difference between N and
9782 the actual number of articles marked is returned."
9783   (interactive "p")
9784   (gnus-summary-mark-forward n gnus-expirable-mark))
9785
9786 (defun gnus-summary-mark-as-spam (n)
9787   "Mark N articles forward as spam.
9788 If N is negative, mark backward instead.  The difference between N and
9789 the actual number of articles marked is returned."
9790   (interactive "p")
9791   (gnus-summary-mark-forward n gnus-spam-mark))
9792
9793 (defun gnus-summary-mark-article-as-replied (article)
9794   "Mark ARTICLE as replied to and update the summary line.
9795 ARTICLE can also be a list of articles."
9796   (interactive (list (gnus-summary-article-number)))
9797   (let ((articles (if (listp article) article (list article))))
9798     (dolist (article articles)
9799       (unless (numberp article)
9800         (error "%s is not a number" article))
9801       (push article gnus-newsgroup-replied)
9802       (let ((buffer-read-only nil))
9803         (when (gnus-summary-goto-subject article nil t)
9804           (gnus-summary-update-secondary-mark article))))))
9805
9806 (defun gnus-summary-mark-article-as-forwarded (article)
9807   "Mark ARTICLE as forwarded and update the summary line.
9808 ARTICLE can also be a list of articles."
9809   (let ((articles (if (listp article) article (list article))))
9810     (dolist (article articles)
9811       (push article gnus-newsgroup-forwarded)
9812       (let ((buffer-read-only nil))
9813         (when (gnus-summary-goto-subject article nil t)
9814           (gnus-summary-update-secondary-mark article))))))
9815
9816 (defun gnus-summary-set-bookmark (article)
9817   "Set a bookmark in current article."
9818   (interactive (list (gnus-summary-article-number)))
9819   (when (or (not (get-buffer gnus-article-buffer))
9820             (not gnus-current-article)
9821             (not gnus-article-current)
9822             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9823     (error "No current article selected"))
9824   ;; Remove old bookmark, if one exists.
9825   (gnus-pull article gnus-newsgroup-bookmarks)
9826   ;; Set the new bookmark, which is on the form
9827   ;; (article-number . line-number-in-body).
9828   (push
9829    (cons article
9830          (save-excursion
9831            (set-buffer gnus-article-buffer)
9832            (count-lines
9833             (min (point)
9834                  (save-excursion
9835                    (article-goto-body)
9836                    (point)))
9837             (point))))
9838    gnus-newsgroup-bookmarks)
9839   (gnus-message 6 "A bookmark has been added to the current article."))
9840
9841 (defun gnus-summary-remove-bookmark (article)
9842   "Remove the bookmark from the current article."
9843   (interactive (list (gnus-summary-article-number)))
9844   ;; Remove old bookmark, if one exists.
9845   (if (not (assq article gnus-newsgroup-bookmarks))
9846       (gnus-message 6 "No bookmark in current article.")
9847     (gnus-pull article gnus-newsgroup-bookmarks)
9848     (gnus-message 6 "Removed bookmark.")))
9849
9850 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9851 (defun gnus-summary-mark-as-dormant (n)
9852   "Mark N articles forward as dormant.
9853 If N is negative, mark backward instead.  The difference between N and
9854 the actual number of articles marked is returned."
9855   (interactive "p")
9856   (gnus-summary-mark-forward n gnus-dormant-mark))
9857
9858 (defun gnus-summary-set-process-mark (article)
9859   "Set the process mark on ARTICLE and update the summary line."
9860   (setq gnus-newsgroup-processable
9861         (cons article
9862               (delq article gnus-newsgroup-processable)))
9863   (when (gnus-summary-goto-subject article)
9864     (gnus-summary-show-thread)
9865     (gnus-summary-goto-subject article)
9866     (gnus-summary-update-secondary-mark article)))
9867
9868 (defun gnus-summary-remove-process-mark (article)
9869   "Remove the process mark from ARTICLE and update the summary line."
9870   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9871   (when (gnus-summary-goto-subject article)
9872     (gnus-summary-show-thread)
9873     (gnus-summary-goto-subject article)
9874     (gnus-summary-update-secondary-mark article)))
9875
9876 (defun gnus-summary-set-saved-mark (article)
9877   "Set the process mark on ARTICLE and update the summary line."
9878   (push article gnus-newsgroup-saved)
9879   (when (gnus-summary-goto-subject article)
9880     (gnus-summary-update-secondary-mark article)))
9881
9882 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9883   "Mark N articles as read forwards.
9884 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9885 The difference between N and the actual number of articles marked is
9886 returned.
9887 If NO-EXPIRE, auto-expiry will be inhibited."
9888   (interactive "p")
9889   (gnus-summary-show-thread)
9890   (let ((backward (< n 0))
9891         (gnus-summary-goto-unread
9892          (and gnus-summary-goto-unread
9893               (not (eq gnus-summary-goto-unread 'never))
9894               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9895                                     gnus-ticked-mark gnus-dormant-mark)))))
9896         (n (abs n))
9897         (mark (or mark gnus-del-mark)))
9898     (while (and (> n 0)
9899                 (gnus-summary-mark-article nil mark no-expire)
9900                 (zerop (gnus-summary-next-subject
9901                         (if backward -1 1)
9902                         (and gnus-summary-goto-unread
9903                              (not (eq gnus-summary-goto-unread 'never)))
9904                         t)))
9905       (setq n (1- n)))
9906     (when (/= 0 n)
9907       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9908     (gnus-summary-recenter)
9909     (gnus-summary-position-point)
9910     (gnus-set-mode-line 'summary)
9911     n))
9912
9913 (defun gnus-summary-mark-article-as-read (mark)
9914   "Mark the current article quickly as read with MARK."
9915   (let ((article (gnus-summary-article-number)))
9916     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9917     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9918     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9919     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9920     (push (cons article mark) gnus-newsgroup-reads)
9921     ;; Possibly remove from cache, if that is used.
9922     (when gnus-use-cache
9923       (gnus-cache-enter-remove-article article))
9924     ;; Allow the backend to change the mark.
9925     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9926     ;; Check for auto-expiry.
9927     (when (and gnus-newsgroup-auto-expire
9928                (memq mark gnus-auto-expirable-marks))
9929       (setq mark gnus-expirable-mark)
9930       ;; Let the backend know about the mark change.
9931       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9932       (push article gnus-newsgroup-expirable))
9933     ;; Set the mark in the buffer.
9934     (gnus-summary-update-mark mark 'unread)
9935     t))
9936
9937 (defun gnus-summary-mark-article-as-unread (mark)
9938   "Mark the current article quickly as unread with MARK."
9939   (let* ((article (gnus-summary-article-number))
9940          (old-mark (gnus-summary-article-mark article)))
9941     ;; Allow the backend to change the mark.
9942     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9943     (if (eq mark old-mark)
9944         t
9945       (if (<= article 0)
9946           (progn
9947             (gnus-error 1 "Can't mark negative article numbers")
9948             nil)
9949         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9950         (setq gnus-newsgroup-spam-marked
9951               (delq article gnus-newsgroup-spam-marked))
9952         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9953         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9954         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9955         (cond ((= mark gnus-ticked-mark)
9956                (setq gnus-newsgroup-marked
9957                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9958                                               article)))
9959               ((= mark gnus-spam-mark)
9960                (setq gnus-newsgroup-spam-marked
9961                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9962                                               article)))
9963               ((= mark gnus-dormant-mark)
9964                (setq gnus-newsgroup-dormant
9965                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9966                                               article)))
9967               (t
9968                (setq gnus-newsgroup-unreads
9969                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9970                                               article))))
9971         (gnus-pull article gnus-newsgroup-reads)
9972
9973         ;; See whether the article is to be put in the cache.
9974         (and gnus-use-cache
9975              (vectorp (gnus-summary-article-header article))
9976              (save-excursion
9977                (gnus-cache-possibly-enter-article
9978                 gnus-newsgroup-name article
9979                 (gnus-summary-article-header article)
9980                 (= mark gnus-ticked-mark)
9981                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9982
9983         ;; Fix the mark.
9984         (gnus-summary-update-mark mark 'unread)
9985         t))))
9986
9987 (defun gnus-summary-mark-article (&optional article mark no-expire)
9988   "Mark ARTICLE with MARK.  MARK can be any character.
9989 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9990 `??' (dormant) and `?E' (expirable).
9991 If MARK is nil, then the default character `?r' is used.
9992 If ARTICLE is nil, then the article on the current line will be
9993 marked.
9994 If NO-EXPIRE, auto-expiry will be inhibited."
9995   ;; The mark might be a string.
9996   (when (stringp mark)
9997     (setq mark (aref mark 0)))
9998   ;; If no mark is given, then we check auto-expiring.
9999   (when (null mark)
10000     (setq mark gnus-del-mark))
10001   (when (and (not no-expire)
10002              gnus-newsgroup-auto-expire
10003              (memq mark gnus-auto-expirable-marks))
10004     (setq mark gnus-expirable-mark))
10005   (let ((article (or article (gnus-summary-article-number)))
10006         (old-mark (gnus-summary-article-mark article)))
10007     ;; Allow the backend to change the mark.
10008     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10009     (if (eq mark old-mark)
10010         t
10011       (unless article
10012         (error "No article on current line"))
10013       (if (not (if (or (= mark gnus-unread-mark)
10014                        (= mark gnus-ticked-mark)
10015                        (= mark gnus-spam-mark)
10016                        (= mark gnus-dormant-mark))
10017                    (gnus-mark-article-as-unread article mark)
10018                  (gnus-mark-article-as-read article mark)))
10019           t
10020         ;; See whether the article is to be put in the cache.
10021         (and gnus-use-cache
10022              (not (= mark gnus-canceled-mark))
10023              (vectorp (gnus-summary-article-header article))
10024              (save-excursion
10025                (gnus-cache-possibly-enter-article
10026                 gnus-newsgroup-name article
10027                 (gnus-summary-article-header article)
10028                 (= mark gnus-ticked-mark)
10029                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10030
10031         (when (gnus-summary-goto-subject article nil t)
10032           (let ((buffer-read-only nil))
10033             (gnus-summary-show-thread)
10034             ;; Fix the mark.
10035             (gnus-summary-update-mark mark 'unread)
10036             t))))))
10037
10038 (defun gnus-summary-update-secondary-mark (article)
10039   "Update the secondary (read, process, cache) mark."
10040   (gnus-summary-update-mark
10041    (cond ((memq article gnus-newsgroup-processable)
10042           gnus-process-mark)
10043          ((memq article gnus-newsgroup-cached)
10044           gnus-cached-mark)
10045          ((memq article gnus-newsgroup-replied)
10046           gnus-replied-mark)
10047          ((memq article gnus-newsgroup-forwarded)
10048           gnus-forwarded-mark)
10049          ((memq article gnus-newsgroup-saved)
10050           gnus-saved-mark)
10051          ((memq article gnus-newsgroup-recent)
10052           gnus-recent-mark)
10053          ((memq article gnus-newsgroup-unseen)
10054           gnus-unseen-mark)
10055          (t gnus-no-mark))
10056    'replied)
10057   (when (gnus-visual-p 'summary-highlight 'highlight)
10058     (gnus-run-hooks 'gnus-summary-update-hook))
10059   t)
10060
10061 (defun gnus-summary-update-download-mark (article)
10062   "Update the download mark."
10063   (gnus-summary-update-mark
10064    (cond ((memq article gnus-newsgroup-undownloaded)
10065           gnus-undownloaded-mark)
10066          (gnus-newsgroup-agentized
10067           gnus-downloaded-mark)
10068          (t
10069           gnus-no-mark))
10070    'download)
10071   (gnus-summary-update-line t)
10072   t)
10073
10074 (defun gnus-summary-update-mark (mark type)
10075   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10076         (buffer-read-only nil))
10077     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10078     (when forward
10079       (when (looking-at "\r")
10080         (incf forward))
10081       (when (<= (+ forward (point)) (point-max))
10082         ;; Go to the right position on the line.
10083         (goto-char (+ forward (point)))
10084         ;; Replace the old mark with the new mark.
10085         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10086         ;; Optionally update the marks by some user rule.
10087         (when (eq type 'unread)
10088           (gnus-data-set-mark
10089            (gnus-data-find (gnus-summary-article-number)) mark)
10090           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10091
10092 (defun gnus-mark-article-as-read (article &optional mark)
10093   "Enter ARTICLE in the pertinent lists and remove it from others."
10094   ;; Make the article expirable.
10095   (let ((mark (or mark gnus-del-mark)))
10096     (setq gnus-newsgroup-expirable
10097           (if (= mark gnus-expirable-mark)
10098               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10099             (delq article gnus-newsgroup-expirable)))
10100     ;; Remove from unread and marked lists.
10101     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10102     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10103     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10104     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10105     (push (cons article mark) gnus-newsgroup-reads)
10106     ;; Possibly remove from cache, if that is used.
10107     (when gnus-use-cache
10108       (gnus-cache-enter-remove-article article))
10109     t))
10110
10111 (defun gnus-mark-article-as-unread (article &optional mark)
10112   "Enter ARTICLE in the pertinent lists and remove it from others."
10113   (let ((mark (or mark gnus-ticked-mark)))
10114     (if (<= article 0)
10115         (progn
10116           (gnus-error 1 "Can't mark negative article numbers")
10117           nil)
10118       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10119             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10120             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10121             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10122             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10123
10124       ;; Unsuppress duplicates?
10125       (when gnus-suppress-duplicates
10126         (gnus-dup-unsuppress-article article))
10127
10128       (cond ((= mark gnus-ticked-mark)
10129              (setq gnus-newsgroup-marked
10130                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10131             ((= mark gnus-spam-mark)
10132              (setq gnus-newsgroup-spam-marked
10133                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10134                                             article)))
10135             ((= mark gnus-dormant-mark)
10136              (setq gnus-newsgroup-dormant
10137                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10138             (t
10139              (setq gnus-newsgroup-unreads
10140                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10141       (gnus-pull article gnus-newsgroup-reads)
10142       t)))
10143
10144 (defalias 'gnus-summary-mark-as-unread-forward
10145   'gnus-summary-tick-article-forward)
10146 (make-obsolete 'gnus-summary-mark-as-unread-forward
10147                'gnus-summary-tick-article-forward)
10148 (defun gnus-summary-tick-article-forward (n)
10149   "Tick N articles forwards.
10150 If N is negative, tick backwards instead.
10151 The difference between N and the number of articles ticked is returned."
10152   (interactive "p")
10153   (gnus-summary-mark-forward n gnus-ticked-mark))
10154
10155 (defalias 'gnus-summary-mark-as-unread-backward
10156   'gnus-summary-tick-article-backward)
10157 (make-obsolete 'gnus-summary-mark-as-unread-backward
10158                'gnus-summary-tick-article-backward)
10159 (defun gnus-summary-tick-article-backward (n)
10160   "Tick N articles backwards.
10161 The difference between N and the number of articles ticked is returned."
10162   (interactive "p")
10163   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10164
10165 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10166 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10167 (defun gnus-summary-tick-article (&optional article clear-mark)
10168   "Mark current article as unread.
10169 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10170 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10171   (interactive)
10172   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10173                                        gnus-ticked-mark)))
10174
10175 (defun gnus-summary-mark-as-read-forward (n)
10176   "Mark N articles as read forwards.
10177 If N is negative, mark backwards instead.
10178 The difference between N and the actual number of articles marked is
10179 returned."
10180   (interactive "p")
10181   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10182
10183 (defun gnus-summary-mark-as-read-backward (n)
10184   "Mark the N articles as read backwards.
10185 The difference between N and the actual number of articles marked is
10186 returned."
10187   (interactive "p")
10188   (gnus-summary-mark-forward
10189    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10190
10191 (defun gnus-summary-mark-as-read (&optional article mark)
10192   "Mark current article as read.
10193 ARTICLE specifies the article to be marked as read.
10194 MARK specifies a string to be inserted at the beginning of the line."
10195   (gnus-summary-mark-article article mark))
10196
10197 (defun gnus-summary-clear-mark-forward (n)
10198   "Clear marks from N articles forward.
10199 If N is negative, clear backward instead.
10200 The difference between N and the number of marks cleared is returned."
10201   (interactive "p")
10202   (gnus-summary-mark-forward n gnus-unread-mark))
10203
10204 (defun gnus-summary-clear-mark-backward (n)
10205   "Clear marks from N articles backward.
10206 The difference between N and the number of marks cleared is returned."
10207   (interactive "p")
10208   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10209
10210 (defun gnus-summary-mark-unread-as-read ()
10211   "Intended to be used by `gnus-summary-mark-article-hook'."
10212   (when (memq gnus-current-article gnus-newsgroup-unreads)
10213     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10214
10215 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10216   "Intended to be used by `gnus-summary-mark-article-hook'."
10217   (let ((mark (gnus-summary-article-mark)))
10218     (when (or (gnus-unread-mark-p mark)
10219               (gnus-read-mark-p mark))
10220       (gnus-summary-mark-article gnus-current-article
10221                                  (or new-mark gnus-read-mark)))))
10222
10223 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10224   "Intended to be used by `gnus-summary-mark-article-hook'."
10225   (let ((mark (gnus-summary-article-mark)))
10226     (when (or (gnus-unread-mark-p mark)
10227               (gnus-read-mark-p mark))
10228       (gnus-summary-mark-article (gnus-summary-article-number)
10229                                  (or new-mark gnus-read-mark)))))
10230
10231 (defun gnus-summary-mark-unread-as-ticked ()
10232   "Intended to be used by `gnus-summary-mark-article-hook'."
10233   (when (memq gnus-current-article gnus-newsgroup-unreads)
10234     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10235
10236 (defun gnus-summary-mark-region-as-read (point mark all)
10237   "Mark all unread articles between point and mark as read.
10238 If given a prefix, mark all articles between point and mark as read,
10239 even ticked and dormant ones."
10240   (interactive "r\nP")
10241   (save-excursion
10242     (let (article)
10243       (goto-char point)
10244       (beginning-of-line)
10245       (while (and
10246               (< (point) mark)
10247               (progn
10248                 (when (or all
10249                           (memq (setq article (gnus-summary-article-number))
10250                                 gnus-newsgroup-unreads))
10251                   (gnus-summary-mark-article article gnus-del-mark))
10252                 t)
10253               (gnus-summary-find-next))))))
10254
10255 (defun gnus-summary-mark-below (score mark)
10256   "Mark articles with score less than SCORE with MARK."
10257   (interactive "P\ncMark: ")
10258   (setq score (if score
10259                   (prefix-numeric-value score)
10260                 (or gnus-summary-default-score 0)))
10261   (save-excursion
10262     (set-buffer gnus-summary-buffer)
10263     (goto-char (point-min))
10264     (while
10265         (progn
10266           (and (< (gnus-summary-article-score) score)
10267                (gnus-summary-mark-article nil mark))
10268           (gnus-summary-find-next)))))
10269
10270 (defun gnus-summary-kill-below (&optional score)
10271   "Mark articles with score below SCORE as read."
10272   (interactive "P")
10273   (gnus-summary-mark-below score gnus-killed-mark))
10274
10275 (defun gnus-summary-clear-above (&optional score)
10276   "Clear all marks from articles with score above SCORE."
10277   (interactive "P")
10278   (gnus-summary-mark-above score gnus-unread-mark))
10279
10280 (defun gnus-summary-tick-above (&optional score)
10281   "Tick all articles with score above SCORE."
10282   (interactive "P")
10283   (gnus-summary-mark-above score gnus-ticked-mark))
10284
10285 (defun gnus-summary-mark-above (score mark)
10286   "Mark articles with score over SCORE with MARK."
10287   (interactive "P\ncMark: ")
10288   (setq score (if score
10289                   (prefix-numeric-value score)
10290                 (or gnus-summary-default-score 0)))
10291   (save-excursion
10292     (set-buffer gnus-summary-buffer)
10293     (goto-char (point-min))
10294     (while (and (progn
10295                   (when (> (gnus-summary-article-score) score)
10296                     (gnus-summary-mark-article nil mark))
10297                   t)
10298                 (gnus-summary-find-next)))))
10299
10300 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10301 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10302 (defun gnus-summary-limit-include-expunged (&optional no-error)
10303   "Display all the hidden articles that were expunged for low scores."
10304   (interactive)
10305   (let ((buffer-read-only nil))
10306     (let ((scored gnus-newsgroup-scored)
10307           headers h)
10308       (while scored
10309         (unless (gnus-summary-article-header (caar scored))
10310           (and (setq h (gnus-number-to-header (caar scored)))
10311                (< (cdar scored) gnus-summary-expunge-below)
10312                (push h headers)))
10313         (setq scored (cdr scored)))
10314       (if (not headers)
10315           (when (not no-error)
10316             (error "No expunged articles hidden"))
10317         (goto-char (point-min))
10318         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10319         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10320         (mapcar (lambda (x) (push (mail-header-number x)
10321                                   gnus-newsgroup-limit))
10322                 headers)
10323         (gnus-summary-prepare-unthreaded (nreverse headers))
10324         (goto-char (point-min))
10325         (gnus-summary-position-point)
10326         t))))
10327
10328 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10329   "Mark all unread articles in this newsgroup as read.
10330 If prefix argument ALL is non-nil, ticked and dormant articles will
10331 also be marked as read.
10332 If QUIETLY is non-nil, no questions will be asked.
10333
10334 If TO-HERE is non-nil, it should be a point in the buffer.  All
10335 articles before (after, if REVERSE is set) this point will be marked
10336 as read.
10337
10338 Note that this function will only catch up the unread article
10339 in the current summary buffer limitation.
10340
10341 The number of articles marked as read is returned."
10342   (interactive "P")
10343   (prog1
10344       (save-excursion
10345         (when (or quietly
10346                   (not gnus-interactive-catchup) ;Without confirmation?
10347                   gnus-expert-user
10348                   (gnus-y-or-n-p
10349                    (if all
10350                        "Mark absolutely all articles as read? "
10351                      "Mark all unread articles as read? ")))
10352           (if (and not-mark
10353                    (not gnus-newsgroup-adaptive)
10354                    (not gnus-newsgroup-auto-expire)
10355                    (not gnus-suppress-duplicates)
10356                    (or (not gnus-use-cache)
10357                        (eq gnus-use-cache 'passive)))
10358               (progn
10359                 (when all
10360                   (setq gnus-newsgroup-marked nil
10361                         gnus-newsgroup-spam-marked nil
10362                         gnus-newsgroup-dormant nil))
10363                 (setq gnus-newsgroup-unreads
10364                       (gnus-sorted-nunion
10365                        (gnus-intersection gnus-newsgroup-unreads
10366                                           gnus-newsgroup-downloadable)
10367                        gnus-newsgroup-unfetched)))
10368             ;; We actually mark all articles as canceled, which we
10369             ;; have to do when using auto-expiry or adaptive scoring.
10370             (gnus-summary-show-all-threads)
10371             (if (and to-here reverse)
10372                 (progn
10373                   (goto-char to-here)
10374                   (gnus-summary-mark-current-read-and-unread-as-read
10375                    gnus-catchup-mark)
10376                   (while (gnus-summary-find-next (not all))
10377                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10378               (when (gnus-summary-first-subject (not all))
10379                 (while (and
10380                         (if to-here (< (point) to-here) t)
10381                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10382                         (gnus-summary-find-next (not all))))))
10383             (gnus-set-mode-line 'summary))
10384           t))
10385     (gnus-summary-position-point)))
10386
10387 (defun gnus-summary-catchup-to-here (&optional all)
10388   "Mark all unticked articles before the current one as read.
10389 If ALL is non-nil, also mark ticked and dormant articles as read."
10390   (interactive "P")
10391   (save-excursion
10392     (gnus-save-hidden-threads
10393       (let ((beg (point)))
10394         ;; We check that there are unread articles.
10395         (when (or all (gnus-summary-find-prev))
10396           (gnus-summary-catchup all t beg)))))
10397   (gnus-summary-position-point))
10398
10399 (defun gnus-summary-catchup-from-here (&optional all)
10400   "Mark all unticked articles after (and including) the current one as read.
10401 If ALL is non-nil, also mark ticked and dormant articles as read."
10402   (interactive "P")
10403   (save-excursion
10404     (gnus-save-hidden-threads
10405       (let ((beg (point)))
10406         ;; We check that there are unread articles.
10407         (when (or all (gnus-summary-find-next))
10408           (gnus-summary-catchup all t beg nil t)))))
10409   (gnus-summary-position-point))
10410
10411 (defun gnus-summary-catchup-all (&optional quietly)
10412   "Mark all articles in this newsgroup as read.
10413 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10414 instead, which marks only unread articles as read."
10415   (interactive "P")
10416   (gnus-summary-catchup t quietly))
10417
10418 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10419   "Mark all unread articles in this group as read, then exit.
10420 If prefix argument ALL is non-nil, all articles are marked as read.
10421 If QUIETLY is non-nil, no questions will be asked."
10422   (interactive "P")
10423   (when (gnus-summary-catchup all quietly nil 'fast)
10424     ;; Select next newsgroup or exit.
10425     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10426              (eq gnus-auto-select-next 'quietly))
10427         (gnus-summary-next-group nil)
10428       (gnus-summary-exit))))
10429
10430 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10431   "Mark all articles in this newsgroup as read, and then exit.
10432 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10433 instead, which marks only unread articles as read."
10434   (interactive "P")
10435   (gnus-summary-catchup-and-exit t quietly))
10436
10437 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10438   "Mark all articles in this group as read and select the next group.
10439 If given a prefix, mark all articles, unread as well as ticked, as
10440 read."
10441   (interactive "P")
10442   (save-excursion
10443     (gnus-summary-catchup all))
10444   (gnus-summary-next-group))
10445
10446 ;;;
10447 ;;; with article
10448 ;;;
10449
10450 (defmacro gnus-with-article (article &rest forms)
10451   "Select ARTICLE and perform FORMS in the original article buffer.
10452 Then replace the article with the result."
10453   `(progn
10454      ;; We don't want the article to be marked as read.
10455      (let (gnus-mark-article-hook)
10456        (gnus-summary-select-article t t nil ,article))
10457      (set-buffer gnus-original-article-buffer)
10458      ,@forms
10459      (if (not (gnus-check-backend-function
10460                'request-replace-article (car gnus-article-current)))
10461          (gnus-message 5 "Read-only group; not replacing")
10462        (unless (gnus-request-replace-article
10463                 ,article (car gnus-article-current)
10464                 (current-buffer) t)
10465          (error "Couldn't replace article")))
10466      ;; The cache and backlog have to be flushed somewhat.
10467      (when gnus-keep-backlog
10468        (gnus-backlog-remove-article
10469         (car gnus-article-current) (cdr gnus-article-current)))
10470      (when gnus-use-cache
10471        (gnus-cache-update-article
10472         (car gnus-article-current) (cdr gnus-article-current)))))
10473
10474 (put 'gnus-with-article 'lisp-indent-function 1)
10475 (put 'gnus-with-article 'edebug-form-spec '(form body))
10476
10477 ;; Thread-based commands.
10478
10479 (defun gnus-summary-articles-in-thread (&optional article)
10480   "Return a list of all articles in the current thread.
10481 If ARTICLE is non-nil, return all articles in the thread that starts
10482 with that article."
10483   (let* ((article (or article (gnus-summary-article-number)))
10484          (data (gnus-data-find-list article))
10485          (top-level (gnus-data-level (car data)))
10486          (top-subject
10487           (cond ((null gnus-thread-operation-ignore-subject)
10488                  (gnus-simplify-subject-re
10489                   (mail-header-subject (gnus-data-header (car data)))))
10490                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10491                  (gnus-simplify-subject-fuzzy
10492                   (mail-header-subject (gnus-data-header (car data)))))
10493                 (t nil)))
10494          (end-point (save-excursion
10495                       (if (gnus-summary-go-to-next-thread)
10496                           (point) (point-max))))
10497          articles)
10498     (while (and data
10499                 (< (gnus-data-pos (car data)) end-point))
10500       (when (or (not top-subject)
10501                 (string= top-subject
10502                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10503                              (gnus-simplify-subject-fuzzy
10504                               (mail-header-subject
10505                                (gnus-data-header (car data))))
10506                            (gnus-simplify-subject-re
10507                             (mail-header-subject
10508                              (gnus-data-header (car data)))))))
10509         (push (gnus-data-number (car data)) articles))
10510       (unless (and (setq data (cdr data))
10511                    (> (gnus-data-level (car data)) top-level))
10512         (setq data nil)))
10513     ;; Return the list of articles.
10514     (nreverse articles)))
10515
10516 (defun gnus-summary-rethread-current ()
10517   "Rethread the thread the current article is part of."
10518   (interactive)
10519   (let* ((gnus-show-threads t)
10520          (article (gnus-summary-article-number))
10521          (id (mail-header-id (gnus-summary-article-header)))
10522          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10523     (unless id
10524       (error "No article on the current line"))
10525     (gnus-rebuild-thread id)
10526     (gnus-summary-goto-subject article)))
10527
10528 (defun gnus-summary-reparent-thread ()
10529   "Make the current article child of the marked (or previous) article.
10530
10531 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10532 is non-nil or the Subject: of both articles are the same."
10533   (interactive)
10534   (unless (not (gnus-group-read-only-p))
10535     (error "The current newsgroup does not support article editing"))
10536   (unless (<= (length gnus-newsgroup-processable) 1)
10537     (error "No more than one article may be marked"))
10538   (save-window-excursion
10539     (let ((gnus-article-buffer " *reparent*")
10540           (current-article (gnus-summary-article-number))
10541           ;; First grab the marked article, otherwise one line up.
10542           (parent-article (if (not (null gnus-newsgroup-processable))
10543                               (car gnus-newsgroup-processable)
10544                             (save-excursion
10545                               (if (eq (forward-line -1) 0)
10546                                   (gnus-summary-article-number)
10547                                 (error "Beginning of summary buffer"))))))
10548       (unless (not (eq current-article parent-article))
10549         (error "An article may not be self-referential"))
10550       (let ((message-id (mail-header-id
10551                          (gnus-summary-article-header parent-article))))
10552         (unless (and message-id (not (equal message-id "")))
10553           (error "No message-id in desired parent"))
10554         (gnus-with-article current-article
10555           (save-restriction
10556             (goto-char (point-min))
10557             (message-narrow-to-head)
10558             (if (re-search-forward "^References: " nil t)
10559                 (progn
10560                   (re-search-forward "^[^ \t]" nil t)
10561                   (forward-line -1)
10562                   (end-of-line)
10563                   (insert " " message-id))
10564               (insert "References: " message-id "\n"))))
10565         (set-buffer gnus-summary-buffer)
10566         (gnus-summary-unmark-all-processable)
10567         (gnus-summary-update-article current-article)
10568         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10569             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10570         (gnus-summary-rethread-current)
10571         (gnus-message 3 "Article %d is now the child of article %d"
10572                       current-article parent-article)))))
10573
10574 (defun gnus-summary-toggle-threads (&optional arg)
10575   "Toggle showing conversation threads.
10576 If ARG is positive number, turn showing conversation threads on."
10577   (interactive "P")
10578   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10579     (setq gnus-show-threads
10580           (if (null arg) (not gnus-show-threads)
10581             (> (prefix-numeric-value arg) 0)))
10582     (gnus-summary-prepare)
10583     (gnus-summary-goto-subject current)
10584     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10585     (gnus-summary-position-point)))
10586
10587 (defun gnus-summary-show-all-threads ()
10588   "Show all threads."
10589   (interactive)
10590   (save-excursion
10591     (let ((buffer-read-only nil))
10592       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10593   (gnus-summary-position-point))
10594
10595 (defun gnus-summary-show-thread ()
10596   "Show thread subtrees.
10597 Returns nil if no thread was there to be shown."
10598   (interactive)
10599   (let ((buffer-read-only nil)
10600         (orig (point))
10601         (end (point-at-eol))
10602         ;; Leave point at bol
10603         (beg (progn (beginning-of-line) (point))))
10604     (prog1
10605         ;; Any hidden lines here?
10606         (search-forward "\r" end t)
10607       (subst-char-in-region beg end ?\^M ?\n t)
10608       (goto-char orig)
10609       (gnus-summary-position-point))))
10610
10611 (defun gnus-summary-maybe-hide-threads ()
10612   "If requested, hide the threads that should be hidden."
10613   (when (and gnus-show-threads
10614              gnus-thread-hide-subtree)
10615     (gnus-summary-hide-all-threads
10616      (if (or (consp gnus-thread-hide-subtree)
10617              (functionp gnus-thread-hide-subtree))
10618          (gnus-make-predicate gnus-thread-hide-subtree)
10619        nil))))
10620
10621 ;;; Hiding predicates.
10622
10623 (defun gnus-article-unread-p (header)
10624   (memq (mail-header-number header) gnus-newsgroup-unreads))
10625
10626 (defun gnus-article-unseen-p (header)
10627   (memq (mail-header-number header) gnus-newsgroup-unseen))
10628
10629 (defun gnus-map-articles (predicate articles)
10630   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10631   (apply 'gnus-or (mapcar predicate
10632                           (mapcar 'gnus-summary-article-header articles))))
10633
10634 (defun gnus-summary-hide-all-threads (&optional predicate)
10635   "Hide all thread subtrees.
10636 If PREDICATE is supplied, threads that satisfy this predicate
10637 will not be hidden."
10638   (interactive)
10639   (save-excursion
10640     (goto-char (point-min))
10641     (let ((end nil))
10642       (while (not end)
10643         (when (or (not predicate)
10644                   (gnus-map-articles
10645                    predicate (gnus-summary-article-children)))
10646             (gnus-summary-hide-thread))
10647         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10648   (gnus-summary-position-point))
10649
10650 (defun gnus-summary-hide-thread ()
10651   "Hide thread subtrees.
10652 If PREDICATE is supplied, threads that satisfy this predicate
10653 will not be hidden.
10654 Returns nil if no threads were there to be hidden."
10655   (interactive)
10656   (let ((buffer-read-only nil)
10657         (start (point))
10658         (article (gnus-summary-article-number)))
10659     (goto-char start)
10660     ;; Go forward until either the buffer ends or the subthread
10661     ;; ends.
10662     (when (and (not (eobp))
10663                (or (zerop (gnus-summary-next-thread 1 t))
10664                    (goto-char (point-max))))
10665       (prog1
10666           (if (and (> (point) start)
10667                    (search-backward "\n" start t))
10668               (progn
10669                 (subst-char-in-region start (point) ?\n ?\^M)
10670                 (gnus-summary-goto-subject article))
10671             (goto-char start)
10672             nil)))))
10673
10674 (defun gnus-summary-go-to-next-thread (&optional previous)
10675   "Go to the same level (or less) next thread.
10676 If PREVIOUS is non-nil, go to previous thread instead.
10677 Return the article number moved to, or nil if moving was impossible."
10678   (let ((level (gnus-summary-thread-level))
10679         (way (if previous -1 1))
10680         (beg (point)))
10681     (forward-line way)
10682     (while (and (not (eobp))
10683                 (< level (gnus-summary-thread-level)))
10684       (forward-line way))
10685     (if (eobp)
10686         (progn
10687           (goto-char beg)
10688           nil)
10689       (setq beg (point))
10690       (prog1
10691           (gnus-summary-article-number)
10692         (goto-char beg)))))
10693
10694 (defun gnus-summary-next-thread (n &optional silent)
10695   "Go to the same level next N'th thread.
10696 If N is negative, search backward instead.
10697 Returns the difference between N and the number of skips actually
10698 done.
10699
10700 If SILENT, don't output messages."
10701   (interactive "p")
10702   (let ((backward (< n 0))
10703         (n (abs n)))
10704     (while (and (> n 0)
10705                 (gnus-summary-go-to-next-thread backward))
10706       (decf n))
10707     (unless silent
10708       (gnus-summary-position-point))
10709     (when (and (not silent) (/= 0 n))
10710       (gnus-message 7 "No more threads"))
10711     n))
10712
10713 (defun gnus-summary-prev-thread (n)
10714   "Go to the same level previous N'th thread.
10715 Returns the difference between N and the number of skips actually
10716 done."
10717   (interactive "p")
10718   (gnus-summary-next-thread (- n)))
10719
10720 (defun gnus-summary-go-down-thread ()
10721   "Go down one level in the current thread."
10722   (let ((children (gnus-summary-article-children)))
10723     (when children
10724       (gnus-summary-goto-subject (car children)))))
10725
10726 (defun gnus-summary-go-up-thread ()
10727   "Go up one level in the current thread."
10728   (let ((parent (gnus-summary-article-parent)))
10729     (when parent
10730       (gnus-summary-goto-subject parent))))
10731
10732 (defun gnus-summary-down-thread (n)
10733   "Go down thread N steps.
10734 If N is negative, go up instead.
10735 Returns the difference between N and how many steps down that were
10736 taken."
10737   (interactive "p")
10738   (let ((up (< n 0))
10739         (n (abs n)))
10740     (while (and (> n 0)
10741                 (if up (gnus-summary-go-up-thread)
10742                   (gnus-summary-go-down-thread)))
10743       (setq n (1- n)))
10744     (gnus-summary-position-point)
10745     (when (/= 0 n)
10746       (gnus-message 7 "Can't go further"))
10747     n))
10748
10749 (defun gnus-summary-up-thread (n)
10750   "Go up thread N steps.
10751 If N is negative, go down instead.
10752 Returns the difference between N and how many steps down that were
10753 taken."
10754   (interactive "p")
10755   (gnus-summary-down-thread (- n)))
10756
10757 (defun gnus-summary-top-thread ()
10758   "Go to the top of the thread."
10759   (interactive)
10760   (while (gnus-summary-go-up-thread))
10761   (gnus-summary-article-number))
10762
10763 (defun gnus-summary-kill-thread (&optional unmark)
10764   "Mark articles under current thread as read.
10765 If the prefix argument is positive, remove any kinds of marks.
10766 If the prefix argument is negative, tick articles instead."
10767   (interactive "P")
10768   (when unmark
10769     (setq unmark (prefix-numeric-value unmark)))
10770   (let ((articles (gnus-summary-articles-in-thread)))
10771     (save-excursion
10772       ;; Expand the thread.
10773       (gnus-summary-show-thread)
10774       ;; Mark all the articles.
10775       (while articles
10776         (gnus-summary-goto-subject (car articles))
10777         (cond ((null unmark)
10778                (gnus-summary-mark-article-as-read gnus-killed-mark))
10779               ((> unmark 0)
10780                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10781               (t
10782                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10783         (setq articles (cdr articles))))
10784     ;; Hide killed subtrees.
10785     (and (null unmark)
10786          gnus-thread-hide-killed
10787          (gnus-summary-hide-thread))
10788     ;; If marked as read, go to next unread subject.
10789     (when (null unmark)
10790       ;; Go to next unread subject.
10791       (gnus-summary-next-subject 1 t)))
10792   (gnus-set-mode-line 'summary))
10793
10794 ;; Summary sorting commands
10795
10796 (defun gnus-summary-sort-by-number (&optional reverse)
10797   "Sort the summary buffer by article number.
10798 Argument REVERSE means reverse order."
10799   (interactive "P")
10800   (gnus-summary-sort 'number reverse))
10801
10802 (defun gnus-summary-sort-by-random (&optional reverse)
10803   "Randomize the order in the summary buffer.
10804 Argument REVERSE means to randomize in reverse order."
10805   (interactive "P")
10806   (gnus-summary-sort 'random reverse))
10807
10808 (defun gnus-summary-sort-by-author (&optional reverse)
10809   "Sort the summary buffer by author name alphabetically.
10810 If `case-fold-search' is non-nil, case of letters is ignored.
10811 Argument REVERSE means reverse order."
10812   (interactive "P")
10813   (gnus-summary-sort 'author reverse))
10814
10815 (defun gnus-summary-sort-by-subject (&optional reverse)
10816   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10817 If `case-fold-search' is non-nil, case of letters is ignored.
10818 Argument REVERSE means reverse order."
10819   (interactive "P")
10820   (gnus-summary-sort 'subject reverse))
10821
10822 (defun gnus-summary-sort-by-date (&optional reverse)
10823   "Sort the summary buffer by date.
10824 Argument REVERSE means reverse order."
10825   (interactive "P")
10826   (gnus-summary-sort 'date reverse))
10827
10828 (defun gnus-summary-sort-by-score (&optional reverse)
10829   "Sort the summary buffer by score.
10830 Argument REVERSE means reverse order."
10831   (interactive "P")
10832   (gnus-summary-sort 'score reverse))
10833
10834 (defun gnus-summary-sort-by-lines (&optional reverse)
10835   "Sort the summary buffer by the number of lines.
10836 Argument REVERSE means reverse order."
10837   (interactive "P")
10838   (gnus-summary-sort 'lines reverse))
10839
10840 (defun gnus-summary-sort-by-chars (&optional reverse)
10841   "Sort the summary buffer by article length.
10842 Argument REVERSE means reverse order."
10843   (interactive "P")
10844   (gnus-summary-sort 'chars reverse))
10845
10846 (defun gnus-summary-sort-by-original (&optional reverse)
10847   "Sort the summary buffer using the default sorting method.
10848 Argument REVERSE means reverse order."
10849   (interactive "P")
10850   (let* ((buffer-read-only)
10851          (gnus-summary-prepare-hook nil))
10852     ;; We do the sorting by regenerating the threads.
10853     (gnus-summary-prepare)
10854     ;; Hide subthreads if needed.
10855     (gnus-summary-maybe-hide-threads)))
10856
10857 (defun gnus-summary-sort (predicate reverse)
10858   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10859   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10860          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10861          (gnus-thread-sort-functions
10862           (if (not reverse)
10863               thread
10864             `(lambda (t1 t2)
10865                (,thread t2 t1))))
10866          (gnus-sort-gathered-threads-function
10867           gnus-thread-sort-functions)
10868          (gnus-article-sort-functions
10869           (if (not reverse)
10870               article
10871             `(lambda (t1 t2)
10872                (,article t2 t1))))
10873          (buffer-read-only)
10874          (gnus-summary-prepare-hook nil))
10875     ;; We do the sorting by regenerating the threads.
10876     (gnus-summary-prepare)
10877     ;; Hide subthreads if needed.
10878     (gnus-summary-maybe-hide-threads)))
10879
10880 ;; Summary saving commands.
10881
10882 (defun gnus-summary-save-article (&optional n not-saved)
10883   "Save the current article using the default saver function.
10884 If N is a positive number, save the N next articles.
10885 If N is a negative number, save the N previous articles.
10886 If N is nil and any articles have been marked with the process mark,
10887 save those articles instead.
10888 The variable `gnus-default-article-saver' specifies the saver function."
10889   (interactive "P")
10890   (let* ((articles (gnus-summary-work-articles n))
10891          (save-buffer (save-excursion
10892                         (nnheader-set-temp-buffer " *Gnus Save*")))
10893          (num (length articles))
10894          header file)
10895     (dolist (article articles)
10896       (setq header (gnus-summary-article-header article))
10897       (if (not (vectorp header))
10898           ;; This is a pseudo-article.
10899           (if (assq 'name header)
10900               (gnus-copy-file (cdr (assq 'name header)))
10901             (gnus-message 1 "Article %d is unsaveable" article))
10902         ;; This is a real article.
10903         (save-window-excursion
10904           (let ((gnus-display-mime-function nil)
10905                 (gnus-article-prepare-hook nil))
10906             (gnus-summary-select-article t nil nil article)))
10907         (save-excursion
10908           (set-buffer save-buffer)
10909           (erase-buffer)
10910           (insert-buffer-substring gnus-original-article-buffer))
10911         (setq file (gnus-article-save save-buffer file num))
10912         (gnus-summary-remove-process-mark article)
10913         (unless not-saved
10914           (gnus-summary-set-saved-mark article))))
10915     (gnus-kill-buffer save-buffer)
10916     (gnus-summary-position-point)
10917     (gnus-set-mode-line 'summary)
10918     n))
10919
10920 (defun gnus-summary-pipe-output (&optional arg headers)
10921   "Pipe the current article to a subprocess.
10922 If N is a positive number, pipe the N next articles.
10923 If N is a negative number, pipe the N previous articles.
10924 If N is nil and any articles have been marked with the process mark,
10925 pipe those articles instead.
10926 If HEADERS (the symbolic prefix), include the headers, too."
10927   (interactive (gnus-interactive "P\ny"))
10928   (require 'gnus-art)
10929   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10930         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10931     (gnus-summary-save-article arg t))
10932   (let ((buffer (get-buffer "*Shell Command Output*")))
10933     (when (and buffer
10934                (not (zerop (buffer-size buffer))))
10935       (gnus-configure-windows 'pipe))))
10936
10937 (defun gnus-summary-save-article-mail (&optional arg)
10938   "Append the current article to an mail file.
10939 If N is a positive number, save the N next articles.
10940 If N is a negative number, save the N previous articles.
10941 If N is nil and any articles have been marked with the process mark,
10942 save those articles instead."
10943   (interactive "P")
10944   (require 'gnus-art)
10945   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10946     (gnus-summary-save-article arg)))
10947
10948 (defun gnus-summary-save-article-rmail (&optional arg)
10949   "Append the current article to an rmail file.
10950 If N is a positive number, save the N next articles.
10951 If N is a negative number, save the N previous articles.
10952 If N is nil and any articles have been marked with the process mark,
10953 save those articles instead."
10954   (interactive "P")
10955   (require 'gnus-art)
10956   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10957     (gnus-summary-save-article arg)))
10958
10959 (defun gnus-summary-save-article-file (&optional arg)
10960   "Append the current article to a file.
10961 If N is a positive number, save the N next articles.
10962 If N is a negative number, save the N previous articles.
10963 If N is nil and any articles have been marked with the process mark,
10964 save those articles instead."
10965   (interactive "P")
10966   (require 'gnus-art)
10967   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10968     (gnus-summary-save-article arg)))
10969
10970 (defun gnus-summary-write-article-file (&optional arg)
10971   "Write the current article to a file, deleting the previous file.
10972 If N is a positive number, save the N next articles.
10973 If N is a negative number, save the N previous articles.
10974 If N is nil and any articles have been marked with the process mark,
10975 save those articles instead."
10976   (interactive "P")
10977   (require 'gnus-art)
10978   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10979     (gnus-summary-save-article arg)))
10980
10981 (defun gnus-summary-save-article-body-file (&optional arg)
10982   "Append the current article body to a file.
10983 If N is a positive number, save the N next articles.
10984 If N is a negative number, save the N previous articles.
10985 If N is nil and any articles have been marked with the process mark,
10986 save those articles instead."
10987   (interactive "P")
10988   (require 'gnus-art)
10989   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10990     (gnus-summary-save-article arg)))
10991
10992 (defun gnus-summary-muttprint (&optional arg)
10993   "Print the current article using Muttprint.
10994 If N is a positive number, save the N next articles.
10995 If N is a negative number, save the N previous articles.
10996 If N is nil and any articles have been marked with the process mark,
10997 save those articles instead."
10998   (interactive "P")
10999   (require 'gnus-art)
11000   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11001     (gnus-summary-save-article arg t)))
11002
11003 (defun gnus-summary-pipe-message (program)
11004   "Pipe the current article through PROGRAM."
11005   (interactive "sProgram: ")
11006   (gnus-summary-select-article)
11007   (let ((mail-header-separator ""))
11008     (gnus-eval-in-buffer-window gnus-article-buffer
11009       (save-restriction
11010         (widen)
11011         (let ((start (window-start))
11012               buffer-read-only)
11013           (message-pipe-buffer-body program)
11014           (set-window-start (get-buffer-window (current-buffer)) start))))))
11015
11016 (defun gnus-get-split-value (methods)
11017   "Return a value based on the split METHODS."
11018   (let (split-name method result match)
11019     (when methods
11020       (save-excursion
11021         (set-buffer gnus-original-article-buffer)
11022         (save-restriction
11023           (nnheader-narrow-to-headers)
11024           (while (and methods (not split-name))
11025             (goto-char (point-min))
11026             (setq method (pop methods))
11027             (setq match (car method))
11028             (when (cond
11029                    ((stringp match)
11030                     ;; Regular expression.
11031                     (ignore-errors
11032                       (re-search-forward match nil t)))
11033                    ((functionp match)
11034                     ;; Function.
11035                     (save-restriction
11036                       (widen)
11037                       (setq result (funcall match gnus-newsgroup-name))))
11038                    ((consp match)
11039                     ;; Form.
11040                     (save-restriction
11041                       (widen)
11042                       (setq result (eval match)))))
11043               (setq split-name (cdr method))
11044               (cond ((stringp result)
11045                      (push (expand-file-name
11046                             result gnus-article-save-directory)
11047                            split-name))
11048                     ((consp result)
11049                      (setq split-name (append result split-name)))))))))
11050     (nreverse split-name)))
11051
11052 (defun gnus-valid-move-group-p (group)
11053   (and (boundp group)
11054        (symbol-name group)
11055        (symbol-value group)
11056        (gnus-get-function (gnus-find-method-for-group
11057                            (symbol-name group)) 'request-accept-article t)))
11058
11059 (defun gnus-read-move-group-name (prompt default articles prefix)
11060   "Read a group name."
11061   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11062          (minibuffer-confirm-incomplete nil) ; XEmacs
11063          (prom
11064           (format "%s %s to:"
11065                   prompt
11066                   (if (> (length articles) 1)
11067                       (format "these %d articles" (length articles))
11068                     "this article")))
11069          (to-newsgroup
11070           (cond
11071            ((null split-name)
11072             (gnus-completing-read-with-default
11073              default prom
11074              gnus-active-hashtb
11075              'gnus-valid-move-group-p
11076              nil prefix
11077              'gnus-group-history))
11078            ((= 1 (length split-name))
11079             (gnus-completing-read-with-default
11080              (car split-name) prom
11081              gnus-active-hashtb
11082              'gnus-valid-move-group-p
11083              nil nil
11084              'gnus-group-history))
11085            (t
11086             (gnus-completing-read-with-default
11087              nil prom
11088              (mapcar (lambda (el) (list el))
11089                      (nreverse split-name))
11090              nil nil nil
11091              'gnus-group-history))))
11092          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11093     (when to-newsgroup
11094       (if (or (string= to-newsgroup "")
11095               (string= to-newsgroup prefix))
11096           (setq to-newsgroup default))
11097       (unless to-newsgroup
11098         (error "No group name entered"))
11099       (or (gnus-active to-newsgroup)
11100           (gnus-activate-group to-newsgroup nil nil to-method)
11101           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11102                                      to-newsgroup))
11103               (or (and (gnus-request-create-group to-newsgroup to-method)
11104                        (gnus-activate-group
11105                         to-newsgroup nil nil to-method)
11106                        (gnus-subscribe-group to-newsgroup))
11107                   (error "Couldn't create group %s" to-newsgroup)))
11108           (error "No such group: %s" to-newsgroup)))
11109     to-newsgroup))
11110
11111 (defun gnus-summary-save-parts (type dir n &optional reverse)
11112   "Save parts matching TYPE to DIR.
11113 If REVERSE, save parts that do not match TYPE."
11114   (interactive
11115    (list (read-string "Save parts of type: "
11116                       (or (car gnus-summary-save-parts-type-history)
11117                           gnus-summary-save-parts-default-mime)
11118                       'gnus-summary-save-parts-type-history)
11119          (setq gnus-summary-save-parts-last-directory
11120                (read-file-name "Save to directory: "
11121                                gnus-summary-save-parts-last-directory
11122                                nil t))
11123          current-prefix-arg))
11124   (gnus-summary-iterate n
11125     (let ((gnus-display-mime-function nil)
11126           (gnus-inhibit-treatment t))
11127       (gnus-summary-select-article))
11128     (save-excursion
11129       (set-buffer gnus-article-buffer)
11130       (let ((handles (or gnus-article-mime-handles
11131                          (mm-dissect-buffer nil gnus-article-loose-mime)
11132                          (and gnus-article-emulate-mime
11133                               (mm-uu-dissect)))))
11134         (when handles
11135           (gnus-summary-save-parts-1 type dir handles reverse)
11136           (unless gnus-article-mime-handles ;; Don't destroy this case.
11137             (mm-destroy-parts handles)))))))
11138
11139 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11140   (if (stringp (car handle))
11141       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11142               (cdr handle))
11143     (when (if reverse
11144               (not (string-match type (mm-handle-media-type handle)))
11145             (string-match type (mm-handle-media-type handle)))
11146       (let ((file (expand-file-name
11147                    (gnus-map-function
11148                     mm-file-name-rewrite-functions
11149                     (file-name-nondirectory
11150                      (or
11151                       (mail-content-type-get
11152                        (mm-handle-disposition handle) 'filename)
11153                       (mail-content-type-get
11154                        (mm-handle-type handle) 'name)
11155                       (concat gnus-newsgroup-name
11156                               "." (number-to-string
11157                                    (cdr gnus-article-current))))))
11158                    dir)))
11159         (unless (file-exists-p file)
11160           (mm-save-part-to-file handle file))))))
11161
11162 ;; Summary extract commands
11163
11164 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11165   (let ((buffer-read-only nil)
11166         (article (gnus-summary-article-number))
11167         after-article b e)
11168     (unless (gnus-summary-goto-subject article)
11169       (error "No such article: %d" article))
11170     (gnus-summary-position-point)
11171     ;; If all commands are to be bunched up on one line, we collect
11172     ;; them here.
11173     (unless gnus-view-pseudos-separately
11174       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11175             files action)
11176         (while ps
11177           (setq action (cdr (assq 'action (car ps))))
11178           (setq files (list (cdr (assq 'name (car ps)))))
11179           (while (and ps (cdr ps)
11180                       (string= (or action "1")
11181                                (or (cdr (assq 'action (cadr ps))) "2")))
11182             (push (cdr (assq 'name (cadr ps))) files)
11183             (setcdr ps (cddr ps)))
11184           (when files
11185             (when (not (string-match "%s" action))
11186               (push " " files))
11187             (push " " files)
11188             (when (assq 'execute (car ps))
11189               (setcdr (assq 'execute (car ps))
11190                       (funcall (if (string-match "%s" action)
11191                                    'format 'concat)
11192                                action
11193                                (mapconcat
11194                                 (lambda (f)
11195                                   (if (equal f " ")
11196                                       f
11197                                     (shell-quote-argument f)))
11198                                 files " ")))))
11199           (setq ps (cdr ps)))))
11200     (if (and gnus-view-pseudos (not not-view))
11201         (while pslist
11202           (when (assq 'execute (car pslist))
11203             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11204                                   (eq gnus-view-pseudos 'not-confirm)))
11205           (setq pslist (cdr pslist)))
11206       (save-excursion
11207         (while pslist
11208           (setq after-article (or (cdr (assq 'article (car pslist)))
11209                                   (gnus-summary-article-number)))
11210           (gnus-summary-goto-subject after-article)
11211           (forward-line 1)
11212           (setq b (point))
11213           (insert "    " (file-name-nondirectory
11214                           (cdr (assq 'name (car pslist))))
11215                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11216           (setq e (point))
11217           (forward-line -1)             ; back to `b'
11218           (gnus-add-text-properties
11219            b (1- e) (list 'gnus-number gnus-reffed-article-number
11220                           gnus-mouse-face-prop gnus-mouse-face))
11221           (gnus-data-enter
11222            after-article gnus-reffed-article-number
11223            gnus-unread-mark b (car pslist) 0 (- e b))
11224           (setq gnus-newsgroup-unreads
11225                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11226                                          gnus-reffed-article-number))
11227           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11228           (setq pslist (cdr pslist)))))))
11229
11230 (defun gnus-pseudos< (p1 p2)
11231   (let ((c1 (cdr (assq 'action p1)))
11232         (c2 (cdr (assq 'action p2))))
11233     (and c1 c2 (string< c1 c2))))
11234
11235 (defun gnus-request-pseudo-article (props)
11236   (cond ((assq 'execute props)
11237          (gnus-execute-command (cdr (assq 'execute props)))))
11238   (let ((gnus-current-article (gnus-summary-article-number)))
11239     (gnus-run-hooks 'gnus-mark-article-hook)))
11240
11241 (defun gnus-execute-command (command &optional automatic)
11242   (save-excursion
11243     (gnus-article-setup-buffer)
11244     (set-buffer gnus-article-buffer)
11245     (setq buffer-read-only nil)
11246     (let ((command (if automatic command
11247                      (read-string "Command: " (cons command 0)))))
11248       (erase-buffer)
11249       (insert "$ " command "\n\n")
11250       (if gnus-view-pseudo-asynchronously
11251           (start-process "gnus-execute" (current-buffer) shell-file-name
11252                          shell-command-switch command)
11253         (call-process shell-file-name nil t nil
11254                       shell-command-switch command)))))
11255
11256 ;; Summary kill commands.
11257
11258 (defun gnus-summary-edit-global-kill (article)
11259   "Edit the \"global\" kill file."
11260   (interactive (list (gnus-summary-article-number)))
11261   (gnus-group-edit-global-kill article))
11262
11263 (defun gnus-summary-edit-local-kill ()
11264   "Edit a local kill file applied to the current newsgroup."
11265   (interactive)
11266   (setq gnus-current-headers (gnus-summary-article-header))
11267   (gnus-group-edit-local-kill
11268    (gnus-summary-article-number) gnus-newsgroup-name))
11269
11270 ;;; Header reading.
11271
11272 (defun gnus-read-header (id &optional header)
11273   "Read the headers of article ID and enter them into the Gnus system."
11274   (let ((group gnus-newsgroup-name)
11275         (gnus-override-method
11276          (or
11277           gnus-override-method
11278           (and (gnus-news-group-p gnus-newsgroup-name)
11279                (car (gnus-refer-article-methods)))))
11280         where)
11281     ;; First we check to see whether the header in question is already
11282     ;; fetched.
11283     (if (stringp id)
11284         ;; This is a Message-ID.
11285         (setq header (or header (gnus-id-to-header id)))
11286       ;; This is an article number.
11287       (setq header (or header (gnus-summary-article-header id))))
11288     (if (and header
11289              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11290         ;; We have found the header.
11291         header
11292       ;; If this is a sparse article, we have to nix out its
11293       ;; previous entry in the thread hashtb.
11294       (when (and header
11295                  (gnus-summary-article-sparse-p (mail-header-number header)))
11296         (let* ((parent (gnus-parent-id (mail-header-references header)))
11297                (thread (and parent (gnus-id-to-thread parent))))
11298           (when thread
11299             (delq (assq header thread) thread))))
11300       ;; We have to really fetch the header to this article.
11301       (save-excursion
11302         (set-buffer nntp-server-buffer)
11303         (when (setq where (gnus-request-head id group))
11304           (nnheader-fold-continuation-lines)
11305           (goto-char (point-max))
11306           (insert ".\n")
11307           (goto-char (point-min))
11308           (insert "211 ")
11309           (princ (cond
11310                   ((numberp id) id)
11311                   ((cdr where) (cdr where))
11312                   (header (mail-header-number header))
11313                   (t gnus-reffed-article-number))
11314                  (current-buffer))
11315           (insert " Article retrieved.\n"))
11316         (if (or (not where)
11317                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11318             ()                          ; Malformed head.
11319           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11320             (when (and (stringp id)
11321                        (not (string= (gnus-group-real-name group)
11322                                      (car where))))
11323               ;; If we fetched by Message-ID and the article came
11324               ;; from a different group, we fudge some bogus article
11325               ;; numbers for this article.
11326               (mail-header-set-number header gnus-reffed-article-number))
11327             (save-excursion
11328               (set-buffer gnus-summary-buffer)
11329               (decf gnus-reffed-article-number)
11330               (gnus-remove-header (mail-header-number header))
11331               (push header gnus-newsgroup-headers)
11332               (setq gnus-current-headers header)
11333               (push (mail-header-number header) gnus-newsgroup-limit)))
11334           header)))))
11335
11336 (defun gnus-remove-header (number)
11337   "Remove header NUMBER from `gnus-newsgroup-headers'."
11338   (if (and gnus-newsgroup-headers
11339            (= number (mail-header-number (car gnus-newsgroup-headers))))
11340       (pop gnus-newsgroup-headers)
11341     (let ((headers gnus-newsgroup-headers))
11342       (while (and (cdr headers)
11343                   (not (= number (mail-header-number (cadr headers)))))
11344         (pop headers))
11345       (when (cdr headers)
11346         (setcdr headers (cddr headers))))))
11347
11348 ;;;
11349 ;;; summary highlights
11350 ;;;
11351
11352 (defun gnus-highlight-selected-summary ()
11353   "Highlight selected article in summary buffer."
11354   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11355   (when gnus-summary-selected-face
11356     (save-excursion
11357       (let* ((beg (point-at-bol))
11358              (end (point-at-eol))
11359              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11360              (from (if (get-text-property beg gnus-mouse-face-prop)
11361                        beg
11362                      (or (next-single-property-change
11363                           beg gnus-mouse-face-prop nil end)
11364                          beg)))
11365              (to
11366               (if (= from end)
11367                   (- from 2)
11368                 (or (next-single-property-change
11369                      from gnus-mouse-face-prop nil end)
11370                     end))))
11371         ;; If no mouse-face prop on line we will have to = from = end,
11372         ;; so we highlight the entire line instead.
11373         (when (= (+ to 2) from)
11374           (setq from beg)
11375           (setq to end))
11376         (if gnus-newsgroup-selected-overlay
11377             ;; Move old overlay.
11378             (gnus-move-overlay
11379              gnus-newsgroup-selected-overlay from to (current-buffer))
11380           ;; Create new overlay.
11381           (gnus-overlay-put
11382            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11383            'face gnus-summary-selected-face))))))
11384
11385 (defvar gnus-summary-highlight-line-cached nil)
11386 (defvar gnus-summary-highlight-line-trigger nil)
11387
11388 (defun gnus-summary-highlight-line-0 ()
11389   (if (and (eq gnus-summary-highlight-line-trigger
11390                gnus-summary-highlight)
11391            gnus-summary-highlight-line-cached)
11392       gnus-summary-highlight-line-cached
11393     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11394           gnus-summary-highlight-line-cached
11395           (let* ((cond (list 'cond))
11396                  (c cond)
11397                  (list gnus-summary-highlight))
11398             (while list
11399               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11400                               nil))
11401               (setq c (cdr c)
11402                     list (cdr list)))
11403             (gnus-byte-compile (list 'lambda nil cond))))))
11404
11405 (defun gnus-summary-highlight-line ()
11406   "Highlight current line according to `gnus-summary-highlight'."
11407   (let* ((beg (point-at-bol))
11408          (article (or (gnus-summary-article-number) gnus-current-article))
11409          (score (or (cdr (assq article
11410                                gnus-newsgroup-scored))
11411                     gnus-summary-default-score 0))
11412          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11413          (inhibit-read-only t)
11414          (default gnus-summary-default-score)
11415          (default-high gnus-summary-default-high-score)
11416          (default-low gnus-summary-default-low-score)
11417          (uncached (and gnus-summary-use-undownloaded-faces
11418                         (memq article gnus-newsgroup-undownloaded))))
11419     (let ((face (funcall (gnus-summary-highlight-line-0))))
11420       (unless (eq face (get-text-property beg 'face))
11421         (gnus-put-text-property-excluding-characters-with-faces
11422          beg (point-at-eol) 'face
11423          (setq face (if (boundp face) (symbol-value face) face)))
11424         (when gnus-summary-highlight-line-function
11425           (funcall gnus-summary-highlight-line-function article face))))))
11426
11427 (defun gnus-update-read-articles (group unread &optional compute)
11428   "Update the list of read articles in GROUP.
11429 UNREAD is a sorted list."
11430   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11431         (info (gnus-get-info group))
11432         (prev 1)
11433         read)
11434     (if (or (not info) (not active))
11435         ;; There is no info on this group if it was, in fact,
11436         ;; killed.  Gnus stores no information on killed groups, so
11437         ;; there's nothing to be done.
11438         ;; One could store the information somewhere temporarily,
11439         ;; perhaps...  Hmmm...
11440         ()
11441       ;; Remove any negative articles numbers.
11442       (while (and unread (< (car unread) 0))
11443         (setq unread (cdr unread)))
11444       ;; Remove any expired article numbers
11445       (while (and unread (< (car unread) (car active)))
11446         (setq unread (cdr unread)))
11447       ;; Compute the ranges of read articles by looking at the list of
11448       ;; unread articles.
11449       (while unread
11450         (when (/= (car unread) prev)
11451           (push (if (= prev (1- (car unread))) prev
11452                   (cons prev (1- (car unread))))
11453                 read))
11454         (setq prev (1+ (car unread)))
11455         (setq unread (cdr unread)))
11456       (when (<= prev (cdr active))
11457         (push (cons prev (cdr active)) read))
11458       (setq read (if (> (length read) 1) (nreverse read) read))
11459       (if compute
11460           read
11461         (save-excursion
11462           (let (setmarkundo)
11463             ;; Propagate the read marks to the backend.
11464             (when (gnus-check-backend-function 'request-set-mark group)
11465               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11466                     (add (gnus-remove-from-range read (gnus-info-read info))))
11467                 (when (or add del)
11468                   (unless (gnus-check-group group)
11469                     (error "Can't open server for %s" group))
11470                   (gnus-request-set-mark
11471                    group (delq nil (list (if add (list add 'add '(read)))
11472                                          (if del (list del 'del '(read))))))
11473                   (setq setmarkundo
11474                         `(gnus-request-set-mark
11475                           ,group
11476                           ',(delq nil (list
11477                                        (if del (list del 'add '(read)))
11478                                        (if add (list add 'del '(read))))))))))
11479             (set-buffer gnus-group-buffer)
11480             (gnus-undo-register
11481               `(progn
11482                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11483                  (gnus-info-set-read ',info ',(gnus-info-read info))
11484                  (gnus-get-unread-articles-in-group ',info
11485                                                     (gnus-active ,group))
11486                  (gnus-group-update-group ,group t)
11487                  ,setmarkundo))))
11488         ;; Enter this list into the group info.
11489         (gnus-info-set-read info read)
11490         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11491         (gnus-get-unread-articles-in-group info (gnus-active group))
11492         t))))
11493
11494 (defun gnus-offer-save-summaries ()
11495   "Offer to save all active summary buffers."
11496   (let (buffers)
11497     ;; Go through all buffers and find all summaries.
11498     (dolist (buffer (buffer-list))
11499       (when (and (setq buffer (buffer-name buffer))
11500                  (string-match "Summary" buffer)
11501                  (save-excursion
11502                    (set-buffer buffer)
11503                    ;; We check that this is, indeed, a summary buffer.
11504                    (and (eq major-mode 'gnus-summary-mode)
11505                         ;; Also make sure this isn't bogus.
11506                         gnus-newsgroup-prepared
11507                         ;; Also make sure that this isn't a
11508                         ;; dead summary buffer.
11509                         (not gnus-dead-summary-mode))))
11510         (push buffer buffers)))
11511     ;; Go through all these summary buffers and offer to save them.
11512     (when buffers
11513       (save-excursion
11514         (map-y-or-n-p
11515          "Update summary buffer %s? "
11516          (lambda (buf)
11517            (switch-to-buffer buf)
11518            (gnus-summary-exit))
11519          buffers)))))
11520
11521
11522 ;;; @ for mime-partial
11523 ;;;
11524
11525 (defun gnus-request-partial-message ()
11526   (save-excursion
11527     (let ((number (gnus-summary-article-number))
11528           (group gnus-newsgroup-name)
11529           (mother gnus-article-buffer))
11530       (set-buffer (get-buffer-create " *Partial Article*"))
11531       (erase-buffer)
11532       (setq mime-preview-buffer mother)
11533       (gnus-request-article-this-buffer number group)
11534       (mime-parse-buffer)
11535       )))
11536
11537 (autoload 'mime-combine-message/partial-pieces-automatically
11538   "mime-partial"
11539   "Internal method to combine message/partial messages automatically.")
11540
11541 (mime-add-condition
11542  'action '((type . message)(subtype . partial)
11543            (major-mode . gnus-original-article-mode)
11544            (method . mime-combine-message/partial-pieces-automatically)
11545            (summary-buffer-exp . gnus-summary-buffer)
11546            (request-partial-message-method . gnus-request-partial-message)
11547            ))
11548
11549
11550 ;;; @ for message/rfc822
11551 ;;;
11552
11553 (defun gnus-mime-extract-message/rfc822 (entity situation)
11554   "Burst a forwarded article."
11555   (save-excursion
11556     (set-buffer gnus-summary-buffer)
11557     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11558                                    gnus-newsgroup-name 'gnus-group-history))
11559            (gnus-group-marked (list group))
11560            article info)
11561       (with-temp-buffer
11562         (mime-insert-entity-content entity)
11563         (setq article (gnus-request-accept-article group)))
11564       (when (and (consp article)
11565                  (numberp (setq article (cdr article))))
11566         (setq info (gnus-get-info group))
11567         (gnus-info-set-read info
11568                             (gnus-remove-from-range (gnus-info-read info)
11569                                                     (list article)))
11570         (when (string-equal group gnus-newsgroup-name)
11571           (forward-line 1)
11572           (let (gnus-show-threads)
11573             (gnus-summary-goto-subject article t))
11574           (gnus-summary-clear-mark-forward 1))
11575         (set-buffer gnus-group-buffer)
11576         (gnus-group-get-new-news-this-group nil t)))))
11577
11578 (mime-add-condition
11579  'action '((type . message)(subtype . rfc822)
11580            (major-mode . gnus-original-article-mode)
11581            (method . gnus-mime-extract-message/rfc822)
11582            (mode . "extract")
11583            ))
11584
11585 (mime-add-condition
11586  'action '((type . message)(subtype . news)
11587            (major-mode . gnus-original-article-mode)
11588            (method . gnus-mime-extract-message/rfc822)
11589            (mode . "extract")
11590            ))
11591
11592 (defun gnus-mime-extract-multipart (entity situation)
11593   (let ((children (mime-entity-children entity))
11594         mime-acting-situation-to-override
11595         f)
11596     (while children
11597       (mime-play-entity (car children)
11598                         (cons (assq 'mode situation)
11599                               mime-acting-situation-to-override))
11600       (setq children (cdr children)))
11601     (if (setq f (cdr (assq 'after-method
11602                            mime-acting-situation-to-override)))
11603         (eval f)
11604       )))
11605
11606 (mime-add-condition
11607  'action '((type . multipart)
11608            (method . gnus-mime-extract-multipart)
11609            (mode . "extract")
11610            )
11611  'with-default)
11612
11613
11614 ;;; @ end
11615 ;;;
11616
11617 (defun gnus-summary-inherit-default-charset ()
11618   "Import `default-mime-charset' from summary buffer.
11619 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11620 of FLIM is used."
11621   (if (buffer-live-p gnus-summary-buffer)
11622       (let (d-m-c d-m-c-u)
11623         (with-current-buffer gnus-summary-buffer
11624           (setq d-m-c (if (local-variable-p 'default-mime-charset
11625                                             gnus-summary-buffer)
11626                           default-mime-charset
11627                         t)
11628                 ;; LIMIT
11629                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11630                                               gnus-summary-buffer)
11631                             (symbol-value 'default-mime-charset-unlimited)
11632                           t)))
11633         (if (eq t d-m-c)
11634             (kill-local-variable 'default-mime-charset)
11635           (set (make-local-variable 'default-mime-charset) d-m-c))
11636         (if (eq t d-m-c-u)
11637             (kill-local-variable 'default-mime-charset-unlimited)
11638           (set (make-local-variable 'default-mime-charset-unlimited)
11639                d-m-c-u)))))
11640
11641 (defun gnus-summary-setup-default-charset ()
11642   "Setup newsgroup default charset."
11643   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11644       (progn
11645         (setq gnus-newsgroup-charset nil)
11646         (set (make-local-variable 'default-mime-charset) nil)
11647         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11648           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11649     (let ((ignored-charsets
11650            (or gnus-newsgroup-ephemeral-ignored-charsets
11651                (append
11652                 (and gnus-newsgroup-name
11653                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11654                 gnus-newsgroup-ignored-charsets)))
11655           charset)
11656       (setq gnus-newsgroup-charset
11657             (or gnus-newsgroup-ephemeral-charset
11658                 (when (and gnus-newsgroup-name
11659                            (setq charset (gnus-parameter-charset
11660                                           gnus-newsgroup-name)))
11661                   (make-local-variable 'default-mime-charset)
11662                   (setq default-mime-charset charset))
11663                 gnus-default-charset))
11664       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11665            ignored-charsets))))
11666
11667 ;;;
11668 ;;; Mime Commands
11669 ;;;
11670
11671 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11672   "Display the current article buffer fully MIME-buttonized.
11673 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11674 treated as multipart/mixed."
11675   (interactive "P")
11676   (require 'gnus-art)
11677   (let ((gnus-unbuttonized-mime-types nil)
11678         (gnus-mime-display-multipart-as-mixed show-all-parts))
11679     (gnus-summary-show-article)))
11680
11681 (defun gnus-summary-repair-multipart (article)
11682   "Add a Content-Type header to a multipart article without one."
11683   (interactive (list (gnus-summary-article-number)))
11684   (gnus-with-article article
11685     (message-narrow-to-head)
11686     (message-remove-header "Mime-Version")
11687     (goto-char (point-max))
11688     (insert "Mime-Version: 1.0\n")
11689     (widen)
11690     (when (search-forward "\n--" nil t)
11691       (let ((separator (buffer-substring (point) (point-at-eol))))
11692         (message-narrow-to-head)
11693         (message-remove-header "Content-Type")
11694         (goto-char (point-max))
11695         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11696                         separator))
11697         (widen))))
11698   (let (gnus-mark-article-hook)
11699     (gnus-summary-select-article t t nil article)))
11700
11701 (defun gnus-summary-toggle-display-buttonized ()
11702   "Toggle the buttonizing of the article buffer."
11703   (interactive)
11704   (require 'gnus-art)
11705   (if (setq gnus-inhibit-mime-unbuttonizing
11706             (not gnus-inhibit-mime-unbuttonizing))
11707       (let ((gnus-unbuttonized-mime-types nil))
11708         (gnus-summary-show-article))
11709     (gnus-summary-show-article)))
11710
11711 ;;;
11712 ;;; Intelli-mouse commmands
11713 ;;;
11714
11715 (defun gnus-wheel-summary-scroll (event)
11716   (interactive "e")
11717   (let ((amount (if (memq 'shift (event-modifiers event))
11718                     (car gnus-wheel-scroll-amount)
11719                   (cdr gnus-wheel-scroll-amount)))
11720         (direction (- (* (static-if (featurep 'xemacs)
11721                              (event-button event)
11722                            (cond ((eq 'mouse-4 (event-basic-type event))
11723                                   4)
11724                                  ((eq 'mouse-5 (event-basic-type event))
11725                                   5)))
11726                          2) 9))
11727         edge)
11728     (gnus-summary-scroll-up (* amount direction))
11729     (when (gnus-eval-in-buffer-window gnus-article-buffer
11730             (save-restriction
11731               (widen)
11732               (and (if (< 0 direction)
11733                        (gnus-article-next-page 0)
11734                      (gnus-article-prev-page 0)
11735                      (bobp))
11736                    (if (setq edge (get-text-property
11737                                    (point-min) 'gnus-wheel-edge))
11738                        (setq edge (* edge direction))
11739                      (setq edge -1))
11740                    (or (plusp edge)
11741                        (let ((buffer-read-only nil)
11742                              (inhibit-read-only t))
11743                          (put-text-property (point-min) (point-max)
11744                                             'gnus-wheel-edge direction)
11745                          nil))
11746                    (or (> edge gnus-wheel-edge-resistance)
11747                        (let ((buffer-read-only nil)
11748                              (inhibit-read-only t))
11749                          (put-text-property (point-min) (point-max)
11750                                             'gnus-wheel-edge
11751                                             (* (1+ edge) direction))
11752                          nil))
11753                    (eq last-command 'gnus-wheel-summary-scroll))))
11754       (gnus-summary-next-article nil nil (minusp direction)))))
11755
11756 (defun gnus-wheel-install ()
11757   "Enable mouse wheel support on summary window."
11758   (when gnus-use-wheel
11759     (let ((keys
11760            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11761       (dolist (key keys)
11762         (define-key gnus-summary-mode-map key
11763           'gnus-wheel-summary-scroll)))))
11764
11765 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11766
11767 ;;;
11768 ;;; Traditional PGP commmands
11769 ;;;
11770
11771 (defun gnus-summary-decrypt-article (&optional force)
11772   "Decrypt the current article in traditional PGP way.
11773 This will have permanent effect only in mail groups.
11774 If FORCE is non-nil, allow editing of articles even in read-only
11775 groups."
11776   (interactive "P")
11777   (gnus-summary-select-article t)
11778   (gnus-eval-in-buffer-window gnus-article-buffer
11779     (save-excursion
11780       (save-restriction
11781         (widen)
11782         (goto-char (point-min))
11783         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11784           (error "Not a traditional PGP message!"))
11785         (let ((armor-start (match-beginning 0)))
11786           (if (and (pgg-decrypt-region armor-start (point-max))
11787                    (or force (not (gnus-group-read-only-p))))
11788               (let ((inhibit-read-only t)
11789                     buffer-read-only)
11790                 (delete-region armor-start
11791                                (progn
11792                                  (re-search-forward "^-+END PGP" nil t)
11793                                  (beginning-of-line 2)
11794                                  (point)))
11795                 (insert-buffer-substring pgg-output-buffer))))))))
11796
11797 (defun gnus-summary-verify-article ()
11798   "Verify the current article in traditional PGP way."
11799   (interactive)
11800   (save-excursion
11801     (set-buffer gnus-original-article-buffer)
11802     (goto-char (point-min))
11803     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11804       (error "Not a traditional PGP message!"))
11805     (re-search-forward "^-+END PGP" nil t)
11806     (beginning-of-line 2)
11807     (call-interactively (function pgg-verify-region))))
11808
11809 ;;;
11810 ;;; Generic summary marking commands
11811 ;;;
11812
11813 (defvar gnus-summary-marking-alist
11814   '((read gnus-del-mark "d")
11815     (unread gnus-unread-mark "u")
11816     (ticked gnus-ticked-mark "!")
11817     (dormant gnus-dormant-mark "?")
11818     (expirable gnus-expirable-mark "e"))
11819   "An alist of names/marks/keystrokes.")
11820
11821 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11822 (defvar gnus-summary-mark-map)
11823
11824 (defun gnus-summary-make-all-marking-commands ()
11825   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11826   (dolist (elem gnus-summary-marking-alist)
11827     (apply 'gnus-summary-make-marking-command elem)))
11828
11829 (defun gnus-summary-make-marking-command (name mark keystroke)
11830   (let ((map (make-sparse-keymap)))
11831     (define-key gnus-summary-generic-mark-map keystroke map)
11832     (dolist (lway `((next "next" next nil "n")
11833                     (next-unread "next unread" next t "N")
11834                     (prev "previous" prev nil "p")
11835                     (prev-unread "previous unread" prev t "P")
11836                     (nomove "" nil nil ,keystroke)))
11837       (let ((func (gnus-summary-make-marking-command-1
11838                    mark (car lway) lway name)))
11839         (setq func (eval func))
11840         (define-key map (nth 4 lway) func)))))
11841
11842 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11843   `(defun ,(intern
11844             (format "gnus-summary-put-mark-as-%s%s"
11845                     name (if (eq way 'nomove)
11846                              ""
11847                            (concat "-" (symbol-name way)))))
11848      (n)
11849      ,(format
11850        "Mark the current article as %s%s.
11851 If N, the prefix, then repeat N times.
11852 If N is negative, move in reverse order.
11853 The difference between N and the actual number of articles marked is
11854 returned."
11855        name (car (cdr lway)))
11856      (interactive "p")
11857      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11858
11859 (defun gnus-summary-generic-mark (n mark move unread)
11860   "Mark N articles with MARK."
11861   (unless (eq major-mode 'gnus-summary-mode)
11862     (error "This command can only be used in the summary buffer"))
11863   (gnus-summary-show-thread)
11864   (let ((nummove
11865          (cond
11866           ((eq move 'next) 1)
11867           ((eq move 'prev) -1)
11868           (t 0))))
11869     (if (zerop nummove)
11870         (setq n 1)
11871       (when (< n 0)
11872         (setq n (abs n)
11873               nummove (* -1 nummove))))
11874     (while (and (> n 0)
11875                 (gnus-summary-mark-article nil mark)
11876                 (zerop (gnus-summary-next-subject nummove unread t)))
11877       (setq n (1- n)))
11878     (when (/= 0 n)
11879       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11880     (gnus-summary-recenter)
11881     (gnus-summary-position-point)
11882     (gnus-set-mode-line 'summary)
11883     n))
11884
11885 (defun gnus-summary-insert-articles (articles)
11886   (when (setq articles
11887               (gnus-sorted-difference articles
11888                                       (mapcar (lambda (h)
11889                                                 (mail-header-number h))
11890                                               gnus-newsgroup-headers)))
11891     (setq gnus-newsgroup-headers
11892           (gnus-merge 'list
11893                       gnus-newsgroup-headers
11894                       (gnus-fetch-headers articles)
11895                       'gnus-article-sort-by-number))
11896     ;; Suppress duplicates?
11897     (when gnus-suppress-duplicates
11898       (gnus-dup-suppress-articles))
11899
11900     ;; We might want to build some more threads first.
11901     (when (and gnus-fetch-old-headers
11902                (eq gnus-headers-retrieved-by 'nov))
11903       (if (eq gnus-fetch-old-headers 'invisible)
11904           (gnus-build-all-threads)
11905         (gnus-build-old-threads)))
11906     ;; Let the Gnus agent mark articles as read.
11907     (when gnus-agent
11908       (gnus-agent-get-undownloaded-list))
11909     ;; Remove list identifiers from subject
11910     (when gnus-list-identifiers
11911       (gnus-summary-remove-list-identifiers))
11912     ;; First and last article in this newsgroup.
11913     (when gnus-newsgroup-headers
11914       (setq gnus-newsgroup-begin
11915             (mail-header-number (car gnus-newsgroup-headers))
11916             gnus-newsgroup-end
11917             (mail-header-number
11918              (gnus-last-element gnus-newsgroup-headers))))
11919     (when gnus-use-scoring
11920       (gnus-possibly-score-headers))))
11921
11922 (defun gnus-summary-insert-old-articles (&optional all)
11923   "Insert all old articles in this group.
11924 If ALL is non-nil, already read articles become readable.
11925 If ALL is a number, fetch this number of articles."
11926   (interactive "P")
11927   (prog1
11928       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11929             older len)
11930         (setq older
11931               ;; Some nntp servers lie about their active range.  When
11932               ;; this happens, the active range can be in the millions.
11933               ;; Use a compressed range to avoid creating a huge list.
11934               (gnus-range-difference (list gnus-newsgroup-active) old))
11935         (setq len (gnus-range-length older))
11936         (cond
11937          ((null older) nil)
11938          ((numberp all)
11939           (if (< all len)
11940               (let ((older-range (nreverse older)))
11941                 (setq older nil)
11942
11943                 (while (> all 0)
11944                   (let* ((r (pop older-range))
11945                          (min (if (numberp r) r (car r)))
11946                          (max (if (numberp r) r (cdr r))))
11947                     (while (and (<= min max)
11948                                 (> all 0))
11949                       (push max older)
11950                       (setq all (1- all)
11951                             max (1- max))))))
11952             (setq older (gnus-uncompress-range older))))
11953          (all
11954           (setq older (gnus-uncompress-range older)))
11955          (t
11956           (when (and (numberp gnus-large-newsgroup)
11957                    (> len gnus-large-newsgroup))
11958               (let* ((cursor-in-echo-area nil)
11959                      (initial (gnus-parameter-large-newsgroup-initial
11960                                gnus-newsgroup-name))
11961                      (input
11962                       (read-string
11963                        (format
11964                         "How many articles from %s (%s %d): "
11965                         (gnus-limit-string
11966                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11967                         (if initial "max" "default")
11968                         len)
11969                        (if initial
11970                            (cons (number-to-string initial)
11971                                  0)))))
11972                 (unless (string-match "^[ \t]*$" input)
11973                   (setq all (string-to-number input))
11974                   (if (< all len)
11975                       (let ((older-range (nreverse older)))
11976                         (setq older nil)
11977
11978                         (while (> all 0)
11979                           (let* ((r (pop older-range))
11980                                  (min (if (numberp r) r (car r)))
11981                                  (max (if (numberp r) r (cdr r))))
11982                             (while (and (<= min max)
11983                                         (> all 0))
11984                               (push max older)
11985                               (setq all (1- all)
11986                                     max (1- max))))))))))
11987           (setq older (gnus-uncompress-range older))))
11988         (if (not older)
11989             (message "No old news.")
11990           (gnus-summary-insert-articles older)
11991           (gnus-summary-limit (gnus-sorted-nunion old older))))
11992     (gnus-summary-position-point)))
11993
11994 (defun gnus-summary-insert-new-articles ()
11995   "Insert all new articles in this group."
11996   (interactive)
11997   (prog1
11998       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11999             (old-active gnus-newsgroup-active)
12000             (nnmail-fetched-sources (list t))
12001             i new)
12002         (setq gnus-newsgroup-active
12003               (gnus-activate-group gnus-newsgroup-name 'scan))
12004         (setq i (cdr gnus-newsgroup-active))
12005         (while (> i (cdr old-active))
12006           (push i new)
12007           (decf i))
12008         (if (not new)
12009             (message "No gnus is bad news")
12010           (gnus-summary-insert-articles new)
12011           (setq gnus-newsgroup-unreads
12012                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12013           (gnus-summary-limit (gnus-sorted-nunion old new))))
12014     (gnus-summary-position-point)))
12015
12016 (gnus-summary-make-all-marking-commands)
12017
12018 (gnus-ems-redefine)
12019
12020 (provide 'gnus-sum)
12021
12022 (run-hooks 'gnus-sum-load-hook)
12023
12024 ;; Local Variables:
12025 ;; coding: iso-8859-1
12026 ;; End:
12027
12028 ;;; gnus-sum.el ends here