Synch to No Gnus 200402120656.
[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     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1216     (?U gnus-tmp-unread ?c)
1217     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1218         ?s)
1219     (?t (gnus-summary-number-of-articles-in-thread
1220          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1221         ?d)
1222     (?e (gnus-summary-number-of-articles-in-thread
1223          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1224         ?c)
1225     (?u gnus-tmp-user-defined ?s)
1226     (?P (gnus-pick-line-number) ?d)
1227     (?B gnus-tmp-thread-tree-header-string ?s)
1228     (user-date (gnus-user-date
1229                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1230   "An alist of format specifications that can appear in summary lines.
1231 These are paired with what variables they correspond with, along with
1232 the type of the variable (string, integer, character, etc).")
1233
1234 (defvar gnus-summary-dummy-line-format-alist
1235   `((?S gnus-tmp-subject ?s)
1236     (?N gnus-tmp-number ?d)
1237     (?u gnus-tmp-user-defined ?s)))
1238
1239 (defvar gnus-summary-mode-line-format-alist
1240   `((?G gnus-tmp-group-name ?s)
1241     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1242     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1243     (?A gnus-tmp-article-number ?d)
1244     (?Z gnus-tmp-unread-and-unselected ?s)
1245     (?V gnus-version ?s)
1246     (?U gnus-tmp-unread-and-unticked ?d)
1247     (?S gnus-tmp-subject ?s)
1248     (?e gnus-tmp-unselected ?d)
1249     (?u gnus-tmp-user-defined ?s)
1250     (?d (length gnus-newsgroup-dormant) ?d)
1251     (?t (length gnus-newsgroup-marked) ?d)
1252     (?h (length gnus-newsgroup-spam-marked) ?d)
1253     (?r (length gnus-newsgroup-reads) ?d)
1254     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1255     (?E gnus-newsgroup-expunged-tally ?d)
1256     (?s (gnus-current-score-file-nondirectory) ?s)))
1257
1258 (defvar gnus-last-search-regexp nil
1259   "Default regexp for article search command.")
1260
1261 (defvar gnus-summary-search-article-matched-data nil
1262   "Last matched data of article search command.  It is the local variable
1263 in `gnus-article-buffer' which consists of the list of start position,
1264 end position and text.")
1265
1266 (defvar gnus-last-shell-command nil
1267   "Default shell command on article.")
1268
1269 (defvar gnus-newsgroup-agentized nil
1270   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1271 (defvar gnus-newsgroup-begin nil)
1272 (defvar gnus-newsgroup-end nil)
1273 (defvar gnus-newsgroup-last-rmail nil)
1274 (defvar gnus-newsgroup-last-mail nil)
1275 (defvar gnus-newsgroup-last-folder nil)
1276 (defvar gnus-newsgroup-last-file nil)
1277 (defvar gnus-newsgroup-auto-expire nil)
1278 (defvar gnus-newsgroup-active nil)
1279
1280 (defvar gnus-newsgroup-data nil)
1281 (defvar gnus-newsgroup-data-reverse nil)
1282 (defvar gnus-newsgroup-limit nil)
1283 (defvar gnus-newsgroup-limits nil)
1284 (defvar gnus-summary-use-undownloaded-faces nil)
1285
1286 (defvar gnus-newsgroup-unreads nil
1287   "Sorted list of unread articles in the current newsgroup.")
1288
1289 (defvar gnus-newsgroup-unselected nil
1290   "Sorted list of unselected unread articles in the current newsgroup.")
1291
1292 (defvar gnus-newsgroup-reads nil
1293   "Alist of read articles and article marks in the current newsgroup.")
1294
1295 (defvar gnus-newsgroup-expunged-tally nil)
1296
1297 (defvar gnus-newsgroup-marked nil
1298   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1299
1300 (defvar gnus-newsgroup-spam-marked nil
1301   "List of ranges of articles that have been marked as spam.")
1302
1303 (defvar gnus-newsgroup-killed nil
1304   "List of ranges of articles that have been through the scoring process.")
1305
1306 (defvar gnus-newsgroup-cached nil
1307   "Sorted list of articles that come from the article cache.")
1308
1309 (defvar gnus-newsgroup-saved nil
1310   "List of articles that have been saved.")
1311
1312 (defvar gnus-newsgroup-kill-headers nil)
1313
1314 (defvar gnus-newsgroup-replied nil
1315   "List of articles that have been replied to in the current newsgroup.")
1316
1317 (defvar gnus-newsgroup-forwarded nil
1318   "List of articles that have been forwarded in the current newsgroup.")
1319
1320 (defvar gnus-newsgroup-recent nil
1321   "List of articles that have are recent in the current newsgroup.")
1322
1323 (defvar gnus-newsgroup-expirable nil
1324   "Sorted list of articles in the current newsgroup that can be expired.")
1325
1326 (defvar gnus-newsgroup-processable nil
1327   "List of articles in the current newsgroup that can be processed.")
1328
1329 (defvar gnus-newsgroup-downloadable nil
1330   "Sorted list of articles in the current newsgroup that can be processed.")
1331
1332 (defvar gnus-newsgroup-unfetched nil
1333   "Sorted list of articles in the current newsgroup whose headers have
1334 not been fetched into the agent.
1335
1336 This list will always be a subset of gnus-newsgroup-undownloaded.")
1337
1338 (defvar gnus-newsgroup-undownloaded nil
1339   "List of articles in the current newsgroup that haven't been downloaded.")
1340
1341 (defvar gnus-newsgroup-unsendable nil
1342   "List of articles in the current newsgroup that won't be sent.")
1343
1344 (defvar gnus-newsgroup-bookmarks nil
1345   "List of articles in the current newsgroup that have bookmarks.")
1346
1347 (defvar gnus-newsgroup-dormant nil
1348   "Sorted list of dormant articles in the current newsgroup.")
1349
1350 (defvar gnus-newsgroup-unseen nil
1351   "List of unseen articles in the current newsgroup.")
1352
1353 (defvar gnus-newsgroup-seen nil
1354   "Range of seen articles in the current newsgroup.")
1355
1356 (defvar gnus-newsgroup-articles nil
1357   "List of articles in the current newsgroup.")
1358
1359 (defvar gnus-newsgroup-scored nil
1360   "List of scored articles in the current newsgroup.")
1361
1362 (defvar gnus-newsgroup-incorporated nil
1363   "List of incorporated articles in the current newsgroup.")
1364
1365 (defvar gnus-newsgroup-headers nil
1366   "List of article headers in the current newsgroup.")
1367
1368 (defvar gnus-newsgroup-threads nil)
1369
1370 (defvar gnus-newsgroup-prepared nil
1371   "Whether the current group has been prepared properly.")
1372
1373 (defvar gnus-newsgroup-ancient nil
1374   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1375
1376 (defvar gnus-newsgroup-sparse nil)
1377
1378 (defvar gnus-current-article nil)
1379 (defvar gnus-article-current nil)
1380 (defvar gnus-current-headers nil)
1381 (defvar gnus-have-all-headers nil)
1382 (defvar gnus-last-article nil)
1383 (defvar gnus-newsgroup-history nil)
1384 (defvar gnus-newsgroup-charset nil)
1385 (defvar gnus-newsgroup-ephemeral-charset nil)
1386 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1387
1388 (defvar gnus-article-before-search nil)
1389
1390 (defvar gnus-summary-local-variables
1391   '(gnus-newsgroup-name
1392     gnus-newsgroup-begin gnus-newsgroup-end
1393     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1394     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1395     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1396     gnus-newsgroup-unselected gnus-newsgroup-marked
1397     gnus-newsgroup-spam-marked
1398     gnus-newsgroup-reads gnus-newsgroup-saved
1399     gnus-newsgroup-replied gnus-newsgroup-forwarded
1400     gnus-newsgroup-recent
1401     gnus-newsgroup-expirable
1402     gnus-newsgroup-processable gnus-newsgroup-killed
1403     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1404     gnus-newsgroup-unfetched
1405     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1406     gnus-newsgroup-seen gnus-newsgroup-articles
1407     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1408     gnus-newsgroup-headers gnus-newsgroup-threads
1409     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1410     gnus-current-article gnus-current-headers gnus-have-all-headers
1411     gnus-last-article gnus-article-internal-prepare-hook
1412     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1413     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1414     gnus-thread-expunge-below
1415     gnus-score-alist gnus-current-score-file
1416     (gnus-summary-expunge-below . global)
1417     (gnus-summary-mark-below . global)
1418     (gnus-orphan-score . global)
1419     gnus-newsgroup-active gnus-scores-exclude-files
1420     gnus-newsgroup-history gnus-newsgroup-ancient
1421     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1422     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1423     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1424     (gnus-newsgroup-expunged-tally . 0)
1425     gnus-cache-removable-articles gnus-newsgroup-cached
1426     gnus-newsgroup-data gnus-newsgroup-data-reverse
1427     gnus-newsgroup-limit gnus-newsgroup-limits
1428     gnus-newsgroup-charset gnus-newsgroup-display
1429     gnus-summary-use-undownloaded-faces
1430     gnus-newsgroup-incorporated)
1431   "Variables that are buffer-local to the summary buffers.")
1432
1433 (defvar gnus-newsgroup-variables nil
1434   "A list of variables that have separate values in different newsgroups.
1435 A list of newsgroup (summary buffer) local variables, or cons of
1436 variables and their default values (when the default values are not
1437 nil), that should be made global while the summary buffer is active.
1438 These variables can be used to set variables in the group parameters
1439 while still allowing them to affect operations done in other
1440 buffers. For example:
1441
1442 \(setq gnus-newsgroup-variables
1443      '(message-use-followup-to
1444        (gnus-visible-headers .
1445          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1446 ")
1447
1448 ;; Byte-compiler warning.
1449 (eval-when-compile
1450   ;; Bind features so that require will believe that gnus-sum has
1451   ;; already been loaded (avoids infinite recursion)
1452   (let ((features (cons 'gnus-sum features)))
1453     ;; Several of the declarations in gnus-sum are needed to load the
1454     ;; following files. Right now, these definitions have been
1455     ;; compiled but not defined (evaluated).  We could either do a
1456     ;; eval-and-compile about all of the declarations or evaluate the
1457     ;; source file.
1458     (if (boundp 'gnus-newsgroup-variables)
1459         nil
1460       (load "gnus-sum.el" t t t))
1461     (require 'gnus)
1462     (require 'gnus-agent)
1463     (require 'gnus-art)))
1464
1465 ;; Subject simplification.
1466
1467 (defun gnus-simplify-whitespace (str)
1468   "Remove excessive whitespace from STR."
1469   ;; Multiple spaces.
1470   (while (string-match "[ \t][ \t]+" str)
1471     (setq str (concat (substring str 0 (match-beginning 0))
1472                         " "
1473                         (substring str (match-end 0)))))
1474   ;; Leading spaces.
1475   (when (string-match "^[ \t]+" str)
1476     (setq str (substring str (match-end 0))))
1477   ;; Trailing spaces.
1478   (when (string-match "[ \t]+$" str)
1479     (setq str (substring str 0 (match-beginning 0))))
1480   str)
1481
1482 (defun gnus-simplify-all-whitespace (str)
1483   "Remove all whitespace from STR."
1484   (while (string-match "[ \t\n]+" str)
1485     (setq str (replace-match "" nil nil str)))
1486   str)
1487
1488 (defsubst gnus-simplify-subject-re (subject)
1489   "Remove \"Re:\" from subject lines."
1490   (if (string-match message-subject-re-regexp subject)
1491       (substring subject (match-end 0))
1492     subject))
1493
1494 (defun gnus-simplify-subject (subject &optional re-only)
1495   "Remove `Re:' and words in parentheses.
1496 If RE-ONLY is non-nil, strip leading `Re:'s only."
1497   (let ((case-fold-search t))           ;Ignore case.
1498     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1499     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1500       (setq subject (substring subject (match-end 0))))
1501     ;; Remove uninteresting prefixes.
1502     (when (and (not re-only)
1503                gnus-simplify-ignored-prefixes
1504                (string-match gnus-simplify-ignored-prefixes subject))
1505       (setq subject (substring subject (match-end 0))))
1506     ;; Remove words in parentheses from end.
1507     (unless re-only
1508       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1509         (setq subject (substring subject 0 (match-beginning 0)))))
1510     ;; Return subject string.
1511     subject))
1512
1513 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1514 ;; all whitespace.
1515 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1516   (goto-char (point-min))
1517   (while (re-search-forward regexp nil t)
1518     (replace-match (or newtext ""))))
1519
1520 (defun gnus-simplify-buffer-fuzzy ()
1521   "Simplify string in the buffer fuzzily.
1522 The string in the accessible portion of the current buffer is simplified.
1523 It is assumed to be a single-line subject.
1524 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1525 matter is removed.  Additional things can be deleted by setting
1526 `gnus-simplify-subject-fuzzy-regexp'."
1527   (let ((case-fold-search t)
1528         (modified-tick))
1529     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1530
1531     (while (not (eq modified-tick (buffer-modified-tick)))
1532       (setq modified-tick (buffer-modified-tick))
1533       (cond
1534        ((listp gnus-simplify-subject-fuzzy-regexp)
1535         (mapcar 'gnus-simplify-buffer-fuzzy-step
1536                 gnus-simplify-subject-fuzzy-regexp))
1537        (gnus-simplify-subject-fuzzy-regexp
1538         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1539       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1540       (gnus-simplify-buffer-fuzzy-step
1541        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1542       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1543
1544     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1545     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1546     (gnus-simplify-buffer-fuzzy-step " $")
1547     (gnus-simplify-buffer-fuzzy-step "^ +")))
1548
1549 (defun gnus-simplify-subject-fuzzy (subject)
1550   "Simplify a subject string fuzzily.
1551 See `gnus-simplify-buffer-fuzzy' for details."
1552   (save-excursion
1553     (gnus-set-work-buffer)
1554     (let ((case-fold-search t))
1555       ;; Remove uninteresting prefixes.
1556       (when (and gnus-simplify-ignored-prefixes
1557                  (string-match gnus-simplify-ignored-prefixes subject))
1558         (setq subject (substring subject (match-end 0))))
1559       (insert subject)
1560       (inline (gnus-simplify-buffer-fuzzy))
1561       (buffer-string))))
1562
1563 (defsubst gnus-simplify-subject-fully (subject)
1564   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1565   (cond
1566    (gnus-simplify-subject-functions
1567     (gnus-map-function gnus-simplify-subject-functions subject))
1568    ((null gnus-summary-gather-subject-limit)
1569     (gnus-simplify-subject-re subject))
1570    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1571     (gnus-simplify-subject-fuzzy subject))
1572    ((numberp gnus-summary-gather-subject-limit)
1573     (gnus-limit-string (gnus-simplify-subject-re subject)
1574                        gnus-summary-gather-subject-limit))
1575    (t
1576     subject)))
1577
1578 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1579   "Check whether two subjects are equal.
1580 If optional argument SIMPLE-FIRST is t, first argument is already
1581 simplified."
1582   (cond
1583    ((null simple-first)
1584     (equal (gnus-simplify-subject-fully s1)
1585            (gnus-simplify-subject-fully s2)))
1586    (t
1587     (equal s1
1588            (gnus-simplify-subject-fully s2)))))
1589
1590 (defun gnus-summary-bubble-group ()
1591   "Increase the score of the current group.
1592 This is a handy function to add to `gnus-summary-exit-hook' to
1593 increase the score of each group you read."
1594   (gnus-group-add-score gnus-newsgroup-name))
1595
1596 \f
1597 ;;;
1598 ;;; Gnus summary mode
1599 ;;;
1600
1601 (put 'gnus-summary-mode 'mode-class 'special)
1602
1603 (defvar gnus-article-commands-menu)
1604
1605 ;; Non-orthogonal keys
1606
1607 (gnus-define-keys gnus-summary-mode-map
1608   " " gnus-summary-next-page
1609   "\177" gnus-summary-prev-page
1610   [delete] gnus-summary-prev-page
1611   [backspace] gnus-summary-prev-page
1612   "\r" gnus-summary-scroll-up
1613   "\M-\r" gnus-summary-scroll-down
1614   "n" gnus-summary-next-unread-article
1615   "p" gnus-summary-prev-unread-article
1616   "N" gnus-summary-next-article
1617   "P" gnus-summary-prev-article
1618   "\M-\C-n" gnus-summary-next-same-subject
1619   "\M-\C-p" gnus-summary-prev-same-subject
1620   "\M-n" gnus-summary-next-unread-subject
1621   "\M-p" gnus-summary-prev-unread-subject
1622   "." gnus-summary-first-unread-article
1623   "," gnus-summary-best-unread-article
1624   "\M-s" gnus-summary-search-article-forward
1625   "\M-r" gnus-summary-search-article-backward
1626   "<" gnus-summary-beginning-of-article
1627   ">" gnus-summary-end-of-article
1628   "j" gnus-summary-goto-article
1629   "^" gnus-summary-refer-parent-article
1630   "\M-^" gnus-summary-refer-article
1631   "u" gnus-summary-tick-article-forward
1632   "!" gnus-summary-tick-article-forward
1633   "U" gnus-summary-tick-article-backward
1634   "d" gnus-summary-mark-as-read-forward
1635   "D" gnus-summary-mark-as-read-backward
1636   "E" gnus-summary-mark-as-expirable
1637   "\M-u" gnus-summary-clear-mark-forward
1638   "\M-U" gnus-summary-clear-mark-backward
1639   "k" gnus-summary-kill-same-subject-and-select
1640   "\C-k" gnus-summary-kill-same-subject
1641   "\M-\C-k" gnus-summary-kill-thread
1642   "\M-\C-l" gnus-summary-lower-thread
1643   "e" gnus-summary-edit-article
1644   "#" gnus-summary-mark-as-processable
1645   "\M-#" gnus-summary-unmark-as-processable
1646   "\M-\C-t" gnus-summary-toggle-threads
1647   "\M-\C-s" gnus-summary-show-thread
1648   "\M-\C-h" gnus-summary-hide-thread
1649   "\M-\C-f" gnus-summary-next-thread
1650   "\M-\C-b" gnus-summary-prev-thread
1651   [(meta down)] gnus-summary-next-thread
1652   [(meta up)] gnus-summary-prev-thread
1653   "\M-\C-u" gnus-summary-up-thread
1654   "\M-\C-d" gnus-summary-down-thread
1655   "&" gnus-summary-execute-command
1656   "c" gnus-summary-catchup-and-exit
1657   "\C-w" gnus-summary-mark-region-as-read
1658   "\C-t" gnus-summary-toggle-truncation
1659   "?" gnus-summary-mark-as-dormant
1660   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1661   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1662   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1663   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1664   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1665   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1666   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1667   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1668   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1669   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1670   "=" gnus-summary-expand-window
1671   "\C-x\C-s" gnus-summary-reselect-current-group
1672   "\M-g" gnus-summary-rescan-group
1673   "w" gnus-summary-stop-page-breaking
1674   "\C-c\C-r" gnus-summary-caesar-message
1675   "\M-t" gnus-summary-toggle-mime
1676   "f" gnus-summary-followup
1677   "F" gnus-summary-followup-with-original
1678   "C" gnus-summary-cancel-article
1679   "r" gnus-summary-reply
1680   "R" gnus-summary-reply-with-original
1681   "\C-c\C-f" gnus-summary-mail-forward
1682   "o" gnus-summary-save-article
1683   "\C-o" gnus-summary-save-article-mail
1684   "|" gnus-summary-pipe-output
1685   "\M-k" gnus-summary-edit-local-kill
1686   "\M-K" gnus-summary-edit-global-kill
1687   ;; "V" gnus-version
1688   "\C-c\C-d" gnus-summary-describe-group
1689   "q" gnus-summary-exit
1690   "Q" gnus-summary-exit-no-update
1691   "\C-c\C-i" gnus-info-find-node
1692   gnus-mouse-2 gnus-mouse-pick-article
1693   "m" gnus-summary-mail-other-window
1694   "a" gnus-summary-post-news
1695   "i" gnus-summary-news-other-window
1696   "x" gnus-summary-limit-to-unread
1697   "s" gnus-summary-isearch-article
1698   "t" gnus-summary-toggle-header
1699   "g" gnus-summary-show-article
1700   "l" gnus-summary-goto-last-article
1701   "v" gnus-summary-preview-mime-message
1702   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1703   "\C-d" gnus-summary-enter-digest-group
1704   "\M-\C-d" gnus-summary-read-document
1705   "\M-\C-e" gnus-summary-edit-parameters
1706   "\M-\C-a" gnus-summary-customize-parameters
1707   "\C-c\C-b" gnus-bug
1708   "\C-c\C-n" gnus-namazu-search
1709   "*" gnus-cache-enter-article
1710   "\M-*" gnus-cache-remove-article
1711   "\M-&" gnus-summary-universal-argument
1712   "\C-l" gnus-recenter
1713   "I" gnus-summary-increase-score
1714   "L" gnus-summary-lower-score
1715   "\M-i" gnus-symbolic-argument
1716   "h" gnus-summary-select-article-buffer
1717
1718   "V" gnus-summary-score-map
1719   "X" gnus-uu-extract-map
1720   "S" gnus-summary-send-map)
1721
1722   ;; Sort of orthogonal keymap
1723 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1724   "t" gnus-summary-tick-article-forward
1725   "!" gnus-summary-tick-article-forward
1726   "d" gnus-summary-mark-as-read-forward
1727   "r" gnus-summary-mark-as-read-forward
1728   "c" gnus-summary-clear-mark-forward
1729   " " gnus-summary-clear-mark-forward
1730   "e" gnus-summary-mark-as-expirable
1731   "x" gnus-summary-mark-as-expirable
1732   "?" gnus-summary-mark-as-dormant
1733   "b" gnus-summary-set-bookmark
1734   "B" gnus-summary-remove-bookmark
1735   "#" gnus-summary-mark-as-processable
1736   "\M-#" gnus-summary-unmark-as-processable
1737   "S" gnus-summary-limit-include-expunged
1738   "C" gnus-summary-catchup
1739   "H" gnus-summary-catchup-to-here
1740   "h" gnus-summary-catchup-from-here
1741   "\C-c" gnus-summary-catchup-all
1742   "k" gnus-summary-kill-same-subject-and-select
1743   "K" gnus-summary-kill-same-subject
1744   "P" gnus-uu-mark-map)
1745
1746 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1747   "c" gnus-summary-clear-above
1748   "u" gnus-summary-tick-above
1749   "m" gnus-summary-mark-above
1750   "k" gnus-summary-kill-below)
1751
1752 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1753   "/" gnus-summary-limit-to-subject
1754   "n" gnus-summary-limit-to-articles
1755   "w" gnus-summary-pop-limit
1756   "s" gnus-summary-limit-to-subject
1757   "a" gnus-summary-limit-to-author
1758   "u" gnus-summary-limit-to-unread
1759   "m" gnus-summary-limit-to-marks
1760   "M" gnus-summary-limit-exclude-marks
1761   "v" gnus-summary-limit-to-score
1762   "*" gnus-summary-limit-include-cached
1763   "D" gnus-summary-limit-include-dormant
1764   "T" gnus-summary-limit-include-thread
1765   "d" gnus-summary-limit-exclude-dormant
1766   "t" gnus-summary-limit-to-age
1767   "." gnus-summary-limit-to-unseen
1768   "x" gnus-summary-limit-to-extra
1769   "p" gnus-summary-limit-to-display-predicate
1770   "E" gnus-summary-limit-include-expunged
1771   "c" gnus-summary-limit-exclude-childless-dormant
1772   "C" gnus-summary-limit-mark-excluded-as-read
1773   "o" gnus-summary-insert-old-articles
1774   "N" gnus-summary-insert-new-articles
1775   "r" gnus-summary-limit-to-replied)
1776
1777 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1778   "n" gnus-summary-next-unread-article
1779   "p" gnus-summary-prev-unread-article
1780   "N" gnus-summary-next-article
1781   "P" gnus-summary-prev-article
1782   "\C-n" gnus-summary-next-same-subject
1783   "\C-p" gnus-summary-prev-same-subject
1784   "\M-n" gnus-summary-next-unread-subject
1785   "\M-p" gnus-summary-prev-unread-subject
1786   "f" gnus-summary-first-unread-article
1787   "b" gnus-summary-best-unread-article
1788   "j" gnus-summary-goto-article
1789   "g" gnus-summary-goto-subject
1790   "l" gnus-summary-goto-last-article
1791   "o" gnus-summary-pop-article)
1792
1793 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1794   "k" gnus-summary-kill-thread
1795   "l" gnus-summary-lower-thread
1796   "i" gnus-summary-raise-thread
1797   "T" gnus-summary-toggle-threads
1798   "t" gnus-summary-rethread-current
1799   "^" gnus-summary-reparent-thread
1800   "s" gnus-summary-show-thread
1801   "S" gnus-summary-show-all-threads
1802   "h" gnus-summary-hide-thread
1803   "H" gnus-summary-hide-all-threads
1804   "n" gnus-summary-next-thread
1805   "p" gnus-summary-prev-thread
1806   "u" gnus-summary-up-thread
1807   "o" gnus-summary-top-thread
1808   "d" gnus-summary-down-thread
1809   "#" gnus-uu-mark-thread
1810   "\M-#" gnus-uu-unmark-thread)
1811
1812 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1813   "g" gnus-summary-prepare
1814   "c" gnus-summary-insert-cached-articles
1815   "d" gnus-summary-insert-dormant-articles)
1816
1817 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1818   "c" gnus-summary-catchup-and-exit
1819   "C" gnus-summary-catchup-all-and-exit
1820   "E" gnus-summary-exit-no-update
1821   "J" gnus-summary-jump-to-other-group
1822   "Q" gnus-summary-exit
1823   "Z" gnus-summary-exit
1824   "n" gnus-summary-catchup-and-goto-next-group
1825   "R" gnus-summary-reselect-current-group
1826   "G" gnus-summary-rescan-group
1827   "N" gnus-summary-next-group
1828   "s" gnus-summary-save-newsrc
1829   "P" gnus-summary-prev-group)
1830
1831 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1832   " " gnus-summary-next-page
1833   "n" gnus-summary-next-page
1834   "\177" gnus-summary-prev-page
1835   [delete] gnus-summary-prev-page
1836   "p" gnus-summary-prev-page
1837   "\r" gnus-summary-scroll-up
1838   "\M-\r" gnus-summary-scroll-down
1839   "<" gnus-summary-beginning-of-article
1840   ">" gnus-summary-end-of-article
1841   "b" gnus-summary-beginning-of-article
1842   "e" gnus-summary-end-of-article
1843   "^" gnus-summary-refer-parent-article
1844   "r" gnus-summary-refer-parent-article
1845   "D" gnus-summary-enter-digest-group
1846   "R" gnus-summary-refer-references
1847   "T" gnus-summary-refer-thread
1848   "g" gnus-summary-show-article
1849   "s" gnus-summary-isearch-article
1850   "P" gnus-summary-print-article
1851   "M" gnus-mailing-list-insinuate
1852   "t" gnus-article-babel)
1853
1854 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1855   "b" gnus-article-add-buttons
1856   "B" gnus-article-add-buttons-to-head
1857   "o" gnus-article-treat-overstrike
1858   "e" gnus-article-emphasize
1859   "w" gnus-article-fill-cited-article
1860   "Q" gnus-article-fill-long-lines
1861   "C" gnus-article-capitalize-sentences
1862   "c" gnus-article-remove-cr
1863   "Z" gnus-article-decode-HZ
1864   "A" gnus-article-treat-ansi-sequences
1865   "h" gnus-article-wash-html
1866   "u" gnus-article-unsplit-urls
1867   "f" gnus-article-display-x-face
1868   "l" gnus-summary-stop-page-breaking
1869   "r" gnus-summary-caesar-message
1870   "m" gnus-summary-morse-message
1871   "t" gnus-summary-toggle-header
1872   "g" gnus-treat-smiley
1873   "v" gnus-summary-verbose-headers
1874   "m" gnus-summary-toggle-mime
1875   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1876   "p" gnus-article-verify-x-pgp-sig
1877   "d" gnus-article-treat-dumbquotes)
1878
1879 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1880   ;; mnemonic: deuglif*Y*
1881   "u" gnus-article-outlook-unwrap-lines
1882   "a" gnus-article-outlook-repair-attribution
1883   "c" gnus-article-outlook-rearrange-citation
1884   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1885
1886 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1887   "a" gnus-article-hide
1888   "h" gnus-article-hide-headers
1889   "b" gnus-article-hide-boring-headers
1890   "s" gnus-article-hide-signature
1891   "c" gnus-article-hide-citation
1892   "C" gnus-article-hide-citation-in-followups
1893   "l" gnus-article-hide-list-identifiers
1894   "B" gnus-article-strip-banner
1895   "P" gnus-article-hide-pem
1896   "\C-c" gnus-article-hide-citation-maybe)
1897
1898 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1899   "a" gnus-article-highlight
1900   "h" gnus-article-highlight-headers
1901   "c" gnus-article-highlight-citation
1902   "s" gnus-article-highlight-signature)
1903
1904 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1905   "f" gnus-article-treat-fold-headers
1906   "u" gnus-article-treat-unfold-headers
1907   "n" gnus-article-treat-fold-newsgroups)
1908
1909 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1910   "x" gnus-article-display-x-face
1911   "d" gnus-article-display-face
1912   "s" gnus-treat-smiley
1913   "D" gnus-article-remove-images
1914   "f" gnus-treat-from-picon
1915   "m" gnus-treat-mail-picon
1916   "n" gnus-treat-newsgroups-picon)
1917
1918 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1919   "z" gnus-article-date-ut
1920   "u" gnus-article-date-ut
1921   "l" gnus-article-date-local
1922   "p" gnus-article-date-english
1923   "e" gnus-article-date-lapsed
1924   "o" gnus-article-date-original
1925   "i" gnus-article-date-iso8601
1926   "s" gnus-article-date-user)
1927
1928 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1929   "t" gnus-article-remove-trailing-blank-lines
1930   "l" gnus-article-strip-leading-blank-lines
1931   "m" gnus-article-strip-multiple-blank-lines
1932   "a" gnus-article-strip-blank-lines
1933   "A" gnus-article-strip-all-blank-lines
1934   "s" gnus-article-strip-leading-space
1935   "e" gnus-article-strip-trailing-space
1936   "w" gnus-article-remove-leading-whitespace)
1937
1938 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1939   "v" gnus-version
1940   "f" gnus-summary-fetch-faq
1941   "d" gnus-summary-describe-group
1942   "h" gnus-summary-describe-briefly
1943   "i" gnus-info-find-node
1944   "c" gnus-group-fetch-charter
1945   "C" gnus-group-fetch-control)
1946
1947 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1948   "e" gnus-summary-expire-articles
1949   "\M-\C-e" gnus-summary-expire-articles-now
1950   "\177" gnus-summary-delete-article
1951   [delete] gnus-summary-delete-article
1952   [backspace] gnus-summary-delete-article
1953   "m" gnus-summary-move-article
1954   "r" gnus-summary-respool-article
1955   "w" gnus-summary-edit-article
1956   "c" gnus-summary-copy-article
1957   "B" gnus-summary-crosspost-article
1958   "q" gnus-summary-respool-query
1959   "t" gnus-summary-respool-trace
1960   "i" gnus-summary-import-article
1961   "I" gnus-summary-create-article
1962   "p" gnus-summary-article-posted-p)
1963
1964 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1965   "o" gnus-summary-save-article
1966   "m" gnus-summary-save-article-mail
1967   "F" gnus-summary-write-article-file
1968   "r" gnus-summary-save-article-rmail
1969   "f" gnus-summary-save-article-file
1970   "b" gnus-summary-save-article-body-file
1971   "h" gnus-summary-save-article-folder
1972   "v" gnus-summary-save-article-vm
1973   "p" gnus-summary-pipe-output
1974   "P" gnus-summary-muttprint
1975   "s" gnus-soup-add-article)
1976
1977 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1978   "b" gnus-summary-display-buttonized
1979   "m" gnus-summary-repair-multipart
1980   "v" gnus-article-view-part
1981   "o" gnus-article-save-part
1982   "c" gnus-article-copy-part
1983   "C" gnus-article-view-part-as-charset
1984   "e" gnus-article-view-part-externally
1985   "E" gnus-article-encrypt-body
1986   "i" gnus-article-inline-part
1987   "|" gnus-article-pipe-part)
1988
1989 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1990   "p" gnus-summary-mark-as-processable
1991   "u" gnus-summary-unmark-as-processable
1992   "U" gnus-summary-unmark-all-processable
1993   "v" gnus-uu-mark-over
1994   "s" gnus-uu-mark-series
1995   "r" gnus-uu-mark-region
1996   "g" gnus-uu-unmark-region
1997   "R" gnus-uu-mark-by-regexp
1998   "G" gnus-uu-unmark-by-regexp
1999   "t" gnus-uu-mark-thread
2000   "T" gnus-uu-unmark-thread
2001   "a" gnus-uu-mark-all
2002   "b" gnus-uu-mark-buffer
2003   "S" gnus-uu-mark-sparse
2004   "k" gnus-summary-kill-process-mark
2005   "y" gnus-summary-yank-process-mark
2006   "w" gnus-summary-save-process-mark
2007   "i" gnus-uu-invert-processable)
2008
2009 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2010   ;;"x" gnus-uu-extract-any
2011   "m" gnus-summary-save-parts
2012   "u" gnus-uu-decode-uu
2013   "U" gnus-uu-decode-uu-and-save
2014   "s" gnus-uu-decode-unshar
2015   "S" gnus-uu-decode-unshar-and-save
2016   "o" gnus-uu-decode-save
2017   "O" gnus-uu-decode-save
2018   "b" gnus-uu-decode-binhex
2019   "B" gnus-uu-decode-binhex
2020   "p" gnus-uu-decode-postscript
2021   "P" gnus-uu-decode-postscript-and-save)
2022
2023 (gnus-define-keys
2024     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2025   "u" gnus-uu-decode-uu-view
2026   "U" gnus-uu-decode-uu-and-save-view
2027   "s" gnus-uu-decode-unshar-view
2028   "S" gnus-uu-decode-unshar-and-save-view
2029   "o" gnus-uu-decode-save-view
2030   "O" gnus-uu-decode-save-view
2031   "b" gnus-uu-decode-binhex-view
2032   "B" gnus-uu-decode-binhex-view
2033   "p" gnus-uu-decode-postscript-view
2034   "P" gnus-uu-decode-postscript-and-save-view)
2035
2036 (defvar gnus-article-post-menu nil)
2037
2038 (defconst gnus-summary-menu-maxlen 20)
2039
2040 (defun gnus-summary-menu-split (menu)
2041   ;; If we have lots of elements, divide them into groups of 20
2042   ;; and make a pane (or submenu) for each one.
2043   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2044       (let ((menu menu) sublists next
2045             (i 1))
2046         (while menu
2047           ;; Pull off the next gnus-summary-menu-maxlen elements
2048           ;; and make them the next element of sublist.
2049           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2050           (if next
2051               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2052                       nil))
2053           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2054                                              (aref (car (last menu)) 0)) menu)
2055                                sublists))
2056           (setq i (1+ i))
2057           (setq menu next))
2058         (nreverse sublists))
2059     ;; Few elements--put them all in one pane.
2060     menu))
2061
2062 (defun gnus-summary-make-menu-bar ()
2063   (gnus-turn-off-edit-menu 'summary)
2064
2065   (unless (boundp 'gnus-summary-misc-menu)
2066
2067     (easy-menu-define
2068      gnus-summary-kill-menu gnus-summary-mode-map ""
2069      (cons
2070       "Score"
2071       (nconc
2072        (list
2073         ["Customize" gnus-score-customize t])
2074        (gnus-make-score-map 'increase)
2075        (gnus-make-score-map 'lower)
2076        '(("Mark"
2077           ["Kill below" gnus-summary-kill-below t]
2078           ["Mark above" gnus-summary-mark-above t]
2079           ["Tick above" gnus-summary-tick-above t]
2080           ["Clear above" gnus-summary-clear-above t])
2081          ["Current score" gnus-summary-current-score t]
2082          ["Set score" gnus-summary-set-score t]
2083          ["Switch current score file..." gnus-score-change-score-file t]
2084          ["Set mark below..." gnus-score-set-mark-below t]
2085          ["Set expunge below..." gnus-score-set-expunge-below t]
2086          ["Edit current score file" gnus-score-edit-current-scores t]
2087          ["Edit score file" gnus-score-edit-file t]
2088          ["Trace score" gnus-score-find-trace t]
2089          ["Find words" gnus-score-find-favourite-words t]
2090          ["Rescore buffer" gnus-summary-rescore t]
2091          ["Increase score..." gnus-summary-increase-score t]
2092          ["Lower score..." gnus-summary-lower-score t]))))
2093
2094     ;; Define both the Article menu in the summary buffer and the
2095     ;; equivalent Commands menu in the article buffer here for
2096     ;; consistency.
2097     (let ((innards
2098            `(("Hide"
2099               ["All" gnus-article-hide t]
2100               ["Headers" gnus-article-hide-headers t]
2101               ["Signature" gnus-article-hide-signature t]
2102               ["Citation" gnus-article-hide-citation t]
2103               ["List identifiers" gnus-article-hide-list-identifiers t]
2104               ["Banner" gnus-article-strip-banner t]
2105               ["Boring headers" gnus-article-hide-boring-headers t])
2106              ("Highlight"
2107               ["All" gnus-article-highlight t]
2108               ["Headers" gnus-article-highlight-headers t]
2109               ["Signature" gnus-article-highlight-signature t]
2110               ["Citation" gnus-article-highlight-citation t])
2111              ("Date"
2112               ["Local" gnus-article-date-local t]
2113               ["ISO8601" gnus-article-date-iso8601 t]
2114               ["UT" gnus-article-date-ut t]
2115               ["Original" gnus-article-date-original t]
2116               ["Lapsed" gnus-article-date-lapsed t]
2117               ["User-defined" gnus-article-date-user t])
2118              ("Display"
2119               ["Remove images" gnus-article-remove-images t]
2120               ["Toggle smiley" gnus-treat-smiley t]
2121               ["Show X-Face" gnus-article-display-x-face t]
2122               ["Show picons in From" gnus-treat-from-picon t]
2123               ["Show picons in mail headers" gnus-treat-mail-picon t]
2124               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2125               ("View as different encoding"
2126                ,@(gnus-summary-menu-split
2127                   (mapcar
2128                    (lambda (cs)
2129                      ;; Since easymenu under Emacs doesn't allow
2130                      ;; lambda forms for menu commands, we should
2131                      ;; provide intern'ed function symbols.
2132                      (let ((command (intern (format "\
2133 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2134                        (fset command
2135                              `(lambda ()
2136                                 (interactive)
2137                                 (let ((gnus-summary-show-article-charset-alist
2138                                        '((1 . ,cs))))
2139                                   (gnus-summary-show-article 1))))
2140                        `[,(symbol-name cs) ,command t]))
2141                    (sort (if (fboundp 'coding-system-list)
2142                              (coding-system-list)
2143                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2144                            )
2145                          'string<)))))
2146              ("Washing"
2147               ("Remove Blanks"
2148                ["Leading" gnus-article-strip-leading-blank-lines t]
2149                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2150                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2151                ["All of the above" gnus-article-strip-blank-lines t]
2152                ["All" gnus-article-strip-all-blank-lines t]
2153                ["Leading space" gnus-article-strip-leading-space t]
2154                ["Trailing space" gnus-article-strip-trailing-space t]
2155                ["Leading space in headers"
2156                 gnus-article-remove-leading-whitespace t])
2157               ["Overstrike" gnus-article-treat-overstrike t]
2158               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2159               ["Emphasis" gnus-article-emphasize t]
2160               ["Word wrap" gnus-article-fill-cited-article t]
2161               ["Fill long lines" gnus-article-fill-long-lines t]
2162               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2163               ["Remove CR" gnus-article-remove-cr t]
2164               ["Rot 13" gnus-summary-caesar-message
2165                ,@(if (featurep 'xemacs) '(t)
2166                    '(:help "\"Caesar rotate\" article by 13"))]
2167               ["Morse decode" gnus-summary-morse-message t]
2168               ["Unix pipe..." gnus-summary-pipe-message t]
2169               ["Add buttons" gnus-article-add-buttons t]
2170               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2171               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2172               ["Toggle MIME" gnus-summary-toggle-mime t]
2173               ["Verbose header" gnus-summary-verbose-headers t]
2174               ["Toggle header" gnus-summary-toggle-header t]
2175               ["Unfold headers" gnus-article-treat-unfold-headers t]
2176               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2177               ["Html" gnus-article-wash-html t]
2178               ["Unsplit URLs" gnus-article-unsplit-urls t]
2179               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2180               ["Decode HZ" gnus-article-decode-HZ t]
2181               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2182               ("(Outlook) Deuglify"
2183                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2184                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2185                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2186                ["Full (Outlook) deuglify"
2187                 gnus-article-outlook-deuglify-article t])
2188               )
2189              ("Output"
2190               ["Save in default format..." gnus-summary-save-article
2191                ,@(if (featurep 'xemacs) '(t)
2192                    '(:help "Save article using default method"))]
2193               ["Save in file..." gnus-summary-save-article-file
2194                ,@(if (featurep 'xemacs) '(t)
2195                    '(:help "Save article in file"))]
2196               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2197               ["Save in MH folder..." gnus-summary-save-article-folder t]
2198               ["Save in VM folder..." gnus-summary-save-article-vm t]
2199               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2200               ["Save body in file..." gnus-summary-save-article-body-file t]
2201               ["Pipe through a filter..." gnus-summary-pipe-output t]
2202               ["Add to SOUP packet" gnus-soup-add-article t]
2203               ["Print with Muttprint..." gnus-summary-muttprint t]
2204               ["Print" gnus-summary-print-article t])
2205              ("Backend"
2206               ["Respool article..." gnus-summary-respool-article t]
2207               ["Move article..." gnus-summary-move-article
2208                (gnus-check-backend-function
2209                 'request-move-article gnus-newsgroup-name)]
2210               ["Copy article..." gnus-summary-copy-article t]
2211               ["Crosspost article..." gnus-summary-crosspost-article
2212                (gnus-check-backend-function
2213                 'request-replace-article gnus-newsgroup-name)]
2214               ["Import file..." gnus-summary-import-article
2215                (gnus-check-backend-function
2216                 'request-accept-article gnus-newsgroup-name)]
2217               ["Create article..." gnus-summary-create-article
2218                (gnus-check-backend-function
2219                 'request-accept-article gnus-newsgroup-name)]
2220               ["Check if posted" gnus-summary-article-posted-p t]
2221               ["Edit article" gnus-summary-edit-article
2222                (not (gnus-group-read-only-p))]
2223               ["Delete article" gnus-summary-delete-article
2224                (gnus-check-backend-function
2225                 'request-expire-articles gnus-newsgroup-name)]
2226               ["Query respool" gnus-summary-respool-query t]
2227               ["Trace respool" gnus-summary-respool-trace t]
2228               ["Delete expirable articles" gnus-summary-expire-articles-now
2229                (gnus-check-backend-function
2230                 'request-expire-articles gnus-newsgroup-name)])
2231              ("Extract"
2232               ["Uudecode" gnus-uu-decode-uu
2233                ,@(if (featurep 'xemacs) '(t)
2234                    '(:help "Decode uuencoded article(s)"))]
2235               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2236               ["Unshar" gnus-uu-decode-unshar t]
2237               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2238               ["Save" gnus-uu-decode-save t]
2239               ["Binhex" gnus-uu-decode-binhex t]
2240               ["Postscript" gnus-uu-decode-postscript t])
2241              ("Cache"
2242               ["Enter article" gnus-cache-enter-article t]
2243               ["Remove article" gnus-cache-remove-article t])
2244              ["Translate" gnus-article-babel t]
2245              ["Select article buffer" gnus-summary-select-article-buffer t]
2246              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2247              ["Isearch article..." gnus-summary-isearch-article t]
2248              ["Beginning of the article" gnus-summary-beginning-of-article t]
2249              ["End of the article" gnus-summary-end-of-article t]
2250              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2251              ["Fetch referenced articles" gnus-summary-refer-references t]
2252              ["Fetch current thread" gnus-summary-refer-thread t]
2253              ["Fetch article with id..." gnus-summary-refer-article t]
2254              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2255              ["Redisplay" gnus-summary-show-article t]
2256              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2257       (easy-menu-define
2258        gnus-summary-article-menu gnus-summary-mode-map ""
2259        (cons "Article" innards))
2260
2261       (if (not (keymapp gnus-summary-article-menu))
2262           (easy-menu-define
2263            gnus-article-commands-menu gnus-article-mode-map ""
2264            (cons "Commands" innards))
2265         ;; in Emacs, don't share menu.
2266         (setq gnus-article-commands-menu
2267               (copy-keymap gnus-summary-article-menu))
2268         (define-key gnus-article-mode-map [menu-bar commands]
2269           (cons "Commands" gnus-article-commands-menu))))
2270
2271     (easy-menu-define
2272      gnus-summary-thread-menu gnus-summary-mode-map ""
2273      '("Threads"
2274        ["Find all messages in thread" gnus-summary-refer-thread t]
2275        ["Toggle threading" gnus-summary-toggle-threads t]
2276        ["Hide threads" gnus-summary-hide-all-threads t]
2277        ["Show threads" gnus-summary-show-all-threads t]
2278        ["Hide thread" gnus-summary-hide-thread t]
2279        ["Show thread" gnus-summary-show-thread t]
2280        ["Go to next thread" gnus-summary-next-thread t]
2281        ["Go to previous thread" gnus-summary-prev-thread t]
2282        ["Go down thread" gnus-summary-down-thread t]
2283        ["Go up thread" gnus-summary-up-thread t]
2284        ["Top of thread" gnus-summary-top-thread t]
2285        ["Mark thread as read" gnus-summary-kill-thread t]
2286        ["Lower thread score" gnus-summary-lower-thread t]
2287        ["Raise thread score" gnus-summary-raise-thread t]
2288        ["Rethread current" gnus-summary-rethread-current t]))
2289
2290     (easy-menu-define
2291      gnus-summary-post-menu gnus-summary-mode-map ""
2292      `("Post"
2293        ["Send a message (mail or news)" gnus-summary-post-news
2294         ,@(if (featurep 'xemacs) '(t)
2295             '(:help "Post an article"))]
2296        ["Followup" gnus-summary-followup
2297         ,@(if (featurep 'xemacs) '(t)
2298             '(:help "Post followup to this article"))]
2299        ["Followup and yank" gnus-summary-followup-with-original
2300         ,@(if (featurep 'xemacs) '(t)
2301             '(:help "Post followup to this article, quoting its contents"))]
2302        ["Supersede article" gnus-summary-supersede-article t]
2303        ["Cancel article" gnus-summary-cancel-article
2304         ,@(if (featurep 'xemacs) '(t)
2305             '(:help "Cancel an article you posted"))]
2306        ["Reply" gnus-summary-reply t]
2307        ["Reply and yank" gnus-summary-reply-with-original t]
2308        ["Wide reply" gnus-summary-wide-reply t]
2309        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2310         ,@(if (featurep 'xemacs) '(t)
2311             '(:help "Mail a reply, quoting this article"))]
2312        ["Very wide reply" gnus-summary-very-wide-reply t]
2313        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2314         ,@(if (featurep 'xemacs) '(t)
2315             '(:help "Mail a very wide reply, quoting this article"))]
2316        ["Mail forward" gnus-summary-mail-forward t]
2317        ["Post forward" gnus-summary-post-forward t]
2318        ["Digest and mail" gnus-summary-digest-mail-forward t]
2319        ["Digest and post" gnus-summary-digest-post-forward t]
2320        ["Resend message" gnus-summary-resend-message t]
2321        ["Resend message edit" gnus-summary-resend-message-edit t]
2322        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2323        ["Send a mail" gnus-summary-mail-other-window t]
2324        ["Create a local message" gnus-summary-news-other-window t]
2325        ["Uuencode and post" gnus-uu-post-news
2326         ,@(if (featurep 'xemacs) '(t)
2327             '(:help "Post a uuencoded article"))]
2328        ["Followup via news" gnus-summary-followup-to-mail t]
2329        ["Followup via news and yank"
2330         gnus-summary-followup-to-mail-with-original t]
2331        ;;("Draft"
2332        ;;["Send" gnus-summary-send-draft t]
2333        ;;["Send bounced" gnus-resend-bounced-mail t])
2334        ))
2335
2336     (cond
2337      ((not (keymapp gnus-summary-post-menu))
2338       (setq gnus-article-post-menu gnus-summary-post-menu))
2339      ((not gnus-article-post-menu)
2340       ;; Don't share post menu.
2341       (setq gnus-article-post-menu
2342             (copy-keymap gnus-summary-post-menu))))
2343     (define-key gnus-article-mode-map [menu-bar post]
2344       (cons "Post" gnus-article-post-menu))
2345
2346     (easy-menu-define
2347      gnus-summary-misc-menu gnus-summary-mode-map ""
2348      `("Gnus"
2349        ("Mark Read"
2350         ["Mark as read" gnus-summary-mark-as-read-forward t]
2351         ["Mark same subject and select"
2352          gnus-summary-kill-same-subject-and-select t]
2353         ["Mark same subject" gnus-summary-kill-same-subject t]
2354         ["Catchup" gnus-summary-catchup
2355          ,@(if (featurep 'xemacs) '(t)
2356              '(:help "Mark unread articles in this group as read"))]
2357         ["Catchup all" gnus-summary-catchup-all t]
2358         ["Catchup to here" gnus-summary-catchup-to-here t]
2359         ["Catchup from here" gnus-summary-catchup-from-here t]
2360         ["Catchup region" gnus-summary-mark-region-as-read
2361          (gnus-mark-active-p)]
2362         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2363        ("Mark Various"
2364         ["Tick" gnus-summary-tick-article-forward t]
2365         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2366         ["Remove marks" gnus-summary-clear-mark-forward t]
2367         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2368         ["Set bookmark" gnus-summary-set-bookmark t]
2369         ["Remove bookmark" gnus-summary-remove-bookmark t])
2370        ("Limit to"
2371         ["Marks..." gnus-summary-limit-to-marks t]
2372         ["Subject..." gnus-summary-limit-to-subject t]
2373         ["Author..." gnus-summary-limit-to-author t]
2374         ["Age..." gnus-summary-limit-to-age t]
2375         ["Extra..." gnus-summary-limit-to-extra t]
2376         ["Score..." gnus-summary-limit-to-score t]
2377         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2378         ["Unread" gnus-summary-limit-to-unread t]
2379         ["Unseen" gnus-summary-limit-to-unseen t]
2380         ["Replied" gnus-summary-limit-to-replied t]
2381         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2382         ["Next articles" gnus-summary-limit-to-articles t]
2383         ["Pop limit" gnus-summary-pop-limit t]
2384         ["Show dormant" gnus-summary-limit-include-dormant t]
2385         ["Hide childless dormant"
2386          gnus-summary-limit-exclude-childless-dormant t]
2387         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2388         ["Hide marked" gnus-summary-limit-exclude-marks t]
2389         ["Show expunged" gnus-summary-limit-include-expunged t])
2390        ("Process Mark"
2391         ["Set mark" gnus-summary-mark-as-processable t]
2392         ["Remove mark" gnus-summary-unmark-as-processable t]
2393         ["Remove all marks" gnus-summary-unmark-all-processable t]
2394         ["Mark above" gnus-uu-mark-over t]
2395         ["Mark series" gnus-uu-mark-series t]
2396         ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2397         ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2398         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2399         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2400         ["Mark all" gnus-uu-mark-all t]
2401         ["Mark buffer" gnus-uu-mark-buffer t]
2402         ["Mark sparse" gnus-uu-mark-sparse t]
2403         ["Mark thread" gnus-uu-mark-thread t]
2404         ["Unmark thread" gnus-uu-unmark-thread t]
2405         ("Process Mark Sets"
2406          ["Kill" gnus-summary-kill-process-mark t]
2407          ["Yank" gnus-summary-yank-process-mark
2408           gnus-newsgroup-process-stack]
2409          ["Save" gnus-summary-save-process-mark t]
2410          ["Run command on marked..." gnus-summary-universal-argument t]))
2411        ("Scroll article"
2412         ["Page forward" gnus-summary-next-page
2413          ,@(if (featurep 'xemacs) '(t)
2414              '(:help "Show next page of article"))]
2415         ["Page backward" gnus-summary-prev-page
2416          ,@(if (featurep 'xemacs) '(t)
2417              '(:help "Show previous page of article"))]
2418         ["Line forward" gnus-summary-scroll-up t])
2419        ("Move"
2420         ["Next unread article" gnus-summary-next-unread-article t]
2421         ["Previous unread article" gnus-summary-prev-unread-article t]
2422         ["Next article" gnus-summary-next-article t]
2423         ["Previous article" gnus-summary-prev-article t]
2424         ["Next unread subject" gnus-summary-next-unread-subject t]
2425         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2426         ["Next article same subject" gnus-summary-next-same-subject t]
2427         ["Previous article same subject" gnus-summary-prev-same-subject t]
2428         ["First unread article" gnus-summary-first-unread-article t]
2429         ["Best unread article" gnus-summary-best-unread-article t]
2430         ["Go to subject number..." gnus-summary-goto-subject t]
2431         ["Go to article number..." gnus-summary-goto-article t]
2432         ["Go to the last article" gnus-summary-goto-last-article t]
2433         ["Pop article off history" gnus-summary-pop-article t])
2434        ("Sort"
2435         ["Sort by number" gnus-summary-sort-by-number t]
2436         ["Sort by author" gnus-summary-sort-by-author t]
2437         ["Sort by subject" gnus-summary-sort-by-subject t]
2438         ["Sort by date" gnus-summary-sort-by-date t]
2439         ["Sort by score" gnus-summary-sort-by-score t]
2440         ["Sort by lines" gnus-summary-sort-by-lines t]
2441         ["Sort by characters" gnus-summary-sort-by-chars t]
2442         ["Randomize" gnus-summary-sort-by-random t]
2443         ["Original sort" gnus-summary-sort-by-original t])
2444        ("Help"
2445         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2446         ["Describe group" gnus-summary-describe-group t]
2447         ["Fetch charter" gnus-group-fetch-charter
2448          ,@(if (featurep 'xemacs) nil
2449              '(:help "Display the charter of the current group"))]
2450         ["Fetch control message" gnus-group-fetch-control
2451          ,@(if (featurep 'xemacs) nil
2452              '(:help "Display the archived control message for the current group"))]
2453         ["Read manual" gnus-info-find-node t])
2454        ("Modes"
2455         ["Pick and read" gnus-pick-mode t]
2456         ["Binary" gnus-binary-mode t])
2457        ("Regeneration"
2458         ["Regenerate" gnus-summary-prepare t]
2459         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2460         ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2461         ["Toggle threading" gnus-summary-toggle-threads t])
2462        ["See old articles" gnus-summary-insert-old-articles t]
2463        ["See new articles" gnus-summary-insert-new-articles t]
2464        ["Filter articles..." gnus-summary-execute-command t]
2465        ["Run command on articles..." gnus-summary-universal-argument t]
2466        ["Search articles forward..." gnus-summary-search-article-forward t]
2467        ["Search articles backward..." gnus-summary-search-article-backward t]
2468        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2469        ["Expand window" gnus-summary-expand-window t]
2470        ["Expire expirable articles" gnus-summary-expire-articles
2471         (gnus-check-backend-function
2472          'request-expire-articles gnus-newsgroup-name)]
2473        ["Edit local kill file" gnus-summary-edit-local-kill t]
2474        ["Edit main kill file" gnus-summary-edit-global-kill t]
2475        ["Edit group parameters" gnus-summary-edit-parameters t]
2476        ["Customize group parameters" gnus-summary-customize-parameters t]
2477        ["Send a bug report" gnus-bug t]
2478        ("Exit"
2479         ["Catchup and exit" gnus-summary-catchup-and-exit
2480          ,@(if (featurep 'xemacs) '(t)
2481              '(:help "Mark unread articles in this group as read, then exit"))]
2482         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2483         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2484         ["Exit group" gnus-summary-exit
2485          ,@(if (featurep 'xemacs) '(t)
2486              '(:help "Exit current group, return to group selection mode"))]
2487         ["Exit group without updating" gnus-summary-exit-no-update t]
2488         ["Exit and goto next group" gnus-summary-next-group t]
2489         ["Exit and goto prev group" gnus-summary-prev-group t]
2490         ["Reselect group" gnus-summary-reselect-current-group t]
2491         ["Rescan group" gnus-summary-rescan-group t]
2492         ["Update dribble" gnus-summary-save-newsrc t])))
2493
2494     (gnus-run-hooks 'gnus-summary-menu-hook)))
2495
2496 (defvar gnus-summary-tool-bar-map nil)
2497
2498 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2499 (defun gnus-summary-make-tool-bar ()
2500   (if (and (fboundp 'tool-bar-add-item-from-menu)
2501            (default-value 'tool-bar-mode)
2502            (not gnus-summary-tool-bar-map))
2503       (setq gnus-summary-tool-bar-map
2504             (let ((tool-bar-map (make-sparse-keymap))
2505                   (load-path (mm-image-load-path)))
2506               (tool-bar-add-item-from-menu
2507                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2508               (tool-bar-add-item-from-menu
2509                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2510               (tool-bar-add-item-from-menu
2511                'gnus-summary-post-news "post" gnus-summary-mode-map)
2512               (tool-bar-add-item-from-menu
2513                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2514               (tool-bar-add-item-from-menu
2515                'gnus-summary-followup "followup" gnus-summary-mode-map)
2516               (tool-bar-add-item-from-menu
2517                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2518               (tool-bar-add-item-from-menu
2519                'gnus-summary-reply "reply" gnus-summary-mode-map)
2520               (tool-bar-add-item-from-menu
2521                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2522               (tool-bar-add-item-from-menu
2523                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2524               (tool-bar-add-item-from-menu
2525                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2526               (tool-bar-add-item-from-menu
2527                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2528               (tool-bar-add-item-from-menu
2529                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2530               (tool-bar-add-item-from-menu
2531                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2532               (tool-bar-add-item-from-menu
2533                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2534               (tool-bar-add-item-from-menu
2535                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2536               tool-bar-map)))
2537   (if gnus-summary-tool-bar-map
2538       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2539
2540 (defun gnus-score-set-default (var value)
2541   "A version of set that updates the GNU Emacs menu-bar."
2542   (set var value)
2543   ;; It is the message that forces the active status to be updated.
2544   (message ""))
2545
2546 (defun gnus-make-score-map (type)
2547   "Make a summary score map of type TYPE."
2548   (if t
2549       nil
2550     (let ((headers '(("author" "from" string)
2551                      ("subject" "subject" string)
2552                      ("article body" "body" string)
2553                      ("article head" "head" string)
2554                      ("xref" "xref" string)
2555                      ("extra header" "extra" string)
2556                      ("lines" "lines" number)
2557                      ("followups to author" "followup" string)))
2558           (types '((number ("less than" <)
2559                            ("greater than" >)
2560                            ("equal" =))
2561                    (string ("substring" s)
2562                            ("exact string" e)
2563                            ("fuzzy string" f)
2564                            ("regexp" r))))
2565           (perms '(("temporary" (current-time-string))
2566                    ("permanent" nil)
2567                    ("immediate" now)))
2568           header)
2569       (list
2570        (apply
2571         'nconc
2572         (list
2573          (if (eq type 'lower)
2574              "Lower score"
2575            "Increase score"))
2576         (let (outh)
2577           (while headers
2578             (setq header (car headers))
2579             (setq outh
2580                   (cons
2581                    (apply
2582                     'nconc
2583                     (list (car header))
2584                     (let ((ts (cdr (assoc (nth 2 header) types)))
2585                           outt)
2586                       (while ts
2587                         (setq outt
2588                               (cons
2589                                (apply
2590                                 'nconc
2591                                 (list (caar ts))
2592                                 (let ((ps perms)
2593                                       outp)
2594                                   (while ps
2595                                     (setq outp
2596                                           (cons
2597                                            (vector
2598                                             (caar ps)
2599                                             (list
2600                                              'gnus-summary-score-entry
2601                                              (nth 1 header)
2602                                              (if (or (string= (nth 1 header)
2603                                                               "head")
2604                                                      (string= (nth 1 header)
2605                                                               "body"))
2606                                                  ""
2607                                                (list 'gnus-summary-header
2608                                                      (nth 1 header)))
2609                                              (list 'quote (nth 1 (car ts)))
2610                                              (list 'gnus-score-delta-default
2611                                                    nil)
2612                                              (nth 1 (car ps))
2613                                              t)
2614                                             t)
2615                                            outp))
2616                                     (setq ps (cdr ps)))
2617                                   (list (nreverse outp))))
2618                                outt))
2619                         (setq ts (cdr ts)))
2620                       (list (nreverse outt))))
2621                    outh))
2622             (setq headers (cdr headers)))
2623           (list (nreverse outh))))))))
2624
2625 \f
2626
2627 (defun gnus-summary-mode (&optional group)
2628   "Major mode for reading articles.
2629
2630 All normal editing commands are switched off.
2631 \\<gnus-summary-mode-map>
2632 Each line in this buffer represents one article.  To read an
2633 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2634 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2635 respectively.
2636
2637 You can also post articles and send mail from this buffer.  To
2638 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2639 of an article, type `\\[gnus-summary-reply]'.
2640
2641 There are approx. one gazillion commands you can execute in this
2642 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2643
2644 The following commands are available:
2645
2646 \\{gnus-summary-mode-map}"
2647   (interactive)
2648   (kill-all-local-variables)
2649   (when (gnus-visual-p 'summary-menu 'menu)
2650     (gnus-summary-make-menu-bar)
2651     (gnus-summary-make-tool-bar))
2652   (gnus-summary-make-local-variables)
2653   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2654     (gnus-summary-make-local-variables))
2655   (gnus-make-thread-indent-array)
2656   (gnus-simplify-mode-line)
2657   (setq major-mode 'gnus-summary-mode)
2658   (setq mode-name "Summary")
2659   (make-local-variable 'minor-mode-alist)
2660   (use-local-map gnus-summary-mode-map)
2661   (buffer-disable-undo)
2662   (setq buffer-read-only t)             ;Disable modification
2663   (setq truncate-lines t)
2664   (setq selective-display t)
2665   (setq selective-display-ellipses t)   ;Display `...'
2666   (gnus-summary-set-display-table)
2667   (gnus-set-default-directory)
2668   (setq gnus-newsgroup-name group)
2669   (unless (gnus-news-group-p group)
2670     (setq gnus-newsgroup-incorporated
2671           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2672   (make-local-variable 'gnus-summary-line-format)
2673   (make-local-variable 'gnus-summary-line-format-spec)
2674   (make-local-variable 'gnus-summary-dummy-line-format)
2675   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2676   (make-local-variable 'gnus-summary-mark-positions)
2677   (gnus-make-local-hook 'pre-command-hook)
2678   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2679   (gnus-run-hooks 'gnus-summary-mode-hook)
2680   (turn-on-gnus-mailing-list-mode)
2681   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2682   (gnus-update-summary-mark-positions))
2683
2684 (defun gnus-summary-make-local-variables ()
2685   "Make all the local summary buffer variables."
2686   (let (global)
2687     (dolist (local gnus-summary-local-variables)
2688       (if (consp local)
2689           (progn
2690             (if (eq (cdr local) 'global)
2691                 ;; Copy the global value of the variable.
2692                 (setq global (symbol-value (car local)))
2693               ;; Use the value from the list.
2694               (setq global (eval (cdr local))))
2695             (set (make-local-variable (car local)) global))
2696         ;; Simple nil-valued local variable.
2697         (set (make-local-variable local) nil)))))
2698
2699 (defun gnus-summary-clear-local-variables ()
2700   (let ((locals gnus-summary-local-variables))
2701     (while locals
2702       (if (consp (car locals))
2703           (and (vectorp (caar locals))
2704                (set (caar locals) nil))
2705         (and (vectorp (car locals))
2706              (set (car locals) nil)))
2707       (setq locals (cdr locals)))))
2708
2709 ;; Summary data functions.
2710
2711 (defmacro gnus-data-number (data)
2712   `(car ,data))
2713
2714 (defmacro gnus-data-set-number (data number)
2715   `(setcar ,data ,number))
2716
2717 (defmacro gnus-data-mark (data)
2718   `(nth 1 ,data))
2719
2720 (defmacro gnus-data-set-mark (data mark)
2721   `(setcar (nthcdr 1 ,data) ,mark))
2722
2723 (defmacro gnus-data-pos (data)
2724   `(nth 2 ,data))
2725
2726 (defmacro gnus-data-set-pos (data pos)
2727   `(setcar (nthcdr 2 ,data) ,pos))
2728
2729 (defmacro gnus-data-header (data)
2730   `(nth 3 ,data))
2731
2732 (defmacro gnus-data-set-header (data header)
2733   `(setcar (nthcdr 3 ,data) ,header))
2734
2735 (defmacro gnus-data-level (data)
2736   `(nth 4 ,data))
2737
2738 (defmacro gnus-data-unread-p (data)
2739   `(= (nth 1 ,data) gnus-unread-mark))
2740
2741 (defmacro gnus-data-read-p (data)
2742   `(/= (nth 1 ,data) gnus-unread-mark))
2743
2744 (defmacro gnus-data-pseudo-p (data)
2745   `(consp (nth 3 ,data)))
2746
2747 (defmacro gnus-data-find (number)
2748   `(assq ,number gnus-newsgroup-data))
2749
2750 (defmacro gnus-data-find-list (number &optional data)
2751   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2752      (memq (assq ,number bdata)
2753            bdata)))
2754
2755 (defmacro gnus-data-make (number mark pos header level)
2756   `(list ,number ,mark ,pos ,header ,level))
2757
2758 (defun gnus-data-enter (after-article number mark pos header level offset)
2759   (let ((data (gnus-data-find-list after-article)))
2760     (unless data
2761       (error "No such article: %d" after-article))
2762     (setcdr data (cons (gnus-data-make number mark pos header level)
2763                        (cdr data)))
2764     (setq gnus-newsgroup-data-reverse nil)
2765     (gnus-data-update-list (cddr data) offset)))
2766
2767 (defun gnus-data-enter-list (after-article list &optional offset)
2768   (when list
2769     (let ((data (and after-article (gnus-data-find-list after-article)))
2770           (ilist list))
2771       (if (not (or data
2772                    after-article))
2773           (let ((odata gnus-newsgroup-data))
2774             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2775             (when offset
2776               (gnus-data-update-list odata offset)))
2777         ;; Find the last element in the list to be spliced into the main
2778         ;; list.
2779         (while (cdr list)
2780           (setq list (cdr list)))
2781         (if (not data)
2782             (progn
2783               (setcdr list gnus-newsgroup-data)
2784               (setq gnus-newsgroup-data ilist)
2785               (when offset
2786                 (gnus-data-update-list (cdr list) offset)))
2787           (setcdr list (cdr data))
2788           (setcdr data ilist)
2789           (when offset
2790             (gnus-data-update-list (cdr list) offset))))
2791       (setq gnus-newsgroup-data-reverse nil))))
2792
2793 (defun gnus-data-remove (article &optional offset)
2794   (let ((data gnus-newsgroup-data))
2795     (if (= (gnus-data-number (car data)) article)
2796         (progn
2797           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2798                 gnus-newsgroup-data-reverse nil)
2799           (when offset
2800             (gnus-data-update-list gnus-newsgroup-data offset)))
2801       (while (cdr data)
2802         (when (= (gnus-data-number (cadr data)) article)
2803           (setcdr data (cddr data))
2804           (when offset
2805             (gnus-data-update-list (cdr data) offset))
2806           (setq data nil
2807                 gnus-newsgroup-data-reverse nil))
2808         (setq data (cdr data))))))
2809
2810 (defmacro gnus-data-list (backward)
2811   `(if ,backward
2812        (or gnus-newsgroup-data-reverse
2813            (setq gnus-newsgroup-data-reverse
2814                  (reverse gnus-newsgroup-data)))
2815      gnus-newsgroup-data))
2816
2817 (defun gnus-data-update-list (data offset)
2818   "Add OFFSET to the POS of all data entries in DATA."
2819   (setq gnus-newsgroup-data-reverse nil)
2820   (while data
2821     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2822     (setq data (cdr data))))
2823
2824 (defun gnus-summary-article-pseudo-p (article)
2825   "Say whether this article is a pseudo article or not."
2826   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2827
2828 (defmacro gnus-summary-article-sparse-p (article)
2829   "Say whether this article is a sparse article or not."
2830   `(memq ,article gnus-newsgroup-sparse))
2831
2832 (defmacro gnus-summary-article-ancient-p (article)
2833   "Say whether this article is a sparse article or not."
2834   `(memq ,article gnus-newsgroup-ancient))
2835
2836 (defun gnus-article-parent-p (number)
2837   "Say whether this article is a parent or not."
2838   (let ((data (gnus-data-find-list number)))
2839     (and (cdr data)                     ; There has to be an article after...
2840          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2841             (gnus-data-level (nth 1 data))))))
2842
2843 (defun gnus-article-children (number)
2844   "Return a list of all children to NUMBER."
2845   (let* ((data (gnus-data-find-list number))
2846          (level (gnus-data-level (car data)))
2847          children)
2848     (setq data (cdr data))
2849     (while (and data
2850                 (= (gnus-data-level (car data)) (1+ level)))
2851       (push (gnus-data-number (car data)) children)
2852       (setq data (cdr data)))
2853     children))
2854
2855 (defmacro gnus-summary-skip-intangible ()
2856   "If the current article is intangible, then jump to a different article."
2857   '(let ((to (get-text-property (point) 'gnus-intangible)))
2858      (and to (gnus-summary-goto-subject to))))
2859
2860 (defmacro gnus-summary-article-intangible-p ()
2861   "Say whether this article is intangible or not."
2862   '(get-text-property (point) 'gnus-intangible))
2863
2864 (defun gnus-article-read-p (article)
2865   "Say whether ARTICLE is read or not."
2866   (not (or (memq article gnus-newsgroup-marked)
2867            (memq article gnus-newsgroup-spam-marked)
2868            (memq article gnus-newsgroup-unreads)
2869            (memq article gnus-newsgroup-unselected)
2870            (memq article gnus-newsgroup-dormant))))
2871
2872 ;; Some summary mode macros.
2873
2874 (defmacro gnus-summary-article-number ()
2875   "The article number of the article on the current line.
2876 If there isn't an article number here, then we return the current
2877 article number."
2878   '(progn
2879      (gnus-summary-skip-intangible)
2880      (or (get-text-property (point) 'gnus-number)
2881          (gnus-summary-last-subject))))
2882
2883 (defmacro gnus-summary-article-header (&optional number)
2884   "Return the header of article NUMBER."
2885   `(gnus-data-header (gnus-data-find
2886                       ,(or number '(gnus-summary-article-number)))))
2887
2888 (defmacro gnus-summary-thread-level (&optional number)
2889   "Return the level of thread that starts with article NUMBER."
2890   `(if (and (eq gnus-summary-make-false-root 'dummy)
2891             (get-text-property (point) 'gnus-intangible))
2892        0
2893      (gnus-data-level (gnus-data-find
2894                        ,(or number '(gnus-summary-article-number))))))
2895
2896 (defmacro gnus-summary-article-mark (&optional number)
2897   "Return the mark of article NUMBER."
2898   `(gnus-data-mark (gnus-data-find
2899                     ,(or number '(gnus-summary-article-number)))))
2900
2901 (defmacro gnus-summary-article-pos (&optional number)
2902   "Return the position of the line of article NUMBER."
2903   `(gnus-data-pos (gnus-data-find
2904                    ,(or number '(gnus-summary-article-number)))))
2905
2906 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2907 (defmacro gnus-summary-article-subject (&optional number)
2908   "Return current subject string or nil if nothing."
2909   `(let ((headers
2910           ,(if number
2911                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2912              '(gnus-data-header (assq (gnus-summary-article-number)
2913                                       gnus-newsgroup-data)))))
2914      (and headers
2915           (vectorp headers)
2916           (mail-header-subject headers))))
2917
2918 (defmacro gnus-summary-article-score (&optional number)
2919   "Return current article score."
2920   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2921                   gnus-newsgroup-scored))
2922        gnus-summary-default-score 0))
2923
2924 (defun gnus-summary-article-children (&optional number)
2925   "Return a list of article numbers that are children of article NUMBER."
2926   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2927          (level (gnus-data-level (car data)))
2928          l children)
2929     (while (and (setq data (cdr data))
2930                 (> (setq l (gnus-data-level (car data))) level))
2931       (and (= (1+ level) l)
2932            (push (gnus-data-number (car data))
2933                  children)))
2934     (nreverse children)))
2935
2936 (defun gnus-summary-article-parent (&optional number)
2937   "Return the article number of the parent of article NUMBER."
2938   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2939                                     (gnus-data-list t)))
2940          (level (gnus-data-level (car data))))
2941     (if (zerop level)
2942         ()                              ; This is a root.
2943       ;; We search until we find an article with a level less than
2944       ;; this one.  That function has to be the parent.
2945       (while (and (setq data (cdr data))
2946                   (not (< (gnus-data-level (car data)) level))))
2947       (and data (gnus-data-number (car data))))))
2948
2949 (defun gnus-unread-mark-p (mark)
2950   "Say whether MARK is the unread mark."
2951   (= mark gnus-unread-mark))
2952
2953 (defun gnus-read-mark-p (mark)
2954   "Say whether MARK is one of the marks that mark as read.
2955 This is all marks except unread, ticked, dormant, and expirable."
2956   (not (or (= mark gnus-unread-mark)
2957            (= mark gnus-ticked-mark)
2958            (= mark gnus-spam-mark)
2959            (= mark gnus-dormant-mark)
2960            (= mark gnus-expirable-mark))))
2961
2962 (defmacro gnus-article-mark (number)
2963   "Return the MARK of article NUMBER.
2964 This macro should only be used when computing the mark the \"first\"
2965 time; i.e., when generating the summary lines.  After that,
2966 `gnus-summary-article-mark' should be used to examine the
2967 marks of articles."
2968   `(cond
2969     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2970     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2971     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2972     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2973     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2974     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2975     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2976     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2977            gnus-ancient-mark))))
2978
2979 ;; Saving hidden threads.
2980
2981 (defmacro gnus-save-hidden-threads (&rest forms)
2982   "Save hidden threads, eval FORMS, and restore the hidden threads."
2983   (let ((config (make-symbol "config")))
2984     `(let ((,config (gnus-hidden-threads-configuration)))
2985        (unwind-protect
2986            (save-excursion
2987              ,@forms)
2988          (gnus-restore-hidden-threads-configuration ,config)))))
2989 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2990 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2991
2992 (defun gnus-data-compute-positions ()
2993   "Compute the positions of all articles."
2994   (setq gnus-newsgroup-data-reverse nil)
2995   (let ((data gnus-newsgroup-data))
2996     (save-excursion
2997       (gnus-save-hidden-threads
2998         (gnus-summary-show-all-threads)
2999         (goto-char (point-min))
3000         (while data
3001           (while (get-text-property (point) 'gnus-intangible)
3002             (forward-line 1))
3003           (gnus-data-set-pos (car data) (+ (point) 3))
3004           (setq data (cdr data))
3005           (forward-line 1))))))
3006
3007 (defun gnus-hidden-threads-configuration ()
3008   "Return the current hidden threads configuration."
3009   (save-excursion
3010     (let (config)
3011       (goto-char (point-min))
3012       (while (search-forward "\r" nil t)
3013         (push (1- (point)) config))
3014       config)))
3015
3016 (defun gnus-restore-hidden-threads-configuration (config)
3017   "Restore hidden threads configuration from CONFIG."
3018   (save-excursion
3019     (let (point buffer-read-only)
3020       (while (setq point (pop config))
3021         (when (and (< point (point-max))
3022                    (goto-char point)
3023                    (eq (char-after) ?\n))
3024           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3025
3026 ;; Various summary mode internalish functions.
3027
3028 (defun gnus-mouse-pick-article (e)
3029   (interactive "e")
3030   (mouse-set-point e)
3031   (gnus-summary-next-page nil t))
3032
3033 (defun gnus-summary-set-display-table ()
3034   "Change the display table.
3035 Odd characters have a tendency to mess
3036 up nicely formatted displays - we make all possible glyphs
3037 display only a single character."
3038
3039   ;; We start from the standard display table, if any.
3040   (let ((table (or (copy-sequence standard-display-table)
3041                    (make-display-table)))
3042         (i 32))
3043     ;; Nix out all the control chars...
3044     (while (>= (setq i (1- i)) 0)
3045       (aset table i [??]))
3046     ;; ... but not newline and cr, of course.  (cr is necessary for the
3047     ;; selective display).
3048     (aset table ?\n nil)
3049     (aset table ?\r nil)
3050     ;; We keep TAB as well.
3051     (aset table ?\t nil)
3052     ;; We nix out any glyphs over 126 that are not set already.
3053     (let ((i 256))
3054       (while (>= (setq i (1- i)) 127)
3055         ;; Only modify if the entry is nil.
3056         (unless (aref table i)
3057           (aset table i [??]))))
3058     (setq buffer-display-table table)))
3059
3060 (defun gnus-summary-set-article-display-arrow (pos)
3061   "Update the overlay arrow to point to line at position POS."
3062   (when (and gnus-summary-display-arrow
3063              (boundp 'overlay-arrow-position)
3064              (boundp 'overlay-arrow-string))
3065     (save-excursion
3066       (goto-char pos)
3067       (beginning-of-line)
3068       (unless overlay-arrow-position
3069         (setq overlay-arrow-position (make-marker)))
3070       (setq overlay-arrow-string "=>"
3071             overlay-arrow-position (set-marker overlay-arrow-position
3072                                                (point)
3073                                                (current-buffer))))))
3074
3075 (defun gnus-summary-setup-buffer (group)
3076   "Initialize summary buffer."
3077   (let ((buffer (gnus-summary-buffer-name group))
3078         (dead-name (concat "*Dead Summary "
3079                            (gnus-group-decoded-name group) "*")))
3080     ;; If a dead summary buffer exists, we kill it.
3081     (when (gnus-buffer-live-p dead-name)
3082       (gnus-kill-buffer dead-name))
3083     (if (get-buffer buffer)
3084         (progn
3085           (set-buffer buffer)
3086           (setq gnus-summary-buffer (current-buffer))
3087           (not gnus-newsgroup-prepared))
3088       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3089       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3090       (gnus-summary-mode group)
3091       (when gnus-carpal
3092         (gnus-carpal-setup-buffer 'summary))
3093       (unless gnus-single-article-buffer
3094         (make-local-variable 'gnus-article-buffer)
3095         (make-local-variable 'gnus-article-current)
3096         (make-local-variable 'gnus-original-article-buffer))
3097       (setq gnus-newsgroup-name group)
3098       ;; Set any local variables in the group parameters.
3099       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3100       t)))
3101
3102 (defun gnus-set-global-variables ()
3103   "Set the global equivalents of the buffer-local variables.
3104 They are set to the latest values they had.  These reflect the summary
3105 buffer that was in action when the last article was fetched."
3106   (when (eq major-mode 'gnus-summary-mode)
3107     (setq gnus-summary-buffer (current-buffer))
3108     (let ((name gnus-newsgroup-name)
3109           (marked gnus-newsgroup-marked)
3110           (spam gnus-newsgroup-spam-marked)
3111           (unread gnus-newsgroup-unreads)
3112           (headers gnus-current-headers)
3113           (data gnus-newsgroup-data)
3114           (summary gnus-summary-buffer)
3115           (article-buffer gnus-article-buffer)
3116           (original gnus-original-article-buffer)
3117           (gac gnus-article-current)
3118           (reffed gnus-reffed-article-number)
3119           (score-file gnus-current-score-file)
3120           (default-charset gnus-newsgroup-charset)
3121           vlist)
3122       (let ((locals gnus-newsgroup-variables))
3123         (while locals
3124           (if (consp (car locals))
3125               (push (eval (caar locals)) vlist)
3126             (push (eval (car locals)) vlist))
3127           (setq locals (cdr locals)))
3128         (setq vlist (nreverse vlist)))
3129       (save-excursion
3130         (set-buffer gnus-group-buffer)
3131         (setq gnus-newsgroup-name name
3132               gnus-newsgroup-marked marked
3133               gnus-newsgroup-spam-marked spam
3134               gnus-newsgroup-unreads unread
3135               gnus-current-headers headers
3136               gnus-newsgroup-data data
3137               gnus-article-current gac
3138               gnus-summary-buffer summary
3139               gnus-article-buffer article-buffer
3140               gnus-original-article-buffer original
3141               gnus-reffed-article-number reffed
3142               gnus-current-score-file score-file
3143               gnus-newsgroup-charset default-charset)
3144         (let ((locals gnus-newsgroup-variables))
3145           (while locals
3146             (if (consp (car locals))
3147                 (set (caar locals) (pop vlist))
3148               (set (car locals) (pop vlist)))
3149             (setq locals (cdr locals))))
3150         ;; The article buffer also has local variables.
3151         (when (gnus-buffer-live-p gnus-article-buffer)
3152           (set-buffer gnus-article-buffer)
3153           (setq gnus-summary-buffer summary))))))
3154
3155 (defun gnus-summary-article-unread-p (article)
3156   "Say whether ARTICLE is unread or not."
3157   (memq article gnus-newsgroup-unreads))
3158
3159 (defun gnus-summary-first-article-p (&optional article)
3160   "Return whether ARTICLE is the first article in the buffer."
3161   (if (not (setq article (or article (gnus-summary-article-number))))
3162       nil
3163     (eq article (caar gnus-newsgroup-data))))
3164
3165 (defun gnus-summary-last-article-p (&optional article)
3166   "Return whether ARTICLE is the last article in the buffer."
3167   (if (not (setq article (or article (gnus-summary-article-number))))
3168       ;; All non-existent numbers are the last article.  :-)
3169       t
3170     (not (cdr (gnus-data-find-list article)))))
3171
3172 (defun gnus-make-thread-indent-array ()
3173   (let ((n 200))
3174     (unless (and gnus-thread-indent-array
3175                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3176       (setq gnus-thread-indent-array (make-vector 201 "")
3177             gnus-thread-indent-array-level gnus-thread-indent-level)
3178       (while (>= n 0)
3179         (aset gnus-thread-indent-array n
3180               (make-string (* n gnus-thread-indent-level) ? ))
3181         (setq n (1- n))))))
3182
3183 (defun gnus-update-summary-mark-positions ()
3184   "Compute where the summary marks are to go."
3185   (save-excursion
3186     (when (gnus-buffer-exists-p gnus-summary-buffer)
3187       (set-buffer gnus-summary-buffer))
3188     (let ((gnus-replied-mark 129)
3189           (gnus-score-below-mark 130)
3190           (gnus-score-over-mark 130)
3191           (gnus-undownloaded-mark 131)
3192           (spec gnus-summary-line-format-spec)
3193           gnus-visual pos)
3194       (save-excursion
3195         (gnus-set-work-buffer)
3196         (let ((gnus-summary-line-format-spec spec)
3197               (gnus-newsgroup-downloadable '(0)))
3198           (gnus-summary-insert-line
3199            (make-full-mail-header 0 "" "nobody"
3200                                   "05 Apr 2001 23:33:09 +0400"
3201                                   "" "" 0 0 "" nil)
3202            0 nil t 128 t nil "" nil 1)
3203           (goto-char (point-min))
3204           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3205                                              (- (point) (point-min) 1)))))
3206           (goto-char (point-min))
3207           (push (cons 'replied (and (search-forward "\201" nil t)
3208                                     (- (point) (point-min) 1)))
3209                 pos)
3210           (goto-char (point-min))
3211           (push (cons 'score (and (search-forward "\202" nil t)
3212                                   (- (point) (point-min) 1)))
3213                 pos)
3214           (goto-char (point-min))
3215           (push (cons 'download
3216                       (and (search-forward "\203" nil t)
3217                            (- (point) (point-min) 1)))
3218                 pos)))
3219       (setq gnus-summary-mark-positions pos))))
3220
3221 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3222   "Insert a dummy root in the summary buffer."
3223   (beginning-of-line)
3224   (gnus-add-text-properties
3225    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3226    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3227
3228 (defun gnus-summary-extract-address-component (from)
3229   (or (car (funcall gnus-extract-address-components from))
3230       from))
3231
3232 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3233   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3234                                 default-mime-charset)))
3235     ;; Is it really necessary to do this next part for each summary line?
3236     ;; Luckily, doesn't seem to slow things down much.
3237     (or
3238      (and gnus-ignored-from-addresses
3239           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3240           (let ((extra-headers (mail-header-extra header))
3241                 to
3242                 newsgroups)
3243             (cond
3244              ((setq to (cdr (assq 'To extra-headers)))
3245               (concat "-> "
3246                       (inline
3247                         (gnus-summary-extract-address-component
3248                          (funcall gnus-decode-encoded-word-function to)))))
3249              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3250               (concat "=> " newsgroups)))))
3251      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3252
3253 (defun gnus-summary-insert-line (gnus-tmp-header
3254                                  gnus-tmp-level gnus-tmp-current
3255                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3256                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3257                                  &optional gnus-tmp-dummy gnus-tmp-score
3258                                  gnus-tmp-process)
3259   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3260          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3261          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3262          (gnus-tmp-score-char
3263           (if (or (null gnus-summary-default-score)
3264                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3265                       gnus-summary-zcore-fuzz))
3266               ?\ ;;;Whitespace
3267             (if (< gnus-tmp-score gnus-summary-default-score)
3268                 gnus-score-below-mark gnus-score-over-mark)))
3269          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3270          (gnus-tmp-replied
3271           (cond (gnus-tmp-process gnus-process-mark)
3272                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3273                  gnus-cached-mark)
3274                 (gnus-tmp-replied gnus-replied-mark)
3275                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3276                  gnus-forwarded-mark)
3277                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3278                  gnus-saved-mark)
3279                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3280                  gnus-recent-mark)
3281                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3282                  gnus-unseen-mark)
3283                 (t gnus-no-mark)))
3284          (gnus-tmp-downloaded
3285           (cond (undownloaded
3286                  gnus-undownloaded-mark)
3287                 (gnus-newsgroup-agentized
3288                  gnus-downloaded-mark)
3289                 (t
3290                  gnus-no-mark)))
3291          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3292          (gnus-tmp-name
3293           (cond
3294            ((string-match "<[^>]+> *$" gnus-tmp-from)
3295             (let ((beg (match-beginning 0)))
3296               (or (and (string-match "^\".+\"" gnus-tmp-from)
3297                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3298                   (substring gnus-tmp-from 0 beg))))
3299            ((string-match "(.+)" gnus-tmp-from)
3300             (substring gnus-tmp-from
3301                        (1+ (match-beginning 0)) (1- (match-end 0))))
3302            (t gnus-tmp-from)))
3303          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3304          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3305          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3306          (buffer-read-only nil))
3307     (when (string= gnus-tmp-name "")
3308       (setq gnus-tmp-name gnus-tmp-from))
3309     (unless (numberp gnus-tmp-lines)
3310       (setq gnus-tmp-lines -1))
3311     (if (= gnus-tmp-lines -1)
3312         (setq gnus-tmp-lines "?")
3313       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3314     (gnus-put-text-property-excluding-characters-with-faces
3315      (point)
3316      (progn (eval gnus-summary-line-format-spec) (point))
3317      'gnus-number gnus-tmp-number)
3318     (when (gnus-visual-p 'summary-highlight 'highlight)
3319       (forward-line -1)
3320       (gnus-run-hooks 'gnus-summary-update-hook)
3321       (forward-line 1))))
3322
3323 (defun gnus-summary-update-line (&optional dont-update)
3324   "Update summary line after change."
3325   (when (and gnus-summary-default-score
3326              (not gnus-summary-inhibit-highlight))
3327     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3328            (article (gnus-summary-article-number))
3329            (score (gnus-summary-article-score article)))
3330       (unless dont-update
3331         (if (and gnus-summary-mark-below
3332                  (< (gnus-summary-article-score)
3333                     gnus-summary-mark-below))
3334             ;; This article has a low score, so we mark it as read.
3335             (when (memq article gnus-newsgroup-unreads)
3336               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3337           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3338             ;; This article was previously marked as read on account
3339             ;; of a low score, but now it has risen, so we mark it as
3340             ;; unread.
3341             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3342         (gnus-summary-update-mark
3343          (if (or (null gnus-summary-default-score)
3344                  (<= (abs (- score gnus-summary-default-score))
3345                      gnus-summary-zcore-fuzz))
3346              ?\ ;;;Whitespace
3347            (if (< score gnus-summary-default-score)
3348                gnus-score-below-mark gnus-score-over-mark))
3349          'score))
3350       ;; Do visual highlighting.
3351       (when (gnus-visual-p 'summary-highlight 'highlight)
3352         (gnus-run-hooks 'gnus-summary-update-hook)))))
3353
3354 (defvar gnus-tmp-new-adopts nil)
3355
3356 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3357   "Return the number of articles in THREAD.
3358 This may be 0 in some cases -- if none of the articles in
3359 the thread are to be displayed."
3360   (let* ((number
3361           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3362           (cond
3363            ((not (listp thread))
3364             1)
3365            ((and (consp thread) (cdr thread))
3366             (apply
3367              '+ 1 (mapcar
3368                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3369            ((null thread)
3370             1)
3371            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3372             1)
3373            (t 0))))
3374     (when (and level (zerop level) gnus-tmp-new-adopts)
3375       (incf number
3376             (apply '+ (mapcar
3377                        'gnus-summary-number-of-articles-in-thread
3378                        gnus-tmp-new-adopts))))
3379     (if char
3380         (if (> number 1) gnus-not-empty-thread-mark
3381           gnus-empty-thread-mark)
3382       number)))
3383
3384 (defsubst gnus-summary-line-message-size (head)
3385   "Return pretty-printed version of message size.
3386 This function is intended to be used in
3387 `gnus-summary-line-format-alist'."
3388   (let ((c (or (mail-header-chars head) -1)))
3389     (cond ((< c 0) "n/a")               ; chars not available
3390           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3391           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3392           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3393           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3394
3395
3396 (defun gnus-summary-set-local-parameters (group)
3397   "Go through the local params of GROUP and set all variable specs in that list."
3398   (let ((params (gnus-group-find-parameter group))
3399         (vars '(quit-config))           ; Ignore quit-config.
3400         elem)
3401     (while params
3402       (setq elem (car params)
3403             params (cdr params))
3404       (and (consp elem)                 ; Has to be a cons.
3405            (consp (cdr elem))           ; The cdr has to be a list.
3406            (symbolp (car elem))         ; Has to be a symbol in there.
3407            (not (memq (car elem) vars))
3408            (ignore-errors               ; So we set it.
3409              (push (car elem) vars)
3410              (make-local-variable (car elem))
3411              (set (car elem) (eval (nth 1 elem))))))))
3412
3413 (defun gnus-summary-read-group (group &optional show-all no-article
3414                                       kill-buffer no-display backward
3415                                       select-articles)
3416   "Start reading news in newsgroup GROUP.
3417 If SHOW-ALL is non-nil, already read articles are also listed.
3418 If NO-ARTICLE is non-nil, no article is selected initially.
3419 If NO-DISPLAY, don't generate a summary buffer."
3420   (let (result)
3421     (while (and group
3422                 (null (setq result
3423                             (let ((gnus-auto-select-next nil))
3424                               (or (gnus-summary-read-group-1
3425                                    group show-all no-article
3426                                    kill-buffer no-display
3427                                    select-articles)
3428                                   (setq show-all nil
3429                                         select-articles nil)))))
3430                 (eq gnus-auto-select-next 'quietly))
3431       (set-buffer gnus-group-buffer)
3432       ;; The entry function called above goes to the next
3433       ;; group automatically, so we go two groups back
3434       ;; if we are searching for the previous group.
3435       (when backward
3436         (gnus-group-prev-unread-group 2))
3437       (if (not (equal group (gnus-group-group-name)))
3438           (setq group (gnus-group-group-name))
3439         (setq group nil)))
3440     result))
3441
3442 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3443   "Directly jump to the other GROUP from summary buffer.
3444 If SHOW-ALL is non-nil, already read articles are also listed."
3445   (interactive
3446    (if (eq gnus-summary-buffer (current-buffer))
3447        (list (completing-read
3448               "Group: " gnus-active-hashtb nil t
3449               (when (and gnus-newsgroup-name
3450                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3451                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3452               'gnus-group-history)
3453              current-prefix-arg)
3454      (error "%s must be invoked from a gnus summary buffer." this-command)))
3455   (unless (or (zerop (length group))
3456               (and gnus-newsgroup-name
3457                    (string-equal gnus-newsgroup-name group)))
3458     (gnus-summary-exit)
3459     (gnus-summary-read-group group show-all
3460                              gnus-dont-select-after-jump-to-other-group)))
3461
3462 (defun gnus-summary-read-group-1 (group show-all no-article
3463                                         kill-buffer no-display
3464                                         &optional select-articles)
3465   ;; Killed foreign groups can't be entered.
3466   ;;  (when (and (not (gnus-group-native-p group))
3467   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3468   ;;    (error "Dead non-native groups can't be entered"))
3469   (gnus-message 5 "Retrieving newsgroup: %s..."
3470                 (gnus-group-decoded-name group))
3471   (let* ((new-group (gnus-summary-setup-buffer group))
3472          (quit-config (gnus-group-quit-config group))
3473          (did-select (and new-group (gnus-select-newsgroup
3474                                      group show-all select-articles))))
3475     (cond
3476      ;; This summary buffer exists already, so we just select it.
3477      ((not new-group)
3478       (gnus-set-global-variables)
3479       (when kill-buffer
3480         (gnus-kill-or-deaden-summary kill-buffer))
3481       (gnus-configure-windows 'summary 'force)
3482       (gnus-set-mode-line 'summary)
3483       (gnus-summary-position-point)
3484       (message "")
3485       t)
3486      ;; We couldn't select this group.
3487      ((null did-select)
3488       (when (and (eq major-mode 'gnus-summary-mode)
3489                  (not (equal (current-buffer) kill-buffer)))
3490         (kill-buffer (current-buffer))
3491         (if (not quit-config)
3492             (progn
3493               ;; Update the info -- marks might need to be removed,
3494               ;; for instance.
3495               (gnus-summary-update-info)
3496               (set-buffer gnus-group-buffer)
3497               (gnus-group-jump-to-group group)
3498               (gnus-group-next-unread-group 1))
3499           (gnus-handle-ephemeral-exit quit-config)))
3500       (let ((grpinfo (gnus-get-info group)))
3501         (if (null (gnus-info-read grpinfo))
3502             (gnus-message 3 "Group %s contains no messages"
3503                           (gnus-group-decoded-name group))
3504           (gnus-message 3 "Can't select group")))
3505       nil)
3506      ;; The user did a `C-g' while prompting for number of articles,
3507      ;; so we exit this group.
3508      ((eq did-select 'quit)
3509       (and (eq major-mode 'gnus-summary-mode)
3510            (not (equal (current-buffer) kill-buffer))
3511            (kill-buffer (current-buffer)))
3512       (when kill-buffer
3513         (gnus-kill-or-deaden-summary kill-buffer))
3514       (if (not quit-config)
3515           (progn
3516             (set-buffer gnus-group-buffer)
3517             (gnus-group-jump-to-group group)
3518             (gnus-group-next-unread-group 1)
3519             (gnus-configure-windows 'group 'force))
3520         (gnus-handle-ephemeral-exit quit-config))
3521       ;; Finally signal the quit.
3522       (signal 'quit nil))
3523      ;; The group was successfully selected.
3524      (t
3525       (gnus-set-global-variables)
3526       ;; Save the active value in effect when the group was entered.
3527       (setq gnus-newsgroup-active
3528             (gnus-copy-sequence
3529              (gnus-active gnus-newsgroup-name)))
3530       ;; You can change the summary buffer in some way with this hook.
3531       (gnus-run-hooks 'gnus-select-group-hook)
3532       (gnus-update-format-specifications
3533        nil 'summary 'summary-mode 'summary-dummy)
3534       (gnus-update-summary-mark-positions)
3535       ;; Do score processing.
3536       (when gnus-use-scoring
3537         (gnus-possibly-score-headers))
3538       ;; Check whether to fill in the gaps in the threads.
3539       (when gnus-build-sparse-threads
3540         (gnus-build-sparse-threads))
3541       ;; Find the initial limit.
3542       (if gnus-show-threads
3543           (if show-all
3544               (let ((gnus-newsgroup-dormant nil))
3545                 (gnus-summary-initial-limit show-all))
3546             (gnus-summary-initial-limit show-all))
3547         ;; When unthreaded, all articles are always shown.
3548         (setq gnus-newsgroup-limit
3549               (mapcar
3550                (lambda (header) (mail-header-number header))
3551                gnus-newsgroup-headers)))
3552       ;; Generate the summary buffer.
3553       (unless no-display
3554         (gnus-summary-prepare))
3555       (when gnus-use-trees
3556         (gnus-tree-open group)
3557         (setq gnus-summary-highlight-line-function
3558               'gnus-tree-highlight-article))
3559       ;; If the summary buffer is empty, but there are some low-scored
3560       ;; articles or some excluded dormants, we include these in the
3561       ;; buffer.
3562       (when (and (zerop (buffer-size))
3563                  (not no-display))
3564         (cond (gnus-newsgroup-dormant
3565                (gnus-summary-limit-include-dormant))
3566               ((and gnus-newsgroup-scored show-all)
3567                (gnus-summary-limit-include-expunged t))))
3568       ;; Function `gnus-apply-kill-file' must be called in this hook.
3569       (gnus-run-hooks 'gnus-apply-kill-hook)
3570       (if (and (zerop (buffer-size))
3571                (not no-display))
3572           (progn
3573             ;; This newsgroup is empty.
3574             (gnus-summary-catchup-and-exit nil t)
3575             (gnus-message 6 "No unread news")
3576             (when kill-buffer
3577               (gnus-kill-or-deaden-summary kill-buffer))
3578             ;; Return nil from this function.
3579             nil)
3580         ;; Hide conversation thread subtrees.  We cannot do this in
3581         ;; gnus-summary-prepare-hook since kill processing may not
3582         ;; work with hidden articles.
3583         (gnus-summary-maybe-hide-threads)
3584         (when kill-buffer
3585           (gnus-kill-or-deaden-summary kill-buffer))
3586         (gnus-summary-auto-select-subject)
3587         ;; Show first unread article if requested.
3588         (if (and (not no-article)
3589                  (not no-display)
3590                  gnus-newsgroup-unreads
3591                  gnus-auto-select-first)
3592             (progn
3593               (gnus-configure-windows 'summary)
3594               (let ((art (gnus-summary-article-number)))
3595                 (unless (and (not gnus-plugged)
3596                              (or (memq art gnus-newsgroup-undownloaded)
3597                                  (memq art gnus-newsgroup-downloadable)))
3598                   (gnus-summary-goto-article art))))
3599           ;; Don't select any articles.
3600           (gnus-summary-position-point)
3601           (gnus-configure-windows 'summary 'force)
3602           (gnus-set-mode-line 'summary))
3603         (when (and gnus-auto-center-group
3604                    (get-buffer-window gnus-group-buffer t))
3605           ;; Gotta use windows, because recenter does weird stuff if
3606           ;; the current buffer ain't the displayed window.
3607           (let ((owin (selected-window)))
3608             (select-window (get-buffer-window gnus-group-buffer t))
3609             (when (gnus-group-goto-group group)
3610               (recenter))
3611             (select-window owin)))
3612         ;; Mark this buffer as "prepared".
3613         (setq gnus-newsgroup-prepared t)
3614         (gnus-run-hooks 'gnus-summary-prepared-hook)
3615         (unless (gnus-ephemeral-group-p group)
3616           (gnus-group-update-group group))
3617         t)))))
3618
3619 (defun gnus-summary-auto-select-subject ()
3620   "Select the subject line on initial group entry."
3621   (goto-char (point-min))
3622   (cond
3623    ((eq gnus-auto-select-subject 'best)
3624     (gnus-summary-best-unread-subject))
3625    ((eq gnus-auto-select-subject 'unread)
3626     (gnus-summary-first-unread-subject))
3627    ((eq gnus-auto-select-subject 'unseen)
3628     (gnus-summary-first-unseen-subject))
3629    ((eq gnus-auto-select-subject 'unseen-or-unread)
3630     (gnus-summary-first-unseen-or-unread-subject))
3631    ((eq gnus-auto-select-subject 'first)
3632     ;; Do nothing.
3633     )
3634    ((functionp gnus-auto-select-subject)
3635     (funcall gnus-auto-select-subject))))
3636
3637 (defun gnus-summary-prepare ()
3638   "Generate the summary buffer."
3639   (interactive)
3640   (let ((buffer-read-only nil))
3641     (erase-buffer)
3642     (setq gnus-newsgroup-data nil
3643           gnus-newsgroup-data-reverse nil)
3644     (gnus-run-hooks 'gnus-summary-generate-hook)
3645     ;; Generate the buffer, either with threads or without.
3646     (when gnus-newsgroup-headers
3647       (gnus-summary-prepare-threads
3648        (if gnus-show-threads
3649            (gnus-sort-gathered-threads
3650             (funcall gnus-summary-thread-gathering-function
3651                      (gnus-sort-threads
3652                       (gnus-cut-threads (gnus-make-threads)))))
3653          ;; Unthreaded display.
3654          (gnus-sort-articles gnus-newsgroup-headers))))
3655     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3656     ;; Call hooks for modifying summary buffer.
3657     (goto-char (point-min))
3658     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3659
3660 (defsubst gnus-general-simplify-subject (subject)
3661   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3662   (setq subject
3663         (cond
3664          ;; Truncate the subject.
3665          (gnus-simplify-subject-functions
3666           (gnus-map-function gnus-simplify-subject-functions subject))
3667          ((numberp gnus-summary-gather-subject-limit)
3668           (setq subject (gnus-simplify-subject-re subject))
3669           (if (> (length subject) gnus-summary-gather-subject-limit)
3670               (substring subject 0 gnus-summary-gather-subject-limit)
3671             subject))
3672          ;; Fuzzily simplify it.
3673          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3674           (gnus-simplify-subject-fuzzy subject))
3675          ;; Just remove the leading "Re:".
3676          (t
3677           (gnus-simplify-subject-re subject))))
3678
3679   (if (and gnus-summary-gather-exclude-subject
3680            (string-match gnus-summary-gather-exclude-subject subject))
3681       nil                               ; This article shouldn't be gathered
3682     subject))
3683
3684 (defun gnus-summary-simplify-subject-query ()
3685   "Query where the respool algorithm would put this article."
3686   (interactive)
3687   (gnus-summary-select-article)
3688   (message "%s"
3689            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3690
3691 (defun gnus-gather-threads-by-subject (threads)
3692   "Gather threads by looking at Subject headers."
3693   (if (not gnus-summary-make-false-root)
3694       threads
3695     (let ((hashtb (gnus-make-hashtable 1024))
3696           (prev threads)
3697           (result threads)
3698           subject hthread whole-subject)
3699       (while threads
3700         (setq subject (gnus-general-simplify-subject
3701                        (setq whole-subject (mail-header-subject
3702                                             (caar threads)))))
3703         (when subject
3704           (if (setq hthread (gnus-gethash subject hashtb))
3705               (progn
3706                 ;; We enter a dummy root into the thread, if we
3707                 ;; haven't done that already.
3708                 (unless (stringp (caar hthread))
3709                   (setcar hthread (list whole-subject (car hthread))))
3710                 ;; We add this new gathered thread to this gathered
3711                 ;; thread.
3712                 (setcdr (car hthread)
3713                         (nconc (cdar hthread) (list (car threads))))
3714                 ;; Remove it from the list of threads.
3715                 (setcdr prev (cdr threads))
3716                 (setq threads prev))
3717             ;; Enter this thread into the hash table.
3718             (gnus-sethash subject
3719                           (if gnus-summary-make-false-root-always
3720                               (progn
3721                                 ;; If you want a dummy root above all
3722                                 ;; threads...
3723                                 (setcar threads (list whole-subject
3724                                                       (car threads)))
3725                                 threads)
3726                             threads)
3727                           hashtb)))
3728         (setq prev threads)
3729         (setq threads (cdr threads)))
3730       result)))
3731
3732 (defun gnus-gather-threads-by-references (threads)
3733   "Gather threads by looking at References headers."
3734   (let ((idhashtb (gnus-make-hashtable 1024))
3735         (thhashtb (gnus-make-hashtable 1024))
3736         (prev threads)
3737         (result threads)
3738         ids references id gthread gid entered ref)
3739     (while threads
3740       (when (setq references (mail-header-references (caar threads)))
3741         (setq id (mail-header-id (caar threads))
3742               ids (inline (gnus-split-references references))
3743               entered nil)
3744         (while (setq ref (pop ids))
3745           (setq ids (delete ref ids))
3746           (if (not (setq gid (gnus-gethash ref idhashtb)))
3747               (progn
3748                 (gnus-sethash ref id idhashtb)
3749                 (gnus-sethash id threads thhashtb))
3750             (setq gthread (gnus-gethash gid thhashtb))
3751             (unless entered
3752               ;; We enter a dummy root into the thread, if we
3753               ;; haven't done that already.
3754               (unless (stringp (caar gthread))
3755                 (setcar gthread (list (mail-header-subject (caar gthread))
3756                                       (car gthread))))
3757               ;; We add this new gathered thread to this gathered
3758               ;; thread.
3759               (setcdr (car gthread)
3760                       (nconc (cdar gthread) (list (car threads)))))
3761             ;; Add it into the thread hash table.
3762             (gnus-sethash id gthread thhashtb)
3763             (setq entered t)
3764             ;; Remove it from the list of threads.
3765             (setcdr prev (cdr threads))
3766             (setq threads prev))))
3767       (setq prev threads)
3768       (setq threads (cdr threads)))
3769     result))
3770
3771 (defun gnus-sort-gathered-threads (threads)
3772   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3773   (let ((result threads))
3774     (while threads
3775       (when (stringp (caar threads))
3776         (setcdr (car threads)
3777                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3778       (setq threads (cdr threads)))
3779     result))
3780
3781 (defun gnus-thread-loop-p (root thread)
3782   "Say whether ROOT is in THREAD."
3783   (let ((stack (list thread))
3784         (infloop 0)
3785         th)
3786     (while (setq thread (pop stack))
3787       (setq th (cdr thread))
3788       (while (and th
3789                   (not (eq (caar th) root)))
3790         (pop th))
3791       (if th
3792           ;; We have found a loop.
3793           (let (ref-dep)
3794             (setcdr thread (delq (car th) (cdr thread)))
3795             (if (boundp (setq ref-dep (intern "none"
3796                                               gnus-newsgroup-dependencies)))
3797                 (setcdr (symbol-value ref-dep)
3798                         (nconc (cdr (symbol-value ref-dep))
3799                                (list (car th))))
3800               (set ref-dep (list nil (car th))))
3801             (setq infloop 1
3802                   stack nil))
3803         ;; Push all the subthreads onto the stack.
3804         (push (cdr thread) stack)))
3805     infloop))
3806
3807 (defun gnus-make-threads ()
3808   "Go through the dependency hashtb and find the roots.  Return all threads."
3809   (let (threads)
3810     (while (catch 'infloop
3811              (mapatoms
3812               (lambda (refs)
3813                 ;; Deal with self-referencing References loops.
3814                 (when (and (car (symbol-value refs))
3815                            (not (zerop
3816                                  (apply
3817                                   '+
3818                                   (mapcar
3819                                    (lambda (thread)
3820                                      (gnus-thread-loop-p
3821                                       (car (symbol-value refs)) thread))
3822                                    (cdr (symbol-value refs)))))))
3823                   (setq threads nil)
3824                   (throw 'infloop t))
3825                 (unless (car (symbol-value refs))
3826                   ;; These threads do not refer back to any other articles,
3827                   ;; so they're roots.
3828                   (setq threads (append (cdr (symbol-value refs)) threads))))
3829               gnus-newsgroup-dependencies)))
3830     threads))
3831
3832 ;; Build the thread tree.
3833 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3834   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3835
3836 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3837 if it was already present.
3838
3839 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3840 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3841 Message-IDs will be renamed to a unique Message-ID before being
3842 entered.
3843
3844 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3845   (let* ((id (mail-header-id header))
3846          (id-dep (and id (intern id dependencies)))
3847          parent-id ref ref-dep ref-header replaced)
3848     ;; Enter this `header' in the `dependencies' table.
3849     (cond
3850      ((not id-dep)
3851       (setq header nil))
3852      ;; The first two cases do the normal part: enter a new `header'
3853      ;; in the `dependencies' table.
3854      ((not (boundp id-dep))
3855       (set id-dep (list header)))
3856      ((null (car (symbol-value id-dep)))
3857       (setcar (symbol-value id-dep) header))
3858
3859      ;; From here the `header' was already present in the
3860      ;; `dependencies' table.
3861      (force-new
3862       ;; Overrides an existing entry;
3863       ;; just set the header part of the entry.
3864       (setcar (symbol-value id-dep) header)
3865       (setq replaced t))
3866
3867      ;; Renames the existing `header' to a unique Message-ID.
3868      ((not gnus-summary-ignore-duplicates)
3869       ;; An article with this Message-ID has already been seen.
3870       ;; We rename the Message-ID.
3871       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3872            (list header))
3873       (mail-header-set-id header id))
3874
3875      ;; The last case ignores an existing entry, except it adds any
3876      ;; additional Xrefs (in case the two articles came from different
3877      ;; servers.
3878      ;; Also sets `header' to `nil' meaning that the `dependencies'
3879      ;; table was *not* modified.
3880      (t
3881       (mail-header-set-xref
3882        (car (symbol-value id-dep))
3883        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3884                    "")
3885                (or (mail-header-xref header) "")))
3886       (setq header nil)))
3887
3888     (when (and header (not replaced))
3889       ;; First check that we are not creating a References loop.
3890       (setq parent-id (gnus-parent-id (mail-header-references header)))
3891       (setq ref parent-id)
3892       (while (and ref
3893                   (setq ref-dep (intern-soft ref dependencies))
3894                   (boundp ref-dep)
3895                   (setq ref-header (car (symbol-value ref-dep))))
3896         (if (string= id ref)
3897             ;; Yuk!  This is a reference loop.  Make the article be a
3898             ;; root article.
3899             (progn
3900               (mail-header-set-references (car (symbol-value id-dep)) "none")
3901               (setq ref nil)
3902               (setq parent-id nil))
3903           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3904       (setq ref-dep (intern (or parent-id "none") dependencies))
3905       (if (boundp ref-dep)
3906           (setcdr (symbol-value ref-dep)
3907                   (nconc (cdr (symbol-value ref-dep))
3908                          (list (symbol-value id-dep))))
3909         (set ref-dep (list nil (symbol-value id-dep)))))
3910     header))
3911
3912 (defun gnus-extract-message-id-from-in-reply-to (string)
3913   (if (string-match "<[^>]+>" string)
3914       (substring string (match-beginning 0) (match-end 0))
3915     nil))
3916
3917 (defun gnus-build-sparse-threads ()
3918   (let ((headers gnus-newsgroup-headers)
3919         (mail-parse-charset gnus-newsgroup-charset)
3920         (gnus-summary-ignore-duplicates t)
3921         header references generation relations
3922         subject child end new-child date)
3923     ;; First we create an alist of generations/relations, where
3924     ;; generations is how much we trust the relation, and the relation
3925     ;; is parent/child.
3926     (gnus-message 7 "Making sparse threads...")
3927     (save-excursion
3928       (nnheader-set-temp-buffer " *gnus sparse threads*")
3929       (while (setq header (pop headers))
3930         (when (and (setq references (mail-header-references header))
3931                    (not (string= references "")))
3932           (insert references)
3933           (setq child (mail-header-id header)
3934                 subject (mail-header-subject header)
3935                 date (mail-header-date header)
3936                 generation 0)
3937           (while (search-backward ">" nil t)
3938             (setq end (1+ (point)))
3939             (when (search-backward "<" nil t)
3940               (setq new-child (buffer-substring (point) end))
3941               (push (list (incf generation)
3942                           child (setq child new-child)
3943                           subject date)
3944                     relations)))
3945           (when child
3946             (push (list (1+ generation) child nil subject) relations))
3947           (erase-buffer)))
3948       (kill-buffer (current-buffer)))
3949     ;; Sort over trustworthiness.
3950     (mapcar
3951      (lambda (relation)
3952        (when (gnus-dependencies-add-header
3953               (make-full-mail-header-from-decoded-header
3954                gnus-reffed-article-number
3955                (nth 3 relation) "" (or (nth 4 relation) "")
3956                (nth 1 relation)
3957                (or (nth 2 relation) "") 0 0 "")
3958               gnus-newsgroup-dependencies nil)
3959          (push gnus-reffed-article-number gnus-newsgroup-limit)
3960          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3961          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3962                gnus-newsgroup-reads)
3963          (decf gnus-reffed-article-number)))
3964      (sort relations 'car-less-than-car))
3965     (gnus-message 7 "Making sparse threads...done")))
3966
3967 (defun gnus-build-old-threads ()
3968   ;; Look at all the articles that refer back to old articles, and
3969   ;; fetch the headers for the articles that aren't there.  This will
3970   ;; build complete threads - if the roots haven't been expired by the
3971   ;; server, that is.
3972   (let ((mail-parse-charset gnus-newsgroup-charset)
3973         id heads)
3974     (mapatoms
3975      (lambda (refs)
3976        (when (not (car (symbol-value refs)))
3977          (setq heads (cdr (symbol-value refs)))
3978          (while heads
3979            (if (memq (mail-header-number (caar heads))
3980                      gnus-newsgroup-dormant)
3981                (setq heads (cdr heads))
3982              (setq id (symbol-name refs))
3983              (while (and (setq id (gnus-build-get-header id))
3984                          (not (car (gnus-id-to-thread id)))))
3985              (setq heads nil)))))
3986      gnus-newsgroup-dependencies)))
3987
3988 (defsubst gnus-remove-odd-characters (string)
3989   "Translate STRING into something that doesn't contain weird characters."
3990   (mm-subst-char-in-string
3991    ?\r ?\-
3992    (mm-subst-char-in-string ?\n ?\- string t) t))
3993
3994 ;; This function has to be called with point after the article number
3995 ;; on the beginning of the line.
3996 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3997   (let ((eol (point-at-eol))
3998         (buffer (current-buffer))
3999         header references in-reply-to)
4000
4001     ;; overview: [num subject from date id refs chars lines misc]
4002     (unwind-protect
4003         (progn
4004           (narrow-to-region (point) eol)
4005           (unless (eobp)
4006             (forward-char))
4007
4008           (setq header
4009                 (make-full-mail-header
4010                  number                         ; number
4011                  (nnheader-nov-field)           ; subject
4012                  (nnheader-nov-field)           ; from
4013                  (nnheader-nov-field)           ; date
4014                  (nnheader-nov-read-message-id) ; id
4015                  (nnheader-nov-field)           ; refs
4016                  (nnheader-nov-read-integer)    ; chars
4017                  (nnheader-nov-read-integer)    ; lines
4018                  (unless (eobp)
4019                    (if (looking-at "Xref: ")
4020                        (goto-char (match-end 0)))
4021                    (nnheader-nov-field))        ; Xref
4022                  (nnheader-nov-parse-extra))))  ; extra
4023
4024       (widen))
4025
4026     (when (and (string= references "")
4027                (setq in-reply-to (mail-header-extra header))
4028                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4029       (mail-header-set-references
4030        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4031
4032     (when gnus-alter-header-function
4033       (funcall gnus-alter-header-function header))
4034     (gnus-dependencies-add-header header dependencies force-new)))
4035
4036 (defun gnus-build-get-header (id)
4037   "Look through the buffer of NOV lines and find the header to ID.
4038 Enter this line into the dependencies hash table, and return
4039 the id of the parent article (if any)."
4040   (let ((deps gnus-newsgroup-dependencies)
4041         found header)
4042     (prog1
4043         (save-excursion
4044           (set-buffer nntp-server-buffer)
4045           (let ((case-fold-search nil))
4046             (goto-char (point-min))
4047             (while (and (not found)
4048                         (search-forward id nil t))
4049               (beginning-of-line)
4050               (setq found (looking-at
4051                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4052                                    (regexp-quote id))))
4053               (or found (beginning-of-line 2)))
4054             (when found
4055               (beginning-of-line)
4056               (and
4057                (setq header (gnus-nov-parse-line
4058                              (read (current-buffer)) deps))
4059                (gnus-parent-id (mail-header-references header))))))
4060       (when header
4061         (let ((number (mail-header-number header)))
4062           (push number gnus-newsgroup-limit)
4063           (push header gnus-newsgroup-headers)
4064           (if (memq number gnus-newsgroup-unselected)
4065               (progn
4066                 (setq gnus-newsgroup-unreads
4067                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4068                                                number))
4069                 (setq gnus-newsgroup-unselected
4070                       (delq number gnus-newsgroup-unselected)))
4071             (push number gnus-newsgroup-ancient)))))))
4072
4073 (defun gnus-build-all-threads ()
4074   "Read all the headers."
4075   (let ((gnus-summary-ignore-duplicates t)
4076         (mail-parse-charset gnus-newsgroup-charset)
4077         (dependencies gnus-newsgroup-dependencies)
4078         header article)
4079     (save-excursion
4080       (set-buffer nntp-server-buffer)
4081       (let ((case-fold-search nil))
4082         (goto-char (point-min))
4083         (while (not (eobp))
4084           (ignore-errors
4085             (setq article (read (current-buffer))
4086                   header (gnus-nov-parse-line article dependencies)))
4087           (when header
4088             (save-excursion
4089               (set-buffer gnus-summary-buffer)
4090               (push header gnus-newsgroup-headers)
4091               (if (memq (setq article (mail-header-number header))
4092                         gnus-newsgroup-unselected)
4093                   (progn
4094                     (setq gnus-newsgroup-unreads
4095                           (gnus-add-to-sorted-list
4096                            gnus-newsgroup-unreads article))
4097                     (setq gnus-newsgroup-unselected
4098                           (delq article gnus-newsgroup-unselected)))
4099                 (push article gnus-newsgroup-ancient)))
4100             (forward-line 1)))))))
4101
4102 (defun gnus-summary-update-article-line (article header)
4103   "Update the line for ARTICLE using HEADER."
4104   (let* ((id (mail-header-id header))
4105          (thread (gnus-id-to-thread id)))
4106     (unless thread
4107       (error "Article in no thread"))
4108     ;; Update the thread.
4109     (setcar thread header)
4110     (gnus-summary-goto-subject article)
4111     (let* ((datal (gnus-data-find-list article))
4112            (data (car datal))
4113            (buffer-read-only nil)
4114            (level (gnus-summary-thread-level)))
4115       (gnus-delete-line)
4116       (let ((inserted (- (point)
4117                          (progn
4118                            (gnus-summary-insert-line
4119                             header level nil
4120                             (memq article gnus-newsgroup-undownloaded)
4121                             (gnus-article-mark article)
4122                             (memq article gnus-newsgroup-replied)
4123                             (memq article gnus-newsgroup-expirable)
4124                             ;; Only insert the Subject string when it's different
4125                             ;; from the previous Subject string.
4126                             (if (and
4127                                  gnus-show-threads
4128                                  (gnus-subject-equal
4129                                   (condition-case ()
4130                                       (mail-header-subject
4131                                        (gnus-data-header
4132                                         (cadr
4133                                          (gnus-data-find-list
4134                                           article
4135                                           (gnus-data-list t)))))
4136                                     ;; Error on the side of excessive subjects.
4137                                     (error ""))
4138                                   (mail-header-subject header)))
4139                                 ""
4140                               (mail-header-subject header))
4141                             nil (cdr (assq article gnus-newsgroup-scored))
4142                             (memq article gnus-newsgroup-processable))
4143                            (point)))))
4144         (when (cdr datal)
4145           (gnus-data-update-list
4146            (cdr datal)
4147            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4148
4149 (defun gnus-summary-update-article (article &optional iheader)
4150   "Update ARTICLE in the summary buffer."
4151   (set-buffer gnus-summary-buffer)
4152   (let* ((header (gnus-summary-article-header article))
4153          (id (mail-header-id header))
4154          (data (gnus-data-find article))
4155          (thread (gnus-id-to-thread id))
4156          (references (mail-header-references header))
4157          (parent
4158           (gnus-id-to-thread
4159            (or (gnus-parent-id
4160                 (when (and references
4161                            (not (equal "" references)))
4162                   references))
4163                "none")))
4164          (buffer-read-only nil)
4165          (old (car thread)))
4166     (when thread
4167       (unless iheader
4168         (setcar thread nil)
4169         (when parent
4170           (delq thread parent)))
4171       (if (gnus-summary-insert-subject id header)
4172           ;; Set the (possibly) new article number in the data structure.
4173           (gnus-data-set-number data (gnus-id-to-article id))
4174         (setcar thread old)
4175         nil))))
4176
4177 (defun gnus-rebuild-thread (id &optional line)
4178   "Rebuild the thread containing ID.
4179 If LINE, insert the rebuilt thread starting on line LINE."
4180   (let ((buffer-read-only nil)
4181         old-pos current thread data)
4182     (if (not gnus-show-threads)
4183         (setq thread (list (car (gnus-id-to-thread id))))
4184       ;; Get the thread this article is part of.
4185       (setq thread (gnus-remove-thread id)))
4186     (setq old-pos (point-at-bol))
4187     (setq current (save-excursion
4188                     (and (re-search-backward "[\r\n]" nil t)
4189                          (gnus-summary-article-number))))
4190     ;; If this is a gathered thread, we have to go some re-gathering.
4191     (when (stringp (car thread))
4192       (let ((subject (car thread))
4193             roots thr)
4194         (setq thread (cdr thread))
4195         (while thread
4196           (unless (memq (setq thr (gnus-id-to-thread
4197                                    (gnus-root-id
4198                                     (mail-header-id (caar thread)))))
4199                         roots)
4200             (push thr roots))
4201           (setq thread (cdr thread)))
4202         ;; We now have all (unique) roots.
4203         (if (= (length roots) 1)
4204             ;; All the loose roots are now one solid root.
4205             (setq thread (car roots))
4206           (setq thread (cons subject (gnus-sort-threads roots))))))
4207     (let (threads)
4208       ;; We then insert this thread into the summary buffer.
4209       (when line
4210         (goto-char (point-min))
4211         (forward-line (1- line)))
4212       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4213         (if gnus-show-threads
4214             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4215           (gnus-summary-prepare-unthreaded thread))
4216         (setq data (nreverse gnus-newsgroup-data))
4217         (setq threads gnus-newsgroup-threads))
4218       ;; We splice the new data into the data structure.
4219       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4220       ;;!!! then we want to insert at the beginning of the buffer.
4221       ;;!!! That happens to be true with Gnus now, but that may
4222       ;;!!! change in the future.  Perhaps.
4223       (gnus-data-enter-list
4224        (if line nil current) data (- (point) old-pos))
4225       (setq gnus-newsgroup-threads
4226             (nconc threads gnus-newsgroup-threads))
4227       (gnus-data-compute-positions))))
4228
4229 (defun gnus-number-to-header (number)
4230   "Return the header for article NUMBER."
4231   (let ((headers gnus-newsgroup-headers))
4232     (while (and headers
4233                 (not (= number (mail-header-number (car headers)))))
4234       (pop headers))
4235     (when headers
4236       (car headers))))
4237
4238 (defun gnus-parent-headers (in-headers &optional generation)
4239   "Return the headers of the GENERATIONeth parent of HEADERS."
4240   (unless generation
4241     (setq generation 1))
4242   (let ((parent t)
4243         (headers in-headers)
4244         references)
4245     (while (and parent
4246                 (not (zerop generation))
4247                 (setq references (mail-header-references headers)))
4248       (setq headers (if (and references
4249                              (setq parent (gnus-parent-id references)))
4250                         (car (gnus-id-to-thread parent))
4251                       nil))
4252       (decf generation))
4253     (and (not (eq headers in-headers))
4254          headers)))
4255
4256 (defun gnus-id-to-thread (id)
4257   "Return the (sub-)thread where ID appears."
4258   (gnus-gethash id gnus-newsgroup-dependencies))
4259
4260 (defun gnus-id-to-article (id)
4261   "Return the article number of ID."
4262   (let ((thread (gnus-id-to-thread id)))
4263     (when (and thread
4264                (car thread))
4265       (mail-header-number (car thread)))))
4266
4267 (defun gnus-id-to-header (id)
4268   "Return the article headers of ID."
4269   (car (gnus-id-to-thread id)))
4270
4271 (defun gnus-article-displayed-root-p (article)
4272   "Say whether ARTICLE is a root(ish) article."
4273   (let ((level (gnus-summary-thread-level article))
4274         (refs (mail-header-references  (gnus-summary-article-header article)))
4275         particle)
4276     (cond
4277      ((null level) nil)
4278      ((zerop level) t)
4279      ((null refs) t)
4280      ((null (gnus-parent-id refs)) t)
4281      ((and (= 1 level)
4282            (null (setq particle (gnus-id-to-article
4283                                  (gnus-parent-id refs))))
4284            (null (gnus-summary-thread-level particle)))))))
4285
4286 (defun gnus-root-id (id)
4287   "Return the id of the root of the thread where ID appears."
4288   (let (last-id prev)
4289     (while (and id (setq prev (car (gnus-id-to-thread id))))
4290       (setq last-id id
4291             id (gnus-parent-id (mail-header-references prev))))
4292     last-id))
4293
4294 (defun gnus-articles-in-thread (thread)
4295   "Return the list of articles in THREAD."
4296   (cons (mail-header-number (car thread))
4297         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4298
4299 (defun gnus-remove-thread (id &optional dont-remove)
4300   "Remove the thread that has ID in it."
4301   (let (headers thread last-id)
4302     ;; First go up in this thread until we find the root.
4303     (setq last-id (gnus-root-id id)
4304           headers (message-flatten-list (gnus-id-to-thread last-id)))
4305     ;; We have now found the real root of this thread.  It might have
4306     ;; been gathered into some loose thread, so we have to search
4307     ;; through the threads to find the thread we wanted.
4308     (let ((threads gnus-newsgroup-threads)
4309           sub)
4310       (while threads
4311         (setq sub (car threads))
4312         (if (stringp (car sub))
4313             ;; This is a gathered thread, so we look at the roots
4314             ;; below it to find whether this article is in this
4315             ;; gathered root.
4316             (progn
4317               (setq sub (cdr sub))
4318               (while sub
4319                 (when (member (caar sub) headers)
4320                   (setq thread (car threads)
4321                         threads nil
4322                         sub nil))
4323                 (setq sub (cdr sub))))
4324           ;; It's an ordinary thread, so we check it.
4325           (when (eq (car sub) (car headers))
4326             (setq thread sub
4327                   threads nil)))
4328         (setq threads (cdr threads)))
4329       ;; If this article is in no thread, then it's a root.
4330       (if thread
4331           (unless dont-remove
4332             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4333         (setq thread (gnus-id-to-thread last-id)))
4334       (when thread
4335         (prog1
4336             thread                      ; We return this thread.
4337           (unless dont-remove
4338             (if (stringp (car thread))
4339                 (progn
4340                   ;; If we use dummy roots, then we have to remove the
4341                   ;; dummy root as well.
4342                   (when (eq gnus-summary-make-false-root 'dummy)
4343                     ;; We go to the dummy root by going to
4344                     ;; the first sub-"thread", and then one line up.
4345                     (gnus-summary-goto-article
4346                      (mail-header-number (caadr thread)))
4347                     (forward-line -1)
4348                     (gnus-delete-line)
4349                     (gnus-data-compute-positions))
4350                   (setq thread (cdr thread))
4351                   (while thread
4352                     (gnus-remove-thread-1 (car thread))
4353                     (setq thread (cdr thread))))
4354               (gnus-remove-thread-1 thread))))))))
4355
4356 (defun gnus-remove-thread-1 (thread)
4357   "Remove the thread THREAD recursively."
4358   (let ((number (mail-header-number (pop thread)))
4359         d)
4360     (setq thread (reverse thread))
4361     (while thread
4362       (gnus-remove-thread-1 (pop thread)))
4363     (when (setq d (gnus-data-find number))
4364       (goto-char (gnus-data-pos d))
4365       (gnus-summary-show-thread)
4366       (gnus-data-remove
4367        number
4368        (- (point-at-bol)
4369           (prog1
4370               (1+ (point-at-eol))
4371             (gnus-delete-line)))))))
4372
4373 (defun gnus-sort-threads-1 (threads func)
4374   (sort (mapcar (lambda (thread)
4375                   (cons (car thread)
4376                         (and (cdr thread)
4377                              (gnus-sort-threads-1 (cdr thread) func))))
4378                 threads) func))
4379
4380 (defun gnus-sort-threads (threads)
4381   "Sort THREADS."
4382   (if (not gnus-thread-sort-functions)
4383       threads
4384     (gnus-message 8 "Sorting threads...")
4385     (let ((max-lisp-eval-depth 5000))
4386       (prog1 (gnus-sort-threads-1
4387          threads
4388          (gnus-make-sort-function gnus-thread-sort-functions))
4389         (gnus-message 8 "Sorting threads...done")))))
4390
4391 (defun gnus-sort-articles (articles)
4392   "Sort ARTICLES."
4393   (when gnus-article-sort-functions
4394     (gnus-message 7 "Sorting articles...")
4395     (prog1
4396         (setq gnus-newsgroup-headers
4397               (sort articles (gnus-make-sort-function
4398                               gnus-article-sort-functions)))
4399       (gnus-message 7 "Sorting articles...done"))))
4400
4401 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4402 (defmacro gnus-thread-header (thread)
4403   "Return header of first article in THREAD.
4404 Note that THREAD must never, ever be anything else than a variable -
4405 using some other form will lead to serious barfage."
4406   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4407   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4408   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4409         (vector thread) 2))
4410
4411 (defsubst gnus-article-sort-by-number (h1 h2)
4412   "Sort articles by article number."
4413   (< (mail-header-number h1)
4414      (mail-header-number h2)))
4415
4416 (defun gnus-thread-sort-by-number (h1 h2)
4417   "Sort threads by root article number."
4418   (gnus-article-sort-by-number
4419    (gnus-thread-header h1) (gnus-thread-header h2)))
4420
4421 (defsubst gnus-article-sort-by-random (h1 h2)
4422   "Sort articles by article number."
4423   (zerop (random 2)))
4424
4425 (defun gnus-thread-sort-by-random (h1 h2)
4426   "Sort threads by root article number."
4427   (gnus-article-sort-by-random
4428    (gnus-thread-header h1) (gnus-thread-header h2)))
4429
4430 (defsubst gnus-article-sort-by-lines (h1 h2)
4431   "Sort articles by article Lines header."
4432   (< (mail-header-lines h1)
4433      (mail-header-lines h2)))
4434
4435 (defun gnus-thread-sort-by-lines (h1 h2)
4436   "Sort threads by root article Lines header."
4437   (gnus-article-sort-by-lines
4438    (gnus-thread-header h1) (gnus-thread-header h2)))
4439
4440 (defsubst gnus-article-sort-by-chars (h1 h2)
4441   "Sort articles by octet length."
4442   (< (mail-header-chars h1)
4443      (mail-header-chars h2)))
4444
4445 (defun gnus-thread-sort-by-chars (h1 h2)
4446   "Sort threads by root article octet length."
4447   (gnus-article-sort-by-chars
4448    (gnus-thread-header h1) (gnus-thread-header h2)))
4449
4450 (defsubst gnus-article-sort-by-author (h1 h2)
4451   "Sort articles by root author."
4452   (string-lessp
4453    (let ((addr (car (mime-entity-read-field h1 'From))))
4454      (or (std11-full-name-string addr)
4455          (std11-address-string addr)
4456          ""))
4457    (let ((addr (car (mime-entity-read-field h2 'From))))
4458      (or (std11-full-name-string addr)
4459          (std11-address-string addr)
4460          ""))
4461    ))
4462
4463 (defun gnus-thread-sort-by-author (h1 h2)
4464   "Sort threads by root author."
4465   (gnus-article-sort-by-author
4466    (gnus-thread-header h1)  (gnus-thread-header h2)))
4467
4468 (defsubst gnus-article-sort-by-subject (h1 h2)
4469   "Sort articles by root subject."
4470   (string-lessp
4471    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4472    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4473
4474 (defun gnus-thread-sort-by-subject (h1 h2)
4475   "Sort threads by root subject."
4476   (gnus-article-sort-by-subject
4477    (gnus-thread-header h1) (gnus-thread-header h2)))
4478
4479 (defsubst gnus-article-sort-by-date (h1 h2)
4480   "Sort articles by root article date."
4481   (time-less-p
4482    (gnus-date-get-time (mail-header-date h1))
4483    (gnus-date-get-time (mail-header-date h2))))
4484
4485 (defun gnus-thread-sort-by-date (h1 h2)
4486   "Sort threads by root article date."
4487   (gnus-article-sort-by-date
4488    (gnus-thread-header h1) (gnus-thread-header h2)))
4489
4490 (defsubst gnus-article-sort-by-score (h1 h2)
4491   "Sort articles by root article score.
4492 Unscored articles will be counted as having a score of zero."
4493   (> (or (cdr (assq (mail-header-number h1)
4494                     gnus-newsgroup-scored))
4495          gnus-summary-default-score 0)
4496      (or (cdr (assq (mail-header-number h2)
4497                     gnus-newsgroup-scored))
4498          gnus-summary-default-score 0)))
4499
4500 (defun gnus-thread-sort-by-score (h1 h2)
4501   "Sort threads by root article score."
4502   (gnus-article-sort-by-score
4503    (gnus-thread-header h1) (gnus-thread-header h2)))
4504
4505 (defun gnus-thread-sort-by-total-score (h1 h2)
4506   "Sort threads by the sum of all scores in the thread.
4507 Unscored articles will be counted as having a score of zero."
4508   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4509
4510 (defun gnus-thread-total-score (thread)
4511   ;; This function find the total score of THREAD.
4512   (cond
4513    ((null thread)
4514     0)
4515    ((consp thread)
4516     (if (stringp (car thread))
4517         (apply gnus-thread-score-function 0
4518                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4519       (gnus-thread-total-score-1 thread)))
4520    (t
4521     (gnus-thread-total-score-1 (list thread)))))
4522
4523 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4524   "Sort threads such that the thread with the most recently arrived article comes first."
4525   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4526
4527 (defun gnus-thread-highest-number (thread)
4528   "Return the highest article number in THREAD."
4529   (apply 'max (mapcar (lambda (header)
4530                         (mail-header-number header))
4531                       (message-flatten-list thread))))
4532
4533 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4534   "Sort threads such that the thread with the most recently dated article comes first."
4535   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4536
4537 (defun gnus-thread-latest-date (thread)
4538   "Return the highest article date in THREAD."
4539   (let ((previous-time 0))
4540     (apply 'max
4541            (mapcar
4542             (lambda (header)
4543               (setq previous-time
4544                     (condition-case ()
4545                         (time-to-seconds (mail-header-parse-date
4546                                           (mail-header-date header)))
4547                       (error previous-time))))
4548             (sort
4549              (message-flatten-list thread)
4550              (lambda (h1 h2)
4551                (< (mail-header-number h1)
4552                   (mail-header-number h2))))))))
4553
4554 (defun gnus-thread-total-score-1 (root)
4555   ;; This function find the total score of the thread below ROOT.
4556   (setq root (car root))
4557   (apply gnus-thread-score-function
4558          (or (append
4559               (mapcar 'gnus-thread-total-score
4560                       (cdr (gnus-id-to-thread (mail-header-id root))))
4561               (when (> (mail-header-number root) 0)
4562                 (list (or (cdr (assq (mail-header-number root)
4563                                      gnus-newsgroup-scored))
4564                           gnus-summary-default-score 0))))
4565              (list gnus-summary-default-score)
4566              '(0))))
4567
4568 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4569 (defvar gnus-tmp-prev-subject nil)
4570 (defvar gnus-tmp-false-parent nil)
4571 (defvar gnus-tmp-root-expunged nil)
4572 (defvar gnus-tmp-dummy-line nil)
4573
4574 (eval-when-compile (defvar gnus-tmp-header))
4575 (defun gnus-extra-header (type &optional header)
4576   "Return the extra header of TYPE."
4577   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4578       ""))
4579
4580 (defvar gnus-tmp-thread-tree-header-string "")
4581
4582 (defcustom gnus-sum-thread-tree-root "> "
4583   "With %B spec, used for the root of a thread.
4584 If nil, use subject instead."
4585   :type '(radio (const :format "%v  " nil) (string :size 0))
4586   :group 'gnus-thread)
4587 (defcustom gnus-sum-thread-tree-false-root "> "
4588   "With %B spec, used for a false root of a thread.
4589 If nil, use subject instead."
4590   :type '(radio (const :format "%v  " nil) (string :size 0))
4591   :group 'gnus-thread)
4592 (defcustom gnus-sum-thread-tree-single-indent ""
4593   "With %B spec, used for a thread with just one message.
4594 If nil, use subject instead."
4595   :type '(radio (const :format "%v  " nil) (string :size 0))
4596   :group 'gnus-thread)
4597 (defcustom gnus-sum-thread-tree-vertical "| "
4598   "With %B spec, used for drawing a vertical line."
4599   :type 'string
4600   :group 'gnus-thread)
4601 (defcustom gnus-sum-thread-tree-indent "  "
4602   "With %B spec, used for indenting."
4603   :type 'string
4604   :group 'gnus-thread)
4605 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4606   "With %B spec, used for a leaf with brothers."
4607   :type 'string
4608   :group 'gnus-thread)
4609 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4610   "With %B spec, used for a leaf without brothers."
4611   :type 'string
4612   :group 'gnus-thread)
4613
4614 (defun gnus-summary-prepare-threads (threads)
4615   "Prepare summary buffer from THREADS and indentation LEVEL.
4616 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4617 or a straight list of headers."
4618   (gnus-message 7 "Generating summary...")
4619
4620   (setq gnus-newsgroup-threads threads)
4621   (beginning-of-line)
4622
4623   (let ((gnus-tmp-level 0)
4624         (default-score (or gnus-summary-default-score 0))
4625         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4626         (building-line-count gnus-summary-display-while-building)
4627         (building-count (integerp gnus-summary-display-while-building))
4628         thread number subject stack state gnus-tmp-gathered beg-match
4629         new-roots gnus-tmp-new-adopts thread-end simp-subject
4630         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4631         gnus-tmp-replied gnus-tmp-subject-or-nil
4632         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4633         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4634         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4635         tree-stack)
4636
4637     (setq gnus-tmp-prev-subject nil
4638           gnus-tmp-thread-tree-header-string "")
4639
4640     (if (vectorp (car threads))
4641         ;; If this is a straight (sic) list of headers, then a
4642         ;; threaded summary display isn't required, so we just create
4643         ;; an unthreaded one.
4644         (gnus-summary-prepare-unthreaded threads)
4645
4646       ;; Do the threaded display.
4647
4648       (if gnus-summary-display-while-building
4649           (switch-to-buffer (buffer-name)))
4650       (while (or threads stack gnus-tmp-new-adopts new-roots)
4651
4652         (if (and (= gnus-tmp-level 0)
4653                  (or (not stack)
4654                      (= (caar stack) 0))
4655                  (not gnus-tmp-false-parent)
4656                  (or gnus-tmp-new-adopts new-roots))
4657             (if gnus-tmp-new-adopts
4658                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4659                       thread (list (car gnus-tmp-new-adopts))
4660                       gnus-tmp-header (caar thread)
4661                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4662               (when new-roots
4663                 (setq thread (list (car new-roots))
4664                       gnus-tmp-header (caar thread)
4665                       new-roots (cdr new-roots))))
4666
4667           (if threads
4668               ;; If there are some threads, we do them before the
4669               ;; threads on the stack.
4670               (setq thread threads
4671                     gnus-tmp-header (caar thread))
4672             ;; There were no current threads, so we pop something off
4673             ;; the stack.
4674             (setq state (car stack)
4675                   gnus-tmp-level (car state)
4676                   tree-stack (cadr state)
4677                   thread (caddr state)
4678                   stack (cdr stack)
4679                   gnus-tmp-header (caar thread))))
4680
4681         (setq gnus-tmp-false-parent nil)
4682         (setq gnus-tmp-root-expunged nil)
4683         (setq thread-end nil)
4684
4685         (if (stringp gnus-tmp-header)
4686             ;; The header is a dummy root.
4687             (cond
4688              ((eq gnus-summary-make-false-root 'adopt)
4689               ;; We let the first article adopt the rest.
4690               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4691                                                (cddar thread)))
4692               (setq gnus-tmp-gathered
4693                     (nconc (mapcar
4694                             (lambda (h) (mail-header-number (car h)))
4695                             (cddar thread))
4696                            gnus-tmp-gathered))
4697               (setq thread (cons (list (caar thread)
4698                                        (cadar thread))
4699                                  (cdr thread)))
4700               (setq gnus-tmp-level -1
4701                     gnus-tmp-false-parent t))
4702              ((eq gnus-summary-make-false-root 'empty)
4703               ;; We print adopted articles with empty subject fields.
4704               (setq gnus-tmp-gathered
4705                     (nconc (mapcar
4706                             (lambda (h) (mail-header-number (car h)))
4707                             (cddar thread))
4708                            gnus-tmp-gathered))
4709               (setq gnus-tmp-level -1))
4710              ((eq gnus-summary-make-false-root 'dummy)
4711               ;; We remember that we probably want to output a dummy
4712               ;; root.
4713               (setq gnus-tmp-dummy-line gnus-tmp-header)
4714               (setq gnus-tmp-prev-subject gnus-tmp-header))
4715              (t
4716               ;; We do not make a root for the gathered
4717               ;; sub-threads at all.
4718               (setq gnus-tmp-level -1)))
4719
4720           (setq number (mail-header-number gnus-tmp-header)
4721                 subject (mail-header-subject gnus-tmp-header)
4722                 simp-subject (gnus-simplify-subject-fully subject))
4723
4724           (cond
4725            ;; If the thread has changed subject, we might want to make
4726            ;; this subthread into a root.
4727            ((and (null gnus-thread-ignore-subject)
4728                  (not (zerop gnus-tmp-level))
4729                  gnus-tmp-prev-subject
4730                  (not (string= gnus-tmp-prev-subject simp-subject)))
4731             (setq new-roots (nconc new-roots (list (car thread)))
4732                   thread-end t
4733                   gnus-tmp-header nil))
4734            ;; If the article lies outside the current limit,
4735            ;; then we do not display it.
4736            ((not (memq number gnus-newsgroup-limit))
4737             (setq gnus-tmp-gathered
4738                   (nconc (mapcar
4739                           (lambda (h) (mail-header-number (car h)))
4740                           (cdar thread))
4741                          gnus-tmp-gathered))
4742             (setq gnus-tmp-new-adopts (if (cdar thread)
4743                                           (append gnus-tmp-new-adopts
4744                                                   (cdar thread))
4745                                         gnus-tmp-new-adopts)
4746                   thread-end t
4747                   gnus-tmp-header nil)
4748             (when (zerop gnus-tmp-level)
4749               (setq gnus-tmp-root-expunged t)))
4750            ;; Perhaps this article is to be marked as read?
4751            ((and gnus-summary-mark-below
4752                  (< (or (cdr (assq number gnus-newsgroup-scored))
4753                         default-score)
4754                     gnus-summary-mark-below)
4755                  ;; Don't touch sparse articles.
4756                  (not (gnus-summary-article-sparse-p number))
4757                  (not (gnus-summary-article-ancient-p number)))
4758             (setq gnus-newsgroup-unreads
4759                   (delq number gnus-newsgroup-unreads))
4760             (if gnus-newsgroup-auto-expire
4761                 (setq gnus-newsgroup-expirable
4762                       (gnus-add-to-sorted-list
4763                        gnus-newsgroup-expirable number))
4764               (push (cons number gnus-low-score-mark)
4765                     gnus-newsgroup-reads))))
4766
4767           (when gnus-tmp-header
4768             ;; We may have an old dummy line to output before this
4769             ;; article.
4770             (when (and gnus-tmp-dummy-line
4771                        (gnus-subject-equal
4772                         gnus-tmp-dummy-line
4773                         (mail-header-subject gnus-tmp-header)))
4774               (gnus-summary-insert-dummy-line
4775                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4776               (setq gnus-tmp-dummy-line nil))
4777
4778             ;; Compute the mark.
4779             (setq gnus-tmp-unread (gnus-article-mark number))
4780
4781             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4782                                   gnus-tmp-header gnus-tmp-level)
4783                   gnus-newsgroup-data)
4784
4785             ;; Actually insert the line.
4786             (setq
4787              gnus-tmp-subject-or-nil
4788              (cond
4789               ((and gnus-thread-ignore-subject
4790                     gnus-tmp-prev-subject
4791                     (not (string= gnus-tmp-prev-subject simp-subject)))
4792                subject)
4793               ((zerop gnus-tmp-level)
4794                (if (and (eq gnus-summary-make-false-root 'empty)
4795                         (memq number gnus-tmp-gathered)
4796                         gnus-tmp-prev-subject
4797                         (string= gnus-tmp-prev-subject simp-subject))
4798                    gnus-summary-same-subject
4799                  subject))
4800               (t gnus-summary-same-subject)))
4801             (if (and (eq gnus-summary-make-false-root 'adopt)
4802                      (= gnus-tmp-level 1)
4803                      (memq number gnus-tmp-gathered))
4804                 (setq gnus-tmp-opening-bracket ?\<
4805                       gnus-tmp-closing-bracket ?\>)
4806               (setq gnus-tmp-opening-bracket ?\[
4807                     gnus-tmp-closing-bracket ?\]))
4808             (setq
4809              gnus-tmp-indentation
4810              (aref gnus-thread-indent-array gnus-tmp-level)
4811              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4812              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4813                                 gnus-summary-default-score 0)
4814              gnus-tmp-score-char
4815              (if (or (null gnus-summary-default-score)
4816                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4817                          gnus-summary-zcore-fuzz))
4818                  ?\ ;;;Whitespace
4819                (if (< gnus-tmp-score gnus-summary-default-score)
4820                    gnus-score-below-mark gnus-score-over-mark))
4821              gnus-tmp-replied
4822              (cond ((memq number gnus-newsgroup-processable)
4823                     gnus-process-mark)
4824                    ((memq number gnus-newsgroup-cached)
4825                     gnus-cached-mark)
4826                    ((memq number gnus-newsgroup-replied)
4827                     gnus-replied-mark)
4828                    ((memq number gnus-newsgroup-forwarded)
4829                     gnus-forwarded-mark)
4830                    ((memq number gnus-newsgroup-saved)
4831                     gnus-saved-mark)
4832                    ((memq number gnus-newsgroup-recent)
4833                     gnus-recent-mark)
4834                    ((memq number gnus-newsgroup-unseen)
4835                     gnus-unseen-mark)
4836                    (t gnus-no-mark))
4837              gnus-tmp-downloaded
4838              (cond ((memq number gnus-newsgroup-undownloaded)
4839                     gnus-undownloaded-mark)
4840                    (gnus-newsgroup-agentized
4841                     gnus-downloaded-mark)
4842                    (t
4843                     gnus-no-mark))
4844              gnus-tmp-from (mail-header-from gnus-tmp-header)
4845              gnus-tmp-name
4846              (cond
4847               ((string-match "<[^>]+> *$" gnus-tmp-from)
4848                (setq beg-match (match-beginning 0))
4849                (or (and (string-match "^\".+\"" gnus-tmp-from)
4850                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4851                    (substring gnus-tmp-from 0 beg-match)))
4852               ((string-match "(.+)" gnus-tmp-from)
4853                (substring gnus-tmp-from
4854                           (1+ (match-beginning 0)) (1- (match-end 0))))
4855               (t gnus-tmp-from))
4856
4857              ;; Do the %B string
4858              gnus-tmp-thread-tree-header-string
4859              (cond
4860               ((not gnus-show-threads) "")
4861               ((zerop gnus-tmp-level)
4862                (cond ((cdar thread)
4863                       (or gnus-sum-thread-tree-root subject))
4864                      (gnus-tmp-new-adopts
4865                       (or gnus-sum-thread-tree-false-root subject))
4866                      (t
4867                       (or gnus-sum-thread-tree-single-indent subject))))
4868               (t
4869                (concat (apply 'concat
4870                               (mapcar (lambda (item)
4871                                         (if (= item 1)
4872                                             gnus-sum-thread-tree-vertical
4873                                           gnus-sum-thread-tree-indent))
4874                                       (cdr (reverse tree-stack))))
4875                        (if (nth 1 thread)
4876                            gnus-sum-thread-tree-leaf-with-other
4877                          gnus-sum-thread-tree-single-leaf)))))
4878             (when (string= gnus-tmp-name "")
4879               (setq gnus-tmp-name gnus-tmp-from))
4880             (unless (numberp gnus-tmp-lines)
4881               (setq gnus-tmp-lines -1))
4882             (if (= gnus-tmp-lines -1)
4883                 (setq gnus-tmp-lines "?")
4884               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4885               (gnus-put-text-property
4886              (point)
4887              (progn (eval gnus-summary-line-format-spec) (point))
4888                'gnus-number number)
4889             (when gnus-visual-p
4890               (forward-line -1)
4891               (gnus-run-hooks 'gnus-summary-update-hook)
4892               (forward-line 1))
4893
4894             (setq gnus-tmp-prev-subject simp-subject)))
4895
4896         (when (nth 1 thread)
4897           (push (list (max 0 gnus-tmp-level)
4898                       (copy-sequence tree-stack)
4899                       (nthcdr 1 thread))
4900                 stack))
4901         (push (if (nth 1 thread) 1 0) tree-stack)
4902         (incf gnus-tmp-level)
4903         (setq threads (if thread-end nil (cdar thread)))
4904         (if gnus-summary-display-while-building
4905             (if building-count
4906                 (progn
4907                   ;; use a set frequency
4908                   (setq building-line-count (1- building-line-count))
4909                   (when (= building-line-count 0)
4910                     (sit-for 0)
4911                     (setq building-line-count
4912                           gnus-summary-display-while-building)))
4913               ;; always
4914               (sit-for 0)))
4915         (unless threads
4916           (setq gnus-tmp-level 0)))))
4917   (gnus-message 7 "Generating summary...done"))
4918
4919 (defun gnus-summary-prepare-unthreaded (headers)
4920   "Generate an unthreaded summary buffer based on HEADERS."
4921   (let (header number mark)
4922
4923     (beginning-of-line)
4924
4925     (while headers
4926       ;; We may have to root out some bad articles...
4927       (when (memq (setq number (mail-header-number
4928                                 (setq header (pop headers))))
4929                   gnus-newsgroup-limit)
4930         ;; Mark article as read when it has a low score.
4931         (when (and gnus-summary-mark-below
4932                    (< (or (cdr (assq number gnus-newsgroup-scored))
4933                           gnus-summary-default-score 0)
4934                       gnus-summary-mark-below)
4935                    (not (gnus-summary-article-ancient-p number)))
4936           (setq gnus-newsgroup-unreads
4937                 (delq number gnus-newsgroup-unreads))
4938           (if gnus-newsgroup-auto-expire
4939               (push number gnus-newsgroup-expirable)
4940             (push (cons number gnus-low-score-mark)
4941                   gnus-newsgroup-reads)))
4942
4943         (setq mark (gnus-article-mark number))
4944         (push (gnus-data-make number mark (1+ (point)) header 0)
4945               gnus-newsgroup-data)
4946         (gnus-summary-insert-line
4947          header 0 number
4948          (memq number gnus-newsgroup-undownloaded)
4949          mark (memq number gnus-newsgroup-replied)
4950          (memq number gnus-newsgroup-expirable)
4951          (mail-header-subject header) nil
4952          (cdr (assq number gnus-newsgroup-scored))
4953          (memq number gnus-newsgroup-processable))))))
4954
4955 (defun gnus-summary-remove-list-identifiers ()
4956   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4957   (let ((regexp (if (consp gnus-list-identifiers)
4958                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4959                   gnus-list-identifiers))
4960         changed subject)
4961     (when regexp
4962       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
4963       (dolist (header gnus-newsgroup-headers)
4964         (setq subject (mail-header-subject header)
4965               changed nil)
4966         (while (string-match regexp subject)
4967           (setq subject
4968                 (concat (substring subject 0 (match-beginning 1))
4969                         (substring subject (match-end 0)))
4970                 changed t))
4971         (when changed
4972           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
4973             (setq subject
4974                   (concat (substring subject 0 (match-beginning 1))
4975                           (substring subject (match-end 1)))))
4976           (mail-header-set-subject header subject))))))
4977
4978 (defun gnus-fetch-headers (articles)
4979   "Fetch headers of ARTICLES."
4980   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4981     (gnus-message 5 "Fetching headers for %s..." name)
4982     (prog1
4983         (if (eq 'nov
4984                 (setq gnus-headers-retrieved-by
4985                       (gnus-retrieve-headers
4986                        articles gnus-newsgroup-name
4987                        ;; We might want to fetch old headers, but
4988                        ;; not if there is only 1 article.
4989                        (and (or (and
4990                                  (not (eq gnus-fetch-old-headers 'some))
4991                                  (not (numberp gnus-fetch-old-headers)))
4992                                 (> (length articles) 1))
4993                             gnus-fetch-old-headers))))
4994             (gnus-get-newsgroup-headers-xover
4995              articles nil nil gnus-newsgroup-name t)
4996           (gnus-get-newsgroup-headers))
4997       (gnus-message 5 "Fetching headers for %s...done" name))))
4998
4999 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5000   "Select newsgroup GROUP.
5001 If READ-ALL is non-nil, all articles in the group are selected.
5002 If SELECT-ARTICLES, only select those articles from GROUP."
5003   (let* ((entry (gnus-group-entry group))
5004          ;;!!! Dirty hack; should be removed.
5005          (gnus-summary-ignore-duplicates
5006           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5007               t
5008             gnus-summary-ignore-duplicates))
5009          (info (nth 2 entry))
5010          articles fetched-articles cached)
5011
5012     (unless (gnus-check-server
5013              (set (make-local-variable 'gnus-current-select-method)
5014                   (gnus-find-method-for-group group)))
5015       (error "Couldn't open server"))
5016
5017     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5018         (gnus-activate-group group)     ; Or we can activate it...
5019         (progn                          ; Or we bug out.
5020           (when (equal major-mode 'gnus-summary-mode)
5021             (gnus-kill-buffer (current-buffer)))
5022           (error "Couldn't activate group %s: %s"
5023                  group (gnus-status-message group))))
5024
5025     (unless (gnus-request-group group t)
5026       (when (equal major-mode 'gnus-summary-mode)
5027         (gnus-kill-buffer (current-buffer)))
5028       (error "Couldn't request group %s: %s"
5029              group (gnus-status-message group)))
5030
5031     (when gnus-agent
5032       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5033       
5034       (setq gnus-summary-use-undownloaded-faces
5035             (gnus-agent-find-parameter
5036              group
5037              'agent-enable-undownloaded-faces)))
5038
5039     (setq gnus-newsgroup-name group
5040           gnus-newsgroup-unselected nil
5041           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5042
5043     (let ((display (gnus-group-find-parameter group 'display)))
5044       (setq gnus-newsgroup-display
5045             (cond
5046              ((not (zerop (or (car-safe read-all) 0)))
5047               ;; The user entered the group with C-u SPC/RET, let's show
5048               ;; all articles.
5049               'gnus-not-ignore)
5050              ((eq display 'all)
5051               'gnus-not-ignore)
5052              ((arrayp display)
5053               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5054              ((numberp display)
5055               ;; The following is probably the "correct" solution, but
5056               ;; it makes Gnus fetch all headers and then limit the
5057               ;; articles (which is slow), so instead we hack the
5058               ;; select-articles parameter instead. -- Simon Josefsson
5059               ;; <jas@kth.se>
5060               ;;
5061               ;; (gnus-byte-compile
5062               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5063               ;;                         display)))))
5064               (setq select-articles
5065                     (gnus-uncompress-range
5066                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5067                              (if (> tmp 0)
5068                                  tmp
5069                                1))
5070                            (cdr (gnus-active group)))))
5071               nil)
5072              (t
5073               nil))))
5074
5075     (gnus-summary-setup-default-charset)
5076
5077     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5078     (when (gnus-virtual-group-p group)
5079       (setq cached gnus-newsgroup-cached))
5080
5081     (setq gnus-newsgroup-unreads
5082           (gnus-sorted-ndifference
5083            (gnus-sorted-ndifference gnus-newsgroup-unreads
5084                                     gnus-newsgroup-marked)
5085            gnus-newsgroup-dormant))
5086
5087     (setq gnus-newsgroup-processable nil)
5088
5089     (gnus-update-read-articles group gnus-newsgroup-unreads)
5090
5091     ;; Adjust and set lists of article marks.
5092     (when info
5093       (gnus-adjust-marked-articles info))
5094     (if (setq articles select-articles)
5095         (setq gnus-newsgroup-unselected
5096               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5097       (setq articles (gnus-articles-to-read group read-all)))
5098
5099     (cond
5100      ((null articles)
5101       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5102       'quit)
5103      ((eq articles 0) nil)
5104      (t
5105       ;; Init the dependencies hash table.
5106       (setq gnus-newsgroup-dependencies
5107             (gnus-make-hashtable (length articles)))
5108       (gnus-set-global-variables)
5109       ;; Retrieve the headers and read them in.
5110
5111       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5112
5113       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5114       (when cached
5115         (setq gnus-newsgroup-cached cached))
5116
5117       ;; Suppress duplicates?
5118       (when gnus-suppress-duplicates
5119         (gnus-dup-suppress-articles))
5120
5121       ;; Set the initial limit.
5122       (setq gnus-newsgroup-limit (copy-sequence articles))
5123       ;; Remove canceled articles from the list of unread articles.
5124       (setq fetched-articles
5125             (mapcar (lambda (headers) (mail-header-number headers))
5126                     gnus-newsgroup-headers))
5127       (setq gnus-newsgroup-articles fetched-articles)
5128       (setq gnus-newsgroup-unreads
5129             (gnus-sorted-nintersection
5130              gnus-newsgroup-unreads fetched-articles))
5131       (gnus-compute-unseen-list)
5132
5133       ;; Removed marked articles that do not exist.
5134       (gnus-update-missing-marks
5135        (gnus-sorted-difference articles fetched-articles))
5136       ;; We might want to build some more threads first.
5137       (when (and gnus-fetch-old-headers
5138                  (eq gnus-headers-retrieved-by 'nov))
5139         (if (eq gnus-fetch-old-headers 'invisible)
5140             (gnus-build-all-threads)
5141           (gnus-build-old-threads)))
5142       ;; Let the Gnus agent mark articles as read.
5143       (when gnus-agent
5144         (gnus-agent-get-undownloaded-list))
5145       ;; Remove list identifiers from subject
5146       (when gnus-list-identifiers
5147         (gnus-summary-remove-list-identifiers))
5148       ;; Check whether auto-expire is to be done in this group.
5149       (setq gnus-newsgroup-auto-expire
5150             (gnus-group-auto-expirable-p group))
5151       ;; Set up the article buffer now, if necessary.
5152       (unless gnus-single-article-buffer
5153         (gnus-article-setup-buffer))
5154       ;; First and last article in this newsgroup.
5155       (when gnus-newsgroup-headers
5156         (setq gnus-newsgroup-begin
5157               (mail-header-number (car gnus-newsgroup-headers))
5158               gnus-newsgroup-end
5159               (mail-header-number
5160                (gnus-last-element gnus-newsgroup-headers))))
5161       ;; GROUP is successfully selected.
5162       (or gnus-newsgroup-headers t)))))
5163
5164 (defun gnus-compute-unseen-list ()
5165   ;; The `seen' marks are treated specially.
5166   (if (not gnus-newsgroup-seen)
5167       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5168     (setq gnus-newsgroup-unseen
5169           (gnus-inverse-list-range-intersection
5170            gnus-newsgroup-articles gnus-newsgroup-seen))))
5171
5172 (defun gnus-summary-display-make-predicate (display)
5173   (require 'gnus-agent)
5174   (when (= (length display) 1)
5175     (setq display (car display)))
5176   (unless gnus-summary-display-cache
5177     (dolist (elem (append '((unread . unread)
5178                             (read . read)
5179                             (unseen . unseen))
5180                           gnus-article-mark-lists))
5181       (push (cons (cdr elem)
5182                   (gnus-byte-compile
5183                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5184             gnus-summary-display-cache)))
5185   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5186         (gnus-category-predicate-cache gnus-summary-display-cache))
5187     (gnus-get-predicate display)))
5188
5189 ;; Uses the dynamically bound `number' variable.
5190 (eval-when-compile
5191   (defvar number))
5192 (defun gnus-article-marked-p (type &optional article)
5193   (let ((article (or article number)))
5194     (cond
5195      ((eq type 'tick)
5196       (memq article gnus-newsgroup-marked))
5197      ((eq type 'spam)
5198       (memq article gnus-newsgroup-spam-marked))
5199      ((eq type 'unsend)
5200       (memq article gnus-newsgroup-unsendable))
5201      ((eq type 'undownload)
5202       (memq article gnus-newsgroup-undownloaded))
5203      ((eq type 'download)
5204       (memq article gnus-newsgroup-downloadable))
5205      ((eq type 'unread)
5206       (memq article gnus-newsgroup-unreads))
5207      ((eq type 'read)
5208       (memq article gnus-newsgroup-reads))
5209      ((eq type 'dormant)
5210       (memq article gnus-newsgroup-dormant) )
5211      ((eq type 'expire)
5212       (memq article gnus-newsgroup-expirable))
5213      ((eq type 'reply)
5214       (memq article gnus-newsgroup-replied))
5215      ((eq type 'killed)
5216       (memq article gnus-newsgroup-killed))
5217      ((eq type 'bookmark)
5218       (assq article gnus-newsgroup-bookmarks))
5219      ((eq type 'score)
5220       (assq article gnus-newsgroup-scored))
5221      ((eq type 'save)
5222       (memq article gnus-newsgroup-saved))
5223      ((eq type 'cache)
5224       (memq article gnus-newsgroup-cached))
5225      ((eq type 'forward)
5226       (memq article gnus-newsgroup-forwarded))
5227      ((eq type 'seen)
5228       (not (memq article gnus-newsgroup-unseen)))
5229      ((eq type 'recent)
5230       (memq article gnus-newsgroup-recent))
5231      (t t))))
5232
5233 (defun gnus-articles-to-read (group &optional read-all)
5234   "Find out what articles the user wants to read."
5235   (let* ((display (gnus-group-find-parameter group 'display))
5236          (articles
5237           ;; Select all articles if `read-all' is non-nil, or if there
5238           ;; are no unread articles.
5239           (if (or read-all
5240                   (and (zerop (length gnus-newsgroup-marked))
5241                        (zerop (length gnus-newsgroup-unreads)))
5242                   ;; Fetch all if the predicate is non-nil.
5243                   gnus-newsgroup-display)
5244               ;; We want to select the headers for all the articles in
5245               ;; the group, so we select either all the active
5246               ;; articles in the group, or (if that's nil), the
5247               ;; articles in the cache.
5248               (or
5249                (gnus-uncompress-range (gnus-active group))
5250                (gnus-cache-articles-in-group group))
5251             ;; Select only the "normal" subset of articles.
5252             (gnus-sorted-nunion
5253              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5254              gnus-newsgroup-unreads)))
5255          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5256          (scored (length scored-list))
5257          (number (length articles))
5258          (marked (+ (length gnus-newsgroup-marked)
5259                     (length gnus-newsgroup-dormant)))
5260          (select
5261           (cond
5262            ((numberp read-all)
5263             read-all)
5264            ((numberp gnus-newsgroup-display)
5265             gnus-newsgroup-display)
5266            (t
5267             (condition-case ()
5268                 (cond
5269                  ((and (or (<= scored marked) (= scored number))
5270                        (numberp gnus-large-newsgroup)
5271                        (> number gnus-large-newsgroup))
5272                   (let* ((cursor-in-echo-area nil)
5273                          (initial (gnus-parameter-large-newsgroup-initial
5274                                    gnus-newsgroup-name))
5275                          (input
5276                           (read-string
5277                            (format
5278                             "How many articles from %s (%s %d): "
5279                             (gnus-limit-string
5280                              (gnus-group-decoded-name gnus-newsgroup-name)
5281                              35)
5282                             (if initial "max" "default")
5283                             number)
5284                            (if initial
5285                                (cons (number-to-string initial)
5286                                      0)))))
5287                     (if (string-match "^[ \t]*$" input) number input)))
5288                  ((and (> scored marked) (< scored number)
5289                        (> (- scored number) 20))
5290                   (let ((input
5291                          (read-string
5292                           (format "%s %s (%d scored, %d total): "
5293                                   "How many articles from"
5294                                   (gnus-group-decoded-name group)
5295                                   scored number))))
5296                     (if (string-match "^[ \t]*$" input)
5297                         number input)))
5298                  (t number))
5299               (quit
5300                (message "Quit getting the articles to read")
5301                nil))))))
5302     (setq select (if (stringp select) (string-to-number select) select))
5303     (if (or (null select) (zerop select))
5304         select
5305       (if (and (not (zerop scored)) (<= (abs select) scored))
5306           (progn
5307             (setq articles (sort scored-list '<))
5308             (setq number (length articles)))
5309         (setq articles (copy-sequence articles)))
5310
5311       (when (< (abs select) number)
5312         (if (< select 0)
5313             ;; Select the N oldest articles.
5314             (setcdr (nthcdr (1- (abs select)) articles) nil)
5315           ;; Select the N most recent articles.
5316           (setq articles (nthcdr (- number select) articles))))
5317       (setq gnus-newsgroup-unselected
5318             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5319       (when gnus-alter-articles-to-read-function
5320         (setq articles
5321               (sort
5322                (funcall gnus-alter-articles-to-read-function
5323                         gnus-newsgroup-name articles)
5324                '<)))
5325       articles)))
5326
5327 (defun gnus-killed-articles (killed articles)
5328   (let (out)
5329     (while articles
5330       (when (inline (gnus-member-of-range (car articles) killed))
5331         (push (car articles) out))
5332       (setq articles (cdr articles)))
5333     out))
5334
5335 (defun gnus-uncompress-marks (marks)
5336   "Uncompress the mark ranges in MARKS."
5337   (let ((uncompressed '(score bookmark))
5338         out)
5339     (while marks
5340       (if (memq (caar marks) uncompressed)
5341           (push (car marks) out)
5342         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5343       (setq marks (cdr marks)))
5344     out))
5345
5346 (defun gnus-article-mark-to-type (mark)
5347   "Return the type of MARK."
5348   (or (cadr (assq mark gnus-article-special-mark-lists))
5349       'list))
5350
5351 (defun gnus-article-unpropagatable-p (mark)
5352   "Return whether MARK should be propagated to back end."
5353   (memq mark gnus-article-unpropagated-mark-lists))
5354
5355 (defun gnus-adjust-marked-articles (info)
5356   "Set all article lists and remove all marks that are no longer valid."
5357   (let* ((marked-lists (gnus-info-marks info))
5358          (active (gnus-active (gnus-info-group info)))
5359          (min (car active))
5360          (max (cdr active))
5361          (types gnus-article-mark-lists)
5362          marks var articles article mark mark-type
5363          bgn end)
5364
5365     (dolist (marks marked-lists)
5366       (setq mark (car marks)
5367             mark-type (gnus-article-mark-to-type mark)
5368             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5369
5370       ;; We set the variable according to the type of the marks list,
5371       ;; and then adjust the marks to a subset of the active articles.
5372       (cond
5373        ;; Adjust "simple" lists - compressed yet unsorted
5374        ((eq mark-type 'list)
5375         ;; Simultaneously uncompress and clip to active range
5376         ;; See gnus-uncompress-range for a description of possible marks
5377         (let (l lh)
5378           (if (not (cadr marks))
5379               (set var nil)
5380             (setq articles (if (numberp (cddr marks))
5381                                (list (cdr marks))
5382                              (cdr marks))
5383                   lh (cons nil nil)
5384                   l lh)
5385
5386             (while (setq article (pop articles))
5387               (cond ((consp article)
5388                      (setq bgn (max (car article) min)
5389                            end (min (cdr article) max))
5390                      (while (<= bgn end)
5391                        (setq l (setcdr l (cons bgn nil))
5392                              bgn (1+ bgn))))
5393                     ((and (<= min article)
5394                           (>= max article))
5395                      (setq l (setcdr l (cons article nil))))))
5396             (set var (cdr lh)))))
5397        ;; Adjust assocs.
5398        ((eq mark-type 'tuple)
5399         (set var (setq articles (cdr marks)))
5400         (when (not (listp (cdr (symbol-value var))))
5401           (set var (list (symbol-value var))))
5402         (when (not (listp (cdr articles)))
5403           (setq articles (list articles)))
5404         (while articles
5405           (when (or (not (consp (setq article (pop articles))))
5406                     (< (car article) min)
5407                     (> (car article) max))
5408             (set var (delq article (symbol-value var))))))
5409        ;; Adjust ranges (sloppily).
5410        ((eq mark-type 'range)
5411         (cond
5412          ((eq mark 'seen)
5413           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5414           ;; It should be (seen (NUM1 . NUM2)).
5415           (when (numberp (cddr marks))
5416             (setcdr marks (list (cdr marks))))
5417           (setq articles (cdr marks))
5418           (while (and articles
5419                       (or (and (consp (car articles))
5420                                (> min (cdar articles)))
5421                           (and (numberp (car articles))
5422                                (> min (car articles)))))
5423             (pop articles))
5424           (set var articles))))))))
5425
5426 (defun gnus-update-missing-marks (missing)
5427   "Go through the list of MISSING articles and remove them from the mark lists."
5428   (when missing
5429     (let (var m)
5430       ;; Go through all types.
5431       (dolist (elem gnus-article-mark-lists)
5432         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5433           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5434           (when (symbol-value var)
5435             ;; This list has articles.  So we delete all missing
5436             ;; articles from it.
5437             (setq m missing)
5438             (while m
5439               (set var (delq (pop m) (symbol-value var))))))))))
5440
5441 (defun gnus-update-marks ()
5442   "Enter the various lists of marked articles into the newsgroup info list."
5443   (let ((types gnus-article-mark-lists)
5444         (info (gnus-get-info gnus-newsgroup-name))
5445         type list newmarked symbol delta-marks)
5446     (when info
5447       ;; Add all marks lists to the list of marks lists.
5448       (while (setq type (pop types))
5449         (setq list (symbol-value
5450                     (setq symbol
5451                           (intern (format "gnus-newsgroup-%s" (car type))))))
5452
5453         (when list
5454           ;; Get rid of the entries of the articles that have the
5455           ;; default score.
5456           (when (and (eq (cdr type) 'score)
5457                      gnus-save-score
5458                      list)
5459             (let* ((arts list)
5460                    (prev (cons nil list))
5461                    (all prev))
5462               (while arts
5463                 (if (or (not (consp (car arts)))
5464                         (= (cdar arts) gnus-summary-default-score))
5465                     (setcdr prev (cdr arts))
5466                   (setq prev arts))
5467                 (setq arts (cdr arts)))
5468               (setq list (cdr all)))))
5469
5470         (when (eq (cdr type) 'seen)
5471           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5472
5473         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5474           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5475
5476         (when (and (gnus-check-backend-function
5477                     'request-set-mark gnus-newsgroup-name)
5478                    (not (gnus-article-unpropagatable-p (cdr type))))
5479           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5480                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5481                  (add (gnus-remove-from-range
5482                        (gnus-copy-sequence list) old)))
5483             (when add
5484               (push (list add 'add (list (cdr type))) delta-marks))
5485             (when del
5486               (push (list del 'del (list (cdr type))) delta-marks))))
5487
5488         (when list
5489           (push (cons (cdr type) list) newmarked)))
5490
5491       (when delta-marks
5492         (unless (gnus-check-group gnus-newsgroup-name)
5493           (error "Can't open server for %s" gnus-newsgroup-name))
5494         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5495
5496       ;; Enter these new marks into the info of the group.
5497       (if (nthcdr 3 info)
5498           (setcar (nthcdr 3 info) newmarked)
5499         ;; Add the marks lists to the end of the info.
5500         (when newmarked
5501           (setcdr (nthcdr 2 info) (list newmarked))))
5502
5503       ;; Cut off the end of the info if there's nothing else there.
5504       (let ((i 5))
5505         (while (and (> i 2)
5506                     (not (nth i info)))
5507           (when (nthcdr (decf i) info)
5508             (setcdr (nthcdr i info) nil)))))))
5509
5510 (defun gnus-set-mode-line (where)
5511   "Set the mode line of the article or summary buffers.
5512 If WHERE is `summary', the summary mode line format will be used."
5513   ;; Is this mode line one we keep updated?
5514   (when (and (memq where gnus-updated-mode-lines)
5515              (symbol-value
5516               (intern (format "gnus-%s-mode-line-format-spec" where))))
5517     (let (mode-string)
5518       (save-excursion
5519         ;; We evaluate this in the summary buffer since these
5520         ;; variables are buffer-local to that buffer.
5521         (set-buffer gnus-summary-buffer)
5522         ;; We bind all these variables that are used in the `eval' form
5523         ;; below.
5524         (let* ((mformat (symbol-value
5525                          (intern
5526                           (format "gnus-%s-mode-line-format-spec" where))))
5527                (gnus-tmp-group-name (gnus-group-decoded-name
5528                                      gnus-newsgroup-name))
5529                (gnus-tmp-article-number (or gnus-current-article 0))
5530                (gnus-tmp-unread gnus-newsgroup-unreads)
5531                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5532                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5533                (gnus-tmp-unread-and-unselected
5534                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5535                             (zerop gnus-tmp-unselected))
5536                        "")
5537                       ((zerop gnus-tmp-unselected)
5538                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5539                       (t (format "{%d(+%d) more}"
5540                                  gnus-tmp-unread-and-unticked
5541                                  gnus-tmp-unselected))))
5542                (gnus-tmp-subject
5543                 (if (and gnus-current-headers
5544                          (vectorp gnus-current-headers))
5545                     (gnus-mode-string-quote
5546                      (mail-header-subject gnus-current-headers))
5547                   ""))
5548                bufname-length max-len
5549                gnus-tmp-header);; passed as argument to any user-format-funcs
5550           (setq mode-string (eval mformat))
5551           (setq bufname-length (if (string-match "%b" mode-string)
5552                                    (- (length
5553                                        (buffer-name
5554                                         (if (eq where 'summary)
5555                                             nil
5556                                           (get-buffer gnus-article-buffer))))
5557                                       2)
5558                                  0))
5559           (setq max-len (max 4 (if gnus-mode-non-string-length
5560                                    (- (window-width)
5561                                       gnus-mode-non-string-length
5562                                       bufname-length)
5563                                  (length mode-string))))
5564           ;; We might have to chop a bit of the string off...
5565           (when (> (length mode-string) max-len)
5566             (setq mode-string
5567                   (concat (gnus-truncate-string mode-string (- max-len 3))
5568                           "...")))
5569           ;; Pad the mode string a bit.
5570           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5571       ;; Update the mode line.
5572       (setq mode-line-buffer-identification
5573             (gnus-mode-line-buffer-identification (list mode-string)))
5574       (set-buffer-modified-p t))))
5575
5576 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5577   "Go through the HEADERS list and add all Xrefs to a hash table.
5578 The resulting hash table is returned, or nil if no Xrefs were found."
5579   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5580          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5581          (xref-hashtb (gnus-make-hashtable))
5582          start group entry number xrefs header)
5583     (while headers
5584       (setq header (pop headers))
5585       (when (and (setq xrefs (mail-header-xref header))
5586                  (not (memq (setq number (mail-header-number header))
5587                             unreads)))
5588         (setq start 0)
5589         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5590           (setq start (match-end 0))
5591           (setq group (if prefix
5592                           (concat prefix (substring xrefs (match-beginning 1)
5593                                                     (match-end 1)))
5594                         (substring xrefs (match-beginning 1) (match-end 1))))
5595           (setq number
5596                 (string-to-int (substring xrefs (match-beginning 2)
5597                                           (match-end 2))))
5598           (if (setq entry (gnus-gethash group xref-hashtb))
5599               (setcdr entry (cons number (cdr entry)))
5600             (gnus-sethash group (cons number nil) xref-hashtb)))))
5601     (and start xref-hashtb)))
5602
5603 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5604   "Look through all the headers and mark the Xrefs as read."
5605   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5606         name info xref-hashtb idlist method nth4)
5607     (save-excursion
5608       (set-buffer gnus-group-buffer)
5609       (when (setq xref-hashtb
5610                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5611         (mapatoms
5612          (lambda (group)
5613            (unless (string= from-newsgroup (setq name (symbol-name group)))
5614              (setq idlist (symbol-value group))
5615              ;; Dead groups are not updated.
5616              (and (prog1
5617                       (setq info (gnus-get-info name))
5618                     (when (stringp (setq nth4 (gnus-info-method info)))
5619                       (setq nth4 (gnus-server-to-method nth4))))
5620                   ;; Only do the xrefs if the group has the same
5621                   ;; select method as the group we have just read.
5622                   (or (gnus-methods-equal-p
5623                        nth4 (gnus-find-method-for-group from-newsgroup))
5624                       virtual
5625                       (equal nth4 (setq method (gnus-find-method-for-group
5626                                                 from-newsgroup)))
5627                       (and (equal (car nth4) (car method))
5628                            (equal (nth 1 nth4) (nth 1 method))))
5629                   gnus-use-cross-reference
5630                   (or (not (eq gnus-use-cross-reference t))
5631                       virtual
5632                       ;; Only do cross-references on subscribed
5633                       ;; groups, if that is what is wanted.
5634                       (<= (gnus-info-level info) gnus-level-subscribed))
5635                   (gnus-group-make-articles-read name idlist))))
5636          xref-hashtb)))))
5637
5638 (defun gnus-compute-read-articles (group articles)
5639   (let* ((entry (gnus-group-entry group))
5640          (info (nth 2 entry))
5641          (active (gnus-active group))
5642          ninfo)
5643     (when entry
5644       ;; First peel off all invalid article numbers.
5645       (when active
5646         (let ((ids articles)
5647               id first)
5648           (while (setq id (pop ids))
5649             (when (and first (> id (cdr active)))
5650               ;; We'll end up in this situation in one particular
5651               ;; obscure situation.  If you re-scan a group and get
5652               ;; a new article that is cross-posted to a different
5653               ;; group that has not been re-scanned, you might get
5654               ;; crossposted article that has a higher number than
5655               ;; Gnus believes possible.  So we re-activate this
5656               ;; group as well.  This might mean doing the
5657               ;; crossposting thingy will *increase* the number
5658               ;; of articles in some groups.  Tsk, tsk.
5659               (setq active (or (gnus-activate-group group) active)))
5660             (when (or (> id (cdr active))
5661                       (< id (car active)))
5662               (setq articles (delq id articles))))))
5663       ;; If the read list is nil, we init it.
5664       (if (and active
5665                (null (gnus-info-read info))
5666                (> (car active) 1))
5667           (setq ninfo (cons 1 (1- (car active))))
5668         (setq ninfo (gnus-info-read info)))
5669       ;; Then we add the read articles to the range.
5670       (gnus-add-to-range
5671        ninfo (setq articles (sort articles '<))))))
5672
5673 (defun gnus-group-make-articles-read (group articles)
5674   "Update the info of GROUP to say that ARTICLES are read."
5675   (let* ((num 0)
5676          (entry (gnus-group-entry group))
5677          (info (nth 2 entry))
5678          (active (gnus-active group))
5679          range)
5680     (when entry
5681       (setq range (gnus-compute-read-articles group articles))
5682       (with-current-buffer gnus-group-buffer
5683         (gnus-undo-register
5684           `(progn
5685              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5686              (gnus-info-set-read ',info ',(gnus-info-read info))
5687              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5688              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5689              (gnus-group-update-group ,group t))))
5690       ;; Add the read articles to the range.
5691       (gnus-info-set-read info range)
5692       (gnus-request-set-mark group (list (list range 'add '(read))))
5693       ;; Then we have to re-compute how many unread
5694       ;; articles there are in this group.
5695       (when active
5696         (cond
5697          ((not range)
5698           (setq num (- (1+ (cdr active)) (car active))))
5699          ((not (listp (cdr range)))
5700           (setq num (- (cdr active) (- (1+ (cdr range))
5701                                        (car range)))))
5702          (t
5703           (while range
5704             (if (numberp (car range))
5705                 (setq num (1+ num))
5706               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5707             (setq range (cdr range)))
5708           (setq num (- (cdr active) num))))
5709         ;; Update the number of unread articles.
5710         (setcar entry num)
5711         ;; Update the group buffer.
5712         (unless (gnus-ephemeral-group-p group)
5713           (gnus-group-update-group group t))))))
5714
5715 (defvar gnus-newsgroup-none-id 0)
5716
5717 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5718   (let ((cur nntp-server-buffer)
5719         (dependencies
5720          (or dependencies
5721              (save-excursion (set-buffer gnus-summary-buffer)
5722                              gnus-newsgroup-dependencies)))
5723         headers id end ref
5724         (mail-parse-charset gnus-newsgroup-charset)
5725         (mail-parse-ignored-charsets
5726          (save-excursion (condition-case nil
5727                              (set-buffer gnus-summary-buffer)
5728                            (error))
5729                          gnus-newsgroup-ignored-charsets)))
5730     (save-excursion
5731       (set-buffer nntp-server-buffer)
5732       ;; Translate all TAB characters into SPACE characters.
5733       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5734       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5735       (ietf-drums-unfold-fws)
5736       (gnus-run-hooks 'gnus-parse-headers-hook)
5737       (let ((case-fold-search t)
5738             in-reply-to header p lines chars ctype)
5739         (goto-char (point-min))
5740         ;; Search to the beginning of the next header.  Error messages
5741         ;; do not begin with 2 or 3.
5742         (while (re-search-forward "^[23][0-9]+ " nil t)
5743           (setq id nil
5744                 ref nil)
5745           ;; This implementation of this function, with nine
5746           ;; search-forwards instead of the one re-search-forward and
5747           ;; a case (which basically was the old function) is actually
5748           ;; about twice as fast, even though it looks messier.  You
5749           ;; can't have everything, I guess.  Speed and elegance
5750           ;; doesn't always go hand in hand.
5751           (setq
5752            header
5753            (make-full-mail-header
5754             ;; Number.
5755             (prog1
5756                 (read cur)
5757               (end-of-line)
5758               (setq p (point))
5759               (narrow-to-region (point)
5760                                 (or (and (search-forward "\n.\n" nil t)
5761                                          (- (point) 2))
5762                                     (point))))
5763             ;; Subject.
5764             (progn
5765               (goto-char p)
5766               (if (search-forward "\nsubject:" nil t)
5767                   (nnheader-header-value)
5768                 "(none)"))
5769             ;; From.
5770             (progn
5771               (goto-char p)
5772               (if (search-forward "\nfrom:" nil t)
5773                   (nnheader-header-value)
5774                 "(nobody)"))
5775             ;; Date.
5776             (progn
5777               (goto-char p)
5778               (if (search-forward "\ndate:" nil t)
5779                   (nnheader-header-value) ""))
5780             ;; Message-ID.
5781             (progn
5782               (goto-char p)
5783               (setq id (if (re-search-forward
5784                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5785                            ;; We do it this way to make sure the Message-ID
5786                            ;; is (somewhat) syntactically valid.
5787                            (buffer-substring (match-beginning 1)
5788                                              (match-end 1))
5789                          ;; If there was no message-id, we just fake one
5790                          ;; to make subsequent routines simpler.
5791                          (nnheader-generate-fake-message-id))))
5792             ;; References.
5793             (progn
5794               (goto-char p)
5795               (if (search-forward "\nreferences:" nil t)
5796                   (progn
5797                     (setq end (point))
5798                     (prog1
5799                         (nnheader-header-value)
5800                       (setq ref
5801                             (buffer-substring
5802                              (progn
5803                                ;; (end-of-line)
5804                                (search-backward ">" end t)
5805                                (1+ (point)))
5806                              (progn
5807                                (search-backward "<" end t)
5808                                (point))))))
5809                 ;; Get the references from the in-reply-to header if there
5810                 ;; were no references and the in-reply-to header looks
5811                 ;; promising.
5812                 (if (and (search-forward "\nin-reply-to:" nil t)
5813                          (setq in-reply-to (nnheader-header-value))
5814                          (string-match "<[^>]+>" in-reply-to))
5815                     (let (ref2)
5816                       (setq ref (substring in-reply-to (match-beginning 0)
5817                                            (match-end 0)))
5818                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5819                         (setq ref2 (substring in-reply-to (match-beginning 0)
5820                                               (match-end 0)))
5821                         (when (> (length ref2) (length ref))
5822                           (setq ref ref2)))
5823                       ref)
5824                   (setq ref nil))))
5825             ;; Chars.
5826             (progn
5827               (goto-char p)
5828               (if (search-forward "\nchars: " nil t)
5829                   (if (numberp (setq chars (ignore-errors (read cur))))
5830                       chars -1)
5831                 -1))
5832             ;; Lines.
5833             (progn
5834               (goto-char p)
5835               (if (search-forward "\nlines: " nil t)
5836                   (if (numberp (setq lines (ignore-errors (read cur))))
5837                       lines -1)
5838                 -1))
5839             ;; Xref.
5840             (progn
5841               (goto-char p)
5842               (and (search-forward "\nxref:" nil t)
5843                    (nnheader-header-value)))
5844             ;; Extra.
5845             (when gnus-extra-headers
5846               (let ((extra gnus-extra-headers)
5847                     out)
5848                 (while extra
5849                   (goto-char p)
5850                   (when (search-forward
5851                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5852                     (push (cons (car extra) (nnheader-header-value)) out))
5853                   (pop extra))
5854                 out))))
5855           (goto-char p)
5856           (if (and (search-forward "\ncontent-type: " nil t)
5857                    (setq ctype (nnheader-header-value)))
5858               (mime-entity-set-content-type-internal
5859                header (mime-parse-Content-Type ctype)))
5860           (when (equal id ref)
5861             (setq ref nil))
5862
5863           (when gnus-alter-header-function
5864             (funcall gnus-alter-header-function header)
5865             (setq id (mail-header-id header)
5866                   ref (gnus-parent-id (mail-header-references header))))
5867
5868           (when (setq header
5869                       (gnus-dependencies-add-header
5870                        header dependencies force-new))
5871             (push header headers))
5872           (goto-char (point-max))
5873           (widen))
5874         (nreverse headers)))))
5875
5876 ;; Goes through the xover lines and returns a list of vectors
5877 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5878                                                   force-new dependencies
5879                                                   group also-fetch-heads)
5880   "Parse the news overview data in the server buffer.
5881 Return a list of headers that match SEQUENCE (see
5882 `nntp-retrieve-headers')."
5883   ;; Get the Xref when the users reads the articles since most/some
5884   ;; NNTP servers do not include Xrefs when using XOVER.
5885   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5886   (let ((mail-parse-charset gnus-newsgroup-charset)
5887         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5888         (cur nntp-server-buffer)
5889         (dependencies (or dependencies gnus-newsgroup-dependencies))
5890         (allp (cond
5891                ((eq gnus-read-all-available-headers t)
5892                 t)
5893                ((stringp gnus-read-all-available-headers)
5894                 (string-match gnus-read-all-available-headers group))
5895                (t
5896                 nil)))
5897         number headers header)
5898     (save-excursion
5899       (set-buffer nntp-server-buffer)
5900       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5901       ;; Allow the user to mangle the headers before parsing them.
5902       (gnus-run-hooks 'gnus-parse-headers-hook)
5903       (goto-char (point-min))
5904       (gnus-parse-without-error
5905         (while (and (or sequence allp)
5906                     (not (eobp)))
5907           (setq number (read cur))
5908           (when (not allp)
5909             (while (and sequence
5910                         (< (car sequence) number))
5911               (setq sequence (cdr sequence))))
5912           (when (and (or allp
5913                          (and sequence
5914                               (eq number (car sequence))))
5915                      (progn
5916                        (setq sequence (cdr sequence))
5917                        (setq header (inline
5918                                       (gnus-nov-parse-line
5919                                        number dependencies force-new)))))
5920             (push header headers))
5921           (forward-line 1)))
5922       ;; A common bug in inn is that if you have posted an article and
5923       ;; then retrieves the active file, it will answer correctly --
5924       ;; the new article is included.  However, a NOV entry for the
5925       ;; article may not have been generated yet, so this may fail.
5926       ;; We work around this problem by retrieving the last few
5927       ;; headers using HEAD.
5928       (if (or (not also-fetch-heads)
5929               (not sequence))
5930           ;; We (probably) got all the headers.
5931           (nreverse headers)
5932         (let ((gnus-nov-is-evil t))
5933           (nconc
5934            (nreverse headers)
5935            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5936              (gnus-get-newsgroup-headers))))))))
5937
5938 (defun gnus-article-get-xrefs ()
5939   "Fill in the Xref value in `gnus-current-headers', if necessary.
5940 This is meant to be called in `gnus-article-internal-prepare-hook'."
5941   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5942                                  gnus-current-headers)))
5943     (or (not gnus-use-cross-reference)
5944         (not headers)
5945         (and (mail-header-xref headers)
5946              (not (string= (mail-header-xref headers) "")))
5947         (let ((case-fold-search t)
5948               xref)
5949           (save-restriction
5950             (nnheader-narrow-to-headers)
5951             (goto-char (point-min))
5952             (when (or (and (not (eobp))
5953                            (eq (downcase (char-after)) ?x)
5954                            (looking-at "Xref:"))
5955                       (search-forward "\nXref:" nil t))
5956               (goto-char (1+ (match-end 0)))
5957               (setq xref (buffer-substring (point) (point-at-eol)))
5958               (mail-header-set-xref headers xref)))))))
5959
5960 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5961   "Find article ID and insert the summary line for that article.
5962 OLD-HEADER can either be a header or a line number to insert
5963 the subject line on."
5964   (let* ((line (and (numberp old-header) old-header))
5965          (old-header (and (vectorp old-header) old-header))
5966          (header (cond ((and old-header use-old-header)
5967                         old-header)
5968                        ((and (numberp id)
5969                              (gnus-number-to-header id))
5970                         (gnus-number-to-header id))
5971                        (t
5972                         (gnus-read-header id))))
5973          (number (and (numberp id) id))
5974          d)
5975     (when header
5976       ;; Rebuild the thread that this article is part of and go to the
5977       ;; article we have fetched.
5978       (when (and (not gnus-show-threads)
5979                  old-header)
5980         (when (and number
5981                    (setq d (gnus-data-find (mail-header-number old-header))))
5982           (goto-char (gnus-data-pos d))
5983           (gnus-data-remove
5984            number
5985            (- (point-at-bol)
5986               (prog1
5987                   (1+ (point-at-eol))
5988                 (gnus-delete-line))))))
5989       (when old-header
5990         (mail-header-set-number header (mail-header-number old-header)))
5991       (setq gnus-newsgroup-sparse
5992             (delq (setq number (mail-header-number header))
5993                   gnus-newsgroup-sparse))
5994       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5995       (push number gnus-newsgroup-limit)
5996       (gnus-rebuild-thread (mail-header-id header) line)
5997       (gnus-summary-goto-subject number nil t))
5998     (when (and (numberp number)
5999                (> number 0))
6000       ;; We have to update the boundaries even if we can't fetch the
6001       ;; article if ID is a number -- so that the next `P' or `N'
6002       ;; command will fetch the previous (or next) article even
6003       ;; if the one we tried to fetch this time has been canceled.
6004       (when (> number gnus-newsgroup-end)
6005         (setq gnus-newsgroup-end number))
6006       (when (< number gnus-newsgroup-begin)
6007         (setq gnus-newsgroup-begin number))
6008       (setq gnus-newsgroup-unselected
6009             (delq number gnus-newsgroup-unselected)))
6010     ;; Report back a success?
6011     (and header (mail-header-number header))))
6012
6013 ;;; Process/prefix in the summary buffer
6014
6015 (defun gnus-summary-work-articles (n)
6016   "Return a list of articles to be worked upon.
6017 The prefix argument, the list of process marked articles, and the
6018 current article will be taken into consideration."
6019   (save-excursion
6020     (set-buffer gnus-summary-buffer)
6021     (cond
6022      (n
6023       ;; A numerical prefix has been given.
6024       (setq n (prefix-numeric-value n))
6025       (let ((backward (< n 0))
6026             (n (abs (prefix-numeric-value n)))
6027             articles article)
6028         (save-excursion
6029           (while
6030               (and (> n 0)
6031                    (push (setq article (gnus-summary-article-number))
6032                          articles)
6033                    (if backward
6034                        (gnus-summary-find-prev nil article)
6035                      (gnus-summary-find-next nil article)))
6036             (decf n)))
6037         (nreverse articles)))
6038      ((and (gnus-region-active-p) (mark))
6039       (message "region active")
6040       ;; Work on the region between point and mark.
6041       (let ((max (max (point) (mark)))
6042             articles article)
6043         (save-excursion
6044           (goto-char (min (point) (mark)))
6045           (while
6046               (and
6047                (push (setq article (gnus-summary-article-number)) articles)
6048                (gnus-summary-find-next nil article)
6049                (< (point) max)))
6050           (nreverse articles))))
6051      (gnus-newsgroup-processable
6052       ;; There are process-marked articles present.
6053       ;; Save current state.
6054       (gnus-summary-save-process-mark)
6055       ;; Return the list.
6056       (reverse gnus-newsgroup-processable))
6057      (t
6058       ;; Just return the current article.
6059       (list (gnus-summary-article-number))))))
6060
6061 (defmacro gnus-summary-iterate (arg &rest forms)
6062   "Iterate over the process/prefixed articles and do FORMS.
6063 ARG is the interactive prefix given to the command.  FORMS will be
6064 executed with point over the summary line of the articles."
6065   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6066     `(let ((,articles (gnus-summary-work-articles ,arg)))
6067        (while ,articles
6068          (gnus-summary-goto-subject (car ,articles))
6069          ,@forms
6070          (pop ,articles)))))
6071
6072 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6073 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6074
6075 (defun gnus-summary-save-process-mark ()
6076   "Push the current set of process marked articles on the stack."
6077   (interactive)
6078   (push (copy-sequence gnus-newsgroup-processable)
6079         gnus-newsgroup-process-stack))
6080
6081 (defun gnus-summary-kill-process-mark ()
6082   "Push the current set of process marked articles on the stack and unmark."
6083   (interactive)
6084   (gnus-summary-save-process-mark)
6085   (gnus-summary-unmark-all-processable))
6086
6087 (defun gnus-summary-yank-process-mark ()
6088   "Pop the last process mark state off the stack and restore it."
6089   (interactive)
6090   (unless gnus-newsgroup-process-stack
6091     (error "Empty mark stack"))
6092   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6093
6094 (defun gnus-summary-process-mark-set (set)
6095   "Make SET into the current process marked articles."
6096   (gnus-summary-unmark-all-processable)
6097   (while set
6098     (gnus-summary-set-process-mark (pop set))))
6099
6100 ;;; Searching and stuff
6101
6102 (defun gnus-summary-search-group (&optional backward use-level)
6103   "Search for next unread newsgroup.
6104 If optional argument BACKWARD is non-nil, search backward instead."
6105   (save-excursion
6106     (set-buffer gnus-group-buffer)
6107     (when (gnus-group-search-forward
6108            backward nil (if use-level (gnus-group-group-level) nil))
6109       (gnus-group-group-name))))
6110
6111 (defun gnus-summary-best-group (&optional exclude-group)
6112   "Find the name of the best unread group.
6113 If EXCLUDE-GROUP, do not go to this group."
6114   (save-excursion
6115     (set-buffer gnus-group-buffer)
6116     (save-excursion
6117       (gnus-group-best-unread-group exclude-group))))
6118
6119 (defun gnus-summary-find-next (&optional unread article backward)
6120   (if backward
6121       (gnus-summary-find-prev unread article)
6122     (let* ((dummy (gnus-summary-article-intangible-p))
6123            (article (or article (gnus-summary-article-number)))
6124            (data (gnus-data-find-list article))
6125            result)
6126       (when (and (not dummy)
6127                  (or (not gnus-summary-check-current)
6128                      (not unread)
6129                      (not (gnus-data-unread-p (car data)))))
6130         (setq data (cdr data)))
6131       (when (setq result
6132                   (if unread
6133                       (progn
6134                         (while data
6135                           (unless (memq (gnus-data-number (car data))
6136                                         (cond
6137                                          ((eq gnus-auto-goto-ignores
6138                                               'always-undownloaded)
6139                                           gnus-newsgroup-undownloaded)
6140                                          (gnus-plugged
6141                                           nil)
6142                                          ((eq gnus-auto-goto-ignores
6143                                               'unfetched)
6144                                           gnus-newsgroup-unfetched)
6145                                          ((eq gnus-auto-goto-ignores
6146                                               'undownloaded)
6147                                           gnus-newsgroup-undownloaded)))
6148                             (when (gnus-data-unread-p (car data))
6149                               (setq result (car data)
6150                                     data nil)))
6151                           (setq data (cdr data)))
6152                         result)
6153                     (car data)))
6154         (goto-char (gnus-data-pos result))
6155         (gnus-data-number result)))))
6156
6157 (defun gnus-summary-find-prev (&optional unread article)
6158   (let* ((eobp (eobp))
6159          (article (or article (gnus-summary-article-number)))
6160          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6161          result)
6162     (when (and (not eobp)
6163                (or (not gnus-summary-check-current)
6164                    (not unread)
6165                    (not (gnus-data-unread-p (car data)))))
6166       (setq data (cdr data)))
6167     (when (setq result
6168                 (if unread
6169                     (progn
6170                       (while data
6171                         (unless (memq (gnus-data-number (car data))
6172                                       (cond
6173                                        ((eq gnus-auto-goto-ignores
6174                                             'always-undownloaded)
6175                                         gnus-newsgroup-undownloaded)
6176                                        (gnus-plugged
6177                                         nil)
6178                                        ((eq gnus-auto-goto-ignores
6179                                             'unfetched)
6180                                         gnus-newsgroup-unfetched)
6181                                        ((eq gnus-auto-goto-ignores
6182                                             'undownloaded)
6183                                         gnus-newsgroup-undownloaded)))
6184                           (when (gnus-data-unread-p (car data))
6185                             (setq result (car data)
6186                                   data nil)))
6187                         (setq data (cdr data)))
6188                       result)
6189                   (car data)))
6190       (goto-char (gnus-data-pos result))
6191       (gnus-data-number result))))
6192
6193 (defun gnus-summary-find-subject (subject &optional unread backward article)
6194   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6195          (article (or article (gnus-summary-article-number)))
6196          (articles (gnus-data-list backward))
6197          (arts (gnus-data-find-list article articles))
6198          result)
6199     (when (or (not gnus-summary-check-current)
6200               (not unread)
6201               (not (gnus-data-unread-p (car arts))))
6202       (setq arts (cdr arts)))
6203     (while arts
6204       (and (or (not unread)
6205                (gnus-data-unread-p (car arts)))
6206            (vectorp (gnus-data-header (car arts)))
6207            (gnus-subject-equal
6208             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6209            (setq result (car arts)
6210                  arts nil))
6211       (setq arts (cdr arts)))
6212     (and result
6213          (goto-char (gnus-data-pos result))
6214          (gnus-data-number result))))
6215
6216 (defun gnus-summary-search-forward (&optional unread subject backward)
6217   "Search forward for an article.
6218 If UNREAD, look for unread articles.  If SUBJECT, look for
6219 articles with that subject.  If BACKWARD, search backward instead."
6220   (cond (subject (gnus-summary-find-subject subject unread backward))
6221         (backward (gnus-summary-find-prev unread))
6222         (t (gnus-summary-find-next unread))))
6223
6224 (defun gnus-recenter (&optional n)
6225   "Center point in window and redisplay frame.
6226 Also do horizontal recentering."
6227   (interactive "P")
6228   (when (and gnus-auto-center-summary
6229              (not (eq gnus-auto-center-summary 'vertical)))
6230     (gnus-horizontal-recenter))
6231   (recenter n))
6232
6233 (defun gnus-summary-recenter ()
6234   "Center point in the summary window.
6235 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6236 displayed, no centering will be performed."
6237   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6238   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6239   (interactive)
6240   ;; The user has to want it.
6241   (when gnus-auto-center-summary
6242     (let* ((top (cond ((< (window-height) 4) 0)
6243                       ((< (window-height) 7) 1)
6244                       (t (if (numberp gnus-auto-center-summary)
6245                              gnus-auto-center-summary
6246                            2))))
6247            (height (1- (window-height)))
6248            (bottom (save-excursion (goto-char (point-max))
6249                                    (forward-line (- height))
6250                                    (point)))
6251            (window (get-buffer-window (current-buffer))))
6252       (when (get-buffer-window gnus-article-buffer)
6253         ;; Only do recentering when the article buffer is displayed,
6254         ;; Set the window start to either `bottom', which is the biggest
6255         ;; possible valid number, or the second line from the top,
6256         ;; whichever is the least.
6257         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6258           (if (> bottom top-pos)
6259               ;; Keep the second line from the top visible
6260               (set-window-start window top-pos t)
6261             ;; Try to keep the bottom line visible; if it's partially
6262             ;; obscured, either scroll one more line to make it fully
6263             ;; visible, or revert to using TOP-POS.
6264             (save-excursion
6265               (goto-char (point-max))
6266               (forward-line -1)
6267               (let ((last-line-start (point)))
6268                 (goto-char bottom)
6269                 (set-window-start window (point) t)
6270                 (when (not (pos-visible-in-window-p last-line-start window))
6271                   (forward-line 1)
6272                   (set-window-start window (min (point) top-pos) t)))))))
6273       ;; Do horizontal recentering while we're at it.
6274       (when (and (get-buffer-window (current-buffer) t)
6275                  (not (eq gnus-auto-center-summary 'vertical)))
6276         (let ((selected (selected-window)))
6277           (select-window (get-buffer-window (current-buffer) t))
6278           (gnus-summary-position-point)
6279           (gnus-horizontal-recenter)
6280           (select-window selected))))))
6281
6282 (defun gnus-summary-jump-to-group (newsgroup)
6283   "Move point to NEWSGROUP in group mode buffer."
6284   ;; Keep update point of group mode buffer if visible.
6285   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6286       (save-window-excursion
6287         ;; Take care of tree window mode.
6288         (when (get-buffer-window gnus-group-buffer)
6289           (pop-to-buffer gnus-group-buffer))
6290         (gnus-group-jump-to-group newsgroup))
6291     (save-excursion
6292       ;; Take care of tree window mode.
6293       (if (get-buffer-window gnus-group-buffer)
6294           (pop-to-buffer gnus-group-buffer)
6295         (set-buffer gnus-group-buffer))
6296       (gnus-group-jump-to-group newsgroup))))
6297
6298 ;; This function returns a list of article numbers based on the
6299 ;; difference between the ranges of read articles in this group and
6300 ;; the range of active articles.
6301 (defun gnus-list-of-unread-articles (group)
6302   (let* ((read (gnus-info-read (gnus-get-info group)))
6303          (active (or (gnus-active group) (gnus-activate-group group)))
6304          (last (cdr active))
6305          first nlast unread)
6306     ;; If none are read, then all are unread.
6307     (if (not read)
6308         (setq first (car active))
6309       ;; If the range of read articles is a single range, then the
6310       ;; first unread article is the article after the last read
6311       ;; article.  Sounds logical, doesn't it?
6312       (if (and (not (listp (cdr read)))
6313                (or (< (car read) (car active))
6314                    (progn (setq read (list read))
6315                           nil)))
6316           (setq first (max (car active) (1+ (cdr read))))
6317         ;; `read' is a list of ranges.
6318         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6319                                   (caar read)))
6320                   1)
6321           (setq first (car active)))
6322         (while read
6323           (when first
6324             (while (< first nlast)
6325               (setq unread (cons first unread)
6326                     first (1+ first))))
6327           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6328           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6329           (setq read (cdr read)))))
6330     ;; And add the last unread articles.
6331     (while (<= first last)
6332       (setq unread (cons first unread)
6333             first (1+ first)))
6334     ;; Return the list of unread articles.
6335     (delq 0 (nreverse unread))))
6336
6337 (defun gnus-list-of-read-articles (group)
6338   "Return a list of unread, unticked and non-dormant articles."
6339   (let* ((info (gnus-get-info group))
6340          (marked (gnus-info-marks info))
6341          (active (gnus-active group)))
6342     (and info active
6343          (gnus-list-range-difference
6344           (gnus-list-range-difference
6345            (gnus-sorted-complement
6346             (gnus-uncompress-range active)
6347             (gnus-list-of-unread-articles group))
6348            (cdr (assq 'dormant marked)))
6349           (cdr (assq 'tick marked))))))
6350
6351 ;; This function returns a sequence of article numbers based on the
6352 ;; difference between the ranges of read articles in this group and
6353 ;; the range of active articles.
6354 (defun gnus-sequence-of-unread-articles (group)
6355   (let* ((read (gnus-info-read (gnus-get-info group)))
6356          (active (or (gnus-active group) (gnus-activate-group group)))
6357          (last (cdr active))
6358          first nlast unread)
6359     ;; If none are read, then all are unread.
6360     (if (not read)
6361         (setq first (car active))
6362       ;; If the range of read articles is a single range, then the
6363       ;; first unread article is the article after the last read
6364       ;; article.  Sounds logical, doesn't it?
6365       (if (and (not (listp (cdr read)))
6366                (or (< (car read) (car active))
6367                    (progn (setq read (list read))
6368                           nil)))
6369           (setq first (max (car active) (1+ (cdr read))))
6370         ;; `read' is a list of ranges.
6371         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6372                                   (caar read)))
6373                   1)
6374           (setq first (car active)))
6375         (while read
6376           (when first
6377             (push (cons first nlast) unread))
6378           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6379           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6380           (setq read (cdr read)))))
6381     ;; And add the last unread articles.
6382     (cond ((< first last)
6383            (push (cons first last) unread))
6384           ((= first last)
6385            (push first unread)))
6386     ;; Return the sequence of unread articles.
6387     (delq 0 (nreverse unread))))
6388
6389 ;; Various summary commands
6390
6391 (defun gnus-summary-select-article-buffer ()
6392   "Reconfigure windows to show article buffer."
6393   (interactive)
6394   (if (not (gnus-buffer-live-p gnus-article-buffer))
6395       (error "There is no article buffer for this summary buffer")
6396     (gnus-configure-windows 'article)
6397     (select-window (get-buffer-window gnus-article-buffer))))
6398
6399 (defun gnus-summary-universal-argument (arg)
6400   "Perform any operation on all articles that are process/prefixed."
6401   (interactive "P")
6402   (let ((articles (gnus-summary-work-articles arg))
6403         func article)
6404     (if (eq
6405          (setq
6406           func
6407           (key-binding
6408            (read-key-sequence
6409             (substitute-command-keys
6410              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6411          'undefined)
6412         (gnus-error 1 "Undefined key")
6413       (save-excursion
6414         (while articles
6415           (gnus-summary-goto-subject (setq article (pop articles)))
6416           (let (gnus-newsgroup-processable)
6417             (command-execute func))
6418           (gnus-summary-remove-process-mark article)))))
6419   (gnus-summary-position-point))
6420
6421 (defun gnus-summary-toggle-truncation (&optional arg)
6422   "Toggle truncation of summary lines.
6423 With ARG, turn line truncation on if ARG is positive."
6424   (interactive "P")
6425   (setq truncate-lines
6426         (if (null arg) (not truncate-lines)
6427           (> (prefix-numeric-value arg) 0)))
6428   (redraw-display))
6429
6430 (defun gnus-summary-find-for-reselect ()
6431   "Return the number of an article to stay on across a reselect.
6432 The current article is considered, then following articles, then previous
6433 articles.  An article is sought which is not cancelled and isn't a temporary
6434 insertion from another group.  If there's no such then return a dummy 0."
6435   (let (found)
6436     (dolist (rev '(nil t))
6437       (unless found      ; don't demand the reverse list if we don't need it
6438         (let ((data (gnus-data-find-list
6439                      (gnus-summary-article-number) (gnus-data-list rev))))
6440           (while (and data (not found))
6441             (if (and (< 0 (gnus-data-number (car data)))
6442                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6443                 (setq found (gnus-data-number (car data))))
6444             (setq data (cdr data))))))
6445     (or found 0)))
6446
6447 (defun gnus-summary-reselect-current-group (&optional all rescan)
6448   "Exit and then reselect the current newsgroup.
6449 The prefix argument ALL means to select all articles."
6450   (interactive "P")
6451   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6452     (error "Ephemeral groups can't be reselected"))
6453   (let ((current-subject (gnus-summary-find-for-reselect))
6454         (group gnus-newsgroup-name))
6455     (setq gnus-newsgroup-begin nil)
6456     (gnus-summary-exit nil 'leave-hidden)
6457     ;; We have to adjust the point of group mode buffer because
6458     ;; point was moved to the next unread newsgroup by exiting.
6459     (gnus-summary-jump-to-group group)
6460     (when rescan
6461       (save-excursion
6462         (save-window-excursion
6463           ;; Don't show group contents.
6464           (set-window-start (selected-window) (point-max))
6465           (gnus-group-get-new-news-this-group 1))))
6466     (gnus-group-read-group all t)
6467     (gnus-summary-goto-subject current-subject nil t)))
6468
6469 (defun gnus-summary-rescan-group (&optional all)
6470   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6471   (interactive "P")
6472   (gnus-summary-reselect-current-group all t))
6473
6474 (defun gnus-summary-update-info (&optional non-destructive)
6475   (save-excursion
6476     (let ((group gnus-newsgroup-name))
6477       (when group
6478         (when gnus-newsgroup-kill-headers
6479           (setq gnus-newsgroup-killed
6480                 (gnus-compress-sequence
6481                  (gnus-sorted-union
6482                   (gnus-list-range-intersection
6483                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6484                   gnus-newsgroup-unreads)
6485                  t)))
6486         (unless (listp (cdr gnus-newsgroup-killed))
6487           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6488         (let ((headers gnus-newsgroup-headers))
6489           ;; Set the new ranges of read articles.
6490           (save-excursion
6491             (set-buffer gnus-group-buffer)
6492             (gnus-undo-force-boundary))
6493           (gnus-update-read-articles
6494            group (gnus-sorted-union
6495                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6496           ;; Set the current article marks.
6497           (let ((gnus-newsgroup-scored
6498                  (if (and (not gnus-save-score)
6499                           (not non-destructive))
6500                      nil
6501                    gnus-newsgroup-scored)))
6502             (save-excursion
6503               (gnus-update-marks)))
6504           ;; Do the cross-ref thing.
6505           (when gnus-use-cross-reference
6506             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6507           ;; Do not switch windows but change the buffer to work.
6508           (set-buffer gnus-group-buffer)
6509           (unless (gnus-ephemeral-group-p group)
6510             (gnus-group-update-group group)))))))
6511
6512 (defun gnus-summary-save-newsrc (&optional force)
6513   "Save the current number of read/marked articles in the dribble buffer.
6514 The dribble buffer will then be saved.
6515 If FORCE (the prefix), also save the .newsrc file(s)."
6516   (interactive "P")
6517   (gnus-summary-update-info t)
6518   (if force
6519       (gnus-save-newsrc-file)
6520     (gnus-dribble-save)))
6521
6522 (defun gnus-summary-exit (&optional temporary leave-hidden)
6523   "Exit reading current newsgroup, and then return to group selection mode.
6524 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6525   (interactive)
6526   (gnus-set-global-variables)
6527   (gnus-kill-save-kill-buffer)
6528   (gnus-async-halt-prefetch)
6529   (let* ((group gnus-newsgroup-name)
6530          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6531          (gnus-group-is-exiting-p t)
6532          (mode major-mode)
6533          (group-point nil)
6534          (buf (current-buffer)))
6535     (unless quit-config
6536       ;; Do adaptive scoring, and possibly save score files.
6537       (when gnus-newsgroup-adaptive
6538         (gnus-score-adaptive))
6539       (when gnus-use-scoring
6540         (gnus-score-save)))
6541     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6542     ;; If we have several article buffers, we kill them at exit.
6543     (unless gnus-single-article-buffer
6544       (gnus-kill-buffer gnus-original-article-buffer)
6545       (setq gnus-article-current nil))
6546     (when gnus-use-cache
6547       (gnus-cache-possibly-remove-articles)
6548       (gnus-cache-save-buffers))
6549     (gnus-async-prefetch-remove-group group)
6550     (when gnus-suppress-duplicates
6551       (gnus-dup-enter-articles))
6552     (when gnus-use-trees
6553       (gnus-tree-close group))
6554     (when gnus-use-cache
6555       (gnus-cache-write-active))
6556     ;; Remove entries for this group.
6557     (nnmail-purge-split-history (gnus-group-real-name group))
6558     ;; Make all changes in this group permanent.
6559     (unless quit-config
6560       (gnus-run-hooks 'gnus-exit-group-hook)
6561       (gnus-summary-update-info))
6562     (gnus-close-group group)
6563     ;; Make sure where we were, and go to next newsgroup.
6564     (set-buffer gnus-group-buffer)
6565     (unless quit-config
6566       (gnus-group-jump-to-group group))
6567     (gnus-run-hooks 'gnus-summary-exit-hook)
6568     (unless (or quit-config
6569                 ;; If this group has disappeared from the summary
6570                 ;; buffer, don't skip forwards.
6571                 (not (string= group (gnus-group-group-name))))
6572       (gnus-group-next-unread-group 1))
6573     (setq group-point (point))
6574     (if temporary
6575         nil                             ;Nothing to do.
6576       ;; If we have several article buffers, we kill them at exit.
6577       (unless gnus-single-article-buffer
6578         (gnus-kill-buffer gnus-article-buffer)
6579         (gnus-kill-buffer gnus-original-article-buffer)
6580         (setq gnus-article-current nil))
6581       (set-buffer buf)
6582       (if (not gnus-kill-summary-on-exit)
6583           (progn
6584             (gnus-deaden-summary)
6585             (setq mode nil))
6586         ;; We set all buffer-local variables to nil.  It is unclear why
6587         ;; this is needed, but if we don't, buffer-local variables are
6588         ;; not garbage-collected, it seems.  This would the lead to en
6589         ;; ever-growing Emacs.
6590         (gnus-summary-clear-local-variables)
6591         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6592           (gnus-summary-clear-local-variables))
6593         (when (get-buffer gnus-article-buffer)
6594           (bury-buffer gnus-article-buffer))
6595         ;; We clear the global counterparts of the buffer-local
6596         ;; variables as well, just to be on the safe side.
6597         (set-buffer gnus-group-buffer)
6598         (gnus-summary-clear-local-variables)
6599         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6600           (gnus-summary-clear-local-variables))
6601         ;; Return to group mode buffer.
6602         (when (eq mode 'gnus-summary-mode)
6603           (gnus-kill-buffer buf)))
6604       (setq gnus-current-select-method gnus-select-method)
6605       (if leave-hidden
6606           (set-buffer gnus-group-buffer)
6607         (pop-to-buffer gnus-group-buffer))
6608       (if (not quit-config)
6609           (progn
6610             (goto-char group-point)
6611             (unless leave-hidden
6612               (gnus-configure-windows 'group 'force))
6613             (unless (pos-visible-in-window-p)
6614               (forward-line (/ (static-if (featurep 'xemacs)
6615                                    (window-displayed-height)
6616                                  (1- (window-height)))
6617                                -2))
6618               (set-window-start (selected-window) (point))
6619               (goto-char group-point)))
6620         (gnus-handle-ephemeral-exit quit-config))
6621       ;; Clear the current group name.
6622       (unless quit-config
6623         (setq gnus-newsgroup-name nil)))))
6624
6625 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6626 (defun gnus-summary-exit-no-update (&optional no-questions)
6627   "Quit reading current newsgroup without updating read article info."
6628   (interactive)
6629   (let* ((group gnus-newsgroup-name)
6630          (gnus-group-is-exiting-p t)
6631          (gnus-group-is-exiting-without-update-p t)
6632          (quit-config (gnus-group-quit-config group)))
6633     (when (or no-questions
6634               gnus-expert-user
6635               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6636       (gnus-async-halt-prefetch)
6637       (run-hooks 'gnus-summary-prepare-exit-hook)
6638       ;; If we have several article buffers, we kill them at exit.
6639       (unless gnus-single-article-buffer
6640         (gnus-kill-buffer gnus-article-buffer)
6641         (gnus-kill-buffer gnus-original-article-buffer)
6642         (setq gnus-article-current nil))
6643       (if (not gnus-kill-summary-on-exit)
6644           (gnus-deaden-summary)
6645         (gnus-close-group group)
6646         (gnus-summary-clear-local-variables)
6647         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6648           (gnus-summary-clear-local-variables))
6649         (set-buffer gnus-group-buffer)
6650         (gnus-summary-clear-local-variables)
6651         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6652           (gnus-summary-clear-local-variables))
6653         (gnus-kill-buffer gnus-summary-buffer))
6654       (unless gnus-single-article-buffer
6655         (setq gnus-article-current nil))
6656       (when gnus-use-trees
6657         (gnus-tree-close group))
6658       (gnus-async-prefetch-remove-group group)
6659       (when (get-buffer gnus-article-buffer)
6660         (bury-buffer gnus-article-buffer))
6661       ;; Return to the group buffer.
6662       (gnus-configure-windows 'group 'force)
6663       ;; Clear the current group name.
6664       (setq gnus-newsgroup-name nil)
6665       (unless (gnus-ephemeral-group-p group)
6666         (gnus-group-update-group group))
6667       (when (equal (gnus-group-group-name) group)
6668         (gnus-group-next-unread-group 1))
6669       (when quit-config
6670         (gnus-handle-ephemeral-exit quit-config)))))
6671
6672 (defun gnus-handle-ephemeral-exit (quit-config)
6673   "Handle movement when leaving an ephemeral group.
6674 The state which existed when entering the ephemeral is reset."
6675   (if (not (buffer-name (car quit-config)))
6676       (gnus-configure-windows 'group 'force)
6677     (set-buffer (car quit-config))
6678     (cond ((eq major-mode 'gnus-summary-mode)
6679            (gnus-set-global-variables))
6680           ((eq major-mode 'gnus-article-mode)
6681            (save-excursion
6682              ;; The `gnus-summary-buffer' variable may point
6683              ;; to the old summary buffer when using a single
6684              ;; article buffer.
6685              (unless (gnus-buffer-live-p gnus-summary-buffer)
6686                (set-buffer gnus-group-buffer))
6687              (set-buffer gnus-summary-buffer)
6688              (gnus-set-global-variables))))
6689     (if (or (eq (cdr quit-config) 'article)
6690             (eq (cdr quit-config) 'pick))
6691         (progn
6692           ;; The current article may be from the ephemeral group
6693           ;; thus it is best that we reload this article
6694           ;;
6695           ;; If we're exiting from a large digest, this can be
6696           ;; extremely slow.  So, it's better not to reload it. -- jh.
6697           ;;(gnus-summary-show-article)
6698           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6699               (gnus-configure-windows 'pick 'force)
6700             (gnus-configure-windows (cdr quit-config) 'force)))
6701       (gnus-configure-windows (cdr quit-config) 'force))
6702     (when (eq major-mode 'gnus-summary-mode)
6703       (gnus-summary-next-subject 1 nil t)
6704       (gnus-summary-recenter)
6705       (gnus-summary-position-point))))
6706
6707 (defun gnus-summary-preview-mime-message ()
6708   "MIME decode and play this message."
6709   (interactive)
6710   (let ((gnus-break-pages nil)
6711         (gnus-show-mime t))
6712     (gnus-summary-select-article gnus-show-all-headers t))
6713   (let ((w (get-buffer-window gnus-article-buffer)))
6714     (when w
6715       (select-window (get-buffer-window gnus-article-buffer)))))
6716
6717 ;;; Dead summaries.
6718
6719 (defvar gnus-dead-summary-mode-map nil)
6720
6721 (unless gnus-dead-summary-mode-map
6722   (setq gnus-dead-summary-mode-map (make-keymap))
6723   (suppress-keymap gnus-dead-summary-mode-map)
6724   (substitute-key-definition
6725    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6726   (dolist (key '("\C-d" "\r" "\177" [delete]))
6727     (define-key gnus-dead-summary-mode-map
6728       key 'gnus-summary-wake-up-the-dead))
6729   (dolist (key '("q" "Q"))
6730     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6731
6732 (defvar gnus-dead-summary-mode nil
6733   "Minor mode for Gnus summary buffers.")
6734
6735 (defun gnus-dead-summary-mode (&optional arg)
6736   "Minor mode for Gnus summary buffers."
6737   (interactive "P")
6738   (when (eq major-mode 'gnus-summary-mode)
6739     (make-local-variable 'gnus-dead-summary-mode)
6740     (setq gnus-dead-summary-mode
6741           (if (null arg) (not gnus-dead-summary-mode)
6742             (> (prefix-numeric-value arg) 0)))
6743     (when gnus-dead-summary-mode
6744       (add-minor-mode
6745        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6746
6747 (defun gnus-deaden-summary ()
6748   "Make the current summary buffer into a dead summary buffer."
6749   ;; Kill any previous dead summary buffer.
6750   (when (and gnus-dead-summary
6751              (buffer-name gnus-dead-summary))
6752     (save-excursion
6753       (set-buffer gnus-dead-summary)
6754       (when gnus-dead-summary-mode
6755         (kill-buffer (current-buffer)))))
6756   ;; Make this the current dead summary.
6757   (setq gnus-dead-summary (current-buffer))
6758   (gnus-dead-summary-mode 1)
6759   (let ((name (buffer-name)))
6760     (when (string-match "Summary" name)
6761       (rename-buffer
6762        (concat (substring name 0 (match-beginning 0)) "Dead "
6763                (substring name (match-beginning 0)))
6764        t)
6765       (bury-buffer))))
6766
6767 (defun gnus-kill-or-deaden-summary (buffer)
6768   "Kill or deaden the summary BUFFER."
6769   (save-excursion
6770     (when (and (buffer-name buffer)
6771                (not gnus-single-article-buffer))
6772       (save-excursion
6773         (set-buffer buffer)
6774         (gnus-kill-buffer gnus-article-buffer)
6775         (gnus-kill-buffer gnus-original-article-buffer)))
6776     (cond
6777      ;; Kill the buffer.
6778      (gnus-kill-summary-on-exit
6779       (when (and gnus-use-trees
6780                  (gnus-buffer-exists-p buffer))
6781         (save-excursion
6782           (set-buffer buffer)
6783           (gnus-tree-close gnus-newsgroup-name)))
6784       (gnus-kill-buffer buffer))
6785      ;; Deaden the buffer.
6786      ((gnus-buffer-exists-p buffer)
6787       (save-excursion
6788         (set-buffer buffer)
6789         (gnus-deaden-summary))))))
6790
6791 (defun gnus-summary-wake-up-the-dead (&rest args)
6792   "Wake up the dead summary buffer."
6793   (interactive)
6794   (gnus-dead-summary-mode -1)
6795   (let ((name (buffer-name)))
6796     (when (string-match "Dead " name)
6797       (rename-buffer
6798        (concat (substring name 0 (match-beginning 0))
6799                (substring name (match-end 0)))
6800        t)))
6801   (gnus-message 3 "This dead summary is now alive again"))
6802
6803 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6804 (defun gnus-summary-fetch-faq (&optional faq-dir)
6805   "Fetch the FAQ for the current group.
6806 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6807 in."
6808   (interactive
6809    (list
6810     (when current-prefix-arg
6811       (completing-read
6812        "FAQ dir: " (and (listp gnus-group-faq-directory)
6813                         (mapcar (lambda (file) (list file))
6814                                 gnus-group-faq-directory))))))
6815   (let (gnus-faq-buffer)
6816     (when (setq gnus-faq-buffer
6817                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6818       (gnus-configure-windows 'summary-faq))))
6819
6820 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6821 (defun gnus-summary-describe-group (&optional force)
6822   "Describe the current newsgroup."
6823   (interactive "P")
6824   (gnus-group-describe-group force gnus-newsgroup-name))
6825
6826 (defun gnus-summary-describe-briefly ()
6827   "Describe summary mode commands briefly."
6828   (interactive)
6829   (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")))
6830
6831 ;; Walking around group mode buffer from summary mode.
6832
6833 (defun gnus-summary-next-group (&optional no-article target-group backward)
6834   "Exit current newsgroup and then select next unread newsgroup.
6835 If prefix argument NO-ARTICLE is non-nil, no article is selected
6836 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6837 previous group instead."
6838   (interactive "P")
6839   ;; Stop pre-fetching.
6840   (gnus-async-halt-prefetch)
6841   (let ((current-group gnus-newsgroup-name)
6842         (current-buffer (current-buffer))
6843         entered)
6844     ;; First we semi-exit this group to update Xrefs and all variables.
6845     ;; We can't do a real exit, because the window conf must remain
6846     ;; the same in case the user is prompted for info, and we don't
6847     ;; want the window conf to change before that...
6848     (gnus-summary-exit t)
6849     (while (not entered)
6850       ;; Then we find what group we are supposed to enter.
6851       (set-buffer gnus-group-buffer)
6852       (gnus-group-jump-to-group current-group)
6853       (setq target-group
6854             (or target-group
6855                 (if (eq gnus-keep-same-level 'best)
6856                     (gnus-summary-best-group gnus-newsgroup-name)
6857                   (gnus-summary-search-group backward gnus-keep-same-level))))
6858       (if (not target-group)
6859           ;; There are no further groups, so we return to the group
6860           ;; buffer.
6861           (progn
6862             (gnus-message 5 "Returning to the group buffer")
6863             (setq entered t)
6864             (when (gnus-buffer-live-p current-buffer)
6865               (set-buffer current-buffer)
6866               (gnus-summary-exit))
6867             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6868         ;; We try to enter the target group.
6869         (gnus-group-jump-to-group target-group)
6870         (let ((unreads (gnus-group-group-unread)))
6871           (if (and (or (eq t unreads)
6872                        (and unreads (not (zerop unreads))))
6873                    (gnus-summary-read-group
6874                     target-group nil no-article
6875                     (and (buffer-name current-buffer) current-buffer)
6876                     nil backward))
6877               (setq entered t)
6878             (setq current-group target-group
6879                   target-group nil)))))))
6880
6881 (defun gnus-summary-prev-group (&optional no-article)
6882   "Exit current newsgroup and then select previous unread newsgroup.
6883 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6884   (interactive "P")
6885   (gnus-summary-next-group no-article nil t))
6886
6887 ;; Walking around summary lines.
6888
6889 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6890   "Go to the first subject satisfying any non-nil constraint.
6891 If UNREAD is non-nil, the article should be unread.
6892 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6893 If UNSEEN is non-nil, the article should be unseen.
6894 Returns the article selected or nil if there are no matching articles."
6895   (interactive "P")
6896   (cond
6897    ;; Empty summary.
6898    ((null gnus-newsgroup-data)
6899     (gnus-message 3 "No articles in the group")
6900     nil)
6901    ;; Pick the first article.
6902    ((not (or unread undownloaded unseen))
6903     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6904     (gnus-data-number (car gnus-newsgroup-data)))
6905    ;; Find the first unread article.
6906    (t
6907     (let ((data gnus-newsgroup-data))
6908       (while (and data
6909                   (let ((num (gnus-data-number (car data))))
6910                     (or (memq num gnus-newsgroup-unfetched)
6911                         (not (or (and unread
6912                                       (memq num gnus-newsgroup-unreads))
6913                                  (and undownloaded
6914                                       (memq num gnus-newsgroup-undownloaded))
6915                                  (and unseen
6916                                       (memq num gnus-newsgroup-unseen)))))))
6917         (setq data (cdr data)))
6918       (prog1
6919           (if data
6920               (progn
6921                 (goto-char (gnus-data-pos (car data)))
6922                 (gnus-data-number (car data)))
6923             (gnus-message 3 "No more%s articles"
6924                           (let* ((r (when unread " unread"))
6925                                  (d (when undownloaded " undownloaded"))
6926                                  (s (when unseen " unseen"))
6927                                  (l (delq nil (list r d s))))
6928                             (cond ((= 3 (length l))
6929                                    (concat r "," d ", or" s))
6930                                   ((= 2 (length l))
6931                                    (concat (car l) ", or" (cadr l)))
6932                                   ((= 1 (length l))
6933                                    (car l))
6934                                   (t
6935                                    ""))))
6936             nil
6937             )
6938         (gnus-summary-position-point))))))
6939
6940 (defun gnus-summary-next-subject (n &optional unread dont-display)
6941   "Go to next N'th summary line.
6942 If N is negative, go to the previous N'th subject line.
6943 If UNREAD is non-nil, only unread articles are selected.
6944 The difference between N and the actual number of steps taken is
6945 returned."
6946   (interactive "p")
6947   (let ((backward (< n 0))
6948         (n (abs n)))
6949     (while (and (> n 0)
6950                 (if backward
6951                     (gnus-summary-find-prev unread)
6952                   (gnus-summary-find-next unread)))
6953       (unless (zerop (setq n (1- n)))
6954         (gnus-summary-show-thread)))
6955     (when (/= 0 n)
6956       (gnus-message 7 "No more%s articles"
6957                     (if unread " unread" "")))
6958     (unless dont-display
6959       (gnus-summary-recenter)
6960       (gnus-summary-position-point))
6961     n))
6962
6963 (defun gnus-summary-next-unread-subject (n)
6964   "Go to next N'th unread summary line."
6965   (interactive "p")
6966   (gnus-summary-next-subject n t))
6967
6968 (defun gnus-summary-prev-subject (n &optional unread)
6969   "Go to previous N'th summary line.
6970 If optional argument UNREAD is non-nil, only unread article is selected."
6971   (interactive "p")
6972   (gnus-summary-next-subject (- n) unread))
6973
6974 (defun gnus-summary-prev-unread-subject (n)
6975   "Go to previous N'th unread summary line."
6976   (interactive "p")
6977   (gnus-summary-next-subject (- n) t))
6978
6979 (defun gnus-summary-goto-subjects (articles)
6980   "Insert the subject header for ARTICLES in the current buffer."
6981   (save-excursion
6982     (dolist (article articles)
6983       (gnus-summary-goto-subject article t)))
6984   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6985   (gnus-summary-position-point))
6986  
6987 (defun gnus-summary-goto-subject (article &optional force silent)
6988   "Go the subject line of ARTICLE.
6989 If FORCE, also allow jumping to articles not currently shown."
6990   (interactive "nArticle number: ")
6991   (unless (numberp article)
6992     (error "Article %s is not a number" article))
6993   (let ((b (point))
6994         (data (gnus-data-find article)))
6995     ;; We read in the article if we have to.
6996     (and (not data)
6997          force
6998          (gnus-summary-insert-subject
6999           article
7000           (if (or (numberp force) (vectorp force)) force)
7001           t)
7002          (setq data (gnus-data-find article)))
7003     (goto-char b)
7004     (if (not data)
7005         (progn
7006           (unless silent
7007             (gnus-message 3 "Can't find article %d" article))
7008           nil)
7009       (let ((pt (gnus-data-pos data)))
7010         (goto-char pt)
7011         (gnus-summary-set-article-display-arrow pt))
7012       (gnus-summary-position-point)
7013       article)))
7014
7015 ;; Walking around summary lines with displaying articles.
7016
7017 (defun gnus-summary-expand-window (&optional arg)
7018   "Make the summary buffer take up the entire Emacs frame.
7019 Given a prefix, will force an `article' buffer configuration."
7020   (interactive "P")
7021   (if arg
7022       (gnus-configure-windows 'article 'force)
7023     (gnus-configure-windows 'summary 'force)))
7024
7025 (defun gnus-summary-display-article (article &optional all-header)
7026   "Display ARTICLE in article buffer."
7027   (when (gnus-buffer-live-p gnus-article-buffer)
7028     (with-current-buffer gnus-article-buffer
7029       (set-buffer-multibyte t)))
7030   (gnus-set-global-variables)
7031   (when (gnus-buffer-live-p gnus-article-buffer)
7032     (with-current-buffer gnus-article-buffer
7033       (setq gnus-article-charset gnus-newsgroup-charset)
7034       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7035       (set-buffer-multibyte t)))
7036   (if (null article)
7037       nil
7038     (prog1
7039         (if gnus-summary-display-article-function
7040             (funcall gnus-summary-display-article-function article all-header)
7041           (gnus-article-prepare article all-header))
7042       (with-current-buffer gnus-article-buffer
7043         (set (make-local-variable 'gnus-summary-search-article-matched-data)
7044              nil))
7045       (gnus-run-hooks 'gnus-select-article-hook)
7046       (when (and gnus-current-article
7047                  (not (zerop gnus-current-article)))
7048         (gnus-summary-goto-subject gnus-current-article))
7049       (gnus-summary-recenter)
7050       (when (and gnus-use-trees gnus-show-threads)
7051         (gnus-possibly-generate-tree article)
7052         (gnus-highlight-selected-tree article))
7053       ;; Successfully display article.
7054       (gnus-article-set-window-start
7055        (cdr (assq article gnus-newsgroup-bookmarks))))))
7056
7057 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7058   "Select the current article.
7059 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7060 non-nil, the article will be re-fetched even if it already present in
7061 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7062 be displayed."
7063   ;; Make sure we are in the summary buffer to work around bbdb bug.
7064   (unless (eq major-mode 'gnus-summary-mode)
7065     (set-buffer gnus-summary-buffer))
7066   (let ((article (or article (gnus-summary-article-number)))
7067         (all-headers (not (not all-headers))) ;Must be T or NIL.
7068         gnus-summary-display-article-function)
7069     (and (not pseudo)
7070          (gnus-summary-article-pseudo-p article)
7071          (error "This is a pseudo-article"))
7072     (save-excursion
7073       (set-buffer gnus-summary-buffer)
7074       (if (or (and gnus-single-article-buffer
7075                    (or (null gnus-current-article)
7076                        (null gnus-article-current)
7077                        (null (get-buffer gnus-article-buffer))
7078                        (not (eq article (cdr gnus-article-current)))
7079                        (not (equal (car gnus-article-current)
7080                                    gnus-newsgroup-name))))
7081               (and (not gnus-single-article-buffer)
7082                    (or (null gnus-current-article)
7083                        (not (eq gnus-current-article article))))
7084               force)
7085           ;; The requested article is different from the current article.
7086           (progn
7087             (gnus-summary-display-article article all-headers)
7088             (gnus-article-set-window-start
7089              (cdr (assq article gnus-newsgroup-bookmarks)))
7090             article)
7091         'old))))
7092
7093 (defun gnus-summary-force-verify-and-decrypt ()
7094   "Display buttons for signed/encrypted parts and verify/decrypt them."
7095   (interactive)
7096   (let ((mm-verify-option 'known)
7097         (mm-decrypt-option 'known)
7098         (gnus-article-emulate-mime t)
7099         (gnus-buttonized-mime-types (append (list "multipart/signed"
7100                                                   "multipart/encrypted")
7101                                             gnus-buttonized-mime-types)))
7102     (gnus-summary-select-article nil 'force)))
7103
7104 (defun gnus-summary-set-current-mark (&optional current-mark)
7105   "Obsolete function."
7106   nil)
7107
7108 (defun gnus-summary-next-article (&optional unread subject backward push)
7109   "Select the next article.
7110 If UNREAD, only unread articles are selected.
7111 If SUBJECT, only articles with SUBJECT are selected.
7112 If BACKWARD, the previous article is selected instead of the next."
7113   (interactive "P")
7114   (cond
7115    ;; Is there such an article?
7116    ((and (gnus-summary-search-forward unread subject backward)
7117          (or (gnus-summary-display-article (gnus-summary-article-number))
7118              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7119     (gnus-summary-position-point))
7120    ;; If not, we try the first unread, if that is wanted.
7121    ((and subject
7122          gnus-auto-select-same
7123          (gnus-summary-first-unread-article))
7124     (gnus-summary-position-point)
7125     (gnus-message 6 "Wrapped"))
7126    ;; Try to get next/previous article not displayed in this group.
7127    ((and gnus-auto-extend-newsgroup
7128          (not unread) (not subject))
7129     (gnus-summary-goto-article
7130      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7131      nil (count-lines (point-min) (point))))
7132    ;; Go to next/previous group.
7133    (t
7134     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7135       (gnus-summary-jump-to-group gnus-newsgroup-name))
7136     (let ((cmd last-command-char)
7137           (point
7138            (save-excursion
7139              (set-buffer gnus-group-buffer)
7140              (point)))
7141           (group
7142            (if (eq gnus-keep-same-level 'best)
7143                (gnus-summary-best-group gnus-newsgroup-name)
7144              (gnus-summary-search-group backward gnus-keep-same-level))))
7145       ;; For some reason, the group window gets selected.  We change
7146       ;; it back.
7147       (select-window (get-buffer-window (current-buffer)))
7148       ;; Select next unread newsgroup automagically.
7149       (cond
7150        ((or (not gnus-auto-select-next)
7151             (not cmd))
7152         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7153        ((or (eq gnus-auto-select-next 'quietly)
7154             (and (eq gnus-auto-select-next 'slightly-quietly)
7155                  push)
7156             (and (eq gnus-auto-select-next 'almost-quietly)
7157                  (gnus-summary-last-article-p)))
7158         ;; Select quietly.
7159         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7160             (gnus-summary-exit)
7161           (gnus-message 7 "No more%s articles (%s)..."
7162                         (if unread " unread" "")
7163                         (if group (concat "selecting " group)
7164                           "exiting"))
7165           (gnus-summary-next-group nil group backward)))
7166        (t
7167         (when (gnus-key-press-event-p last-input-event)
7168           (gnus-summary-walk-group-buffer
7169            gnus-newsgroup-name cmd unread backward point))))))))
7170
7171 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7172   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7173                       (?\C-p (gnus-group-prev-unread-group 1))))
7174         (cursor-in-echo-area t)
7175         keve key group ended prompt)
7176     (save-excursion
7177       (set-buffer gnus-group-buffer)
7178       (goto-char start)
7179       (setq group
7180             (if (eq gnus-keep-same-level 'best)
7181                 (gnus-summary-best-group gnus-newsgroup-name)
7182               (gnus-summary-search-group backward gnus-keep-same-level))))
7183     (while (not ended)
7184       (setq prompt
7185             (format
7186              "No more%s articles%s " (if unread " unread" "")
7187              (if (and group
7188                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7189                  (format " (Type %s for %s [%s])"
7190                          (single-key-description cmd) group
7191                          (gnus-group-unread group))
7192                (format " (Type %s to exit %s)"
7193                        (single-key-description cmd)
7194                        gnus-newsgroup-name))))
7195       ;; Confirm auto selection.
7196       (setq key (car (setq keve (gnus-read-event-char prompt)))
7197             ended t)
7198       (cond
7199        ((assq key keystrokes)
7200         (let ((obuf (current-buffer)))
7201           (switch-to-buffer gnus-group-buffer)
7202           (when group
7203             (gnus-group-jump-to-group group))
7204           (eval (cadr (assq key keystrokes)))
7205           (setq group (gnus-group-group-name))
7206           (switch-to-buffer obuf))
7207         (setq ended nil))
7208        ((equal key cmd)
7209         (if (or (not group)
7210                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7211             (gnus-summary-exit)
7212           (gnus-summary-next-group nil group backward)))
7213        (t
7214         (push (cdr keve) unread-command-events))))))
7215
7216 (defun gnus-summary-next-unread-article ()
7217   "Select unread article after current one."
7218   (interactive)
7219   (gnus-summary-next-article
7220    (or (not (eq gnus-summary-goto-unread 'never))
7221        (gnus-summary-last-article-p (gnus-summary-article-number)))
7222    (and gnus-auto-select-same
7223         (gnus-summary-article-subject))))
7224
7225 (defun gnus-summary-prev-article (&optional unread subject)
7226   "Select the article after the current one.
7227 If UNREAD is non-nil, only unread articles are selected."
7228   (interactive "P")
7229   (gnus-summary-next-article unread subject t))
7230
7231 (defun gnus-summary-prev-unread-article ()
7232   "Select unread article before current one."
7233   (interactive)
7234   (gnus-summary-prev-article
7235    (or (not (eq gnus-summary-goto-unread 'never))
7236        (gnus-summary-first-article-p (gnus-summary-article-number)))
7237    (and gnus-auto-select-same
7238         (gnus-summary-article-subject))))
7239
7240 (defun gnus-summary-next-page (&optional lines circular stop)
7241   "Show next page of the selected article.
7242 If at the end of the current article, select the next article.
7243 LINES says how many lines should be scrolled up.
7244
7245 If CIRCULAR is non-nil, go to the start of the article instead of
7246 selecting the next article when reaching the end of the current
7247 article.
7248
7249 If STOP is non-nil, just stop when reaching the end of the message.
7250
7251 Also see the variable `gnus-article-skip-boring'."
7252   (interactive "P")
7253   (setq gnus-summary-buffer (current-buffer))
7254   (gnus-set-global-variables)
7255   (let ((article (gnus-summary-article-number))
7256         (article-window (get-buffer-window gnus-article-buffer t))
7257         endp)
7258     ;; If the buffer is empty, we have no article.
7259     (unless article
7260       (error "No article to select"))
7261     (gnus-configure-windows 'article)
7262     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7263         (if (and (eq gnus-summary-goto-unread 'never)
7264                  (not (gnus-summary-last-article-p article)))
7265             (gnus-summary-next-article)
7266           (gnus-summary-next-unread-article))
7267       (if (or (null gnus-current-article)
7268               (null gnus-article-current)
7269               (/= article (cdr gnus-article-current))
7270               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7271           ;; Selected subject is different from current article's.
7272           (gnus-summary-display-article article)
7273         (when article-window
7274           (gnus-eval-in-buffer-window gnus-article-buffer
7275             (setq endp (or (gnus-article-next-page lines)
7276                            (gnus-article-only-boring-p))))
7277           (when endp
7278             (cond (stop
7279                    (gnus-message 3 "End of message"))
7280                   (circular
7281                    (gnus-summary-beginning-of-article))
7282                   (lines
7283                    (gnus-message 3 "End of message"))
7284                   ((null lines)
7285                    (if (and (eq gnus-summary-goto-unread 'never)
7286                             (not (gnus-summary-last-article-p article)))
7287                        (gnus-summary-next-article)
7288                      (gnus-summary-next-unread-article))))))))
7289     (gnus-summary-recenter)
7290     (gnus-summary-position-point)))
7291
7292 (defun gnus-summary-prev-page (&optional lines move)
7293   "Show previous page of selected article.
7294 Argument LINES specifies lines to be scrolled down.
7295 If MOVE, move to the previous unread article if point is at
7296 the beginning of the buffer."
7297   (interactive "P")
7298   (let ((article (gnus-summary-article-number))
7299         (article-window (get-buffer-window gnus-article-buffer t))
7300         endp)
7301     (gnus-configure-windows 'article)
7302     (if (or (null gnus-current-article)
7303             (null gnus-article-current)
7304             (/= article (cdr gnus-article-current))
7305             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7306         ;; Selected subject is different from current article's.
7307         (gnus-summary-display-article article)
7308       (gnus-summary-recenter)
7309       (when article-window
7310         (gnus-eval-in-buffer-window gnus-article-buffer
7311           (setq endp (gnus-article-prev-page lines)))
7312         (when (and move endp)
7313           (cond (lines
7314                  (gnus-message 3 "Beginning of message"))
7315                 ((null lines)
7316                  (if (and (eq gnus-summary-goto-unread 'never)
7317                           (not (gnus-summary-first-article-p article)))
7318                      (gnus-summary-prev-article)
7319                    (gnus-summary-prev-unread-article))))))))
7320   (gnus-summary-position-point))
7321
7322 (defun gnus-summary-prev-page-or-article (&optional lines)
7323   "Show previous page of selected article.
7324 Argument LINES specifies lines to be scrolled down.
7325 If at the beginning of the article, go to the next article."
7326   (interactive "P")
7327   (gnus-summary-prev-page lines t))
7328
7329 (defun gnus-summary-scroll-up (lines)
7330   "Scroll up (or down) one line current article.
7331 Argument LINES specifies lines to be scrolled up (or down if negative)."
7332   (interactive "p")
7333   (gnus-configure-windows 'article)
7334   (gnus-summary-show-thread)
7335   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7336     (gnus-eval-in-buffer-window gnus-article-buffer
7337       (cond ((> lines 0)
7338              (when (gnus-article-next-page lines)
7339                (gnus-message 3 "End of message")))
7340             ((< lines 0)
7341              (gnus-article-prev-page (- lines))))))
7342   (gnus-summary-recenter)
7343   (gnus-summary-position-point))
7344
7345 (defun gnus-summary-scroll-down (lines)
7346   "Scroll down (or up) one line current article.
7347 Argument LINES specifies lines to be scrolled down (or up if negative)."
7348   (interactive "p")
7349   (gnus-summary-scroll-up (- lines)))
7350
7351 (defun gnus-summary-next-same-subject ()
7352   "Select next article which has the same subject as current one."
7353   (interactive)
7354   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7355
7356 (defun gnus-summary-prev-same-subject ()
7357   "Select previous article which has the same subject as current one."
7358   (interactive)
7359   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7360
7361 (defun gnus-summary-next-unread-same-subject ()
7362   "Select next unread article which has the same subject as current one."
7363   (interactive)
7364   (gnus-summary-next-article t (gnus-summary-article-subject)))
7365
7366 (defun gnus-summary-prev-unread-same-subject ()
7367   "Select previous unread article which has the same subject as current one."
7368   (interactive)
7369   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7370
7371 (defun gnus-summary-first-unread-article ()
7372   "Select the first unread article.
7373 Return nil if there are no unread articles."
7374   (interactive)
7375   (prog1
7376       (when (gnus-summary-first-subject t)
7377         (gnus-summary-show-thread)
7378         (gnus-summary-first-subject t)
7379         (gnus-summary-display-article (gnus-summary-article-number)))
7380     (gnus-summary-position-point)))
7381
7382 (defun gnus-summary-first-unread-subject ()
7383   "Place the point on the subject line of the first unread article.
7384 Return nil if there are no unread articles."
7385   (interactive)
7386   (prog1
7387       (when (gnus-summary-first-subject t)
7388         (gnus-summary-show-thread)
7389         (gnus-summary-first-subject t))
7390     (gnus-summary-position-point)))
7391
7392 (defun gnus-summary-first-unseen-subject ()
7393   "Place the point on the subject line of the first unseen article.
7394 Return nil if there are no unseen articles."
7395   (interactive)
7396   (prog1
7397       (when (gnus-summary-first-subject nil nil t)
7398         (gnus-summary-show-thread)
7399         (gnus-summary-first-subject nil nil t))
7400     (gnus-summary-position-point)))
7401
7402 (defun gnus-summary-first-unseen-or-unread-subject ()
7403   "Place the point on the subject line of the first unseen article or,
7404 if all article have been seen, on the subject line of the first unread
7405 article."
7406   (interactive)
7407   (prog1
7408       (unless (when (gnus-summary-first-subject nil nil t)
7409                 (gnus-summary-show-thread)
7410                 (gnus-summary-first-subject nil nil t))
7411         (when (gnus-summary-first-subject t)
7412           (gnus-summary-show-thread)
7413           (gnus-summary-first-subject t)))
7414     (gnus-summary-position-point)))
7415
7416 (defun gnus-summary-first-article ()
7417   "Select the first article.
7418 Return nil if there are no articles."
7419   (interactive)
7420   (prog1
7421       (when (gnus-summary-first-subject)
7422         (gnus-summary-show-thread)
7423         (gnus-summary-first-subject)
7424         (gnus-summary-display-article (gnus-summary-article-number)))
7425     (gnus-summary-position-point)))
7426
7427 (defun gnus-summary-best-unread-article (&optional arg)
7428   "Select the unread article with the highest score.
7429 If given a prefix argument, select the next unread article that has a
7430 score higher than the default score."
7431   (interactive "P")
7432   (let ((article (if arg
7433                      (gnus-summary-better-unread-subject)
7434                    (gnus-summary-best-unread-subject))))
7435     (if article
7436         (gnus-summary-goto-article article)
7437       (error "No unread articles"))))
7438
7439 (defun gnus-summary-best-unread-subject ()
7440   "Select the unread subject with the highest score."
7441   (interactive)
7442   (let ((best -1000000)
7443         (data gnus-newsgroup-data)
7444         article score)
7445     (while data
7446       (and (gnus-data-unread-p (car data))
7447            (> (setq score
7448                     (gnus-summary-article-score (gnus-data-number (car data))))
7449               best)
7450            (setq best score
7451                  article (gnus-data-number (car data))))
7452       (setq data (cdr data)))
7453     (when article
7454       (gnus-summary-goto-subject article))
7455     (gnus-summary-position-point)
7456     article))
7457
7458 (defun gnus-summary-better-unread-subject ()
7459   "Select the first unread subject that has a score over the default score."
7460   (interactive)
7461   (let ((data gnus-newsgroup-data)
7462         article score)
7463     (while (and (setq article (gnus-data-number (car data)))
7464                 (or (gnus-data-read-p (car data))
7465                     (not (> (gnus-summary-article-score article)
7466                             gnus-summary-default-score))))
7467       (setq data (cdr data)))
7468     (when article
7469       (gnus-summary-goto-subject article))
7470     (gnus-summary-position-point)
7471     article))
7472
7473 (defun gnus-summary-last-subject ()
7474   "Go to the last displayed subject line in the group."
7475   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7476     (when article
7477       (gnus-summary-goto-subject article))))
7478
7479 (defun gnus-summary-goto-article (article &optional all-headers force)
7480   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7481 If ALL-HEADERS is non-nil, no header lines are hidden.
7482 If FORCE, go to the article even if it isn't displayed.  If FORCE
7483 is a number, it is the line the article is to be displayed on."
7484   (interactive
7485    (list
7486     (completing-read
7487      "Article number or Message-ID: "
7488      (mapcar (lambda (number) (list (int-to-string number)))
7489              gnus-newsgroup-limit))
7490     current-prefix-arg
7491     t))
7492   (prog1
7493       (if (and (stringp article)
7494                (string-match "@\\|%40" article))
7495           (gnus-summary-refer-article article)
7496         (when (stringp article)
7497           (setq article (string-to-number article)))
7498         (if (gnus-summary-goto-subject article force)
7499             (gnus-summary-display-article article all-headers)
7500           (gnus-message 4 "Couldn't go to article %s" article) nil))
7501     (gnus-summary-position-point)))
7502
7503 (defun gnus-summary-goto-last-article ()
7504   "Go to the previously read article."
7505   (interactive)
7506   (prog1
7507       (when gnus-last-article
7508         (gnus-summary-goto-article gnus-last-article nil t))
7509     (gnus-summary-position-point)))
7510
7511 (defun gnus-summary-pop-article (number)
7512   "Pop one article off the history and go to the previous.
7513 NUMBER articles will be popped off."
7514   (interactive "p")
7515   (let (to)
7516     (setq gnus-newsgroup-history
7517           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7518     (if to
7519         (gnus-summary-goto-article (car to) nil t)
7520       (error "Article history empty")))
7521   (gnus-summary-position-point))
7522
7523 ;; Summary commands and functions for limiting the summary buffer.
7524
7525 (defun gnus-summary-limit-to-articles (n)
7526   "Limit the summary buffer to the next N articles.
7527 If not given a prefix, use the process marked articles instead."
7528   (interactive "P")
7529   (prog1
7530       (let ((articles (gnus-summary-work-articles n)))
7531         (setq gnus-newsgroup-processable nil)
7532         (gnus-summary-limit articles))
7533     (gnus-summary-position-point)))
7534
7535 (defun gnus-summary-pop-limit (&optional total)
7536   "Restore the previous limit.
7537 If given a prefix, remove all limits."
7538   (interactive "P")
7539   (when total
7540     (setq gnus-newsgroup-limits
7541           (list (mapcar (lambda (h) (mail-header-number h))
7542                         gnus-newsgroup-headers))))
7543   (unless gnus-newsgroup-limits
7544     (error "No limit to pop"))
7545   (prog1
7546       (gnus-summary-limit nil 'pop)
7547     (gnus-summary-position-point)))
7548
7549 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7550   "Limit the summary buffer to articles that have subjects that match a regexp.
7551 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7552   (interactive
7553    (list (read-string (if current-prefix-arg
7554                           "Exclude subject (regexp): "
7555                         "Limit to subject (regexp): "))
7556          nil current-prefix-arg))
7557   (unless header
7558     (setq header "subject"))
7559   (when (not (equal "" subject))
7560     (prog1
7561         (let ((articles (gnus-summary-find-matching
7562                          (or header "subject") subject 'all nil nil
7563                          not-matching)))
7564           (unless articles
7565             (error "Found no matches for \"%s\"" subject))
7566           (gnus-summary-limit articles))
7567       (gnus-summary-position-point))))
7568
7569 (defun gnus-summary-limit-to-author (from &optional not-matching)
7570   "Limit the summary buffer to articles that have authors that match a regexp.
7571 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7572   (interactive
7573    (list (read-string (if current-prefix-arg
7574                           "Exclude author (regexp): "
7575                         "Limit to author (regexp): "))
7576          current-prefix-arg))
7577   (gnus-summary-limit-to-subject from "from" not-matching))
7578
7579 (defun gnus-summary-limit-to-age (age &optional younger-p)
7580   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7581 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7582 articles that are younger than AGE days."
7583   (interactive
7584    (let ((younger current-prefix-arg)
7585          (days-got nil)
7586          days)
7587      (while (not days-got)
7588        (setq days (if younger
7589                       (read-string "Limit to articles younger than (in days, older when negative): ")
7590                     (read-string
7591                      "Limit to articles older than (in days, younger when negative): ")))
7592        (when (> (length days) 0)
7593          (setq days (read days)))
7594        (if (numberp days)
7595            (progn
7596              (setq days-got t)
7597              (if (< days 0)
7598                  (progn
7599                    (setq younger (not younger))
7600                    (setq days (* days -1)))))
7601          (message "Please enter a number.")
7602          (sleep-for 1)))
7603      (list days younger)))
7604   (prog1
7605       (let ((data gnus-newsgroup-data)
7606             (cutoff (days-to-time age))
7607             articles d date is-younger)
7608         (while (setq d (pop data))
7609           (when (and (vectorp (gnus-data-header d))
7610                      (setq date (mail-header-date (gnus-data-header d))))
7611             (setq is-younger (time-less-p
7612                               (time-since (condition-case ()
7613                                               (date-to-time date)
7614                                             (error '(0 0))))
7615                               cutoff))
7616             (when (if younger-p
7617                       is-younger
7618                     (not is-younger))
7619               (push (gnus-data-number d) articles))))
7620         (gnus-summary-limit (nreverse articles)))
7621     (gnus-summary-position-point)))
7622
7623 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7624   "Limit the summary buffer to articles that match an 'extra' header."
7625   (interactive
7626    (let ((header
7627           (intern
7628            (gnus-completing-read-with-default
7629             (symbol-name (car gnus-extra-headers))
7630             (if current-prefix-arg
7631                 "Exclude extra header:"
7632               "Limit extra header:")
7633             (mapcar (lambda (x)
7634                       (cons (symbol-name x) x))
7635                     gnus-extra-headers)
7636             nil
7637             t))))
7638      (list header
7639            (read-string (format "%s header %s (regexp): "
7640                                 (if current-prefix-arg "Exclude" "Limit to")
7641                                 header))
7642            current-prefix-arg)))
7643   (when (not (equal "" regexp))
7644     (prog1
7645         (let ((articles (gnus-summary-find-matching
7646                          (cons 'extra header) regexp 'all nil nil
7647                          not-matching)))
7648           (unless articles
7649             (error "Found no matches for \"%s\"" regexp))
7650           (gnus-summary-limit articles))
7651       (gnus-summary-position-point))))
7652
7653 (defun gnus-summary-limit-to-display-predicate ()
7654   "Limit the summary buffer to the predicated in the `display' group parameter."
7655   (interactive)
7656   (unless gnus-newsgroup-display
7657     (error "There is no `display' group parameter"))
7658   (let (articles)
7659     (dolist (number gnus-newsgroup-articles)
7660       (when (funcall gnus-newsgroup-display)
7661         (push number articles)))
7662     (gnus-summary-limit articles))
7663   (gnus-summary-position-point))
7664
7665 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7666 (make-obsolete
7667  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7668
7669 (defun gnus-summary-limit-to-unread (&optional all)
7670   "Limit the summary buffer to articles that are not marked as read.
7671 If ALL is non-nil, limit strictly to unread articles."
7672   (interactive "P")
7673   (if all
7674       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7675     (gnus-summary-limit-to-marks
7676      ;; Concat all the marks that say that an article is read and have
7677      ;; those removed.
7678      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7679            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7680            gnus-low-score-mark gnus-expirable-mark
7681            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7682            gnus-duplicate-mark gnus-souped-mark)
7683      'reverse)))
7684
7685 (defun gnus-summary-limit-to-replied (&optional unreplied)
7686   "Limit the summary buffer to replied articles.
7687 If UNREPLIED (the prefix), limit to unreplied articles."
7688   (interactive "P")
7689   (if unreplied
7690       (gnus-summary-limit
7691        (gnus-set-difference gnus-newsgroup-articles
7692         gnus-newsgroup-replied))
7693     (gnus-summary-limit gnus-newsgroup-replied))
7694   (gnus-summary-position-point))
7695
7696 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7697 (make-obsolete 'gnus-summary-delete-marked-with
7698                'gnus-summary-limit-exclude-marks)
7699
7700 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7701   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7702 If REVERSE, limit the summary buffer to articles that are marked
7703 with MARKS.  MARKS can either be a string of marks or a list of marks.
7704 Returns how many articles were removed."
7705   (interactive "sMarks: ")
7706   (gnus-summary-limit-to-marks marks t))
7707
7708 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7709   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7710 If REVERSE (the prefix), limit the summary buffer to articles that are
7711 not marked with MARKS.  MARKS can either be a string of marks or a
7712 list of marks.
7713 Returns how many articles were removed."
7714   (interactive "sMarks: \nP")
7715   (prog1
7716       (let ((data gnus-newsgroup-data)
7717             (marks (if (listp marks) marks
7718                      (append marks nil))) ; Transform to list.
7719             articles)
7720         (while data
7721           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7722                   (memq (gnus-data-mark (car data)) marks))
7723             (push (gnus-data-number (car data)) articles))
7724           (setq data (cdr data)))
7725         (gnus-summary-limit articles))
7726     (gnus-summary-position-point)))
7727
7728 (defun gnus-summary-limit-to-score (score)
7729   "Limit to articles with score at or above SCORE."
7730   (interactive "NLimit to articles with score of at least: ")
7731   (let ((data gnus-newsgroup-data)
7732         articles)
7733     (while data
7734       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7735                 score)
7736         (push (gnus-data-number (car data)) articles))
7737       (setq data (cdr data)))
7738     (prog1
7739         (gnus-summary-limit articles)
7740       (gnus-summary-position-point))))
7741
7742 (defun gnus-summary-limit-to-unseen ()
7743   "Limit to unseen articles."
7744   (interactive)
7745   (prog1
7746       (gnus-summary-limit gnus-newsgroup-unseen)
7747     (gnus-summary-position-point)))
7748
7749 (defun gnus-summary-limit-include-thread (id)
7750   "Display all the hidden articles that is in the thread with ID in it.
7751 When called interactively, ID is the Message-ID of the current
7752 article."
7753   (interactive (list (mail-header-id (gnus-summary-article-header))))
7754   (let ((articles (gnus-articles-in-thread
7755                    (gnus-id-to-thread (gnus-root-id id)))))
7756     (prog1
7757         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7758       (gnus-summary-limit-include-matching-articles
7759        "subject"
7760        (regexp-quote (gnus-simplify-subject-re
7761                       (mail-header-subject (gnus-id-to-header id)))))
7762       (gnus-summary-position-point))))
7763
7764 (defun gnus-summary-limit-include-matching-articles (header regexp)
7765   "Display all the hidden articles that have HEADERs that match REGEXP."
7766   (interactive (list (read-string "Match on header: ")
7767                      (read-string "Regexp: ")))
7768   (let ((articles (gnus-find-matching-articles header regexp)))
7769     (prog1
7770         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7771       (gnus-summary-position-point))))
7772
7773 (defun gnus-summary-insert-dormant-articles ()
7774   "Insert all the dormant articles for this group into the current buffer."
7775   (interactive)
7776   (let ((gnus-verbose (max 6 gnus-verbose)))
7777     (if (not gnus-newsgroup-dormant)
7778         (gnus-message 3 "No cached articles for this group")
7779       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7780
7781 (defun gnus-summary-limit-include-dormant ()
7782   "Display all the hidden articles that are marked as dormant.
7783 Note that this command only works on a subset of the articles currently
7784 fetched for this group."
7785   (interactive)
7786   (unless gnus-newsgroup-dormant
7787     (error "There are no dormant articles in this group"))
7788   (prog1
7789       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7790     (gnus-summary-position-point)))
7791
7792 (defun gnus-summary-limit-exclude-dormant ()
7793   "Hide all dormant articles."
7794   (interactive)
7795   (prog1
7796       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7797     (gnus-summary-position-point)))
7798
7799 (defun gnus-summary-limit-exclude-childless-dormant ()
7800   "Hide all dormant articles that have no children."
7801   (interactive)
7802   (let ((data (gnus-data-list t))
7803         articles d children)
7804     ;; Find all articles that are either not dormant or have
7805     ;; children.
7806     (while (setq d (pop data))
7807       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7808                 (and (setq children
7809                            (gnus-article-children (gnus-data-number d)))
7810                      (let (found)
7811                        (while children
7812                          (when (memq (car children) articles)
7813                            (setq children nil
7814                                  found t))
7815                          (pop children))
7816                        found)))
7817         (push (gnus-data-number d) articles)))
7818     ;; Do the limiting.
7819     (prog1
7820         (gnus-summary-limit articles)
7821       (gnus-summary-position-point))))
7822
7823 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7824   "Mark all unread excluded articles as read.
7825 If ALL, mark even excluded ticked and dormants as read."
7826   (interactive "P")
7827   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7828   (let ((articles (gnus-sorted-ndifference
7829                    (sort
7830                     (mapcar (lambda (h) (mail-header-number h))
7831                             gnus-newsgroup-headers)
7832                     '<)
7833                    gnus-newsgroup-limit))
7834         article)
7835     (setq gnus-newsgroup-unreads
7836           (gnus-sorted-intersection gnus-newsgroup-unreads
7837                                     gnus-newsgroup-limit))
7838     (if all
7839         (setq gnus-newsgroup-dormant nil
7840               gnus-newsgroup-marked nil
7841               gnus-newsgroup-reads
7842               (nconc
7843                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7844                gnus-newsgroup-reads))
7845       (while (setq article (pop articles))
7846         (unless (or (memq article gnus-newsgroup-dormant)
7847                     (memq article gnus-newsgroup-marked))
7848           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7849
7850 (defun gnus-summary-limit (articles &optional pop)
7851   (if pop
7852       ;; We pop the previous limit off the stack and use that.
7853       (setq articles (car gnus-newsgroup-limits)
7854             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7855     ;; We use the new limit, so we push the old limit on the stack.
7856     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7857   ;; Set the limit.
7858   (setq gnus-newsgroup-limit articles)
7859   (let ((total (length gnus-newsgroup-data))
7860         (data (gnus-data-find-list (gnus-summary-article-number)))
7861         (gnus-summary-mark-below nil)   ; Inhibit this.
7862         found)
7863     ;; This will do all the work of generating the new summary buffer
7864     ;; according to the new limit.
7865     (gnus-summary-prepare)
7866     ;; Hide any threads, possibly.
7867     (gnus-summary-maybe-hide-threads)
7868     ;; Try to return to the article you were at, or one in the
7869     ;; neighborhood.
7870     (when data
7871       ;; We try to find some article after the current one.
7872       (while data
7873         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7874           (setq data nil
7875                 found t))
7876         (setq data (cdr data))))
7877     (unless found
7878       ;; If there is no data, that means that we were after the last
7879       ;; article.  The same goes when we can't find any articles
7880       ;; after the current one.
7881       (goto-char (point-max))
7882       (gnus-summary-find-prev))
7883     (gnus-set-mode-line 'summary)
7884     ;; We return how many articles were removed from the summary
7885     ;; buffer as a result of the new limit.
7886     (- total (length gnus-newsgroup-data))))
7887
7888 (defsubst gnus-invisible-cut-children (threads)
7889   (let ((num 0))
7890     (while threads
7891       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7892         (incf num))
7893       (pop threads))
7894     (< num 2)))
7895
7896 (defsubst gnus-cut-thread (thread)
7897   "Go forwards in the thread until we find an article that we want to display."
7898   (when (or (eq gnus-fetch-old-headers 'some)
7899             (eq gnus-fetch-old-headers 'invisible)
7900             (numberp gnus-fetch-old-headers)
7901             (eq gnus-build-sparse-threads 'some)
7902             (eq gnus-build-sparse-threads 'more))
7903     ;; Deal with old-fetched headers and sparse threads.
7904     (while (and
7905             thread
7906             (or
7907              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7908              (gnus-summary-article-ancient-p
7909               (mail-header-number (car thread))))
7910             (if (or (<= (length (cdr thread)) 1)
7911                     (eq gnus-fetch-old-headers 'invisible))
7912                 (setq gnus-newsgroup-limit
7913                       (delq (mail-header-number (car thread))
7914                             gnus-newsgroup-limit)
7915                       thread (cadr thread))
7916               (when (gnus-invisible-cut-children (cdr thread))
7917                 (let ((th (cdr thread)))
7918                   (while th
7919                     (if (memq (mail-header-number (caar th))
7920                               gnus-newsgroup-limit)
7921                         (setq thread (car th)
7922                               th nil)
7923                       (setq th (cdr th))))))))))
7924   thread)
7925
7926 (defun gnus-cut-threads (threads)
7927   "Cut off all uninteresting articles from the beginning of THREADS."
7928   (when (or (eq gnus-fetch-old-headers 'some)
7929             (eq gnus-fetch-old-headers 'invisible)
7930             (numberp gnus-fetch-old-headers)
7931             (eq gnus-build-sparse-threads 'some)
7932             (eq gnus-build-sparse-threads 'more))
7933     (let ((th threads))
7934       (while th
7935         (setcar th (gnus-cut-thread (car th)))
7936         (setq th (cdr th)))))
7937   ;; Remove nixed out threads.
7938   (delq nil threads))
7939
7940 (defun gnus-summary-initial-limit (&optional show-if-empty)
7941   "Figure out what the initial limit is supposed to be on group entry.
7942 This entails weeding out unwanted dormants, low-scored articles,
7943 fetch-old-headers verbiage, and so on."
7944   ;; Most groups have nothing to remove.
7945   (if (or gnus-inhibit-limiting
7946           (and (null gnus-newsgroup-dormant)
7947                (eq gnus-newsgroup-display 'gnus-not-ignore)
7948                (not (eq gnus-fetch-old-headers 'some))
7949                (not (numberp gnus-fetch-old-headers))
7950                (not (eq gnus-fetch-old-headers 'invisible))
7951                (null gnus-summary-expunge-below)
7952                (not (eq gnus-build-sparse-threads 'some))
7953                (not (eq gnus-build-sparse-threads 'more))
7954                (null gnus-thread-expunge-below)
7955                (not gnus-use-nocem)))
7956       ()                                ; Do nothing.
7957     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7958     (setq gnus-newsgroup-limit nil)
7959     (mapatoms
7960      (lambda (node)
7961        (unless (car (symbol-value node))
7962          ;; These threads have no parents -- they are roots.
7963          (let ((nodes (cdr (symbol-value node)))
7964                thread)
7965            (while nodes
7966              (if (and gnus-thread-expunge-below
7967                       (< (gnus-thread-total-score (car nodes))
7968                          gnus-thread-expunge-below))
7969                  (gnus-expunge-thread (pop nodes))
7970                (setq thread (pop nodes))
7971                (gnus-summary-limit-children thread))))))
7972      gnus-newsgroup-dependencies)
7973     ;; If this limitation resulted in an empty group, we might
7974     ;; pop the previous limit and use it instead.
7975     (when (and (not gnus-newsgroup-limit)
7976                show-if-empty)
7977       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7978     gnus-newsgroup-limit))
7979
7980 (defun gnus-summary-limit-children (thread)
7981   "Return 1 if this subthread is visible and 0 if it is not."
7982   ;; First we get the number of visible children to this thread.  This
7983   ;; is done by recursing down the thread using this function, so this
7984   ;; will really go down to a leaf article first, before slowly
7985   ;; working its way up towards the root.
7986   (when thread
7987     (let* ((max-lisp-eval-depth 5000)
7988            (children
7989            (if (cdr thread)
7990                (apply '+ (mapcar 'gnus-summary-limit-children
7991                                  (cdr thread)))
7992              0))
7993           (number (mail-header-number (car thread)))
7994           score)
7995       (if (and
7996            (not (memq number gnus-newsgroup-marked))
7997            (or
7998             ;; If this article is dormant and has absolutely no visible
7999             ;; children, then this article isn't visible.
8000             (and (memq number gnus-newsgroup-dormant)
8001                  (zerop children))
8002             ;; If this is "fetch-old-headered" and there is no
8003             ;; visible children, then we don't want this article.
8004             (and (or (eq gnus-fetch-old-headers 'some)
8005                      (numberp gnus-fetch-old-headers))
8006                  (gnus-summary-article-ancient-p number)
8007                  (zerop children))
8008             ;; If this is "fetch-old-headered" and `invisible', then
8009             ;; we don't want this article.
8010             (and (eq gnus-fetch-old-headers 'invisible)
8011                  (gnus-summary-article-ancient-p number))
8012             ;; If this is a sparsely inserted article with no children,
8013             ;; we don't want it.
8014             (and (eq gnus-build-sparse-threads 'some)
8015                  (gnus-summary-article-sparse-p number)
8016                  (zerop children))
8017             ;; If we use expunging, and this article is really
8018             ;; low-scored, then we don't want this article.
8019             (when (and gnus-summary-expunge-below
8020                        (< (setq score
8021                                 (or (cdr (assq number gnus-newsgroup-scored))
8022                                     gnus-summary-default-score))
8023                           gnus-summary-expunge-below))
8024               ;; We increase the expunge-tally here, but that has
8025               ;; nothing to do with the limits, really.
8026               (incf gnus-newsgroup-expunged-tally)
8027               ;; We also mark as read here, if that's wanted.
8028               (when (and gnus-summary-mark-below
8029                          (< score gnus-summary-mark-below))
8030                 (setq gnus-newsgroup-unreads
8031                       (delq number gnus-newsgroup-unreads))
8032                 (if gnus-newsgroup-auto-expire
8033                     (push number gnus-newsgroup-expirable)
8034                   (push (cons number gnus-low-score-mark)
8035                         gnus-newsgroup-reads)))
8036               t)
8037             ;; Do the `display' group parameter.
8038             (and gnus-newsgroup-display
8039                  (not (funcall gnus-newsgroup-display)))
8040             ;; Check NoCeM things.
8041             (if (and gnus-use-nocem
8042                      (gnus-nocem-unwanted-article-p
8043                       (mail-header-id (car thread))))
8044                 (progn
8045                   (setq gnus-newsgroup-unreads
8046                         (delq number gnus-newsgroup-unreads))
8047                   t))))
8048           ;; Nope, invisible article.
8049           0
8050         ;; Ok, this article is to be visible, so we add it to the limit
8051         ;; and return 1.
8052         (push number gnus-newsgroup-limit)
8053         1))))
8054
8055 (defun gnus-expunge-thread (thread)
8056   "Mark all articles in THREAD as read."
8057   (let* ((number (mail-header-number (car thread))))
8058     (incf gnus-newsgroup-expunged-tally)
8059     ;; We also mark as read here, if that's wanted.
8060     (setq gnus-newsgroup-unreads
8061           (delq number gnus-newsgroup-unreads))
8062     (if gnus-newsgroup-auto-expire
8063         (push number gnus-newsgroup-expirable)
8064       (push (cons number gnus-low-score-mark)
8065             gnus-newsgroup-reads)))
8066   ;; Go recursively through all subthreads.
8067   (mapcar 'gnus-expunge-thread (cdr thread)))
8068
8069 ;; Summary article oriented commands
8070
8071 (defun gnus-summary-refer-parent-article (n)
8072   "Refer parent article N times.
8073 If N is negative, go to ancestor -N instead.
8074 The difference between N and the number of articles fetched is returned."
8075   (interactive "p")
8076   (let ((skip 1)
8077         error header ref)
8078     (when (not (natnump n))
8079       (setq skip (abs n)
8080             n 1))
8081     (while (and (> n 0)
8082                 (not error))
8083       (setq header (gnus-summary-article-header))
8084       (if (and (eq (mail-header-number header)
8085                    (cdr gnus-article-current))
8086                (equal gnus-newsgroup-name
8087                       (car gnus-article-current)))
8088           ;; If we try to find the parent of the currently
8089           ;; displayed article, then we take a look at the actual
8090           ;; References header, since this is slightly more
8091           ;; reliable than the References field we got from the
8092           ;; server.
8093           (save-excursion
8094             (set-buffer gnus-original-article-buffer)
8095             (nnheader-narrow-to-headers)
8096             (unless (setq ref (message-fetch-field "references"))
8097               (when (setq ref (message-fetch-field "in-reply-to"))
8098                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8099             (widen))
8100         (setq ref
8101               ;; It's not the current article, so we take a bet on
8102               ;; the value we got from the server.
8103               (mail-header-references header)))
8104       (if (and ref
8105                (not (equal ref "")))
8106           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8107             (gnus-message 1 "Couldn't find parent"))
8108         (gnus-message 1 "No references in article %d"
8109                       (gnus-summary-article-number))
8110         (setq error t))
8111       (decf n))
8112     (gnus-summary-position-point)
8113     n))
8114
8115 (defun gnus-summary-refer-references ()
8116   "Fetch all articles mentioned in the References header.
8117 Return the number of articles fetched."
8118   (interactive)
8119   (let ((ref (mail-header-references (gnus-summary-article-header)))
8120         (current (gnus-summary-article-number))
8121         (n 0))
8122     (if (or (not ref)
8123             (equal ref ""))
8124         (error "No References in the current article")
8125       ;; For each Message-ID in the References header...
8126       (while (string-match "<[^>]*>" ref)
8127         (incf n)
8128         ;; ... fetch that article.
8129         (gnus-summary-refer-article
8130          (prog1 (match-string 0 ref)
8131            (setq ref (substring ref (match-end 0))))))
8132       (gnus-summary-goto-subject current)
8133       (gnus-summary-position-point)
8134       n)))
8135
8136 (defun gnus-summary-refer-thread (&optional limit)
8137   "Fetch all articles in the current thread.
8138 If LIMIT (the numerical prefix), fetch that many old headers instead
8139 of what's specified by the `gnus-refer-thread-limit' variable."
8140   (interactive "P")
8141   (let ((id (mail-header-id (gnus-summary-article-header)))
8142         (limit (if limit (prefix-numeric-value limit)
8143                  gnus-refer-thread-limit)))
8144     (unless (eq gnus-fetch-old-headers 'invisible)
8145       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8146       ;; Retrieve the headers and read them in.
8147       (if (eq (if (numberp limit)
8148                   (gnus-retrieve-headers
8149                    (list (min
8150                           (+ (mail-header-number
8151                               (gnus-summary-article-header))
8152                              limit)
8153                           gnus-newsgroup-end))
8154                    gnus-newsgroup-name (* limit 2))
8155                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8156                 ;; headers.
8157                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8158                                        gnus-newsgroup-name limit))
8159               'nov)
8160           (gnus-build-all-threads)
8161         (error "Can't fetch thread from back ends that don't support NOV"))
8162       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8163     (gnus-summary-limit-include-thread id)))
8164
8165 (defun gnus-summary-refer-article (message-id)
8166   "Fetch an article specified by MESSAGE-ID."
8167   (interactive "sMessage-ID: ")
8168   (when (and (stringp message-id)
8169              (not (zerop (length message-id))))
8170     (setq message-id (gnus-replace-in-string message-id " " ""))
8171     ;; Construct the correct Message-ID if necessary.
8172     ;; Suggested by tale@pawl.rpi.edu.
8173     (unless (string-match "^<" message-id)
8174       (setq message-id (concat "<" message-id)))
8175     (unless (string-match ">$" message-id)
8176       (setq message-id (concat message-id ">")))
8177     ;; People often post MIDs from URLs, so unhex it:
8178     (unless (string-match "@" message-id)
8179       (setq message-id (gnus-url-unhex-string message-id)))
8180     (let* ((header (gnus-id-to-header message-id))
8181            (sparse (and header
8182                         (gnus-summary-article-sparse-p
8183                          (mail-header-number header))
8184                         (memq (mail-header-number header)
8185                               gnus-newsgroup-limit)))
8186            number)
8187       (cond
8188        ;; If the article is present in the buffer we just go to it.
8189        ((and header
8190              (or (not (gnus-summary-article-sparse-p
8191                        (mail-header-number header)))
8192                  sparse))
8193         (prog1
8194             (gnus-summary-goto-article
8195              (mail-header-number header) nil t)
8196           (when sparse
8197             (gnus-summary-update-article (mail-header-number header)))))
8198        (t
8199         ;; We fetch the article.
8200         (catch 'found
8201           (dolist (gnus-override-method (gnus-refer-article-methods))
8202             (when (and (gnus-check-server gnus-override-method)
8203                        ;; Fetch the header,
8204                        (setq number (gnus-summary-insert-subject message-id)))
8205               ;; and display the article.
8206               (gnus-summary-select-article nil nil nil number)
8207               (throw 'found t)))
8208           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8209
8210 (defun gnus-refer-article-methods ()
8211   "Return a list of referable methods."
8212   (cond
8213    ;; No method, so we default to current and native.
8214    ((null gnus-refer-article-method)
8215     (list gnus-current-select-method gnus-select-method))
8216    ;; Current.
8217    ((eq 'current gnus-refer-article-method)
8218     (list gnus-current-select-method))
8219    ;; List of select methods.
8220    ((not (and (symbolp (car gnus-refer-article-method))
8221               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8222     (let (out)
8223       (dolist (method gnus-refer-article-method)
8224         (push (if (eq 'current method)
8225                   gnus-current-select-method
8226                 method)
8227               out))
8228       (nreverse out)))
8229    ;; One single select method.
8230    (t
8231     (list gnus-refer-article-method))))
8232
8233 (defun gnus-summary-edit-parameters ()
8234   "Edit the group parameters of the current group."
8235   (interactive)
8236   (gnus-group-edit-group gnus-newsgroup-name 'params))
8237
8238 (defun gnus-summary-customize-parameters ()
8239   "Customize the group parameters of the current group."
8240   (interactive)
8241   (gnus-group-customize gnus-newsgroup-name))
8242
8243 (defun gnus-summary-enter-digest-group (&optional force)
8244   "Enter an nndoc group based on the current article.
8245 If FORCE, force a digest interpretation.  If not, try
8246 to guess what the document format is."
8247   (interactive "P")
8248   (let ((conf gnus-current-window-configuration))
8249     (save-window-excursion
8250       (save-excursion
8251         (let (gnus-article-prepare-hook
8252               gnus-display-mime-function
8253               gnus-break-pages)
8254           (gnus-summary-select-article))))
8255     (setq gnus-current-window-configuration conf)
8256     (let* ((name (format "%s-%d"
8257                          (gnus-group-prefixed-name
8258                           gnus-newsgroup-name (list 'nndoc ""))
8259                          (save-excursion
8260                            (set-buffer gnus-summary-buffer)
8261                            gnus-current-article)))
8262            (ogroup gnus-newsgroup-name)
8263            (params (append (gnus-info-params (gnus-get-info ogroup))
8264                            (list (cons 'to-group ogroup))
8265                            (list (cons 'parent-group ogroup))
8266                            (list (cons 'save-article-group ogroup))))
8267            (case-fold-search t)
8268            (buf (current-buffer))
8269            dig to-address)
8270       (save-excursion
8271         (set-buffer gnus-original-article-buffer)
8272         ;; Have the digest group inherit the main mail address of
8273         ;; the parent article.
8274         (when (setq to-address (or (gnus-fetch-field "reply-to")
8275                                    (gnus-fetch-field "from")))
8276           (setq params (append
8277                         (list (cons 'to-address
8278                                     (funcall gnus-decode-encoded-word-function
8279                                              to-address))))))
8280         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8281         (insert-buffer-substring gnus-original-article-buffer)
8282         ;; Remove lines that may lead nndoc to misinterpret the
8283         ;; document type.
8284         (narrow-to-region
8285          (goto-char (point-min))
8286          (or (search-forward "\n\n" nil t) (point)))
8287         (goto-char (point-min))
8288         (delete-matching-lines "^Path:\\|^From ")
8289         (widen))
8290       (unwind-protect
8291           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8292                     (gnus-newsgroup-ephemeral-ignored-charsets
8293                      gnus-newsgroup-ignored-charsets))
8294                 (gnus-group-read-ephemeral-group
8295                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8296                               (nndoc-article-type
8297                                ,(if force 'mbox 'guess)))
8298                  t nil nil nil
8299                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8300                                                         "ADAPT")))))
8301               ;; Make all postings to this group go to the parent group.
8302               (nconc (gnus-info-params (gnus-get-info name))
8303                      params)
8304             ;; Couldn't select this doc group.
8305             (switch-to-buffer buf)
8306             (gnus-set-global-variables)
8307             (gnus-configure-windows 'summary)
8308             (gnus-message 3 "Article couldn't be entered?"))
8309         (kill-buffer dig)))))
8310
8311 (defun gnus-summary-read-document (n)
8312   "Open a new group based on the current article(s).
8313 This will allow you to read digests and other similar
8314 documents as newsgroups.
8315 Obeys the standard process/prefix convention."
8316   (interactive "P")
8317   (let* ((articles (gnus-summary-work-articles n))
8318          (ogroup gnus-newsgroup-name)
8319          (params (append (gnus-info-params (gnus-get-info ogroup))
8320                          (list (cons 'to-group ogroup))))
8321          article group egroup groups vgroup)
8322     (while (setq article (pop articles))
8323       (setq group (format "%s-%d" gnus-newsgroup-name article))
8324       (gnus-summary-remove-process-mark article)
8325       (when (gnus-summary-display-article article)
8326         (save-excursion
8327           (with-temp-buffer
8328             (insert-buffer-substring gnus-original-article-buffer)
8329             ;; Remove some headers that may lead nndoc to make
8330             ;; the wrong guess.
8331             (message-narrow-to-head)
8332             (goto-char (point-min))
8333             (delete-matching-lines "^Path:\\|^From ")
8334             (widen)
8335             (if (setq egroup
8336                       (gnus-group-read-ephemeral-group
8337                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8338                                      (nndoc-article-type guess))
8339                        t nil t))
8340                 (progn
8341                   ;; Make all postings to this group go to the parent group.
8342                   (nconc (gnus-info-params (gnus-get-info egroup))
8343                          params)
8344                   (push egroup groups))
8345               ;; Couldn't select this doc group.
8346               (gnus-error 3 "Article couldn't be entered"))))))
8347     ;; Now we have selected all the documents.
8348     (cond
8349      ((not groups)
8350       (error "None of the articles could be interpreted as documents"))
8351      ((gnus-group-read-ephemeral-group
8352        (setq vgroup (format
8353                      "nnvirtual:%s-%s" gnus-newsgroup-name
8354                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8355        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8356        t
8357        (cons (current-buffer) 'summary)))
8358      (t
8359       (error "Couldn't select virtual nndoc group")))))
8360
8361 (defun gnus-summary-isearch-article (&optional regexp-p)
8362   "Do incremental search forward on the current article.
8363 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8364   (interactive "P")
8365   (let* ((gnus-inhibit-treatment t)
8366          (old (gnus-summary-select-article)))
8367     (gnus-configure-windows 'article)
8368     (gnus-eval-in-buffer-window gnus-article-buffer
8369       (save-restriction
8370         (widen)
8371         (when (eq 'old old)
8372           (gnus-article-show-all-headers))
8373         (goto-char (point-min))
8374         (isearch-forward regexp-p)))))
8375
8376 (defun gnus-summary-search-article-forward (regexp &optional backward)
8377   "Search for an article containing REGEXP forward.
8378 If BACKWARD, search backward instead."
8379   (interactive
8380    (list (read-string
8381           (format "Search article %s (regexp%s): "
8382                   (if current-prefix-arg "backward" "forward")
8383                   (if gnus-last-search-regexp
8384                       (concat ", default " gnus-last-search-regexp)
8385                     "")))
8386          current-prefix-arg))
8387   (if (string-equal regexp "")
8388       (setq regexp (or gnus-last-search-regexp ""))
8389     (setq gnus-last-search-regexp regexp)
8390     (setq gnus-article-before-search gnus-current-article))
8391   ;; Intentionally set gnus-last-article.
8392   (setq gnus-last-article gnus-article-before-search)
8393   (let ((gnus-last-article gnus-last-article))
8394     (if (gnus-summary-search-article regexp backward)
8395         (gnus-summary-show-thread)
8396       (error "Search failed: \"%s\"" regexp))))
8397
8398 (defun gnus-summary-search-article-backward (regexp)
8399   "Search for an article containing REGEXP backward."
8400   (interactive
8401    (list (read-string
8402           (format "Search article backward (regexp%s): "
8403                   (if gnus-last-search-regexp
8404                       (concat ", default " gnus-last-search-regexp)
8405                     "")))))
8406   (gnus-summary-search-article-forward regexp 'backward))
8407
8408 (eval-when-compile
8409   (defmacro gnus-summary-search-article-position-point (regexp backward)
8410     "Dehighlight the last matched text and goto the beginning position."
8411     (` (if (and gnus-summary-search-article-matched-data
8412                 (let ((text (caddr gnus-summary-search-article-matched-data))
8413                       (inhibit-read-only t)
8414                       buffer-read-only)
8415                   (delete-region
8416                    (goto-char (car gnus-summary-search-article-matched-data))
8417                    (cadr gnus-summary-search-article-matched-data))
8418                   (insert text)
8419                   (string-match (, regexp) text)))
8420            (if (, backward) (beginning-of-line) (end-of-line))
8421          (goto-char (if (, backward) (point-max) (point-min))))))
8422
8423   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8424     "Place point where X-Face image is displayed."
8425     (if (featurep 'xemacs)
8426         (` (let ((end (if (search-forward "\n\n" nil t)
8427                           (goto-char (1- (point)))
8428                         (point-min)))
8429                  extent)
8430              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8431              (unless (and (re-search-forward "^From:" end t)
8432                           (setq extent (extent-at (point)))
8433                           (extent-begin-glyph extent))
8434                (goto-char (, opoint)))))
8435       (` (let ((end (if (search-forward "\n\n" nil t)
8436                         (goto-char (1- (point)))
8437                       (point-min)))
8438                (start (or (search-backward "\n\n" nil t) (point-min))))
8439            (goto-char
8440             (or (text-property-any start end 'x-face-image t);; x-face-e21
8441                 (text-property-any start end 'x-face-mule-bitmap-image t)
8442                 (, opoint)))))))
8443
8444   (defmacro gnus-summary-search-article-highlight-matched-text
8445     (backward treated x-face)
8446     "Highlight matched text in the function `gnus-summary-search-article'."
8447     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8448              (end (set-marker (make-marker) (match-end 0)))
8449              (inhibit-read-only t)
8450              buffer-read-only)
8451          (unless treated
8452            (let ((,@
8453                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8454                     (mapcar
8455                      (lambda (item) (setq items (delq item items)))
8456                      '(gnus-treat-buttonize
8457                        gnus-treat-fill-article
8458                        gnus-treat-fill-long-lines
8459                        gnus-treat-emphasize
8460                        gnus-treat-highlight-headers
8461                        gnus-treat-highlight-citation
8462                        gnus-treat-highlight-signature
8463                        gnus-treat-overstrike
8464                        gnus-treat-display-x-face
8465                        gnus-treat-buttonize-head
8466                        gnus-treat-decode-article-as-default-mime-charset))
8467                     (static-if (featurep 'xemacs)
8468                         items
8469                       (cons '(x-face-mule-delete-x-face-field
8470                               (quote never))
8471                             items))))
8472                  (gnus-treat-display-x-face
8473                   (when (, x-face) gnus-treat-display-x-face)))
8474              (gnus-article-prepare-mime-display)))
8475          (goto-char (if (, backward) start end))
8476          (when (, x-face)
8477            (gnus-summary-search-article-highlight-goto-x-face (point)))
8478          (setq gnus-summary-search-article-matched-data
8479                (list start end (buffer-substring start end)))
8480          (unless (eq start end);; matched text has been deleted. :-<
8481            (put-text-property start end 'face
8482                               (or (find-face 'isearch)
8483                                   'secondary-selection))))))
8484   )
8485
8486 (defun gnus-summary-search-article (regexp &optional backward)
8487   "Search for an article containing REGEXP.
8488 Optional argument BACKWARD means do search for backward.
8489 `gnus-select-article-hook' is not called during the search."
8490   ;; We have to require this here to make sure that the following
8491   ;; dynamic binding isn't shadowed by autoloading.
8492   (require 'gnus-async)
8493   (require 'gnus-art)
8494   (let ((gnus-select-article-hook nil)  ;Disable hook.
8495         (gnus-article-prepare-hook nil)
8496         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8497         (gnus-use-article-prefetch nil)
8498         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8499         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8500         (gnus-visual nil)
8501         (gnus-keep-backlog nil)
8502         (gnus-break-pages nil)
8503         (gnus-summary-display-arrow nil)
8504         (gnus-updated-mode-lines nil)
8505         (gnus-auto-center-summary nil)
8506         (sum (current-buffer))
8507         (found nil)
8508         point treated)
8509     (gnus-save-hidden-threads
8510       (static-if (featurep 'xemacs)
8511           (let ((gnus-inhibit-treatment t))
8512             (setq treated (eq 'old (gnus-summary-select-article)))
8513             (when (and treated
8514                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8515                                  (window-live-p (get-buffer-window
8516                                                  gnus-article-buffer t)))))
8517               (gnus-summary-select-article nil t)
8518               (setq treated nil)))
8519         (let ((gnus-inhibit-treatment t)
8520               (x-face-mule-delete-x-face-field 'never))
8521           (setq treated (eq 'old (gnus-summary-select-article)))
8522           (when (and treated
8523                      (not
8524                       (and (gnus-buffer-live-p gnus-article-buffer)
8525                            (window-live-p (get-buffer-window
8526                                            gnus-article-buffer t))
8527                            (or (not (string-match "^\\^X-Face:" regexp))
8528                                (with-current-buffer gnus-article-buffer
8529                                  gnus-summary-search-article-matched-data)))))
8530             (gnus-summary-select-article nil t)
8531             (setq treated nil))))
8532       (set-buffer gnus-article-buffer)
8533       (widen)
8534       (if treated
8535           (progn
8536             (gnus-article-show-all-headers)
8537             (gnus-summary-search-article-position-point regexp backward))
8538         (goto-char (if backward (point-max) (point-min))))
8539       (while (not found)
8540         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8541         (if (if backward
8542                 (re-search-backward regexp nil t)
8543               (re-search-forward regexp nil t))
8544             ;; We found the regexp.
8545             (progn
8546               (gnus-summary-search-article-highlight-matched-text
8547                backward treated (string-match "^\\^X-Face:" regexp))
8548               (setq found 'found)
8549               (forward-line
8550                (/ (- 2 (window-height
8551                         (get-buffer-window gnus-article-buffer t)))
8552                   2))
8553               (set-window-start
8554                (get-buffer-window (current-buffer))
8555                (point))
8556               (set-buffer sum)
8557               (setq point (point)))
8558           ;; We didn't find it, so we go to the next article.
8559           (set-buffer sum)
8560           (setq found 'not)
8561           (while (eq found 'not)
8562             (if (not (if backward (gnus-summary-find-prev)
8563                        (gnus-summary-find-next)))
8564                 ;; No more articles.
8565                 (setq found t)
8566               ;; Select the next article and adjust point.
8567               (unless (gnus-summary-article-sparse-p
8568                        (gnus-summary-article-number))
8569                 (setq found nil)
8570                 (let ((gnus-inhibit-treatment t))
8571                   (gnus-summary-select-article))
8572                 (setq treated nil)
8573                 (set-buffer gnus-article-buffer)
8574                 (widen)
8575                 (goto-char (if backward (point-max) (point-min))))))))
8576       (gnus-message 7 ""))
8577     ;; Return whether we found the regexp.
8578     (when (eq found 'found)
8579       (goto-char point)
8580       (gnus-summary-show-thread)
8581       (gnus-summary-goto-subject gnus-current-article)
8582       (gnus-summary-position-point)
8583       t)))
8584
8585 (defun gnus-find-matching-articles (header regexp)
8586   "Return a list of all articles that match REGEXP on HEADER.
8587 This search includes all articles in the current group that Gnus has
8588 fetched headers for, whether they are displayed or not."
8589   (let ((articles nil)
8590         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8591         (case-fold-search t))
8592     (dolist (header gnus-newsgroup-headers)
8593       (when (string-match regexp (funcall func header))
8594         (push (mail-header-number header) articles)))
8595     (nreverse articles)))
8596
8597 (defun gnus-summary-find-matching (header regexp &optional backward unread
8598                                           not-case-fold not-matching)
8599   "Return a list of all articles that match REGEXP on HEADER.
8600 The search stars on the current article and goes forwards unless
8601 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8602 If UNREAD is non-nil, only unread articles will
8603 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8604 in the comparisons. If NOT-MATCHING, return a list of all articles that
8605 not match REGEXP on HEADER."
8606   (let ((case-fold-search (not not-case-fold))
8607         articles d func)
8608     (if (consp header)
8609         (if (eq (car header) 'extra)
8610             (setq func
8611                   `(lambda (h)
8612                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8613                          "")))
8614           (error "%s is an invalid header" header))
8615       (unless (fboundp (intern (concat "mail-header-" header)))
8616         (error "%s is not a valid header" header))
8617       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8618     (dolist (d (if (eq backward 'all)
8619                    gnus-newsgroup-data
8620                  (gnus-data-find-list
8621                   (gnus-summary-article-number)
8622                   (gnus-data-list backward))))
8623       (when (and (or (not unread)       ; We want all articles...
8624                      (gnus-data-unread-p d)) ; Or just unreads.
8625                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8626                  (if not-matching
8627                      (not (string-match
8628                            regexp
8629                            (funcall func (gnus-data-header d))))
8630                    (string-match regexp
8631                                  (funcall func (gnus-data-header d)))))
8632         (push (gnus-data-number d) articles))) ; Success!
8633     (nreverse articles)))
8634
8635 (defun gnus-summary-execute-command (header regexp command &optional backward)
8636   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8637 If HEADER is an empty string (or nil), the match is done on the entire
8638 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8639   (interactive
8640    (list (let ((completion-ignore-case t))
8641            (completing-read
8642             "Header name: "
8643             (mapcar (lambda (header) (list (format "%s" header)))
8644                     (append
8645                      '("Number" "Subject" "From" "Lines" "Date"
8646                        "Message-ID" "Xref" "References" "Body")
8647                      gnus-extra-headers))
8648             nil 'require-match))
8649          (read-string "Regexp: ")
8650          (read-key-sequence "Command: ")
8651          current-prefix-arg))
8652   (when (equal header "Body")
8653     (setq header ""))
8654   ;; Hidden thread subtrees must be searched as well.
8655   (gnus-summary-show-all-threads)
8656   ;; We don't want to change current point nor window configuration.
8657   (save-excursion
8658     (save-window-excursion
8659       (let (gnus-visual
8660             gnus-treat-strip-trailing-blank-lines
8661             gnus-treat-strip-leading-blank-lines
8662             gnus-treat-strip-multiple-blank-lines
8663             gnus-treat-hide-boring-headers
8664             gnus-treat-fold-newsgroups
8665             gnus-article-prepare-hook)
8666         (gnus-message 6 "Executing %s..." (key-description command))
8667         ;; We'd like to execute COMMAND interactively so as to give arguments.
8668         (gnus-execute header regexp
8669                       `(call-interactively ',(key-binding command))
8670                       backward)
8671         (gnus-message 6 "Executing %s...done" (key-description command))))))
8672
8673 (defun gnus-summary-beginning-of-article ()
8674   "Scroll the article back to the beginning."
8675   (interactive)
8676   (gnus-summary-select-article)
8677   (gnus-configure-windows 'article)
8678   (gnus-eval-in-buffer-window gnus-article-buffer
8679     (widen)
8680     (goto-char (point-min))
8681     (when gnus-break-pages
8682       (gnus-narrow-to-page))))
8683
8684 (defun gnus-summary-end-of-article ()
8685   "Scroll to the end of the article."
8686   (interactive)
8687   (gnus-summary-select-article)
8688   (gnus-configure-windows 'article)
8689   (gnus-eval-in-buffer-window gnus-article-buffer
8690     (widen)
8691     (goto-char (point-max))
8692     (recenter -3)
8693     (when gnus-break-pages
8694       (when (re-search-backward page-delimiter nil t)
8695         (narrow-to-region (match-end 0) (point-max)))
8696       (gnus-narrow-to-page))))
8697
8698 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8699   "Truncate to LEN and quote all \"(\"'s in STRING."
8700   (gnus-replace-in-string (if (and len (> (length string) len))
8701                               (substring string 0 len)
8702                             string)
8703                           "[()]" "\\\\\\&"))
8704
8705 (defun gnus-summary-print-article (&optional filename n)
8706   "Generate and print a PostScript image of the process-marked (mail) articles.
8707
8708 If used interactively, print the current article if none are
8709 process-marked.  With prefix arg, prompt the user for the name of the
8710 file to save in.
8711
8712 When used from Lisp, accept two optional args FILENAME and N.  N means
8713 to print the next N articles.  If N is negative, print the N previous
8714 articles.  If N is nil and articles have been marked with the process
8715 mark, print these instead.
8716
8717 If the optional first argument FILENAME is nil, send the image to the
8718 printer.  If FILENAME is a string, save the PostScript image in a file with
8719 that name.  If FILENAME is a number, prompt the user for the name of the file
8720 to save in."
8721   (interactive (list (ps-print-preprint current-prefix-arg)))
8722   (dolist (article (gnus-summary-work-articles n))
8723     (gnus-summary-select-article nil nil 'pseudo article)
8724     (gnus-eval-in-buffer-window gnus-article-buffer
8725       (gnus-print-buffer))
8726     (gnus-summary-remove-process-mark article))
8727   (ps-despool filename))
8728
8729 (defun gnus-print-buffer ()
8730   (let ((buffer (generate-new-buffer " *print*")))
8731     (unwind-protect
8732         (progn
8733           (copy-to-buffer buffer (point-min) (point-max))
8734           (set-buffer buffer)
8735           (gnus-remove-text-with-property 'gnus-decoration)
8736           (when (gnus-visual-p 'article-highlight 'highlight)
8737             ;; Copy-to-buffer doesn't copy overlay.  So redo
8738             ;; highlight.
8739             (let ((gnus-article-buffer buffer))
8740               (gnus-article-highlight-citation t)
8741               (gnus-article-highlight-signature)
8742               (gnus-article-emphasize)
8743               (gnus-article-delete-invisible-text)))
8744           (let ((ps-left-header
8745                  (list
8746                   (concat "("
8747                           (gnus-summary-print-truncate-and-quote
8748                            (mail-header-subject gnus-current-headers)
8749                            66) ")")
8750                   (concat "("
8751                           (gnus-summary-print-truncate-and-quote
8752                            (mail-header-from gnus-current-headers)
8753                            45) ")")))
8754                 (ps-right-header
8755                  (list
8756                   "/pagenumberstring load"
8757                   (concat "("
8758                           (mail-header-date gnus-current-headers) ")"))))
8759             (gnus-run-hooks 'gnus-ps-print-hook)
8760             (save-excursion
8761               (if window-system
8762                   (ps-spool-buffer-with-faces)
8763                 (ps-spool-buffer)))))
8764       (kill-buffer buffer))))
8765
8766 (defun gnus-summary-show-article (&optional arg)
8767   "Force redisplaying of the current article.
8768 If ARG (the prefix) is a number, show the article with the charset
8769 defined in `gnus-summary-show-article-charset-alist', or the charset
8770 input.
8771 If ARG (the prefix) is non-nil and not a number, show the raw article
8772 without any article massaging functions being run.  Normally, the key
8773 strokes are `C-u g'."
8774   (interactive "P")
8775   (cond
8776    ((numberp arg)
8777     (gnus-summary-show-article t)
8778     (let* ((gnus-newsgroup-charset
8779             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8780                 (mm-read-coding-system
8781                  "View as charset: " ;; actually it is coding system.
8782                  (save-excursion
8783                    (set-buffer gnus-article-buffer)
8784                    (mm-detect-coding-region (point) (point-max))))))
8785            (default-mime-charset gnus-newsgroup-charset)
8786            (gnus-newsgroup-ignored-charsets 'gnus-all))
8787       (gnus-summary-select-article nil 'force)
8788       (let ((deps gnus-newsgroup-dependencies)
8789             head header lines)
8790         (save-excursion
8791           (set-buffer gnus-original-article-buffer)
8792           (save-restriction
8793             (message-narrow-to-head)
8794             (setq head (buffer-string))
8795             (goto-char (point-min))
8796             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8797               (goto-char (point-max))
8798               (widen)
8799               (setq lines (1- (count-lines (point) (point-max))))))
8800           (with-temp-buffer
8801             (insert (format "211 %d Article retrieved.\n"
8802                             (cdr gnus-article-current)))
8803             (insert head)
8804             (if lines (insert (format "Lines: %d\n" lines)))
8805             (insert ".\n")
8806             (let ((nntp-server-buffer (current-buffer)))
8807               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8808         (gnus-data-set-header
8809          (gnus-data-find (cdr gnus-article-current))
8810          header)
8811         (gnus-summary-update-article-line
8812          (cdr gnus-article-current) header)
8813         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8814           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8815    ((not arg)
8816     ;; Select the article the normal way.
8817     (gnus-summary-select-article nil 'force))
8818    (t
8819     ;; We have to require this here to make sure that the following
8820     ;; dynamic binding isn't shadowed by autoloading.
8821     (require 'gnus-async)
8822     (require 'gnus-art)
8823     ;; Bind the article treatment functions to nil.
8824     (let ((gnus-have-all-headers t)
8825           gnus-article-prepare-hook
8826           gnus-article-decode-hook
8827           gnus-break-pages
8828           gnus-show-mime
8829           (gnus-inhibit-treatment t))
8830       (gnus-summary-select-article nil 'force))))
8831   (gnus-summary-goto-subject gnus-current-article)
8832   (gnus-summary-position-point))
8833
8834 (defun gnus-summary-show-raw-article ()
8835   "Show the raw article without any article massaging functions being run."
8836   (interactive)
8837   (gnus-summary-show-article t))
8838
8839 (defun gnus-summary-verbose-headers (&optional arg)
8840   "Toggle permanent full header display.
8841 If ARG is a positive number, turn header display on.
8842 If ARG is a negative number, turn header display off."
8843   (interactive "P")
8844   (setq gnus-show-all-headers
8845         (cond ((or (not (numberp arg))
8846                    (zerop arg))
8847                (not gnus-show-all-headers))
8848               ((natnump arg)
8849                t)))
8850   (gnus-summary-show-article))
8851
8852 (defun gnus-summary-toggle-header (&optional arg)
8853   "Show the headers if they are hidden, or hide them if they are shown.
8854 If ARG is a positive number, show the entire header.
8855 If ARG is a negative number, hide the unwanted header lines."
8856   (interactive "P")
8857   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8858                      (get-buffer-window gnus-article-buffer t))))
8859     (with-current-buffer gnus-article-buffer
8860       (widen)
8861       (article-narrow-to-head)
8862       (let* ((buffer-read-only nil)
8863              (inhibit-point-motion-hooks t)
8864              (hidden (if (numberp arg)
8865                          (>= arg 0)
8866                        (gnus-article-hidden-text-p 'headers)))
8867              s e)
8868         (delete-region (point-min) (point-max))
8869         (with-current-buffer gnus-original-article-buffer
8870           (goto-char (setq s (point-min)))
8871           (setq e (if (search-forward "\n\n" nil t)
8872                       (1- (point))
8873                     (point-max))))
8874         (insert-buffer-substring gnus-original-article-buffer s e)
8875         ;; In T-gnus, gnus-article-decode-hook doesn't contain
8876         ;; article-decode-encoded-words by default.
8877         (article-decode-encoded-words)
8878         (run-hooks 'gnus-article-decode-hook)
8879         (if hidden
8880             (let ((gnus-treat-hide-headers nil)
8881                   (gnus-treat-hide-boring-headers nil))
8882               (gnus-delete-wash-type 'headers)
8883               (gnus-treat-article 'head))
8884           (gnus-treat-article 'head))
8885         (widen)
8886         (if window
8887             (set-window-start window (goto-char (point-min))))
8888         (if gnus-break-pages
8889             (gnus-narrow-to-page)
8890           (when (gnus-visual-p 'page-marker)
8891             (let ((buffer-read-only nil))
8892               (gnus-remove-text-with-property 'gnus-prev)
8893               (gnus-remove-text-with-property 'gnus-next))))
8894         (gnus-set-mode-line 'article)))))
8895
8896 (defun gnus-summary-show-all-headers ()
8897   "Make all header lines visible."
8898   (interactive)
8899   (gnus-summary-toggle-header 1))
8900
8901 (defun gnus-summary-toggle-mime (&optional arg)
8902   "Toggle MIME processing.
8903 If ARG is a positive number, turn MIME processing on."
8904   (interactive "P")
8905   (setq gnus-show-mime
8906         (if (null arg)
8907             (not gnus-show-mime)
8908           (> (prefix-numeric-value arg) 0)))
8909   (gnus-summary-select-article t 'force))
8910
8911 (defun gnus-summary-caesar-message (&optional arg)
8912   "Caesar rotate the current article by 13.
8913 The numerical prefix specifies how many places to rotate each letter
8914 forward."
8915   (interactive "P")
8916   (gnus-summary-select-article)
8917   (let ((mail-header-separator ""))
8918     (gnus-eval-in-buffer-window gnus-article-buffer
8919       (save-restriction
8920         (widen)
8921         (let ((start (window-start))
8922               buffer-read-only)
8923           (message-caesar-buffer-body arg)
8924           (set-window-start (get-buffer-window (current-buffer)) start))))))
8925
8926 (autoload 'unmorse-region "morse"
8927   "Convert morse coded text in region to ordinary ASCII text."
8928   t)
8929
8930 (defun gnus-summary-morse-message (&optional arg)
8931   "Morse decode the current article."
8932   (interactive "P")
8933   (gnus-summary-select-article)
8934   (let ((mail-header-separator ""))
8935     (gnus-eval-in-buffer-window gnus-article-buffer
8936       (save-excursion
8937         (save-restriction
8938           (widen)
8939           (let ((pos (window-start))
8940                 buffer-read-only)
8941             (goto-char (point-min))
8942             (when (message-goto-body)
8943               (gnus-narrow-to-body))
8944             (goto-char (point-min))
8945             (while (re-search-forward "·" (point-max) t)
8946               (replace-match "."))
8947             (unmorse-region (point-min) (point-max))
8948             (widen)
8949             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8950
8951 (defun gnus-summary-stop-page-breaking ()
8952   "Stop page breaking in the current article."
8953   (interactive)
8954   (gnus-summary-select-article)
8955   (gnus-eval-in-buffer-window gnus-article-buffer
8956     (widen)
8957     (when (gnus-visual-p 'page-marker)
8958       (let ((buffer-read-only nil))
8959         (gnus-remove-text-with-property 'gnus-prev)
8960         (gnus-remove-text-with-property 'gnus-next))
8961       (setq gnus-page-broken nil))))
8962
8963 (defun gnus-summary-move-article (&optional n to-newsgroup
8964                                             select-method action)
8965   "Move the current article to a different newsgroup.
8966 If N is a positive number, move the N next articles.
8967 If N is a negative number, move the N previous articles.
8968 If N is nil and any articles have been marked with the process mark,
8969 move those articles instead.
8970 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8971 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8972 re-spool using this method.
8973
8974 When called interactively with TO-NEWSGROUP being nil, the value of
8975 the variable `gnus-move-split-methods' is used for finding a default
8976 for the target newsgroup.
8977
8978 For this function to work, both the current newsgroup and the
8979 newsgroup that you want to move to have to support the `request-move'
8980 and `request-accept' functions.
8981
8982 ACTION can be either `move' (the default), `crosspost' or `copy'."
8983   (interactive "P")
8984   (unless action
8985     (setq action 'move))
8986   ;; Check whether the source group supports the required functions.
8987   (cond ((and (eq action 'move)
8988               (not (gnus-check-backend-function
8989                     'request-move-article gnus-newsgroup-name)))
8990          (error "The current group does not support article moving"))
8991         ((and (eq action 'crosspost)
8992               (not (gnus-check-backend-function
8993                     'request-replace-article gnus-newsgroup-name)))
8994          (error "The current group does not support article editing")))
8995   (let ((articles (gnus-summary-work-articles n))
8996         (prefix (if (gnus-check-backend-function
8997                      'request-move-article gnus-newsgroup-name)
8998                     (gnus-group-real-prefix gnus-newsgroup-name)
8999                   ""))
9000         (names '((move "Move" "Moving")
9001                  (copy "Copy" "Copying")
9002                  (crosspost "Crosspost" "Crossposting")))
9003         (copy-buf (save-excursion
9004                     (nnheader-set-temp-buffer " *copy article*")))
9005         art-group to-method new-xref article to-groups)
9006     (unless (assq action names)
9007       (error "Unknown action %s" action))
9008     ;; Read the newsgroup name.
9009     (when (and (not to-newsgroup)
9010                (not select-method))
9011       (if (and gnus-move-split-methods
9012                (not
9013                 (and (memq gnus-current-article articles)
9014                      (gnus-buffer-live-p gnus-original-article-buffer))))
9015           ;; When `gnus-move-split-methods' is non-nil, we have to
9016           ;; select an article to give `gnus-read-move-group-name' an
9017           ;; opportunity to suggest an appropriate default.  However,
9018           ;; we needn't render or mark the article.
9019           (let ((gnus-display-mime-function nil)
9020                 (gnus-article-prepare-hook nil)
9021                 (gnus-mark-article-hook nil))
9022             (gnus-summary-select-article nil nil nil (car articles))))
9023       (setq to-newsgroup
9024             (gnus-read-move-group-name
9025              (cadr (assq action names))
9026              (symbol-value (intern (format "gnus-current-%s-group" action)))
9027              articles prefix))
9028       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9029     (setq to-method (or select-method
9030                         (gnus-server-to-method
9031                          (gnus-group-method to-newsgroup))))
9032     ;; Check the method we are to move this article to...
9033     (unless (gnus-check-backend-function
9034              'request-accept-article (car to-method))
9035       (error "%s does not support article copying" (car to-method)))
9036     (unless (gnus-check-server to-method)
9037       (error "Can't open server %s" (car to-method)))
9038     (gnus-message 6 "%s to %s: %s..."
9039                   (caddr (assq action names))
9040                   (or (car select-method) to-newsgroup) articles)
9041     (while articles
9042       (setq article (pop articles))
9043       (setq
9044        art-group
9045        (cond
9046         ;; Move the article.
9047         ((eq action 'move)
9048          ;; Remove this article from future suppression.
9049          (gnus-dup-unsuppress-article article)
9050          (gnus-request-move-article
9051           article                       ; Article to move
9052           gnus-newsgroup-name           ; From newsgroup
9053           (nth 1 (gnus-find-method-for-group
9054                   gnus-newsgroup-name)) ; Server
9055           (list 'gnus-request-accept-article
9056                 to-newsgroup (list 'quote select-method)
9057                 (not articles) t)       ; Accept form
9058           (not articles)))              ; Only save nov last time
9059         ;; Copy the article.
9060         ((eq action 'copy)
9061          (save-excursion
9062            (set-buffer copy-buf)
9063            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9064              (gnus-request-accept-article
9065               to-newsgroup select-method (not articles) t))))
9066         ;; Crosspost the article.
9067         ((eq action 'crosspost)
9068          (let ((xref (message-tokenize-header
9069                       (mail-header-xref (gnus-summary-article-header article))
9070                       " ")))
9071            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9072                                   ":" (number-to-string article)))
9073            (unless xref
9074              (setq xref (list (system-name))))
9075            (setq new-xref
9076                  (concat
9077                   (mapconcat 'identity
9078                              (delete "Xref:" (delete new-xref xref))
9079                              " ")
9080                   " " new-xref))
9081            (save-excursion
9082              (set-buffer copy-buf)
9083              ;; First put the article in the destination group.
9084              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9085              (when (consp (setq art-group
9086                                 (gnus-request-accept-article
9087                                  to-newsgroup select-method (not articles))))
9088                (setq new-xref (concat new-xref " " (car art-group)
9089                                       ":"
9090                                       (number-to-string (cdr art-group))))
9091                ;; Now we have the new Xrefs header, so we insert
9092                ;; it and replace the new article.
9093                (nnheader-replace-header "Xref" new-xref)
9094                (gnus-request-replace-article
9095                 (cdr art-group) to-newsgroup (current-buffer))
9096                art-group))))))
9097       (cond
9098        ((not art-group)
9099         (gnus-message 1 "Couldn't %s article %s: %s"
9100                       (cadr (assq action names)) article
9101                       (nnheader-get-report (car to-method))))
9102        ((eq art-group 'junk)
9103         (when (eq action 'move)
9104           (gnus-summary-mark-article article gnus-canceled-mark)
9105           (gnus-message 4 "Deleted article %s" article)
9106           ;; run the delete hook
9107           (run-hook-with-args 'gnus-summary-article-delete-hook
9108                               action
9109                               (gnus-data-header
9110                                (assoc article (gnus-data-list nil)))
9111                               gnus-newsgroup-name nil
9112                               select-method)))
9113        (t
9114         (let* ((pto-group (gnus-group-prefixed-name
9115                            (car art-group) to-method))
9116                (info (gnus-get-info pto-group))
9117                (to-group (gnus-info-group info))
9118                to-marks)
9119           ;; Update the group that has been moved to.
9120           (when (and info
9121                      (memq action '(move copy)))
9122             (unless (member to-group to-groups)
9123               (push to-group to-groups))
9124
9125             (unless (memq article gnus-newsgroup-unreads)
9126               (push 'read to-marks)
9127               (gnus-info-set-read
9128                info (gnus-add-to-range (gnus-info-read info)
9129                                        (list (cdr art-group)))))
9130
9131             ;; See whether the article is to be put in the cache.
9132             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9133                              gnus-article-mark-lists
9134                            (delete '(expirable . expire)
9135                                    (copy-sequence gnus-article-mark-lists))))
9136                   (to-article (cdr art-group)))
9137
9138               ;; Enter the article into the cache in the new group,
9139               ;; if that is required.
9140               (when gnus-use-cache
9141                 (gnus-cache-possibly-enter-article
9142                  to-group to-article
9143                  (let ((header (copy-sequence
9144                                 (gnus-summary-article-header article))))
9145                    (mail-header-set-number header to-article)
9146                    header)
9147                  (memq article gnus-newsgroup-marked)
9148                  (memq article gnus-newsgroup-dormant)
9149                  (memq article gnus-newsgroup-unreads)))
9150
9151               (when gnus-preserve-marks
9152                 ;; Copy any marks over to the new group.
9153                 (when (and (equal to-group gnus-newsgroup-name)
9154                            (not (memq article gnus-newsgroup-unreads)))
9155                   ;; Mark this article as read in this group.
9156                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9157                   (setcdr (gnus-active to-group) to-article)
9158                   (setcdr gnus-newsgroup-active to-article))
9159
9160                 (while marks
9161                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9162                     (when (memq article (symbol-value
9163                                          (intern (format "gnus-newsgroup-%s"
9164                                                          (caar marks)))))
9165                       (push (cdar marks) to-marks)
9166                       ;; If the other group is the same as this group,
9167                       ;; then we have to add the mark to the list.
9168                       (when (equal to-group gnus-newsgroup-name)
9169                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9170                              (cons to-article
9171                                    (symbol-value
9172                                     (intern (format "gnus-newsgroup-%s"
9173                                                     (caar marks)))))))
9174                       ;; Copy the marks to other group.
9175                       (gnus-add-marked-articles
9176                        to-group (cdar marks) (list to-article) info)))
9177                   (setq marks (cdr marks)))
9178
9179                 (gnus-request-set-mark
9180                  to-group (list (list (list to-article) 'add to-marks))))
9181
9182               (gnus-dribble-enter
9183                (concat "(gnus-group-set-info '"
9184                        (gnus-prin1-to-string (gnus-get-info to-group))
9185                        ")"))))
9186
9187           ;; Update the Xref header in this article to point to
9188           ;; the new crossposted article we have just created.
9189           (when (eq action 'crosspost)
9190             (save-excursion
9191               (set-buffer copy-buf)
9192               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9193               (nnheader-replace-header "Xref" new-xref)
9194               (gnus-request-replace-article
9195                article gnus-newsgroup-name (current-buffer))))
9196
9197           ;; run the move/copy/crosspost/respool hook
9198           (run-hook-with-args 'gnus-summary-article-move-hook
9199                               action
9200                               (gnus-data-header
9201                                (assoc article (gnus-data-list nil)))
9202                               gnus-newsgroup-name
9203                               to-newsgroup
9204                               select-method))
9205
9206         ;;;!!!Why is this necessary?
9207         (set-buffer gnus-summary-buffer)
9208         
9209         (gnus-summary-goto-subject article)
9210         (when (eq action 'move)
9211           (gnus-summary-mark-article article gnus-canceled-mark))))
9212       (gnus-summary-remove-process-mark article))
9213     ;; Re-activate all groups that have been moved to.
9214     (save-excursion
9215       (set-buffer gnus-group-buffer)
9216       (let ((gnus-group-marked to-groups))
9217         (gnus-group-get-new-news-this-group nil t)))
9218
9219     (gnus-kill-buffer copy-buf)
9220     (gnus-summary-position-point)
9221     (gnus-set-mode-line 'summary)))
9222
9223 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9224   "Move the current article to a different newsgroup.
9225 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9226 When called interactively, if TO-NEWSGROUP is nil, use the value of
9227 the variable `gnus-move-split-methods' for finding a default target
9228 newsgroup.
9229 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9230 re-spool using this method."
9231   (interactive "P")
9232   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9233
9234 (defun gnus-summary-crosspost-article (&optional n)
9235   "Crosspost the current article to some other group."
9236   (interactive "P")
9237   (gnus-summary-move-article n nil nil 'crosspost))
9238
9239 (defcustom gnus-summary-respool-default-method nil
9240   "Default method type for respooling an article.
9241 If nil, use to the current newsgroup method."
9242   :type 'symbol
9243   :group 'gnus-summary-mail)
9244
9245 (defcustom gnus-summary-display-while-building nil
9246   "If non-nil, show and update the summary buffer as it's being built.
9247 If the value is t, update the buffer after every line is inserted.  If
9248 the value is an integer (N), update the display every N lines."
9249   :group 'gnus-thread
9250   :type '(choice (const :tag "off" nil)
9251                  number
9252                  (const :tag "frequently" t)))
9253
9254 (defun gnus-summary-respool-article (&optional n method)
9255   "Respool the current article.
9256 The article will be squeezed through the mail spooling process again,
9257 which means that it will be put in some mail newsgroup or other
9258 depending on `nnmail-split-methods'.
9259 If N is a positive number, respool the N next articles.
9260 If N is a negative number, respool the N previous articles.
9261 If N is nil and any articles have been marked with the process mark,
9262 respool those articles instead.
9263
9264 Respooling can be done both from mail groups and \"real\" newsgroups.
9265 In the former case, the articles in question will be moved from the
9266 current group into whatever groups they are destined to.  In the
9267 latter case, they will be copied into the relevant groups."
9268   (interactive
9269    (list current-prefix-arg
9270          (let* ((methods (gnus-methods-using 'respool))
9271                 (methname
9272                  (symbol-name (or gnus-summary-respool-default-method
9273                                   (car (gnus-find-method-for-group
9274                                         gnus-newsgroup-name)))))
9275                 (method
9276                  (gnus-completing-read-with-default
9277                   methname "What backend do you want to use when respooling?"
9278                   methods nil t nil 'gnus-mail-method-history))
9279                 ms)
9280            (cond
9281             ((zerop (length (setq ms (gnus-servers-using-backend
9282                                       (intern method)))))
9283              (list (intern method) ""))
9284             ((= 1 (length ms))
9285              (car ms))
9286             (t
9287              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9288                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9289                            ms-alist))))))))
9290   (unless method
9291     (error "No method given for respooling"))
9292   (if (assoc (symbol-name
9293               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9294              (gnus-methods-using 'respool))
9295       (gnus-summary-move-article n nil method)
9296     (gnus-summary-copy-article n nil method)))
9297
9298 (defun gnus-summary-import-article (file &optional edit)
9299   "Import an arbitrary file into a mail newsgroup."
9300   (interactive "fImport file: \nP")
9301   (let ((group gnus-newsgroup-name)
9302         (now (current-time))
9303         atts lines group-art)
9304     (unless (gnus-check-backend-function 'request-accept-article group)
9305       (error "%s does not support article importing" group))
9306     (or (file-readable-p file)
9307         (not (file-regular-p file))
9308         (error "Can't read %s" file))
9309     (save-excursion
9310       (set-buffer (gnus-get-buffer-create " *import file*"))
9311       (erase-buffer)
9312       (nnheader-insert-file-contents file)
9313       (goto-char (point-min))
9314       (if (nnheader-article-p)
9315           (save-restriction
9316             (goto-char (point-min))
9317             (search-forward "\n\n" nil t)
9318             (narrow-to-region (point-min) (1- (point)))
9319             (goto-char (point-min))
9320             (unless (re-search-forward "^date:" nil t)
9321               (goto-char (point-max))
9322               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9323         ;; This doesn't look like an article, so we fudge some headers.
9324         (setq atts (file-attributes file)
9325               lines (count-lines (point-min) (point-max)))
9326         (insert "From: " (read-string "From: ") "\n"
9327                 "Subject: " (read-string "Subject: ") "\n"
9328                 "Date: " (message-make-date (nth 5 atts)) "\n"
9329                 "Message-ID: " (message-make-message-id) "\n"
9330                 "Lines: " (int-to-string lines) "\n"
9331                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9332       (setq group-art (gnus-request-accept-article group nil t))
9333       (kill-buffer (current-buffer)))
9334     (setq gnus-newsgroup-active (gnus-activate-group group))
9335     (forward-line 1)
9336     (gnus-summary-goto-article (cdr group-art) nil t)
9337     (when edit
9338       (gnus-summary-edit-article))))
9339
9340 (defun gnus-summary-create-article ()
9341   "Create an article in a mail newsgroup."
9342   (interactive)
9343   (let ((group gnus-newsgroup-name)
9344         (now (current-time))
9345         group-art)
9346     (unless (gnus-check-backend-function 'request-accept-article group)
9347       (error "%s does not support article importing" group))
9348     (save-excursion
9349       (set-buffer (gnus-get-buffer-create " *import file*"))
9350       (erase-buffer)
9351       (goto-char (point-min))
9352       ;; This doesn't look like an article, so we fudge some headers.
9353       (insert "From: " (read-string "From: ") "\n"
9354               "Subject: " (read-string "Subject: ") "\n"
9355               "Date: " (message-make-date now) "\n"
9356               "Message-ID: " (message-make-message-id) "\n")
9357       (setq group-art (gnus-request-accept-article group nil t))
9358       (kill-buffer (current-buffer)))
9359     (setq gnus-newsgroup-active (gnus-activate-group group))
9360     (forward-line 1)
9361     (gnus-summary-goto-article (cdr group-art) nil t)
9362     (gnus-summary-edit-article)))
9363
9364 (defun gnus-summary-article-posted-p ()
9365   "Say whether the current (mail) article is available from news as well.
9366 This will be the case if the article has both been mailed and posted."
9367   (interactive)
9368   (let ((id (mail-header-references (gnus-summary-article-header)))
9369         (gnus-override-method (car (gnus-refer-article-methods))))
9370     (if (gnus-request-head id "")
9371         (gnus-message 2 "The current message was found on %s"
9372                       gnus-override-method)
9373       (gnus-message 2 "The current message couldn't be found on %s"
9374                     gnus-override-method)
9375       nil)))
9376
9377 (defun gnus-summary-expire-articles (&optional now)
9378   "Expire all articles that are marked as expirable in the current group."
9379   (interactive)
9380   (when (and (not gnus-group-is-exiting-without-update-p)
9381              (gnus-check-backend-function
9382               'request-expire-articles gnus-newsgroup-name))
9383     ;; This backend supports expiry.
9384     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9385            (expirable (if total
9386                           (progn
9387                             ;; We need to update the info for
9388                             ;; this group for `gnus-list-of-read-articles'
9389                             ;; to give us the right answer.
9390                             (gnus-run-hooks 'gnus-exit-group-hook)
9391                             (gnus-summary-update-info)
9392                             (gnus-list-of-read-articles gnus-newsgroup-name))
9393                         (setq gnus-newsgroup-expirable
9394                               (sort gnus-newsgroup-expirable '<))))
9395            (expiry-wait (if now 'immediate
9396                           (gnus-group-find-parameter
9397                            gnus-newsgroup-name 'expiry-wait)))
9398            (nnmail-expiry-target
9399             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9400                 nnmail-expiry-target))
9401            es)
9402       (when expirable
9403         ;; There are expirable articles in this group, so we run them
9404         ;; through the expiry process.
9405         (gnus-message 6 "Expiring articles...")
9406         (unless (gnus-check-group gnus-newsgroup-name)
9407           (error "Can't open server for %s" gnus-newsgroup-name))
9408         ;; The list of articles that weren't expired is returned.
9409         (save-excursion
9410           (if expiry-wait
9411               (let ((nnmail-expiry-wait-function nil)
9412                     (nnmail-expiry-wait expiry-wait))
9413                 (setq es (gnus-request-expire-articles
9414                           expirable gnus-newsgroup-name)))
9415             (setq es (gnus-request-expire-articles
9416                       expirable gnus-newsgroup-name)))
9417           (unless total
9418             (setq gnus-newsgroup-expirable es))
9419           ;; We go through the old list of expirable, and mark all
9420           ;; really expired articles as nonexistent.
9421           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9422             (let ((gnus-use-cache nil))
9423               (dolist (article expirable)
9424                 (when (and (not (memq article es))
9425                            (gnus-data-find article))
9426                   (gnus-summary-mark-article article gnus-canceled-mark)
9427                   (run-hook-with-args 'gnus-summary-article-expire-hook
9428                                       'delete
9429                                       (gnus-data-header
9430                                        (assoc article (gnus-data-list nil)))
9431                                       gnus-newsgroup-name
9432                                       nil
9433                                       nil))))))
9434         (gnus-message 6 "Expiring articles...done")))))
9435
9436 (defun gnus-summary-expire-articles-now ()
9437   "Expunge all expirable articles in the current group.
9438 This means that *all* articles that are marked as expirable will be
9439 deleted forever, right now."
9440   (interactive)
9441   (or gnus-expert-user
9442       (gnus-yes-or-no-p
9443        "Are you really, really, really sure you want to delete all these messages? ")
9444       (error "Phew!"))
9445   (gnus-summary-expire-articles t))
9446
9447 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9448 (defun gnus-summary-delete-article (&optional n)
9449   "Delete the N next (mail) articles.
9450 This command actually deletes articles.  This is not a marking
9451 command.  The article will disappear forever from your life, never to
9452 return.
9453
9454 If N is negative, delete backwards.
9455 If N is nil and articles have been marked with the process mark,
9456 delete these instead.
9457
9458 If `gnus-novice-user' is non-nil you will be asked for
9459 confirmation before the articles are deleted."
9460   (interactive "P")
9461   (unless (gnus-check-backend-function 'request-expire-articles
9462                                        gnus-newsgroup-name)
9463     (error "The current newsgroup does not support article deletion"))
9464   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9465     (error "Couldn't open server"))
9466   ;; Compute the list of articles to delete.
9467   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9468         (nnmail-expiry-target 'delete)
9469         not-deleted)
9470     (if (and gnus-novice-user
9471              (not (gnus-yes-or-no-p
9472                    (format "Do you really want to delete %s forever? "
9473                            (if (> (length articles) 1)
9474                                (format "these %s articles" (length articles))
9475                              "this article")))))
9476         ()
9477       ;; Delete the articles.
9478       (setq not-deleted (gnus-request-expire-articles
9479                          articles gnus-newsgroup-name 'force))
9480       (while articles
9481         (gnus-summary-remove-process-mark (car articles))
9482         ;; The backend might not have been able to delete the article
9483         ;; after all.
9484         (unless (memq (car articles) not-deleted)
9485           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9486         (let* ((article (car articles))
9487                (id (mail-header-id (gnus-data-header
9488                                     (assoc article (gnus-data-list nil))))))
9489           (run-hook-with-args 'gnus-summary-article-delete-hook
9490                               'delete id gnus-newsgroup-name nil
9491                               nil))
9492         (setq articles (cdr articles)))
9493       (when not-deleted
9494         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9495     (gnus-summary-position-point)
9496     (gnus-set-mode-line 'summary)
9497     not-deleted))
9498
9499 (defun gnus-summary-edit-article (&optional force)
9500   "Edit the current article.
9501 This will have permanent effect only in mail groups.
9502 If FORCE is non-nil, allow editing of articles even in read-only
9503 groups."
9504   (interactive "P")
9505   (save-excursion
9506     (set-buffer gnus-summary-buffer)
9507     (let ((mail-parse-charset gnus-newsgroup-charset)
9508           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9509       (gnus-set-global-variables)
9510       (when (and (not force)
9511                  (gnus-group-read-only-p))
9512         (error "The current newsgroup does not support article editing"))
9513       (gnus-summary-show-article t)
9514       (gnus-article-edit-article
9515        'ignore
9516        `(lambda (no-highlight)
9517           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9518                 (message-options message-options)
9519                 (message-options-set-recipient)
9520                 (mail-parse-ignored-charsets
9521                  ',gnus-newsgroup-ignored-charsets))
9522             (gnus-summary-edit-article-done
9523              ,(or (mail-header-references gnus-current-headers) "")
9524              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9525
9526 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9527
9528 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9529                                                  no-highlight)
9530   "Make edits to the current article permanent."
9531   (interactive)
9532   (save-excursion
9533     ;; The buffer restriction contains the entire article if it exists.
9534     (when (article-goto-body)
9535       (let ((lines (count-lines (point) (point-max)))
9536             (length (- (point-max) (point)))
9537             (case-fold-search t)
9538             (body (copy-marker (point))))
9539         (goto-char (point-min))
9540         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9541           (delete-region (match-beginning 1) (match-end 1))
9542           (insert (number-to-string length)))
9543         (goto-char (point-min))
9544         (when (re-search-forward
9545                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9546           (delete-region (match-beginning 1) (match-end 1))
9547           (insert (number-to-string length)))
9548         (goto-char (point-min))
9549         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9550           (delete-region (match-beginning 1) (match-end 1))
9551           (insert (number-to-string lines))))))
9552   ;; Replace the article.
9553   (let ((buf (current-buffer)))
9554     (with-temp-buffer
9555       (insert-buffer-substring buf)
9556
9557       (if (and (not read-only)
9558                (not (gnus-request-replace-article
9559                      (cdr gnus-article-current) (car gnus-article-current)
9560                      (current-buffer) t)))
9561           (error "Couldn't replace article")
9562         ;; Update the summary buffer.
9563         (if (and references
9564                  (equal (message-tokenize-header references " ")
9565                         (message-tokenize-header
9566                          (or (message-fetch-field "references") "") " ")))
9567             ;; We only have to update this line.
9568             (save-excursion
9569               (save-restriction
9570                 (message-narrow-to-head)
9571                 (let ((head (buffer-string))
9572                       header)
9573                   (with-temp-buffer
9574                     (insert (format "211 %d Article retrieved.\n"
9575                                     (cdr gnus-article-current)))
9576                     (insert head)
9577                     (insert ".\n")
9578                     (let ((nntp-server-buffer (current-buffer)))
9579                       (setq header (car (gnus-get-newsgroup-headers
9580                                          nil t))))
9581                     (save-excursion
9582                       (set-buffer gnus-summary-buffer)
9583                       (gnus-data-set-header
9584                        (gnus-data-find (cdr gnus-article-current))
9585                        header)
9586                       (gnus-summary-update-article-line
9587                        (cdr gnus-article-current) header)
9588                       (if (gnus-summary-goto-subject
9589                            (cdr gnus-article-current) nil t)
9590                           (gnus-summary-update-secondary-mark
9591                            (cdr gnus-article-current))))))))
9592           ;; Update threads.
9593           (set-buffer (or buffer gnus-summary-buffer))
9594           (gnus-summary-update-article (cdr gnus-article-current))
9595           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9596               (gnus-summary-update-secondary-mark
9597                (cdr gnus-article-current))))
9598         ;; Prettify the article buffer again.
9599         (unless no-highlight
9600           (save-excursion
9601             (set-buffer gnus-article-buffer)
9602             ;;;!!! Fix this -- article should be rehighlighted.
9603             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9604             (set-buffer gnus-original-article-buffer)
9605             (gnus-request-article
9606              (cdr gnus-article-current)
9607              (car gnus-article-current) (current-buffer))))
9608         ;; Prettify the summary buffer line.
9609         (when (gnus-visual-p 'summary-highlight 'highlight)
9610           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9611
9612 (defun gnus-summary-edit-wash (key)
9613   "Perform editing command KEY in the article buffer."
9614   (interactive
9615    (list
9616     (progn
9617       (message "%s" (concat (this-command-keys) "- "))
9618       (read-char))))
9619   (message "")
9620   (gnus-summary-edit-article)
9621   (execute-kbd-macro (concat (this-command-keys) key))
9622   (gnus-article-edit-done))
9623
9624 ;;; Respooling
9625
9626 (defun gnus-summary-respool-query (&optional silent trace)
9627   "Query where the respool algorithm would put this article."
9628   (interactive)
9629   (let (gnus-mark-article-hook)
9630     (gnus-summary-select-article)
9631     (save-excursion
9632       (set-buffer gnus-original-article-buffer)
9633       (let ((groups (nnmail-article-group 'identity trace)))
9634         (unless silent
9635           (if groups
9636               (message "This message would go to %s"
9637                        (mapconcat 'car groups ", "))
9638             (message "This message would go to no groups"))
9639           groups)))))
9640
9641 (defun gnus-summary-respool-trace ()
9642   "Trace where the respool algorithm would put this article.
9643 Display a buffer showing all fancy splitting patterns which matched."
9644   (interactive)
9645   (gnus-summary-respool-query nil t))
9646
9647 ;; Summary marking commands.
9648
9649 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9650   "Mark articles which has the same subject as read, and then select the next.
9651 If UNMARK is positive, remove any kind of mark.
9652 If UNMARK is negative, tick articles."
9653   (interactive "P")
9654   (when unmark
9655     (setq unmark (prefix-numeric-value unmark)))
9656   (let ((count
9657          (gnus-summary-mark-same-subject
9658           (gnus-summary-article-subject) unmark)))
9659     ;; Select next unread article.  If auto-select-same mode, should
9660     ;; select the first unread article.
9661     (gnus-summary-next-article t (and gnus-auto-select-same
9662                                       (gnus-summary-article-subject)))
9663     (gnus-message 7 "%d article%s marked as %s"
9664                   count (if (= count 1) " is" "s are")
9665                   (if unmark "unread" "read"))))
9666
9667 (defun gnus-summary-kill-same-subject (&optional unmark)
9668   "Mark articles which has the same subject as read.
9669 If UNMARK is positive, remove any kind of mark.
9670 If UNMARK is negative, tick articles."
9671   (interactive "P")
9672   (when unmark
9673     (setq unmark (prefix-numeric-value unmark)))
9674   (let ((count
9675          (gnus-summary-mark-same-subject
9676           (gnus-summary-article-subject) unmark)))
9677     ;; If marked as read, go to next unread subject.
9678     (when (null unmark)
9679       ;; Go to next unread subject.
9680       (gnus-summary-next-subject 1 t))
9681     (gnus-message 7 "%d articles are marked as %s"
9682                   count (if unmark "unread" "read"))))
9683
9684 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9685   "Mark articles with same SUBJECT as read, and return marked number.
9686 If optional argument UNMARK is positive, remove any kinds of marks.
9687 If optional argument UNMARK is negative, mark articles as unread instead."
9688   (let ((count 1))
9689     (save-excursion
9690       (cond
9691        ((null unmark)                   ; Mark as read.
9692         (while (and
9693                 (progn
9694                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9695                   (gnus-summary-show-thread) t)
9696                 (gnus-summary-find-subject subject))
9697           (setq count (1+ count))))
9698        ((> unmark 0)                    ; Tick.
9699         (while (and
9700                 (progn
9701                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9702                   (gnus-summary-show-thread) t)
9703                 (gnus-summary-find-subject subject))
9704           (setq count (1+ count))))
9705        (t                               ; Mark as unread.
9706         (while (and
9707                 (progn
9708                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9709                   (gnus-summary-show-thread) t)
9710                 (gnus-summary-find-subject subject))
9711           (setq count (1+ count)))))
9712       (gnus-set-mode-line 'summary)
9713       ;; Return the number of marked articles.
9714       count)))
9715
9716 (defun gnus-summary-mark-as-processable (n &optional unmark)
9717   "Set the process mark on the next N articles.
9718 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9719 the process mark instead.  The difference between N and the actual
9720 number of articles marked is returned."
9721   (interactive "P")
9722   (if (and (null n) (gnus-region-active-p))
9723       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9724     (setq n (prefix-numeric-value n))
9725     (let ((backward (< n 0))
9726           (n (abs n)))
9727       (while (and
9728               (> n 0)
9729               (if unmark
9730                   (gnus-summary-remove-process-mark
9731                    (gnus-summary-article-number))
9732                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9733               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9734         (setq n (1- n)))
9735       (when (/= 0 n)
9736         (gnus-message 7 "No more articles"))
9737       (gnus-summary-recenter)
9738       (gnus-summary-position-point)
9739       n)))
9740
9741 (defun gnus-summary-unmark-as-processable (n)
9742   "Remove the process mark from the next N articles.
9743 If N is negative, unmark backward instead.  The difference between N and
9744 the actual number of articles unmarked is returned."
9745   (interactive "P")
9746   (gnus-summary-mark-as-processable n t))
9747
9748 (defun gnus-summary-unmark-all-processable ()
9749   "Remove the process mark from all articles."
9750   (interactive)
9751   (save-excursion
9752     (while gnus-newsgroup-processable
9753       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9754   (gnus-summary-position-point))
9755
9756 (defun gnus-summary-add-mark (article type)
9757   "Mark ARTICLE with a mark of TYPE."
9758   (let ((vtype (car (assq type gnus-article-mark-lists)))
9759         var)
9760     (if (not vtype)
9761         (error "No such mark type: %s" type)
9762       (setq var (intern (format "gnus-newsgroup-%s" type)))
9763       (set var (cons article (symbol-value var)))
9764       (if (memq type '(processable cached replied forwarded recent saved))
9765           (gnus-summary-update-secondary-mark article)
9766         ;;; !!! This is bogus.  We should find out what primary
9767         ;;; !!! mark we want to set.
9768         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9769
9770 (defun gnus-summary-mark-as-expirable (n)
9771   "Mark N articles forward as expirable.
9772 If N is negative, mark backward instead.  The difference between N and
9773 the actual number of articles marked is returned."
9774   (interactive "p")
9775   (gnus-summary-mark-forward n gnus-expirable-mark))
9776
9777 (defun gnus-summary-mark-as-spam (n)
9778   "Mark N articles forward as spam.
9779 If N is negative, mark backward instead.  The difference between N and
9780 the actual number of articles marked is returned."
9781   (interactive "p")
9782   (gnus-summary-mark-forward n gnus-spam-mark))
9783
9784 (defun gnus-summary-mark-article-as-replied (article)
9785   "Mark ARTICLE as replied to and update the summary line.
9786 ARTICLE can also be a list of articles."
9787   (interactive (list (gnus-summary-article-number)))
9788   (let ((articles (if (listp article) article (list article))))
9789     (dolist (article articles)
9790       (unless (numberp article)
9791         (error "%s is not a number" article))
9792       (push article gnus-newsgroup-replied)
9793       (let ((buffer-read-only nil))
9794         (when (gnus-summary-goto-subject article nil t)
9795           (gnus-summary-update-secondary-mark article))))))
9796
9797 (defun gnus-summary-mark-article-as-forwarded (article)
9798   "Mark ARTICLE as forwarded and update the summary line.
9799 ARTICLE can also be a list of articles."
9800   (let ((articles (if (listp article) article (list article))))
9801     (dolist (article articles)
9802       (push article gnus-newsgroup-forwarded)
9803       (let ((buffer-read-only nil))
9804         (when (gnus-summary-goto-subject article nil t)
9805           (gnus-summary-update-secondary-mark article))))))
9806
9807 (defun gnus-summary-set-bookmark (article)
9808   "Set a bookmark in current article."
9809   (interactive (list (gnus-summary-article-number)))
9810   (when (or (not (get-buffer gnus-article-buffer))
9811             (not gnus-current-article)
9812             (not gnus-article-current)
9813             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9814     (error "No current article selected"))
9815   ;; Remove old bookmark, if one exists.
9816   (gnus-pull article gnus-newsgroup-bookmarks)
9817   ;; Set the new bookmark, which is on the form
9818   ;; (article-number . line-number-in-body).
9819   (push
9820    (cons article
9821          (save-excursion
9822            (set-buffer gnus-article-buffer)
9823            (count-lines
9824             (min (point)
9825                  (save-excursion
9826                    (article-goto-body)
9827                    (point)))
9828             (point))))
9829    gnus-newsgroup-bookmarks)
9830   (gnus-message 6 "A bookmark has been added to the current article."))
9831
9832 (defun gnus-summary-remove-bookmark (article)
9833   "Remove the bookmark from the current article."
9834   (interactive (list (gnus-summary-article-number)))
9835   ;; Remove old bookmark, if one exists.
9836   (if (not (assq article gnus-newsgroup-bookmarks))
9837       (gnus-message 6 "No bookmark in current article.")
9838     (gnus-pull article gnus-newsgroup-bookmarks)
9839     (gnus-message 6 "Removed bookmark.")))
9840
9841 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9842 (defun gnus-summary-mark-as-dormant (n)
9843   "Mark N articles forward as dormant.
9844 If N is negative, mark backward instead.  The difference between N and
9845 the actual number of articles marked is returned."
9846   (interactive "p")
9847   (gnus-summary-mark-forward n gnus-dormant-mark))
9848
9849 (defun gnus-summary-set-process-mark (article)
9850   "Set the process mark on ARTICLE and update the summary line."
9851   (setq gnus-newsgroup-processable
9852         (cons article
9853               (delq article gnus-newsgroup-processable)))
9854   (when (gnus-summary-goto-subject article)
9855     (gnus-summary-show-thread)
9856     (gnus-summary-goto-subject article)
9857     (gnus-summary-update-secondary-mark article)))
9858
9859 (defun gnus-summary-remove-process-mark (article)
9860   "Remove the process mark from ARTICLE and update the summary line."
9861   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9862   (when (gnus-summary-goto-subject article)
9863     (gnus-summary-show-thread)
9864     (gnus-summary-goto-subject article)
9865     (gnus-summary-update-secondary-mark article)))
9866
9867 (defun gnus-summary-set-saved-mark (article)
9868   "Set the process mark on ARTICLE and update the summary line."
9869   (push article gnus-newsgroup-saved)
9870   (when (gnus-summary-goto-subject article)
9871     (gnus-summary-update-secondary-mark article)))
9872
9873 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9874   "Mark N articles as read forwards.
9875 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9876 The difference between N and the actual number of articles marked is
9877 returned.
9878 If NO-EXPIRE, auto-expiry will be inhibited."
9879   (interactive "p")
9880   (gnus-summary-show-thread)
9881   (let ((backward (< n 0))
9882         (gnus-summary-goto-unread
9883          (and gnus-summary-goto-unread
9884               (not (eq gnus-summary-goto-unread 'never))
9885               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9886                                     gnus-ticked-mark gnus-dormant-mark)))))
9887         (n (abs n))
9888         (mark (or mark gnus-del-mark)))
9889     (while (and (> n 0)
9890                 (gnus-summary-mark-article nil mark no-expire)
9891                 (zerop (gnus-summary-next-subject
9892                         (if backward -1 1)
9893                         (and gnus-summary-goto-unread
9894                              (not (eq gnus-summary-goto-unread 'never)))
9895                         t)))
9896       (setq n (1- n)))
9897     (when (/= 0 n)
9898       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9899     (gnus-summary-recenter)
9900     (gnus-summary-position-point)
9901     (gnus-set-mode-line 'summary)
9902     n))
9903
9904 (defun gnus-summary-mark-article-as-read (mark)
9905   "Mark the current article quickly as read with MARK."
9906   (let ((article (gnus-summary-article-number)))
9907     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9908     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9909     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9910     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9911     (push (cons article mark) gnus-newsgroup-reads)
9912     ;; Possibly remove from cache, if that is used.
9913     (when gnus-use-cache
9914       (gnus-cache-enter-remove-article article))
9915     ;; Allow the backend to change the mark.
9916     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9917     ;; Check for auto-expiry.
9918     (when (and gnus-newsgroup-auto-expire
9919                (memq mark gnus-auto-expirable-marks))
9920       (setq mark gnus-expirable-mark)
9921       ;; Let the backend know about the mark change.
9922       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9923       (push article gnus-newsgroup-expirable))
9924     ;; Set the mark in the buffer.
9925     (gnus-summary-update-mark mark 'unread)
9926     t))
9927
9928 (defun gnus-summary-mark-article-as-unread (mark)
9929   "Mark the current article quickly as unread with MARK."
9930   (let* ((article (gnus-summary-article-number))
9931          (old-mark (gnus-summary-article-mark article)))
9932     ;; Allow the backend to change the mark.
9933     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9934     (if (eq mark old-mark)
9935         t
9936       (if (<= article 0)
9937           (progn
9938             (gnus-error 1 "Can't mark negative article numbers")
9939             nil)
9940         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9941         (setq gnus-newsgroup-spam-marked
9942               (delq article gnus-newsgroup-spam-marked))
9943         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9944         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9945         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9946         (cond ((= mark gnus-ticked-mark)
9947                (setq gnus-newsgroup-marked
9948                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9949                                               article)))
9950               ((= mark gnus-spam-mark)
9951                (setq gnus-newsgroup-spam-marked
9952                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9953                                               article)))
9954               ((= mark gnus-dormant-mark)
9955                (setq gnus-newsgroup-dormant
9956                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9957                                               article)))
9958               (t
9959                (setq gnus-newsgroup-unreads
9960                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9961                                               article))))
9962         (gnus-pull article gnus-newsgroup-reads)
9963
9964         ;; See whether the article is to be put in the cache.
9965         (and gnus-use-cache
9966              (vectorp (gnus-summary-article-header article))
9967              (save-excursion
9968                (gnus-cache-possibly-enter-article
9969                 gnus-newsgroup-name article
9970                 (gnus-summary-article-header article)
9971                 (= mark gnus-ticked-mark)
9972                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9973
9974         ;; Fix the mark.
9975         (gnus-summary-update-mark mark 'unread)
9976         t))))
9977
9978 (defun gnus-summary-mark-article (&optional article mark no-expire)
9979   "Mark ARTICLE with MARK.  MARK can be any character.
9980 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9981 `??' (dormant) and `?E' (expirable).
9982 If MARK is nil, then the default character `?r' is used.
9983 If ARTICLE is nil, then the article on the current line will be
9984 marked.
9985 If NO-EXPIRE, auto-expiry will be inhibited."
9986   ;; The mark might be a string.
9987   (when (stringp mark)
9988     (setq mark (aref mark 0)))
9989   ;; If no mark is given, then we check auto-expiring.
9990   (when (null mark)
9991     (setq mark gnus-del-mark))
9992   (when (and (not no-expire)
9993              gnus-newsgroup-auto-expire
9994              (memq mark gnus-auto-expirable-marks))
9995     (setq mark gnus-expirable-mark))
9996   (let ((article (or article (gnus-summary-article-number)))
9997         (old-mark (gnus-summary-article-mark article)))
9998     ;; Allow the backend to change the mark.
9999     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10000     (if (eq mark old-mark)
10001         t
10002       (unless article
10003         (error "No article on current line"))
10004       (if (not (if (or (= mark gnus-unread-mark)
10005                        (= mark gnus-ticked-mark)
10006                        (= mark gnus-spam-mark)
10007                        (= mark gnus-dormant-mark))
10008                    (gnus-mark-article-as-unread article mark)
10009                  (gnus-mark-article-as-read article mark)))
10010           t
10011         ;; See whether the article is to be put in the cache.
10012         (and gnus-use-cache
10013              (not (= mark gnus-canceled-mark))
10014              (vectorp (gnus-summary-article-header article))
10015              (save-excursion
10016                (gnus-cache-possibly-enter-article
10017                 gnus-newsgroup-name article
10018                 (gnus-summary-article-header article)
10019                 (= mark gnus-ticked-mark)
10020                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10021
10022         (when (gnus-summary-goto-subject article nil t)
10023           (let ((buffer-read-only nil))
10024             (gnus-summary-show-thread)
10025             ;; Fix the mark.
10026             (gnus-summary-update-mark mark 'unread)
10027             t))))))
10028
10029 (defun gnus-summary-update-secondary-mark (article)
10030   "Update the secondary (read, process, cache) mark."
10031   (gnus-summary-update-mark
10032    (cond ((memq article gnus-newsgroup-processable)
10033           gnus-process-mark)
10034          ((memq article gnus-newsgroup-cached)
10035           gnus-cached-mark)
10036          ((memq article gnus-newsgroup-replied)
10037           gnus-replied-mark)
10038          ((memq article gnus-newsgroup-forwarded)
10039           gnus-forwarded-mark)
10040          ((memq article gnus-newsgroup-saved)
10041           gnus-saved-mark)
10042          ((memq article gnus-newsgroup-recent)
10043           gnus-recent-mark)
10044          ((memq article gnus-newsgroup-unseen)
10045           gnus-unseen-mark)
10046          (t gnus-no-mark))
10047    'replied)
10048   (when (gnus-visual-p 'summary-highlight 'highlight)
10049     (gnus-run-hooks 'gnus-summary-update-hook))
10050   t)
10051
10052 (defun gnus-summary-update-download-mark (article)
10053   "Update the download mark."
10054   (gnus-summary-update-mark
10055    (cond ((memq article gnus-newsgroup-undownloaded)
10056           gnus-undownloaded-mark)
10057          (gnus-newsgroup-agentized
10058           gnus-downloaded-mark)
10059          (t
10060           gnus-no-mark))
10061    'download)
10062   (gnus-summary-update-line t)
10063   t)
10064
10065 (defun gnus-summary-update-mark (mark type)
10066   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10067         (buffer-read-only nil))
10068     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10069     (when forward
10070       (when (looking-at "\r")
10071         (incf forward))
10072       (when (<= (+ forward (point)) (point-max))
10073         ;; Go to the right position on the line.
10074         (goto-char (+ forward (point)))
10075         ;; Replace the old mark with the new mark.
10076         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10077         ;; Optionally update the marks by some user rule.
10078         (when (eq type 'unread)
10079           (gnus-data-set-mark
10080            (gnus-data-find (gnus-summary-article-number)) mark)
10081           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10082
10083 (defun gnus-mark-article-as-read (article &optional mark)
10084   "Enter ARTICLE in the pertinent lists and remove it from others."
10085   ;; Make the article expirable.
10086   (let ((mark (or mark gnus-del-mark)))
10087     (setq gnus-newsgroup-expirable
10088           (if (= mark gnus-expirable-mark)
10089               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10090             (delq article gnus-newsgroup-expirable)))
10091     ;; Remove from unread and marked lists.
10092     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10093     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10094     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10095     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10096     (push (cons article mark) gnus-newsgroup-reads)
10097     ;; Possibly remove from cache, if that is used.
10098     (when gnus-use-cache
10099       (gnus-cache-enter-remove-article article))
10100     t))
10101
10102 (defun gnus-mark-article-as-unread (article &optional mark)
10103   "Enter ARTICLE in the pertinent lists and remove it from others."
10104   (let ((mark (or mark gnus-ticked-mark)))
10105     (if (<= article 0)
10106         (progn
10107           (gnus-error 1 "Can't mark negative article numbers")
10108           nil)
10109       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10110             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10111             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10112             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10113             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10114
10115       ;; Unsuppress duplicates?
10116       (when gnus-suppress-duplicates
10117         (gnus-dup-unsuppress-article article))
10118
10119       (cond ((= mark gnus-ticked-mark)
10120              (setq gnus-newsgroup-marked
10121                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10122             ((= mark gnus-spam-mark)
10123              (setq gnus-newsgroup-spam-marked
10124                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10125                                             article)))
10126             ((= mark gnus-dormant-mark)
10127              (setq gnus-newsgroup-dormant
10128                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10129             (t
10130              (setq gnus-newsgroup-unreads
10131                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10132       (gnus-pull article gnus-newsgroup-reads)
10133       t)))
10134
10135 (defalias 'gnus-summary-mark-as-unread-forward
10136   'gnus-summary-tick-article-forward)
10137 (make-obsolete 'gnus-summary-mark-as-unread-forward
10138                'gnus-summary-tick-article-forward)
10139 (defun gnus-summary-tick-article-forward (n)
10140   "Tick N articles forwards.
10141 If N is negative, tick backwards instead.
10142 The difference between N and the number of articles ticked is returned."
10143   (interactive "p")
10144   (gnus-summary-mark-forward n gnus-ticked-mark))
10145
10146 (defalias 'gnus-summary-mark-as-unread-backward
10147   'gnus-summary-tick-article-backward)
10148 (make-obsolete 'gnus-summary-mark-as-unread-backward
10149                'gnus-summary-tick-article-backward)
10150 (defun gnus-summary-tick-article-backward (n)
10151   "Tick N articles backwards.
10152 The difference between N and the number of articles ticked is returned."
10153   (interactive "p")
10154   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10155
10156 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10157 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10158 (defun gnus-summary-tick-article (&optional article clear-mark)
10159   "Mark current article as unread.
10160 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10161 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10162   (interactive)
10163   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10164                                        gnus-ticked-mark)))
10165
10166 (defun gnus-summary-mark-as-read-forward (n)
10167   "Mark N articles as read forwards.
10168 If N is negative, mark backwards instead.
10169 The difference between N and the actual number of articles marked is
10170 returned."
10171   (interactive "p")
10172   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10173
10174 (defun gnus-summary-mark-as-read-backward (n)
10175   "Mark the N articles as read backwards.
10176 The difference between N and the actual number of articles marked is
10177 returned."
10178   (interactive "p")
10179   (gnus-summary-mark-forward
10180    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10181
10182 (defun gnus-summary-mark-as-read (&optional article mark)
10183   "Mark current article as read.
10184 ARTICLE specifies the article to be marked as read.
10185 MARK specifies a string to be inserted at the beginning of the line."
10186   (gnus-summary-mark-article article mark))
10187
10188 (defun gnus-summary-clear-mark-forward (n)
10189   "Clear marks from N articles forward.
10190 If N is negative, clear backward instead.
10191 The difference between N and the number of marks cleared is returned."
10192   (interactive "p")
10193   (gnus-summary-mark-forward n gnus-unread-mark))
10194
10195 (defun gnus-summary-clear-mark-backward (n)
10196   "Clear marks from N articles backward.
10197 The difference between N and the number of marks cleared is returned."
10198   (interactive "p")
10199   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10200
10201 (defun gnus-summary-mark-unread-as-read ()
10202   "Intended to be used by `gnus-summary-mark-article-hook'."
10203   (when (memq gnus-current-article gnus-newsgroup-unreads)
10204     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10205
10206 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10207   "Intended to be used by `gnus-summary-mark-article-hook'."
10208   (let ((mark (gnus-summary-article-mark)))
10209     (when (or (gnus-unread-mark-p mark)
10210               (gnus-read-mark-p mark))
10211       (gnus-summary-mark-article gnus-current-article
10212                                  (or new-mark gnus-read-mark)))))
10213
10214 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10215   "Intended to be used by `gnus-summary-mark-article-hook'."
10216   (let ((mark (gnus-summary-article-mark)))
10217     (when (or (gnus-unread-mark-p mark)
10218               (gnus-read-mark-p mark))
10219       (gnus-summary-mark-article (gnus-summary-article-number)
10220                                  (or new-mark gnus-read-mark)))))
10221
10222 (defun gnus-summary-mark-unread-as-ticked ()
10223   "Intended to be used by `gnus-summary-mark-article-hook'."
10224   (when (memq gnus-current-article gnus-newsgroup-unreads)
10225     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10226
10227 (defun gnus-summary-mark-region-as-read (point mark all)
10228   "Mark all unread articles between point and mark as read.
10229 If given a prefix, mark all articles between point and mark as read,
10230 even ticked and dormant ones."
10231   (interactive "r\nP")
10232   (save-excursion
10233     (let (article)
10234       (goto-char point)
10235       (beginning-of-line)
10236       (while (and
10237               (< (point) mark)
10238               (progn
10239                 (when (or all
10240                           (memq (setq article (gnus-summary-article-number))
10241                                 gnus-newsgroup-unreads))
10242                   (gnus-summary-mark-article article gnus-del-mark))
10243                 t)
10244               (gnus-summary-find-next))))))
10245
10246 (defun gnus-summary-mark-below (score mark)
10247   "Mark articles with score less than SCORE with MARK."
10248   (interactive "P\ncMark: ")
10249   (setq score (if score
10250                   (prefix-numeric-value score)
10251                 (or gnus-summary-default-score 0)))
10252   (save-excursion
10253     (set-buffer gnus-summary-buffer)
10254     (goto-char (point-min))
10255     (while
10256         (progn
10257           (and (< (gnus-summary-article-score) score)
10258                (gnus-summary-mark-article nil mark))
10259           (gnus-summary-find-next)))))
10260
10261 (defun gnus-summary-kill-below (&optional score)
10262   "Mark articles with score below SCORE as read."
10263   (interactive "P")
10264   (gnus-summary-mark-below score gnus-killed-mark))
10265
10266 (defun gnus-summary-clear-above (&optional score)
10267   "Clear all marks from articles with score above SCORE."
10268   (interactive "P")
10269   (gnus-summary-mark-above score gnus-unread-mark))
10270
10271 (defun gnus-summary-tick-above (&optional score)
10272   "Tick all articles with score above SCORE."
10273   (interactive "P")
10274   (gnus-summary-mark-above score gnus-ticked-mark))
10275
10276 (defun gnus-summary-mark-above (score mark)
10277   "Mark articles with score over SCORE with MARK."
10278   (interactive "P\ncMark: ")
10279   (setq score (if score
10280                   (prefix-numeric-value score)
10281                 (or gnus-summary-default-score 0)))
10282   (save-excursion
10283     (set-buffer gnus-summary-buffer)
10284     (goto-char (point-min))
10285     (while (and (progn
10286                   (when (> (gnus-summary-article-score) score)
10287                     (gnus-summary-mark-article nil mark))
10288                   t)
10289                 (gnus-summary-find-next)))))
10290
10291 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10292 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10293 (defun gnus-summary-limit-include-expunged (&optional no-error)
10294   "Display all the hidden articles that were expunged for low scores."
10295   (interactive)
10296   (let ((buffer-read-only nil))
10297     (let ((scored gnus-newsgroup-scored)
10298           headers h)
10299       (while scored
10300         (unless (gnus-summary-article-header (caar scored))
10301           (and (setq h (gnus-number-to-header (caar scored)))
10302                (< (cdar scored) gnus-summary-expunge-below)
10303                (push h headers)))
10304         (setq scored (cdr scored)))
10305       (if (not headers)
10306           (when (not no-error)
10307             (error "No expunged articles hidden"))
10308         (goto-char (point-min))
10309         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10310         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10311         (mapcar (lambda (x) (push (mail-header-number x)
10312                                   gnus-newsgroup-limit))
10313                 headers)
10314         (gnus-summary-prepare-unthreaded (nreverse headers))
10315         (goto-char (point-min))
10316         (gnus-summary-position-point)
10317         t))))
10318
10319 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10320   "Mark all unread articles in this newsgroup as read.
10321 If prefix argument ALL is non-nil, ticked and dormant articles will
10322 also be marked as read.
10323 If QUIETLY is non-nil, no questions will be asked.
10324
10325 If TO-HERE is non-nil, it should be a point in the buffer.  All
10326 articles before (after, if REVERSE is set) this point will be marked
10327 as read.
10328
10329 Note that this function will only catch up the unread article
10330 in the current summary buffer limitation.
10331
10332 The number of articles marked as read is returned."
10333   (interactive "P")
10334   (prog1
10335       (save-excursion
10336         (when (or quietly
10337                   (not gnus-interactive-catchup) ;Without confirmation?
10338                   gnus-expert-user
10339                   (gnus-y-or-n-p
10340                    (if all
10341                        "Mark absolutely all articles as read? "
10342                      "Mark all unread articles as read? ")))
10343           (if (and not-mark
10344                    (not gnus-newsgroup-adaptive)
10345                    (not gnus-newsgroup-auto-expire)
10346                    (not gnus-suppress-duplicates)
10347                    (or (not gnus-use-cache)
10348                        (eq gnus-use-cache 'passive)))
10349               (progn
10350                 (when all
10351                   (setq gnus-newsgroup-marked nil
10352                         gnus-newsgroup-spam-marked nil
10353                         gnus-newsgroup-dormant nil))
10354                 (setq gnus-newsgroup-unreads
10355                       (gnus-sorted-nunion
10356                        (gnus-intersection gnus-newsgroup-unreads
10357                                           gnus-newsgroup-downloadable)
10358                        gnus-newsgroup-unfetched)))
10359             ;; We actually mark all articles as canceled, which we
10360             ;; have to do when using auto-expiry or adaptive scoring.
10361             (gnus-summary-show-all-threads)
10362             (if (and to-here reverse)
10363                 (progn
10364                   (goto-char to-here)
10365                   (gnus-summary-mark-current-read-and-unread-as-read
10366                    gnus-catchup-mark)
10367                   (while (gnus-summary-find-next (not all))
10368                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10369               (when (gnus-summary-first-subject (not all))
10370                 (while (and
10371                         (if to-here (< (point) to-here) t)
10372                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10373                         (gnus-summary-find-next (not all))))))
10374             (gnus-set-mode-line 'summary))
10375           t))
10376     (gnus-summary-position-point)))
10377
10378 (defun gnus-summary-catchup-to-here (&optional all)
10379   "Mark all unticked articles before the current one as read.
10380 If ALL is non-nil, also mark ticked and dormant articles as read."
10381   (interactive "P")
10382   (save-excursion
10383     (gnus-save-hidden-threads
10384       (let ((beg (point)))
10385         ;; We check that there are unread articles.
10386         (when (or all (gnus-summary-find-prev))
10387           (gnus-summary-catchup all t beg)))))
10388   (gnus-summary-position-point))
10389
10390 (defun gnus-summary-catchup-from-here (&optional all)
10391   "Mark all unticked articles after (and including) the current one as read.
10392 If ALL is non-nil, also mark ticked and dormant articles as read."
10393   (interactive "P")
10394   (save-excursion
10395     (gnus-save-hidden-threads
10396       (let ((beg (point)))
10397         ;; We check that there are unread articles.
10398         (when (or all (gnus-summary-find-next))
10399           (gnus-summary-catchup all t beg nil t)))))
10400   (gnus-summary-position-point))
10401
10402 (defun gnus-summary-catchup-all (&optional quietly)
10403   "Mark all articles in this newsgroup as read.
10404 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10405 instead, which marks only unread articles as read."
10406   (interactive "P")
10407   (gnus-summary-catchup t quietly))
10408
10409 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10410   "Mark all unread articles in this group as read, then exit.
10411 If prefix argument ALL is non-nil, all articles are marked as read.
10412 If QUIETLY is non-nil, no questions will be asked."
10413   (interactive "P")
10414   (when (gnus-summary-catchup all quietly nil 'fast)
10415     ;; Select next newsgroup or exit.
10416     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10417              (eq gnus-auto-select-next 'quietly))
10418         (gnus-summary-next-group nil)
10419       (gnus-summary-exit))))
10420
10421 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10422   "Mark all articles in this newsgroup as read, and then exit.
10423 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10424 instead, which marks only unread articles as read."
10425   (interactive "P")
10426   (gnus-summary-catchup-and-exit t quietly))
10427
10428 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10429   "Mark all articles in this group as read and select the next group.
10430 If given a prefix, mark all articles, unread as well as ticked, as
10431 read."
10432   (interactive "P")
10433   (save-excursion
10434     (gnus-summary-catchup all))
10435   (gnus-summary-next-group))
10436
10437 ;;;
10438 ;;; with article
10439 ;;;
10440
10441 (defmacro gnus-with-article (article &rest forms)
10442   "Select ARTICLE and perform FORMS in the original article buffer.
10443 Then replace the article with the result."
10444   `(progn
10445      ;; We don't want the article to be marked as read.
10446      (let (gnus-mark-article-hook)
10447        (gnus-summary-select-article t t nil ,article))
10448      (set-buffer gnus-original-article-buffer)
10449      ,@forms
10450      (if (not (gnus-check-backend-function
10451                'request-replace-article (car gnus-article-current)))
10452          (gnus-message 5 "Read-only group; not replacing")
10453        (unless (gnus-request-replace-article
10454                 ,article (car gnus-article-current)
10455                 (current-buffer) t)
10456          (error "Couldn't replace article")))
10457      ;; The cache and backlog have to be flushed somewhat.
10458      (when gnus-keep-backlog
10459        (gnus-backlog-remove-article
10460         (car gnus-article-current) (cdr gnus-article-current)))
10461      (when gnus-use-cache
10462        (gnus-cache-update-article
10463         (car gnus-article-current) (cdr gnus-article-current)))))
10464
10465 (put 'gnus-with-article 'lisp-indent-function 1)
10466 (put 'gnus-with-article 'edebug-form-spec '(form body))
10467
10468 ;; Thread-based commands.
10469
10470 (defun gnus-summary-articles-in-thread (&optional article)
10471   "Return a list of all articles in the current thread.
10472 If ARTICLE is non-nil, return all articles in the thread that starts
10473 with that article."
10474   (let* ((article (or article (gnus-summary-article-number)))
10475          (data (gnus-data-find-list article))
10476          (top-level (gnus-data-level (car data)))
10477          (top-subject
10478           (cond ((null gnus-thread-operation-ignore-subject)
10479                  (gnus-simplify-subject-re
10480                   (mail-header-subject (gnus-data-header (car data)))))
10481                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10482                  (gnus-simplify-subject-fuzzy
10483                   (mail-header-subject (gnus-data-header (car data)))))
10484                 (t nil)))
10485          (end-point (save-excursion
10486                       (if (gnus-summary-go-to-next-thread)
10487                           (point) (point-max))))
10488          articles)
10489     (while (and data
10490                 (< (gnus-data-pos (car data)) end-point))
10491       (when (or (not top-subject)
10492                 (string= top-subject
10493                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10494                              (gnus-simplify-subject-fuzzy
10495                               (mail-header-subject
10496                                (gnus-data-header (car data))))
10497                            (gnus-simplify-subject-re
10498                             (mail-header-subject
10499                              (gnus-data-header (car data)))))))
10500         (push (gnus-data-number (car data)) articles))
10501       (unless (and (setq data (cdr data))
10502                    (> (gnus-data-level (car data)) top-level))
10503         (setq data nil)))
10504     ;; Return the list of articles.
10505     (nreverse articles)))
10506
10507 (defun gnus-summary-rethread-current ()
10508   "Rethread the thread the current article is part of."
10509   (interactive)
10510   (let* ((gnus-show-threads t)
10511          (article (gnus-summary-article-number))
10512          (id (mail-header-id (gnus-summary-article-header)))
10513          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10514     (unless id
10515       (error "No article on the current line"))
10516     (gnus-rebuild-thread id)
10517     (gnus-summary-goto-subject article)))
10518
10519 (defun gnus-summary-reparent-thread ()
10520   "Make the current article child of the marked (or previous) article.
10521
10522 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10523 is non-nil or the Subject: of both articles are the same."
10524   (interactive)
10525   (unless (not (gnus-group-read-only-p))
10526     (error "The current newsgroup does not support article editing"))
10527   (unless (<= (length gnus-newsgroup-processable) 1)
10528     (error "No more than one article may be marked"))
10529   (save-window-excursion
10530     (let ((gnus-article-buffer " *reparent*")
10531           (current-article (gnus-summary-article-number))
10532           ;; First grab the marked article, otherwise one line up.
10533           (parent-article (if (not (null gnus-newsgroup-processable))
10534                               (car gnus-newsgroup-processable)
10535                             (save-excursion
10536                               (if (eq (forward-line -1) 0)
10537                                   (gnus-summary-article-number)
10538                                 (error "Beginning of summary buffer"))))))
10539       (unless (not (eq current-article parent-article))
10540         (error "An article may not be self-referential"))
10541       (let ((message-id (mail-header-id
10542                          (gnus-summary-article-header parent-article))))
10543         (unless (and message-id (not (equal message-id "")))
10544           (error "No message-id in desired parent"))
10545         (gnus-with-article current-article
10546           (save-restriction
10547             (goto-char (point-min))
10548             (message-narrow-to-head)
10549             (if (re-search-forward "^References: " nil t)
10550                 (progn
10551                   (re-search-forward "^[^ \t]" nil t)
10552                   (forward-line -1)
10553                   (end-of-line)
10554                   (insert " " message-id))
10555               (insert "References: " message-id "\n"))))
10556         (set-buffer gnus-summary-buffer)
10557         (gnus-summary-unmark-all-processable)
10558         (gnus-summary-update-article current-article)
10559         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10560             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10561         (gnus-summary-rethread-current)
10562         (gnus-message 3 "Article %d is now the child of article %d"
10563                       current-article parent-article)))))
10564
10565 (defun gnus-summary-toggle-threads (&optional arg)
10566   "Toggle showing conversation threads.
10567 If ARG is positive number, turn showing conversation threads on."
10568   (interactive "P")
10569   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10570     (setq gnus-show-threads
10571           (if (null arg) (not gnus-show-threads)
10572             (> (prefix-numeric-value arg) 0)))
10573     (gnus-summary-prepare)
10574     (gnus-summary-goto-subject current)
10575     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10576     (gnus-summary-position-point)))
10577
10578 (defun gnus-summary-show-all-threads ()
10579   "Show all threads."
10580   (interactive)
10581   (save-excursion
10582     (let ((buffer-read-only nil))
10583       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10584   (gnus-summary-position-point))
10585
10586 (defun gnus-summary-show-thread ()
10587   "Show thread subtrees.
10588 Returns nil if no thread was there to be shown."
10589   (interactive)
10590   (let ((buffer-read-only nil)
10591         (orig (point))
10592         (end (point-at-eol))
10593         ;; Leave point at bol
10594         (beg (progn (beginning-of-line) (point))))
10595     (prog1
10596         ;; Any hidden lines here?
10597         (search-forward "\r" end t)
10598       (subst-char-in-region beg end ?\^M ?\n t)
10599       (goto-char orig)
10600       (gnus-summary-position-point))))
10601
10602 (defun gnus-summary-maybe-hide-threads ()
10603   "If requested, hide the threads that should be hidden."
10604   (when (and gnus-show-threads
10605              gnus-thread-hide-subtree)
10606     (gnus-summary-hide-all-threads
10607      (if (or (consp gnus-thread-hide-subtree)
10608              (functionp gnus-thread-hide-subtree))
10609          (gnus-make-predicate gnus-thread-hide-subtree)
10610        nil))))
10611
10612 ;;; Hiding predicates.
10613
10614 (defun gnus-article-unread-p (header)
10615   (memq (mail-header-number header) gnus-newsgroup-unreads))
10616
10617 (defun gnus-article-unseen-p (header)
10618   (memq (mail-header-number header) gnus-newsgroup-unseen))
10619
10620 (defun gnus-map-articles (predicate articles)
10621   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10622   (apply 'gnus-or (mapcar predicate
10623                           (mapcar 'gnus-summary-article-header articles))))
10624
10625 (defun gnus-summary-hide-all-threads (&optional predicate)
10626   "Hide all thread subtrees.
10627 If PREDICATE is supplied, threads that satisfy this predicate
10628 will not be hidden."
10629   (interactive)
10630   (save-excursion
10631     (goto-char (point-min))
10632     (let ((end nil))
10633       (while (not end)
10634         (when (or (not predicate)
10635                   (gnus-map-articles
10636                    predicate (gnus-summary-article-children)))
10637             (gnus-summary-hide-thread))
10638         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10639   (gnus-summary-position-point))
10640
10641 (defun gnus-summary-hide-thread ()
10642   "Hide thread subtrees.
10643 If PREDICATE is supplied, threads that satisfy this predicate
10644 will not be hidden.
10645 Returns nil if no threads were there to be hidden."
10646   (interactive)
10647   (let ((buffer-read-only nil)
10648         (start (point))
10649         (article (gnus-summary-article-number)))
10650     (goto-char start)
10651     ;; Go forward until either the buffer ends or the subthread
10652     ;; ends.
10653     (when (and (not (eobp))
10654                (or (zerop (gnus-summary-next-thread 1 t))
10655                    (goto-char (point-max))))
10656       (prog1
10657           (if (and (> (point) start)
10658                    (search-backward "\n" start t))
10659               (progn
10660                 (subst-char-in-region start (point) ?\n ?\^M)
10661                 (gnus-summary-goto-subject article))
10662             (goto-char start)
10663             nil)))))
10664
10665 (defun gnus-summary-go-to-next-thread (&optional previous)
10666   "Go to the same level (or less) next thread.
10667 If PREVIOUS is non-nil, go to previous thread instead.
10668 Return the article number moved to, or nil if moving was impossible."
10669   (let ((level (gnus-summary-thread-level))
10670         (way (if previous -1 1))
10671         (beg (point)))
10672     (forward-line way)
10673     (while (and (not (eobp))
10674                 (< level (gnus-summary-thread-level)))
10675       (forward-line way))
10676     (if (eobp)
10677         (progn
10678           (goto-char beg)
10679           nil)
10680       (setq beg (point))
10681       (prog1
10682           (gnus-summary-article-number)
10683         (goto-char beg)))))
10684
10685 (defun gnus-summary-next-thread (n &optional silent)
10686   "Go to the same level next N'th thread.
10687 If N is negative, search backward instead.
10688 Returns the difference between N and the number of skips actually
10689 done.
10690
10691 If SILENT, don't output messages."
10692   (interactive "p")
10693   (let ((backward (< n 0))
10694         (n (abs n)))
10695     (while (and (> n 0)
10696                 (gnus-summary-go-to-next-thread backward))
10697       (decf n))
10698     (unless silent
10699       (gnus-summary-position-point))
10700     (when (and (not silent) (/= 0 n))
10701       (gnus-message 7 "No more threads"))
10702     n))
10703
10704 (defun gnus-summary-prev-thread (n)
10705   "Go to the same level previous N'th thread.
10706 Returns the difference between N and the number of skips actually
10707 done."
10708   (interactive "p")
10709   (gnus-summary-next-thread (- n)))
10710
10711 (defun gnus-summary-go-down-thread ()
10712   "Go down one level in the current thread."
10713   (let ((children (gnus-summary-article-children)))
10714     (when children
10715       (gnus-summary-goto-subject (car children)))))
10716
10717 (defun gnus-summary-go-up-thread ()
10718   "Go up one level in the current thread."
10719   (let ((parent (gnus-summary-article-parent)))
10720     (when parent
10721       (gnus-summary-goto-subject parent))))
10722
10723 (defun gnus-summary-down-thread (n)
10724   "Go down thread N steps.
10725 If N is negative, go up instead.
10726 Returns the difference between N and how many steps down that were
10727 taken."
10728   (interactive "p")
10729   (let ((up (< n 0))
10730         (n (abs n)))
10731     (while (and (> n 0)
10732                 (if up (gnus-summary-go-up-thread)
10733                   (gnus-summary-go-down-thread)))
10734       (setq n (1- n)))
10735     (gnus-summary-position-point)
10736     (when (/= 0 n)
10737       (gnus-message 7 "Can't go further"))
10738     n))
10739
10740 (defun gnus-summary-up-thread (n)
10741   "Go up thread N steps.
10742 If N is negative, go down instead.
10743 Returns the difference between N and how many steps down that were
10744 taken."
10745   (interactive "p")
10746   (gnus-summary-down-thread (- n)))
10747
10748 (defun gnus-summary-top-thread ()
10749   "Go to the top of the thread."
10750   (interactive)
10751   (while (gnus-summary-go-up-thread))
10752   (gnus-summary-article-number))
10753
10754 (defun gnus-summary-kill-thread (&optional unmark)
10755   "Mark articles under current thread as read.
10756 If the prefix argument is positive, remove any kinds of marks.
10757 If the prefix argument is negative, tick articles instead."
10758   (interactive "P")
10759   (when unmark
10760     (setq unmark (prefix-numeric-value unmark)))
10761   (let ((articles (gnus-summary-articles-in-thread)))
10762     (save-excursion
10763       ;; Expand the thread.
10764       (gnus-summary-show-thread)
10765       ;; Mark all the articles.
10766       (while articles
10767         (gnus-summary-goto-subject (car articles))
10768         (cond ((null unmark)
10769                (gnus-summary-mark-article-as-read gnus-killed-mark))
10770               ((> unmark 0)
10771                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10772               (t
10773                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10774         (setq articles (cdr articles))))
10775     ;; Hide killed subtrees.
10776     (and (null unmark)
10777          gnus-thread-hide-killed
10778          (gnus-summary-hide-thread))
10779     ;; If marked as read, go to next unread subject.
10780     (when (null unmark)
10781       ;; Go to next unread subject.
10782       (gnus-summary-next-subject 1 t)))
10783   (gnus-set-mode-line 'summary))
10784
10785 ;; Summary sorting commands
10786
10787 (defun gnus-summary-sort-by-number (&optional reverse)
10788   "Sort the summary buffer by article number.
10789 Argument REVERSE means reverse order."
10790   (interactive "P")
10791   (gnus-summary-sort 'number reverse))
10792
10793 (defun gnus-summary-sort-by-random (&optional reverse)
10794   "Randomize the order in the summary buffer.
10795 Argument REVERSE means to randomize in reverse order."
10796   (interactive "P")
10797   (gnus-summary-sort 'random reverse))
10798
10799 (defun gnus-summary-sort-by-author (&optional reverse)
10800   "Sort the summary buffer by author name alphabetically.
10801 If `case-fold-search' is non-nil, case of letters is ignored.
10802 Argument REVERSE means reverse order."
10803   (interactive "P")
10804   (gnus-summary-sort 'author reverse))
10805
10806 (defun gnus-summary-sort-by-subject (&optional reverse)
10807   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10808 If `case-fold-search' is non-nil, case of letters is ignored.
10809 Argument REVERSE means reverse order."
10810   (interactive "P")
10811   (gnus-summary-sort 'subject reverse))
10812
10813 (defun gnus-summary-sort-by-date (&optional reverse)
10814   "Sort the summary buffer by date.
10815 Argument REVERSE means reverse order."
10816   (interactive "P")
10817   (gnus-summary-sort 'date reverse))
10818
10819 (defun gnus-summary-sort-by-score (&optional reverse)
10820   "Sort the summary buffer by score.
10821 Argument REVERSE means reverse order."
10822   (interactive "P")
10823   (gnus-summary-sort 'score reverse))
10824
10825 (defun gnus-summary-sort-by-lines (&optional reverse)
10826   "Sort the summary buffer by the number of lines.
10827 Argument REVERSE means reverse order."
10828   (interactive "P")
10829   (gnus-summary-sort 'lines reverse))
10830
10831 (defun gnus-summary-sort-by-chars (&optional reverse)
10832   "Sort the summary buffer by article length.
10833 Argument REVERSE means reverse order."
10834   (interactive "P")
10835   (gnus-summary-sort 'chars reverse))
10836
10837 (defun gnus-summary-sort-by-original (&optional reverse)
10838   "Sort the summary buffer using the default sorting method.
10839 Argument REVERSE means reverse order."
10840   (interactive "P")
10841   (let* ((buffer-read-only)
10842          (gnus-summary-prepare-hook nil))
10843     ;; We do the sorting by regenerating the threads.
10844     (gnus-summary-prepare)
10845     ;; Hide subthreads if needed.
10846     (gnus-summary-maybe-hide-threads)))
10847
10848 (defun gnus-summary-sort (predicate reverse)
10849   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10850   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10851          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10852          (gnus-thread-sort-functions
10853           (if (not reverse)
10854               thread
10855             `(lambda (t1 t2)
10856                (,thread t2 t1))))
10857          (gnus-sort-gathered-threads-function
10858           gnus-thread-sort-functions)
10859          (gnus-article-sort-functions
10860           (if (not reverse)
10861               article
10862             `(lambda (t1 t2)
10863                (,article t2 t1))))
10864          (buffer-read-only)
10865          (gnus-summary-prepare-hook nil))
10866     ;; We do the sorting by regenerating the threads.
10867     (gnus-summary-prepare)
10868     ;; Hide subthreads if needed.
10869     (gnus-summary-maybe-hide-threads)))
10870
10871 ;; Summary saving commands.
10872
10873 (defun gnus-summary-save-article (&optional n not-saved)
10874   "Save the current article using the default saver function.
10875 If N is a positive number, save the N next articles.
10876 If N is a negative number, save the N previous articles.
10877 If N is nil and any articles have been marked with the process mark,
10878 save those articles instead.
10879 The variable `gnus-default-article-saver' specifies the saver function."
10880   (interactive "P")
10881   (let* ((articles (gnus-summary-work-articles n))
10882          (save-buffer (save-excursion
10883                         (nnheader-set-temp-buffer " *Gnus Save*")))
10884          (num (length articles))
10885          header file)
10886     (dolist (article articles)
10887       (setq header (gnus-summary-article-header article))
10888       (if (not (vectorp header))
10889           ;; This is a pseudo-article.
10890           (if (assq 'name header)
10891               (gnus-copy-file (cdr (assq 'name header)))
10892             (gnus-message 1 "Article %d is unsaveable" article))
10893         ;; This is a real article.
10894         (save-window-excursion
10895           (let ((gnus-display-mime-function nil)
10896                 (gnus-article-prepare-hook nil))
10897             (gnus-summary-select-article t nil nil article)))
10898         (save-excursion
10899           (set-buffer save-buffer)
10900           (erase-buffer)
10901           (insert-buffer-substring gnus-original-article-buffer))
10902         (setq file (gnus-article-save save-buffer file num))
10903         (gnus-summary-remove-process-mark article)
10904         (unless not-saved
10905           (gnus-summary-set-saved-mark article))))
10906     (gnus-kill-buffer save-buffer)
10907     (gnus-summary-position-point)
10908     (gnus-set-mode-line 'summary)
10909     n))
10910
10911 (defun gnus-summary-pipe-output (&optional arg headers)
10912   "Pipe the current article to a subprocess.
10913 If N is a positive number, pipe the N next articles.
10914 If N is a negative number, pipe the N previous articles.
10915 If N is nil and any articles have been marked with the process mark,
10916 pipe those articles instead.
10917 If HEADERS (the symbolic prefix), include the headers, too."
10918   (interactive (gnus-interactive "P\ny"))
10919   (require 'gnus-art)
10920   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10921         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10922     (gnus-summary-save-article arg t))
10923   (let ((buffer (get-buffer "*Shell Command Output*")))
10924     (when (and buffer
10925                (not (zerop (buffer-size buffer))))
10926       (gnus-configure-windows 'pipe))))
10927
10928 (defun gnus-summary-save-article-mail (&optional arg)
10929   "Append the current article to an mail file.
10930 If N is a positive number, save the N next articles.
10931 If N is a negative number, save the N previous articles.
10932 If N is nil and any articles have been marked with the process mark,
10933 save those articles instead."
10934   (interactive "P")
10935   (require 'gnus-art)
10936   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10937     (gnus-summary-save-article arg)))
10938
10939 (defun gnus-summary-save-article-rmail (&optional arg)
10940   "Append the current article to an rmail file.
10941 If N is a positive number, save the N next articles.
10942 If N is a negative number, save the N previous articles.
10943 If N is nil and any articles have been marked with the process mark,
10944 save those articles instead."
10945   (interactive "P")
10946   (require 'gnus-art)
10947   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10948     (gnus-summary-save-article arg)))
10949
10950 (defun gnus-summary-save-article-file (&optional arg)
10951   "Append the current article to a file.
10952 If N is a positive number, save the N next articles.
10953 If N is a negative number, save the N previous articles.
10954 If N is nil and any articles have been marked with the process mark,
10955 save those articles instead."
10956   (interactive "P")
10957   (require 'gnus-art)
10958   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10959     (gnus-summary-save-article arg)))
10960
10961 (defun gnus-summary-write-article-file (&optional arg)
10962   "Write the current article to a file, deleting the previous file.
10963 If N is a positive number, save the N next articles.
10964 If N is a negative number, save the N previous articles.
10965 If N is nil and any articles have been marked with the process mark,
10966 save those articles instead."
10967   (interactive "P")
10968   (require 'gnus-art)
10969   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10970     (gnus-summary-save-article arg)))
10971
10972 (defun gnus-summary-save-article-body-file (&optional arg)
10973   "Append the current article body to a file.
10974 If N is a positive number, save the N next articles.
10975 If N is a negative number, save the N previous articles.
10976 If N is nil and any articles have been marked with the process mark,
10977 save those articles instead."
10978   (interactive "P")
10979   (require 'gnus-art)
10980   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10981     (gnus-summary-save-article arg)))
10982
10983 (defun gnus-summary-muttprint (&optional arg)
10984   "Print the current article using Muttprint.
10985 If N is a positive number, save the N next articles.
10986 If N is a negative number, save the N previous articles.
10987 If N is nil and any articles have been marked with the process mark,
10988 save those articles instead."
10989   (interactive "P")
10990   (require 'gnus-art)
10991   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10992     (gnus-summary-save-article arg t)))
10993
10994 (defun gnus-summary-pipe-message (program)
10995   "Pipe the current article through PROGRAM."
10996   (interactive "sProgram: ")
10997   (gnus-summary-select-article)
10998   (let ((mail-header-separator ""))
10999     (gnus-eval-in-buffer-window gnus-article-buffer
11000       (save-restriction
11001         (widen)
11002         (let ((start (window-start))
11003               buffer-read-only)
11004           (message-pipe-buffer-body program)
11005           (set-window-start (get-buffer-window (current-buffer)) start))))))
11006
11007 (defun gnus-get-split-value (methods)
11008   "Return a value based on the split METHODS."
11009   (let (split-name method result match)
11010     (when methods
11011       (save-excursion
11012         (set-buffer gnus-original-article-buffer)
11013         (save-restriction
11014           (nnheader-narrow-to-headers)
11015           (while (and methods (not split-name))
11016             (goto-char (point-min))
11017             (setq method (pop methods))
11018             (setq match (car method))
11019             (when (cond
11020                    ((stringp match)
11021                     ;; Regular expression.
11022                     (ignore-errors
11023                       (re-search-forward match nil t)))
11024                    ((functionp match)
11025                     ;; Function.
11026                     (save-restriction
11027                       (widen)
11028                       (setq result (funcall match gnus-newsgroup-name))))
11029                    ((consp match)
11030                     ;; Form.
11031                     (save-restriction
11032                       (widen)
11033                       (setq result (eval match)))))
11034               (setq split-name (cdr method))
11035               (cond ((stringp result)
11036                      (push (expand-file-name
11037                             result gnus-article-save-directory)
11038                            split-name))
11039                     ((consp result)
11040                      (setq split-name (append result split-name)))))))))
11041     (nreverse split-name)))
11042
11043 (defun gnus-valid-move-group-p (group)
11044   (and (boundp group)
11045        (symbol-name group)
11046        (symbol-value group)
11047        (gnus-get-function (gnus-find-method-for-group
11048                            (symbol-name group)) 'request-accept-article t)))
11049
11050 (defun gnus-read-move-group-name (prompt default articles prefix)
11051   "Read a group name."
11052   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11053          (minibuffer-confirm-incomplete nil) ; XEmacs
11054          (prom
11055           (format "%s %s to:"
11056                   prompt
11057                   (if (> (length articles) 1)
11058                       (format "these %d articles" (length articles))
11059                     "this article")))
11060          (to-newsgroup
11061           (cond
11062            ((null split-name)
11063             (gnus-completing-read-with-default
11064              default prom
11065              gnus-active-hashtb
11066              'gnus-valid-move-group-p
11067              nil prefix
11068              'gnus-group-history))
11069            ((= 1 (length split-name))
11070             (gnus-completing-read-with-default
11071              (car split-name) prom
11072              gnus-active-hashtb
11073              'gnus-valid-move-group-p
11074              nil nil
11075              'gnus-group-history))
11076            (t
11077             (gnus-completing-read-with-default
11078              nil prom
11079              (mapcar (lambda (el) (list el))
11080                      (nreverse split-name))
11081              nil nil nil
11082              'gnus-group-history))))
11083          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11084     (when to-newsgroup
11085       (if (or (string= to-newsgroup "")
11086               (string= to-newsgroup prefix))
11087           (setq to-newsgroup default))
11088       (unless to-newsgroup
11089         (error "No group name entered"))
11090       (or (gnus-active to-newsgroup)
11091           (gnus-activate-group to-newsgroup nil nil to-method)
11092           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11093                                      to-newsgroup))
11094               (or (and (gnus-request-create-group to-newsgroup to-method)
11095                        (gnus-activate-group
11096                         to-newsgroup nil nil to-method)
11097                        (gnus-subscribe-group to-newsgroup))
11098                   (error "Couldn't create group %s" to-newsgroup)))
11099           (error "No such group: %s" to-newsgroup)))
11100     to-newsgroup))
11101
11102 (defun gnus-summary-save-parts (type dir n &optional reverse)
11103   "Save parts matching TYPE to DIR.
11104 If REVERSE, save parts that do not match TYPE."
11105   (interactive
11106    (list (read-string "Save parts of type: "
11107                       (or (car gnus-summary-save-parts-type-history)
11108                           gnus-summary-save-parts-default-mime)
11109                       'gnus-summary-save-parts-type-history)
11110          (setq gnus-summary-save-parts-last-directory
11111                (read-file-name "Save to directory: "
11112                                gnus-summary-save-parts-last-directory
11113                                nil t))
11114          current-prefix-arg))
11115   (gnus-summary-iterate n
11116     (let ((gnus-display-mime-function nil)
11117           (gnus-inhibit-treatment t))
11118       (gnus-summary-select-article))
11119     (save-excursion
11120       (set-buffer gnus-article-buffer)
11121       (let ((handles (or gnus-article-mime-handles
11122                          (mm-dissect-buffer nil gnus-article-loose-mime)
11123                          (and gnus-article-emulate-mime
11124                               (mm-uu-dissect)))))
11125         (when handles
11126           (gnus-summary-save-parts-1 type dir handles reverse)
11127           (unless gnus-article-mime-handles ;; Don't destroy this case.
11128             (mm-destroy-parts handles)))))))
11129
11130 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11131   (if (stringp (car handle))
11132       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11133               (cdr handle))
11134     (when (if reverse
11135               (not (string-match type (mm-handle-media-type handle)))
11136             (string-match type (mm-handle-media-type handle)))
11137       (let ((file (expand-file-name
11138                    (gnus-map-function
11139                     mm-file-name-rewrite-functions
11140                     (file-name-nondirectory
11141                      (or
11142                       (mail-content-type-get
11143                        (mm-handle-disposition handle) 'filename)
11144                       (mail-content-type-get
11145                        (mm-handle-type handle) 'name)
11146                       (concat gnus-newsgroup-name
11147                               "." (number-to-string
11148                                    (cdr gnus-article-current))))))
11149                    dir)))
11150         (unless (file-exists-p file)
11151           (mm-save-part-to-file handle file))))))
11152
11153 ;; Summary extract commands
11154
11155 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11156   (let ((buffer-read-only nil)
11157         (article (gnus-summary-article-number))
11158         after-article b e)
11159     (unless (gnus-summary-goto-subject article)
11160       (error "No such article: %d" article))
11161     (gnus-summary-position-point)
11162     ;; If all commands are to be bunched up on one line, we collect
11163     ;; them here.
11164     (unless gnus-view-pseudos-separately
11165       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11166             files action)
11167         (while ps
11168           (setq action (cdr (assq 'action (car ps))))
11169           (setq files (list (cdr (assq 'name (car ps)))))
11170           (while (and ps (cdr ps)
11171                       (string= (or action "1")
11172                                (or (cdr (assq 'action (cadr ps))) "2")))
11173             (push (cdr (assq 'name (cadr ps))) files)
11174             (setcdr ps (cddr ps)))
11175           (when files
11176             (when (not (string-match "%s" action))
11177               (push " " files))
11178             (push " " files)
11179             (when (assq 'execute (car ps))
11180               (setcdr (assq 'execute (car ps))
11181                       (funcall (if (string-match "%s" action)
11182                                    'format 'concat)
11183                                action
11184                                (mapconcat
11185                                 (lambda (f)
11186                                   (if (equal f " ")
11187                                       f
11188                                     (shell-quote-argument f)))
11189                                 files " ")))))
11190           (setq ps (cdr ps)))))
11191     (if (and gnus-view-pseudos (not not-view))
11192         (while pslist
11193           (when (assq 'execute (car pslist))
11194             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11195                                   (eq gnus-view-pseudos 'not-confirm)))
11196           (setq pslist (cdr pslist)))
11197       (save-excursion
11198         (while pslist
11199           (setq after-article (or (cdr (assq 'article (car pslist)))
11200                                   (gnus-summary-article-number)))
11201           (gnus-summary-goto-subject after-article)
11202           (forward-line 1)
11203           (setq b (point))
11204           (insert "    " (file-name-nondirectory
11205                           (cdr (assq 'name (car pslist))))
11206                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11207           (setq e (point))
11208           (forward-line -1)             ; back to `b'
11209           (gnus-add-text-properties
11210            b (1- e) (list 'gnus-number gnus-reffed-article-number
11211                           gnus-mouse-face-prop gnus-mouse-face))
11212           (gnus-data-enter
11213            after-article gnus-reffed-article-number
11214            gnus-unread-mark b (car pslist) 0 (- e b))
11215           (setq gnus-newsgroup-unreads
11216                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11217                                          gnus-reffed-article-number))
11218           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11219           (setq pslist (cdr pslist)))))))
11220
11221 (defun gnus-pseudos< (p1 p2)
11222   (let ((c1 (cdr (assq 'action p1)))
11223         (c2 (cdr (assq 'action p2))))
11224     (and c1 c2 (string< c1 c2))))
11225
11226 (defun gnus-request-pseudo-article (props)
11227   (cond ((assq 'execute props)
11228          (gnus-execute-command (cdr (assq 'execute props)))))
11229   (let ((gnus-current-article (gnus-summary-article-number)))
11230     (gnus-run-hooks 'gnus-mark-article-hook)))
11231
11232 (defun gnus-execute-command (command &optional automatic)
11233   (save-excursion
11234     (gnus-article-setup-buffer)
11235     (set-buffer gnus-article-buffer)
11236     (setq buffer-read-only nil)
11237     (let ((command (if automatic command
11238                      (read-string "Command: " (cons command 0)))))
11239       (erase-buffer)
11240       (insert "$ " command "\n\n")
11241       (if gnus-view-pseudo-asynchronously
11242           (start-process "gnus-execute" (current-buffer) shell-file-name
11243                          shell-command-switch command)
11244         (call-process shell-file-name nil t nil
11245                       shell-command-switch command)))))
11246
11247 ;; Summary kill commands.
11248
11249 (defun gnus-summary-edit-global-kill (article)
11250   "Edit the \"global\" kill file."
11251   (interactive (list (gnus-summary-article-number)))
11252   (gnus-group-edit-global-kill article))
11253
11254 (defun gnus-summary-edit-local-kill ()
11255   "Edit a local kill file applied to the current newsgroup."
11256   (interactive)
11257   (setq gnus-current-headers (gnus-summary-article-header))
11258   (gnus-group-edit-local-kill
11259    (gnus-summary-article-number) gnus-newsgroup-name))
11260
11261 ;;; Header reading.
11262
11263 (defun gnus-read-header (id &optional header)
11264   "Read the headers of article ID and enter them into the Gnus system."
11265   (let ((group gnus-newsgroup-name)
11266         (gnus-override-method
11267          (or
11268           gnus-override-method
11269           (and (gnus-news-group-p gnus-newsgroup-name)
11270                (car (gnus-refer-article-methods)))))
11271         where)
11272     ;; First we check to see whether the header in question is already
11273     ;; fetched.
11274     (if (stringp id)
11275         ;; This is a Message-ID.
11276         (setq header (or header (gnus-id-to-header id)))
11277       ;; This is an article number.
11278       (setq header (or header (gnus-summary-article-header id))))
11279     (if (and header
11280              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11281         ;; We have found the header.
11282         header
11283       ;; If this is a sparse article, we have to nix out its
11284       ;; previous entry in the thread hashtb.
11285       (when (and header
11286                  (gnus-summary-article-sparse-p (mail-header-number header)))
11287         (let* ((parent (gnus-parent-id (mail-header-references header)))
11288                (thread (and parent (gnus-id-to-thread parent))))
11289           (when thread
11290             (delq (assq header thread) thread))))
11291       ;; We have to really fetch the header to this article.
11292       (save-excursion
11293         (set-buffer nntp-server-buffer)
11294         (when (setq where (gnus-request-head id group))
11295           (nnheader-fold-continuation-lines)
11296           (goto-char (point-max))
11297           (insert ".\n")
11298           (goto-char (point-min))
11299           (insert "211 ")
11300           (princ (cond
11301                   ((numberp id) id)
11302                   ((cdr where) (cdr where))
11303                   (header (mail-header-number header))
11304                   (t gnus-reffed-article-number))
11305                  (current-buffer))
11306           (insert " Article retrieved.\n"))
11307         (if (or (not where)
11308                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11309             ()                          ; Malformed head.
11310           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11311             (when (and (stringp id)
11312                        (not (string= (gnus-group-real-name group)
11313                                      (car where))))
11314               ;; If we fetched by Message-ID and the article came
11315               ;; from a different group, we fudge some bogus article
11316               ;; numbers for this article.
11317               (mail-header-set-number header gnus-reffed-article-number))
11318             (save-excursion
11319               (set-buffer gnus-summary-buffer)
11320               (decf gnus-reffed-article-number)
11321               (gnus-remove-header (mail-header-number header))
11322               (push header gnus-newsgroup-headers)
11323               (setq gnus-current-headers header)
11324               (push (mail-header-number header) gnus-newsgroup-limit)))
11325           header)))))
11326
11327 (defun gnus-remove-header (number)
11328   "Remove header NUMBER from `gnus-newsgroup-headers'."
11329   (if (and gnus-newsgroup-headers
11330            (= number (mail-header-number (car gnus-newsgroup-headers))))
11331       (pop gnus-newsgroup-headers)
11332     (let ((headers gnus-newsgroup-headers))
11333       (while (and (cdr headers)
11334                   (not (= number (mail-header-number (cadr headers)))))
11335         (pop headers))
11336       (when (cdr headers)
11337         (setcdr headers (cddr headers))))))
11338
11339 ;;;
11340 ;;; summary highlights
11341 ;;;
11342
11343 (defun gnus-highlight-selected-summary ()
11344   "Highlight selected article in summary buffer."
11345   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11346   (when gnus-summary-selected-face
11347     (save-excursion
11348       (let* ((beg (point-at-bol))
11349              (end (point-at-eol))
11350              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11351              (from (if (get-text-property beg gnus-mouse-face-prop)
11352                        beg
11353                      (or (next-single-property-change
11354                           beg gnus-mouse-face-prop nil end)
11355                          beg)))
11356              (to
11357               (if (= from end)
11358                   (- from 2)
11359                 (or (next-single-property-change
11360                      from gnus-mouse-face-prop nil end)
11361                     end))))
11362         ;; If no mouse-face prop on line we will have to = from = end,
11363         ;; so we highlight the entire line instead.
11364         (when (= (+ to 2) from)
11365           (setq from beg)
11366           (setq to end))
11367         (if gnus-newsgroup-selected-overlay
11368             ;; Move old overlay.
11369             (gnus-move-overlay
11370              gnus-newsgroup-selected-overlay from to (current-buffer))
11371           ;; Create new overlay.
11372           (gnus-overlay-put
11373            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11374            'face gnus-summary-selected-face))))))
11375
11376 (defvar gnus-summary-highlight-line-cached nil)
11377 (defvar gnus-summary-highlight-line-trigger nil)
11378
11379 (defun gnus-summary-highlight-line-0 ()
11380   (if (and (eq gnus-summary-highlight-line-trigger
11381                gnus-summary-highlight)
11382            gnus-summary-highlight-line-cached)
11383       gnus-summary-highlight-line-cached
11384     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11385           gnus-summary-highlight-line-cached
11386           (let* ((cond (list 'cond))
11387                  (c cond)
11388                  (list gnus-summary-highlight))
11389             (while list
11390               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11391                               nil))
11392               (setq c (cdr c)
11393                     list (cdr list)))
11394             (gnus-byte-compile (list 'lambda nil cond))))))
11395
11396 (defun gnus-summary-highlight-line ()
11397   "Highlight current line according to `gnus-summary-highlight'."
11398   (let* ((beg (point-at-bol))
11399          (article (or (gnus-summary-article-number) gnus-current-article))
11400          (score (or (cdr (assq article
11401                                gnus-newsgroup-scored))
11402                     gnus-summary-default-score 0))
11403          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11404          (inhibit-read-only t)
11405          (default gnus-summary-default-score)
11406          (default-high gnus-summary-default-high-score)
11407          (default-low gnus-summary-default-low-score)
11408          (uncached (and gnus-summary-use-undownloaded-faces
11409                         (memq article gnus-newsgroup-undownloaded))))
11410     (let ((face (funcall (gnus-summary-highlight-line-0))))
11411       (unless (eq face (get-text-property beg 'face))
11412         (gnus-put-text-property-excluding-characters-with-faces
11413          beg (point-at-eol) 'face
11414          (setq face (if (boundp face) (symbol-value face) face)))
11415         (when gnus-summary-highlight-line-function
11416           (funcall gnus-summary-highlight-line-function article face))))))
11417
11418 (defun gnus-update-read-articles (group unread &optional compute)
11419   "Update the list of read articles in GROUP.
11420 UNREAD is a sorted list."
11421   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11422         (info (gnus-get-info group))
11423         (prev 1)
11424         read)
11425     (if (or (not info) (not active))
11426         ;; There is no info on this group if it was, in fact,
11427         ;; killed.  Gnus stores no information on killed groups, so
11428         ;; there's nothing to be done.
11429         ;; One could store the information somewhere temporarily,
11430         ;; perhaps...  Hmmm...
11431         ()
11432       ;; Remove any negative articles numbers.
11433       (while (and unread (< (car unread) 0))
11434         (setq unread (cdr unread)))
11435       ;; Remove any expired article numbers
11436       (while (and unread (< (car unread) (car active)))
11437         (setq unread (cdr unread)))
11438       ;; Compute the ranges of read articles by looking at the list of
11439       ;; unread articles.
11440       (while unread
11441         (when (/= (car unread) prev)
11442           (push (if (= prev (1- (car unread))) prev
11443                   (cons prev (1- (car unread))))
11444                 read))
11445         (setq prev (1+ (car unread)))
11446         (setq unread (cdr unread)))
11447       (when (<= prev (cdr active))
11448         (push (cons prev (cdr active)) read))
11449       (setq read (if (> (length read) 1) (nreverse read) read))
11450       (if compute
11451           read
11452         (save-excursion
11453           (let (setmarkundo)
11454             ;; Propagate the read marks to the backend.
11455             (when (gnus-check-backend-function 'request-set-mark group)
11456               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11457                     (add (gnus-remove-from-range read (gnus-info-read info))))
11458                 (when (or add del)
11459                   (unless (gnus-check-group group)
11460                     (error "Can't open server for %s" group))
11461                   (gnus-request-set-mark
11462                    group (delq nil (list (if add (list add 'add '(read)))
11463                                          (if del (list del 'del '(read))))))
11464                   (setq setmarkundo
11465                         `(gnus-request-set-mark
11466                           ,group
11467                           ',(delq nil (list
11468                                        (if del (list del 'add '(read)))
11469                                        (if add (list add 'del '(read))))))))))
11470             (set-buffer gnus-group-buffer)
11471             (gnus-undo-register
11472               `(progn
11473                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11474                  (gnus-info-set-read ',info ',(gnus-info-read info))
11475                  (gnus-get-unread-articles-in-group ',info
11476                                                     (gnus-active ,group))
11477                  (gnus-group-update-group ,group t)
11478                  ,setmarkundo))))
11479         ;; Enter this list into the group info.
11480         (gnus-info-set-read info read)
11481         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11482         (gnus-get-unread-articles-in-group info (gnus-active group))
11483         t))))
11484
11485 (defun gnus-offer-save-summaries ()
11486   "Offer to save all active summary buffers."
11487   (let (buffers)
11488     ;; Go through all buffers and find all summaries.
11489     (dolist (buffer (buffer-list))
11490       (when (and (setq buffer (buffer-name buffer))
11491                  (string-match "Summary" buffer)
11492                  (save-excursion
11493                    (set-buffer buffer)
11494                    ;; We check that this is, indeed, a summary buffer.
11495                    (and (eq major-mode 'gnus-summary-mode)
11496                         ;; Also make sure this isn't bogus.
11497                         gnus-newsgroup-prepared
11498                         ;; Also make sure that this isn't a
11499                         ;; dead summary buffer.
11500                         (not gnus-dead-summary-mode))))
11501         (push buffer buffers)))
11502     ;; Go through all these summary buffers and offer to save them.
11503     (when buffers
11504       (save-excursion
11505         (map-y-or-n-p
11506          "Update summary buffer %s? "
11507          (lambda (buf)
11508            (switch-to-buffer buf)
11509            (gnus-summary-exit))
11510          buffers)))))
11511
11512
11513 ;;; @ for mime-partial
11514 ;;;
11515
11516 (defun gnus-request-partial-message ()
11517   (save-excursion
11518     (let ((number (gnus-summary-article-number))
11519           (group gnus-newsgroup-name)
11520           (mother gnus-article-buffer))
11521       (set-buffer (get-buffer-create " *Partial Article*"))
11522       (erase-buffer)
11523       (setq mime-preview-buffer mother)
11524       (gnus-request-article-this-buffer number group)
11525       (mime-parse-buffer)
11526       )))
11527
11528 (autoload 'mime-combine-message/partial-pieces-automatically
11529   "mime-partial"
11530   "Internal method to combine message/partial messages automatically.")
11531
11532 (mime-add-condition
11533  'action '((type . message)(subtype . partial)
11534            (major-mode . gnus-original-article-mode)
11535            (method . mime-combine-message/partial-pieces-automatically)
11536            (summary-buffer-exp . gnus-summary-buffer)
11537            (request-partial-message-method . gnus-request-partial-message)
11538            ))
11539
11540
11541 ;;; @ for message/rfc822
11542 ;;;
11543
11544 (defun gnus-mime-extract-message/rfc822 (entity situation)
11545   "Burst a forwarded article."
11546   (save-excursion
11547     (set-buffer gnus-summary-buffer)
11548     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11549                                    gnus-newsgroup-name 'gnus-group-history))
11550            (gnus-group-marked (list group))
11551            article info)
11552       (with-temp-buffer
11553         (mime-insert-entity-content entity)
11554         (setq article (gnus-request-accept-article group)))
11555       (when (and (consp article)
11556                  (numberp (setq article (cdr article))))
11557         (setq info (gnus-get-info group))
11558         (gnus-info-set-read info
11559                             (gnus-remove-from-range (gnus-info-read info)
11560                                                     (list article)))
11561         (when (string-equal group gnus-newsgroup-name)
11562           (forward-line 1)
11563           (let (gnus-show-threads)
11564             (gnus-summary-goto-subject article t))
11565           (gnus-summary-clear-mark-forward 1))
11566         (set-buffer gnus-group-buffer)
11567         (gnus-group-get-new-news-this-group nil t)))))
11568
11569 (mime-add-condition
11570  'action '((type . message)(subtype . rfc822)
11571            (major-mode . gnus-original-article-mode)
11572            (method . gnus-mime-extract-message/rfc822)
11573            (mode . "extract")
11574            ))
11575
11576 (mime-add-condition
11577  'action '((type . message)(subtype . news)
11578            (major-mode . gnus-original-article-mode)
11579            (method . gnus-mime-extract-message/rfc822)
11580            (mode . "extract")
11581            ))
11582
11583 (defun gnus-mime-extract-multipart (entity situation)
11584   (let ((children (mime-entity-children entity))
11585         mime-acting-situation-to-override
11586         f)
11587     (while children
11588       (mime-play-entity (car children)
11589                         (cons (assq 'mode situation)
11590                               mime-acting-situation-to-override))
11591       (setq children (cdr children)))
11592     (if (setq f (cdr (assq 'after-method
11593                            mime-acting-situation-to-override)))
11594         (eval f)
11595       )))
11596
11597 (mime-add-condition
11598  'action '((type . multipart)
11599            (method . gnus-mime-extract-multipart)
11600            (mode . "extract")
11601            )
11602  'with-default)
11603
11604
11605 ;;; @ end
11606 ;;;
11607
11608 (defun gnus-summary-inherit-default-charset ()
11609   "Import `default-mime-charset' from summary buffer.
11610 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11611 of FLIM is used."
11612   (if (buffer-live-p gnus-summary-buffer)
11613       (let (d-m-c d-m-c-u)
11614         (with-current-buffer gnus-summary-buffer
11615           (setq d-m-c (if (local-variable-p 'default-mime-charset
11616                                             gnus-summary-buffer)
11617                           default-mime-charset
11618                         t)
11619                 ;; LIMIT
11620                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11621                                               gnus-summary-buffer)
11622                             (symbol-value 'default-mime-charset-unlimited)
11623                           t)))
11624         (if (eq t d-m-c)
11625             (kill-local-variable 'default-mime-charset)
11626           (set (make-local-variable 'default-mime-charset) d-m-c))
11627         (if (eq t d-m-c-u)
11628             (kill-local-variable 'default-mime-charset-unlimited)
11629           (set (make-local-variable 'default-mime-charset-unlimited)
11630                d-m-c-u)))))
11631
11632 (defun gnus-summary-setup-default-charset ()
11633   "Setup newsgroup default charset."
11634   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11635       (progn
11636         (setq gnus-newsgroup-charset nil)
11637         (set (make-local-variable 'default-mime-charset) nil)
11638         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11639           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11640     (let ((ignored-charsets
11641            (or gnus-newsgroup-ephemeral-ignored-charsets
11642                (append
11643                 (and gnus-newsgroup-name
11644                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11645                 gnus-newsgroup-ignored-charsets)))
11646           charset)
11647       (setq gnus-newsgroup-charset
11648             (or gnus-newsgroup-ephemeral-charset
11649                 (when (and gnus-newsgroup-name
11650                            (setq charset (gnus-parameter-charset
11651                                           gnus-newsgroup-name)))
11652                   (make-local-variable 'default-mime-charset)
11653                   (setq default-mime-charset charset))
11654                 gnus-default-charset))
11655       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11656            ignored-charsets))))
11657
11658 ;;;
11659 ;;; Mime Commands
11660 ;;;
11661
11662 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11663   "Display the current article buffer fully MIME-buttonized.
11664 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11665 treated as multipart/mixed."
11666   (interactive "P")
11667   (require 'gnus-art)
11668   (let ((gnus-unbuttonized-mime-types nil)
11669         (gnus-mime-display-multipart-as-mixed show-all-parts))
11670     (gnus-summary-show-article)))
11671
11672 (defun gnus-summary-repair-multipart (article)
11673   "Add a Content-Type header to a multipart article without one."
11674   (interactive (list (gnus-summary-article-number)))
11675   (gnus-with-article article
11676     (message-narrow-to-head)
11677     (message-remove-header "Mime-Version")
11678     (goto-char (point-max))
11679     (insert "Mime-Version: 1.0\n")
11680     (widen)
11681     (when (search-forward "\n--" nil t)
11682       (let ((separator (buffer-substring (point) (point-at-eol))))
11683         (message-narrow-to-head)
11684         (message-remove-header "Content-Type")
11685         (goto-char (point-max))
11686         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11687                         separator))
11688         (widen))))
11689   (let (gnus-mark-article-hook)
11690     (gnus-summary-select-article t t nil article)))
11691
11692 (defun gnus-summary-toggle-display-buttonized ()
11693   "Toggle the buttonizing of the article buffer."
11694   (interactive)
11695   (require 'gnus-art)
11696   (if (setq gnus-inhibit-mime-unbuttonizing
11697             (not gnus-inhibit-mime-unbuttonizing))
11698       (let ((gnus-unbuttonized-mime-types nil))
11699         (gnus-summary-show-article))
11700     (gnus-summary-show-article)))
11701
11702 ;;;
11703 ;;; Intelli-mouse commmands
11704 ;;;
11705
11706 (defun gnus-wheel-summary-scroll (event)
11707   (interactive "e")
11708   (let ((amount (if (memq 'shift (event-modifiers event))
11709                     (car gnus-wheel-scroll-amount)
11710                   (cdr gnus-wheel-scroll-amount)))
11711         (direction (- (* (static-if (featurep 'xemacs)
11712                              (event-button event)
11713                            (cond ((eq 'mouse-4 (event-basic-type event))
11714                                   4)
11715                                  ((eq 'mouse-5 (event-basic-type event))
11716                                   5)))
11717                          2) 9))
11718         edge)
11719     (gnus-summary-scroll-up (* amount direction))
11720     (when (gnus-eval-in-buffer-window gnus-article-buffer
11721             (save-restriction
11722               (widen)
11723               (and (if (< 0 direction)
11724                        (gnus-article-next-page 0)
11725                      (gnus-article-prev-page 0)
11726                      (bobp))
11727                    (if (setq edge (get-text-property
11728                                    (point-min) 'gnus-wheel-edge))
11729                        (setq edge (* edge direction))
11730                      (setq edge -1))
11731                    (or (plusp edge)
11732                        (let ((buffer-read-only nil)
11733                              (inhibit-read-only t))
11734                          (put-text-property (point-min) (point-max)
11735                                             'gnus-wheel-edge direction)
11736                          nil))
11737                    (or (> edge gnus-wheel-edge-resistance)
11738                        (let ((buffer-read-only nil)
11739                              (inhibit-read-only t))
11740                          (put-text-property (point-min) (point-max)
11741                                             'gnus-wheel-edge
11742                                             (* (1+ edge) direction))
11743                          nil))
11744                    (eq last-command 'gnus-wheel-summary-scroll))))
11745       (gnus-summary-next-article nil nil (minusp direction)))))
11746
11747 (defun gnus-wheel-install ()
11748   "Enable mouse wheel support on summary window."
11749   (when gnus-use-wheel
11750     (let ((keys
11751            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11752       (dolist (key keys)
11753         (define-key gnus-summary-mode-map key
11754           'gnus-wheel-summary-scroll)))))
11755
11756 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11757
11758 ;;;
11759 ;;; Traditional PGP commmands
11760 ;;;
11761
11762 (defun gnus-summary-decrypt-article (&optional force)
11763   "Decrypt the current article in traditional PGP way.
11764 This will have permanent effect only in mail groups.
11765 If FORCE is non-nil, allow editing of articles even in read-only
11766 groups."
11767   (interactive "P")
11768   (gnus-summary-select-article t)
11769   (gnus-eval-in-buffer-window gnus-article-buffer
11770     (save-excursion
11771       (save-restriction
11772         (widen)
11773         (goto-char (point-min))
11774         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11775           (error "Not a traditional PGP message!"))
11776         (let ((armor-start (match-beginning 0)))
11777           (if (and (pgg-decrypt-region armor-start (point-max))
11778                    (or force (not (gnus-group-read-only-p))))
11779               (let ((inhibit-read-only t)
11780                     buffer-read-only)
11781                 (delete-region armor-start
11782                                (progn
11783                                  (re-search-forward "^-+END PGP" nil t)
11784                                  (beginning-of-line 2)
11785                                  (point)))
11786                 (insert-buffer-substring pgg-output-buffer))))))))
11787
11788 (defun gnus-summary-verify-article ()
11789   "Verify the current article in traditional PGP way."
11790   (interactive)
11791   (save-excursion
11792     (set-buffer gnus-original-article-buffer)
11793     (goto-char (point-min))
11794     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11795       (error "Not a traditional PGP message!"))
11796     (re-search-forward "^-+END PGP" nil t)
11797     (beginning-of-line 2)
11798     (call-interactively (function pgg-verify-region))))
11799
11800 ;;;
11801 ;;; Generic summary marking commands
11802 ;;;
11803
11804 (defvar gnus-summary-marking-alist
11805   '((read gnus-del-mark "d")
11806     (unread gnus-unread-mark "u")
11807     (ticked gnus-ticked-mark "!")
11808     (dormant gnus-dormant-mark "?")
11809     (expirable gnus-expirable-mark "e"))
11810   "An alist of names/marks/keystrokes.")
11811
11812 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11813 (defvar gnus-summary-mark-map)
11814
11815 (defun gnus-summary-make-all-marking-commands ()
11816   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11817   (dolist (elem gnus-summary-marking-alist)
11818     (apply 'gnus-summary-make-marking-command elem)))
11819
11820 (defun gnus-summary-make-marking-command (name mark keystroke)
11821   (let ((map (make-sparse-keymap)))
11822     (define-key gnus-summary-generic-mark-map keystroke map)
11823     (dolist (lway `((next "next" next nil "n")
11824                     (next-unread "next unread" next t "N")
11825                     (prev "previous" prev nil "p")
11826                     (prev-unread "previous unread" prev t "P")
11827                     (nomove "" nil nil ,keystroke)))
11828       (let ((func (gnus-summary-make-marking-command-1
11829                    mark (car lway) lway name)))
11830         (setq func (eval func))
11831         (define-key map (nth 4 lway) func)))))
11832
11833 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11834   `(defun ,(intern
11835             (format "gnus-summary-put-mark-as-%s%s"
11836                     name (if (eq way 'nomove)
11837                              ""
11838                            (concat "-" (symbol-name way)))))
11839      (n)
11840      ,(format
11841        "Mark the current article as %s%s.
11842 If N, the prefix, then repeat N times.
11843 If N is negative, move in reverse order.
11844 The difference between N and the actual number of articles marked is
11845 returned."
11846        name (car (cdr lway)))
11847      (interactive "p")
11848      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11849
11850 (defun gnus-summary-generic-mark (n mark move unread)
11851   "Mark N articles with MARK."
11852   (unless (eq major-mode 'gnus-summary-mode)
11853     (error "This command can only be used in the summary buffer"))
11854   (gnus-summary-show-thread)
11855   (let ((nummove
11856          (cond
11857           ((eq move 'next) 1)
11858           ((eq move 'prev) -1)
11859           (t 0))))
11860     (if (zerop nummove)
11861         (setq n 1)
11862       (when (< n 0)
11863         (setq n (abs n)
11864               nummove (* -1 nummove))))
11865     (while (and (> n 0)
11866                 (gnus-summary-mark-article nil mark)
11867                 (zerop (gnus-summary-next-subject nummove unread t)))
11868       (setq n (1- n)))
11869     (when (/= 0 n)
11870       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11871     (gnus-summary-recenter)
11872     (gnus-summary-position-point)
11873     (gnus-set-mode-line 'summary)
11874     n))
11875
11876 (defun gnus-summary-insert-articles (articles)
11877   (when (setq articles
11878               (gnus-sorted-difference articles
11879                                       (mapcar (lambda (h)
11880                                                 (mail-header-number h))
11881                                               gnus-newsgroup-headers)))
11882     (setq gnus-newsgroup-headers
11883           (gnus-merge 'list
11884                       gnus-newsgroup-headers
11885                       (gnus-fetch-headers articles)
11886                       'gnus-article-sort-by-number))
11887     ;; Suppress duplicates?
11888     (when gnus-suppress-duplicates
11889       (gnus-dup-suppress-articles))
11890
11891     ;; We might want to build some more threads first.
11892     (when (and gnus-fetch-old-headers
11893                (eq gnus-headers-retrieved-by 'nov))
11894       (if (eq gnus-fetch-old-headers 'invisible)
11895           (gnus-build-all-threads)
11896         (gnus-build-old-threads)))
11897     ;; Let the Gnus agent mark articles as read.
11898     (when gnus-agent
11899       (gnus-agent-get-undownloaded-list))
11900     ;; Remove list identifiers from subject
11901     (when gnus-list-identifiers
11902       (gnus-summary-remove-list-identifiers))
11903     ;; First and last article in this newsgroup.
11904     (when gnus-newsgroup-headers
11905       (setq gnus-newsgroup-begin
11906             (mail-header-number (car gnus-newsgroup-headers))
11907             gnus-newsgroup-end
11908             (mail-header-number
11909              (gnus-last-element gnus-newsgroup-headers))))
11910     (when gnus-use-scoring
11911       (gnus-possibly-score-headers))))
11912
11913 (defun gnus-summary-insert-old-articles (&optional all)
11914   "Insert all old articles in this group.
11915 If ALL is non-nil, already read articles become readable.
11916 If ALL is a number, fetch this number of articles."
11917   (interactive "P")
11918   (prog1
11919       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11920             older len)
11921         (setq older
11922               ;; Some nntp servers lie about their active range.  When
11923               ;; this happens, the active range can be in the millions.
11924               ;; Use a compressed range to avoid creating a huge list.
11925               (gnus-range-difference (list gnus-newsgroup-active) old))
11926         (setq len (gnus-range-length older))
11927         (cond
11928          ((null older) nil)
11929          ((numberp all)
11930           (if (< all len)
11931               (let ((older-range (nreverse older)))
11932                 (setq older nil)
11933
11934                 (while (> all 0)
11935                   (let* ((r (pop older-range))
11936                          (min (if (numberp r) r (car r)))
11937                          (max (if (numberp r) r (cdr r))))
11938                     (while (and (<= min max)
11939                                 (> all 0))
11940                       (push max older)
11941                       (setq all (1- all)
11942                             max (1- max))))))
11943             (setq older (gnus-uncompress-range older))))
11944          (all
11945           (setq older (gnus-uncompress-range older)))
11946          (t
11947           (when (and (numberp gnus-large-newsgroup)
11948                    (> len gnus-large-newsgroup))
11949               (let* ((cursor-in-echo-area nil)
11950                      (initial (gnus-parameter-large-newsgroup-initial
11951                                gnus-newsgroup-name))
11952                      (input
11953                       (read-string
11954                        (format
11955                         "How many articles from %s (%s %d): "
11956                         (gnus-limit-string
11957                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11958                         (if initial "max" "default")
11959                         len)
11960                        (if initial
11961                            (cons (number-to-string initial)
11962                                  0)))))
11963                 (unless (string-match "^[ \t]*$" input)
11964                   (setq all (string-to-number input))
11965                   (if (< all len)
11966                       (let ((older-range (nreverse older)))
11967                         (setq older nil)
11968
11969                         (while (> all 0)
11970                           (let* ((r (pop older-range))
11971                                  (min (if (numberp r) r (car r)))
11972                                  (max (if (numberp r) r (cdr r))))
11973                             (while (and (<= min max)
11974                                         (> all 0))
11975                               (push max older)
11976                               (setq all (1- all)
11977                                     max (1- max))))))))))
11978           (setq older (gnus-uncompress-range older))))
11979         (if (not older)
11980             (message "No old news.")
11981           (gnus-summary-insert-articles older)
11982           (gnus-summary-limit (gnus-sorted-nunion old older))))
11983     (gnus-summary-position-point)))
11984
11985 (defun gnus-summary-insert-new-articles ()
11986   "Insert all new articles in this group."
11987   (interactive)
11988   (prog1
11989       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11990             (old-active gnus-newsgroup-active)
11991             (nnmail-fetched-sources (list t))
11992             i new)
11993         (setq gnus-newsgroup-active
11994               (gnus-activate-group gnus-newsgroup-name 'scan))
11995         (setq i (cdr gnus-newsgroup-active))
11996         (while (> i (cdr old-active))
11997           (push i new)
11998           (decf i))
11999         (if (not new)
12000             (message "No gnus is bad news")
12001           (gnus-summary-insert-articles new)
12002           (setq gnus-newsgroup-unreads
12003                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12004           (gnus-summary-limit (gnus-sorted-nunion old new))))
12005     (gnus-summary-position-point)))
12006
12007 (gnus-summary-make-all-marking-commands)
12008
12009 (gnus-ems-redefine)
12010
12011 (provide 'gnus-sum)
12012
12013 (run-hooks 'gnus-sum-load-hook)
12014
12015 ;; Local Variables:
12016 ;; coding: iso-8859-1
12017 ;; End:
12018
12019 ;;; gnus-sum.el ends here