4cf307635ba0825362fa3cfc1a982048696a6bc3
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile
32   (require 'cl)
33   (require 'gnus-clfns)
34   (defvar tool-bar-map))
35
36 (require 'gnus)
37 (require 'gnus-group)
38 (require 'gnus-spec)
39 (require 'gnus-range)
40 (require 'gnus-int)
41 (require 'gnus-undo)
42 (require 'gnus-util)
43 (require 'nnoo)
44
45 (eval-when-compile
46   (require 'mime-play)
47   (require 'static))
48
49 (eval-and-compile
50   (autoload 'pgg-decrypt-region "pgg" nil t)
51   (autoload 'pgg-verify-region "pgg" nil t))
52
53 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
54 (autoload 'gnus-cache-write-active "gnus-cache")
55 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
56 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
57 (autoload 'mm-uu-dissect "mm-uu")
58 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
59   "Deuglify broken Outlook (Express) articles and redisplay."
60   t)
61 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
62 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
63 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
64
65 (defcustom gnus-kill-summary-on-exit t
66   "*If non-nil, kill the summary buffer when you exit from it.
67 If nil, the summary will become a \"*Dead Summary*\" buffer, and
68 it will be killed sometime later."
69   :group 'gnus-summary-exit
70   :type 'boolean)
71
72 (defcustom gnus-fetch-old-headers nil
73   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
74 If an unread article in the group refers to an older, already read (or
75 just marked as read) article, the old article will not normally be
76 displayed in the Summary buffer.  If this variable is non-nil, Gnus
77 will attempt to grab the headers to the old articles, and thereby
78 build complete threads.  If it has the value `some', only enough
79 headers to connect otherwise loose threads will be displayed.  This
80 variable can also be a number.  In that case, no more than that number
81 of old headers will be fetched.  If it has the value `invisible', all
82 old headers will be fetched, but none will be displayed.
83
84 The server has to support NOV for any of this to work."
85   :group 'gnus-thread
86   :type '(choice (const :tag "off" nil)
87                  (const some)
88                  number
89                  (sexp :menu-tag "other" t)))
90
91 (defcustom gnus-refer-thread-limit 200
92   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
93 If t, fetch all the available old headers."
94   :group 'gnus-thread
95   :type '(choice number
96                  (sexp :menu-tag "other" t)))
97
98 (defcustom gnus-summary-make-false-root 'adopt
99   "*nil means that Gnus won't gather loose threads.
100 If the root of a thread has expired or been read in a previous
101 session, the information necessary to build a complete thread has been
102 lost.  Instead of having many small sub-threads from this original thread
103 scattered all over the summary buffer, Gnus can gather them.
104
105 If non-nil, Gnus will try to gather all loose sub-threads from an
106 original thread into one large thread.
107
108 If this variable is non-nil, it should be one of `none', `adopt',
109 `dummy' or `empty'.
110
111 If this variable is `none', Gnus will not make a false root, but just
112 present the sub-threads after another.
113 If this variable is `dummy', Gnus will create a dummy root that will
114 have all the sub-threads as children.
115 If this variable is `adopt', Gnus will make one of the \"children\"
116 the parent and mark all the step-children as such.
117 If this variable is `empty', the \"children\" are printed with empty
118 subject fields.  (Or rather, they will be printed with a string
119 given by the `gnus-summary-same-subject' variable.)"
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const none)
123                  (const dummy)
124                  (const adopt)
125                  (const empty)))
126
127 (defcustom gnus-summary-make-false-root-always nil
128   "Always make a false dummy root."
129   :group 'gnus-thread
130   :type 'boolean)
131
132 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
133   "*A regexp to match subjects to be excluded from loose thread gathering.
134 As loose thread gathering is done on subjects only, that means that
135 there can be many false gatherings performed.  By rooting out certain
136 common subjects, gathering might become saner."
137   :group 'gnus-thread
138   :type 'regexp)
139
140 (defcustom gnus-summary-gather-subject-limit nil
141   "*Maximum length of subject comparisons when gathering loose threads.
142 Use nil to compare full subjects.  Setting this variable to a low
143 number will help gather threads that have been corrupted by
144 newsreaders chopping off subject lines, but it might also mean that
145 unrelated articles that have subject that happen to begin with the
146 same few characters will be incorrectly gathered.
147
148 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
149 comparing subjects."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  (const fuzzy)
153                  (sexp :menu-tag "on" t)))
154
155 (defcustom gnus-simplify-subject-functions nil
156   "List of functions taking a string argument that simplify subjects.
157 The functions are applied recursively.
158
159 Useful functions to put in this list include:
160 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
161 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
162   :group 'gnus-thread
163   :type '(repeat function))
164
165 (defcustom gnus-simplify-ignored-prefixes nil
166   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  regexp))
170
171 (defcustom gnus-build-sparse-threads nil
172   "*If non-nil, fill in the gaps in threads.
173 If `some', only fill in the gaps that are needed to tie loose threads
174 together.  If `more', fill in all leaf nodes that Gnus can find.  If
175 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
176   :group 'gnus-thread
177   :type '(choice (const :tag "off" nil)
178                  (const some)
179                  (const more)
180                  (sexp :menu-tag "all" t)))
181
182 (defcustom gnus-summary-thread-gathering-function
183   'gnus-gather-threads-by-subject
184   "*Function used for gathering loose threads.
185 There are two pre-defined functions: `gnus-gather-threads-by-subject',
186 which only takes Subjects into consideration; and
187 `gnus-gather-threads-by-references', which compared the References
188 headers of the articles to find matches."
189   :group 'gnus-thread
190   :type '(radio (function-item gnus-gather-threads-by-subject)
191                 (function-item gnus-gather-threads-by-references)
192                 (function :tag "other")))
193
194 (defcustom gnus-summary-same-subject ""
195   "*String indicating that the current article has the same subject as the previous.
196 This variable will only be used if the value of
197 `gnus-summary-make-false-root' is `empty'."
198   :group 'gnus-summary-format
199   :type 'string)
200
201 (defcustom gnus-summary-goto-unread t
202   "*If t, many commands will go to the next unread article.
203 This applies to marking commands as well as other commands that
204 \"naturally\" select the next article, like, for instance, `SPC' at
205 the end of an article.
206
207 If nil, the marking commands do NOT go to the next unread article
208 \(they go to the next article instead).  If `never', commands that
209 usually go to the next unread article, will go to the next article,
210 whether it is read or not."
211   :group 'gnus-summary-marks
212   :link '(custom-manual "(gnus)Setting Marks")
213   :type '(choice (const :tag "off" nil)
214                  (const never)
215                  (sexp :menu-tag "on" t)))
216
217 (defcustom gnus-summary-default-score 0
218   "*Default article score level.
219 All scores generated by the score files will be added to this score.
220 If this variable is nil, scoring will be disabled."
221   :group 'gnus-score-default
222   :type '(choice (const :tag "disable")
223                  integer))
224
225 (defcustom gnus-summary-default-high-score 0
226   "*Default threshold for a high scored article.
227 An article will be highlighted as high scored if its score is greater
228 than this score."
229   :group 'gnus-score-default
230   :type 'integer)
231
232 (defcustom gnus-summary-default-low-score 0
233   "*Default threshold for a low scored article.
234 An article will be highlighted as low scored if its score is smaller
235 than this score."
236   :group 'gnus-score-default
237   :type 'integer)
238
239 (defcustom gnus-summary-zcore-fuzz 0
240   "*Fuzziness factor for the zcore in the summary buffer.
241 Articles with scores closer than this to `gnus-summary-default-score'
242 will not be marked."
243   :group 'gnus-summary-format
244   :type 'integer)
245
246 (defcustom gnus-simplify-subject-fuzzy-regexp nil
247   "*Strings to be removed when doing fuzzy matches.
248 This can either be a regular expression or list of regular expressions
249 that will be removed from subject strings if fuzzy subject
250 simplification is selected."
251   :group 'gnus-thread
252   :type '(repeat regexp))
253
254 (defcustom gnus-show-threads t
255   "*If non-nil, display threads in summary mode."
256   :group 'gnus-thread
257   :type 'boolean)
258
259 (defcustom gnus-thread-hide-subtree nil
260   "*If non-nil, hide all threads initially.
261 This can be a predicate specifier which says which threads to hide.
262 If threads are hidden, you have to run the command
263 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
264 to expose hidden threads."
265   :group 'gnus-thread
266   :type '(radio (sexp :format "Non-nil\n"
267                       :match (lambda (widget value)
268                                (not (or (consp value) (functionp value))))
269                       :value t)
270                 (const nil)
271                 (sexp :tag "Predicate specifier" :size 0)))
272
273 (defcustom gnus-thread-hide-killed t
274   "*If non-nil, hide killed threads automatically."
275   :group 'gnus-thread
276   :type 'boolean)
277
278 (defcustom gnus-thread-ignore-subject t
279   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
280 If nil, articles that have different subjects from their parents will
281 start separate threads."
282   :group 'gnus-thread
283   :type 'boolean)
284
285 (defcustom gnus-thread-operation-ignore-subject t
286   "*If non-nil, subjects will be ignored when doing thread commands.
287 This affects commands like `gnus-summary-kill-thread' and
288 `gnus-summary-lower-thread'.
289
290 If this variable is nil, articles in the same thread with different
291 subjects will not be included in the operation in question.  If this
292 variable is `fuzzy', only articles that have subjects that are fuzzily
293 equal will be included."
294   :group 'gnus-thread
295   :type '(choice (const :tag "off" nil)
296                  (const fuzzy)
297                  (sexp :tag "on" t)))
298
299 (defcustom gnus-thread-indent-level 4
300   "*Number that says how much each sub-thread should be indented."
301   :group 'gnus-thread
302   :type 'integer)
303
304 (defcustom gnus-auto-extend-newsgroup t
305   "*If non-nil, extend newsgroup forward and backward when requested."
306   :group 'gnus-summary-choose
307   :type 'boolean)
308
309 (defcustom gnus-auto-select-first t
310   "*If non-nil, select the article under point.
311 Which article this is is controlled by the `gnus-auto-select-subject'
312 variable.
313
314 If you want to prevent automatic selection of articles in some
315 newsgroups, set the variable to nil in `gnus-select-group-hook'."
316   :group 'gnus-group-select
317   :type '(choice (const :tag "none" nil)
318                  (sexp :menu-tag "first" t)))
319
320 (defcustom gnus-auto-select-subject 'unread
321   "*Says what subject to place under point when entering a group.
322
323 This variable can either be the symbols `first' (place point on the
324 first subject), `unread' (place point on the subject line of the first
325 unread article), `best' (place point on the subject line of the
326 higest-scored article), `unseen' (place point on the subject line of
327 the first unseen article), 'unseen-or-unread' (place point on the subject
328 line of the first unseen article or, if all article have been seen, on the
329 subject line of the first unread article), or a function to be called to
330 place point on some subject line."
331   :group 'gnus-group-select
332   :type '(choice (const best)
333                  (const unread)
334                  (const first)
335                  (const unseen)
336                  (const unseen-or-unread)))
337
338 (defcustom gnus-dont-select-after-jump-to-other-group nil
339   "If non-nil, don't select the first unread article after entering the
340 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
341 it is depend on the value of `gnus-auto-select-first' whether to select
342 or not."
343   :group 'gnus-group-select
344   :type 'boolean)
345
346 (defcustom gnus-auto-select-next t
347   "*If non-nil, offer to go to the next group from the end of the previous.
348 If the value is t and the next newsgroup is empty, Gnus will exit
349 summary mode and go back to group mode.  If the value is neither nil
350 nor t, Gnus will select the following unread newsgroup.  In
351 particular, if the value is the symbol `quietly', the next unread
352 newsgroup will be selected without any confirmation, and if it is
353 `almost-quietly', the next group will be selected without any
354 confirmation if you are located on the last article in the group.
355 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
356 will go to the next group without confirmation."
357   :group 'gnus-summary-maneuvering
358   :type '(choice (const :tag "off" nil)
359                  (const quietly)
360                  (const almost-quietly)
361                  (const slightly-quietly)
362                  (sexp :menu-tag "on" t)))
363
364 (defcustom gnus-auto-select-same nil
365   "*If non-nil, select the next article with the same subject.
366 If there are no more articles with the same subject, go to
367 the first unread article."
368   :group 'gnus-summary-maneuvering
369   :type 'boolean)
370
371 (defcustom gnus-auto-goto-ignores 'unfetched
372   "*Says how to handle unfetched articles when maneuvering.
373
374 This variable can either be the symbols nil (maneuver to any
375 article), `undownloaded' (maneuvering while unplugged ignores articles
376 that have not been fetched), `always-undownloaded' (maneuvering always
377 ignores articles that have not been fetched), `unfetched' (maneuvering
378 ignores articles whose headers have not been fetched).
379
380 NOTE: The list of unfetched articles will always be nil when plugged
381 and, when unplugged, a subset of the undownloaded article list."
382   :group 'gnus-summary-maneuvering
383   :type '(choice (const :tag "None" nil)
384                  (const :tag "Undownloaded when unplugged" undownloaded)
385                  (const :tag "Undownloaded" always-undownloaded)
386                  (const :tag "Unfetched" unfetched)))
387
388 (defcustom gnus-summary-check-current nil
389   "*If non-nil, consider the current article when moving.
390 The \"unread\" movement commands will stay on the same line if the
391 current article is unread."
392   :group 'gnus-summary-maneuvering
393   :type 'boolean)
394
395 (defcustom gnus-auto-center-summary t
396   "*If non-nil, always center the current summary buffer.
397 In particular, if `vertical' do only vertical recentering.  If non-nil
398 and non-`vertical', do both horizontal and vertical recentering."
399   :group 'gnus-summary-maneuvering
400   :type '(choice (const :tag "none" nil)
401                  (const vertical)
402                  (integer :tag "height")
403                  (sexp :menu-tag "both" t)))
404
405 (defvar gnus-auto-center-group t
406   "*If non-nil, always center the group buffer.")
407
408 (defcustom gnus-show-all-headers nil
409   "*If non-nil, don't hide any headers."
410   :group 'gnus-article-hiding
411   :group 'gnus-article-headers
412   :type 'boolean)
413
414 (defcustom gnus-summary-ignore-duplicates nil
415   "*If non-nil, ignore articles with identical Message-ID headers."
416   :group 'gnus-summary
417   :type 'boolean)
418
419 (defcustom gnus-single-article-buffer t
420   "*If non-nil, display all articles in the same buffer.
421 If nil, each group will get its own article buffer."
422   :group 'gnus-article-various
423   :type 'boolean)
424
425 (defcustom gnus-break-pages t
426   "*If non-nil, do page breaking on articles.
427 The page delimiter is specified by the `gnus-page-delimiter'
428 variable."
429   :group 'gnus-article-various
430   :type 'boolean)
431
432 (defcustom gnus-show-mime t
433   "*If non-nil, do mime processing of articles.
434 The articles will simply be fed to the function given by
435 `gnus-article-display-method-for-mime'."
436   :group 'gnus-article-mime
437   :type 'boolean)
438
439 (defcustom gnus-move-split-methods nil
440   "*Variable used to suggest where articles are to be moved to.
441 It uses the same syntax as the `gnus-split-methods' variable.
442 However, whereas `gnus-split-methods' specifies file names as targets,
443 this variable specifies group names."
444   :group 'gnus-summary-mail
445   :type '(repeat (choice (list :value (fun) function)
446                          (cons :value ("" "") regexp (repeat string))
447                          (sexp :value nil))))
448
449 (defcustom gnus-unread-mark ?\ ;;;Whitespace
450   "*Mark used for unread articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-ticked-mark ?!
455   "*Mark used for ticked articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-dormant-mark ??
460   "*Mark used for dormant articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-del-mark ?r
465   "*Mark used for del'd articles."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-read-mark ?R
470   "*Mark used for read articles."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-expirable-mark ?E
475   "*Mark used for expirable articles."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-killed-mark ?K
480   "*Mark used for killed articles."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-spam-mark ?$
485   "*Mark used for spam articles."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-souped-mark ?F
490   "*Mark used for souped articles."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-kill-file-mark ?X
495   "*Mark used for articles killed by kill files."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-low-score-mark ?Y
500   "*Mark used for articles with a low score."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-catchup-mark ?C
505   "*Mark used for articles that are caught up."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-replied-mark ?A
510   "*Mark used for articles that have been replied to."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-forwarded-mark ?F
515   "*Mark used for articles that have been forwarded."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-recent-mark ?N
520   "*Mark used for articles that are recent."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-cached-mark ?*
525   "*Mark used for articles that are in the cache."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-saved-mark ?S
530   "*Mark used for articles that have been saved."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-unseen-mark ?.
535   "*Mark used for articles that haven't been seen."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-no-mark ?\ ;;;Whitespace
540   "*Mark used for articles that have no other secondary mark."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-ancient-mark ?O
545   "*Mark used for ancient articles."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-sparse-mark ?Q
550   "*Mark used for sparsely reffed articles."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-canceled-mark ?G
555   "*Mark used for canceled articles."
556   :group 'gnus-summary-marks
557   :type 'character)
558
559 (defcustom gnus-duplicate-mark ?M
560   "*Mark used for duplicate articles."
561   :group 'gnus-summary-marks
562   :type 'character)
563
564 (defcustom gnus-undownloaded-mark ?-
565   "*Mark used for articles that weren't downloaded."
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-downloaded-mark ?+
570   "*Mark used for articles that were downloaded."
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-downloadable-mark ?%
575   "*Mark used for articles that are to be downloaded."
576   :group 'gnus-summary-marks
577   :type 'character)
578
579 (defcustom gnus-unsendable-mark ?=
580   "*Mark used for articles that won't be sent."
581   :group 'gnus-summary-marks
582   :type 'character)
583
584 (defcustom gnus-score-over-mark ?+
585   "*Score mark used for articles with high scores."
586   :group 'gnus-summary-marks
587   :type 'character)
588
589 (defcustom gnus-score-below-mark ?-
590   "*Score mark used for articles with low scores."
591   :group 'gnus-summary-marks
592   :type 'character)
593
594 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
595   "*There is no thread under the article."
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-not-empty-thread-mark ?=
600   "*There is a thread under the article."
601   :group 'gnus-summary-marks
602   :type 'character)
603
604 (defcustom gnus-view-pseudo-asynchronously nil
605   "*If non-nil, Gnus will view pseudo-articles asynchronously."
606   :group 'gnus-extract-view
607   :type 'boolean)
608
609 (defcustom gnus-auto-expirable-marks
610   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
611         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
612         gnus-souped-mark gnus-duplicate-mark)
613   "*The list of marks converted into expiration if a group is auto-expirable."
614   :version "21.1"
615   :group 'gnus-summary
616   :type '(repeat character))
617
618 (defcustom gnus-inhibit-user-auto-expire t
619   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
620   :version "21.1"
621   :group 'gnus-summary
622   :type 'boolean)
623
624 (defcustom gnus-view-pseudos nil
625   "*If `automatic', pseudo-articles will be viewed automatically.
626 If `not-confirm', pseudos will be viewed automatically, and the user
627 will not be asked to confirm the command."
628   :group 'gnus-extract-view
629   :type '(choice (const :tag "off" nil)
630                  (const automatic)
631                  (const not-confirm)))
632
633 (defcustom gnus-view-pseudos-separately t
634   "*If non-nil, one pseudo-article will be created for each file to be viewed.
635 If nil, all files that use the same viewing command will be given as a
636 list of parameters to that command."
637   :group 'gnus-extract-view
638   :type 'boolean)
639
640 (defcustom gnus-insert-pseudo-articles t
641   "*If non-nil, insert pseudo-articles when decoding articles."
642   :group 'gnus-extract-view
643   :type 'boolean)
644
645 (defcustom gnus-summary-dummy-line-format
646   "   %(:                             :%) %S\n"
647   "*The format specification for the dummy roots in the summary buffer.
648 It works along the same lines as a normal formatting string,
649 with some simple extensions.
650
651 %S  The subject
652
653 General format specifiers can also be used.
654 See `(gnus)Formatting Variables'."
655   :link '(custom-manual "(gnus)Formatting Variables")
656   :group 'gnus-threading
657   :type 'string)
658
659 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
660   "*The format specification for the summary mode line.
661 It works along the same lines as a normal formatting string,
662 with some simple extensions:
663
664 %G  Group name
665 %p  Unprefixed group name
666 %A  Current article number
667 %z  Current article score
668 %V  Gnus version
669 %U  Number of unread articles in the group
670 %e  Number of unselected articles in the group
671 %Z  A string with unread/unselected article counts
672 %g  Shortish group name
673 %S  Subject of the current article
674 %u  User-defined spec
675 %s  Current score file name
676 %d  Number of dormant articles
677 %r  Number of articles that have been marked as read in this session
678 %E  Number of articles expunged by the score files"
679   :group 'gnus-summary-format
680   :type 'string)
681
682 (defcustom gnus-list-identifiers nil
683   "Regexp that matches list identifiers to be removed from subject.
684 This can also be a list of regexps."
685   :version "21.1"
686   :group 'gnus-summary-format
687   :group 'gnus-article-hiding
688   :type '(choice (const :tag "none" nil)
689                  (regexp :value ".*")
690                  (repeat :value (".*") regexp)))
691
692 (defcustom gnus-summary-mark-below 0
693   "*Mark all articles with a score below this variable as read.
694 This variable is local to each summary buffer and usually set by the
695 score file."
696   :group 'gnus-score-default
697   :type 'integer)
698
699 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
700   "*List of functions used for sorting articles in the summary buffer.
701
702 Each function takes two articles and returns non-nil if the first
703 article should be sorted before the other.  If you use more than one
704 function, the primary sort function should be the last.  You should
705 probably always include `gnus-article-sort-by-number' in the list of
706 sorting functions -- preferably first.  Also note that sorting by date
707 is often much slower than sorting by number, and the sorting order is
708 very similar.  (Sorting by date means sorting by the time the message
709 was sent, sorting by number means sorting by arrival time.)
710
711 Ready-made functions include `gnus-article-sort-by-number',
712 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
713 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
714 and `gnus-article-sort-by-score'.
715
716 When threading is turned on, the variable `gnus-thread-sort-functions'
717 controls how articles are sorted."
718   :group 'gnus-summary-sort
719   :type '(repeat (choice (function-item gnus-article-sort-by-number)
720                          (function-item gnus-article-sort-by-author)
721                          (function-item gnus-article-sort-by-subject)
722                          (function-item gnus-article-sort-by-date)
723                          (function-item gnus-article-sort-by-score)
724                          (function-item gnus-article-sort-by-random)
725                          (function :tag "other"))))
726
727 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
728   "*List of functions used for sorting threads in the summary buffer.
729 By default, threads are sorted by article number.
730
731 Each function takes two threads and returns non-nil if the first
732 thread should be sorted before the other.  If you use more than one
733 function, the primary sort function should be the last.  You should
734 probably always include `gnus-thread-sort-by-number' in the list of
735 sorting functions -- preferably first.  Also note that sorting by date
736 is often much slower than sorting by number, and the sorting order is
737 very similar.  (Sorting by date means sorting by the time the message
738 was sent, sorting by number means sorting by arrival time.)
739
740 Ready-made functions include `gnus-thread-sort-by-number',
741 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
742 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
743 `gnus-thread-sort-by-most-recent-number',
744 `gnus-thread-sort-by-most-recent-date',
745 `gnus-thread-sort-by-random', and
746 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
747
748 When threading is turned off, the variable
749 `gnus-article-sort-functions' controls how articles are sorted."
750   :group 'gnus-summary-sort
751   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
752                          (function-item gnus-thread-sort-by-author)
753                          (function-item gnus-thread-sort-by-subject)
754                          (function-item gnus-thread-sort-by-date)
755                          (function-item gnus-thread-sort-by-score)
756                          (function-item gnus-thread-sort-by-total-score)
757                          (function-item gnus-thread-sort-by-random)
758                          (function :tag "other"))))
759
760 (defcustom gnus-thread-score-function '+
761   "*Function used for calculating the total score of a thread.
762
763 The function is called with the scores of the article and each
764 subthread and should then return the score of the thread.
765
766 Some functions you can use are `+', `max', or `min'."
767   :group 'gnus-summary-sort
768   :type 'function)
769
770 (defcustom gnus-summary-expunge-below nil
771   "All articles that have a score less than this variable will be expunged.
772 This variable is local to the summary buffers."
773   :group 'gnus-score-default
774   :type '(choice (const :tag "off" nil)
775                  integer))
776
777 (defcustom gnus-thread-expunge-below nil
778   "All threads that have a total score less than this variable will be expunged.
779 See `gnus-thread-score-function' for en explanation of what a
780 \"thread score\" is.
781
782 This variable is local to the summary buffers."
783   :group 'gnus-threading
784   :group 'gnus-score-default
785   :type '(choice (const :tag "off" nil)
786                  integer))
787
788 (defcustom gnus-summary-mode-hook nil
789   "*A hook for Gnus summary mode.
790 This hook is run before any variables are set in the summary buffer."
791   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
792   :group 'gnus-summary-various
793   :type 'hook)
794
795 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
796 (when (featurep 'xemacs)
797   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
798   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
799   (add-hook 'gnus-summary-mode-hook
800             'gnus-xmas-switch-horizontal-scrollbar-off))
801
802 (defcustom gnus-summary-menu-hook nil
803   "*Hook run after the creation of the summary mode menu."
804   :group 'gnus-summary-visual
805   :type 'hook)
806
807 (defcustom gnus-summary-exit-hook nil
808   "*A hook called on exit from the summary buffer.
809 It will be called with point in the group buffer."
810   :group 'gnus-summary-exit
811   :type 'hook)
812
813 (defcustom gnus-summary-prepare-hook nil
814   "*A hook called after the summary buffer has been generated.
815 If you want to modify the summary buffer, you can use this hook."
816   :group 'gnus-summary-various
817   :type 'hook)
818
819 (defcustom gnus-summary-prepared-hook nil
820   "*A hook called as the last thing after the summary buffer has been generated."
821   :group 'gnus-summary-various
822   :type 'hook)
823
824 (defcustom gnus-summary-generate-hook nil
825   "*A hook run just before generating the summary buffer.
826 This hook is commonly used to customize threading variables and the
827 like."
828   :group 'gnus-summary-various
829   :type 'hook)
830
831 (defcustom gnus-select-group-hook nil
832   "*A hook called when a newsgroup is selected.
833
834 If you'd like to simplify subjects like the
835 `gnus-summary-next-same-subject' command does, you can use the
836 following hook:
837
838  (add-hook gnus-select-group-hook
839            (lambda ()
840              (mapcar (lambda (header)
841                        (mail-header-set-subject
842                         header
843                         (gnus-simplify-subject
844                          (mail-header-subject header) 're-only)))
845                      gnus-newsgroup-headers)))"
846   :group 'gnus-group-select
847   :type 'hook)
848
849 (defcustom gnus-select-article-hook nil
850   "*A hook called when an article is selected."
851   :group 'gnus-summary-choose
852   :options '(gnus-agent-fetch-selected-article)
853   :type 'hook)
854
855 (defcustom gnus-visual-mark-article-hook
856   (list 'gnus-highlight-selected-summary)
857   "*Hook run after selecting an article in the summary buffer.
858 It is meant to be used for highlighting the article in some way.  It
859 is not run if `gnus-visual' is nil."
860   :group 'gnus-summary-visual
861   :type 'hook)
862
863 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
864   "*A hook called before parsing the headers."
865   :group 'gnus-various
866   :type 'hook)
867
868 (defcustom gnus-exit-group-hook nil
869   "*A hook called when exiting summary mode.
870 This hook is not called from the non-updating exit commands like `Q'."
871   :group 'gnus-various
872   :type 'hook)
873
874 (defcustom gnus-summary-update-hook
875   (list 'gnus-summary-highlight-line)
876   "*A hook called when a summary line is changed.
877 The hook will not be called if `gnus-visual' is nil.
878
879 The default function `gnus-summary-highlight-line' will
880 highlight the line according to the `gnus-summary-highlight'
881 variable."
882   :group 'gnus-summary-visual
883   :type 'hook)
884
885 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
886   "*A hook called when an article is selected for the first time.
887 The hook is intended to mark an article as read (or unread)
888 automatically when it is selected."
889   :group 'gnus-summary-choose
890   :type 'hook)
891
892 (defcustom gnus-group-no-more-groups-hook nil
893   "*A hook run when returning to group mode having no more (unread) groups."
894   :group 'gnus-group-select
895   :type 'hook)
896
897 (defcustom gnus-ps-print-hook nil
898   "*A hook run before ps-printing something from Gnus."
899   :group 'gnus-summary
900   :type 'hook)
901
902 (defcustom gnus-summary-article-move-hook nil
903   "*A hook called after an article is moved, copied, respooled, or crossposted."
904   :group 'gnus-summary
905   :type 'hook)
906
907 (defcustom gnus-summary-article-delete-hook nil
908   "*A hook called after an article is deleted."
909   :group 'gnus-summary
910   :type 'hook)
911
912 (defcustom gnus-summary-article-expire-hook nil
913   "*A hook called after an article is expired."
914   :group 'gnus-summary
915   :type 'hook)
916
917 (defcustom gnus-summary-display-arrow
918   (and (fboundp 'display-graphic-p)
919        (display-graphic-p))
920   "*If non-nil, display an arrow highlighting the current article."
921   :version "21.1"
922   :group 'gnus-summary
923   :type 'boolean)
924
925 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
926   "Face used for highlighting the current article in the summary buffer."
927   :group 'gnus-summary-visual
928   :type 'face)
929
930 (defvar gnus-tmp-downloaded nil)
931
932 (defcustom gnus-summary-highlight
933   '(((eq mark gnus-canceled-mark)
934      . gnus-summary-cancelled-face)
935     ((and uncached (> score default-high))
936      . gnus-summary-high-undownloaded-face)
937     ((and uncached (< score default-low))
938      . gnus-summary-low-undownloaded-face)
939     (uncached
940      . gnus-summary-normal-undownloaded-face)
941     ((and (> score default-high)
942           (or (eq mark gnus-dormant-mark)
943               (eq mark gnus-ticked-mark)))
944      . gnus-summary-high-ticked-face)
945     ((and (< score default-low)
946           (or (eq mark gnus-dormant-mark)
947               (eq mark gnus-ticked-mark)))
948      . gnus-summary-low-ticked-face)
949     ((or (eq mark gnus-dormant-mark)
950          (eq mark gnus-ticked-mark))
951      . gnus-summary-normal-ticked-face)
952     ((and (> score default-high) (eq mark gnus-ancient-mark))
953      . gnus-summary-high-ancient-face)
954     ((and (< score default-low) (eq mark gnus-ancient-mark))
955      . gnus-summary-low-ancient-face)
956     ((eq mark gnus-ancient-mark)
957      . gnus-summary-normal-ancient-face)
958     ((and (> score default-high) (eq mark gnus-unread-mark))
959      . gnus-summary-high-unread-face)
960     ((and (< score default-low) (eq mark gnus-unread-mark))
961      . gnus-summary-low-unread-face)
962     ((eq mark gnus-unread-mark)
963      . gnus-summary-normal-unread-face)
964     ((> score default-high)
965      . gnus-summary-high-read-face)
966     ((< score default-low)
967      . gnus-summary-low-read-face)
968     (t
969      . gnus-summary-normal-read-face))
970   "*Controls the highlighting of summary buffer lines.
971
972 A list of (FORM . FACE) pairs.  When deciding how a a particular
973 summary line should be displayed, each form is evaluated.  The content
974 of the face field after the first true form is used.  You can change
975 how those summary lines are displayed, by editing the face field.
976
977 You can use the following variables in the FORM field.
978
979 score:        The article's score
980 default:      The default article score.
981 default-high: The default score for high scored articles.
982 default-low:  The default score for low scored articles.
983 below:        The score below which articles are automatically marked as read.
984 mark:         The article's mark.
985 uncached:     Non-nil if the article is uncached."
986   :group 'gnus-summary-visual
987   :type '(repeat (cons (sexp :tag "Form" nil)
988                        face)))
989
990 (defcustom gnus-alter-header-function nil
991   "Function called to allow alteration of article header structures.
992 The function is called with one parameter, the article header vector,
993 which it may alter in any way."
994   :type '(choice (const :tag "None" nil)
995                  function)
996   :group 'gnus-summary)
997
998 (defvar gnus-decode-encoded-word-function
999   (mime-find-field-decoder 'From 'nov)
1000   "Variable that says which function should be used to decode a string with encoded words.")
1001
1002 (defcustom gnus-extra-headers '(To Newsgroups)
1003   "*Extra headers to parse."
1004   :version "21.1"
1005   :group 'gnus-summary
1006   :type '(repeat symbol))
1007
1008 (defcustom gnus-ignored-from-addresses
1009   (and user-mail-address (regexp-quote user-mail-address))
1010   "*Regexp of From headers that may be suppressed in favor of To headers."
1011   :version "21.1"
1012   :group 'gnus-summary
1013   :type 'regexp)
1014
1015 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1016   "List of charsets that should be ignored.
1017 When these charsets are used in the \"charset\" parameter, the
1018 default charset will be used instead."
1019   :version "21.1"
1020   :type '(repeat symbol)
1021   :group 'gnus-charset)
1022
1023 (gnus-define-group-parameter
1024  ignored-charsets
1025  :type list
1026  :function-document
1027  "Return the ignored charsets of GROUP."
1028  :variable gnus-group-ignored-charsets-alist
1029  :variable-default
1030  '(("alt\\.chinese\\.text" iso-8859-1))
1031  :variable-document
1032  "Alist of regexps (to match group names) and charsets that should be ignored.
1033 When these charsets are used in the \"charset\" parameter, the
1034 default charset will be used instead."
1035  :variable-group gnus-charset
1036  :variable-type '(repeat (cons (regexp :tag "Group")
1037                                (repeat symbol)))
1038  :parameter-type '(choice :tag "Ignored charsets"
1039                           :value nil
1040                           (repeat (symbol)))
1041  :parameter-document       "\
1042 List of charsets that should be ignored.
1043
1044 When these charsets are used in the \"charset\" parameter, the
1045 default charset will be used instead.")
1046
1047 (defcustom gnus-group-highlight-words-alist nil
1048   "Alist of group regexps and highlight regexps.
1049 This variable uses the same syntax as `gnus-emphasis-alist'."
1050   :version "21.1"
1051   :type '(repeat (cons (regexp :tag "Group")
1052                        (repeat (list (regexp :tag "Highlight regexp")
1053                                      (number :tag "Group for entire word" 0)
1054                                      (number :tag "Group for displayed part" 0)
1055                                      (symbol :tag "Face"
1056                                              gnus-emphasis-highlight-words)))))
1057   :group 'gnus-summary-visual)
1058
1059 (defcustom gnus-use-wheel nil
1060   "Use Intelli-mouse on summary movement"
1061   :type 'boolean
1062   :group 'gnus-summary-maneuvering)
1063
1064 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1065   "Amount to scroll messages by spinning the mouse wheel.
1066 This is actually a cons cell, where the first item is the amount to scroll
1067 on a normal wheel event, and the second is the amount to scroll when the
1068 wheel is moved with the shift key depressed."
1069   :type '(cons (integer :tag "Shift") integer)
1070   :group 'gnus-summary-maneuvering)
1071
1072 (defcustom gnus-wheel-edge-resistance 2
1073   "How hard it should be to change the current article
1074 by moving the mouse over the edge of the article window."
1075   :type 'integer
1076   :group 'gnus-summary-maneuvering)
1077
1078 (defcustom gnus-summary-show-article-charset-alist
1079   nil
1080   "Alist of number and charset.
1081 The article will be shown with the charset corresponding to the
1082 numbered argument.
1083 For example: ((1 . cn-gb-2312) (2 . big5))."
1084   :version "21.1"
1085   :type '(repeat (cons (number :tag "Argument" 1)
1086                        (symbol :tag "Charset")))
1087   :group 'gnus-charset)
1088
1089 (defcustom gnus-preserve-marks t
1090   "Whether marks are preserved when moving, copying and respooling messages."
1091   :version "21.1"
1092   :type 'boolean
1093   :group 'gnus-summary-marks)
1094
1095 (defcustom gnus-alter-articles-to-read-function nil
1096   "Function to be called to alter the list of articles to be selected."
1097   :type '(choice (const nil) function)
1098   :group 'gnus-summary)
1099
1100 (defcustom gnus-orphan-score nil
1101   "*All orphans get this score added.  Set in the score file."
1102   :group 'gnus-score-default
1103   :type '(choice (const nil)
1104                  integer))
1105
1106 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1107   "*A regexp to match MIME parts when saving multiple parts of a
1108 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1109 This regexp will be used by default when prompting the user for which
1110 type of files to save."
1111   :group 'gnus-summary
1112   :type 'regexp)
1113
1114 (defcustom gnus-read-all-available-headers nil
1115   "Whether Gnus should parse all headers made available to it.
1116 This is mostly relevant for slow back ends where the user may
1117 wish to widen the summary buffer to include all headers
1118 that were fetched.  Say, for nnultimate groups."
1119   :group 'gnus-summary
1120   :type '(choice boolean regexp))
1121
1122 (defcustom gnus-summary-muttprint-program "muttprint"
1123   "Command (and optional arguments) used to run Muttprint."
1124   :version "21.3"
1125   :group 'gnus-summary
1126   :type 'string)
1127
1128 (defcustom gnus-article-loose-mime nil
1129   "If non-nil, don't require MIME-Version header.
1130 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1131 supply the MIME-Version header or deliberately strip it From the mail.
1132 Set it to non-nil, Gnus will treat some articles as MIME even if
1133 the MIME-Version header is missed."
1134   :version "21.3"
1135   :type 'boolean
1136   :group 'gnus-article-mime)
1137
1138 (defcustom gnus-article-emulate-mime t
1139   "If non-nil, use MIME emulation for uuencode and the like.
1140 This means that Gnus will search message bodies for text that look
1141 like uuencoded bits, yEncoded bits, and so on, and present that using
1142 the normal Gnus MIME machinery."
1143   :type 'boolean
1144   :group 'gnus-article-mime)
1145
1146 ;;; Internal variables
1147
1148 (defvar gnus-summary-display-cache nil)
1149 (defvar gnus-article-mime-handles nil)
1150 (defvar gnus-article-decoded-p nil)
1151 (defvar gnus-article-charset nil)
1152 (defvar gnus-article-ignored-charsets nil)
1153 (defvar gnus-scores-exclude-files nil)
1154 (defvar gnus-page-broken nil)
1155
1156 (defvar gnus-original-article nil)
1157 (defvar gnus-article-internal-prepare-hook nil)
1158 (defvar gnus-newsgroup-process-stack nil)
1159
1160 (defvar gnus-thread-indent-array nil)
1161 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1162 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1163   "Function called to sort the articles within a thread after it has been gathered together.")
1164
1165 (defvar gnus-summary-save-parts-type-history nil)
1166 (defvar gnus-summary-save-parts-last-directory nil)
1167
1168 ;; Avoid highlighting in kill files.
1169 (defvar gnus-summary-inhibit-highlight nil)
1170 (defvar gnus-newsgroup-selected-overlay nil)
1171 (defvar gnus-inhibit-limiting nil)
1172 (defvar gnus-newsgroup-adaptive-score-file nil)
1173 (defvar gnus-current-score-file nil)
1174 (defvar gnus-current-move-group nil)
1175 (defvar gnus-current-copy-group nil)
1176 (defvar gnus-current-crosspost-group nil)
1177 (defvar gnus-newsgroup-display nil)
1178
1179 (defvar gnus-newsgroup-dependencies nil)
1180 (defvar gnus-newsgroup-adaptive nil)
1181 (defvar gnus-summary-display-article-function nil)
1182 (defvar gnus-summary-highlight-line-function nil
1183   "Function called after highlighting a summary line.")
1184
1185 (defvar gnus-summary-line-format-alist
1186   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1187     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1188     (?s gnus-tmp-subject-or-nil ?s)
1189     (?n gnus-tmp-name ?s)
1190     (?A (std11-address-string
1191          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1192     (?a (or (std11-full-name-string
1193              (car (mime-entity-read-field gnus-tmp-header 'From)))
1194             gnus-tmp-from) ?s)
1195     (?F gnus-tmp-from ?s)
1196     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1197     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1198     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1199     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1200     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1201     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1202     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1203     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1204     (?L gnus-tmp-lines ?s)
1205     (?O gnus-tmp-downloaded ?c)
1206     (?I gnus-tmp-indentation ?s)
1207     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1208     (?R gnus-tmp-replied ?c)
1209     (?\[ gnus-tmp-opening-bracket ?c)
1210     (?\] gnus-tmp-closing-bracket ?c)
1211     (?\> (make-string gnus-tmp-level ? ) ?s)
1212     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1213     (?i gnus-tmp-score ?d)
1214     (?z gnus-tmp-score-char ?c)
1215     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1216     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1217     (?U gnus-tmp-unread ?c)
1218     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1219         ?s)
1220     (?t (gnus-summary-number-of-articles-in-thread
1221          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1222         ?d)
1223     (?e (gnus-summary-number-of-articles-in-thread
1224          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1225         ?c)
1226     (?u gnus-tmp-user-defined ?s)
1227     (?P (gnus-pick-line-number) ?d)
1228     (?B gnus-tmp-thread-tree-header-string ?s)
1229     (user-date (gnus-user-date
1230                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1231   "An alist of format specifications that can appear in summary lines.
1232 These are paired with what variables they correspond with, along with
1233 the type of the variable (string, integer, character, etc).")
1234
1235 (defvar gnus-summary-dummy-line-format-alist
1236   `((?S gnus-tmp-subject ?s)
1237     (?N gnus-tmp-number ?d)
1238     (?u gnus-tmp-user-defined ?s)))
1239
1240 (defvar gnus-summary-mode-line-format-alist
1241   `((?G gnus-tmp-group-name ?s)
1242     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1243     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1244     (?A gnus-tmp-article-number ?d)
1245     (?Z gnus-tmp-unread-and-unselected ?s)
1246     (?V gnus-version ?s)
1247     (?U gnus-tmp-unread-and-unticked ?d)
1248     (?S gnus-tmp-subject ?s)
1249     (?e gnus-tmp-unselected ?d)
1250     (?u gnus-tmp-user-defined ?s)
1251     (?d (length gnus-newsgroup-dormant) ?d)
1252     (?t (length gnus-newsgroup-marked) ?d)
1253     (?h (length gnus-newsgroup-spam-marked) ?d)
1254     (?r (length gnus-newsgroup-reads) ?d)
1255     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1256     (?E gnus-newsgroup-expunged-tally ?d)
1257     (?s (gnus-current-score-file-nondirectory) ?s)))
1258
1259 (defvar gnus-last-search-regexp nil
1260   "Default regexp for article search command.")
1261
1262 (defvar gnus-summary-search-article-matched-data nil
1263   "Last matched data of article search command.  It is the local variable
1264 in `gnus-article-buffer' which consists of the list of start position,
1265 end position and text.")
1266
1267 (defvar gnus-last-shell-command nil
1268   "Default shell command on article.")
1269
1270 (defvar gnus-newsgroup-agentized nil
1271   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1272 (defvar gnus-newsgroup-begin nil)
1273 (defvar gnus-newsgroup-end nil)
1274 (defvar gnus-newsgroup-last-rmail nil)
1275 (defvar gnus-newsgroup-last-mail nil)
1276 (defvar gnus-newsgroup-last-folder nil)
1277 (defvar gnus-newsgroup-last-file nil)
1278 (defvar gnus-newsgroup-auto-expire nil)
1279 (defvar gnus-newsgroup-active nil)
1280
1281 (defvar gnus-newsgroup-data nil)
1282 (defvar gnus-newsgroup-data-reverse nil)
1283 (defvar gnus-newsgroup-limit nil)
1284 (defvar gnus-newsgroup-limits nil)
1285 (defvar gnus-summary-use-undownloaded-faces nil)
1286
1287 (defvar gnus-newsgroup-unreads nil
1288   "Sorted list of unread articles in the current newsgroup.")
1289
1290 (defvar gnus-newsgroup-unselected nil
1291   "Sorted list of unselected unread articles in the current newsgroup.")
1292
1293 (defvar gnus-newsgroup-reads nil
1294   "Alist of read articles and article marks in the current newsgroup.")
1295
1296 (defvar gnus-newsgroup-expunged-tally nil)
1297
1298 (defvar gnus-newsgroup-marked nil
1299   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1300
1301 (defvar gnus-newsgroup-spam-marked nil
1302   "List of ranges of articles that have been marked as spam.")
1303
1304 (defvar gnus-newsgroup-killed nil
1305   "List of ranges of articles that have been through the scoring process.")
1306
1307 (defvar gnus-newsgroup-cached nil
1308   "Sorted list of articles that come from the article cache.")
1309
1310 (defvar gnus-newsgroup-saved nil
1311   "List of articles that have been saved.")
1312
1313 (defvar gnus-newsgroup-kill-headers nil)
1314
1315 (defvar gnus-newsgroup-replied nil
1316   "List of articles that have been replied to in the current newsgroup.")
1317
1318 (defvar gnus-newsgroup-forwarded nil
1319   "List of articles that have been forwarded in the current newsgroup.")
1320
1321 (defvar gnus-newsgroup-recent nil
1322   "List of articles that have are recent in the current newsgroup.")
1323
1324 (defvar gnus-newsgroup-expirable nil
1325   "Sorted list of articles in the current newsgroup that can be expired.")
1326
1327 (defvar gnus-newsgroup-processable nil
1328   "List of articles in the current newsgroup that can be processed.")
1329
1330 (defvar gnus-newsgroup-downloadable nil
1331   "Sorted list of articles in the current newsgroup that can be processed.")
1332
1333 (defvar gnus-newsgroup-unfetched nil
1334   "Sorted list of articles in the current newsgroup whose headers have
1335 not been fetched into the agent.
1336
1337 This list will always be a subset of gnus-newsgroup-undownloaded.")
1338
1339 (defvar gnus-newsgroup-undownloaded nil
1340   "List of articles in the current newsgroup that haven't been downloaded.")
1341
1342 (defvar gnus-newsgroup-unsendable nil
1343   "List of articles in the current newsgroup that won't be sent.")
1344
1345 (defvar gnus-newsgroup-bookmarks nil
1346   "List of articles in the current newsgroup that have bookmarks.")
1347
1348 (defvar gnus-newsgroup-dormant nil
1349   "Sorted list of dormant articles in the current newsgroup.")
1350
1351 (defvar gnus-newsgroup-unseen nil
1352   "List of unseen articles in the current newsgroup.")
1353
1354 (defvar gnus-newsgroup-seen nil
1355   "Range of seen articles in the current newsgroup.")
1356
1357 (defvar gnus-newsgroup-articles nil
1358   "List of articles in the current newsgroup.")
1359
1360 (defvar gnus-newsgroup-scored nil
1361   "List of scored articles in the current newsgroup.")
1362
1363 (defvar gnus-newsgroup-incorporated nil
1364   "List of incorporated articles in the current newsgroup.")
1365
1366 (defvar gnus-newsgroup-headers nil
1367   "List of article headers in the current newsgroup.")
1368
1369 (defvar gnus-newsgroup-threads nil)
1370
1371 (defvar gnus-newsgroup-prepared nil
1372   "Whether the current group has been prepared properly.")
1373
1374 (defvar gnus-newsgroup-ancient nil
1375   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1376
1377 (defvar gnus-newsgroup-sparse nil)
1378
1379 (defvar gnus-current-article nil)
1380 (defvar gnus-article-current nil)
1381 (defvar gnus-current-headers nil)
1382 (defvar gnus-have-all-headers nil)
1383 (defvar gnus-last-article nil)
1384 (defvar gnus-newsgroup-history nil)
1385 (defvar gnus-newsgroup-charset nil)
1386 (defvar gnus-newsgroup-ephemeral-charset nil)
1387 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1388
1389 (defvar gnus-article-before-search nil)
1390
1391 (defvar gnus-summary-local-variables
1392   '(gnus-newsgroup-name
1393     gnus-newsgroup-begin gnus-newsgroup-end
1394     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1395     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1396     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1397     gnus-newsgroup-unselected gnus-newsgroup-marked
1398     gnus-newsgroup-spam-marked
1399     gnus-newsgroup-reads gnus-newsgroup-saved
1400     gnus-newsgroup-replied gnus-newsgroup-forwarded
1401     gnus-newsgroup-recent
1402     gnus-newsgroup-expirable
1403     gnus-newsgroup-processable gnus-newsgroup-killed
1404     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1405     gnus-newsgroup-unfetched
1406     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1407     gnus-newsgroup-seen gnus-newsgroup-articles
1408     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1409     gnus-newsgroup-headers gnus-newsgroup-threads
1410     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1411     gnus-current-article gnus-current-headers gnus-have-all-headers
1412     gnus-last-article gnus-article-internal-prepare-hook
1413     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1414     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1415     gnus-thread-expunge-below
1416     gnus-score-alist gnus-current-score-file
1417     (gnus-summary-expunge-below . global)
1418     (gnus-summary-mark-below . global)
1419     (gnus-orphan-score . global)
1420     gnus-newsgroup-active gnus-scores-exclude-files
1421     gnus-newsgroup-history gnus-newsgroup-ancient
1422     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1423     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1424     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1425     (gnus-newsgroup-expunged-tally . 0)
1426     gnus-cache-removable-articles gnus-newsgroup-cached
1427     gnus-newsgroup-data gnus-newsgroup-data-reverse
1428     gnus-newsgroup-limit gnus-newsgroup-limits
1429     gnus-newsgroup-charset gnus-newsgroup-display
1430     gnus-summary-use-undownloaded-faces
1431     gnus-newsgroup-incorporated)
1432   "Variables that are buffer-local to the summary buffers.")
1433
1434 (defvar gnus-newsgroup-variables nil
1435   "A list of variables that have separate values in different newsgroups.
1436 A list of newsgroup (summary buffer) local variables, or cons of
1437 variables and their default values (when the default values are not
1438 nil), that should be made global while the summary buffer is active.
1439 These variables can be used to set variables in the group parameters
1440 while still allowing them to affect operations done in other
1441 buffers. For example:
1442
1443 \(setq gnus-newsgroup-variables
1444      '(message-use-followup-to
1445        (gnus-visible-headers .
1446          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1447 ")
1448
1449 ;; Byte-compiler warning.
1450 (eval-when-compile
1451   ;; Bind features so that require will believe that gnus-sum has
1452   ;; already been loaded (avoids infinite recursion)
1453   (let ((features (cons 'gnus-sum features)))
1454     ;; Several of the declarations in gnus-sum are needed to load the
1455     ;; following files. Right now, these definitions have been
1456     ;; compiled but not defined (evaluated).  We could either do a
1457     ;; eval-and-compile about all of the declarations or evaluate the
1458     ;; source file.
1459     (if (boundp 'gnus-newsgroup-variables)
1460         nil
1461       (load "gnus-sum.el" t t t))
1462     (require 'gnus)
1463     (require 'gnus-agent)
1464     (require 'gnus-art)))
1465
1466 ;; Subject simplification.
1467
1468 (defun gnus-simplify-whitespace (str)
1469   "Remove excessive whitespace from STR."
1470   ;; Multiple spaces.
1471   (while (string-match "[ \t][ \t]+" str)
1472     (setq str (concat (substring str 0 (match-beginning 0))
1473                         " "
1474                         (substring str (match-end 0)))))
1475   ;; Leading spaces.
1476   (when (string-match "^[ \t]+" str)
1477     (setq str (substring str (match-end 0))))
1478   ;; Trailing spaces.
1479   (when (string-match "[ \t]+$" str)
1480     (setq str (substring str 0 (match-beginning 0))))
1481   str)
1482
1483 (defun gnus-simplify-all-whitespace (str)
1484   "Remove all whitespace from STR."
1485   (while (string-match "[ \t\n]+" str)
1486     (setq str (replace-match "" nil nil str)))
1487   str)
1488
1489 (defsubst gnus-simplify-subject-re (subject)
1490   "Remove \"Re:\" from subject lines."
1491   (if (string-match message-subject-re-regexp subject)
1492       (substring subject (match-end 0))
1493     subject))
1494
1495 (defun gnus-simplify-subject (subject &optional re-only)
1496   "Remove `Re:' and words in parentheses.
1497 If RE-ONLY is non-nil, strip leading `Re:'s only."
1498   (let ((case-fold-search t))           ;Ignore case.
1499     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1500     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1501       (setq subject (substring subject (match-end 0))))
1502     ;; Remove uninteresting prefixes.
1503     (when (and (not re-only)
1504                gnus-simplify-ignored-prefixes
1505                (string-match gnus-simplify-ignored-prefixes subject))
1506       (setq subject (substring subject (match-end 0))))
1507     ;; Remove words in parentheses from end.
1508     (unless re-only
1509       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1510         (setq subject (substring subject 0 (match-beginning 0)))))
1511     ;; Return subject string.
1512     subject))
1513
1514 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1515 ;; all whitespace.
1516 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1517   (goto-char (point-min))
1518   (while (re-search-forward regexp nil t)
1519     (replace-match (or newtext ""))))
1520
1521 (defun gnus-simplify-buffer-fuzzy ()
1522   "Simplify string in the buffer fuzzily.
1523 The string in the accessible portion of the current buffer is simplified.
1524 It is assumed to be a single-line subject.
1525 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1526 matter is removed.  Additional things can be deleted by setting
1527 `gnus-simplify-subject-fuzzy-regexp'."
1528   (let ((case-fold-search t)
1529         (modified-tick))
1530     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1531
1532     (while (not (eq modified-tick (buffer-modified-tick)))
1533       (setq modified-tick (buffer-modified-tick))
1534       (cond
1535        ((listp gnus-simplify-subject-fuzzy-regexp)
1536         (mapcar 'gnus-simplify-buffer-fuzzy-step
1537                 gnus-simplify-subject-fuzzy-regexp))
1538        (gnus-simplify-subject-fuzzy-regexp
1539         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1540       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1541       (gnus-simplify-buffer-fuzzy-step
1542        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1543       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1544
1545     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1546     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1547     (gnus-simplify-buffer-fuzzy-step " $")
1548     (gnus-simplify-buffer-fuzzy-step "^ +")))
1549
1550 (defun gnus-simplify-subject-fuzzy (subject)
1551   "Simplify a subject string fuzzily.
1552 See `gnus-simplify-buffer-fuzzy' for details."
1553   (save-excursion
1554     (gnus-set-work-buffer)
1555     (let ((case-fold-search t))
1556       ;; Remove uninteresting prefixes.
1557       (when (and gnus-simplify-ignored-prefixes
1558                  (string-match gnus-simplify-ignored-prefixes subject))
1559         (setq subject (substring subject (match-end 0))))
1560       (insert subject)
1561       (inline (gnus-simplify-buffer-fuzzy))
1562       (buffer-string))))
1563
1564 (defsubst gnus-simplify-subject-fully (subject)
1565   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1566   (cond
1567    (gnus-simplify-subject-functions
1568     (gnus-map-function gnus-simplify-subject-functions subject))
1569    ((null gnus-summary-gather-subject-limit)
1570     (gnus-simplify-subject-re subject))
1571    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1572     (gnus-simplify-subject-fuzzy subject))
1573    ((numberp gnus-summary-gather-subject-limit)
1574     (gnus-limit-string (gnus-simplify-subject-re subject)
1575                        gnus-summary-gather-subject-limit))
1576    (t
1577     subject)))
1578
1579 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1580   "Check whether two subjects are equal.
1581 If optional argument SIMPLE-FIRST is t, first argument is already
1582 simplified."
1583   (cond
1584    ((null simple-first)
1585     (equal (gnus-simplify-subject-fully s1)
1586            (gnus-simplify-subject-fully s2)))
1587    (t
1588     (equal s1
1589            (gnus-simplify-subject-fully s2)))))
1590
1591 (defun gnus-summary-bubble-group ()
1592   "Increase the score of the current group.
1593 This is a handy function to add to `gnus-summary-exit-hook' to
1594 increase the score of each group you read."
1595   (gnus-group-add-score gnus-newsgroup-name))
1596
1597 \f
1598 ;;;
1599 ;;; Gnus summary mode
1600 ;;;
1601
1602 (put 'gnus-summary-mode 'mode-class 'special)
1603
1604 (defvar gnus-article-commands-menu)
1605
1606 ;; Non-orthogonal keys
1607
1608 (gnus-define-keys gnus-summary-mode-map
1609   " " gnus-summary-next-page
1610   "\177" gnus-summary-prev-page
1611   [delete] gnus-summary-prev-page
1612   [backspace] gnus-summary-prev-page
1613   "\r" gnus-summary-scroll-up
1614   "\M-\r" gnus-summary-scroll-down
1615   "n" gnus-summary-next-unread-article
1616   "p" gnus-summary-prev-unread-article
1617   "N" gnus-summary-next-article
1618   "P" gnus-summary-prev-article
1619   "\M-\C-n" gnus-summary-next-same-subject
1620   "\M-\C-p" gnus-summary-prev-same-subject
1621   "\M-n" gnus-summary-next-unread-subject
1622   "\M-p" gnus-summary-prev-unread-subject
1623   "." gnus-summary-first-unread-article
1624   "," gnus-summary-best-unread-article
1625   "\M-s" gnus-summary-search-article-forward
1626   "\M-r" gnus-summary-search-article-backward
1627   "<" gnus-summary-beginning-of-article
1628   ">" gnus-summary-end-of-article
1629   "j" gnus-summary-goto-article
1630   "^" gnus-summary-refer-parent-article
1631   "\M-^" gnus-summary-refer-article
1632   "u" gnus-summary-tick-article-forward
1633   "!" gnus-summary-tick-article-forward
1634   "U" gnus-summary-tick-article-backward
1635   "d" gnus-summary-mark-as-read-forward
1636   "D" gnus-summary-mark-as-read-backward
1637   "E" gnus-summary-mark-as-expirable
1638   "\M-u" gnus-summary-clear-mark-forward
1639   "\M-U" gnus-summary-clear-mark-backward
1640   "k" gnus-summary-kill-same-subject-and-select
1641   "\C-k" gnus-summary-kill-same-subject
1642   "\M-\C-k" gnus-summary-kill-thread
1643   "\M-\C-l" gnus-summary-lower-thread
1644   "e" gnus-summary-edit-article
1645   "#" gnus-summary-mark-as-processable
1646   "\M-#" gnus-summary-unmark-as-processable
1647   "\M-\C-t" gnus-summary-toggle-threads
1648   "\M-\C-s" gnus-summary-show-thread
1649   "\M-\C-h" gnus-summary-hide-thread
1650   "\M-\C-f" gnus-summary-next-thread
1651   "\M-\C-b" gnus-summary-prev-thread
1652   [(meta down)] gnus-summary-next-thread
1653   [(meta up)] gnus-summary-prev-thread
1654   "\M-\C-u" gnus-summary-up-thread
1655   "\M-\C-d" gnus-summary-down-thread
1656   "&" gnus-summary-execute-command
1657   "c" gnus-summary-catchup-and-exit
1658   "\C-w" gnus-summary-mark-region-as-read
1659   "\C-t" gnus-summary-toggle-truncation
1660   "?" gnus-summary-mark-as-dormant
1661   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1662   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1663   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1664   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1665   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1666   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1667   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1668   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1669   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1670   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1671   "=" gnus-summary-expand-window
1672   "\C-x\C-s" gnus-summary-reselect-current-group
1673   "\M-g" gnus-summary-rescan-group
1674   "w" gnus-summary-stop-page-breaking
1675   "\C-c\C-r" gnus-summary-caesar-message
1676   "\M-t" gnus-summary-toggle-mime
1677   "f" gnus-summary-followup
1678   "F" gnus-summary-followup-with-original
1679   "C" gnus-summary-cancel-article
1680   "r" gnus-summary-reply
1681   "R" gnus-summary-reply-with-original
1682   "\C-c\C-f" gnus-summary-mail-forward
1683   "o" gnus-summary-save-article
1684   "\C-o" gnus-summary-save-article-mail
1685   "|" gnus-summary-pipe-output
1686   "\M-k" gnus-summary-edit-local-kill
1687   "\M-K" gnus-summary-edit-global-kill
1688   ;; "V" gnus-version
1689   "\C-c\C-d" gnus-summary-describe-group
1690   "q" gnus-summary-exit
1691   "Q" gnus-summary-exit-no-update
1692   "\C-c\C-i" gnus-info-find-node
1693   gnus-mouse-2 gnus-mouse-pick-article
1694   "m" gnus-summary-mail-other-window
1695   "a" gnus-summary-post-news
1696   "i" gnus-summary-news-other-window
1697   "x" gnus-summary-limit-to-unread
1698   "s" gnus-summary-isearch-article
1699   "t" gnus-summary-toggle-header
1700   "g" gnus-summary-show-article
1701   "l" gnus-summary-goto-last-article
1702   "v" gnus-summary-preview-mime-message
1703   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1704   "\C-d" gnus-summary-enter-digest-group
1705   "\M-\C-d" gnus-summary-read-document
1706   "\M-\C-e" gnus-summary-edit-parameters
1707   "\M-\C-a" gnus-summary-customize-parameters
1708   "\C-c\C-b" gnus-bug
1709   "\C-c\C-n" gnus-namazu-search
1710   "*" gnus-cache-enter-article
1711   "\M-*" gnus-cache-remove-article
1712   "\M-&" gnus-summary-universal-argument
1713   "\C-l" gnus-recenter
1714   "I" gnus-summary-increase-score
1715   "L" gnus-summary-lower-score
1716   "\M-i" gnus-symbolic-argument
1717   "h" gnus-summary-select-article-buffer
1718
1719   "V" gnus-summary-score-map
1720   "X" gnus-uu-extract-map
1721   "S" gnus-summary-send-map)
1722
1723   ;; Sort of orthogonal keymap
1724 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1725   "t" gnus-summary-tick-article-forward
1726   "!" gnus-summary-tick-article-forward
1727   "d" gnus-summary-mark-as-read-forward
1728   "r" gnus-summary-mark-as-read-forward
1729   "c" gnus-summary-clear-mark-forward
1730   " " gnus-summary-clear-mark-forward
1731   "e" gnus-summary-mark-as-expirable
1732   "x" gnus-summary-mark-as-expirable
1733   "?" gnus-summary-mark-as-dormant
1734   "b" gnus-summary-set-bookmark
1735   "B" gnus-summary-remove-bookmark
1736   "#" gnus-summary-mark-as-processable
1737   "\M-#" gnus-summary-unmark-as-processable
1738   "S" gnus-summary-limit-include-expunged
1739   "C" gnus-summary-catchup
1740   "H" gnus-summary-catchup-to-here
1741   "h" gnus-summary-catchup-from-here
1742   "\C-c" gnus-summary-catchup-all
1743   "k" gnus-summary-kill-same-subject-and-select
1744   "K" gnus-summary-kill-same-subject
1745   "P" gnus-uu-mark-map)
1746
1747 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1748   "c" gnus-summary-clear-above
1749   "u" gnus-summary-tick-above
1750   "m" gnus-summary-mark-above
1751   "k" gnus-summary-kill-below)
1752
1753 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1754   "/" gnus-summary-limit-to-subject
1755   "n" gnus-summary-limit-to-articles
1756   "w" gnus-summary-pop-limit
1757   "s" gnus-summary-limit-to-subject
1758   "a" gnus-summary-limit-to-author
1759   "u" gnus-summary-limit-to-unread
1760   "m" gnus-summary-limit-to-marks
1761   "M" gnus-summary-limit-exclude-marks
1762   "v" gnus-summary-limit-to-score
1763   "*" gnus-summary-limit-include-cached
1764   "D" gnus-summary-limit-include-dormant
1765   "T" gnus-summary-limit-include-thread
1766   "d" gnus-summary-limit-exclude-dormant
1767   "t" gnus-summary-limit-to-age
1768   "." gnus-summary-limit-to-unseen
1769   "x" gnus-summary-limit-to-extra
1770   "p" gnus-summary-limit-to-display-predicate
1771   "E" gnus-summary-limit-include-expunged
1772   "c" gnus-summary-limit-exclude-childless-dormant
1773   "C" gnus-summary-limit-mark-excluded-as-read
1774   "o" gnus-summary-insert-old-articles
1775   "N" gnus-summary-insert-new-articles)
1776
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   "h" gnus-article-wash-html
1865   "u" gnus-article-unsplit-urls
1866   "f" gnus-article-display-x-face
1867   "l" gnus-summary-stop-page-breaking
1868   "r" gnus-summary-caesar-message
1869   "m" gnus-summary-morse-message
1870   "t" gnus-summary-toggle-header
1871   "g" gnus-treat-smiley
1872   "v" gnus-summary-verbose-headers
1873   "m" gnus-summary-toggle-mime
1874   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1875   "p" gnus-article-verify-x-pgp-sig
1876   "d" gnus-article-treat-dumbquotes)
1877
1878 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1879   ;; mnemonic: deuglif*Y*
1880   "u" gnus-article-outlook-unwrap-lines
1881   "a" gnus-article-outlook-repair-attribution
1882   "c" gnus-article-outlook-rearrange-citation
1883   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1884
1885 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1886   "a" gnus-article-hide
1887   "h" gnus-article-hide-headers
1888   "b" gnus-article-hide-boring-headers
1889   "s" gnus-article-hide-signature
1890   "c" gnus-article-hide-citation
1891   "C" gnus-article-hide-citation-in-followups
1892   "l" gnus-article-hide-list-identifiers
1893   "B" gnus-article-strip-banner
1894   "P" gnus-article-hide-pem
1895   "\C-c" gnus-article-hide-citation-maybe)
1896
1897 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1898   "a" gnus-article-highlight
1899   "h" gnus-article-highlight-headers
1900   "c" gnus-article-highlight-citation
1901   "s" gnus-article-highlight-signature)
1902
1903 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1904   "f" gnus-article-treat-fold-headers
1905   "u" gnus-article-treat-unfold-headers
1906   "n" gnus-article-treat-fold-newsgroups)
1907
1908 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1909   "x" gnus-article-display-x-face
1910   "d" gnus-article-display-face
1911   "s" gnus-treat-smiley
1912   "D" gnus-article-remove-images
1913   "f" gnus-treat-from-picon
1914   "m" gnus-treat-mail-picon
1915   "n" gnus-treat-newsgroups-picon)
1916
1917 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1918   "z" gnus-article-date-ut
1919   "u" gnus-article-date-ut
1920   "l" gnus-article-date-local
1921   "p" gnus-article-date-english
1922   "e" gnus-article-date-lapsed
1923   "o" gnus-article-date-original
1924   "i" gnus-article-date-iso8601
1925   "s" gnus-article-date-user)
1926
1927 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1928   "t" gnus-article-remove-trailing-blank-lines
1929   "l" gnus-article-strip-leading-blank-lines
1930   "m" gnus-article-strip-multiple-blank-lines
1931   "a" gnus-article-strip-blank-lines
1932   "A" gnus-article-strip-all-blank-lines
1933   "s" gnus-article-strip-leading-space
1934   "e" gnus-article-strip-trailing-space
1935   "w" gnus-article-remove-leading-whitespace)
1936
1937 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1938   "v" gnus-version
1939   "f" gnus-summary-fetch-faq
1940   "d" gnus-summary-describe-group
1941   "h" gnus-summary-describe-briefly
1942   "i" gnus-info-find-node
1943   "c" gnus-group-fetch-charter
1944   "C" gnus-group-fetch-control)
1945
1946 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1947   "e" gnus-summary-expire-articles
1948   "\M-\C-e" gnus-summary-expire-articles-now
1949   "\177" gnus-summary-delete-article
1950   [delete] gnus-summary-delete-article
1951   [backspace] gnus-summary-delete-article
1952   "m" gnus-summary-move-article
1953   "r" gnus-summary-respool-article
1954   "w" gnus-summary-edit-article
1955   "c" gnus-summary-copy-article
1956   "B" gnus-summary-crosspost-article
1957   "q" gnus-summary-respool-query
1958   "t" gnus-summary-respool-trace
1959   "i" gnus-summary-import-article
1960   "I" gnus-summary-create-article
1961   "p" gnus-summary-article-posted-p)
1962
1963 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1964   "o" gnus-summary-save-article
1965   "m" gnus-summary-save-article-mail
1966   "F" gnus-summary-write-article-file
1967   "r" gnus-summary-save-article-rmail
1968   "f" gnus-summary-save-article-file
1969   "b" gnus-summary-save-article-body-file
1970   "h" gnus-summary-save-article-folder
1971   "v" gnus-summary-save-article-vm
1972   "p" gnus-summary-pipe-output
1973   "P" gnus-summary-muttprint
1974   "s" gnus-soup-add-article)
1975
1976 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1977   "b" gnus-summary-display-buttonized
1978   "m" gnus-summary-repair-multipart
1979   "v" gnus-article-view-part
1980   "o" gnus-article-save-part
1981   "c" gnus-article-copy-part
1982   "C" gnus-article-view-part-as-charset
1983   "e" gnus-article-view-part-externally
1984   "E" gnus-article-encrypt-body
1985   "i" gnus-article-inline-part
1986   "|" gnus-article-pipe-part)
1987
1988 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1989   "p" gnus-summary-mark-as-processable
1990   "u" gnus-summary-unmark-as-processable
1991   "U" gnus-summary-unmark-all-processable
1992   "v" gnus-uu-mark-over
1993   "s" gnus-uu-mark-series
1994   "r" gnus-uu-mark-region
1995   "g" gnus-uu-unmark-region
1996   "R" gnus-uu-mark-by-regexp
1997   "G" gnus-uu-unmark-by-regexp
1998   "t" gnus-uu-mark-thread
1999   "T" gnus-uu-unmark-thread
2000   "a" gnus-uu-mark-all
2001   "b" gnus-uu-mark-buffer
2002   "S" gnus-uu-mark-sparse
2003   "k" gnus-summary-kill-process-mark
2004   "y" gnus-summary-yank-process-mark
2005   "w" gnus-summary-save-process-mark
2006   "i" gnus-uu-invert-processable)
2007
2008 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2009   ;;"x" gnus-uu-extract-any
2010   "m" gnus-summary-save-parts
2011   "u" gnus-uu-decode-uu
2012   "U" gnus-uu-decode-uu-and-save
2013   "s" gnus-uu-decode-unshar
2014   "S" gnus-uu-decode-unshar-and-save
2015   "o" gnus-uu-decode-save
2016   "O" gnus-uu-decode-save
2017   "b" gnus-uu-decode-binhex
2018   "B" gnus-uu-decode-binhex
2019   "p" gnus-uu-decode-postscript
2020   "P" gnus-uu-decode-postscript-and-save)
2021
2022 (gnus-define-keys
2023     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2024   "u" gnus-uu-decode-uu-view
2025   "U" gnus-uu-decode-uu-and-save-view
2026   "s" gnus-uu-decode-unshar-view
2027   "S" gnus-uu-decode-unshar-and-save-view
2028   "o" gnus-uu-decode-save-view
2029   "O" gnus-uu-decode-save-view
2030   "b" gnus-uu-decode-binhex-view
2031   "B" gnus-uu-decode-binhex-view
2032   "p" gnus-uu-decode-postscript-view
2033   "P" gnus-uu-decode-postscript-and-save-view)
2034
2035 (defvar gnus-article-post-menu nil)
2036
2037 (defconst gnus-summary-menu-maxlen 20)
2038
2039 (defun gnus-summary-menu-split (menu)
2040   ;; If we have lots of elements, divide them into groups of 20
2041   ;; and make a pane (or submenu) for each one.
2042   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2043       (let ((menu menu) sublists next
2044             (i 1))
2045         (while menu
2046           ;; Pull off the next gnus-summary-menu-maxlen elements
2047           ;; and make them the next element of sublist.
2048           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2049           (if next
2050               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2051                       nil))
2052           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2053                                              (aref (car (last menu)) 0)) menu)
2054                                sublists))
2055           (setq i (1+ i))
2056           (setq menu next))
2057         (nreverse sublists))
2058     ;; Few elements--put them all in one pane.
2059     menu))
2060
2061 (defun gnus-summary-make-menu-bar ()
2062   (gnus-turn-off-edit-menu 'summary)
2063
2064   (unless (boundp 'gnus-summary-misc-menu)
2065
2066     (easy-menu-define
2067      gnus-summary-kill-menu gnus-summary-mode-map ""
2068      (cons
2069       "Score"
2070       (nconc
2071        (list
2072         ["Customize" gnus-score-customize t])
2073        (gnus-make-score-map 'increase)
2074        (gnus-make-score-map 'lower)
2075        '(("Mark"
2076           ["Kill below" gnus-summary-kill-below t]
2077           ["Mark above" gnus-summary-mark-above t]
2078           ["Tick above" gnus-summary-tick-above t]
2079           ["Clear above" gnus-summary-clear-above t])
2080          ["Current score" gnus-summary-current-score t]
2081          ["Set score" gnus-summary-set-score t]
2082          ["Switch current score file..." gnus-score-change-score-file t]
2083          ["Set mark below..." gnus-score-set-mark-below t]
2084          ["Set expunge below..." gnus-score-set-expunge-below t]
2085          ["Edit current score file" gnus-score-edit-current-scores t]
2086          ["Edit score file" gnus-score-edit-file t]
2087          ["Trace score" gnus-score-find-trace t]
2088          ["Find words" gnus-score-find-favourite-words t]
2089          ["Rescore buffer" gnus-summary-rescore t]
2090          ["Increase score..." gnus-summary-increase-score t]
2091          ["Lower score..." gnus-summary-lower-score t]))))
2092
2093     ;; Define both the Article menu in the summary buffer and the
2094     ;; equivalent Commands menu in the article buffer here for
2095     ;; consistency.
2096     (let ((innards
2097            `(("Hide"
2098               ["All" gnus-article-hide t]
2099               ["Headers" gnus-article-hide-headers t]
2100               ["Signature" gnus-article-hide-signature t]
2101               ["Citation" gnus-article-hide-citation t]
2102               ["List identifiers" gnus-article-hide-list-identifiers t]
2103               ["Banner" gnus-article-strip-banner t]
2104               ["Boring headers" gnus-article-hide-boring-headers t])
2105              ("Highlight"
2106               ["All" gnus-article-highlight t]
2107               ["Headers" gnus-article-highlight-headers t]
2108               ["Signature" gnus-article-highlight-signature t]
2109               ["Citation" gnus-article-highlight-citation t])
2110              ("Date"
2111               ["Local" gnus-article-date-local t]
2112               ["ISO8601" gnus-article-date-iso8601 t]
2113               ["UT" gnus-article-date-ut t]
2114               ["Original" gnus-article-date-original t]
2115               ["Lapsed" gnus-article-date-lapsed t]
2116               ["User-defined" gnus-article-date-user t])
2117              ("Display"
2118               ["Remove images" gnus-article-remove-images t]
2119               ["Toggle smiley" gnus-treat-smiley t]
2120               ["Show X-Face" gnus-article-display-x-face t]
2121               ["Show picons in From" gnus-treat-from-picon t]
2122               ["Show picons in mail headers" gnus-treat-mail-picon t]
2123               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2124               ("View as different encoding"
2125                ,@(gnus-summary-menu-split
2126                   (mapcar
2127                    (lambda (cs)
2128                      ;; Since easymenu under Emacs doesn't allow
2129                      ;; lambda forms for menu commands, we should
2130                      ;; provide intern'ed function symbols.
2131                      (let ((command (intern (format "\
2132 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2133                        (fset command
2134                              `(lambda ()
2135                                 (interactive)
2136                                 (let ((gnus-summary-show-article-charset-alist
2137                                        '((1 . ,cs))))
2138                                   (gnus-summary-show-article 1))))
2139                        `[,(symbol-name cs) ,command t]))
2140                    (sort (if (fboundp 'coding-system-list)
2141                              (coding-system-list)
2142                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2143                            )
2144                          'string<)))))
2145              ("Washing"
2146               ("Remove Blanks"
2147                ["Leading" gnus-article-strip-leading-blank-lines t]
2148                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2149                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2150                ["All of the above" gnus-article-strip-blank-lines t]
2151                ["All" gnus-article-strip-all-blank-lines t]
2152                ["Leading space" gnus-article-strip-leading-space t]
2153                ["Trailing space" gnus-article-strip-trailing-space t]
2154                ["Leading space in headers"
2155                 gnus-article-remove-leading-whitespace t])
2156               ["Overstrike" gnus-article-treat-overstrike t]
2157               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2158               ["Emphasis" gnus-article-emphasize t]
2159               ["Word wrap" gnus-article-fill-cited-article t]
2160               ["Fill long lines" gnus-article-fill-long-lines t]
2161               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2162               ["CR" gnus-article-remove-cr t]
2163               ["Rot 13" gnus-summary-caesar-message
2164                ,@(if (featurep 'xemacs) '(t)
2165                    '(:help "\"Caesar rotate\" article by 13"))]
2166               ["Morse decode" gnus-summary-morse-message t]
2167               ["Unix pipe..." gnus-summary-pipe-message t]
2168               ["Add buttons" gnus-article-add-buttons t]
2169               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2170               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2171               ["Toggle MIME" gnus-summary-toggle-mime t]
2172               ["Verbose header" gnus-summary-verbose-headers t]
2173               ["Toggle header" gnus-summary-toggle-header t]
2174               ["Unfold headers" gnus-article-treat-unfold-headers t]
2175               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2176               ["Html" gnus-article-wash-html t]
2177               ["URLs" gnus-article-unsplit-urls t]
2178               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2179               ["HZ" gnus-article-decode-HZ t]
2180               ("(Outlook) Deuglify"
2181                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2182                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2183                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2184                ["Full (Outlook) deuglify"
2185                 gnus-article-outlook-deuglify-article t])
2186               )
2187              ("Output"
2188               ["Save in default format..." gnus-summary-save-article
2189                ,@(if (featurep 'xemacs) '(t)
2190                    '(:help "Save article using default method"))]
2191               ["Save in file..." gnus-summary-save-article-file
2192                ,@(if (featurep 'xemacs) '(t)
2193                    '(:help "Save article in file"))]
2194               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2195               ["Save in MH folder..." gnus-summary-save-article-folder t]
2196               ["Save in VM folder..." gnus-summary-save-article-vm t]
2197               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2198               ["Save body in file..." gnus-summary-save-article-body-file t]
2199               ["Pipe through a filter..." gnus-summary-pipe-output t]
2200               ["Add to SOUP packet" gnus-soup-add-article t]
2201               ["Print with Muttprint..." gnus-summary-muttprint t]
2202               ["Print" gnus-summary-print-article t])
2203              ("Backend"
2204               ["Respool article..." gnus-summary-respool-article t]
2205               ["Move article..." gnus-summary-move-article
2206                (gnus-check-backend-function
2207                 'request-move-article gnus-newsgroup-name)]
2208               ["Copy article..." gnus-summary-copy-article t]
2209               ["Crosspost article..." gnus-summary-crosspost-article
2210                (gnus-check-backend-function
2211                 'request-replace-article gnus-newsgroup-name)]
2212               ["Import file..." gnus-summary-import-article
2213                (gnus-check-backend-function
2214                 'request-accept-article gnus-newsgroup-name)]
2215               ["Create article..." gnus-summary-create-article
2216                (gnus-check-backend-function
2217                 'request-accept-article gnus-newsgroup-name)]
2218               ["Check if posted" gnus-summary-article-posted-p t]
2219               ["Edit article" gnus-summary-edit-article
2220                (not (gnus-group-read-only-p))]
2221               ["Delete article" gnus-summary-delete-article
2222                (gnus-check-backend-function
2223                 'request-expire-articles gnus-newsgroup-name)]
2224               ["Query respool" gnus-summary-respool-query t]
2225               ["Trace respool" gnus-summary-respool-trace t]
2226               ["Delete expirable articles" gnus-summary-expire-articles-now
2227                (gnus-check-backend-function
2228                 'request-expire-articles gnus-newsgroup-name)])
2229              ("Extract"
2230               ["Uudecode" gnus-uu-decode-uu
2231                ,@(if (featurep 'xemacs) '(t)
2232                    '(:help "Decode uuencoded article(s)"))]
2233               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2234               ["Unshar" gnus-uu-decode-unshar t]
2235               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2236               ["Save" gnus-uu-decode-save t]
2237               ["Binhex" gnus-uu-decode-binhex t]
2238               ["Postscript" gnus-uu-decode-postscript t])
2239              ("Cache"
2240               ["Enter article" gnus-cache-enter-article t]
2241               ["Remove article" gnus-cache-remove-article t])
2242              ["Translate" gnus-article-babel t]
2243              ["Select article buffer" gnus-summary-select-article-buffer t]
2244              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2245              ["Isearch article..." gnus-summary-isearch-article t]
2246              ["Beginning of the article" gnus-summary-beginning-of-article t]
2247              ["End of the article" gnus-summary-end-of-article t]
2248              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2249              ["Fetch referenced articles" gnus-summary-refer-references t]
2250              ["Fetch current thread" gnus-summary-refer-thread t]
2251              ["Fetch article with id..." gnus-summary-refer-article t]
2252              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2253              ["Redisplay" gnus-summary-show-article t]
2254              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2255       (easy-menu-define
2256        gnus-summary-article-menu gnus-summary-mode-map ""
2257        (cons "Article" innards))
2258
2259       (if (not (keymapp gnus-summary-article-menu))
2260           (easy-menu-define
2261            gnus-article-commands-menu gnus-article-mode-map ""
2262            (cons "Commands" innards))
2263         ;; in Emacs, don't share menu.
2264         (setq gnus-article-commands-menu
2265               (copy-keymap gnus-summary-article-menu))
2266         (define-key gnus-article-mode-map [menu-bar commands]
2267           (cons "Commands" gnus-article-commands-menu))))
2268
2269     (easy-menu-define
2270      gnus-summary-thread-menu gnus-summary-mode-map ""
2271      '("Threads"
2272        ["Find all messages in thread" gnus-summary-refer-thread t]
2273        ["Toggle threading" gnus-summary-toggle-threads t]
2274        ["Hide threads" gnus-summary-hide-all-threads t]
2275        ["Show threads" gnus-summary-show-all-threads t]
2276        ["Hide thread" gnus-summary-hide-thread t]
2277        ["Show thread" gnus-summary-show-thread t]
2278        ["Go to next thread" gnus-summary-next-thread t]
2279        ["Go to previous thread" gnus-summary-prev-thread t]
2280        ["Go down thread" gnus-summary-down-thread t]
2281        ["Go up thread" gnus-summary-up-thread t]
2282        ["Top of thread" gnus-summary-top-thread t]
2283        ["Mark thread as read" gnus-summary-kill-thread t]
2284        ["Lower thread score" gnus-summary-lower-thread t]
2285        ["Raise thread score" gnus-summary-raise-thread t]
2286        ["Rethread current" gnus-summary-rethread-current t]))
2287
2288     (easy-menu-define
2289      gnus-summary-post-menu gnus-summary-mode-map ""
2290      `("Post"
2291        ["Send a message (mail or news)" gnus-summary-post-news
2292         ,@(if (featurep 'xemacs) '(t)
2293             '(:help "Post an article"))]
2294        ["Followup" gnus-summary-followup
2295         ,@(if (featurep 'xemacs) '(t)
2296             '(:help "Post followup to this article"))]
2297        ["Followup and yank" gnus-summary-followup-with-original
2298         ,@(if (featurep 'xemacs) '(t)
2299             '(:help "Post followup to this article, quoting its contents"))]
2300        ["Supersede article" gnus-summary-supersede-article t]
2301        ["Cancel article" gnus-summary-cancel-article
2302         ,@(if (featurep 'xemacs) '(t)
2303             '(:help "Cancel an article you posted"))]
2304        ["Reply" gnus-summary-reply t]
2305        ["Reply and yank" gnus-summary-reply-with-original t]
2306        ["Wide reply" gnus-summary-wide-reply t]
2307        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2308         ,@(if (featurep 'xemacs) '(t)
2309             '(:help "Mail a reply, quoting this article"))]
2310        ["Very wide reply" gnus-summary-very-wide-reply t]
2311        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2312         ,@(if (featurep 'xemacs) '(t)
2313             '(:help "Mail a very wide reply, quoting this article"))]
2314        ["Mail forward" gnus-summary-mail-forward t]
2315        ["Post forward" gnus-summary-post-forward t]
2316        ["Digest and mail" gnus-summary-digest-mail-forward t]
2317        ["Digest and post" gnus-summary-digest-post-forward t]
2318        ["Resend message" gnus-summary-resend-message t]
2319        ["Resend message edit" gnus-summary-resend-message-edit t]
2320        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2321        ["Send a mail" gnus-summary-mail-other-window t]
2322        ["Create a local message" gnus-summary-news-other-window t]
2323        ["Uuencode and post" gnus-uu-post-news
2324         ,@(if (featurep 'xemacs) '(t)
2325             '(:help "Post a uuencoded article"))]
2326        ["Followup via news" gnus-summary-followup-to-mail t]
2327        ["Followup via news and yank"
2328         gnus-summary-followup-to-mail-with-original t]
2329        ;;("Draft"
2330        ;;["Send" gnus-summary-send-draft t]
2331        ;;["Send bounced" gnus-resend-bounced-mail t])
2332        ))
2333
2334     (cond
2335      ((not (keymapp gnus-summary-post-menu))
2336       (setq gnus-article-post-menu gnus-summary-post-menu))
2337      ((not gnus-article-post-menu)
2338       ;; Don't share post menu.
2339       (setq gnus-article-post-menu
2340             (copy-keymap gnus-summary-post-menu))))
2341     (define-key gnus-article-mode-map [menu-bar post]
2342       (cons "Post" gnus-article-post-menu))
2343
2344     (easy-menu-define
2345      gnus-summary-misc-menu gnus-summary-mode-map ""
2346      `("Gnus"
2347        ("Mark Read"
2348         ["Mark as read" gnus-summary-mark-as-read-forward t]
2349         ["Mark same subject and select"
2350          gnus-summary-kill-same-subject-and-select t]
2351         ["Mark same subject" gnus-summary-kill-same-subject t]
2352         ["Catchup" gnus-summary-catchup
2353          ,@(if (featurep 'xemacs) '(t)
2354              '(:help "Mark unread articles in this group as read"))]
2355         ["Catchup all" gnus-summary-catchup-all t]
2356         ["Catchup to here" gnus-summary-catchup-to-here t]
2357         ["Catchup from here" gnus-summary-catchup-from-here t]
2358         ["Catchup region" gnus-summary-mark-region-as-read
2359          (gnus-mark-active-p)]
2360         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2361        ("Mark Various"
2362         ["Tick" gnus-summary-tick-article-forward t]
2363         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2364         ["Remove marks" gnus-summary-clear-mark-forward t]
2365         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2366         ["Set bookmark" gnus-summary-set-bookmark t]
2367         ["Remove bookmark" gnus-summary-remove-bookmark t])
2368        ("Limit to"
2369         ["Marks..." gnus-summary-limit-to-marks t]
2370         ["Subject..." gnus-summary-limit-to-subject t]
2371         ["Author..." gnus-summary-limit-to-author t]
2372         ["Age..." gnus-summary-limit-to-age t]
2373         ["Extra..." gnus-summary-limit-to-extra t]
2374         ["Score..." gnus-summary-limit-to-score t]
2375         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2376         ["Unread" gnus-summary-limit-to-unread t]
2377         ["Unseen" gnus-summary-limit-to-unseen t]
2378         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2379         ["Next articles" gnus-summary-limit-to-articles t]
2380         ["Pop limit" gnus-summary-pop-limit t]
2381         ["Show dormant" gnus-summary-limit-include-dormant t]
2382         ["Hide childless dormant"
2383          gnus-summary-limit-exclude-childless-dormant t]
2384         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2385         ["Hide marked" gnus-summary-limit-exclude-marks t]
2386         ["Show expunged" gnus-summary-limit-include-expunged t])
2387        ("Process Mark"
2388         ["Set mark" gnus-summary-mark-as-processable t]
2389         ["Remove mark" gnus-summary-unmark-as-processable t]
2390         ["Remove all marks" gnus-summary-unmark-all-processable t]
2391         ["Mark above" gnus-uu-mark-over t]
2392         ["Mark series" gnus-uu-mark-series t]
2393         ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2394         ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2395         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2396         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2397         ["Mark all" gnus-uu-mark-all t]
2398         ["Mark buffer" gnus-uu-mark-buffer t]
2399         ["Mark sparse" gnus-uu-mark-sparse t]
2400         ["Mark thread" gnus-uu-mark-thread t]
2401         ["Unmark thread" gnus-uu-unmark-thread t]
2402         ("Process Mark Sets"
2403          ["Kill" gnus-summary-kill-process-mark t]
2404          ["Yank" gnus-summary-yank-process-mark
2405           gnus-newsgroup-process-stack]
2406          ["Save" gnus-summary-save-process-mark t]
2407          ["Run command on marked..." gnus-summary-universal-argument t]))
2408        ("Scroll article"
2409         ["Page forward" gnus-summary-next-page
2410          ,@(if (featurep 'xemacs) '(t)
2411              '(:help "Show next page of article"))]
2412         ["Page backward" gnus-summary-prev-page
2413          ,@(if (featurep 'xemacs) '(t)
2414              '(:help "Show previous page of article"))]
2415         ["Line forward" gnus-summary-scroll-up t])
2416        ("Move"
2417         ["Next unread article" gnus-summary-next-unread-article t]
2418         ["Previous unread article" gnus-summary-prev-unread-article t]
2419         ["Next article" gnus-summary-next-article t]
2420         ["Previous article" gnus-summary-prev-article t]
2421         ["Next unread subject" gnus-summary-next-unread-subject t]
2422         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2423         ["Next article same subject" gnus-summary-next-same-subject t]
2424         ["Previous article same subject" gnus-summary-prev-same-subject t]
2425         ["First unread article" gnus-summary-first-unread-article t]
2426         ["Best unread article" gnus-summary-best-unread-article t]
2427         ["Go to subject number..." gnus-summary-goto-subject t]
2428         ["Go to article number..." gnus-summary-goto-article t]
2429         ["Go to the last article" gnus-summary-goto-last-article t]
2430         ["Pop article off history" gnus-summary-pop-article t])
2431        ("Sort"
2432         ["Sort by number" gnus-summary-sort-by-number t]
2433         ["Sort by author" gnus-summary-sort-by-author t]
2434         ["Sort by subject" gnus-summary-sort-by-subject t]
2435         ["Sort by date" gnus-summary-sort-by-date t]
2436         ["Sort by score" gnus-summary-sort-by-score t]
2437         ["Sort by lines" gnus-summary-sort-by-lines t]
2438         ["Sort by characters" gnus-summary-sort-by-chars t]
2439         ["Randomize" gnus-summary-sort-by-random t]
2440         ["Original sort" gnus-summary-sort-by-original t])
2441        ("Help"
2442         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2443         ["Describe group" gnus-summary-describe-group t]
2444         ["Fetch charter" gnus-group-fetch-charter
2445          ,@(if (featurep 'xemacs) nil
2446              '(:help "Display the charter of the current group"))]
2447         ["Fetch control message" gnus-group-fetch-control
2448          ,@(if (featurep 'xemacs) nil
2449              '(:help "Display the archived control message for the current group"))]
2450         ["Read manual" gnus-info-find-node t])
2451        ("Modes"
2452         ["Pick and read" gnus-pick-mode t]
2453         ["Binary" gnus-binary-mode t])
2454        ("Regeneration"
2455         ["Regenerate" gnus-summary-prepare t]
2456         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2457         ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2458         ["Toggle threading" gnus-summary-toggle-threads t])
2459        ["See old articles" gnus-summary-insert-old-articles t]
2460        ["See new articles" gnus-summary-insert-new-articles t]
2461        ["Filter articles..." gnus-summary-execute-command t]
2462        ["Run command on articles..." gnus-summary-universal-argument t]
2463        ["Search articles forward..." gnus-summary-search-article-forward t]
2464        ["Search articles backward..." gnus-summary-search-article-backward t]
2465        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2466        ["Expand window" gnus-summary-expand-window t]
2467        ["Expire expirable articles" gnus-summary-expire-articles
2468         (gnus-check-backend-function
2469          'request-expire-articles gnus-newsgroup-name)]
2470        ["Edit local kill file" gnus-summary-edit-local-kill t]
2471        ["Edit main kill file" gnus-summary-edit-global-kill t]
2472        ["Edit group parameters" gnus-summary-edit-parameters t]
2473        ["Customize group parameters" gnus-summary-customize-parameters t]
2474        ["Send a bug report" gnus-bug t]
2475        ("Exit"
2476         ["Catchup and exit" gnus-summary-catchup-and-exit
2477          ,@(if (featurep 'xemacs) '(t)
2478              '(:help "Mark unread articles in this group as read, then exit"))]
2479         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2480         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2481         ["Exit group" gnus-summary-exit
2482          ,@(if (featurep 'xemacs) '(t)
2483              '(:help "Exit current group, return to group selection mode"))]
2484         ["Exit group without updating" gnus-summary-exit-no-update t]
2485         ["Exit and goto next group" gnus-summary-next-group t]
2486         ["Exit and goto prev group" gnus-summary-prev-group t]
2487         ["Reselect group" gnus-summary-reselect-current-group t]
2488         ["Rescan group" gnus-summary-rescan-group t]
2489         ["Update dribble" gnus-summary-save-newsrc t])))
2490
2491     (gnus-run-hooks 'gnus-summary-menu-hook)))
2492
2493 (defvar gnus-summary-tool-bar-map nil)
2494
2495 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2496 (defun gnus-summary-make-tool-bar ()
2497   (if (and (fboundp 'tool-bar-add-item-from-menu)
2498            (default-value 'tool-bar-mode)
2499            (not gnus-summary-tool-bar-map))
2500       (setq gnus-summary-tool-bar-map
2501             (let ((tool-bar-map (make-sparse-keymap))
2502                   (load-path (mm-image-load-path)))
2503               (tool-bar-add-item-from-menu
2504                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2505               (tool-bar-add-item-from-menu
2506                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2507               (tool-bar-add-item-from-menu
2508                'gnus-summary-post-news "post" gnus-summary-mode-map)
2509               (tool-bar-add-item-from-menu
2510                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2511               (tool-bar-add-item-from-menu
2512                'gnus-summary-followup "followup" gnus-summary-mode-map)
2513               (tool-bar-add-item-from-menu
2514                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2515               (tool-bar-add-item-from-menu
2516                'gnus-summary-reply "reply" gnus-summary-mode-map)
2517               (tool-bar-add-item-from-menu
2518                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2519               (tool-bar-add-item-from-menu
2520                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2521               (tool-bar-add-item-from-menu
2522                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2523               (tool-bar-add-item-from-menu
2524                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2525               (tool-bar-add-item-from-menu
2526                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2527               (tool-bar-add-item-from-menu
2528                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2529               (tool-bar-add-item-from-menu
2530                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2531               (tool-bar-add-item-from-menu
2532                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2533               tool-bar-map)))
2534   (if gnus-summary-tool-bar-map
2535       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2536
2537 (defun gnus-score-set-default (var value)
2538   "A version of set that updates the GNU Emacs menu-bar."
2539   (set var value)
2540   ;; It is the message that forces the active status to be updated.
2541   (message ""))
2542
2543 (defun gnus-make-score-map (type)
2544   "Make a summary score map of type TYPE."
2545   (if t
2546       nil
2547     (let ((headers '(("author" "from" string)
2548                      ("subject" "subject" string)
2549                      ("article body" "body" string)
2550                      ("article head" "head" string)
2551                      ("xref" "xref" string)
2552                      ("extra header" "extra" string)
2553                      ("lines" "lines" number)
2554                      ("followups to author" "followup" string)))
2555           (types '((number ("less than" <)
2556                            ("greater than" >)
2557                            ("equal" =))
2558                    (string ("substring" s)
2559                            ("exact string" e)
2560                            ("fuzzy string" f)
2561                            ("regexp" r))))
2562           (perms '(("temporary" (current-time-string))
2563                    ("permanent" nil)
2564                    ("immediate" now)))
2565           header)
2566       (list
2567        (apply
2568         'nconc
2569         (list
2570          (if (eq type 'lower)
2571              "Lower score"
2572            "Increase score"))
2573         (let (outh)
2574           (while headers
2575             (setq header (car headers))
2576             (setq outh
2577                   (cons
2578                    (apply
2579                     'nconc
2580                     (list (car header))
2581                     (let ((ts (cdr (assoc (nth 2 header) types)))
2582                           outt)
2583                       (while ts
2584                         (setq outt
2585                               (cons
2586                                (apply
2587                                 'nconc
2588                                 (list (caar ts))
2589                                 (let ((ps perms)
2590                                       outp)
2591                                   (while ps
2592                                     (setq outp
2593                                           (cons
2594                                            (vector
2595                                             (caar ps)
2596                                             (list
2597                                              'gnus-summary-score-entry
2598                                              (nth 1 header)
2599                                              (if (or (string= (nth 1 header)
2600                                                               "head")
2601                                                      (string= (nth 1 header)
2602                                                               "body"))
2603                                                  ""
2604                                                (list 'gnus-summary-header
2605                                                      (nth 1 header)))
2606                                              (list 'quote (nth 1 (car ts)))
2607                                              (list 'gnus-score-delta-default
2608                                                    nil)
2609                                              (nth 1 (car ps))
2610                                              t)
2611                                             t)
2612                                            outp))
2613                                     (setq ps (cdr ps)))
2614                                   (list (nreverse outp))))
2615                                outt))
2616                         (setq ts (cdr ts)))
2617                       (list (nreverse outt))))
2618                    outh))
2619             (setq headers (cdr headers)))
2620           (list (nreverse outh))))))))
2621
2622 \f
2623
2624 (defun gnus-summary-mode (&optional group)
2625   "Major mode for reading articles.
2626
2627 All normal editing commands are switched off.
2628 \\<gnus-summary-mode-map>
2629 Each line in this buffer represents one article.  To read an
2630 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2631 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2632 respectively.
2633
2634 You can also post articles and send mail from this buffer.  To
2635 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2636 of an article, type `\\[gnus-summary-reply]'.
2637
2638 There are approx. one gazillion commands you can execute in this
2639 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2640
2641 The following commands are available:
2642
2643 \\{gnus-summary-mode-map}"
2644   (interactive)
2645   (kill-all-local-variables)
2646   (when (gnus-visual-p 'summary-menu 'menu)
2647     (gnus-summary-make-menu-bar)
2648     (gnus-summary-make-tool-bar))
2649   (gnus-summary-make-local-variables)
2650   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2651     (gnus-summary-make-local-variables))
2652   (gnus-make-thread-indent-array)
2653   (gnus-simplify-mode-line)
2654   (setq major-mode 'gnus-summary-mode)
2655   (setq mode-name "Summary")
2656   (make-local-variable 'minor-mode-alist)
2657   (use-local-map gnus-summary-mode-map)
2658   (buffer-disable-undo)
2659   (setq buffer-read-only t)             ;Disable modification
2660   (setq truncate-lines t)
2661   (setq selective-display t)
2662   (setq selective-display-ellipses t)   ;Display `...'
2663   (gnus-summary-set-display-table)
2664   (gnus-set-default-directory)
2665   (setq gnus-newsgroup-name group)
2666   (unless (gnus-news-group-p group)
2667     (setq gnus-newsgroup-incorporated
2668           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2669   (make-local-variable 'gnus-summary-line-format)
2670   (make-local-variable 'gnus-summary-line-format-spec)
2671   (make-local-variable 'gnus-summary-dummy-line-format)
2672   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2673   (make-local-variable 'gnus-summary-mark-positions)
2674   (gnus-make-local-hook 'pre-command-hook)
2675   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2676   (gnus-run-hooks 'gnus-summary-mode-hook)
2677   (turn-on-gnus-mailing-list-mode)
2678   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2679   (gnus-update-summary-mark-positions))
2680
2681 (defun gnus-summary-make-local-variables ()
2682   "Make all the local summary buffer variables."
2683   (let (global)
2684     (dolist (local gnus-summary-local-variables)
2685       (if (consp local)
2686           (progn
2687             (if (eq (cdr local) 'global)
2688                 ;; Copy the global value of the variable.
2689                 (setq global (symbol-value (car local)))
2690               ;; Use the value from the list.
2691               (setq global (eval (cdr local))))
2692             (set (make-local-variable (car local)) global))
2693         ;; Simple nil-valued local variable.
2694         (set (make-local-variable local) nil)))))
2695
2696 (defun gnus-summary-clear-local-variables ()
2697   (let ((locals gnus-summary-local-variables))
2698     (while locals
2699       (if (consp (car locals))
2700           (and (vectorp (caar locals))
2701                (set (caar locals) nil))
2702         (and (vectorp (car locals))
2703              (set (car locals) nil)))
2704       (setq locals (cdr locals)))))
2705
2706 ;; Summary data functions.
2707
2708 (defmacro gnus-data-number (data)
2709   `(car ,data))
2710
2711 (defmacro gnus-data-set-number (data number)
2712   `(setcar ,data ,number))
2713
2714 (defmacro gnus-data-mark (data)
2715   `(nth 1 ,data))
2716
2717 (defmacro gnus-data-set-mark (data mark)
2718   `(setcar (nthcdr 1 ,data) ,mark))
2719
2720 (defmacro gnus-data-pos (data)
2721   `(nth 2 ,data))
2722
2723 (defmacro gnus-data-set-pos (data pos)
2724   `(setcar (nthcdr 2 ,data) ,pos))
2725
2726 (defmacro gnus-data-header (data)
2727   `(nth 3 ,data))
2728
2729 (defmacro gnus-data-set-header (data header)
2730   `(setcar (nthcdr 3 ,data) ,header))
2731
2732 (defmacro gnus-data-level (data)
2733   `(nth 4 ,data))
2734
2735 (defmacro gnus-data-unread-p (data)
2736   `(= (nth 1 ,data) gnus-unread-mark))
2737
2738 (defmacro gnus-data-read-p (data)
2739   `(/= (nth 1 ,data) gnus-unread-mark))
2740
2741 (defmacro gnus-data-pseudo-p (data)
2742   `(consp (nth 3 ,data)))
2743
2744 (defmacro gnus-data-find (number)
2745   `(assq ,number gnus-newsgroup-data))
2746
2747 (defmacro gnus-data-find-list (number &optional data)
2748   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2749      (memq (assq ,number bdata)
2750            bdata)))
2751
2752 (defmacro gnus-data-make (number mark pos header level)
2753   `(list ,number ,mark ,pos ,header ,level))
2754
2755 (defun gnus-data-enter (after-article number mark pos header level offset)
2756   (let ((data (gnus-data-find-list after-article)))
2757     (unless data
2758       (error "No such article: %d" after-article))
2759     (setcdr data (cons (gnus-data-make number mark pos header level)
2760                        (cdr data)))
2761     (setq gnus-newsgroup-data-reverse nil)
2762     (gnus-data-update-list (cddr data) offset)))
2763
2764 (defun gnus-data-enter-list (after-article list &optional offset)
2765   (when list
2766     (let ((data (and after-article (gnus-data-find-list after-article)))
2767           (ilist list))
2768       (if (not (or data
2769                    after-article))
2770           (let ((odata gnus-newsgroup-data))
2771             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2772             (when offset
2773               (gnus-data-update-list odata offset)))
2774         ;; Find the last element in the list to be spliced into the main
2775         ;; list.
2776         (while (cdr list)
2777           (setq list (cdr list)))
2778         (if (not data)
2779             (progn
2780               (setcdr list gnus-newsgroup-data)
2781               (setq gnus-newsgroup-data ilist)
2782               (when offset
2783                 (gnus-data-update-list (cdr list) offset)))
2784           (setcdr list (cdr data))
2785           (setcdr data ilist)
2786           (when offset
2787             (gnus-data-update-list (cdr list) offset))))
2788       (setq gnus-newsgroup-data-reverse nil))))
2789
2790 (defun gnus-data-remove (article &optional offset)
2791   (let ((data gnus-newsgroup-data))
2792     (if (= (gnus-data-number (car data)) article)
2793         (progn
2794           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2795                 gnus-newsgroup-data-reverse nil)
2796           (when offset
2797             (gnus-data-update-list gnus-newsgroup-data offset)))
2798       (while (cdr data)
2799         (when (= (gnus-data-number (cadr data)) article)
2800           (setcdr data (cddr data))
2801           (when offset
2802             (gnus-data-update-list (cdr data) offset))
2803           (setq data nil
2804                 gnus-newsgroup-data-reverse nil))
2805         (setq data (cdr data))))))
2806
2807 (defmacro gnus-data-list (backward)
2808   `(if ,backward
2809        (or gnus-newsgroup-data-reverse
2810            (setq gnus-newsgroup-data-reverse
2811                  (reverse gnus-newsgroup-data)))
2812      gnus-newsgroup-data))
2813
2814 (defun gnus-data-update-list (data offset)
2815   "Add OFFSET to the POS of all data entries in DATA."
2816   (setq gnus-newsgroup-data-reverse nil)
2817   (while data
2818     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2819     (setq data (cdr data))))
2820
2821 (defun gnus-summary-article-pseudo-p (article)
2822   "Say whether this article is a pseudo article or not."
2823   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2824
2825 (defmacro gnus-summary-article-sparse-p (article)
2826   "Say whether this article is a sparse article or not."
2827   `(memq ,article gnus-newsgroup-sparse))
2828
2829 (defmacro gnus-summary-article-ancient-p (article)
2830   "Say whether this article is a sparse article or not."
2831   `(memq ,article gnus-newsgroup-ancient))
2832
2833 (defun gnus-article-parent-p (number)
2834   "Say whether this article is a parent or not."
2835   (let ((data (gnus-data-find-list number)))
2836     (and (cdr data)                     ; There has to be an article after...
2837          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2838             (gnus-data-level (nth 1 data))))))
2839
2840 (defun gnus-article-children (number)
2841   "Return a list of all children to NUMBER."
2842   (let* ((data (gnus-data-find-list number))
2843          (level (gnus-data-level (car data)))
2844          children)
2845     (setq data (cdr data))
2846     (while (and data
2847                 (= (gnus-data-level (car data)) (1+ level)))
2848       (push (gnus-data-number (car data)) children)
2849       (setq data (cdr data)))
2850     children))
2851
2852 (defmacro gnus-summary-skip-intangible ()
2853   "If the current article is intangible, then jump to a different article."
2854   '(let ((to (get-text-property (point) 'gnus-intangible)))
2855      (and to (gnus-summary-goto-subject to))))
2856
2857 (defmacro gnus-summary-article-intangible-p ()
2858   "Say whether this article is intangible or not."
2859   '(get-text-property (point) 'gnus-intangible))
2860
2861 (defun gnus-article-read-p (article)
2862   "Say whether ARTICLE is read or not."
2863   (not (or (memq article gnus-newsgroup-marked)
2864            (memq article gnus-newsgroup-spam-marked)
2865            (memq article gnus-newsgroup-unreads)
2866            (memq article gnus-newsgroup-unselected)
2867            (memq article gnus-newsgroup-dormant))))
2868
2869 ;; Some summary mode macros.
2870
2871 (defmacro gnus-summary-article-number ()
2872   "The article number of the article on the current line.
2873 If there isn't an article number here, then we return the current
2874 article number."
2875   '(progn
2876      (gnus-summary-skip-intangible)
2877      (or (get-text-property (point) 'gnus-number)
2878          (gnus-summary-last-subject))))
2879
2880 (defmacro gnus-summary-article-header (&optional number)
2881   "Return the header of article NUMBER."
2882   `(gnus-data-header (gnus-data-find
2883                       ,(or number '(gnus-summary-article-number)))))
2884
2885 (defmacro gnus-summary-thread-level (&optional number)
2886   "Return the level of thread that starts with article NUMBER."
2887   `(if (and (eq gnus-summary-make-false-root 'dummy)
2888             (get-text-property (point) 'gnus-intangible))
2889        0
2890      (gnus-data-level (gnus-data-find
2891                        ,(or number '(gnus-summary-article-number))))))
2892
2893 (defmacro gnus-summary-article-mark (&optional number)
2894   "Return the mark of article NUMBER."
2895   `(gnus-data-mark (gnus-data-find
2896                     ,(or number '(gnus-summary-article-number)))))
2897
2898 (defmacro gnus-summary-article-pos (&optional number)
2899   "Return the position of the line of article NUMBER."
2900   `(gnus-data-pos (gnus-data-find
2901                    ,(or number '(gnus-summary-article-number)))))
2902
2903 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2904 (defmacro gnus-summary-article-subject (&optional number)
2905   "Return current subject string or nil if nothing."
2906   `(let ((headers
2907           ,(if number
2908                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2909              '(gnus-data-header (assq (gnus-summary-article-number)
2910                                       gnus-newsgroup-data)))))
2911      (and headers
2912           (vectorp headers)
2913           (mail-header-subject headers))))
2914
2915 (defmacro gnus-summary-article-score (&optional number)
2916   "Return current article score."
2917   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2918                   gnus-newsgroup-scored))
2919        gnus-summary-default-score 0))
2920
2921 (defun gnus-summary-article-children (&optional number)
2922   "Return a list of article numbers that are children of article NUMBER."
2923   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2924          (level (gnus-data-level (car data)))
2925          l children)
2926     (while (and (setq data (cdr data))
2927                 (> (setq l (gnus-data-level (car data))) level))
2928       (and (= (1+ level) l)
2929            (push (gnus-data-number (car data))
2930                  children)))
2931     (nreverse children)))
2932
2933 (defun gnus-summary-article-parent (&optional number)
2934   "Return the article number of the parent of article NUMBER."
2935   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2936                                     (gnus-data-list t)))
2937          (level (gnus-data-level (car data))))
2938     (if (zerop level)
2939         ()                              ; This is a root.
2940       ;; We search until we find an article with a level less than
2941       ;; this one.  That function has to be the parent.
2942       (while (and (setq data (cdr data))
2943                   (not (< (gnus-data-level (car data)) level))))
2944       (and data (gnus-data-number (car data))))))
2945
2946 (defun gnus-unread-mark-p (mark)
2947   "Say whether MARK is the unread mark."
2948   (= mark gnus-unread-mark))
2949
2950 (defun gnus-read-mark-p (mark)
2951   "Say whether MARK is one of the marks that mark as read.
2952 This is all marks except unread, ticked, dormant, and expirable."
2953   (not (or (= mark gnus-unread-mark)
2954            (= mark gnus-ticked-mark)
2955            (= mark gnus-spam-mark)
2956            (= mark gnus-dormant-mark)
2957            (= mark gnus-expirable-mark))))
2958
2959 (defmacro gnus-article-mark (number)
2960   "Return the MARK of article NUMBER.
2961 This macro should only be used when computing the mark the \"first\"
2962 time; i.e., when generating the summary lines.  After that,
2963 `gnus-summary-article-mark' should be used to examine the
2964 marks of articles."
2965   `(cond
2966     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2967     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2968     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2969     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2970     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2971     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2972     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2973     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2974            gnus-ancient-mark))))
2975
2976 ;; Saving hidden threads.
2977
2978 (defmacro gnus-save-hidden-threads (&rest forms)
2979   "Save hidden threads, eval FORMS, and restore the hidden threads."
2980   (let ((config (make-symbol "config")))
2981     `(let ((,config (gnus-hidden-threads-configuration)))
2982        (unwind-protect
2983            (save-excursion
2984              ,@forms)
2985          (gnus-restore-hidden-threads-configuration ,config)))))
2986 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2987 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2988
2989 (defun gnus-data-compute-positions ()
2990   "Compute the positions of all articles."
2991   (setq gnus-newsgroup-data-reverse nil)
2992   (let ((data gnus-newsgroup-data))
2993     (save-excursion
2994       (gnus-save-hidden-threads
2995         (gnus-summary-show-all-threads)
2996         (goto-char (point-min))
2997         (while data
2998           (while (get-text-property (point) 'gnus-intangible)
2999             (forward-line 1))
3000           (gnus-data-set-pos (car data) (+ (point) 3))
3001           (setq data (cdr data))
3002           (forward-line 1))))))
3003
3004 (defun gnus-hidden-threads-configuration ()
3005   "Return the current hidden threads configuration."
3006   (save-excursion
3007     (let (config)
3008       (goto-char (point-min))
3009       (while (search-forward "\r" nil t)
3010         (push (1- (point)) config))
3011       config)))
3012
3013 (defun gnus-restore-hidden-threads-configuration (config)
3014   "Restore hidden threads configuration from CONFIG."
3015   (save-excursion
3016     (let (point buffer-read-only)
3017       (while (setq point (pop config))
3018         (when (and (< point (point-max))
3019                    (goto-char point)
3020                    (eq (char-after) ?\n))
3021           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3022
3023 ;; Various summary mode internalish functions.
3024
3025 (defun gnus-mouse-pick-article (e)
3026   (interactive "e")
3027   (mouse-set-point e)
3028   (gnus-summary-next-page nil t))
3029
3030 (defun gnus-summary-set-display-table ()
3031   "Change the display table.
3032 Odd characters have a tendency to mess
3033 up nicely formatted displays - we make all possible glyphs
3034 display only a single character."
3035
3036   ;; We start from the standard display table, if any.
3037   (let ((table (or (copy-sequence standard-display-table)
3038                    (make-display-table)))
3039         (i 32))
3040     ;; Nix out all the control chars...
3041     (while (>= (setq i (1- i)) 0)
3042       (aset table i [??]))
3043     ;; ... but not newline and cr, of course.  (cr is necessary for the
3044     ;; selective display).
3045     (aset table ?\n nil)
3046     (aset table ?\r nil)
3047     ;; We keep TAB as well.
3048     (aset table ?\t nil)
3049     ;; We nix out any glyphs over 126 that are not set already.
3050     (let ((i 256))
3051       (while (>= (setq i (1- i)) 127)
3052         ;; Only modify if the entry is nil.
3053         (unless (aref table i)
3054           (aset table i [??]))))
3055     (setq buffer-display-table table)))
3056
3057 (defun gnus-summary-set-article-display-arrow (pos)
3058   "Update the overlay arrow to point to line at position POS."
3059   (when (and gnus-summary-display-arrow
3060              (boundp 'overlay-arrow-position)
3061              (boundp 'overlay-arrow-string))
3062     (save-excursion
3063       (goto-char pos)
3064       (beginning-of-line)
3065       (unless overlay-arrow-position
3066         (setq overlay-arrow-position (make-marker)))
3067       (setq overlay-arrow-string "=>"
3068             overlay-arrow-position (set-marker overlay-arrow-position
3069                                                (point)
3070                                                (current-buffer))))))
3071
3072 (defun gnus-summary-setup-buffer (group)
3073   "Initialize summary buffer."
3074   (let ((buffer (gnus-summary-buffer-name group))
3075         (dead-name (concat "*Dead Summary "
3076                            (gnus-group-decoded-name group) "*")))
3077     ;; If a dead summary buffer exists, we kill it.
3078     (when (gnus-buffer-live-p dead-name)
3079       (gnus-kill-buffer dead-name))
3080     (if (get-buffer buffer)
3081         (progn
3082           (set-buffer buffer)
3083           (setq gnus-summary-buffer (current-buffer))
3084           (not gnus-newsgroup-prepared))
3085       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3086       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3087       (gnus-summary-mode group)
3088       (when gnus-carpal
3089         (gnus-carpal-setup-buffer 'summary))
3090       (unless gnus-single-article-buffer
3091         (make-local-variable 'gnus-article-buffer)
3092         (make-local-variable 'gnus-article-current)
3093         (make-local-variable 'gnus-original-article-buffer))
3094       (setq gnus-newsgroup-name group)
3095       ;; Set any local variables in the group parameters.
3096       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3097       t)))
3098
3099 (defun gnus-set-global-variables ()
3100   "Set the global equivalents of the buffer-local variables.
3101 They are set to the latest values they had.  These reflect the summary
3102 buffer that was in action when the last article was fetched."
3103   (when (eq major-mode 'gnus-summary-mode)
3104     (setq gnus-summary-buffer (current-buffer))
3105     (let ((name gnus-newsgroup-name)
3106           (marked gnus-newsgroup-marked)
3107           (spam gnus-newsgroup-spam-marked)
3108           (unread gnus-newsgroup-unreads)
3109           (headers gnus-current-headers)
3110           (data gnus-newsgroup-data)
3111           (summary gnus-summary-buffer)
3112           (article-buffer gnus-article-buffer)
3113           (original gnus-original-article-buffer)
3114           (gac gnus-article-current)
3115           (reffed gnus-reffed-article-number)
3116           (score-file gnus-current-score-file)
3117           (default-charset gnus-newsgroup-charset)
3118           vlist)
3119       (let ((locals gnus-newsgroup-variables))
3120         (while locals
3121           (if (consp (car locals))
3122               (push (eval (caar locals)) vlist)
3123             (push (eval (car locals)) vlist))
3124           (setq locals (cdr locals)))
3125         (setq vlist (nreverse vlist)))
3126       (save-excursion
3127         (set-buffer gnus-group-buffer)
3128         (setq gnus-newsgroup-name name
3129               gnus-newsgroup-marked marked
3130               gnus-newsgroup-spam-marked spam
3131               gnus-newsgroup-unreads unread
3132               gnus-current-headers headers
3133               gnus-newsgroup-data data
3134               gnus-article-current gac
3135               gnus-summary-buffer summary
3136               gnus-article-buffer article-buffer
3137               gnus-original-article-buffer original
3138               gnus-reffed-article-number reffed
3139               gnus-current-score-file score-file
3140               gnus-newsgroup-charset default-charset)
3141         (let ((locals gnus-newsgroup-variables))
3142           (while locals
3143             (if (consp (car locals))
3144                 (set (caar locals) (pop vlist))
3145               (set (car locals) (pop vlist)))
3146             (setq locals (cdr locals))))
3147         ;; The article buffer also has local variables.
3148         (when (gnus-buffer-live-p gnus-article-buffer)
3149           (set-buffer gnus-article-buffer)
3150           (setq gnus-summary-buffer summary))))))
3151
3152 (defun gnus-summary-article-unread-p (article)
3153   "Say whether ARTICLE is unread or not."
3154   (memq article gnus-newsgroup-unreads))
3155
3156 (defun gnus-summary-first-article-p (&optional article)
3157   "Return whether ARTICLE is the first article in the buffer."
3158   (if (not (setq article (or article (gnus-summary-article-number))))
3159       nil
3160     (eq article (caar gnus-newsgroup-data))))
3161
3162 (defun gnus-summary-last-article-p (&optional article)
3163   "Return whether ARTICLE is the last article in the buffer."
3164   (if (not (setq article (or article (gnus-summary-article-number))))
3165       ;; All non-existent numbers are the last article.  :-)
3166       t
3167     (not (cdr (gnus-data-find-list article)))))
3168
3169 (defun gnus-make-thread-indent-array ()
3170   (let ((n 200))
3171     (unless (and gnus-thread-indent-array
3172                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3173       (setq gnus-thread-indent-array (make-vector 201 "")
3174             gnus-thread-indent-array-level gnus-thread-indent-level)
3175       (while (>= n 0)
3176         (aset gnus-thread-indent-array n
3177               (make-string (* n gnus-thread-indent-level) ? ))
3178         (setq n (1- n))))))
3179
3180 (defun gnus-update-summary-mark-positions ()
3181   "Compute where the summary marks are to go."
3182   (save-excursion
3183     (when (gnus-buffer-exists-p gnus-summary-buffer)
3184       (set-buffer gnus-summary-buffer))
3185     (let ((gnus-replied-mark 129)
3186           (gnus-score-below-mark 130)
3187           (gnus-score-over-mark 130)
3188           (gnus-undownloaded-mark 131)
3189           (spec gnus-summary-line-format-spec)
3190           gnus-visual pos)
3191       (save-excursion
3192         (gnus-set-work-buffer)
3193         (let ((gnus-summary-line-format-spec spec)
3194               (gnus-newsgroup-downloadable '(0)))
3195           (gnus-summary-insert-line
3196            (make-full-mail-header 0 "" "nobody"
3197                                   "05 Apr 2001 23:33:09 +0400"
3198                                   "" "" 0 0 "" nil)
3199            0 nil t 128 t nil "" nil 1)
3200           (goto-char (point-min))
3201           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3202                                              (- (point) (point-min) 1)))))
3203           (goto-char (point-min))
3204           (push (cons 'replied (and (search-forward "\201" nil t)
3205                                     (- (point) (point-min) 1)))
3206                 pos)
3207           (goto-char (point-min))
3208           (push (cons 'score (and (search-forward "\202" nil t)
3209                                   (- (point) (point-min) 1)))
3210                 pos)
3211           (goto-char (point-min))
3212           (push (cons 'download
3213                       (and (search-forward "\203" nil t)
3214                            (- (point) (point-min) 1)))
3215                 pos)))
3216       (setq gnus-summary-mark-positions pos))))
3217
3218 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3219   "Insert a dummy root in the summary buffer."
3220   (beginning-of-line)
3221   (gnus-add-text-properties
3222    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3223    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3224
3225 (defun gnus-summary-extract-address-component (from)
3226   (or (car (funcall gnus-extract-address-components from))
3227       from))
3228
3229 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3230   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3231                                 default-mime-charset)))
3232     ;; Is it really necessary to do this next part for each summary line?
3233     ;; Luckily, doesn't seem to slow things down much.
3234     (or
3235      (and gnus-ignored-from-addresses
3236           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3237           (let ((extra-headers (mail-header-extra header))
3238                 to
3239                 newsgroups)
3240             (cond
3241              ((setq to (cdr (assq 'To extra-headers)))
3242               (concat "-> "
3243                       (inline
3244                         (gnus-summary-extract-address-component
3245                          (funcall gnus-decode-encoded-word-function to)))))
3246              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3247               (concat "=> " newsgroups)))))
3248      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3249
3250 (defun gnus-summary-insert-line (gnus-tmp-header
3251                                  gnus-tmp-level gnus-tmp-current
3252                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3253                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3254                                  &optional gnus-tmp-dummy gnus-tmp-score
3255                                  gnus-tmp-process)
3256   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3257          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3258          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3259          (gnus-tmp-score-char
3260           (if (or (null gnus-summary-default-score)
3261                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3262                       gnus-summary-zcore-fuzz))
3263               ?\ ;;;Whitespace
3264             (if (< gnus-tmp-score gnus-summary-default-score)
3265                 gnus-score-below-mark gnus-score-over-mark)))
3266          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3267          (gnus-tmp-replied
3268           (cond (gnus-tmp-process gnus-process-mark)
3269                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3270                  gnus-cached-mark)
3271                 (gnus-tmp-replied gnus-replied-mark)
3272                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3273                  gnus-forwarded-mark)
3274                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3275                  gnus-saved-mark)
3276                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3277                  gnus-recent-mark)
3278                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3279                  gnus-unseen-mark)
3280                 (t gnus-no-mark)))
3281          (gnus-tmp-downloaded
3282           (cond (undownloaded
3283                  gnus-undownloaded-mark)
3284                 (gnus-newsgroup-agentized
3285                  gnus-downloaded-mark)
3286                 (t
3287                  gnus-no-mark)))
3288          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3289          (gnus-tmp-name
3290           (cond
3291            ((string-match "<[^>]+> *$" gnus-tmp-from)
3292             (let ((beg (match-beginning 0)))
3293               (or (and (string-match "^\".+\"" gnus-tmp-from)
3294                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3295                   (substring gnus-tmp-from 0 beg))))
3296            ((string-match "(.+)" gnus-tmp-from)
3297             (substring gnus-tmp-from
3298                        (1+ (match-beginning 0)) (1- (match-end 0))))
3299            (t gnus-tmp-from)))
3300          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3301          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3302          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3303          (buffer-read-only nil))
3304     (when (string= gnus-tmp-name "")
3305       (setq gnus-tmp-name gnus-tmp-from))
3306     (unless (numberp gnus-tmp-lines)
3307       (setq gnus-tmp-lines -1))
3308     (if (= gnus-tmp-lines -1)
3309         (setq gnus-tmp-lines "?")
3310       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3311     (gnus-put-text-property-excluding-characters-with-faces
3312      (point)
3313      (progn (eval gnus-summary-line-format-spec) (point))
3314      'gnus-number gnus-tmp-number)
3315     (when (gnus-visual-p 'summary-highlight 'highlight)
3316       (forward-line -1)
3317       (gnus-run-hooks 'gnus-summary-update-hook)
3318       (forward-line 1))))
3319
3320 (defun gnus-summary-update-line (&optional dont-update)
3321   "Update summary line after change."
3322   (when (and gnus-summary-default-score
3323              (not gnus-summary-inhibit-highlight))
3324     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3325            (article (gnus-summary-article-number))
3326            (score (gnus-summary-article-score article)))
3327       (unless dont-update
3328         (if (and gnus-summary-mark-below
3329                  (< (gnus-summary-article-score)
3330                     gnus-summary-mark-below))
3331             ;; This article has a low score, so we mark it as read.
3332             (when (memq article gnus-newsgroup-unreads)
3333               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3334           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3335             ;; This article was previously marked as read on account
3336             ;; of a low score, but now it has risen, so we mark it as
3337             ;; unread.
3338             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3339         (gnus-summary-update-mark
3340          (if (or (null gnus-summary-default-score)
3341                  (<= (abs (- score gnus-summary-default-score))
3342                      gnus-summary-zcore-fuzz))
3343              ?\ ;;;Whitespace
3344            (if (< score gnus-summary-default-score)
3345                gnus-score-below-mark gnus-score-over-mark))
3346          'score))
3347       ;; Do visual highlighting.
3348       (when (gnus-visual-p 'summary-highlight 'highlight)
3349         (gnus-run-hooks 'gnus-summary-update-hook)))))
3350
3351 (defvar gnus-tmp-new-adopts nil)
3352
3353 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3354   "Return the number of articles in THREAD.
3355 This may be 0 in some cases -- if none of the articles in
3356 the thread are to be displayed."
3357   (let* ((number
3358           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3359           (cond
3360            ((not (listp thread))
3361             1)
3362            ((and (consp thread) (cdr thread))
3363             (apply
3364              '+ 1 (mapcar
3365                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3366            ((null thread)
3367             1)
3368            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3369             1)
3370            (t 0))))
3371     (when (and level (zerop level) gnus-tmp-new-adopts)
3372       (incf number
3373             (apply '+ (mapcar
3374                        'gnus-summary-number-of-articles-in-thread
3375                        gnus-tmp-new-adopts))))
3376     (if char
3377         (if (> number 1) gnus-not-empty-thread-mark
3378           gnus-empty-thread-mark)
3379       number)))
3380
3381 (defsubst gnus-summary-line-message-size (head)
3382   "Return pretty-printed version of message size.
3383 This function is intended to be used in
3384 `gnus-summary-line-format-alist'."
3385   (let ((c (or (mail-header-chars head) -1)))
3386     (cond ((< c 0) "n/a")               ; chars not available
3387           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3388           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3389           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3390           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3391
3392
3393 (defun gnus-summary-set-local-parameters (group)
3394   "Go through the local params of GROUP and set all variable specs in that list."
3395   (let ((params (gnus-group-find-parameter group))
3396         (vars '(quit-config))           ; Ignore quit-config.
3397         elem)
3398     (while params
3399       (setq elem (car params)
3400             params (cdr params))
3401       (and (consp elem)                 ; Has to be a cons.
3402            (consp (cdr elem))           ; The cdr has to be a list.
3403            (symbolp (car elem))         ; Has to be a symbol in there.
3404            (not (memq (car elem) vars))
3405            (ignore-errors               ; So we set it.
3406              (push (car elem) vars)
3407              (make-local-variable (car elem))
3408              (set (car elem) (eval (nth 1 elem))))))))
3409
3410 (defun gnus-summary-read-group (group &optional show-all no-article
3411                                       kill-buffer no-display backward
3412                                       select-articles)
3413   "Start reading news in newsgroup GROUP.
3414 If SHOW-ALL is non-nil, already read articles are also listed.
3415 If NO-ARTICLE is non-nil, no article is selected initially.
3416 If NO-DISPLAY, don't generate a summary buffer."
3417   (let (result)
3418     (while (and group
3419                 (null (setq result
3420                             (let ((gnus-auto-select-next nil))
3421                               (or (gnus-summary-read-group-1
3422                                    group show-all no-article
3423                                    kill-buffer no-display
3424                                    select-articles)
3425                                   (setq show-all nil
3426                                         select-articles nil)))))
3427                 (eq gnus-auto-select-next 'quietly))
3428       (set-buffer gnus-group-buffer)
3429       ;; The entry function called above goes to the next
3430       ;; group automatically, so we go two groups back
3431       ;; if we are searching for the previous group.
3432       (when backward
3433         (gnus-group-prev-unread-group 2))
3434       (if (not (equal group (gnus-group-group-name)))
3435           (setq group (gnus-group-group-name))
3436         (setq group nil)))
3437     result))
3438
3439 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3440   "Directly jump to the other GROUP from summary buffer.
3441 If SHOW-ALL is non-nil, already read articles are also listed."
3442   (interactive
3443    (if (eq gnus-summary-buffer (current-buffer))
3444        (list (completing-read
3445               "Group: " gnus-active-hashtb nil t
3446               (when (and gnus-newsgroup-name
3447                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3448                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3449               'gnus-group-history)
3450              current-prefix-arg)
3451      (error "%s must be invoked from a gnus summary buffer." this-command)))
3452   (unless (or (zerop (length group))
3453               (and gnus-newsgroup-name
3454                    (string-equal gnus-newsgroup-name group)))
3455     (gnus-summary-exit)
3456     (gnus-summary-read-group group show-all
3457                              gnus-dont-select-after-jump-to-other-group)))
3458
3459 (defun gnus-summary-read-group-1 (group show-all no-article
3460                                         kill-buffer no-display
3461                                         &optional select-articles)
3462   ;; Killed foreign groups can't be entered.
3463   ;;  (when (and (not (gnus-group-native-p group))
3464   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3465   ;;    (error "Dead non-native groups can't be entered"))
3466   (gnus-message 5 "Retrieving newsgroup: %s..."
3467                 (gnus-group-decoded-name group))
3468   (let* ((new-group (gnus-summary-setup-buffer group))
3469          (quit-config (gnus-group-quit-config group))
3470          (did-select (and new-group (gnus-select-newsgroup
3471                                      group show-all select-articles))))
3472     (cond
3473      ;; This summary buffer exists already, so we just select it.
3474      ((not new-group)
3475       (gnus-set-global-variables)
3476       (when kill-buffer
3477         (gnus-kill-or-deaden-summary kill-buffer))
3478       (gnus-configure-windows 'summary 'force)
3479       (gnus-set-mode-line 'summary)
3480       (gnus-summary-position-point)
3481       (message "")
3482       t)
3483      ;; We couldn't select this group.
3484      ((null did-select)
3485       (when (and (eq major-mode 'gnus-summary-mode)
3486                  (not (equal (current-buffer) kill-buffer)))
3487         (kill-buffer (current-buffer))
3488         (if (not quit-config)
3489             (progn
3490               ;; Update the info -- marks might need to be removed,
3491               ;; for instance.
3492               (gnus-summary-update-info)
3493               (set-buffer gnus-group-buffer)
3494               (gnus-group-jump-to-group group)
3495               (gnus-group-next-unread-group 1))
3496           (gnus-handle-ephemeral-exit quit-config)))
3497       (let ((grpinfo (gnus-get-info group)))
3498         (if (null (gnus-info-read grpinfo))
3499             (gnus-message 3 "Group %s contains no messages"
3500                           (gnus-group-decoded-name group))
3501           (gnus-message 3 "Can't select group")))
3502       nil)
3503      ;; The user did a `C-g' while prompting for number of articles,
3504      ;; so we exit this group.
3505      ((eq did-select 'quit)
3506       (and (eq major-mode 'gnus-summary-mode)
3507            (not (equal (current-buffer) kill-buffer))
3508            (kill-buffer (current-buffer)))
3509       (when kill-buffer
3510         (gnus-kill-or-deaden-summary kill-buffer))
3511       (if (not quit-config)
3512           (progn
3513             (set-buffer gnus-group-buffer)
3514             (gnus-group-jump-to-group group)
3515             (gnus-group-next-unread-group 1)
3516             (gnus-configure-windows 'group 'force))
3517         (gnus-handle-ephemeral-exit quit-config))
3518       ;; Finally signal the quit.
3519       (signal 'quit nil))
3520      ;; The group was successfully selected.
3521      (t
3522       (gnus-set-global-variables)
3523       ;; Save the active value in effect when the group was entered.
3524       (setq gnus-newsgroup-active
3525             (gnus-copy-sequence
3526              (gnus-active gnus-newsgroup-name)))
3527       ;; You can change the summary buffer in some way with this hook.
3528       (gnus-run-hooks 'gnus-select-group-hook)
3529       (gnus-update-format-specifications
3530        nil 'summary 'summary-mode 'summary-dummy)
3531       (gnus-update-summary-mark-positions)
3532       ;; Do score processing.
3533       (when gnus-use-scoring
3534         (gnus-possibly-score-headers))
3535       ;; Check whether to fill in the gaps in the threads.
3536       (when gnus-build-sparse-threads
3537         (gnus-build-sparse-threads))
3538       ;; Find the initial limit.
3539       (if gnus-show-threads
3540           (if show-all
3541               (let ((gnus-newsgroup-dormant nil))
3542                 (gnus-summary-initial-limit show-all))
3543             (gnus-summary-initial-limit show-all))
3544         ;; When unthreaded, all articles are always shown.
3545         (setq gnus-newsgroup-limit
3546               (mapcar
3547                (lambda (header) (mail-header-number header))
3548                gnus-newsgroup-headers)))
3549       ;; Generate the summary buffer.
3550       (unless no-display
3551         (gnus-summary-prepare))
3552       (when gnus-use-trees
3553         (gnus-tree-open group)
3554         (setq gnus-summary-highlight-line-function
3555               'gnus-tree-highlight-article))
3556       ;; If the summary buffer is empty, but there are some low-scored
3557       ;; articles or some excluded dormants, we include these in the
3558       ;; buffer.
3559       (when (and (zerop (buffer-size))
3560                  (not no-display))
3561         (cond (gnus-newsgroup-dormant
3562                (gnus-summary-limit-include-dormant))
3563               ((and gnus-newsgroup-scored show-all)
3564                (gnus-summary-limit-include-expunged t))))
3565       ;; Function `gnus-apply-kill-file' must be called in this hook.
3566       (gnus-run-hooks 'gnus-apply-kill-hook)
3567       (if (and (zerop (buffer-size))
3568                (not no-display))
3569           (progn
3570             ;; This newsgroup is empty.
3571             (gnus-summary-catchup-and-exit nil t)
3572             (gnus-message 6 "No unread news")
3573             (when kill-buffer
3574               (gnus-kill-or-deaden-summary kill-buffer))
3575             ;; Return nil from this function.
3576             nil)
3577         ;; Hide conversation thread subtrees.  We cannot do this in
3578         ;; gnus-summary-prepare-hook since kill processing may not
3579         ;; work with hidden articles.
3580         (gnus-summary-maybe-hide-threads)
3581         (when kill-buffer
3582           (gnus-kill-or-deaden-summary kill-buffer))
3583         (gnus-summary-auto-select-subject)
3584         ;; Show first unread article if requested.
3585         (if (and (not no-article)
3586                  (not no-display)
3587                  gnus-newsgroup-unreads
3588                  gnus-auto-select-first)
3589             (progn
3590               (gnus-configure-windows 'summary)
3591               (let ((art (gnus-summary-article-number)))
3592                 (unless (and (not gnus-plugged)
3593                              (or (memq art gnus-newsgroup-undownloaded)
3594                                  (memq art gnus-newsgroup-downloadable)))
3595                   (gnus-summary-goto-article art))))
3596           ;; Don't select any articles.
3597           (gnus-summary-position-point)
3598           (gnus-configure-windows 'summary 'force)
3599           (gnus-set-mode-line 'summary))
3600         (when (and gnus-auto-center-group
3601                    (get-buffer-window gnus-group-buffer t))
3602           ;; Gotta use windows, because recenter does weird stuff if
3603           ;; the current buffer ain't the displayed window.
3604           (let ((owin (selected-window)))
3605             (select-window (get-buffer-window gnus-group-buffer t))
3606             (when (gnus-group-goto-group group)
3607               (recenter))
3608             (select-window owin)))
3609         ;; Mark this buffer as "prepared".
3610         (setq gnus-newsgroup-prepared t)
3611         (gnus-run-hooks 'gnus-summary-prepared-hook)
3612         (unless (gnus-ephemeral-group-p group)
3613           (gnus-group-update-group group))
3614         t)))))
3615
3616 (defun gnus-summary-auto-select-subject ()
3617   "Select the subject line on initial group entry."
3618   (goto-char (point-min))
3619   (cond
3620    ((eq gnus-auto-select-subject 'best)
3621     (gnus-summary-best-unread-subject))
3622    ((eq gnus-auto-select-subject 'unread)
3623     (gnus-summary-first-unread-subject))
3624    ((eq gnus-auto-select-subject 'unseen)
3625     (gnus-summary-first-unseen-subject))
3626    ((eq gnus-auto-select-subject 'unseen-or-unread)
3627     (gnus-summary-first-unseen-or-unread-subject))
3628    ((eq gnus-auto-select-subject 'first)
3629     ;; Do nothing.
3630     )
3631    ((functionp gnus-auto-select-subject)
3632     (funcall gnus-auto-select-subject))))
3633
3634 (defun gnus-summary-prepare ()
3635   "Generate the summary buffer."
3636   (interactive)
3637   (let ((buffer-read-only nil))
3638     (erase-buffer)
3639     (setq gnus-newsgroup-data nil
3640           gnus-newsgroup-data-reverse nil)
3641     (gnus-run-hooks 'gnus-summary-generate-hook)
3642     ;; Generate the buffer, either with threads or without.
3643     (when gnus-newsgroup-headers
3644       (gnus-summary-prepare-threads
3645        (if gnus-show-threads
3646            (gnus-sort-gathered-threads
3647             (funcall gnus-summary-thread-gathering-function
3648                      (gnus-sort-threads
3649                       (gnus-cut-threads (gnus-make-threads)))))
3650          ;; Unthreaded display.
3651          (gnus-sort-articles gnus-newsgroup-headers))))
3652     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3653     ;; Call hooks for modifying summary buffer.
3654     (goto-char (point-min))
3655     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3656
3657 (defsubst gnus-general-simplify-subject (subject)
3658   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3659   (setq subject
3660         (cond
3661          ;; Truncate the subject.
3662          (gnus-simplify-subject-functions
3663           (gnus-map-function gnus-simplify-subject-functions subject))
3664          ((numberp gnus-summary-gather-subject-limit)
3665           (setq subject (gnus-simplify-subject-re subject))
3666           (if (> (length subject) gnus-summary-gather-subject-limit)
3667               (substring subject 0 gnus-summary-gather-subject-limit)
3668             subject))
3669          ;; Fuzzily simplify it.
3670          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3671           (gnus-simplify-subject-fuzzy subject))
3672          ;; Just remove the leading "Re:".
3673          (t
3674           (gnus-simplify-subject-re subject))))
3675
3676   (if (and gnus-summary-gather-exclude-subject
3677            (string-match gnus-summary-gather-exclude-subject subject))
3678       nil                               ; This article shouldn't be gathered
3679     subject))
3680
3681 (defun gnus-summary-simplify-subject-query ()
3682   "Query where the respool algorithm would put this article."
3683   (interactive)
3684   (gnus-summary-select-article)
3685   (message "%s"
3686            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3687
3688 (defun gnus-gather-threads-by-subject (threads)
3689   "Gather threads by looking at Subject headers."
3690   (if (not gnus-summary-make-false-root)
3691       threads
3692     (let ((hashtb (gnus-make-hashtable 1024))
3693           (prev threads)
3694           (result threads)
3695           subject hthread whole-subject)
3696       (while threads
3697         (setq subject (gnus-general-simplify-subject
3698                        (setq whole-subject (mail-header-subject
3699                                             (caar threads)))))
3700         (when subject
3701           (if (setq hthread (gnus-gethash subject hashtb))
3702               (progn
3703                 ;; We enter a dummy root into the thread, if we
3704                 ;; haven't done that already.
3705                 (unless (stringp (caar hthread))
3706                   (setcar hthread (list whole-subject (car hthread))))
3707                 ;; We add this new gathered thread to this gathered
3708                 ;; thread.
3709                 (setcdr (car hthread)
3710                         (nconc (cdar hthread) (list (car threads))))
3711                 ;; Remove it from the list of threads.
3712                 (setcdr prev (cdr threads))
3713                 (setq threads prev))
3714             ;; Enter this thread into the hash table.
3715             (gnus-sethash subject
3716                           (if gnus-summary-make-false-root-always
3717                               (progn
3718                                 ;; If you want a dummy root above all
3719                                 ;; threads...
3720                                 (setcar threads (list whole-subject
3721                                                       (car threads)))
3722                                 threads)
3723                             threads)
3724                           hashtb)))
3725         (setq prev threads)
3726         (setq threads (cdr threads)))
3727       result)))
3728
3729 (defun gnus-gather-threads-by-references (threads)
3730   "Gather threads by looking at References headers."
3731   (let ((idhashtb (gnus-make-hashtable 1024))
3732         (thhashtb (gnus-make-hashtable 1024))
3733         (prev threads)
3734         (result threads)
3735         ids references id gthread gid entered ref)
3736     (while threads
3737       (when (setq references (mail-header-references (caar threads)))
3738         (setq id (mail-header-id (caar threads))
3739               ids (inline (gnus-split-references references))
3740               entered nil)
3741         (while (setq ref (pop ids))
3742           (setq ids (delete ref ids))
3743           (if (not (setq gid (gnus-gethash ref idhashtb)))
3744               (progn
3745                 (gnus-sethash ref id idhashtb)
3746                 (gnus-sethash id threads thhashtb))
3747             (setq gthread (gnus-gethash gid thhashtb))
3748             (unless entered
3749               ;; We enter a dummy root into the thread, if we
3750               ;; haven't done that already.
3751               (unless (stringp (caar gthread))
3752                 (setcar gthread (list (mail-header-subject (caar gthread))
3753                                       (car gthread))))
3754               ;; We add this new gathered thread to this gathered
3755               ;; thread.
3756               (setcdr (car gthread)
3757                       (nconc (cdar gthread) (list (car threads)))))
3758             ;; Add it into the thread hash table.
3759             (gnus-sethash id gthread thhashtb)
3760             (setq entered t)
3761             ;; Remove it from the list of threads.
3762             (setcdr prev (cdr threads))
3763             (setq threads prev))))
3764       (setq prev threads)
3765       (setq threads (cdr threads)))
3766     result))
3767
3768 (defun gnus-sort-gathered-threads (threads)
3769   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3770   (let ((result threads))
3771     (while threads
3772       (when (stringp (caar threads))
3773         (setcdr (car threads)
3774                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3775       (setq threads (cdr threads)))
3776     result))
3777
3778 (defun gnus-thread-loop-p (root thread)
3779   "Say whether ROOT is in THREAD."
3780   (let ((stack (list thread))
3781         (infloop 0)
3782         th)
3783     (while (setq thread (pop stack))
3784       (setq th (cdr thread))
3785       (while (and th
3786                   (not (eq (caar th) root)))
3787         (pop th))
3788       (if th
3789           ;; We have found a loop.
3790           (let (ref-dep)
3791             (setcdr thread (delq (car th) (cdr thread)))
3792             (if (boundp (setq ref-dep (intern "none"
3793                                               gnus-newsgroup-dependencies)))
3794                 (setcdr (symbol-value ref-dep)
3795                         (nconc (cdr (symbol-value ref-dep))
3796                                (list (car th))))
3797               (set ref-dep (list nil (car th))))
3798             (setq infloop 1
3799                   stack nil))
3800         ;; Push all the subthreads onto the stack.
3801         (push (cdr thread) stack)))
3802     infloop))
3803
3804 (defun gnus-make-threads ()
3805   "Go through the dependency hashtb and find the roots.  Return all threads."
3806   (let (threads)
3807     (while (catch 'infloop
3808              (mapatoms
3809               (lambda (refs)
3810                 ;; Deal with self-referencing References loops.
3811                 (when (and (car (symbol-value refs))
3812                            (not (zerop
3813                                  (apply
3814                                   '+
3815                                   (mapcar
3816                                    (lambda (thread)
3817                                      (gnus-thread-loop-p
3818                                       (car (symbol-value refs)) thread))
3819                                    (cdr (symbol-value refs)))))))
3820                   (setq threads nil)
3821                   (throw 'infloop t))
3822                 (unless (car (symbol-value refs))
3823                   ;; These threads do not refer back to any other articles,
3824                   ;; so they're roots.
3825                   (setq threads (append (cdr (symbol-value refs)) threads))))
3826               gnus-newsgroup-dependencies)))
3827     threads))
3828
3829 ;; Build the thread tree.
3830 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3831   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3832
3833 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3834 if it was already present.
3835
3836 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3837 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3838 Message-IDs will be renamed to a unique Message-ID before being
3839 entered.
3840
3841 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3842   (let* ((id (mail-header-id header))
3843          (id-dep (and id (intern id dependencies)))
3844          parent-id ref ref-dep ref-header replaced)
3845     ;; Enter this `header' in the `dependencies' table.
3846     (cond
3847      ((not id-dep)
3848       (setq header nil))
3849      ;; The first two cases do the normal part: enter a new `header'
3850      ;; in the `dependencies' table.
3851      ((not (boundp id-dep))
3852       (set id-dep (list header)))
3853      ((null (car (symbol-value id-dep)))
3854       (setcar (symbol-value id-dep) header))
3855
3856      ;; From here the `header' was already present in the
3857      ;; `dependencies' table.
3858      (force-new
3859       ;; Overrides an existing entry;
3860       ;; just set the header part of the entry.
3861       (setcar (symbol-value id-dep) header)
3862       (setq replaced t))
3863
3864      ;; Renames the existing `header' to a unique Message-ID.
3865      ((not gnus-summary-ignore-duplicates)
3866       ;; An article with this Message-ID has already been seen.
3867       ;; We rename the Message-ID.
3868       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3869            (list header))
3870       (mail-header-set-id header id))
3871
3872      ;; The last case ignores an existing entry, except it adds any
3873      ;; additional Xrefs (in case the two articles came from different
3874      ;; servers.
3875      ;; Also sets `header' to `nil' meaning that the `dependencies'
3876      ;; table was *not* modified.
3877      (t
3878       (mail-header-set-xref
3879        (car (symbol-value id-dep))
3880        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3881                    "")
3882                (or (mail-header-xref header) "")))
3883       (setq header nil)))
3884
3885     (when (and header (not replaced))
3886       ;; First check that we are not creating a References loop.
3887       (setq parent-id (gnus-parent-id (mail-header-references header)))
3888       (setq ref parent-id)
3889       (while (and ref
3890                   (setq ref-dep (intern-soft ref dependencies))
3891                   (boundp ref-dep)
3892                   (setq ref-header (car (symbol-value ref-dep))))
3893         (if (string= id ref)
3894             ;; Yuk!  This is a reference loop.  Make the article be a
3895             ;; root article.
3896             (progn
3897               (mail-header-set-references (car (symbol-value id-dep)) "none")
3898               (setq ref nil)
3899               (setq parent-id nil))
3900           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3901       (setq ref-dep (intern (or parent-id "none") dependencies))
3902       (if (boundp ref-dep)
3903           (setcdr (symbol-value ref-dep)
3904                   (nconc (cdr (symbol-value ref-dep))
3905                          (list (symbol-value id-dep))))
3906         (set ref-dep (list nil (symbol-value id-dep)))))
3907     header))
3908
3909 (defun gnus-extract-message-id-from-in-reply-to (string)
3910   (if (string-match "<[^>]+>" string)
3911       (substring string (match-beginning 0) (match-end 0))
3912     nil))
3913
3914 (defun gnus-build-sparse-threads ()
3915   (let ((headers gnus-newsgroup-headers)
3916         (mail-parse-charset gnus-newsgroup-charset)
3917         (gnus-summary-ignore-duplicates t)
3918         header references generation relations
3919         subject child end new-child date)
3920     ;; First we create an alist of generations/relations, where
3921     ;; generations is how much we trust the relation, and the relation
3922     ;; is parent/child.
3923     (gnus-message 7 "Making sparse threads...")
3924     (save-excursion
3925       (nnheader-set-temp-buffer " *gnus sparse threads*")
3926       (while (setq header (pop headers))
3927         (when (and (setq references (mail-header-references header))
3928                    (not (string= references "")))
3929           (insert references)
3930           (setq child (mail-header-id header)
3931                 subject (mail-header-subject header)
3932                 date (mail-header-date header)
3933                 generation 0)
3934           (while (search-backward ">" nil t)
3935             (setq end (1+ (point)))
3936             (when (search-backward "<" nil t)
3937               (setq new-child (buffer-substring (point) end))
3938               (push (list (incf generation)
3939                           child (setq child new-child)
3940                           subject date)
3941                     relations)))
3942           (when child
3943             (push (list (1+ generation) child nil subject) relations))
3944           (erase-buffer)))
3945       (kill-buffer (current-buffer)))
3946     ;; Sort over trustworthiness.
3947     (mapcar
3948      (lambda (relation)
3949        (when (gnus-dependencies-add-header
3950               (make-full-mail-header-from-decoded-header
3951                gnus-reffed-article-number
3952                (nth 3 relation) "" (or (nth 4 relation) "")
3953                (nth 1 relation)
3954                (or (nth 2 relation) "") 0 0 "")
3955               gnus-newsgroup-dependencies nil)
3956          (push gnus-reffed-article-number gnus-newsgroup-limit)
3957          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3958          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3959                gnus-newsgroup-reads)
3960          (decf gnus-reffed-article-number)))
3961      (sort relations 'car-less-than-car))
3962     (gnus-message 7 "Making sparse threads...done")))
3963
3964 (defun gnus-build-old-threads ()
3965   ;; Look at all the articles that refer back to old articles, and
3966   ;; fetch the headers for the articles that aren't there.  This will
3967   ;; build complete threads - if the roots haven't been expired by the
3968   ;; server, that is.
3969   (let ((mail-parse-charset gnus-newsgroup-charset)
3970         id heads)
3971     (mapatoms
3972      (lambda (refs)
3973        (when (not (car (symbol-value refs)))
3974          (setq heads (cdr (symbol-value refs)))
3975          (while heads
3976            (if (memq (mail-header-number (caar heads))
3977                      gnus-newsgroup-dormant)
3978                (setq heads (cdr heads))
3979              (setq id (symbol-name refs))
3980              (while (and (setq id (gnus-build-get-header id))
3981                          (not (car (gnus-id-to-thread id)))))
3982              (setq heads nil)))))
3983      gnus-newsgroup-dependencies)))
3984
3985 (defsubst gnus-remove-odd-characters (string)
3986   "Translate STRING into something that doesn't contain weird characters."
3987   (mm-subst-char-in-string
3988    ?\r ?\-
3989    (mm-subst-char-in-string
3990     ?\n ?\- string)))
3991
3992 ;; This function has to be called with point after the article number
3993 ;; on the beginning of the line.
3994 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3995   (let ((eol (gnus-point-at-eol))
3996         (buffer (current-buffer))
3997         header references in-reply-to)
3998
3999     ;; overview: [num subject from date id refs chars lines misc]
4000     (unwind-protect
4001         (progn
4002           (narrow-to-region (point) eol)
4003           (unless (eobp)
4004             (forward-char))
4005
4006           (setq header
4007                 (make-full-mail-header
4008                  number                         ; number
4009                  (nnheader-nov-field)           ; subject
4010                  (nnheader-nov-field)           ; from
4011                  (nnheader-nov-field)           ; date
4012                  (nnheader-nov-read-message-id) ; id
4013                  (nnheader-nov-field)           ; refs
4014                  (nnheader-nov-read-integer)    ; chars
4015                  (nnheader-nov-read-integer)    ; lines
4016                  (unless (eobp)
4017                    (if (looking-at "Xref: ")
4018                        (goto-char (match-end 0)))
4019                    (nnheader-nov-field))        ; Xref
4020                  (nnheader-nov-parse-extra))))  ; extra
4021
4022       (widen))
4023
4024     (when (and (string= references "")
4025                (setq in-reply-to (mail-header-extra header))
4026                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4027       (mail-header-set-references
4028        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4029
4030     (when gnus-alter-header-function
4031       (funcall gnus-alter-header-function header))
4032     (gnus-dependencies-add-header header dependencies force-new)))
4033
4034 (defun gnus-build-get-header (id)
4035   "Look through the buffer of NOV lines and find the header to ID.
4036 Enter this line into the dependencies hash table, and return
4037 the id of the parent article (if any)."
4038   (let ((deps gnus-newsgroup-dependencies)
4039         found header)
4040     (prog1
4041         (save-excursion
4042           (set-buffer nntp-server-buffer)
4043           (let ((case-fold-search nil))
4044             (goto-char (point-min))
4045             (while (and (not found)
4046                         (search-forward id nil t))
4047               (beginning-of-line)
4048               (setq found (looking-at
4049                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4050                                    (regexp-quote id))))
4051               (or found (beginning-of-line 2)))
4052             (when found
4053               (beginning-of-line)
4054               (and
4055                (setq header (gnus-nov-parse-line
4056                              (read (current-buffer)) deps))
4057                (gnus-parent-id (mail-header-references header))))))
4058       (when header
4059         (let ((number (mail-header-number header)))
4060           (push number gnus-newsgroup-limit)
4061           (push header gnus-newsgroup-headers)
4062           (if (memq number gnus-newsgroup-unselected)
4063               (progn
4064                 (setq gnus-newsgroup-unreads
4065                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4066                                                number))
4067                 (setq gnus-newsgroup-unselected
4068                       (delq number gnus-newsgroup-unselected)))
4069             (push number gnus-newsgroup-ancient)))))))
4070
4071 (defun gnus-build-all-threads ()
4072   "Read all the headers."
4073   (let ((gnus-summary-ignore-duplicates t)
4074         (mail-parse-charset gnus-newsgroup-charset)
4075         (dependencies gnus-newsgroup-dependencies)
4076         header article)
4077     (save-excursion
4078       (set-buffer nntp-server-buffer)
4079       (let ((case-fold-search nil))
4080         (goto-char (point-min))
4081         (while (not (eobp))
4082           (ignore-errors
4083             (setq article (read (current-buffer))
4084                   header (gnus-nov-parse-line article dependencies)))
4085           (when header
4086             (save-excursion
4087               (set-buffer gnus-summary-buffer)
4088               (push header gnus-newsgroup-headers)
4089               (if (memq (setq article (mail-header-number header))
4090                         gnus-newsgroup-unselected)
4091                   (progn
4092                     (setq gnus-newsgroup-unreads
4093                           (gnus-add-to-sorted-list
4094                            gnus-newsgroup-unreads article))
4095                     (setq gnus-newsgroup-unselected
4096                           (delq article gnus-newsgroup-unselected)))
4097                 (push article gnus-newsgroup-ancient)))
4098             (forward-line 1)))))))
4099
4100 (defun gnus-summary-update-article-line (article header)
4101   "Update the line for ARTICLE using HEADER."
4102   (let* ((id (mail-header-id header))
4103          (thread (gnus-id-to-thread id)))
4104     (unless thread
4105       (error "Article in no thread"))
4106     ;; Update the thread.
4107     (setcar thread header)
4108     (gnus-summary-goto-subject article)
4109     (let* ((datal (gnus-data-find-list article))
4110            (data (car datal))
4111            (buffer-read-only nil)
4112            (level (gnus-summary-thread-level)))
4113       (gnus-delete-line)
4114       (let ((inserted (- (point)
4115                          (progn
4116                            (gnus-summary-insert-line
4117                             header level nil
4118                             (memq article gnus-newsgroup-undownloaded)
4119                             (gnus-article-mark article)
4120                             (memq article gnus-newsgroup-replied)
4121                             (memq article gnus-newsgroup-expirable)
4122                             ;; Only insert the Subject string when it's different
4123                             ;; from the previous Subject string.
4124                             (if (and
4125                                  gnus-show-threads
4126                                  (gnus-subject-equal
4127                                   (condition-case ()
4128                                       (mail-header-subject
4129                                        (gnus-data-header
4130                                         (cadr
4131                                          (gnus-data-find-list
4132                                           article
4133                                           (gnus-data-list t)))))
4134                                     ;; Error on the side of excessive subjects.
4135                                     (error ""))
4136                                   (mail-header-subject header)))
4137                                 ""
4138                               (mail-header-subject header))
4139                             nil (cdr (assq article gnus-newsgroup-scored))
4140                             (memq article gnus-newsgroup-processable))
4141                            (point)))))
4142         (when (cdr datal)
4143           (gnus-data-update-list
4144            (cdr datal)
4145            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4146
4147 (defun gnus-summary-update-article (article &optional iheader)
4148   "Update ARTICLE in the summary buffer."
4149   (set-buffer gnus-summary-buffer)
4150   (let* ((header (gnus-summary-article-header article))
4151          (id (mail-header-id header))
4152          (data (gnus-data-find article))
4153          (thread (gnus-id-to-thread id))
4154          (references (mail-header-references header))
4155          (parent
4156           (gnus-id-to-thread
4157            (or (gnus-parent-id
4158                 (when (and references
4159                            (not (equal "" references)))
4160                   references))
4161                "none")))
4162          (buffer-read-only nil)
4163          (old (car thread)))
4164     (when thread
4165       (unless iheader
4166         (setcar thread nil)
4167         (when parent
4168           (delq thread parent)))
4169       (if (gnus-summary-insert-subject id header)
4170           ;; Set the (possibly) new article number in the data structure.
4171           (gnus-data-set-number data (gnus-id-to-article id))
4172         (setcar thread old)
4173         nil))))
4174
4175 (defun gnus-rebuild-thread (id &optional line)
4176   "Rebuild the thread containing ID.
4177 If LINE, insert the rebuilt thread starting on line LINE."
4178   (let ((buffer-read-only nil)
4179         old-pos current thread data)
4180     (if (not gnus-show-threads)
4181         (setq thread (list (car (gnus-id-to-thread id))))
4182       ;; Get the thread this article is part of.
4183       (setq thread (gnus-remove-thread id)))
4184     (setq old-pos (gnus-point-at-bol))
4185     (setq current (save-excursion
4186                     (and (re-search-backward "[\r\n]" nil t)
4187                          (gnus-summary-article-number))))
4188     ;; If this is a gathered thread, we have to go some re-gathering.
4189     (when (stringp (car thread))
4190       (let ((subject (car thread))
4191             roots thr)
4192         (setq thread (cdr thread))
4193         (while thread
4194           (unless (memq (setq thr (gnus-id-to-thread
4195                                    (gnus-root-id
4196                                     (mail-header-id (caar thread)))))
4197                         roots)
4198             (push thr roots))
4199           (setq thread (cdr thread)))
4200         ;; We now have all (unique) roots.
4201         (if (= (length roots) 1)
4202             ;; All the loose roots are now one solid root.
4203             (setq thread (car roots))
4204           (setq thread (cons subject (gnus-sort-threads roots))))))
4205     (let (threads)
4206       ;; We then insert this thread into the summary buffer.
4207       (when line
4208         (goto-char (point-min))
4209         (forward-line (1- line)))
4210       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4211         (if gnus-show-threads
4212             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4213           (gnus-summary-prepare-unthreaded thread))
4214         (setq data (nreverse gnus-newsgroup-data))
4215         (setq threads gnus-newsgroup-threads))
4216       ;; We splice the new data into the data structure.
4217       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4218       ;;!!! then we want to insert at the beginning of the buffer.
4219       ;;!!! That happens to be true with Gnus now, but that may
4220       ;;!!! change in the future.  Perhaps.
4221       (gnus-data-enter-list
4222        (if line nil current) data (- (point) old-pos))
4223       (setq gnus-newsgroup-threads
4224             (nconc threads gnus-newsgroup-threads))
4225       (gnus-data-compute-positions))))
4226
4227 (defun gnus-number-to-header (number)
4228   "Return the header for article NUMBER."
4229   (let ((headers gnus-newsgroup-headers))
4230     (while (and headers
4231                 (not (= number (mail-header-number (car headers)))))
4232       (pop headers))
4233     (when headers
4234       (car headers))))
4235
4236 (defun gnus-parent-headers (in-headers &optional generation)
4237   "Return the headers of the GENERATIONeth parent of HEADERS."
4238   (unless generation
4239     (setq generation 1))
4240   (let ((parent t)
4241         (headers in-headers)
4242         references)
4243     (while (and parent
4244                 (not (zerop generation))
4245                 (setq references (mail-header-references headers)))
4246       (setq headers (if (and references
4247                              (setq parent (gnus-parent-id references)))
4248                         (car (gnus-id-to-thread parent))
4249                       nil))
4250       (decf generation))
4251     (and (not (eq headers in-headers))
4252          headers)))
4253
4254 (defun gnus-id-to-thread (id)
4255   "Return the (sub-)thread where ID appears."
4256   (gnus-gethash id gnus-newsgroup-dependencies))
4257
4258 (defun gnus-id-to-article (id)
4259   "Return the article number of ID."
4260   (let ((thread (gnus-id-to-thread id)))
4261     (when (and thread
4262                (car thread))
4263       (mail-header-number (car thread)))))
4264
4265 (defun gnus-id-to-header (id)
4266   "Return the article headers of ID."
4267   (car (gnus-id-to-thread id)))
4268
4269 (defun gnus-article-displayed-root-p (article)
4270   "Say whether ARTICLE is a root(ish) article."
4271   (let ((level (gnus-summary-thread-level article))
4272         (refs (mail-header-references  (gnus-summary-article-header article)))
4273         particle)
4274     (cond
4275      ((null level) nil)
4276      ((zerop level) t)
4277      ((null refs) t)
4278      ((null (gnus-parent-id refs)) t)
4279      ((and (= 1 level)
4280            (null (setq particle (gnus-id-to-article
4281                                  (gnus-parent-id refs))))
4282            (null (gnus-summary-thread-level particle)))))))
4283
4284 (defun gnus-root-id (id)
4285   "Return the id of the root of the thread where ID appears."
4286   (let (last-id prev)
4287     (while (and id (setq prev (car (gnus-id-to-thread id))))
4288       (setq last-id id
4289             id (gnus-parent-id (mail-header-references prev))))
4290     last-id))
4291
4292 (defun gnus-articles-in-thread (thread)
4293   "Return the list of articles in THREAD."
4294   (cons (mail-header-number (car thread))
4295         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4296
4297 (defun gnus-remove-thread (id &optional dont-remove)
4298   "Remove the thread that has ID in it."
4299   (let (headers thread last-id)
4300     ;; First go up in this thread until we find the root.
4301     (setq last-id (gnus-root-id id)
4302           headers (message-flatten-list (gnus-id-to-thread last-id)))
4303     ;; We have now found the real root of this thread.  It might have
4304     ;; been gathered into some loose thread, so we have to search
4305     ;; through the threads to find the thread we wanted.
4306     (let ((threads gnus-newsgroup-threads)
4307           sub)
4308       (while threads
4309         (setq sub (car threads))
4310         (if (stringp (car sub))
4311             ;; This is a gathered thread, so we look at the roots
4312             ;; below it to find whether this article is in this
4313             ;; gathered root.
4314             (progn
4315               (setq sub (cdr sub))
4316               (while sub
4317                 (when (member (caar sub) headers)
4318                   (setq thread (car threads)
4319                         threads nil
4320                         sub nil))
4321                 (setq sub (cdr sub))))
4322           ;; It's an ordinary thread, so we check it.
4323           (when (eq (car sub) (car headers))
4324             (setq thread sub
4325                   threads nil)))
4326         (setq threads (cdr threads)))
4327       ;; If this article is in no thread, then it's a root.
4328       (if thread
4329           (unless dont-remove
4330             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4331         (setq thread (gnus-id-to-thread last-id)))
4332       (when thread
4333         (prog1
4334             thread                      ; We return this thread.
4335           (unless dont-remove
4336             (if (stringp (car thread))
4337                 (progn
4338                   ;; If we use dummy roots, then we have to remove the
4339                   ;; dummy root as well.
4340                   (when (eq gnus-summary-make-false-root 'dummy)
4341                     ;; We go to the dummy root by going to
4342                     ;; the first sub-"thread", and then one line up.
4343                     (gnus-summary-goto-article
4344                      (mail-header-number (caadr thread)))
4345                     (forward-line -1)
4346                     (gnus-delete-line)
4347                     (gnus-data-compute-positions))
4348                   (setq thread (cdr thread))
4349                   (while thread
4350                     (gnus-remove-thread-1 (car thread))
4351                     (setq thread (cdr thread))))
4352               (gnus-remove-thread-1 thread))))))))
4353
4354 (defun gnus-remove-thread-1 (thread)
4355   "Remove the thread THREAD recursively."
4356   (let ((number (mail-header-number (pop thread)))
4357         d)
4358     (setq thread (reverse thread))
4359     (while thread
4360       (gnus-remove-thread-1 (pop thread)))
4361     (when (setq d (gnus-data-find number))
4362       (goto-char (gnus-data-pos d))
4363       (gnus-summary-show-thread)
4364       (gnus-data-remove
4365        number
4366        (- (gnus-point-at-bol)
4367           (prog1
4368               (1+ (gnus-point-at-eol))
4369             (gnus-delete-line)))))))
4370
4371 (defun gnus-sort-threads-1 (threads func)
4372   (sort (mapcar (lambda (thread)
4373                   (cons (car thread)
4374                         (and (cdr thread)
4375                              (gnus-sort-threads-1 (cdr thread) func))))
4376                 threads) func))
4377
4378 (defun gnus-sort-threads (threads)
4379   "Sort THREADS."
4380   (if (not gnus-thread-sort-functions)
4381       threads
4382     (gnus-message 8 "Sorting threads...")
4383     (let ((max-lisp-eval-depth 5000))
4384       (prog1 (gnus-sort-threads-1
4385          threads
4386          (gnus-make-sort-function gnus-thread-sort-functions))
4387         (gnus-message 8 "Sorting threads...done")))))
4388
4389 (defun gnus-sort-articles (articles)
4390   "Sort ARTICLES."
4391   (when gnus-article-sort-functions
4392     (gnus-message 7 "Sorting articles...")
4393     (prog1
4394         (setq gnus-newsgroup-headers
4395               (sort articles (gnus-make-sort-function
4396                               gnus-article-sort-functions)))
4397       (gnus-message 7 "Sorting articles...done"))))
4398
4399 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4400 (defmacro gnus-thread-header (thread)
4401   "Return header of first article in THREAD.
4402 Note that THREAD must never, ever be anything else than a variable -
4403 using some other form will lead to serious barfage."
4404   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4405   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4406   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4407         (vector thread) 2))
4408
4409 (defsubst gnus-article-sort-by-number (h1 h2)
4410   "Sort articles by article number."
4411   (< (mail-header-number h1)
4412      (mail-header-number h2)))
4413
4414 (defun gnus-thread-sort-by-number (h1 h2)
4415   "Sort threads by root article number."
4416   (gnus-article-sort-by-number
4417    (gnus-thread-header h1) (gnus-thread-header h2)))
4418
4419 (defsubst gnus-article-sort-by-random (h1 h2)
4420   "Sort articles by article number."
4421   (zerop (random 2)))
4422
4423 (defun gnus-thread-sort-by-random (h1 h2)
4424   "Sort threads by root article number."
4425   (gnus-article-sort-by-random
4426    (gnus-thread-header h1) (gnus-thread-header h2)))
4427
4428 (defsubst gnus-article-sort-by-lines (h1 h2)
4429   "Sort articles by article Lines header."
4430   (< (mail-header-lines h1)
4431      (mail-header-lines h2)))
4432
4433 (defun gnus-thread-sort-by-lines (h1 h2)
4434   "Sort threads by root article Lines header."
4435   (gnus-article-sort-by-lines
4436    (gnus-thread-header h1) (gnus-thread-header h2)))
4437
4438 (defsubst gnus-article-sort-by-chars (h1 h2)
4439   "Sort articles by octet length."
4440   (< (mail-header-chars h1)
4441      (mail-header-chars h2)))
4442
4443 (defun gnus-thread-sort-by-chars (h1 h2)
4444   "Sort threads by root article octet length."
4445   (gnus-article-sort-by-chars
4446    (gnus-thread-header h1) (gnus-thread-header h2)))
4447
4448 (defsubst gnus-article-sort-by-author (h1 h2)
4449   "Sort articles by root author."
4450   (string-lessp
4451    (let ((addr (car (mime-entity-read-field h1 'From))))
4452      (or (std11-full-name-string addr)
4453          (std11-address-string addr)
4454          ""))
4455    (let ((addr (car (mime-entity-read-field h2 'From))))
4456      (or (std11-full-name-string addr)
4457          (std11-address-string addr)
4458          ""))
4459    ))
4460
4461 (defun gnus-thread-sort-by-author (h1 h2)
4462   "Sort threads by root author."
4463   (gnus-article-sort-by-author
4464    (gnus-thread-header h1)  (gnus-thread-header h2)))
4465
4466 (defsubst gnus-article-sort-by-subject (h1 h2)
4467   "Sort articles by root subject."
4468   (string-lessp
4469    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4470    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4471
4472 (defun gnus-thread-sort-by-subject (h1 h2)
4473   "Sort threads by root subject."
4474   (gnus-article-sort-by-subject
4475    (gnus-thread-header h1) (gnus-thread-header h2)))
4476
4477 (defsubst gnus-article-sort-by-date (h1 h2)
4478   "Sort articles by root article date."
4479   (time-less-p
4480    (gnus-date-get-time (mail-header-date h1))
4481    (gnus-date-get-time (mail-header-date h2))))
4482
4483 (defun gnus-thread-sort-by-date (h1 h2)
4484   "Sort threads by root article date."
4485   (gnus-article-sort-by-date
4486    (gnus-thread-header h1) (gnus-thread-header h2)))
4487
4488 (defsubst gnus-article-sort-by-score (h1 h2)
4489   "Sort articles by root article score.
4490 Unscored articles will be counted as having a score of zero."
4491   (> (or (cdr (assq (mail-header-number h1)
4492                     gnus-newsgroup-scored))
4493          gnus-summary-default-score 0)
4494      (or (cdr (assq (mail-header-number h2)
4495                     gnus-newsgroup-scored))
4496          gnus-summary-default-score 0)))
4497
4498 (defun gnus-thread-sort-by-score (h1 h2)
4499   "Sort threads by root article score."
4500   (gnus-article-sort-by-score
4501    (gnus-thread-header h1) (gnus-thread-header h2)))
4502
4503 (defun gnus-thread-sort-by-total-score (h1 h2)
4504   "Sort threads by the sum of all scores in the thread.
4505 Unscored articles will be counted as having a score of zero."
4506   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4507
4508 (defun gnus-thread-total-score (thread)
4509   ;; This function find the total score of THREAD.
4510   (cond
4511    ((null thread)
4512     0)
4513    ((consp thread)
4514     (if (stringp (car thread))
4515         (apply gnus-thread-score-function 0
4516                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4517       (gnus-thread-total-score-1 thread)))
4518    (t
4519     (gnus-thread-total-score-1 (list thread)))))
4520
4521 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4522   "Sort threads such that the thread with the most recently arrived article comes first."
4523   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4524
4525 (defun gnus-thread-highest-number (thread)
4526   "Return the highest article number in THREAD."
4527   (apply 'max (mapcar (lambda (header)
4528                         (mail-header-number header))
4529                       (message-flatten-list thread))))
4530
4531 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4532   "Sort threads such that the thread with the most recently dated article comes first."
4533   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4534
4535 (defun gnus-thread-latest-date (thread)
4536   "Return the highest article date in THREAD."
4537   (let ((previous-time 0))
4538     (apply 'max
4539            (mapcar
4540             (lambda (header)
4541               (setq previous-time
4542                     (condition-case ()
4543                         (time-to-seconds (mail-header-parse-date
4544                                           (mail-header-date header)))
4545                       (error previous-time))))
4546             (sort
4547              (message-flatten-list thread)
4548              (lambda (h1 h2)
4549                (< (mail-header-number h1)
4550                   (mail-header-number h2))))))))
4551
4552 (defun gnus-thread-total-score-1 (root)
4553   ;; This function find the total score of the thread below ROOT.
4554   (setq root (car root))
4555   (apply gnus-thread-score-function
4556          (or (append
4557               (mapcar 'gnus-thread-total-score
4558                       (cdr (gnus-id-to-thread (mail-header-id root))))
4559               (when (> (mail-header-number root) 0)
4560                 (list (or (cdr (assq (mail-header-number root)
4561                                      gnus-newsgroup-scored))
4562                           gnus-summary-default-score 0))))
4563              (list gnus-summary-default-score)
4564              '(0))))
4565
4566 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4567 (defvar gnus-tmp-prev-subject nil)
4568 (defvar gnus-tmp-false-parent nil)
4569 (defvar gnus-tmp-root-expunged nil)
4570 (defvar gnus-tmp-dummy-line nil)
4571
4572 (eval-when-compile (defvar gnus-tmp-header))
4573 (defun gnus-extra-header (type &optional header)
4574   "Return the extra header of TYPE."
4575   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4576       ""))
4577
4578 (defvar gnus-tmp-thread-tree-header-string "")
4579
4580 (defcustom gnus-sum-thread-tree-root "> "
4581   "With %B spec, used for the root of a thread.
4582 If nil, use subject instead."
4583   :type '(radio (const :format "%v  " nil) (string :size 0))
4584   :group 'gnus-thread)
4585 (defcustom gnus-sum-thread-tree-false-root "> "
4586   "With %B spec, used for a false root of a thread.
4587 If nil, use subject instead."
4588   :type '(radio (const :format "%v  " nil) (string :size 0))
4589   :group 'gnus-thread)
4590 (defcustom gnus-sum-thread-tree-single-indent ""
4591   "With %B spec, used for a thread with just one message.
4592 If nil, use subject instead."
4593   :type '(radio (const :format "%v  " nil) (string :size 0))
4594   :group 'gnus-thread)
4595 (defcustom gnus-sum-thread-tree-vertical "| "
4596   "With %B spec, used for drawing a vertical line."
4597   :type 'string
4598   :group 'gnus-thread)
4599 (defcustom gnus-sum-thread-tree-indent "  "
4600   "With %B spec, used for indenting."
4601   :type 'string
4602   :group 'gnus-thread)
4603 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4604   "With %B spec, used for a leaf with brothers."
4605   :type 'string
4606   :group 'gnus-thread)
4607 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4608   "With %B spec, used for a leaf without brothers."
4609   :type 'string
4610   :group 'gnus-thread)
4611
4612 (defun gnus-summary-prepare-threads (threads)
4613   "Prepare summary buffer from THREADS and indentation LEVEL.
4614 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4615 or a straight list of headers."
4616   (gnus-message 7 "Generating summary...")
4617
4618   (setq gnus-newsgroup-threads threads)
4619   (beginning-of-line)
4620
4621   (let ((gnus-tmp-level 0)
4622         (default-score (or gnus-summary-default-score 0))
4623         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4624         (building-line-count gnus-summary-display-while-building)
4625         (building-count (integerp gnus-summary-display-while-building))
4626         thread number subject stack state gnus-tmp-gathered beg-match
4627         new-roots gnus-tmp-new-adopts thread-end simp-subject
4628         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4629         gnus-tmp-replied gnus-tmp-subject-or-nil
4630         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4631         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4632         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4633         tree-stack)
4634
4635     (setq gnus-tmp-prev-subject nil
4636           gnus-tmp-thread-tree-header-string "")
4637
4638     (if (vectorp (car threads))
4639         ;; If this is a straight (sic) list of headers, then a
4640         ;; threaded summary display isn't required, so we just create
4641         ;; an unthreaded one.
4642         (gnus-summary-prepare-unthreaded threads)
4643
4644       ;; Do the threaded display.
4645
4646       (if gnus-summary-display-while-building
4647           (switch-to-buffer (buffer-name)))
4648       (while (or threads stack gnus-tmp-new-adopts new-roots)
4649
4650         (if (and (= gnus-tmp-level 0)
4651                  (or (not stack)
4652                      (= (caar stack) 0))
4653                  (not gnus-tmp-false-parent)
4654                  (or gnus-tmp-new-adopts new-roots))
4655             (if gnus-tmp-new-adopts
4656                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4657                       thread (list (car gnus-tmp-new-adopts))
4658                       gnus-tmp-header (caar thread)
4659                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4660               (when new-roots
4661                 (setq thread (list (car new-roots))
4662                       gnus-tmp-header (caar thread)
4663                       new-roots (cdr new-roots))))
4664
4665           (if threads
4666               ;; If there are some threads, we do them before the
4667               ;; threads on the stack.
4668               (setq thread threads
4669                     gnus-tmp-header (caar thread))
4670             ;; There were no current threads, so we pop something off
4671             ;; the stack.
4672             (setq state (car stack)
4673                   gnus-tmp-level (car state)
4674                   tree-stack (cadr state)
4675                   thread (caddr state)
4676                   stack (cdr stack)
4677                   gnus-tmp-header (caar thread))))
4678
4679         (setq gnus-tmp-false-parent nil)
4680         (setq gnus-tmp-root-expunged nil)
4681         (setq thread-end nil)
4682
4683         (if (stringp gnus-tmp-header)
4684             ;; The header is a dummy root.
4685             (cond
4686              ((eq gnus-summary-make-false-root 'adopt)
4687               ;; We let the first article adopt the rest.
4688               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4689                                                (cddar thread)))
4690               (setq gnus-tmp-gathered
4691                     (nconc (mapcar
4692                             (lambda (h) (mail-header-number (car h)))
4693                             (cddar thread))
4694                            gnus-tmp-gathered))
4695               (setq thread (cons (list (caar thread)
4696                                        (cadar thread))
4697                                  (cdr thread)))
4698               (setq gnus-tmp-level -1
4699                     gnus-tmp-false-parent t))
4700              ((eq gnus-summary-make-false-root 'empty)
4701               ;; We print adopted articles with empty subject fields.
4702               (setq gnus-tmp-gathered
4703                     (nconc (mapcar
4704                             (lambda (h) (mail-header-number (car h)))
4705                             (cddar thread))
4706                            gnus-tmp-gathered))
4707               (setq gnus-tmp-level -1))
4708              ((eq gnus-summary-make-false-root 'dummy)
4709               ;; We remember that we probably want to output a dummy
4710               ;; root.
4711               (setq gnus-tmp-dummy-line gnus-tmp-header)
4712               (setq gnus-tmp-prev-subject gnus-tmp-header))
4713              (t
4714               ;; We do not make a root for the gathered
4715               ;; sub-threads at all.
4716               (setq gnus-tmp-level -1)))
4717
4718           (setq number (mail-header-number gnus-tmp-header)
4719                 subject (mail-header-subject gnus-tmp-header)
4720                 simp-subject (gnus-simplify-subject-fully subject))
4721
4722           (cond
4723            ;; If the thread has changed subject, we might want to make
4724            ;; this subthread into a root.
4725            ((and (null gnus-thread-ignore-subject)
4726                  (not (zerop gnus-tmp-level))
4727                  gnus-tmp-prev-subject
4728                  (not (string= gnus-tmp-prev-subject simp-subject)))
4729             (setq new-roots (nconc new-roots (list (car thread)))
4730                   thread-end t
4731                   gnus-tmp-header nil))
4732            ;; If the article lies outside the current limit,
4733            ;; then we do not display it.
4734            ((not (memq number gnus-newsgroup-limit))
4735             (setq gnus-tmp-gathered
4736                   (nconc (mapcar
4737                           (lambda (h) (mail-header-number (car h)))
4738                           (cdar thread))
4739                          gnus-tmp-gathered))
4740             (setq gnus-tmp-new-adopts (if (cdar thread)
4741                                           (append gnus-tmp-new-adopts
4742                                                   (cdar thread))
4743                                         gnus-tmp-new-adopts)
4744                   thread-end t
4745                   gnus-tmp-header nil)
4746             (when (zerop gnus-tmp-level)
4747               (setq gnus-tmp-root-expunged t)))
4748            ;; Perhaps this article is to be marked as read?
4749            ((and gnus-summary-mark-below
4750                  (< (or (cdr (assq number gnus-newsgroup-scored))
4751                         default-score)
4752                     gnus-summary-mark-below)
4753                  ;; Don't touch sparse articles.
4754                  (not (gnus-summary-article-sparse-p number))
4755                  (not (gnus-summary-article-ancient-p number)))
4756             (setq gnus-newsgroup-unreads
4757                   (delq number gnus-newsgroup-unreads))
4758             (if gnus-newsgroup-auto-expire
4759                 (setq gnus-newsgroup-expirable
4760                       (gnus-add-to-sorted-list
4761                        gnus-newsgroup-expirable number))
4762               (push (cons number gnus-low-score-mark)
4763                     gnus-newsgroup-reads))))
4764
4765           (when gnus-tmp-header
4766             ;; We may have an old dummy line to output before this
4767             ;; article.
4768             (when (and gnus-tmp-dummy-line
4769                        (gnus-subject-equal
4770                         gnus-tmp-dummy-line
4771                         (mail-header-subject gnus-tmp-header)))
4772               (gnus-summary-insert-dummy-line
4773                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4774               (setq gnus-tmp-dummy-line nil))
4775
4776             ;; Compute the mark.
4777             (setq gnus-tmp-unread (gnus-article-mark number))
4778
4779             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4780                                   gnus-tmp-header gnus-tmp-level)
4781                   gnus-newsgroup-data)
4782
4783             ;; Actually insert the line.
4784             (setq
4785              gnus-tmp-subject-or-nil
4786              (cond
4787               ((and gnus-thread-ignore-subject
4788                     gnus-tmp-prev-subject
4789                     (not (string= gnus-tmp-prev-subject simp-subject)))
4790                subject)
4791               ((zerop gnus-tmp-level)
4792                (if (and (eq gnus-summary-make-false-root 'empty)
4793                         (memq number gnus-tmp-gathered)
4794                         gnus-tmp-prev-subject
4795                         (string= gnus-tmp-prev-subject simp-subject))
4796                    gnus-summary-same-subject
4797                  subject))
4798               (t gnus-summary-same-subject)))
4799             (if (and (eq gnus-summary-make-false-root 'adopt)
4800                      (= gnus-tmp-level 1)
4801                      (memq number gnus-tmp-gathered))
4802                 (setq gnus-tmp-opening-bracket ?\<
4803                       gnus-tmp-closing-bracket ?\>)
4804               (setq gnus-tmp-opening-bracket ?\[
4805                     gnus-tmp-closing-bracket ?\]))
4806             (setq
4807              gnus-tmp-indentation
4808              (aref gnus-thread-indent-array gnus-tmp-level)
4809              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4810              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4811                                 gnus-summary-default-score 0)
4812              gnus-tmp-score-char
4813              (if (or (null gnus-summary-default-score)
4814                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4815                          gnus-summary-zcore-fuzz))
4816                  ?\ ;;;Whitespace
4817                (if (< gnus-tmp-score gnus-summary-default-score)
4818                    gnus-score-below-mark gnus-score-over-mark))
4819              gnus-tmp-replied
4820              (cond ((memq number gnus-newsgroup-processable)
4821                     gnus-process-mark)
4822                    ((memq number gnus-newsgroup-cached)
4823                     gnus-cached-mark)
4824                    ((memq number gnus-newsgroup-replied)
4825                     gnus-replied-mark)
4826                    ((memq number gnus-newsgroup-forwarded)
4827                     gnus-forwarded-mark)
4828                    ((memq number gnus-newsgroup-saved)
4829                     gnus-saved-mark)
4830                    ((memq number gnus-newsgroup-recent)
4831                     gnus-recent-mark)
4832                    ((memq number gnus-newsgroup-unseen)
4833                     gnus-unseen-mark)
4834                    (t gnus-no-mark))
4835              gnus-tmp-downloaded
4836              (cond ((memq number gnus-newsgroup-undownloaded)
4837                     gnus-undownloaded-mark)
4838                    (gnus-newsgroup-agentized
4839                     gnus-downloaded-mark)
4840                    (t
4841                     gnus-no-mark))
4842              gnus-tmp-from (mail-header-from gnus-tmp-header)
4843              gnus-tmp-name
4844              (cond
4845               ((string-match "<[^>]+> *$" gnus-tmp-from)
4846                (setq beg-match (match-beginning 0))
4847                (or (and (string-match "^\".+\"" gnus-tmp-from)
4848                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4849                    (substring gnus-tmp-from 0 beg-match)))
4850               ((string-match "(.+)" gnus-tmp-from)
4851                (substring gnus-tmp-from
4852                           (1+ (match-beginning 0)) (1- (match-end 0))))
4853               (t gnus-tmp-from))
4854
4855              ;; Do the %B string
4856              gnus-tmp-thread-tree-header-string
4857              (cond
4858               ((not gnus-show-threads) "")
4859               ((zerop gnus-tmp-level)
4860                (cond ((cdar thread)
4861                       (or gnus-sum-thread-tree-root subject))
4862                      (gnus-tmp-new-adopts
4863                       (or gnus-sum-thread-tree-false-root subject))
4864                      (t
4865                       (or gnus-sum-thread-tree-single-indent subject))))
4866               (t
4867                (concat (apply 'concat
4868                               (mapcar (lambda (item)
4869                                         (if (= item 1)
4870                                             gnus-sum-thread-tree-vertical
4871                                           gnus-sum-thread-tree-indent))
4872                                       (cdr (reverse tree-stack))))
4873                        (if (nth 1 thread)
4874                            gnus-sum-thread-tree-leaf-with-other
4875                          gnus-sum-thread-tree-single-leaf)))))
4876             (when (string= gnus-tmp-name "")
4877               (setq gnus-tmp-name gnus-tmp-from))
4878             (unless (numberp gnus-tmp-lines)
4879               (setq gnus-tmp-lines -1))
4880             (if (= gnus-tmp-lines -1)
4881                 (setq gnus-tmp-lines "?")
4882               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4883               (gnus-put-text-property
4884              (point)
4885              (progn (eval gnus-summary-line-format-spec) (point))
4886                'gnus-number number)
4887             (when gnus-visual-p
4888               (forward-line -1)
4889               (gnus-run-hooks 'gnus-summary-update-hook)
4890               (forward-line 1))
4891
4892             (setq gnus-tmp-prev-subject simp-subject)))
4893
4894         (when (nth 1 thread)
4895           (push (list (max 0 gnus-tmp-level)
4896                       (copy-sequence tree-stack)
4897                       (nthcdr 1 thread))
4898                 stack))
4899         (push (if (nth 1 thread) 1 0) tree-stack)
4900         (incf gnus-tmp-level)
4901         (setq threads (if thread-end nil (cdar thread)))
4902         (if gnus-summary-display-while-building
4903             (if building-count
4904                 (progn
4905                   ;; use a set frequency
4906                   (setq building-line-count (1- building-line-count))
4907                   (when (= building-line-count 0)
4908                     (sit-for 0)
4909                     (setq building-line-count
4910                           gnus-summary-display-while-building)))
4911               ;; always
4912               (sit-for 0)))
4913         (unless threads
4914           (setq gnus-tmp-level 0)))))
4915   (gnus-message 7 "Generating summary...done"))
4916
4917 (defun gnus-summary-prepare-unthreaded (headers)
4918   "Generate an unthreaded summary buffer based on HEADERS."
4919   (let (header number mark)
4920
4921     (beginning-of-line)
4922
4923     (while headers
4924       ;; We may have to root out some bad articles...
4925       (when (memq (setq number (mail-header-number
4926                                 (setq header (pop headers))))
4927                   gnus-newsgroup-limit)
4928         ;; Mark article as read when it has a low score.
4929         (when (and gnus-summary-mark-below
4930                    (< (or (cdr (assq number gnus-newsgroup-scored))
4931                           gnus-summary-default-score 0)
4932                       gnus-summary-mark-below)
4933                    (not (gnus-summary-article-ancient-p number)))
4934           (setq gnus-newsgroup-unreads
4935                 (delq number gnus-newsgroup-unreads))
4936           (if gnus-newsgroup-auto-expire
4937               (push number gnus-newsgroup-expirable)
4938             (push (cons number gnus-low-score-mark)
4939                   gnus-newsgroup-reads)))
4940
4941         (setq mark (gnus-article-mark number))
4942         (push (gnus-data-make number mark (1+ (point)) header 0)
4943               gnus-newsgroup-data)
4944         (gnus-summary-insert-line
4945          header 0 number
4946          (memq number gnus-newsgroup-undownloaded)
4947          mark (memq number gnus-newsgroup-replied)
4948          (memq number gnus-newsgroup-expirable)
4949          (mail-header-subject header) nil
4950          (cdr (assq number gnus-newsgroup-scored))
4951          (memq number gnus-newsgroup-processable))))))
4952
4953 (defun gnus-summary-remove-list-identifiers ()
4954   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4955   (let ((regexp (if (consp gnus-list-identifiers)
4956                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4957                   gnus-list-identifiers))
4958         changed subject)
4959     (when regexp
4960       (dolist (header gnus-newsgroup-headers)
4961         (setq subject (mail-header-subject header)
4962               changed nil)
4963         (while (string-match
4964                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4965                 subject)
4966           (setq subject
4967                 (concat (substring subject 0 (match-beginning 2))
4968                         (substring subject (match-end 0)))
4969                 changed t))
4970         (when (and changed
4971                    (string-match
4972                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4973           (setq subject
4974                 (concat (substring subject 0 (match-beginning 1))
4975                         (substring subject (match-end 1)))))
4976         (when changed
4977           (mail-header-set-subject header subject))))))
4978
4979 (defun gnus-fetch-headers (articles)
4980   "Fetch headers of ARTICLES."
4981   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4982     (gnus-message 5 "Fetching headers for %s..." name)
4983     (prog1
4984         (if (eq 'nov
4985                 (setq gnus-headers-retrieved-by
4986                       (gnus-retrieve-headers
4987                        articles gnus-newsgroup-name
4988                        ;; We might want to fetch old headers, but
4989                        ;; not if there is only 1 article.
4990                        (and (or (and
4991                                  (not (eq gnus-fetch-old-headers 'some))
4992                                  (not (numberp gnus-fetch-old-headers)))
4993                                 (> (length articles) 1))
4994                             gnus-fetch-old-headers))))
4995             (gnus-get-newsgroup-headers-xover
4996              articles nil nil gnus-newsgroup-name t)
4997           (gnus-get-newsgroup-headers))
4998       (gnus-message 5 "Fetching headers for %s...done" name))))
4999
5000 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5001   "Select newsgroup GROUP.
5002 If READ-ALL is non-nil, all articles in the group are selected.
5003 If SELECT-ARTICLES, only select those articles from GROUP."
5004   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5005          ;;!!! Dirty hack; should be removed.
5006          (gnus-summary-ignore-duplicates
5007           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5008               t
5009             gnus-summary-ignore-duplicates))
5010          (info (nth 2 entry))
5011          articles fetched-articles cached)
5012
5013     (unless (gnus-check-server
5014              (set (make-local-variable 'gnus-current-select-method)
5015                   (gnus-find-method-for-group group)))
5016       (error "Couldn't open server"))
5017
5018     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5019         (gnus-activate-group group)     ; Or we can activate it...
5020         (progn                          ; Or we bug out.
5021           (when (equal major-mode 'gnus-summary-mode)
5022             (gnus-kill-buffer (current-buffer)))
5023           (error "Couldn't activate group %s: %s"
5024                  group (gnus-status-message group))))
5025
5026     (unless (gnus-request-group group t)
5027       (when (equal major-mode 'gnus-summary-mode)
5028         (gnus-kill-buffer (current-buffer)))
5029       (error "Couldn't request group %s: %s"
5030              group (gnus-status-message group)))
5031
5032     (when gnus-agent
5033       ;; The agent may be storing articles that are no longer in the
5034       ;; server's active range.  If that is the case, the active range
5035       ;; needs to be expanded such that the agent's articles can be
5036       ;; included in the summary.
5037       (let* ((gnus-command-method (gnus-find-method-for-group group))
5038              (alist (gnus-agent-load-alist group))
5039              (active (gnus-active group)))
5040         (if (and (car alist)
5041                  (< (caar alist) (car active)))
5042             (gnus-set-active group (cons (caar alist) (cdr active)))))
5043
5044       (setq gnus-summary-use-undownloaded-faces
5045             (not (gnus-agent-find-parameter
5046                   group
5047                   'agent-disable-undownloaded-faces))))
5048
5049     (setq gnus-newsgroup-name group
5050           gnus-newsgroup-unselected nil
5051           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5052
5053     (let ((display (gnus-group-find-parameter group 'display)))
5054       (setq gnus-newsgroup-display
5055             (cond
5056              ((not (zerop (or (car-safe read-all) 0)))
5057               ;; The user entered the group with C-u SPC/RET, let's show
5058               ;; all articles.
5059               'gnus-not-ignore)
5060              ((eq display 'all)
5061               'gnus-not-ignore)
5062              ((arrayp display)
5063               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5064              ((numberp display)
5065               ;; The following is probably the "correct" solution, but
5066               ;; it makes Gnus fetch all headers and then limit the
5067               ;; articles (which is slow), so instead we hack the
5068               ;; select-articles parameter instead. -- Simon Josefsson
5069               ;; <jas@kth.se>
5070               ;;
5071               ;; (gnus-byte-compile
5072               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5073               ;;                         display)))))
5074               (setq select-articles
5075                     (gnus-uncompress-range
5076                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5077                              (if (> tmp 0)
5078                                  tmp
5079                                1))
5080                            (cdr (gnus-active group)))))
5081               nil)
5082              (t
5083               nil))))
5084
5085     (gnus-summary-setup-default-charset)
5086
5087     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5088     (when (gnus-virtual-group-p group)
5089       (setq cached gnus-newsgroup-cached))
5090
5091     (setq gnus-newsgroup-unreads
5092           (gnus-sorted-ndifference
5093            (gnus-sorted-ndifference gnus-newsgroup-unreads
5094                                     gnus-newsgroup-marked)
5095            gnus-newsgroup-dormant))
5096
5097     (setq gnus-newsgroup-processable nil)
5098
5099     (gnus-update-read-articles group gnus-newsgroup-unreads)
5100
5101     ;; Adjust and set lists of article marks.
5102     (when info
5103       (gnus-adjust-marked-articles info))
5104     (if (setq articles select-articles)
5105         (setq gnus-newsgroup-unselected
5106               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5107       (setq articles (gnus-articles-to-read group read-all)))
5108
5109     (cond
5110      ((null articles)
5111       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5112       'quit)
5113      ((eq articles 0) nil)
5114      (t
5115       ;; Init the dependencies hash table.
5116       (setq gnus-newsgroup-dependencies
5117             (gnus-make-hashtable (length articles)))
5118       (gnus-set-global-variables)
5119       ;; Retrieve the headers and read them in.
5120
5121       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5122
5123       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5124       (when cached
5125         (setq gnus-newsgroup-cached cached))
5126
5127       ;; Suppress duplicates?
5128       (when gnus-suppress-duplicates
5129         (gnus-dup-suppress-articles))
5130
5131       ;; Set the initial limit.
5132       (setq gnus-newsgroup-limit (copy-sequence articles))
5133       ;; Remove canceled articles from the list of unread articles.
5134       (setq fetched-articles
5135             (mapcar (lambda (headers) (mail-header-number headers))
5136                     gnus-newsgroup-headers))
5137       (setq gnus-newsgroup-articles fetched-articles)
5138       (setq gnus-newsgroup-unreads
5139             (gnus-sorted-nintersection
5140              gnus-newsgroup-unreads fetched-articles))
5141       (gnus-compute-unseen-list)
5142
5143       ;; Removed marked articles that do not exist.
5144       (gnus-update-missing-marks
5145        (gnus-sorted-difference articles fetched-articles))
5146       ;; We might want to build some more threads first.
5147       (when (and gnus-fetch-old-headers
5148                  (eq gnus-headers-retrieved-by 'nov))
5149         (if (eq gnus-fetch-old-headers 'invisible)
5150             (gnus-build-all-threads)
5151           (gnus-build-old-threads)))
5152       ;; Let the Gnus agent mark articles as read.
5153       (when gnus-agent
5154         (gnus-agent-get-undownloaded-list))
5155       ;; Remove list identifiers from subject
5156       (when gnus-list-identifiers
5157         (gnus-summary-remove-list-identifiers))
5158       ;; Check whether auto-expire is to be done in this group.
5159       (setq gnus-newsgroup-auto-expire
5160             (gnus-group-auto-expirable-p group))
5161       ;; Set up the article buffer now, if necessary.
5162       (unless gnus-single-article-buffer
5163         (gnus-article-setup-buffer))
5164       ;; First and last article in this newsgroup.
5165       (when gnus-newsgroup-headers
5166         (setq gnus-newsgroup-begin
5167               (mail-header-number (car gnus-newsgroup-headers))
5168               gnus-newsgroup-end
5169               (mail-header-number
5170                (gnus-last-element gnus-newsgroup-headers))))
5171       ;; GROUP is successfully selected.
5172       (or gnus-newsgroup-headers t)))))
5173
5174 (defun gnus-compute-unseen-list ()
5175   ;; The `seen' marks are treated specially.
5176   (if (not gnus-newsgroup-seen)
5177       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5178     (setq gnus-newsgroup-unseen
5179           (gnus-inverse-list-range-intersection
5180            gnus-newsgroup-articles gnus-newsgroup-seen))))
5181
5182 (defun gnus-summary-display-make-predicate (display)
5183   (require 'gnus-agent)
5184   (when (= (length display) 1)
5185     (setq display (car display)))
5186   (unless gnus-summary-display-cache
5187     (dolist (elem (append '((unread . unread)
5188                             (read . read)
5189                             (unseen . unseen))
5190                           gnus-article-mark-lists))
5191       (push (cons (cdr elem)
5192                   (gnus-byte-compile
5193                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5194             gnus-summary-display-cache)))
5195   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5196         (gnus-category-predicate-cache gnus-summary-display-cache))
5197     (gnus-get-predicate display)))
5198
5199 ;; Uses the dynamically bound `number' variable.
5200 (eval-when-compile
5201   (defvar number))
5202 (defun gnus-article-marked-p (type &optional article)
5203   (let ((article (or article number)))
5204     (cond
5205      ((eq type 'tick)
5206       (memq article gnus-newsgroup-marked))
5207      ((eq type 'spam)
5208       (memq article gnus-newsgroup-spam-marked))
5209      ((eq type 'unsend)
5210       (memq article gnus-newsgroup-unsendable))
5211      ((eq type 'undownload)
5212       (memq article gnus-newsgroup-undownloaded))
5213      ((eq type 'download)
5214       (memq article gnus-newsgroup-downloadable))
5215      ((eq type 'unread)
5216       (memq article gnus-newsgroup-unreads))
5217      ((eq type 'read)
5218       (memq article gnus-newsgroup-reads))
5219      ((eq type 'dormant)
5220       (memq article gnus-newsgroup-dormant) )
5221      ((eq type 'expire)
5222       (memq article gnus-newsgroup-expirable))
5223      ((eq type 'reply)
5224       (memq article gnus-newsgroup-replied))
5225      ((eq type 'killed)
5226       (memq article gnus-newsgroup-killed))
5227      ((eq type 'bookmark)
5228       (assq article gnus-newsgroup-bookmarks))
5229      ((eq type 'score)
5230       (assq article gnus-newsgroup-scored))
5231      ((eq type 'save)
5232       (memq article gnus-newsgroup-saved))
5233      ((eq type 'cache)
5234       (memq article gnus-newsgroup-cached))
5235      ((eq type 'forward)
5236       (memq article gnus-newsgroup-forwarded))
5237      ((eq type 'seen)
5238       (not (memq article gnus-newsgroup-unseen)))
5239      ((eq type 'recent)
5240       (memq article gnus-newsgroup-recent))
5241      (t t))))
5242
5243 (defun gnus-articles-to-read (group &optional read-all)
5244   "Find out what articles the user wants to read."
5245   (let* ((display (gnus-group-find-parameter group 'display))
5246          (articles
5247           ;; Select all articles if `read-all' is non-nil, or if there
5248           ;; are no unread articles.
5249           (if (or read-all
5250                   (and (zerop (length gnus-newsgroup-marked))
5251                        (zerop (length gnus-newsgroup-unreads)))
5252                   ;; Fetch all if the predicate is non-nil.
5253                   gnus-newsgroup-display)
5254               ;; We want to select the headers for all the articles in
5255               ;; the group, so we select either all the active
5256               ;; articles in the group, or (if that's nil), the
5257               ;; articles in the cache.
5258               (or
5259                (gnus-uncompress-range (gnus-active group))
5260                (gnus-cache-articles-in-group group))
5261             ;; Select only the "normal" subset of articles.
5262             (gnus-sorted-nunion
5263              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5264              gnus-newsgroup-unreads)))
5265          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5266          (scored (length scored-list))
5267          (number (length articles))
5268          (marked (+ (length gnus-newsgroup-marked)
5269                     (length gnus-newsgroup-dormant)))
5270          (select
5271           (cond
5272            ((numberp read-all)
5273             read-all)
5274            ((numberp gnus-newsgroup-display)
5275             gnus-newsgroup-display)
5276            (t
5277             (condition-case ()
5278                 (cond
5279                  ((and (or (<= scored marked) (= scored number))
5280                        (numberp gnus-large-newsgroup)
5281                        (> number gnus-large-newsgroup))
5282                   (let* ((cursor-in-echo-area nil)
5283                          (initial (gnus-parameter-large-newsgroup-initial
5284                                    gnus-newsgroup-name))
5285                          (input
5286                           (read-string
5287                            (format
5288                             "How many articles from %s (%s %d): "
5289                             (gnus-limit-string
5290                              (gnus-group-decoded-name gnus-newsgroup-name)
5291                              35)
5292                             (if initial "max" "default")
5293                             number)
5294                            (if initial
5295                                (cons (number-to-string initial)
5296                                      0)))))
5297                     (if (string-match "^[ \t]*$" input) number input)))
5298                  ((and (> scored marked) (< scored number)
5299                        (> (- scored number) 20))
5300                   (let ((input
5301                          (read-string
5302                           (format "%s %s (%d scored, %d total): "
5303                                   "How many articles from"
5304                                   (gnus-group-decoded-name group)
5305                                   scored number))))
5306                     (if (string-match "^[ \t]*$" input)
5307                         number input)))
5308                  (t number))
5309               (quit
5310                (message "Quit getting the articles to read")
5311                nil))))))
5312     (setq select (if (stringp select) (string-to-number select) select))
5313     (if (or (null select) (zerop select))
5314         select
5315       (if (and (not (zerop scored)) (<= (abs select) scored))
5316           (progn
5317             (setq articles (sort scored-list '<))
5318             (setq number (length articles)))
5319         (setq articles (copy-sequence articles)))
5320
5321       (when (< (abs select) number)
5322         (if (< select 0)
5323             ;; Select the N oldest articles.
5324             (setcdr (nthcdr (1- (abs select)) articles) nil)
5325           ;; Select the N most recent articles.
5326           (setq articles (nthcdr (- number select) articles))))
5327       (setq gnus-newsgroup-unselected
5328             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5329       (when gnus-alter-articles-to-read-function
5330         (setq articles
5331               (sort
5332                (funcall gnus-alter-articles-to-read-function
5333                         gnus-newsgroup-name articles)
5334                '<)))
5335       articles)))
5336
5337 (defun gnus-killed-articles (killed articles)
5338   (let (out)
5339     (while articles
5340       (when (inline (gnus-member-of-range (car articles) killed))
5341         (push (car articles) out))
5342       (setq articles (cdr articles)))
5343     out))
5344
5345 (defun gnus-uncompress-marks (marks)
5346   "Uncompress the mark ranges in MARKS."
5347   (let ((uncompressed '(score bookmark))
5348         out)
5349     (while marks
5350       (if (memq (caar marks) uncompressed)
5351           (push (car marks) out)
5352         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5353       (setq marks (cdr marks)))
5354     out))
5355
5356 (defun gnus-article-mark-to-type (mark)
5357   "Return the type of MARK."
5358   (or (cadr (assq mark gnus-article-special-mark-lists))
5359       'list))
5360
5361 (defun gnus-article-unpropagatable-p (mark)
5362   "Return whether MARK should be propagated to back end."
5363   (memq mark gnus-article-unpropagated-mark-lists))
5364
5365 (defun gnus-adjust-marked-articles (info)
5366   "Set all article lists and remove all marks that are no longer valid."
5367   (let* ((marked-lists (gnus-info-marks info))
5368          (active (gnus-active (gnus-info-group info)))
5369          (min (car active))
5370          (max (cdr active))
5371          (types gnus-article-mark-lists)
5372          marks var articles article mark mark-type)
5373
5374     (dolist (marks marked-lists)
5375       (setq mark (car marks)
5376             mark-type (gnus-article-mark-to-type mark)
5377             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5378
5379       ;; We set the variable according to the type of the marks list,
5380       ;; and then adjust the marks to a subset of the active articles.
5381       (cond
5382        ;; Adjust "simple" lists.
5383        ((eq mark-type 'list)
5384         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5385         (when (memq mark '(tick dormant expire reply save))
5386           (while articles
5387             (when (or (< (setq article (pop articles)) min) (> article max))
5388               (set var (delq article (symbol-value var)))))))
5389        ;; Adjust assocs.
5390        ((eq mark-type 'tuple)
5391         (set var (setq articles (cdr marks)))
5392         (when (not (listp (cdr (symbol-value var))))
5393           (set var (list (symbol-value var))))
5394         (when (not (listp (cdr articles)))
5395           (setq articles (list articles)))
5396         (while articles
5397           (when (or (not (consp (setq article (pop articles))))
5398                     (< (car article) min)
5399                     (> (car article) max))
5400             (set var (delq article (symbol-value var))))))
5401        ;; Adjust ranges (sloppily).
5402        ((eq mark-type 'range)
5403         (cond
5404          ((eq mark 'seen)
5405           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5406           ;; It should be (seen (NUM1 . NUM2)).
5407           (when (numberp (cddr marks))
5408             (setcdr marks (list (cdr marks))))
5409           (setq articles (cdr marks))
5410           (while (and articles
5411                       (or (and (consp (car articles))
5412                                (> min (cdar articles)))
5413                           (and (numberp (car articles))
5414                                (> min (car articles)))))
5415             (pop articles))
5416           (set var articles))))))))
5417
5418 (defun gnus-update-missing-marks (missing)
5419   "Go through the list of MISSING articles and remove them from the mark lists."
5420   (when missing
5421     (let (var m)
5422       ;; Go through all types.
5423       (dolist (elem gnus-article-mark-lists)
5424         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5425           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5426           (when (symbol-value var)
5427             ;; This list has articles.  So we delete all missing
5428             ;; articles from it.
5429             (setq m missing)
5430             (while m
5431               (set var (delq (pop m) (symbol-value var))))))))))
5432
5433 (defun gnus-update-marks ()
5434   "Enter the various lists of marked articles into the newsgroup info list."
5435   (let ((types gnus-article-mark-lists)
5436         (info (gnus-get-info gnus-newsgroup-name))
5437         type list newmarked symbol delta-marks)
5438     (when info
5439       ;; Add all marks lists to the list of marks lists.
5440       (while (setq type (pop types))
5441         (setq list (symbol-value
5442                     (setq symbol
5443                           (intern (format "gnus-newsgroup-%s" (car type))))))
5444
5445         (when list
5446           ;; Get rid of the entries of the articles that have the
5447           ;; default score.
5448           (when (and (eq (cdr type) 'score)
5449                      gnus-save-score
5450                      list)
5451             (let* ((arts list)
5452                    (prev (cons nil list))
5453                    (all prev))
5454               (while arts
5455                 (if (or (not (consp (car arts)))
5456                         (= (cdar arts) gnus-summary-default-score))
5457                     (setcdr prev (cdr arts))
5458                   (setq prev arts))
5459                 (setq arts (cdr arts)))
5460               (setq list (cdr all)))))
5461
5462         (when (eq (cdr type) 'seen)
5463           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5464
5465         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5466           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5467
5468         (when (and (gnus-check-backend-function
5469                     'request-set-mark gnus-newsgroup-name)
5470                    (not (gnus-article-unpropagatable-p (cdr type))))
5471           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5472                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5473                  (add (gnus-remove-from-range
5474                        (gnus-copy-sequence list) old)))
5475             (when add
5476               (push (list add 'add (list (cdr type))) delta-marks))
5477             (when del
5478               (push (list del 'del (list (cdr type))) delta-marks))))
5479
5480         (when list
5481           (push (cons (cdr type) list) newmarked)))
5482
5483       (when delta-marks
5484         (unless (gnus-check-group gnus-newsgroup-name)
5485           (error "Can't open server for %s" gnus-newsgroup-name))
5486         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5487
5488       ;; Enter these new marks into the info of the group.
5489       (if (nthcdr 3 info)
5490           (setcar (nthcdr 3 info) newmarked)
5491         ;; Add the marks lists to the end of the info.
5492         (when newmarked
5493           (setcdr (nthcdr 2 info) (list newmarked))))
5494
5495       ;; Cut off the end of the info if there's nothing else there.
5496       (let ((i 5))
5497         (while (and (> i 2)
5498                     (not (nth i info)))
5499           (when (nthcdr (decf i) info)
5500             (setcdr (nthcdr i info) nil)))))))
5501
5502 (defun gnus-set-mode-line (where)
5503   "Set the mode line of the article or summary buffers.
5504 If WHERE is `summary', the summary mode line format will be used."
5505   ;; Is this mode line one we keep updated?
5506   (when (and (memq where gnus-updated-mode-lines)
5507              (symbol-value
5508               (intern (format "gnus-%s-mode-line-format-spec" where))))
5509     (let (mode-string)
5510       (save-excursion
5511         ;; We evaluate this in the summary buffer since these
5512         ;; variables are buffer-local to that buffer.
5513         (set-buffer gnus-summary-buffer)
5514         ;; We bind all these variables that are used in the `eval' form
5515         ;; below.
5516         (let* ((mformat (symbol-value
5517                          (intern
5518                           (format "gnus-%s-mode-line-format-spec" where))))
5519                (gnus-tmp-group-name (gnus-group-decoded-name
5520                                      gnus-newsgroup-name))
5521                (gnus-tmp-article-number (or gnus-current-article 0))
5522                (gnus-tmp-unread gnus-newsgroup-unreads)
5523                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5524                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5525                (gnus-tmp-unread-and-unselected
5526                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5527                             (zerop gnus-tmp-unselected))
5528                        "")
5529                       ((zerop gnus-tmp-unselected)
5530                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5531                       (t (format "{%d(+%d) more}"
5532                                  gnus-tmp-unread-and-unticked
5533                                  gnus-tmp-unselected))))
5534                (gnus-tmp-subject
5535                 (if (and gnus-current-headers
5536                          (vectorp gnus-current-headers))
5537                     (gnus-mode-string-quote
5538                      (mail-header-subject gnus-current-headers))
5539                   ""))
5540                bufname-length max-len
5541                gnus-tmp-header);; passed as argument to any user-format-funcs
5542           (setq mode-string (eval mformat))
5543           (setq bufname-length (if (string-match "%b" mode-string)
5544                                    (- (length
5545                                        (buffer-name
5546                                         (if (eq where 'summary)
5547                                             nil
5548                                           (get-buffer gnus-article-buffer))))
5549                                       2)
5550                                  0))
5551           (setq max-len (max 4 (if gnus-mode-non-string-length
5552                                    (- (window-width)
5553                                       gnus-mode-non-string-length
5554                                       bufname-length)
5555                                  (length mode-string))))
5556           ;; We might have to chop a bit of the string off...
5557           (when (> (length mode-string) max-len)
5558             (setq mode-string
5559                   (concat (gnus-truncate-string mode-string (- max-len 3))
5560                           "...")))
5561           ;; Pad the mode string a bit.
5562           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5563       ;; Update the mode line.
5564       (setq mode-line-buffer-identification
5565             (gnus-mode-line-buffer-identification (list mode-string)))
5566       (set-buffer-modified-p t))))
5567
5568 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5569   "Go through the HEADERS list and add all Xrefs to a hash table.
5570 The resulting hash table is returned, or nil if no Xrefs were found."
5571   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5572          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5573          (xref-hashtb (gnus-make-hashtable))
5574          start group entry number xrefs header)
5575     (while headers
5576       (setq header (pop headers))
5577       (when (and (setq xrefs (mail-header-xref header))
5578                  (not (memq (setq number (mail-header-number header))
5579                             unreads)))
5580         (setq start 0)
5581         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5582           (setq start (match-end 0))
5583           (setq group (if prefix
5584                           (concat prefix (substring xrefs (match-beginning 1)
5585                                                     (match-end 1)))
5586                         (substring xrefs (match-beginning 1) (match-end 1))))
5587           (setq number
5588                 (string-to-int (substring xrefs (match-beginning 2)
5589                                           (match-end 2))))
5590           (if (setq entry (gnus-gethash group xref-hashtb))
5591               (setcdr entry (cons number (cdr entry)))
5592             (gnus-sethash group (cons number nil) xref-hashtb)))))
5593     (and start xref-hashtb)))
5594
5595 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5596   "Look through all the headers and mark the Xrefs as read."
5597   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5598         name entry info xref-hashtb idlist method nth4)
5599     (save-excursion
5600       (set-buffer gnus-group-buffer)
5601       (when (setq xref-hashtb
5602                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5603         (mapatoms
5604          (lambda (group)
5605            (unless (string= from-newsgroup (setq name (symbol-name group)))
5606              (setq idlist (symbol-value group))
5607              ;; Dead groups are not updated.
5608              (and (prog1
5609                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5610                             info (nth 2 entry))
5611                     (when (stringp (setq nth4 (gnus-info-method info)))
5612                       (setq nth4 (gnus-server-to-method nth4))))
5613                   ;; Only do the xrefs if the group has the same
5614                   ;; select method as the group we have just read.
5615                   (or (gnus-methods-equal-p
5616                        nth4 (gnus-find-method-for-group from-newsgroup))
5617                       virtual
5618                       (equal nth4 (setq method (gnus-find-method-for-group
5619                                                 from-newsgroup)))
5620                       (and (equal (car nth4) (car method))
5621                            (equal (nth 1 nth4) (nth 1 method))))
5622                   gnus-use-cross-reference
5623                   (or (not (eq gnus-use-cross-reference t))
5624                       virtual
5625                       ;; Only do cross-references on subscribed
5626                       ;; groups, if that is what is wanted.
5627                       (<= (gnus-info-level info) gnus-level-subscribed))
5628                   (gnus-group-make-articles-read name idlist))))
5629          xref-hashtb)))))
5630
5631 (defun gnus-compute-read-articles (group articles)
5632   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5633          (info (nth 2 entry))
5634          (active (gnus-active group))
5635          ninfo)
5636     (when entry
5637       ;; First peel off all invalid article numbers.
5638       (when active
5639         (let ((ids articles)
5640               id first)
5641           (while (setq id (pop ids))
5642             (when (and first (> id (cdr active)))
5643               ;; We'll end up in this situation in one particular
5644               ;; obscure situation.  If you re-scan a group and get
5645               ;; a new article that is cross-posted to a different
5646               ;; group that has not been re-scanned, you might get
5647               ;; crossposted article that has a higher number than
5648               ;; Gnus believes possible.  So we re-activate this
5649               ;; group as well.  This might mean doing the
5650               ;; crossposting thingy will *increase* the number
5651               ;; of articles in some groups.  Tsk, tsk.
5652               (setq active (or (gnus-activate-group group) active)))
5653             (when (or (> id (cdr active))
5654                       (< id (car active)))
5655               (setq articles (delq id articles))))))
5656       ;; If the read list is nil, we init it.
5657       (if (and active
5658                (null (gnus-info-read info))
5659                (> (car active) 1))
5660           (setq ninfo (cons 1 (1- (car active))))
5661         (setq ninfo (gnus-info-read info)))
5662       ;; Then we add the read articles to the range.
5663       (gnus-add-to-range
5664        ninfo (setq articles (sort articles '<))))))
5665
5666 (defun gnus-group-make-articles-read (group articles)
5667   "Update the info of GROUP to say that ARTICLES are read."
5668   (let* ((num 0)
5669          (entry (gnus-gethash group gnus-newsrc-hashtb))
5670          (info (nth 2 entry))
5671          (active (gnus-active group))
5672          range)
5673     (when entry
5674       (setq range (gnus-compute-read-articles group articles))
5675       (save-excursion
5676         (set-buffer gnus-group-buffer)
5677         (gnus-undo-register
5678           `(progn
5679              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5680              (gnus-info-set-read ',info ',(gnus-info-read info))
5681              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5682              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5683              (gnus-group-update-group ,group t))))
5684       ;; Add the read articles to the range.
5685       (gnus-info-set-read info range)
5686       (gnus-request-set-mark group (list (list range 'add '(read))))
5687       ;; Then we have to re-compute how many unread
5688       ;; articles there are in this group.
5689       (when active
5690         (cond
5691          ((not range)
5692           (setq num (- (1+ (cdr active)) (car active))))
5693          ((not (listp (cdr range)))
5694           (setq num (- (cdr active) (- (1+ (cdr range))
5695                                        (car range)))))
5696          (t
5697           (while range
5698             (if (numberp (car range))
5699                 (setq num (1+ num))
5700               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5701             (setq range (cdr range)))
5702           (setq num (- (cdr active) num))))
5703         ;; Update the number of unread articles.
5704         (setcar entry num)
5705         ;; Update the group buffer.
5706         (unless (gnus-ephemeral-group-p group)
5707           (gnus-group-update-group group t))))))
5708
5709 (defvar gnus-newsgroup-none-id 0)
5710
5711 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5712   (let ((cur nntp-server-buffer)
5713         (dependencies
5714          (or dependencies
5715              (save-excursion (set-buffer gnus-summary-buffer)
5716                              gnus-newsgroup-dependencies)))
5717         headers id end ref
5718         (mail-parse-charset gnus-newsgroup-charset)
5719         (mail-parse-ignored-charsets
5720          (save-excursion (condition-case nil
5721                              (set-buffer gnus-summary-buffer)
5722                            (error))
5723                          gnus-newsgroup-ignored-charsets)))
5724     (save-excursion
5725       (set-buffer nntp-server-buffer)
5726       ;; Translate all TAB characters into SPACE characters.
5727       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5728       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5729       (ietf-drums-unfold-fws)
5730       (gnus-run-hooks 'gnus-parse-headers-hook)
5731       (let ((case-fold-search t)
5732             in-reply-to header p lines chars ctype)
5733         (goto-char (point-min))
5734         ;; Search to the beginning of the next header.  Error messages
5735         ;; do not begin with 2 or 3.
5736         (while (re-search-forward "^[23][0-9]+ " nil t)
5737           (setq id nil
5738                 ref nil)
5739           ;; This implementation of this function, with nine
5740           ;; search-forwards instead of the one re-search-forward and
5741           ;; a case (which basically was the old function) is actually
5742           ;; about twice as fast, even though it looks messier.  You
5743           ;; can't have everything, I guess.  Speed and elegance
5744           ;; doesn't always go hand in hand.
5745           (setq
5746            header
5747            (make-full-mail-header
5748             ;; Number.
5749             (prog1
5750                 (read cur)
5751               (end-of-line)
5752               (setq p (point))
5753               (narrow-to-region (point)
5754                                 (or (and (search-forward "\n.\n" nil t)
5755                                          (- (point) 2))
5756                                     (point))))
5757             ;; Subject.
5758             (progn
5759               (goto-char p)
5760               (if (search-forward "\nsubject:" nil t)
5761                   (nnheader-header-value)
5762                 "(none)"))
5763             ;; From.
5764             (progn
5765               (goto-char p)
5766               (if (search-forward "\nfrom:" nil t)
5767                   (nnheader-header-value)
5768                 "(nobody)"))
5769             ;; Date.
5770             (progn
5771               (goto-char p)
5772               (if (search-forward "\ndate:" nil t)
5773                   (nnheader-header-value) ""))
5774             ;; Message-ID.
5775             (progn
5776               (goto-char p)
5777               (setq id (if (re-search-forward
5778                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5779                            ;; We do it this way to make sure the Message-ID
5780                            ;; is (somewhat) syntactically valid.
5781                            (buffer-substring (match-beginning 1)
5782                                              (match-end 1))
5783                          ;; If there was no message-id, we just fake one
5784                          ;; to make subsequent routines simpler.
5785                          (nnheader-generate-fake-message-id))))
5786             ;; References.
5787             (progn
5788               (goto-char p)
5789               (if (search-forward "\nreferences:" nil t)
5790                   (progn
5791                     (setq end (point))
5792                     (prog1
5793                         (nnheader-header-value)
5794                       (setq ref
5795                             (buffer-substring
5796                              (progn
5797                                ;; (end-of-line)
5798                                (search-backward ">" end t)
5799                                (1+ (point)))
5800                              (progn
5801                                (search-backward "<" end t)
5802                                (point))))))
5803                 ;; Get the references from the in-reply-to header if there
5804                 ;; were no references and the in-reply-to header looks
5805                 ;; promising.
5806                 (if (and (search-forward "\nin-reply-to:" nil t)
5807                          (setq in-reply-to (nnheader-header-value))
5808                          (string-match "<[^>]+>" in-reply-to))
5809                     (let (ref2)
5810                       (setq ref (substring in-reply-to (match-beginning 0)
5811                                            (match-end 0)))
5812                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5813                         (setq ref2 (substring in-reply-to (match-beginning 0)
5814                                               (match-end 0)))
5815                         (when (> (length ref2) (length ref))
5816                           (setq ref ref2)))
5817                       ref)
5818                   (setq ref nil))))
5819             ;; Chars.
5820             (progn
5821               (goto-char p)
5822               (if (search-forward "\nchars: " nil t)
5823                   (if (numberp (setq chars (ignore-errors (read cur))))
5824                       chars -1)
5825                 -1))
5826             ;; Lines.
5827             (progn
5828               (goto-char p)
5829               (if (search-forward "\nlines: " nil t)
5830                   (if (numberp (setq lines (ignore-errors (read cur))))
5831                       lines -1)
5832                 -1))
5833             ;; Xref.
5834             (progn
5835               (goto-char p)
5836               (and (search-forward "\nxref:" nil t)
5837                    (nnheader-header-value)))
5838             ;; Extra.
5839             (when gnus-extra-headers
5840               (let ((extra gnus-extra-headers)
5841                     out)
5842                 (while extra
5843                   (goto-char p)
5844                   (when (search-forward
5845                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5846                     (push (cons (car extra) (nnheader-header-value)) out))
5847                   (pop extra))
5848                 out))))
5849           (goto-char p)
5850           (if (and (search-forward "\ncontent-type: " nil t)
5851                    (setq ctype (nnheader-header-value)))
5852               (mime-entity-set-content-type-internal
5853                header (mime-parse-Content-Type ctype)))
5854           (when (equal id ref)
5855             (setq ref nil))
5856
5857           (when gnus-alter-header-function
5858             (funcall gnus-alter-header-function header)
5859             (setq id (mail-header-id header)
5860                   ref (gnus-parent-id (mail-header-references header))))
5861
5862           (when (setq header
5863                       (gnus-dependencies-add-header
5864                        header dependencies force-new))
5865             (push header headers))
5866           (goto-char (point-max))
5867           (widen))
5868         (nreverse headers)))))
5869
5870 ;; Goes through the xover lines and returns a list of vectors
5871 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5872                                                   force-new dependencies
5873                                                   group also-fetch-heads)
5874   "Parse the news overview data in the server buffer.
5875 Return a list of headers that match SEQUENCE (see
5876 `nntp-retrieve-headers')."
5877   ;; Get the Xref when the users reads the articles since most/some
5878   ;; NNTP servers do not include Xrefs when using XOVER.
5879   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5880   (let ((mail-parse-charset gnus-newsgroup-charset)
5881         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5882         (cur nntp-server-buffer)
5883         (dependencies (or dependencies gnus-newsgroup-dependencies))
5884         (allp (cond
5885                ((eq gnus-read-all-available-headers t)
5886                 t)
5887                ((stringp gnus-read-all-available-headers)
5888                 (string-match gnus-read-all-available-headers group))
5889                (t
5890                 nil)))
5891         number headers header)
5892     (save-excursion
5893       (set-buffer nntp-server-buffer)
5894       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5895       ;; Allow the user to mangle the headers before parsing them.
5896       (gnus-run-hooks 'gnus-parse-headers-hook)
5897       (goto-char (point-min))
5898       (gnus-parse-without-error
5899         (while (and (or sequence allp)
5900                     (not (eobp)))
5901           (setq number (read cur))
5902           (when (not allp)
5903             (while (and sequence
5904                         (< (car sequence) number))
5905               (setq sequence (cdr sequence))))
5906           (when (and (or allp
5907                          (and sequence
5908                               (eq number (car sequence))))
5909                      (progn
5910                        (setq sequence (cdr sequence))
5911                        (setq header (inline
5912                                       (gnus-nov-parse-line
5913                                        number dependencies force-new)))))
5914             (push header headers))
5915           (forward-line 1)))
5916       ;; A common bug in inn is that if you have posted an article and
5917       ;; then retrieves the active file, it will answer correctly --
5918       ;; the new article is included.  However, a NOV entry for the
5919       ;; article may not have been generated yet, so this may fail.
5920       ;; We work around this problem by retrieving the last few
5921       ;; headers using HEAD.
5922       (if (or (not also-fetch-heads)
5923               (not sequence))
5924           ;; We (probably) got all the headers.
5925           (nreverse headers)
5926         (let ((gnus-nov-is-evil t))
5927           (nconc
5928            (nreverse headers)
5929            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5930              (gnus-get-newsgroup-headers))))))))
5931
5932 (defun gnus-article-get-xrefs ()
5933   "Fill in the Xref value in `gnus-current-headers', if necessary.
5934 This is meant to be called in `gnus-article-internal-prepare-hook'."
5935   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5936                                  gnus-current-headers)))
5937     (or (not gnus-use-cross-reference)
5938         (not headers)
5939         (and (mail-header-xref headers)
5940              (not (string= (mail-header-xref headers) "")))
5941         (let ((case-fold-search t)
5942               xref)
5943           (save-restriction
5944             (nnheader-narrow-to-headers)
5945             (goto-char (point-min))
5946             (when (or (and (not (eobp))
5947                            (eq (downcase (char-after)) ?x)
5948                            (looking-at "Xref:"))
5949                       (search-forward "\nXref:" nil t))
5950               (goto-char (1+ (match-end 0)))
5951               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5952               (mail-header-set-xref headers xref)))))))
5953
5954 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5955   "Find article ID and insert the summary line for that article.
5956 OLD-HEADER can either be a header or a line number to insert
5957 the subject line on."
5958   (let* ((line (and (numberp old-header) old-header))
5959          (old-header (and (vectorp old-header) old-header))
5960          (header (cond ((and old-header use-old-header)
5961                         old-header)
5962                        ((and (numberp id)
5963                              (gnus-number-to-header id))
5964                         (gnus-number-to-header id))
5965                        (t
5966                         (gnus-read-header id))))
5967          (number (and (numberp id) id))
5968          d)
5969     (when header
5970       ;; Rebuild the thread that this article is part of and go to the
5971       ;; article we have fetched.
5972       (when (and (not gnus-show-threads)
5973                  old-header)
5974         (when (and number
5975                    (setq d (gnus-data-find (mail-header-number old-header))))
5976           (goto-char (gnus-data-pos d))
5977           (gnus-data-remove
5978            number
5979            (- (gnus-point-at-bol)
5980               (prog1
5981                   (1+ (gnus-point-at-eol))
5982                 (gnus-delete-line))))))
5983       (when old-header
5984         (mail-header-set-number header (mail-header-number old-header)))
5985       (setq gnus-newsgroup-sparse
5986             (delq (setq number (mail-header-number header))
5987                   gnus-newsgroup-sparse))
5988       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5989       (push number gnus-newsgroup-limit)
5990       (gnus-rebuild-thread (mail-header-id header) line)
5991       (gnus-summary-goto-subject number nil t))
5992     (when (and (numberp number)
5993                (> number 0))
5994       ;; We have to update the boundaries even if we can't fetch the
5995       ;; article if ID is a number -- so that the next `P' or `N'
5996       ;; command will fetch the previous (or next) article even
5997       ;; if the one we tried to fetch this time has been canceled.
5998       (when (> number gnus-newsgroup-end)
5999         (setq gnus-newsgroup-end number))
6000       (when (< number gnus-newsgroup-begin)
6001         (setq gnus-newsgroup-begin number))
6002       (setq gnus-newsgroup-unselected
6003             (delq number gnus-newsgroup-unselected)))
6004     ;; Report back a success?
6005     (and header (mail-header-number header))))
6006
6007 ;;; Process/prefix in the summary buffer
6008
6009 (defun gnus-summary-work-articles (n)
6010   "Return a list of articles to be worked upon.
6011 The prefix argument, the list of process marked articles, and the
6012 current article will be taken into consideration."
6013   (save-excursion
6014     (set-buffer gnus-summary-buffer)
6015     (cond
6016      (n
6017       ;; A numerical prefix has been given.
6018       (setq n (prefix-numeric-value n))
6019       (let ((backward (< n 0))
6020             (n (abs (prefix-numeric-value n)))
6021             articles article)
6022         (save-excursion
6023           (while
6024               (and (> n 0)
6025                    (push (setq article (gnus-summary-article-number))
6026                          articles)
6027                    (if backward
6028                        (gnus-summary-find-prev nil article)
6029                      (gnus-summary-find-next nil article)))
6030             (decf n)))
6031         (nreverse articles)))
6032      ((and (gnus-region-active-p) (mark))
6033       (message "region active")
6034       ;; Work on the region between point and mark.
6035       (let ((max (max (point) (mark)))
6036             articles article)
6037         (save-excursion
6038           (goto-char (min (point) (mark)))
6039           (while
6040               (and
6041                (push (setq article (gnus-summary-article-number)) articles)
6042                (gnus-summary-find-next nil article)
6043                (< (point) max)))
6044           (nreverse articles))))
6045      (gnus-newsgroup-processable
6046       ;; There are process-marked articles present.
6047       ;; Save current state.
6048       (gnus-summary-save-process-mark)
6049       ;; Return the list.
6050       (reverse gnus-newsgroup-processable))
6051      (t
6052       ;; Just return the current article.
6053       (list (gnus-summary-article-number))))))
6054
6055 (defmacro gnus-summary-iterate (arg &rest forms)
6056   "Iterate over the process/prefixed articles and do FORMS.
6057 ARG is the interactive prefix given to the command.  FORMS will be
6058 executed with point over the summary line of the articles."
6059   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6060     `(let ((,articles (gnus-summary-work-articles ,arg)))
6061        (while ,articles
6062          (gnus-summary-goto-subject (car ,articles))
6063          ,@forms
6064          (pop ,articles)))))
6065
6066 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6067 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6068
6069 (defun gnus-summary-save-process-mark ()
6070   "Push the current set of process marked articles on the stack."
6071   (interactive)
6072   (push (copy-sequence gnus-newsgroup-processable)
6073         gnus-newsgroup-process-stack))
6074
6075 (defun gnus-summary-kill-process-mark ()
6076   "Push the current set of process marked articles on the stack and unmark."
6077   (interactive)
6078   (gnus-summary-save-process-mark)
6079   (gnus-summary-unmark-all-processable))
6080
6081 (defun gnus-summary-yank-process-mark ()
6082   "Pop the last process mark state off the stack and restore it."
6083   (interactive)
6084   (unless gnus-newsgroup-process-stack
6085     (error "Empty mark stack"))
6086   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6087
6088 (defun gnus-summary-process-mark-set (set)
6089   "Make SET into the current process marked articles."
6090   (gnus-summary-unmark-all-processable)
6091   (while set
6092     (gnus-summary-set-process-mark (pop set))))
6093
6094 ;;; Searching and stuff
6095
6096 (defun gnus-summary-search-group (&optional backward use-level)
6097   "Search for next unread newsgroup.
6098 If optional argument BACKWARD is non-nil, search backward instead."
6099   (save-excursion
6100     (set-buffer gnus-group-buffer)
6101     (when (gnus-group-search-forward
6102            backward nil (if use-level (gnus-group-group-level) nil))
6103       (gnus-group-group-name))))
6104
6105 (defun gnus-summary-best-group (&optional exclude-group)
6106   "Find the name of the best unread group.
6107 If EXCLUDE-GROUP, do not go to this group."
6108   (save-excursion
6109     (set-buffer gnus-group-buffer)
6110     (save-excursion
6111       (gnus-group-best-unread-group exclude-group))))
6112
6113 (defun gnus-summary-find-next (&optional unread article backward)
6114   (if backward
6115       (gnus-summary-find-prev unread article)
6116     (let* ((dummy (gnus-summary-article-intangible-p))
6117            (article (or article (gnus-summary-article-number)))
6118            (data (gnus-data-find-list article))
6119            result)
6120       (when (and (not dummy)
6121                  (or (not gnus-summary-check-current)
6122                      (not unread)
6123                      (not (gnus-data-unread-p (car data)))))
6124         (setq data (cdr data)))
6125       (when (setq result
6126                   (if unread
6127                       (progn
6128                         (while data
6129                           (unless (memq (gnus-data-number (car data))
6130                                         (cond
6131                                          ((eq gnus-auto-goto-ignores
6132                                               'always-undownloaded)
6133                                           gnus-newsgroup-undownloaded)
6134                                          (gnus-plugged
6135                                           nil)
6136                                          ((eq gnus-auto-goto-ignores
6137                                               'unfetched)
6138                                           gnus-newsgroup-unfetched)
6139                                          ((eq gnus-auto-goto-ignores
6140                                               'undownloaded)
6141                                           gnus-newsgroup-undownloaded)))
6142                             (when (gnus-data-unread-p (car data))
6143                               (setq result (car data)
6144                                     data nil)))
6145                           (setq data (cdr data)))
6146                         result)
6147                     (car data)))
6148         (goto-char (gnus-data-pos result))
6149         (gnus-data-number result)))))
6150
6151 (defun gnus-summary-find-prev (&optional unread article)
6152   (let* ((eobp (eobp))
6153          (article (or article (gnus-summary-article-number)))
6154          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6155          result)
6156     (when (and (not eobp)
6157                (or (not gnus-summary-check-current)
6158                    (not unread)
6159                    (not (gnus-data-unread-p (car data)))))
6160       (setq data (cdr data)))
6161     (when (setq result
6162                 (if unread
6163                     (progn
6164                       (while data
6165                         (unless (memq (gnus-data-number (car data))
6166                                       (cond
6167                                        ((eq gnus-auto-goto-ignores
6168                                             'always-undownloaded)
6169                                         gnus-newsgroup-undownloaded)
6170                                        (gnus-plugged
6171                                         nil)
6172                                        ((eq gnus-auto-goto-ignores
6173                                             'unfetched)
6174                                         gnus-newsgroup-unfetched)
6175                                        ((eq gnus-auto-goto-ignores
6176                                             'undownloaded)
6177                                         gnus-newsgroup-undownloaded)))
6178                           (when (gnus-data-unread-p (car data))
6179                             (setq result (car data)
6180                                   data nil)))
6181                         (setq data (cdr data)))
6182                       result)
6183                   (car data)))
6184       (goto-char (gnus-data-pos result))
6185       (gnus-data-number result))))
6186
6187 (defun gnus-summary-find-subject (subject &optional unread backward article)
6188   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6189          (article (or article (gnus-summary-article-number)))
6190          (articles (gnus-data-list backward))
6191          (arts (gnus-data-find-list article articles))
6192          result)
6193     (when (or (not gnus-summary-check-current)
6194               (not unread)
6195               (not (gnus-data-unread-p (car arts))))
6196       (setq arts (cdr arts)))
6197     (while arts
6198       (and (or (not unread)
6199                (gnus-data-unread-p (car arts)))
6200            (vectorp (gnus-data-header (car arts)))
6201            (gnus-subject-equal
6202             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6203            (setq result (car arts)
6204                  arts nil))
6205       (setq arts (cdr arts)))
6206     (and result
6207          (goto-char (gnus-data-pos result))
6208          (gnus-data-number result))))
6209
6210 (defun gnus-summary-search-forward (&optional unread subject backward)
6211   "Search forward for an article.
6212 If UNREAD, look for unread articles.  If SUBJECT, look for
6213 articles with that subject.  If BACKWARD, search backward instead."
6214   (cond (subject (gnus-summary-find-subject subject unread backward))
6215         (backward (gnus-summary-find-prev unread))
6216         (t (gnus-summary-find-next unread))))
6217
6218 (defun gnus-recenter (&optional n)
6219   "Center point in window and redisplay frame.
6220 Also do horizontal recentering."
6221   (interactive "P")
6222   (when (and gnus-auto-center-summary
6223              (not (eq gnus-auto-center-summary 'vertical)))
6224     (gnus-horizontal-recenter))
6225   (recenter n))
6226
6227 (defun gnus-summary-recenter ()
6228   "Center point in the summary window.
6229 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6230 displayed, no centering will be performed."
6231   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6232   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6233   (interactive)
6234   ;; The user has to want it.
6235   (when gnus-auto-center-summary
6236     (let* ((top (cond ((< (window-height) 4) 0)
6237                       ((< (window-height) 7) 1)
6238                       (t (if (numberp gnus-auto-center-summary)
6239                              gnus-auto-center-summary
6240                            2))))
6241            (height (1- (window-height)))
6242            (bottom (save-excursion (goto-char (point-max))
6243                                    (forward-line (- height))
6244                                    (point)))
6245            (window (get-buffer-window (current-buffer))))
6246       (when (get-buffer-window gnus-article-buffer)
6247         ;; Only do recentering when the article buffer is displayed,
6248         ;; Set the window start to either `bottom', which is the biggest
6249         ;; possible valid number, or the second line from the top,
6250         ;; whichever is the least.
6251         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6252           (if (> bottom top-pos)
6253               ;; Keep the second line from the top visible
6254               (set-window-start window top-pos t)
6255             ;; Try to keep the bottom line visible; if it's partially
6256             ;; obscured, either scroll one more line to make it fully
6257             ;; visible, or revert to using TOP-POS.
6258             (save-excursion
6259               (goto-char (point-max))
6260               (forward-line -1)
6261               (let ((last-line-start (point)))
6262                 (goto-char bottom)
6263                 (set-window-start window (point) t)
6264                 (when (not (pos-visible-in-window-p last-line-start window))
6265                   (forward-line 1)
6266                   (set-window-start window (min (point) top-pos) t)))))))
6267       ;; Do horizontal recentering while we're at it.
6268       (when (and (get-buffer-window (current-buffer) t)
6269                  (not (eq gnus-auto-center-summary 'vertical)))
6270         (let ((selected (selected-window)))
6271           (select-window (get-buffer-window (current-buffer) t))
6272           (gnus-summary-position-point)
6273           (gnus-horizontal-recenter)
6274           (select-window selected))))))
6275
6276 (defun gnus-summary-jump-to-group (newsgroup)
6277   "Move point to NEWSGROUP in group mode buffer."
6278   ;; Keep update point of group mode buffer if visible.
6279   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6280       (save-window-excursion
6281         ;; Take care of tree window mode.
6282         (when (get-buffer-window gnus-group-buffer)
6283           (pop-to-buffer gnus-group-buffer))
6284         (gnus-group-jump-to-group newsgroup))
6285     (save-excursion
6286       ;; Take care of tree window mode.
6287       (if (get-buffer-window gnus-group-buffer)
6288           (pop-to-buffer gnus-group-buffer)
6289         (set-buffer gnus-group-buffer))
6290       (gnus-group-jump-to-group newsgroup))))
6291
6292 ;; This function returns a list of article numbers based on the
6293 ;; difference between the ranges of read articles in this group and
6294 ;; the range of active articles.
6295 (defun gnus-list-of-unread-articles (group)
6296   (let* ((read (gnus-info-read (gnus-get-info group)))
6297          (active (or (gnus-active group) (gnus-activate-group group)))
6298          (last (cdr active))
6299          first nlast unread)
6300     ;; If none are read, then all are unread.
6301     (if (not read)
6302         (setq first (car active))
6303       ;; If the range of read articles is a single range, then the
6304       ;; first unread article is the article after the last read
6305       ;; article.  Sounds logical, doesn't it?
6306       (if (and (not (listp (cdr read)))
6307                (or (< (car read) (car active))
6308                    (progn (setq read (list read))
6309                           nil)))
6310           (setq first (max (car active) (1+ (cdr read))))
6311         ;; `read' is a list of ranges.
6312         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6313                                   (caar read)))
6314                   1)
6315           (setq first (car active)))
6316         (while read
6317           (when first
6318             (while (< first nlast)
6319               (push first unread)
6320               (setq first (1+ first))))
6321           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6322           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6323           (setq read (cdr read)))))
6324     ;; And add the last unread articles.
6325     (while (<= first last)
6326       (push first unread)
6327       (setq first (1+ first)))
6328     ;; Return the list of unread articles.
6329     (delq 0 (nreverse unread))))
6330
6331 (defun gnus-list-of-read-articles (group)
6332   "Return a list of unread, unticked and non-dormant articles."
6333   (let* ((info (gnus-get-info group))
6334          (marked (gnus-info-marks info))
6335          (active (gnus-active group)))
6336     (and info active
6337          (gnus-list-range-difference
6338           (gnus-list-range-difference
6339            (gnus-sorted-complement
6340             (gnus-uncompress-range active)
6341             (gnus-list-of-unread-articles group))
6342            (cdr (assq 'dormant marked)))
6343           (cdr (assq 'tick marked))))))
6344
6345 ;; Various summary commands
6346
6347 (defun gnus-summary-select-article-buffer ()
6348   "Reconfigure windows to show article buffer."
6349   (interactive)
6350   (if (not (gnus-buffer-live-p gnus-article-buffer))
6351       (error "There is no article buffer for this summary buffer")
6352     (gnus-configure-windows 'article)
6353     (select-window (get-buffer-window gnus-article-buffer))))
6354
6355 (defun gnus-summary-universal-argument (arg)
6356   "Perform any operation on all articles that are process/prefixed."
6357   (interactive "P")
6358   (let ((articles (gnus-summary-work-articles arg))
6359         func article)
6360     (if (eq
6361          (setq
6362           func
6363           (key-binding
6364            (read-key-sequence
6365             (substitute-command-keys
6366              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6367          'undefined)
6368         (gnus-error 1 "Undefined key")
6369       (save-excursion
6370         (while articles
6371           (gnus-summary-goto-subject (setq article (pop articles)))
6372           (let (gnus-newsgroup-processable)
6373             (command-execute func))
6374           (gnus-summary-remove-process-mark article)))))
6375   (gnus-summary-position-point))
6376
6377 (defun gnus-summary-toggle-truncation (&optional arg)
6378   "Toggle truncation of summary lines.
6379 With ARG, turn line truncation on if ARG is positive."
6380   (interactive "P")
6381   (setq truncate-lines
6382         (if (null arg) (not truncate-lines)
6383           (> (prefix-numeric-value arg) 0)))
6384   (redraw-display))
6385
6386 (defun gnus-summary-find-for-reselect ()
6387   "Return the number of an article to stay on across a reselect.
6388 The current article is considered, then following articles, then previous
6389 articles.  An article is sought which is not cancelled and isn't a temporary
6390 insertion from another group.  If there's no such then return a dummy 0."
6391   (let (found)
6392     (dolist (rev '(nil t))
6393       (unless found      ; don't demand the reverse list if we don't need it
6394         (let ((data (gnus-data-find-list
6395                      (gnus-summary-article-number) (gnus-data-list rev))))
6396           (while (and data (not found))
6397             (if (and (< 0 (gnus-data-number (car data)))
6398                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6399                 (setq found (gnus-data-number (car data))))
6400             (setq data (cdr data))))))
6401     (or found 0)))
6402
6403 (defun gnus-summary-reselect-current-group (&optional all rescan)
6404   "Exit and then reselect the current newsgroup.
6405 The prefix argument ALL means to select all articles."
6406   (interactive "P")
6407   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6408     (error "Ephemeral groups can't be reselected"))
6409   (let ((current-subject (gnus-summary-find-for-reselect))
6410         (group gnus-newsgroup-name))
6411     (setq gnus-newsgroup-begin nil)
6412     (gnus-summary-exit nil 'leave-hidden)
6413     ;; We have to adjust the point of group mode buffer because
6414     ;; point was moved to the next unread newsgroup by exiting.
6415     (gnus-summary-jump-to-group group)
6416     (when rescan
6417       (save-excursion
6418         (save-window-excursion
6419           ;; Don't show group contents.
6420           (set-window-start (selected-window) (point-max))
6421           (gnus-group-get-new-news-this-group 1))))
6422     (gnus-group-read-group all t)
6423     (gnus-summary-goto-subject current-subject nil t)))
6424
6425 (defun gnus-summary-rescan-group (&optional all)
6426   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6427   (interactive "P")
6428   (gnus-summary-reselect-current-group all t))
6429
6430 (defun gnus-summary-update-info (&optional non-destructive)
6431   (save-excursion
6432     (let ((group gnus-newsgroup-name))
6433       (when group
6434         (when gnus-newsgroup-kill-headers
6435           (setq gnus-newsgroup-killed
6436                 (gnus-compress-sequence
6437                  (gnus-sorted-union
6438                   (gnus-list-range-intersection
6439                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6440                   gnus-newsgroup-unreads)
6441                  t)))
6442         (unless (listp (cdr gnus-newsgroup-killed))
6443           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6444         (let ((headers gnus-newsgroup-headers))
6445           ;; Set the new ranges of read articles.
6446           (save-excursion
6447             (set-buffer gnus-group-buffer)
6448             (gnus-undo-force-boundary))
6449           (gnus-update-read-articles
6450            group (gnus-sorted-union
6451                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6452           ;; Set the current article marks.
6453           (let ((gnus-newsgroup-scored
6454                  (if (and (not gnus-save-score)
6455                           (not non-destructive))
6456                      nil
6457                    gnus-newsgroup-scored)))
6458             (save-excursion
6459               (gnus-update-marks)))
6460           ;; Do the cross-ref thing.
6461           (when gnus-use-cross-reference
6462             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6463           ;; Do not switch windows but change the buffer to work.
6464           (set-buffer gnus-group-buffer)
6465           (unless (gnus-ephemeral-group-p group)
6466             (gnus-group-update-group group)))))))
6467
6468 (defun gnus-summary-save-newsrc (&optional force)
6469   "Save the current number of read/marked articles in the dribble buffer.
6470 The dribble buffer will then be saved.
6471 If FORCE (the prefix), also save the .newsrc file(s)."
6472   (interactive "P")
6473   (gnus-summary-update-info t)
6474   (if force
6475       (gnus-save-newsrc-file)
6476     (gnus-dribble-save)))
6477
6478 (defun gnus-summary-exit (&optional temporary leave-hidden)
6479   "Exit reading current newsgroup, and then return to group selection mode.
6480 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6481   (interactive)
6482   (gnus-set-global-variables)
6483   (gnus-kill-save-kill-buffer)
6484   (gnus-async-halt-prefetch)
6485   (let* ((group gnus-newsgroup-name)
6486          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6487          (gnus-group-is-exiting-p t)
6488          (mode major-mode)
6489          (group-point nil)
6490          (buf (current-buffer)))
6491     (unless quit-config
6492       ;; Do adaptive scoring, and possibly save score files.
6493       (when gnus-newsgroup-adaptive
6494         (gnus-score-adaptive))
6495       (when gnus-use-scoring
6496         (gnus-score-save)))
6497     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6498     ;; If we have several article buffers, we kill them at exit.
6499     (unless gnus-single-article-buffer
6500       (gnus-kill-buffer gnus-original-article-buffer)
6501       (setq gnus-article-current nil))
6502     (when gnus-use-cache
6503       (gnus-cache-possibly-remove-articles)
6504       (gnus-cache-save-buffers))
6505     (gnus-async-prefetch-remove-group group)
6506     (when gnus-suppress-duplicates
6507       (gnus-dup-enter-articles))
6508     (when gnus-use-trees
6509       (gnus-tree-close group))
6510     (when gnus-use-cache
6511       (gnus-cache-write-active))
6512     ;; Remove entries for this group.
6513     (nnmail-purge-split-history (gnus-group-real-name group))
6514     ;; Make all changes in this group permanent.
6515     (unless quit-config
6516       (gnus-run-hooks 'gnus-exit-group-hook)
6517       (gnus-summary-update-info))
6518     (gnus-close-group group)
6519     ;; Make sure where we were, and go to next newsgroup.
6520     (set-buffer gnus-group-buffer)
6521     (unless quit-config
6522       (gnus-group-jump-to-group group))
6523     (gnus-run-hooks 'gnus-summary-exit-hook)
6524     (unless (or quit-config
6525                 ;; If this group has disappeared from the summary
6526                 ;; buffer, don't skip forwards.
6527                 (not (string= group (gnus-group-group-name))))
6528       (gnus-group-next-unread-group 1))
6529     (setq group-point (point))
6530     (if temporary
6531         nil                             ;Nothing to do.
6532       ;; If we have several article buffers, we kill them at exit.
6533       (unless gnus-single-article-buffer
6534         (gnus-kill-buffer gnus-article-buffer)
6535         (gnus-kill-buffer gnus-original-article-buffer)
6536         (setq gnus-article-current nil))
6537       (set-buffer buf)
6538       (if (not gnus-kill-summary-on-exit)
6539           (progn
6540             (gnus-deaden-summary)
6541             (setq mode nil))
6542         ;; We set all buffer-local variables to nil.  It is unclear why
6543         ;; this is needed, but if we don't, buffer-local variables are
6544         ;; not garbage-collected, it seems.  This would the lead to en
6545         ;; ever-growing Emacs.
6546         (gnus-summary-clear-local-variables)
6547         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6548           (gnus-summary-clear-local-variables))
6549         (when (get-buffer gnus-article-buffer)
6550           (bury-buffer gnus-article-buffer))
6551         ;; We clear the global counterparts of the buffer-local
6552         ;; variables as well, just to be on the safe side.
6553         (set-buffer gnus-group-buffer)
6554         (gnus-summary-clear-local-variables)
6555         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6556           (gnus-summary-clear-local-variables))
6557         ;; Return to group mode buffer.
6558         (when (eq mode 'gnus-summary-mode)
6559           (gnus-kill-buffer buf)))
6560       (setq gnus-current-select-method gnus-select-method)
6561       (if leave-hidden
6562           (set-buffer gnus-group-buffer)
6563         (pop-to-buffer gnus-group-buffer))
6564       (if (not quit-config)
6565           (progn
6566             (goto-char group-point)
6567             (unless leave-hidden
6568               (gnus-configure-windows 'group 'force))
6569             (unless (pos-visible-in-window-p)
6570               (forward-line (/ (static-if (featurep 'xemacs)
6571                                    (window-displayed-height)
6572                                  (1- (window-height)))
6573                                -2))
6574               (set-window-start (selected-window) (point))
6575               (goto-char group-point)))
6576         (gnus-handle-ephemeral-exit quit-config))
6577       ;; Clear the current group name.
6578       (unless quit-config
6579         (setq gnus-newsgroup-name nil)))))
6580
6581 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6582 (defun gnus-summary-exit-no-update (&optional no-questions)
6583   "Quit reading current newsgroup without updating read article info."
6584   (interactive)
6585   (let* ((group gnus-newsgroup-name)
6586          (gnus-group-is-exiting-p t)
6587          (gnus-group-is-exiting-without-update-p t)
6588          (quit-config (gnus-group-quit-config group)))
6589     (when (or no-questions
6590               gnus-expert-user
6591               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6592       (gnus-async-halt-prefetch)
6593       (run-hooks 'gnus-summary-prepare-exit-hook)
6594       ;; If we have several article buffers, we kill them at exit.
6595       (unless gnus-single-article-buffer
6596         (gnus-kill-buffer gnus-article-buffer)
6597         (gnus-kill-buffer gnus-original-article-buffer)
6598         (setq gnus-article-current nil))
6599       (if (not gnus-kill-summary-on-exit)
6600           (gnus-deaden-summary)
6601         (gnus-close-group group)
6602         (gnus-summary-clear-local-variables)
6603         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6604           (gnus-summary-clear-local-variables))
6605         (set-buffer gnus-group-buffer)
6606         (gnus-summary-clear-local-variables)
6607         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6608           (gnus-summary-clear-local-variables))
6609         (gnus-kill-buffer gnus-summary-buffer))
6610       (unless gnus-single-article-buffer
6611         (setq gnus-article-current nil))
6612       (when gnus-use-trees
6613         (gnus-tree-close group))
6614       (gnus-async-prefetch-remove-group group)
6615       (when (get-buffer gnus-article-buffer)
6616         (bury-buffer gnus-article-buffer))
6617       ;; Return to the group buffer.
6618       (gnus-configure-windows 'group 'force)
6619       ;; Clear the current group name.
6620       (setq gnus-newsgroup-name nil)
6621       (unless (gnus-ephemeral-group-p group)
6622         (gnus-group-update-group group))
6623       (when (equal (gnus-group-group-name) group)
6624         (gnus-group-next-unread-group 1))
6625       (when quit-config
6626         (gnus-handle-ephemeral-exit quit-config)))))
6627
6628 (defun gnus-handle-ephemeral-exit (quit-config)
6629   "Handle movement when leaving an ephemeral group.
6630 The state which existed when entering the ephemeral is reset."
6631   (if (not (buffer-name (car quit-config)))
6632       (gnus-configure-windows 'group 'force)
6633     (set-buffer (car quit-config))
6634     (cond ((eq major-mode 'gnus-summary-mode)
6635            (gnus-set-global-variables))
6636           ((eq major-mode 'gnus-article-mode)
6637            (save-excursion
6638              ;; The `gnus-summary-buffer' variable may point
6639              ;; to the old summary buffer when using a single
6640              ;; article buffer.
6641              (unless (gnus-buffer-live-p gnus-summary-buffer)
6642                (set-buffer gnus-group-buffer))
6643              (set-buffer gnus-summary-buffer)
6644              (gnus-set-global-variables))))
6645     (if (or (eq (cdr quit-config) 'article)
6646             (eq (cdr quit-config) 'pick))
6647         (progn
6648           ;; The current article may be from the ephemeral group
6649           ;; thus it is best that we reload this article
6650           ;;
6651           ;; If we're exiting from a large digest, this can be
6652           ;; extremely slow.  So, it's better not to reload it. -- jh.
6653           ;;(gnus-summary-show-article)
6654           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6655               (gnus-configure-windows 'pick 'force)
6656             (gnus-configure-windows (cdr quit-config) 'force)))
6657       (gnus-configure-windows (cdr quit-config) 'force))
6658     (when (eq major-mode 'gnus-summary-mode)
6659       (gnus-summary-next-subject 1 nil t)
6660       (gnus-summary-recenter)
6661       (gnus-summary-position-point))))
6662
6663 (defun gnus-summary-preview-mime-message ()
6664   "MIME decode and play this message."
6665   (interactive)
6666   (let ((gnus-break-pages nil)
6667         (gnus-show-mime t))
6668     (gnus-summary-select-article gnus-show-all-headers t))
6669   (let ((w (get-buffer-window gnus-article-buffer)))
6670     (when w
6671       (select-window (get-buffer-window gnus-article-buffer)))))
6672
6673 ;;; Dead summaries.
6674
6675 (defvar gnus-dead-summary-mode-map nil)
6676
6677 (unless gnus-dead-summary-mode-map
6678   (setq gnus-dead-summary-mode-map (make-keymap))
6679   (suppress-keymap gnus-dead-summary-mode-map)
6680   (substitute-key-definition
6681    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6682   (dolist (key '("\C-d" "\r" "\177" [delete]))
6683     (define-key gnus-dead-summary-mode-map
6684       key 'gnus-summary-wake-up-the-dead))
6685   (dolist (key '("q" "Q"))
6686     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6687
6688 (defvar gnus-dead-summary-mode nil
6689   "Minor mode for Gnus summary buffers.")
6690
6691 (defun gnus-dead-summary-mode (&optional arg)
6692   "Minor mode for Gnus summary buffers."
6693   (interactive "P")
6694   (when (eq major-mode 'gnus-summary-mode)
6695     (make-local-variable 'gnus-dead-summary-mode)
6696     (setq gnus-dead-summary-mode
6697           (if (null arg) (not gnus-dead-summary-mode)
6698             (> (prefix-numeric-value arg) 0)))
6699     (when gnus-dead-summary-mode
6700       (gnus-add-minor-mode
6701        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6702
6703 (defun gnus-deaden-summary ()
6704   "Make the current summary buffer into a dead summary buffer."
6705   ;; Kill any previous dead summary buffer.
6706   (when (and gnus-dead-summary
6707              (buffer-name gnus-dead-summary))
6708     (save-excursion
6709       (set-buffer gnus-dead-summary)
6710       (when gnus-dead-summary-mode
6711         (kill-buffer (current-buffer)))))
6712   ;; Make this the current dead summary.
6713   (setq gnus-dead-summary (current-buffer))
6714   (gnus-dead-summary-mode 1)
6715   (let ((name (buffer-name)))
6716     (when (string-match "Summary" name)
6717       (rename-buffer
6718        (concat (substring name 0 (match-beginning 0)) "Dead "
6719                (substring name (match-beginning 0)))
6720        t)
6721       (bury-buffer))))
6722
6723 (defun gnus-kill-or-deaden-summary (buffer)
6724   "Kill or deaden the summary BUFFER."
6725   (save-excursion
6726     (when (and (buffer-name buffer)
6727                (not gnus-single-article-buffer))
6728       (save-excursion
6729         (set-buffer buffer)
6730         (gnus-kill-buffer gnus-article-buffer)
6731         (gnus-kill-buffer gnus-original-article-buffer)))
6732     (cond
6733      ;; Kill the buffer.
6734      (gnus-kill-summary-on-exit
6735       (when (and gnus-use-trees
6736                  (gnus-buffer-exists-p buffer))
6737         (save-excursion
6738           (set-buffer buffer)
6739           (gnus-tree-close gnus-newsgroup-name)))
6740       (gnus-kill-buffer buffer))
6741      ;; Deaden the buffer.
6742      ((gnus-buffer-exists-p buffer)
6743       (save-excursion
6744         (set-buffer buffer)
6745         (gnus-deaden-summary))))))
6746
6747 (defun gnus-summary-wake-up-the-dead (&rest args)
6748   "Wake up the dead summary buffer."
6749   (interactive)
6750   (gnus-dead-summary-mode -1)
6751   (let ((name (buffer-name)))
6752     (when (string-match "Dead " name)
6753       (rename-buffer
6754        (concat (substring name 0 (match-beginning 0))
6755                (substring name (match-end 0)))
6756        t)))
6757   (gnus-message 3 "This dead summary is now alive again"))
6758
6759 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6760 (defun gnus-summary-fetch-faq (&optional faq-dir)
6761   "Fetch the FAQ for the current group.
6762 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6763 in."
6764   (interactive
6765    (list
6766     (when current-prefix-arg
6767       (completing-read
6768        "FAQ dir: " (and (listp gnus-group-faq-directory)
6769                         (mapcar (lambda (file) (list file))
6770                                 gnus-group-faq-directory))))))
6771   (let (gnus-faq-buffer)
6772     (when (setq gnus-faq-buffer
6773                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6774       (gnus-configure-windows 'summary-faq))))
6775
6776 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6777 (defun gnus-summary-describe-group (&optional force)
6778   "Describe the current newsgroup."
6779   (interactive "P")
6780   (gnus-group-describe-group force gnus-newsgroup-name))
6781
6782 (defun gnus-summary-describe-briefly ()
6783   "Describe summary mode commands briefly."
6784   (interactive)
6785   (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")))
6786
6787 ;; Walking around group mode buffer from summary mode.
6788
6789 (defun gnus-summary-next-group (&optional no-article target-group backward)
6790   "Exit current newsgroup and then select next unread newsgroup.
6791 If prefix argument NO-ARTICLE is non-nil, no article is selected
6792 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6793 previous group instead."
6794   (interactive "P")
6795   ;; Stop pre-fetching.
6796   (gnus-async-halt-prefetch)
6797   (let ((current-group gnus-newsgroup-name)
6798         (current-buffer (current-buffer))
6799         entered)
6800     ;; First we semi-exit this group to update Xrefs and all variables.
6801     ;; We can't do a real exit, because the window conf must remain
6802     ;; the same in case the user is prompted for info, and we don't
6803     ;; want the window conf to change before that...
6804     (gnus-summary-exit t)
6805     (while (not entered)
6806       ;; Then we find what group we are supposed to enter.
6807       (set-buffer gnus-group-buffer)
6808       (gnus-group-jump-to-group current-group)
6809       (setq target-group
6810             (or target-group
6811                 (if (eq gnus-keep-same-level 'best)
6812                     (gnus-summary-best-group gnus-newsgroup-name)
6813                   (gnus-summary-search-group backward gnus-keep-same-level))))
6814       (if (not target-group)
6815           ;; There are no further groups, so we return to the group
6816           ;; buffer.
6817           (progn
6818             (gnus-message 5 "Returning to the group buffer")
6819             (setq entered t)
6820             (when (gnus-buffer-live-p current-buffer)
6821               (set-buffer current-buffer)
6822               (gnus-summary-exit))
6823             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6824         ;; We try to enter the target group.
6825         (gnus-group-jump-to-group target-group)
6826         (let ((unreads (gnus-group-group-unread)))
6827           (if (and (or (eq t unreads)
6828                        (and unreads (not (zerop unreads))))
6829                    (gnus-summary-read-group
6830                     target-group nil no-article
6831                     (and (buffer-name current-buffer) current-buffer)
6832                     nil backward))
6833               (setq entered t)
6834             (setq current-group target-group
6835                   target-group nil)))))))
6836
6837 (defun gnus-summary-prev-group (&optional no-article)
6838   "Exit current newsgroup and then select previous unread newsgroup.
6839 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6840   (interactive "P")
6841   (gnus-summary-next-group no-article nil t))
6842
6843 ;; Walking around summary lines.
6844
6845 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6846   "Go to the first subject satisfying any non-nil constraint.
6847 If UNREAD is non-nil, the article should be unread.
6848 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6849 If UNSEEN is non-nil, the article should be unseen.
6850 Returns the article selected or nil if there are no matching articles."
6851   (interactive "P")
6852   (cond
6853    ;; Empty summary.
6854    ((null gnus-newsgroup-data)
6855     (gnus-message 3 "No articles in the group")
6856     nil)
6857    ;; Pick the first article.
6858    ((not (or unread undownloaded unseen))
6859     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6860     (gnus-data-number (car gnus-newsgroup-data)))
6861    ;; Find the first unread article.
6862    (t
6863     (let ((data gnus-newsgroup-data))
6864       (while (and data
6865                   (let ((num (gnus-data-number (car data))))
6866                     (or (memq num gnus-newsgroup-unfetched)
6867                         (not (or (and unread
6868                                       (memq num gnus-newsgroup-unreads))
6869                                  (and undownloaded
6870                                       (memq num gnus-newsgroup-undownloaded))
6871                                  (and unseen
6872                                       (memq num gnus-newsgroup-unseen)))))))
6873         (setq data (cdr data)))
6874       (prog1
6875           (if data
6876               (progn
6877                 (goto-char (gnus-data-pos (car data)))
6878                 (gnus-data-number (car data)))
6879             (gnus-message 3 "No more%s articles"
6880                           (let* ((r (when unread " unread"))
6881                                  (d (when undownloaded " undownloaded"))
6882                                  (s (when unseen " unseen"))
6883                                  (l (delq nil (list r d s))))
6884                             (cond ((= 3 (length l))
6885                                    (concat r "," d ", or" s))
6886                                   ((= 2 (length l))
6887                                    (concat (car l) ", or" (cadr l)))
6888                                   ((= 1 (length l))
6889                                    (car l))
6890                                   (t
6891                                    ""))))
6892             nil
6893             )
6894         (gnus-summary-position-point))))))
6895
6896 (defun gnus-summary-next-subject (n &optional unread dont-display)
6897   "Go to next N'th summary line.
6898 If N is negative, go to the previous N'th subject line.
6899 If UNREAD is non-nil, only unread articles are selected.
6900 The difference between N and the actual number of steps taken is
6901 returned."
6902   (interactive "p")
6903   (let ((backward (< n 0))
6904         (n (abs n)))
6905     (while (and (> n 0)
6906                 (if backward
6907                     (gnus-summary-find-prev unread)
6908                   (gnus-summary-find-next unread)))
6909       (unless (zerop (setq n (1- n)))
6910         (gnus-summary-show-thread)))
6911     (when (/= 0 n)
6912       (gnus-message 7 "No more%s articles"
6913                     (if unread " unread" "")))
6914     (unless dont-display
6915       (gnus-summary-recenter)
6916       (gnus-summary-position-point))
6917     n))
6918
6919 (defun gnus-summary-next-unread-subject (n)
6920   "Go to next N'th unread summary line."
6921   (interactive "p")
6922   (gnus-summary-next-subject n t))
6923
6924 (defun gnus-summary-prev-subject (n &optional unread)
6925   "Go to previous N'th summary line.
6926 If optional argument UNREAD is non-nil, only unread article is selected."
6927   (interactive "p")
6928   (gnus-summary-next-subject (- n) unread))
6929
6930 (defun gnus-summary-prev-unread-subject (n)
6931   "Go to previous N'th unread summary line."
6932   (interactive "p")
6933   (gnus-summary-next-subject (- n) t))
6934
6935 (defun gnus-summary-goto-subjects (articles)
6936   "Insert the subject header for ARTICLES in the current buffer."
6937   (save-excursion
6938     (dolist (article articles)
6939       (gnus-summary-goto-subject article t)))
6940   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6941   (gnus-summary-position-point))
6942  
6943 (defun gnus-summary-goto-subject (article &optional force silent)
6944   "Go the subject line of ARTICLE.
6945 If FORCE, also allow jumping to articles not currently shown."
6946   (interactive "nArticle number: ")
6947   (unless (numberp article)
6948     (error "Article %s is not a number" article))
6949   (let ((b (point))
6950         (data (gnus-data-find article)))
6951     ;; We read in the article if we have to.
6952     (and (not data)
6953          force
6954          (gnus-summary-insert-subject
6955           article
6956           (if (or (numberp force) (vectorp force)) force)
6957           t)
6958          (setq data (gnus-data-find article)))
6959     (goto-char b)
6960     (if (not data)
6961         (progn
6962           (unless silent
6963             (gnus-message 3 "Can't find article %d" article))
6964           nil)
6965       (let ((pt (gnus-data-pos data)))
6966         (goto-char pt)
6967         (gnus-summary-set-article-display-arrow pt))
6968       (gnus-summary-position-point)
6969       article)))
6970
6971 ;; Walking around summary lines with displaying articles.
6972
6973 (defun gnus-summary-expand-window (&optional arg)
6974   "Make the summary buffer take up the entire Emacs frame.
6975 Given a prefix, will force an `article' buffer configuration."
6976   (interactive "P")
6977   (if arg
6978       (gnus-configure-windows 'article 'force)
6979     (gnus-configure-windows 'summary 'force)))
6980
6981 (defun gnus-summary-display-article (article &optional all-header)
6982   "Display ARTICLE in article buffer."
6983   (when (gnus-buffer-live-p gnus-article-buffer)
6984     (with-current-buffer gnus-article-buffer
6985       (set-buffer-multibyte t)))
6986   (gnus-set-global-variables)
6987   (when (gnus-buffer-live-p gnus-article-buffer)
6988     (with-current-buffer gnus-article-buffer
6989       (setq gnus-article-charset gnus-newsgroup-charset)
6990       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6991       (set-buffer-multibyte t)))
6992   (if (null article)
6993       nil
6994     (prog1
6995         (if gnus-summary-display-article-function
6996             (funcall gnus-summary-display-article-function article all-header)
6997           (gnus-article-prepare article all-header))
6998       (with-current-buffer gnus-article-buffer
6999         (set (make-local-variable 'gnus-summary-search-article-matched-data)
7000              nil))
7001       (gnus-run-hooks 'gnus-select-article-hook)
7002       (when (and gnus-current-article
7003                  (not (zerop gnus-current-article)))
7004         (gnus-summary-goto-subject gnus-current-article))
7005       (gnus-summary-recenter)
7006       (when (and gnus-use-trees gnus-show-threads)
7007         (gnus-possibly-generate-tree article)
7008         (gnus-highlight-selected-tree article))
7009       ;; Successfully display article.
7010       (gnus-article-set-window-start
7011        (cdr (assq article gnus-newsgroup-bookmarks))))))
7012
7013 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7014   "Select the current article.
7015 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7016 non-nil, the article will be re-fetched even if it already present in
7017 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7018 be displayed."
7019   ;; Make sure we are in the summary buffer to work around bbdb bug.
7020   (unless (eq major-mode 'gnus-summary-mode)
7021     (set-buffer gnus-summary-buffer))
7022   (let ((article (or article (gnus-summary-article-number)))
7023         (all-headers (not (not all-headers))) ;Must be T or NIL.
7024         gnus-summary-display-article-function)
7025     (and (not pseudo)
7026          (gnus-summary-article-pseudo-p article)
7027          (error "This is a pseudo-article"))
7028     (save-excursion
7029       (set-buffer gnus-summary-buffer)
7030       (if (or (and gnus-single-article-buffer
7031                    (or (null gnus-current-article)
7032                        (null gnus-article-current)
7033                        (null (get-buffer gnus-article-buffer))
7034                        (not (eq article (cdr gnus-article-current)))
7035                        (not (equal (car gnus-article-current)
7036                                    gnus-newsgroup-name))))
7037               (and (not gnus-single-article-buffer)
7038                    (or (null gnus-current-article)
7039                        (not (eq gnus-current-article article))))
7040               force)
7041           ;; The requested article is different from the current article.
7042           (progn
7043             (gnus-summary-display-article article all-headers)
7044             (gnus-article-set-window-start
7045              (cdr (assq article gnus-newsgroup-bookmarks)))
7046             article)
7047         'old))))
7048
7049 (defun gnus-summary-force-verify-and-decrypt ()
7050   "Display buttons for signed/encrypted parts and verify/decrypt them."
7051   (interactive)
7052   (let ((mm-verify-option 'known)
7053         (mm-decrypt-option 'known)
7054         (gnus-article-emulate-mime t)
7055         (gnus-buttonized-mime-types (append (list "multipart/signed"
7056                                                   "multipart/encrypted")
7057                                             gnus-buttonized-mime-types)))
7058     (gnus-summary-select-article nil 'force)))
7059
7060 (defun gnus-summary-set-current-mark (&optional current-mark)
7061   "Obsolete function."
7062   nil)
7063
7064 (defun gnus-summary-next-article (&optional unread subject backward push)
7065   "Select the next article.
7066 If UNREAD, only unread articles are selected.
7067 If SUBJECT, only articles with SUBJECT are selected.
7068 If BACKWARD, the previous article is selected instead of the next."
7069   (interactive "P")
7070   (cond
7071    ;; Is there such an article?
7072    ((and (gnus-summary-search-forward unread subject backward)
7073          (or (gnus-summary-display-article (gnus-summary-article-number))
7074              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7075     (gnus-summary-position-point))
7076    ;; If not, we try the first unread, if that is wanted.
7077    ((and subject
7078          gnus-auto-select-same
7079          (gnus-summary-first-unread-article))
7080     (gnus-summary-position-point)
7081     (gnus-message 6 "Wrapped"))
7082    ;; Try to get next/previous article not displayed in this group.
7083    ((and gnus-auto-extend-newsgroup
7084          (not unread) (not subject))
7085     (gnus-summary-goto-article
7086      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7087      nil (count-lines (point-min) (point))))
7088    ;; Go to next/previous group.
7089    (t
7090     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7091       (gnus-summary-jump-to-group gnus-newsgroup-name))
7092     (let ((cmd last-command-char)
7093           (point
7094            (save-excursion
7095              (set-buffer gnus-group-buffer)
7096              (point)))
7097           (group
7098            (if (eq gnus-keep-same-level 'best)
7099                (gnus-summary-best-group gnus-newsgroup-name)
7100              (gnus-summary-search-group backward gnus-keep-same-level))))
7101       ;; For some reason, the group window gets selected.  We change
7102       ;; it back.
7103       (select-window (get-buffer-window (current-buffer)))
7104       ;; Select next unread newsgroup automagically.
7105       (cond
7106        ((or (not gnus-auto-select-next)
7107             (not cmd))
7108         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7109        ((or (eq gnus-auto-select-next 'quietly)
7110             (and (eq gnus-auto-select-next 'slightly-quietly)
7111                  push)
7112             (and (eq gnus-auto-select-next 'almost-quietly)
7113                  (gnus-summary-last-article-p)))
7114         ;; Select quietly.
7115         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7116             (gnus-summary-exit)
7117           (gnus-message 7 "No more%s articles (%s)..."
7118                         (if unread " unread" "")
7119                         (if group (concat "selecting " group)
7120                           "exiting"))
7121           (gnus-summary-next-group nil group backward)))
7122        (t
7123         (when (gnus-key-press-event-p last-input-event)
7124           (gnus-summary-walk-group-buffer
7125            gnus-newsgroup-name cmd unread backward point))))))))
7126
7127 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7128   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7129                       (?\C-p (gnus-group-prev-unread-group 1))))
7130         (cursor-in-echo-area t)
7131         keve key group ended prompt)
7132     (save-excursion
7133       (set-buffer gnus-group-buffer)
7134       (goto-char start)
7135       (setq group
7136             (if (eq gnus-keep-same-level 'best)
7137                 (gnus-summary-best-group gnus-newsgroup-name)
7138               (gnus-summary-search-group backward gnus-keep-same-level))))
7139     (while (not ended)
7140       (setq prompt
7141             (format
7142              "No more%s articles%s " (if unread " unread" "")
7143              (if (and group
7144                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7145                  (format " (Type %s for %s [%s])"
7146                          (single-key-description cmd) group
7147                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7148                (format " (Type %s to exit %s)"
7149                        (single-key-description cmd)
7150                        gnus-newsgroup-name))))
7151       ;; Confirm auto selection.
7152       (setq key (car (setq keve (gnus-read-event-char prompt)))
7153             ended t)
7154       (cond
7155        ((assq key keystrokes)
7156         (let ((obuf (current-buffer)))
7157           (switch-to-buffer gnus-group-buffer)
7158           (when group
7159             (gnus-group-jump-to-group group))
7160           (eval (cadr (assq key keystrokes)))
7161           (setq group (gnus-group-group-name))
7162           (switch-to-buffer obuf))
7163         (setq ended nil))
7164        ((equal key cmd)
7165         (if (or (not group)
7166                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7167             (gnus-summary-exit)
7168           (gnus-summary-next-group nil group backward)))
7169        (t
7170         (push (cdr keve) unread-command-events))))))
7171
7172 (defun gnus-summary-next-unread-article ()
7173   "Select unread article after current one."
7174   (interactive)
7175   (gnus-summary-next-article
7176    (or (not (eq gnus-summary-goto-unread 'never))
7177        (gnus-summary-last-article-p (gnus-summary-article-number)))
7178    (and gnus-auto-select-same
7179         (gnus-summary-article-subject))))
7180
7181 (defun gnus-summary-prev-article (&optional unread subject)
7182   "Select the article after the current one.
7183 If UNREAD is non-nil, only unread articles are selected."
7184   (interactive "P")
7185   (gnus-summary-next-article unread subject t))
7186
7187 (defun gnus-summary-prev-unread-article ()
7188   "Select unread article before current one."
7189   (interactive)
7190   (gnus-summary-prev-article
7191    (or (not (eq gnus-summary-goto-unread 'never))
7192        (gnus-summary-first-article-p (gnus-summary-article-number)))
7193    (and gnus-auto-select-same
7194         (gnus-summary-article-subject))))
7195
7196 (defun gnus-summary-next-page (&optional lines circular stop)
7197   "Show next page of the selected article.
7198 If at the end of the current article, select the next article.
7199 LINES says how many lines should be scrolled up.
7200
7201 If CIRCULAR is non-nil, go to the start of the article instead of
7202 selecting the next article when reaching the end of the current
7203 article.
7204
7205 If STOP is non-nil, just stop when reaching the end of the message.
7206
7207 Also see the variable `gnus-article-skip-boring'."
7208   (interactive "P")
7209   (setq gnus-summary-buffer (current-buffer))
7210   (gnus-set-global-variables)
7211   (let ((article (gnus-summary-article-number))
7212         (article-window (get-buffer-window gnus-article-buffer t))
7213         endp)
7214     ;; If the buffer is empty, we have no article.
7215     (unless article
7216       (error "No article to select"))
7217     (gnus-configure-windows 'article)
7218     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7219         (if (and (eq gnus-summary-goto-unread 'never)
7220                  (not (gnus-summary-last-article-p article)))
7221             (gnus-summary-next-article)
7222           (gnus-summary-next-unread-article))
7223       (if (or (null gnus-current-article)
7224               (null gnus-article-current)
7225               (/= article (cdr gnus-article-current))
7226               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7227           ;; Selected subject is different from current article's.
7228           (gnus-summary-display-article article)
7229         (when article-window
7230           (gnus-eval-in-buffer-window gnus-article-buffer
7231             (setq endp (or (gnus-article-next-page lines)
7232                            (gnus-article-only-boring-p))))
7233           (when endp
7234             (cond (stop
7235                    (gnus-message 3 "End of message"))
7236                   (circular
7237                    (gnus-summary-beginning-of-article))
7238                   (lines
7239                    (gnus-message 3 "End of message"))
7240                   ((null lines)
7241                    (if (and (eq gnus-summary-goto-unread 'never)
7242                             (not (gnus-summary-last-article-p article)))
7243                        (gnus-summary-next-article)
7244                      (gnus-summary-next-unread-article))))))))
7245     (gnus-summary-recenter)
7246     (gnus-summary-position-point)))
7247
7248 (defun gnus-summary-prev-page (&optional lines move)
7249   "Show previous page of selected article.
7250 Argument LINES specifies lines to be scrolled down.
7251 If MOVE, move to the previous unread article if point is at
7252 the beginning of the buffer."
7253   (interactive "P")
7254   (let ((article (gnus-summary-article-number))
7255         (article-window (get-buffer-window gnus-article-buffer t))
7256         endp)
7257     (gnus-configure-windows 'article)
7258     (if (or (null gnus-current-article)
7259             (null gnus-article-current)
7260             (/= article (cdr gnus-article-current))
7261             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7262         ;; Selected subject is different from current article's.
7263         (gnus-summary-display-article article)
7264       (gnus-summary-recenter)
7265       (when article-window
7266         (gnus-eval-in-buffer-window gnus-article-buffer
7267           (setq endp (gnus-article-prev-page lines)))
7268         (when (and move endp)
7269           (cond (lines
7270                  (gnus-message 3 "Beginning of message"))
7271                 ((null lines)
7272                  (if (and (eq gnus-summary-goto-unread 'never)
7273                           (not (gnus-summary-first-article-p article)))
7274                      (gnus-summary-prev-article)
7275                    (gnus-summary-prev-unread-article))))))))
7276   (gnus-summary-position-point))
7277
7278 (defun gnus-summary-prev-page-or-article (&optional lines)
7279   "Show previous page of selected article.
7280 Argument LINES specifies lines to be scrolled down.
7281 If at the beginning of the article, go to the next article."
7282   (interactive "P")
7283   (gnus-summary-prev-page lines t))
7284
7285 (defun gnus-summary-scroll-up (lines)
7286   "Scroll up (or down) one line current article.
7287 Argument LINES specifies lines to be scrolled up (or down if negative)."
7288   (interactive "p")
7289   (gnus-configure-windows 'article)
7290   (gnus-summary-show-thread)
7291   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7292     (gnus-eval-in-buffer-window gnus-article-buffer
7293       (cond ((> lines 0)
7294              (when (gnus-article-next-page lines)
7295                (gnus-message 3 "End of message")))
7296             ((< lines 0)
7297              (gnus-article-prev-page (- lines))))))
7298   (gnus-summary-recenter)
7299   (gnus-summary-position-point))
7300
7301 (defun gnus-summary-scroll-down (lines)
7302   "Scroll down (or up) one line current article.
7303 Argument LINES specifies lines to be scrolled down (or up if negative)."
7304   (interactive "p")
7305   (gnus-summary-scroll-up (- lines)))
7306
7307 (defun gnus-summary-next-same-subject ()
7308   "Select next article which has the same subject as current one."
7309   (interactive)
7310   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7311
7312 (defun gnus-summary-prev-same-subject ()
7313   "Select previous article which has the same subject as current one."
7314   (interactive)
7315   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7316
7317 (defun gnus-summary-next-unread-same-subject ()
7318   "Select next unread article which has the same subject as current one."
7319   (interactive)
7320   (gnus-summary-next-article t (gnus-summary-article-subject)))
7321
7322 (defun gnus-summary-prev-unread-same-subject ()
7323   "Select previous unread article which has the same subject as current one."
7324   (interactive)
7325   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7326
7327 (defun gnus-summary-first-unread-article ()
7328   "Select the first unread article.
7329 Return nil if there are no unread articles."
7330   (interactive)
7331   (prog1
7332       (when (gnus-summary-first-subject t)
7333         (gnus-summary-show-thread)
7334         (gnus-summary-first-subject t)
7335         (gnus-summary-display-article (gnus-summary-article-number)))
7336     (gnus-summary-position-point)))
7337
7338 (defun gnus-summary-first-unread-subject ()
7339   "Place the point on the subject line of the first unread article.
7340 Return nil if there are no unread articles."
7341   (interactive)
7342   (prog1
7343       (when (gnus-summary-first-subject t)
7344         (gnus-summary-show-thread)
7345         (gnus-summary-first-subject t))
7346     (gnus-summary-position-point)))
7347
7348 (defun gnus-summary-first-unseen-subject ()
7349   "Place the point on the subject line of the first unseen article.
7350 Return nil if there are no unseen articles."
7351   (interactive)
7352   (prog1
7353       (when (gnus-summary-first-subject nil nil t)
7354         (gnus-summary-show-thread)
7355         (gnus-summary-first-subject nil nil t))
7356     (gnus-summary-position-point)))
7357
7358 (defun gnus-summary-first-unseen-or-unread-subject ()
7359   "Place the point on the subject line of the first unseen article or,
7360 if all article have been seen, on the subject line of the first unread
7361 article."
7362   (interactive)
7363   (prog1
7364       (unless (when (gnus-summary-first-subject nil nil t)
7365                 (gnus-summary-show-thread)
7366                 (gnus-summary-first-subject nil nil t))
7367         (when (gnus-summary-first-subject t)
7368           (gnus-summary-show-thread)
7369           (gnus-summary-first-subject t)))
7370     (gnus-summary-position-point)))
7371
7372 (defun gnus-summary-first-article ()
7373   "Select the first article.
7374 Return nil if there are no articles."
7375   (interactive)
7376   (prog1
7377       (when (gnus-summary-first-subject)
7378         (gnus-summary-show-thread)
7379         (gnus-summary-first-subject)
7380         (gnus-summary-display-article (gnus-summary-article-number)))
7381     (gnus-summary-position-point)))
7382
7383 (defun gnus-summary-best-unread-article (&optional arg)
7384   "Select the unread article with the highest score.
7385 If given a prefix argument, select the next unread article that has a
7386 score higher than the default score."
7387   (interactive "P")
7388   (let ((article (if arg
7389                      (gnus-summary-better-unread-subject)
7390                    (gnus-summary-best-unread-subject))))
7391     (if article
7392         (gnus-summary-goto-article article)
7393       (error "No unread articles"))))
7394
7395 (defun gnus-summary-best-unread-subject ()
7396   "Select the unread subject with the highest score."
7397   (interactive)
7398   (let ((best -1000000)
7399         (data gnus-newsgroup-data)
7400         article score)
7401     (while data
7402       (and (gnus-data-unread-p (car data))
7403            (> (setq score
7404                     (gnus-summary-article-score (gnus-data-number (car data))))
7405               best)
7406            (setq best score
7407                  article (gnus-data-number (car data))))
7408       (setq data (cdr data)))
7409     (when article
7410       (gnus-summary-goto-subject article))
7411     (gnus-summary-position-point)
7412     article))
7413
7414 (defun gnus-summary-better-unread-subject ()
7415   "Select the first unread subject that has a score over the default score."
7416   (interactive)
7417   (let ((data gnus-newsgroup-data)
7418         article score)
7419     (while (and (setq article (gnus-data-number (car data)))
7420                 (or (gnus-data-read-p (car data))
7421                     (not (> (gnus-summary-article-score article)
7422                             gnus-summary-default-score))))
7423       (setq data (cdr data)))
7424     (when article
7425       (gnus-summary-goto-subject article))
7426     (gnus-summary-position-point)
7427     article))
7428
7429 (defun gnus-summary-last-subject ()
7430   "Go to the last displayed subject line in the group."
7431   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7432     (when article
7433       (gnus-summary-goto-subject article))))
7434
7435 (defun gnus-summary-goto-article (article &optional all-headers force)
7436   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7437 If ALL-HEADERS is non-nil, no header lines are hidden.
7438 If FORCE, go to the article even if it isn't displayed.  If FORCE
7439 is a number, it is the line the article is to be displayed on."
7440   (interactive
7441    (list
7442     (completing-read
7443      "Article number or Message-ID: "
7444      (mapcar (lambda (number) (list (int-to-string number)))
7445              gnus-newsgroup-limit))
7446     current-prefix-arg
7447     t))
7448   (prog1
7449       (if (and (stringp article)
7450                (string-match "@\\|%40" article))
7451           (gnus-summary-refer-article article)
7452         (when (stringp article)
7453           (setq article (string-to-number article)))
7454         (if (gnus-summary-goto-subject article force)
7455             (gnus-summary-display-article article all-headers)
7456           (gnus-message 4 "Couldn't go to article %s" article) nil))
7457     (gnus-summary-position-point)))
7458
7459 (defun gnus-summary-goto-last-article ()
7460   "Go to the previously read article."
7461   (interactive)
7462   (prog1
7463       (when gnus-last-article
7464         (gnus-summary-goto-article gnus-last-article nil t))
7465     (gnus-summary-position-point)))
7466
7467 (defun gnus-summary-pop-article (number)
7468   "Pop one article off the history and go to the previous.
7469 NUMBER articles will be popped off."
7470   (interactive "p")
7471   (let (to)
7472     (setq gnus-newsgroup-history
7473           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7474     (if to
7475         (gnus-summary-goto-article (car to) nil t)
7476       (error "Article history empty")))
7477   (gnus-summary-position-point))
7478
7479 ;; Summary commands and functions for limiting the summary buffer.
7480
7481 (defun gnus-summary-limit-to-articles (n)
7482   "Limit the summary buffer to the next N articles.
7483 If not given a prefix, use the process marked articles instead."
7484   (interactive "P")
7485   (prog1
7486       (let ((articles (gnus-summary-work-articles n)))
7487         (setq gnus-newsgroup-processable nil)
7488         (gnus-summary-limit articles))
7489     (gnus-summary-position-point)))
7490
7491 (defun gnus-summary-pop-limit (&optional total)
7492   "Restore the previous limit.
7493 If given a prefix, remove all limits."
7494   (interactive "P")
7495   (when total
7496     (setq gnus-newsgroup-limits
7497           (list (mapcar (lambda (h) (mail-header-number h))
7498                         gnus-newsgroup-headers))))
7499   (unless gnus-newsgroup-limits
7500     (error "No limit to pop"))
7501   (prog1
7502       (gnus-summary-limit nil 'pop)
7503     (gnus-summary-position-point)))
7504
7505 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7506   "Limit the summary buffer to articles that have subjects that match a regexp.
7507 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7508   (interactive
7509    (list (read-string (if current-prefix-arg
7510                           "Exclude subject (regexp): "
7511                         "Limit to subject (regexp): "))
7512          nil current-prefix-arg))
7513   (unless header
7514     (setq header "subject"))
7515   (when (not (equal "" subject))
7516     (prog1
7517         (let ((articles (gnus-summary-find-matching
7518                          (or header "subject") subject 'all nil nil
7519                          not-matching)))
7520           (unless articles
7521             (error "Found no matches for \"%s\"" subject))
7522           (gnus-summary-limit articles))
7523       (gnus-summary-position-point))))
7524
7525 (defun gnus-summary-limit-to-author (from &optional not-matching)
7526   "Limit the summary buffer to articles that have authors that match a regexp.
7527 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7528   (interactive
7529    (list (read-string (if current-prefix-arg
7530                           "Exclude author (regexp): "
7531                         "Limit to author (regexp): "))
7532          current-prefix-arg))
7533   (gnus-summary-limit-to-subject from "from" not-matching))
7534
7535 (defun gnus-summary-limit-to-age (age &optional younger-p)
7536   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7537 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7538 articles that are younger than AGE days."
7539   (interactive
7540    (let ((younger current-prefix-arg)
7541          (days-got nil)
7542          days)
7543      (while (not days-got)
7544        (setq days (if younger
7545                       (read-string "Limit to articles younger than (in days, older when negative): ")
7546                     (read-string
7547                      "Limit to articles older than (in days, younger when negative): ")))
7548        (when (> (length days) 0)
7549          (setq days (read days)))
7550        (if (numberp days)
7551            (progn
7552              (setq days-got t)
7553              (if (< days 0)
7554                  (progn
7555                    (setq younger (not younger))
7556                    (setq days (* days -1)))))
7557          (message "Please enter a number.")
7558          (sleep-for 1)))
7559      (list days younger)))
7560   (prog1
7561       (let ((data gnus-newsgroup-data)
7562             (cutoff (days-to-time age))
7563             articles d date is-younger)
7564         (while (setq d (pop data))
7565           (when (and (vectorp (gnus-data-header d))
7566                      (setq date (mail-header-date (gnus-data-header d))))
7567             (setq is-younger (time-less-p
7568                               (time-since (condition-case ()
7569                                               (date-to-time date)
7570                                             (error '(0 0))))
7571                               cutoff))
7572             (when (if younger-p
7573                       is-younger
7574                     (not is-younger))
7575               (push (gnus-data-number d) articles))))
7576         (gnus-summary-limit (nreverse articles)))
7577     (gnus-summary-position-point)))
7578
7579 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7580   "Limit the summary buffer to articles that match an 'extra' header."
7581   (interactive
7582    (let ((header
7583           (intern
7584            (gnus-completing-read-with-default
7585             (symbol-name (car gnus-extra-headers))
7586             (if current-prefix-arg
7587                 "Exclude extra header:"
7588               "Limit extra header:")
7589             (mapcar (lambda (x)
7590                       (cons (symbol-name x) x))
7591                     gnus-extra-headers)
7592             nil
7593             t))))
7594      (list header
7595            (read-string (format "%s header %s (regexp): "
7596                                 (if current-prefix-arg "Exclude" "Limit to")
7597                                 header))
7598            current-prefix-arg)))
7599   (when (not (equal "" regexp))
7600     (prog1
7601         (let ((articles (gnus-summary-find-matching
7602                          (cons 'extra header) regexp 'all nil nil
7603                          not-matching)))
7604           (unless articles
7605             (error "Found no matches for \"%s\"" regexp))
7606           (gnus-summary-limit articles))
7607       (gnus-summary-position-point))))
7608
7609 (defun gnus-summary-limit-to-display-predicate ()
7610   "Limit the summary buffer to the predicated in the `display' group parameter."
7611   (interactive)
7612   (unless gnus-newsgroup-display
7613     (error "There is no `display' group parameter"))
7614   (let (articles)
7615     (dolist (number gnus-newsgroup-articles)
7616       (when (funcall gnus-newsgroup-display)
7617         (push number articles)))
7618     (gnus-summary-limit articles))
7619   (gnus-summary-position-point))
7620
7621 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7622 (make-obsolete
7623  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7624
7625 (defun gnus-summary-limit-to-unread (&optional all)
7626   "Limit the summary buffer to articles that are not marked as read.
7627 If ALL is non-nil, limit strictly to unread articles."
7628   (interactive "P")
7629   (if all
7630       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7631     (gnus-summary-limit-to-marks
7632      ;; Concat all the marks that say that an article is read and have
7633      ;; those removed.
7634      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7635            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7636            gnus-low-score-mark gnus-expirable-mark
7637            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7638            gnus-duplicate-mark gnus-souped-mark)
7639      'reverse)))
7640
7641 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7642 (make-obsolete 'gnus-summary-delete-marked-with
7643                'gnus-summary-limit-exclude-marks)
7644
7645 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7646   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7647 If REVERSE, limit the summary buffer to articles that are marked
7648 with MARKS.  MARKS can either be a string of marks or a list of marks.
7649 Returns how many articles were removed."
7650   (interactive "sMarks: ")
7651   (gnus-summary-limit-to-marks marks t))
7652
7653 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7654   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7655 If REVERSE (the prefix), limit the summary buffer to articles that are
7656 not marked with MARKS.  MARKS can either be a string of marks or a
7657 list of marks.
7658 Returns how many articles were removed."
7659   (interactive "sMarks: \nP")
7660   (prog1
7661       (let ((data gnus-newsgroup-data)
7662             (marks (if (listp marks) marks
7663                      (append marks nil))) ; Transform to list.
7664             articles)
7665         (while data
7666           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7667                   (memq (gnus-data-mark (car data)) marks))
7668             (push (gnus-data-number (car data)) articles))
7669           (setq data (cdr data)))
7670         (gnus-summary-limit articles))
7671     (gnus-summary-position-point)))
7672
7673 (defun gnus-summary-limit-to-score (score)
7674   "Limit to articles with score at or above SCORE."
7675   (interactive "NLimit to articles with score of at least: ")
7676   (let ((data gnus-newsgroup-data)
7677         articles)
7678     (while data
7679       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7680                 score)
7681         (push (gnus-data-number (car data)) articles))
7682       (setq data (cdr data)))
7683     (prog1
7684         (gnus-summary-limit articles)
7685       (gnus-summary-position-point))))
7686
7687 (defun gnus-summary-limit-to-unseen ()
7688   "Limit to unseen articles."
7689   (interactive)
7690   (prog1
7691       (gnus-summary-limit gnus-newsgroup-unseen)
7692     (gnus-summary-position-point)))
7693
7694 (defun gnus-summary-limit-include-thread (id)
7695   "Display all the hidden articles that is in the thread with ID in it.
7696 When called interactively, ID is the Message-ID of the current
7697 article."
7698   (interactive (list (mail-header-id (gnus-summary-article-header))))
7699   (let ((articles (gnus-articles-in-thread
7700                    (gnus-id-to-thread (gnus-root-id id)))))
7701     (prog1
7702         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7703       (gnus-summary-limit-include-matching-articles
7704        "subject"
7705        (regexp-quote (gnus-simplify-subject-re
7706                       (mail-header-subject (gnus-id-to-header id)))))
7707       (gnus-summary-position-point))))
7708
7709 (defun gnus-summary-limit-include-matching-articles (header regexp)
7710   "Display all the hidden articles that have HEADERs that match REGEXP."
7711   (interactive (list (read-string "Match on header: ")
7712                      (read-string "Regexp: ")))
7713   (let ((articles (gnus-find-matching-articles header regexp)))
7714     (prog1
7715         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7716       (gnus-summary-position-point))))
7717
7718 (defun gnus-summary-insert-dormant-articles ()
7719   "Insert all the dormant articles for this group into the current buffer."
7720   (interactive)
7721   (let ((gnus-verbose (max 6 gnus-verbose)))
7722     (if (not gnus-newsgroup-dormant)
7723         (gnus-message 3 "No cached articles for this group")
7724       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7725
7726 (defun gnus-summary-limit-include-dormant ()
7727   "Display all the hidden articles that are marked as dormant.
7728 Note that this command only works on a subset of the articles currently
7729 fetched for this group."
7730   (interactive)
7731   (unless gnus-newsgroup-dormant
7732     (error "There are no dormant articles in this group"))
7733   (prog1
7734       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7735     (gnus-summary-position-point)))
7736
7737 (defun gnus-summary-limit-exclude-dormant ()
7738   "Hide all dormant articles."
7739   (interactive)
7740   (prog1
7741       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7742     (gnus-summary-position-point)))
7743
7744 (defun gnus-summary-limit-exclude-childless-dormant ()
7745   "Hide all dormant articles that have no children."
7746   (interactive)
7747   (let ((data (gnus-data-list t))
7748         articles d children)
7749     ;; Find all articles that are either not dormant or have
7750     ;; children.
7751     (while (setq d (pop data))
7752       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7753                 (and (setq children
7754                            (gnus-article-children (gnus-data-number d)))
7755                      (let (found)
7756                        (while children
7757                          (when (memq (car children) articles)
7758                            (setq children nil
7759                                  found t))
7760                          (pop children))
7761                        found)))
7762         (push (gnus-data-number d) articles)))
7763     ;; Do the limiting.
7764     (prog1
7765         (gnus-summary-limit articles)
7766       (gnus-summary-position-point))))
7767
7768 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7769   "Mark all unread excluded articles as read.
7770 If ALL, mark even excluded ticked and dormants as read."
7771   (interactive "P")
7772   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7773   (let ((articles (gnus-sorted-ndifference
7774                    (sort
7775                     (mapcar (lambda (h) (mail-header-number h))
7776                             gnus-newsgroup-headers)
7777                     '<)
7778                    gnus-newsgroup-limit))
7779         article)
7780     (setq gnus-newsgroup-unreads
7781           (gnus-sorted-intersection gnus-newsgroup-unreads
7782                                     gnus-newsgroup-limit))
7783     (if all
7784         (setq gnus-newsgroup-dormant nil
7785               gnus-newsgroup-marked nil
7786               gnus-newsgroup-reads
7787               (nconc
7788                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7789                gnus-newsgroup-reads))
7790       (while (setq article (pop articles))
7791         (unless (or (memq article gnus-newsgroup-dormant)
7792                     (memq article gnus-newsgroup-marked))
7793           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7794
7795 (defun gnus-summary-limit (articles &optional pop)
7796   (if pop
7797       ;; We pop the previous limit off the stack and use that.
7798       (setq articles (car gnus-newsgroup-limits)
7799             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7800     ;; We use the new limit, so we push the old limit on the stack.
7801     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7802   ;; Set the limit.
7803   (setq gnus-newsgroup-limit articles)
7804   (let ((total (length gnus-newsgroup-data))
7805         (data (gnus-data-find-list (gnus-summary-article-number)))
7806         (gnus-summary-mark-below nil)   ; Inhibit this.
7807         found)
7808     ;; This will do all the work of generating the new summary buffer
7809     ;; according to the new limit.
7810     (gnus-summary-prepare)
7811     ;; Hide any threads, possibly.
7812     (gnus-summary-maybe-hide-threads)
7813     ;; Try to return to the article you were at, or one in the
7814     ;; neighborhood.
7815     (when data
7816       ;; We try to find some article after the current one.
7817       (while data
7818         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7819           (setq data nil
7820                 found t))
7821         (setq data (cdr data))))
7822     (unless found
7823       ;; If there is no data, that means that we were after the last
7824       ;; article.  The same goes when we can't find any articles
7825       ;; after the current one.
7826       (goto-char (point-max))
7827       (gnus-summary-find-prev))
7828     (gnus-set-mode-line 'summary)
7829     ;; We return how many articles were removed from the summary
7830     ;; buffer as a result of the new limit.
7831     (- total (length gnus-newsgroup-data))))
7832
7833 (defsubst gnus-invisible-cut-children (threads)
7834   (let ((num 0))
7835     (while threads
7836       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7837         (incf num))
7838       (pop threads))
7839     (< num 2)))
7840
7841 (defsubst gnus-cut-thread (thread)
7842   "Go forwards in the thread until we find an article that we want to display."
7843   (when (or (eq gnus-fetch-old-headers 'some)
7844             (eq gnus-fetch-old-headers 'invisible)
7845             (numberp gnus-fetch-old-headers)
7846             (eq gnus-build-sparse-threads 'some)
7847             (eq gnus-build-sparse-threads 'more))
7848     ;; Deal with old-fetched headers and sparse threads.
7849     (while (and
7850             thread
7851             (or
7852              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7853              (gnus-summary-article-ancient-p
7854               (mail-header-number (car thread))))
7855             (if (or (<= (length (cdr thread)) 1)
7856                     (eq gnus-fetch-old-headers 'invisible))
7857                 (setq gnus-newsgroup-limit
7858                       (delq (mail-header-number (car thread))
7859                             gnus-newsgroup-limit)
7860                       thread (cadr thread))
7861               (when (gnus-invisible-cut-children (cdr thread))
7862                 (let ((th (cdr thread)))
7863                   (while th
7864                     (if (memq (mail-header-number (caar th))
7865                               gnus-newsgroup-limit)
7866                         (setq thread (car th)
7867                               th nil)
7868                       (setq th (cdr th))))))))))
7869   thread)
7870
7871 (defun gnus-cut-threads (threads)
7872   "Cut off all uninteresting articles from the beginning of THREADS."
7873   (when (or (eq gnus-fetch-old-headers 'some)
7874             (eq gnus-fetch-old-headers 'invisible)
7875             (numberp gnus-fetch-old-headers)
7876             (eq gnus-build-sparse-threads 'some)
7877             (eq gnus-build-sparse-threads 'more))
7878     (let ((th threads))
7879       (while th
7880         (setcar th (gnus-cut-thread (car th)))
7881         (setq th (cdr th)))))
7882   ;; Remove nixed out threads.
7883   (delq nil threads))
7884
7885 (defun gnus-summary-initial-limit (&optional show-if-empty)
7886   "Figure out what the initial limit is supposed to be on group entry.
7887 This entails weeding out unwanted dormants, low-scored articles,
7888 fetch-old-headers verbiage, and so on."
7889   ;; Most groups have nothing to remove.
7890   (if (or gnus-inhibit-limiting
7891           (and (null gnus-newsgroup-dormant)
7892                (eq gnus-newsgroup-display 'gnus-not-ignore)
7893                (not (eq gnus-fetch-old-headers 'some))
7894                (not (numberp gnus-fetch-old-headers))
7895                (not (eq gnus-fetch-old-headers 'invisible))
7896                (null gnus-summary-expunge-below)
7897                (not (eq gnus-build-sparse-threads 'some))
7898                (not (eq gnus-build-sparse-threads 'more))
7899                (null gnus-thread-expunge-below)
7900                (not gnus-use-nocem)))
7901       ()                                ; Do nothing.
7902     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7903     (setq gnus-newsgroup-limit nil)
7904     (mapatoms
7905      (lambda (node)
7906        (unless (car (symbol-value node))
7907          ;; These threads have no parents -- they are roots.
7908          (let ((nodes (cdr (symbol-value node)))
7909                thread)
7910            (while nodes
7911              (if (and gnus-thread-expunge-below
7912                       (< (gnus-thread-total-score (car nodes))
7913                          gnus-thread-expunge-below))
7914                  (gnus-expunge-thread (pop nodes))
7915                (setq thread (pop nodes))
7916                (gnus-summary-limit-children thread))))))
7917      gnus-newsgroup-dependencies)
7918     ;; If this limitation resulted in an empty group, we might
7919     ;; pop the previous limit and use it instead.
7920     (when (and (not gnus-newsgroup-limit)
7921                show-if-empty)
7922       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7923     gnus-newsgroup-limit))
7924
7925 (defun gnus-summary-limit-children (thread)
7926   "Return 1 if this subthread is visible and 0 if it is not."
7927   ;; First we get the number of visible children to this thread.  This
7928   ;; is done by recursing down the thread using this function, so this
7929   ;; will really go down to a leaf article first, before slowly
7930   ;; working its way up towards the root.
7931   (when thread
7932     (let* ((max-lisp-eval-depth 5000)
7933            (children
7934            (if (cdr thread)
7935                (apply '+ (mapcar 'gnus-summary-limit-children
7936                                  (cdr thread)))
7937              0))
7938           (number (mail-header-number (car thread)))
7939           score)
7940       (if (and
7941            (not (memq number gnus-newsgroup-marked))
7942            (or
7943             ;; If this article is dormant and has absolutely no visible
7944             ;; children, then this article isn't visible.
7945             (and (memq number gnus-newsgroup-dormant)
7946                  (zerop children))
7947             ;; If this is "fetch-old-headered" and there is no
7948             ;; visible children, then we don't want this article.
7949             (and (or (eq gnus-fetch-old-headers 'some)
7950                      (numberp gnus-fetch-old-headers))
7951                  (gnus-summary-article-ancient-p number)
7952                  (zerop children))
7953             ;; If this is "fetch-old-headered" and `invisible', then
7954             ;; we don't want this article.
7955             (and (eq gnus-fetch-old-headers 'invisible)
7956                  (gnus-summary-article-ancient-p number))
7957             ;; If this is a sparsely inserted article with no children,
7958             ;; we don't want it.
7959             (and (eq gnus-build-sparse-threads 'some)
7960                  (gnus-summary-article-sparse-p number)
7961                  (zerop children))
7962             ;; If we use expunging, and this article is really
7963             ;; low-scored, then we don't want this article.
7964             (when (and gnus-summary-expunge-below
7965                        (< (setq score
7966                                 (or (cdr (assq number gnus-newsgroup-scored))
7967                                     gnus-summary-default-score))
7968                           gnus-summary-expunge-below))
7969               ;; We increase the expunge-tally here, but that has
7970               ;; nothing to do with the limits, really.
7971               (incf gnus-newsgroup-expunged-tally)
7972               ;; We also mark as read here, if that's wanted.
7973               (when (and gnus-summary-mark-below
7974                          (< score gnus-summary-mark-below))
7975                 (setq gnus-newsgroup-unreads
7976                       (delq number gnus-newsgroup-unreads))
7977                 (if gnus-newsgroup-auto-expire
7978                     (push number gnus-newsgroup-expirable)
7979                   (push (cons number gnus-low-score-mark)
7980                         gnus-newsgroup-reads)))
7981               t)
7982             ;; Do the `display' group parameter.
7983             (and gnus-newsgroup-display
7984                  (not (funcall gnus-newsgroup-display)))
7985             ;; Check NoCeM things.
7986             (if (and gnus-use-nocem
7987                      (gnus-nocem-unwanted-article-p
7988                       (mail-header-id (car thread))))
7989                 (progn
7990                   (setq gnus-newsgroup-unreads
7991                         (delq number gnus-newsgroup-unreads))
7992                   t))))
7993           ;; Nope, invisible article.
7994           0
7995         ;; Ok, this article is to be visible, so we add it to the limit
7996         ;; and return 1.
7997         (push number gnus-newsgroup-limit)
7998         1))))
7999
8000 (defun gnus-expunge-thread (thread)
8001   "Mark all articles in THREAD as read."
8002   (let* ((number (mail-header-number (car thread))))
8003     (incf gnus-newsgroup-expunged-tally)
8004     ;; We also mark as read here, if that's wanted.
8005     (setq gnus-newsgroup-unreads
8006           (delq number gnus-newsgroup-unreads))
8007     (if gnus-newsgroup-auto-expire
8008         (push number gnus-newsgroup-expirable)
8009       (push (cons number gnus-low-score-mark)
8010             gnus-newsgroup-reads)))
8011   ;; Go recursively through all subthreads.
8012   (mapcar 'gnus-expunge-thread (cdr thread)))
8013
8014 ;; Summary article oriented commands
8015
8016 (defun gnus-summary-refer-parent-article (n)
8017   "Refer parent article N times.
8018 If N is negative, go to ancestor -N instead.
8019 The difference between N and the number of articles fetched is returned."
8020   (interactive "p")
8021   (let ((skip 1)
8022         error header ref)
8023     (when (not (natnump n))
8024       (setq skip (abs n)
8025             n 1))
8026     (while (and (> n 0)
8027                 (not error))
8028       (setq header (gnus-summary-article-header))
8029       (if (and (eq (mail-header-number header)
8030                    (cdr gnus-article-current))
8031                (equal gnus-newsgroup-name
8032                       (car gnus-article-current)))
8033           ;; If we try to find the parent of the currently
8034           ;; displayed article, then we take a look at the actual
8035           ;; References header, since this is slightly more
8036           ;; reliable than the References field we got from the
8037           ;; server.
8038           (save-excursion
8039             (set-buffer gnus-original-article-buffer)
8040             (nnheader-narrow-to-headers)
8041             (unless (setq ref (message-fetch-field "references"))
8042               (when (setq ref (message-fetch-field "in-reply-to"))
8043                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8044             (widen))
8045         (setq ref
8046               ;; It's not the current article, so we take a bet on
8047               ;; the value we got from the server.
8048               (mail-header-references header)))
8049       (if (and ref
8050                (not (equal ref "")))
8051           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8052             (gnus-message 1 "Couldn't find parent"))
8053         (gnus-message 1 "No references in article %d"
8054                       (gnus-summary-article-number))
8055         (setq error t))
8056       (decf n))
8057     (gnus-summary-position-point)
8058     n))
8059
8060 (defun gnus-summary-refer-references ()
8061   "Fetch all articles mentioned in the References header.
8062 Return the number of articles fetched."
8063   (interactive)
8064   (let ((ref (mail-header-references (gnus-summary-article-header)))
8065         (current (gnus-summary-article-number))
8066         (n 0))
8067     (if (or (not ref)
8068             (equal ref ""))
8069         (error "No References in the current article")
8070       ;; For each Message-ID in the References header...
8071       (while (string-match "<[^>]*>" ref)
8072         (incf n)
8073         ;; ... fetch that article.
8074         (gnus-summary-refer-article
8075          (prog1 (match-string 0 ref)
8076            (setq ref (substring ref (match-end 0))))))
8077       (gnus-summary-goto-subject current)
8078       (gnus-summary-position-point)
8079       n)))
8080
8081 (defun gnus-summary-refer-thread (&optional limit)
8082   "Fetch all articles in the current thread.
8083 If LIMIT (the numerical prefix), fetch that many old headers instead
8084 of what's specified by the `gnus-refer-thread-limit' variable."
8085   (interactive "P")
8086   (let ((id (mail-header-id (gnus-summary-article-header)))
8087         (limit (if limit (prefix-numeric-value limit)
8088                  gnus-refer-thread-limit)))
8089     (unless (eq gnus-fetch-old-headers 'invisible)
8090       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8091       ;; Retrieve the headers and read them in.
8092       (if (eq (if (numberp limit)
8093                   (gnus-retrieve-headers
8094                    (list (min
8095                           (+ (mail-header-number
8096                               (gnus-summary-article-header))
8097                              limit)
8098                           gnus-newsgroup-end))
8099                    gnus-newsgroup-name (* limit 2))
8100                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8101                 ;; headers.
8102                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8103                                        gnus-newsgroup-name limit))
8104               'nov)
8105           (gnus-build-all-threads)
8106         (error "Can't fetch thread from back ends that don't support NOV"))
8107       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8108     (gnus-summary-limit-include-thread id)))
8109
8110 (defun gnus-summary-refer-article (message-id)
8111   "Fetch an article specified by MESSAGE-ID."
8112   (interactive "sMessage-ID: ")
8113   (when (and (stringp message-id)
8114              (not (zerop (length message-id))))
8115     (setq message-id (gnus-replace-in-string message-id " " ""))
8116     ;; Construct the correct Message-ID if necessary.
8117     ;; Suggested by tale@pawl.rpi.edu.
8118     (unless (string-match "^<" message-id)
8119       (setq message-id (concat "<" message-id)))
8120     (unless (string-match ">$" message-id)
8121       (setq message-id (concat message-id ">")))
8122     ;; People often post MIDs from URLs, so unhex it:
8123     (unless (string-match "@" message-id)
8124       (setq message-id (gnus-url-unhex-string message-id)))
8125     (let* ((header (gnus-id-to-header message-id))
8126            (sparse (and header
8127                         (gnus-summary-article-sparse-p
8128                          (mail-header-number header))
8129                         (memq (mail-header-number header)
8130                               gnus-newsgroup-limit)))
8131            number)
8132       (cond
8133        ;; If the article is present in the buffer we just go to it.
8134        ((and header
8135              (or (not (gnus-summary-article-sparse-p
8136                        (mail-header-number header)))
8137                  sparse))
8138         (prog1
8139             (gnus-summary-goto-article
8140              (mail-header-number header) nil t)
8141           (when sparse
8142             (gnus-summary-update-article (mail-header-number header)))))
8143        (t
8144         ;; We fetch the article.
8145         (catch 'found
8146           (dolist (gnus-override-method (gnus-refer-article-methods))
8147             (when (and (gnus-check-server gnus-override-method)
8148                        ;; Fetch the header,
8149                        (setq number (gnus-summary-insert-subject message-id)))
8150               ;; and display the article.
8151               (gnus-summary-select-article nil nil nil number)
8152               (throw 'found t)))
8153           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8154
8155 (defun gnus-refer-article-methods ()
8156   "Return a list of referable methods."
8157   (cond
8158    ;; No method, so we default to current and native.
8159    ((null gnus-refer-article-method)
8160     (list gnus-current-select-method gnus-select-method))
8161    ;; Current.
8162    ((eq 'current gnus-refer-article-method)
8163     (list gnus-current-select-method))
8164    ;; List of select methods.
8165    ((not (and (symbolp (car gnus-refer-article-method))
8166               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8167     (let (out)
8168       (dolist (method gnus-refer-article-method)
8169         (push (if (eq 'current method)
8170                   gnus-current-select-method
8171                 method)
8172               out))
8173       (nreverse out)))
8174    ;; One single select method.
8175    (t
8176     (list gnus-refer-article-method))))
8177
8178 (defun gnus-summary-edit-parameters ()
8179   "Edit the group parameters of the current group."
8180   (interactive)
8181   (gnus-group-edit-group gnus-newsgroup-name 'params))
8182
8183 (defun gnus-summary-customize-parameters ()
8184   "Customize the group parameters of the current group."
8185   (interactive)
8186   (gnus-group-customize gnus-newsgroup-name))
8187
8188 (defun gnus-summary-enter-digest-group (&optional force)
8189   "Enter an nndoc group based on the current article.
8190 If FORCE, force a digest interpretation.  If not, try
8191 to guess what the document format is."
8192   (interactive "P")
8193   (let ((conf gnus-current-window-configuration))
8194     (save-window-excursion
8195       (save-excursion
8196         (let (gnus-article-prepare-hook
8197               gnus-display-mime-function
8198               gnus-break-pages)
8199           (gnus-summary-select-article))))
8200     (setq gnus-current-window-configuration conf)
8201     (let* ((name (format "%s-%d"
8202                          (gnus-group-prefixed-name
8203                           gnus-newsgroup-name (list 'nndoc ""))
8204                          (save-excursion
8205                            (set-buffer gnus-summary-buffer)
8206                            gnus-current-article)))
8207            (ogroup gnus-newsgroup-name)
8208            (params (append (gnus-info-params (gnus-get-info ogroup))
8209                            (list (cons 'to-group ogroup))
8210                            (list (cons 'parent-group ogroup))
8211                            (list (cons 'save-article-group ogroup))))
8212            (case-fold-search t)
8213            (buf (current-buffer))
8214            dig to-address)
8215       (save-excursion
8216         (set-buffer gnus-original-article-buffer)
8217         ;; Have the digest group inherit the main mail address of
8218         ;; the parent article.
8219         (when (setq to-address (or (gnus-fetch-field "reply-to")
8220                                    (gnus-fetch-field "from")))
8221           (setq params (append
8222                         (list (cons 'to-address
8223                                     (funcall gnus-decode-encoded-word-function
8224                                              to-address))))))
8225         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8226         (insert-buffer-substring gnus-original-article-buffer)
8227         ;; Remove lines that may lead nndoc to misinterpret the
8228         ;; document type.
8229         (narrow-to-region
8230          (goto-char (point-min))
8231          (or (search-forward "\n\n" nil t) (point)))
8232         (goto-char (point-min))
8233         (delete-matching-lines "^Path:\\|^From ")
8234         (widen))
8235       (unwind-protect
8236           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8237                     (gnus-newsgroup-ephemeral-ignored-charsets
8238                      gnus-newsgroup-ignored-charsets))
8239                 (gnus-group-read-ephemeral-group
8240                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8241                               (nndoc-article-type
8242                                ,(if force 'mbox 'guess)))
8243                  t nil nil nil
8244                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8245                                                         "ADAPT")))))
8246               ;; Make all postings to this group go to the parent group.
8247               (nconc (gnus-info-params (gnus-get-info name))
8248                      params)
8249             ;; Couldn't select this doc group.
8250             (switch-to-buffer buf)
8251             (gnus-set-global-variables)
8252             (gnus-configure-windows 'summary)
8253             (gnus-message 3 "Article couldn't be entered?"))
8254         (kill-buffer dig)))))
8255
8256 (defun gnus-summary-read-document (n)
8257   "Open a new group based on the current article(s).
8258 This will allow you to read digests and other similar
8259 documents as newsgroups.
8260 Obeys the standard process/prefix convention."
8261   (interactive "P")
8262   (let* ((articles (gnus-summary-work-articles n))
8263          (ogroup gnus-newsgroup-name)
8264          (params (append (gnus-info-params (gnus-get-info ogroup))
8265                          (list (cons 'to-group ogroup))))
8266          article group egroup groups vgroup)
8267     (while (setq article (pop articles))
8268       (setq group (format "%s-%d" gnus-newsgroup-name article))
8269       (gnus-summary-remove-process-mark article)
8270       (when (gnus-summary-display-article article)
8271         (save-excursion
8272           (with-temp-buffer
8273             (insert-buffer-substring gnus-original-article-buffer)
8274             ;; Remove some headers that may lead nndoc to make
8275             ;; the wrong guess.
8276             (message-narrow-to-head)
8277             (goto-char (point-min))
8278             (delete-matching-lines "^\\(Path\\):\\|^From ")
8279             (widen)
8280             (if (setq egroup
8281                       (gnus-group-read-ephemeral-group
8282                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8283                                      (nndoc-article-type guess))
8284                        t nil t))
8285                 (progn
8286                   ;; Make all postings to this group go to the parent group.
8287                   (nconc (gnus-info-params (gnus-get-info egroup))
8288                          params)
8289                   (push egroup groups))
8290               ;; Couldn't select this doc group.
8291               (gnus-error 3 "Article couldn't be entered"))))))
8292     ;; Now we have selected all the documents.
8293     (cond
8294      ((not groups)
8295       (error "None of the articles could be interpreted as documents"))
8296      ((gnus-group-read-ephemeral-group
8297        (setq vgroup (format
8298                      "nnvirtual:%s-%s" gnus-newsgroup-name
8299                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8300        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8301        t
8302        (cons (current-buffer) 'summary)))
8303      (t
8304       (error "Couldn't select virtual nndoc group")))))
8305
8306 (defun gnus-summary-isearch-article (&optional regexp-p)
8307   "Do incremental search forward on the current article.
8308 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8309   (interactive "P")
8310   (let* ((gnus-inhibit-treatment t)
8311          (old (gnus-summary-select-article)))
8312     (gnus-configure-windows 'article)
8313     (gnus-eval-in-buffer-window gnus-article-buffer
8314       (save-restriction
8315         (widen)
8316         (when (eq 'old old)
8317           (gnus-article-show-all-headers))
8318         (goto-char (point-min))
8319         (isearch-forward regexp-p)))))
8320
8321 (defun gnus-summary-search-article-forward (regexp &optional backward)
8322   "Search for an article containing REGEXP forward.
8323 If BACKWARD, search backward instead."
8324   (interactive
8325    (list (read-string
8326           (format "Search article %s (regexp%s): "
8327                   (if current-prefix-arg "backward" "forward")
8328                   (if gnus-last-search-regexp
8329                       (concat ", default " gnus-last-search-regexp)
8330                     "")))
8331          current-prefix-arg))
8332   (if (string-equal regexp "")
8333       (setq regexp (or gnus-last-search-regexp ""))
8334     (setq gnus-last-search-regexp regexp)
8335     (setq gnus-article-before-search gnus-current-article))
8336   ;; Intentionally set gnus-last-article.
8337   (setq gnus-last-article gnus-article-before-search)
8338   (let ((gnus-last-article gnus-last-article))
8339     (if (gnus-summary-search-article regexp backward)
8340         (gnus-summary-show-thread)
8341       (error "Search failed: \"%s\"" regexp))))
8342
8343 (defun gnus-summary-search-article-backward (regexp)
8344   "Search for an article containing REGEXP backward."
8345   (interactive
8346    (list (read-string
8347           (format "Search article backward (regexp%s): "
8348                   (if gnus-last-search-regexp
8349                       (concat ", default " gnus-last-search-regexp)
8350                     "")))))
8351   (gnus-summary-search-article-forward regexp 'backward))
8352
8353 (eval-when-compile
8354   (defmacro gnus-summary-search-article-position-point (regexp backward)
8355     "Dehighlight the last matched text and goto the beginning position."
8356     (` (if (and gnus-summary-search-article-matched-data
8357                 (let ((text (caddr gnus-summary-search-article-matched-data))
8358                       (inhibit-read-only t)
8359                       buffer-read-only)
8360                   (delete-region
8361                    (goto-char (car gnus-summary-search-article-matched-data))
8362                    (cadr gnus-summary-search-article-matched-data))
8363                   (insert text)
8364                   (string-match (, regexp) text)))
8365            (if (, backward) (beginning-of-line) (end-of-line))
8366          (goto-char (if (, backward) (point-max) (point-min))))))
8367
8368   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8369     "Place point where X-Face image is displayed."
8370     (if (featurep 'xemacs)
8371         (` (let ((end (if (search-forward "\n\n" nil t)
8372                           (goto-char (1- (point)))
8373                         (point-min)))
8374                  extent)
8375              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8376              (unless (and (re-search-forward "^From:" end t)
8377                           (setq extent (extent-at (point)))
8378                           (extent-begin-glyph extent))
8379                (goto-char (, opoint)))))
8380       (` (let ((end (if (search-forward "\n\n" nil t)
8381                         (goto-char (1- (point)))
8382                       (point-min)))
8383                (start (or (search-backward "\n\n" nil t) (point-min))))
8384            (goto-char
8385             (or (text-property-any start end 'x-face-image t);; x-face-e21
8386                 (text-property-any start end 'x-face-mule-bitmap-image t)
8387                 (, opoint)))))))
8388
8389   (defmacro gnus-summary-search-article-highlight-matched-text
8390     (backward treated x-face)
8391     "Highlight matched text in the function `gnus-summary-search-article'."
8392     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8393              (end (set-marker (make-marker) (match-end 0)))
8394              (inhibit-read-only t)
8395              buffer-read-only)
8396          (unless treated
8397            (let ((,@
8398                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8399                     (mapcar
8400                      (lambda (item) (setq items (delq item items)))
8401                      '(gnus-treat-buttonize
8402                        gnus-treat-fill-article
8403                        gnus-treat-fill-long-lines
8404                        gnus-treat-emphasize
8405                        gnus-treat-highlight-headers
8406                        gnus-treat-highlight-citation
8407                        gnus-treat-highlight-signature
8408                        gnus-treat-overstrike
8409                        gnus-treat-display-x-face
8410                        gnus-treat-buttonize-head
8411                        gnus-treat-decode-article-as-default-mime-charset))
8412                     (static-if (featurep 'xemacs)
8413                         items
8414                       (cons '(x-face-mule-delete-x-face-field
8415                               (quote never))
8416                             items))))
8417                  (gnus-treat-display-x-face
8418                   (when (, x-face) gnus-treat-display-x-face)))
8419              (gnus-article-prepare-mime-display)))
8420          (goto-char (if (, backward) start end))
8421          (when (, x-face)
8422            (gnus-summary-search-article-highlight-goto-x-face (point)))
8423          (setq gnus-summary-search-article-matched-data
8424                (list start end (buffer-substring start end)))
8425          (unless (eq start end);; matched text has been deleted. :-<
8426            (put-text-property start end 'face
8427                               (or (find-face 'isearch)
8428                                   'secondary-selection))))))
8429   )
8430
8431 (defun gnus-summary-search-article (regexp &optional backward)
8432   "Search for an article containing REGEXP.
8433 Optional argument BACKWARD means do search for backward.
8434 `gnus-select-article-hook' is not called during the search."
8435   ;; We have to require this here to make sure that the following
8436   ;; dynamic binding isn't shadowed by autoloading.
8437   (require 'gnus-async)
8438   (require 'gnus-art)
8439   (let ((gnus-select-article-hook nil)  ;Disable hook.
8440         (gnus-article-prepare-hook nil)
8441         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8442         (gnus-use-article-prefetch nil)
8443         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8444         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8445         (gnus-visual nil)
8446         (gnus-keep-backlog nil)
8447         (gnus-break-pages nil)
8448         (gnus-summary-display-arrow nil)
8449         (gnus-updated-mode-lines nil)
8450         (gnus-auto-center-summary nil)
8451         (sum (current-buffer))
8452         (found nil)
8453         point treated)
8454     (gnus-save-hidden-threads
8455       (static-if (featurep 'xemacs)
8456           (let ((gnus-inhibit-treatment t))
8457             (setq treated (eq 'old (gnus-summary-select-article)))
8458             (when (and treated
8459                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8460                                  (window-live-p (get-buffer-window
8461                                                  gnus-article-buffer t)))))
8462               (gnus-summary-select-article nil t)
8463               (setq treated nil)))
8464         (let ((gnus-inhibit-treatment t)
8465               (x-face-mule-delete-x-face-field 'never))
8466           (setq treated (eq 'old (gnus-summary-select-article)))
8467           (when (and treated
8468                      (not
8469                       (and (gnus-buffer-live-p gnus-article-buffer)
8470                            (window-live-p (get-buffer-window
8471                                            gnus-article-buffer t))
8472                            (or (not (string-match "^\\^X-Face:" regexp))
8473                                (with-current-buffer gnus-article-buffer
8474                                  gnus-summary-search-article-matched-data)))))
8475             (gnus-summary-select-article nil t)
8476             (setq treated nil))))
8477       (set-buffer gnus-article-buffer)
8478       (widen)
8479       (if treated
8480           (progn
8481             (gnus-article-show-all-headers)
8482             (gnus-summary-search-article-position-point regexp backward))
8483         (goto-char (if backward (point-max) (point-min))))
8484       (while (not found)
8485         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8486         (if (if backward
8487                 (re-search-backward regexp nil t)
8488               (re-search-forward regexp nil t))
8489             ;; We found the regexp.
8490             (progn
8491               (gnus-summary-search-article-highlight-matched-text
8492                backward treated (string-match "^\\^X-Face:" regexp))
8493               (setq found 'found)
8494               (forward-line
8495                (/ (- 2 (window-height
8496                         (get-buffer-window gnus-article-buffer t)))
8497                   2))
8498               (set-window-start
8499                (get-buffer-window (current-buffer))
8500                (point))
8501               (set-buffer sum)
8502               (setq point (point)))
8503           ;; We didn't find it, so we go to the next article.
8504           (set-buffer sum)
8505           (setq found 'not)
8506           (while (eq found 'not)
8507             (if (not (if backward (gnus-summary-find-prev)
8508                        (gnus-summary-find-next)))
8509                 ;; No more articles.
8510                 (setq found t)
8511               ;; Select the next article and adjust point.
8512               (unless (gnus-summary-article-sparse-p
8513                        (gnus-summary-article-number))
8514                 (setq found nil)
8515                 (let ((gnus-inhibit-treatment t))
8516                   (gnus-summary-select-article))
8517                 (setq treated nil)
8518                 (set-buffer gnus-article-buffer)
8519                 (widen)
8520                 (goto-char (if backward (point-max) (point-min))))))))
8521       (gnus-message 7 ""))
8522     ;; Return whether we found the regexp.
8523     (when (eq found 'found)
8524       (goto-char point)
8525       (gnus-summary-show-thread)
8526       (gnus-summary-goto-subject gnus-current-article)
8527       (gnus-summary-position-point)
8528       t)))
8529
8530 (defun gnus-find-matching-articles (header regexp)
8531   "Return a list of all articles that match REGEXP on HEADER.
8532 This search includes all articles in the current group that Gnus has
8533 fetched headers for, whether they are displayed or not."
8534   (let ((articles nil)
8535         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8536         (case-fold-search t))
8537     (dolist (header gnus-newsgroup-headers)
8538       (when (string-match regexp (funcall func header))
8539         (push (mail-header-number header) articles)))
8540     (nreverse articles)))
8541
8542 (defun gnus-summary-find-matching (header regexp &optional backward unread
8543                                           not-case-fold not-matching)
8544   "Return a list of all articles that match REGEXP on HEADER.
8545 The search stars on the current article and goes forwards unless
8546 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8547 If UNREAD is non-nil, only unread articles will
8548 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8549 in the comparisons. If NOT-MATCHING, return a list of all articles that
8550 not match REGEXP on HEADER."
8551   (let ((case-fold-search (not not-case-fold))
8552         articles d func)
8553     (if (consp header)
8554         (if (eq (car header) 'extra)
8555             (setq func
8556                   `(lambda (h)
8557                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8558                          "")))
8559           (error "%s is an invalid header" header))
8560       (unless (fboundp (intern (concat "mail-header-" header)))
8561         (error "%s is not a valid header" header))
8562       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8563     (dolist (d (if (eq backward 'all)
8564                    gnus-newsgroup-data
8565                  (gnus-data-find-list
8566                   (gnus-summary-article-number)
8567                   (gnus-data-list backward))))
8568       (when (and (or (not unread)       ; We want all articles...
8569                      (gnus-data-unread-p d)) ; Or just unreads.
8570                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8571                  (if not-matching
8572                      (not (string-match
8573                            regexp
8574                            (funcall func (gnus-data-header d))))
8575                    (string-match regexp
8576                                  (funcall func (gnus-data-header d)))))
8577         (push (gnus-data-number d) articles))) ; Success!
8578     (nreverse articles)))
8579
8580 (defun gnus-summary-execute-command (header regexp command &optional backward)
8581   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8582 If HEADER is an empty string (or nil), the match is done on the entire
8583 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8584   (interactive
8585    (list (let ((completion-ignore-case t))
8586            (completing-read
8587             "Header name: "
8588             (mapcar (lambda (header) (list (format "%s" header)))
8589                     (append
8590                      '("Number" "Subject" "From" "Lines" "Date"
8591                        "Message-ID" "Xref" "References" "Body")
8592                      gnus-extra-headers))
8593             nil 'require-match))
8594          (read-string "Regexp: ")
8595          (read-key-sequence "Command: ")
8596          current-prefix-arg))
8597   (when (equal header "Body")
8598     (setq header ""))
8599   ;; Hidden thread subtrees must be searched as well.
8600   (gnus-summary-show-all-threads)
8601   ;; We don't want to change current point nor window configuration.
8602   (save-excursion
8603     (save-window-excursion
8604       (let (gnus-visual
8605             gnus-treat-strip-trailing-blank-lines
8606             gnus-treat-strip-leading-blank-lines
8607             gnus-treat-strip-multiple-blank-lines
8608             gnus-treat-hide-boring-headers
8609             gnus-treat-fold-newsgroups
8610             gnus-article-prepare-hook)
8611         (gnus-message 6 "Executing %s..." (key-description command))
8612         ;; We'd like to execute COMMAND interactively so as to give arguments.
8613         (gnus-execute header regexp
8614                       `(call-interactively ',(key-binding command))
8615                       backward)
8616         (gnus-message 6 "Executing %s...done" (key-description command))))))
8617
8618 (defun gnus-summary-beginning-of-article ()
8619   "Scroll the article back to the beginning."
8620   (interactive)
8621   (gnus-summary-select-article)
8622   (gnus-configure-windows 'article)
8623   (gnus-eval-in-buffer-window gnus-article-buffer
8624     (widen)
8625     (goto-char (point-min))
8626     (when gnus-break-pages
8627       (gnus-narrow-to-page))))
8628
8629 (defun gnus-summary-end-of-article ()
8630   "Scroll to the end of the article."
8631   (interactive)
8632   (gnus-summary-select-article)
8633   (gnus-configure-windows 'article)
8634   (gnus-eval-in-buffer-window gnus-article-buffer
8635     (widen)
8636     (goto-char (point-max))
8637     (recenter -3)
8638     (when gnus-break-pages
8639       (when (re-search-backward page-delimiter nil t)
8640         (narrow-to-region (match-end 0) (point-max)))
8641       (gnus-narrow-to-page))))
8642
8643 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8644   "Truncate to LEN and quote all \"(\"'s in STRING."
8645   (gnus-replace-in-string (if (and len (> (length string) len))
8646                               (substring string 0 len)
8647                             string)
8648                           "[()]" "\\\\\\&"))
8649
8650 (defun gnus-summary-print-article (&optional filename n)
8651   "Generate and print a PostScript image of the N next (mail) articles.
8652
8653 If N is negative, print the N previous articles.  If N is nil and articles
8654 have been marked with the process mark, print these instead.
8655
8656 If the optional first argument FILENAME is nil, send the image to the
8657 printer.  If FILENAME is a string, save the PostScript image in a file with
8658 that name.  If FILENAME is a number, prompt the user for the name of the file
8659 to save in."
8660   (interactive (list (ps-print-preprint current-prefix-arg)))
8661   (dolist (article (gnus-summary-work-articles n))
8662     (gnus-summary-select-article nil nil 'pseudo article)
8663     (gnus-eval-in-buffer-window gnus-article-buffer
8664       (gnus-print-buffer))
8665     (gnus-summary-remove-process-mark article))
8666   (ps-despool filename))
8667
8668 (defun gnus-print-buffer ()
8669   (let ((buffer (generate-new-buffer " *print*")))
8670     (unwind-protect
8671         (progn
8672           (copy-to-buffer buffer (point-min) (point-max))
8673           (set-buffer buffer)
8674           (gnus-remove-text-with-property 'gnus-decoration)
8675           (when (gnus-visual-p 'article-highlight 'highlight)
8676             ;; Copy-to-buffer doesn't copy overlay.  So redo
8677             ;; highlight.
8678             (let ((gnus-article-buffer buffer))
8679               (gnus-article-highlight-citation t)
8680               (gnus-article-highlight-signature)
8681               (gnus-article-emphasize)
8682               (gnus-article-delete-invisible-text)))
8683           (let ((ps-left-header
8684                  (list
8685                   (concat "("
8686                           (gnus-summary-print-truncate-and-quote
8687                            (mail-header-subject gnus-current-headers)
8688                            66) ")")
8689                   (concat "("
8690                           (gnus-summary-print-truncate-and-quote
8691                            (mail-header-from gnus-current-headers)
8692                            45) ")")))
8693                 (ps-right-header
8694                  (list
8695                   "/pagenumberstring load"
8696                   (concat "("
8697                           (mail-header-date gnus-current-headers) ")"))))
8698             (gnus-run-hooks 'gnus-ps-print-hook)
8699             (save-excursion
8700               (if window-system
8701                   (ps-spool-buffer-with-faces)
8702                 (ps-spool-buffer)))))
8703       (kill-buffer buffer))))
8704
8705 (defun gnus-summary-show-article (&optional arg)
8706   "Force redisplaying of the current article.
8707 If ARG (the prefix) is a number, show the article with the charset
8708 defined in `gnus-summary-show-article-charset-alist', or the charset
8709 input.
8710 If ARG (the prefix) is non-nil and not a number, show the raw article
8711 without any article massaging functions being run.  Normally, the key
8712 strokes are `C-u g'."
8713   (interactive "P")
8714   (cond
8715    ((numberp arg)
8716     (gnus-summary-show-article t)
8717     (let* ((gnus-newsgroup-charset
8718             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8719                 (mm-read-coding-system
8720                  "View as charset: " ;; actually it is coding system.
8721                  (save-excursion
8722                    (set-buffer gnus-article-buffer)
8723                    (mm-detect-coding-region (point) (point-max))))))
8724            (default-mime-charset gnus-newsgroup-charset)
8725            (gnus-newsgroup-ignored-charsets 'gnus-all))
8726       (gnus-summary-select-article nil 'force)
8727       (let ((deps gnus-newsgroup-dependencies)
8728             head header lines)
8729         (save-excursion
8730           (set-buffer gnus-original-article-buffer)
8731           (save-restriction
8732             (message-narrow-to-head)
8733             (setq head (buffer-string))
8734             (goto-char (point-min))
8735             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8736               (goto-char (point-max))
8737               (widen)
8738               (setq lines (1- (count-lines (point) (point-max))))))
8739           (with-temp-buffer
8740             (insert (format "211 %d Article retrieved.\n"
8741                             (cdr gnus-article-current)))
8742             (insert head)
8743             (if lines (insert (format "Lines: %d\n" lines)))
8744             (insert ".\n")
8745             (let ((nntp-server-buffer (current-buffer)))
8746               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8747         (gnus-data-set-header
8748          (gnus-data-find (cdr gnus-article-current))
8749          header)
8750         (gnus-summary-update-article-line
8751          (cdr gnus-article-current) header)
8752         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8753           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8754    ((not arg)
8755     ;; Select the article the normal way.
8756     (gnus-summary-select-article nil 'force))
8757    (t
8758     ;; We have to require this here to make sure that the following
8759     ;; dynamic binding isn't shadowed by autoloading.
8760     (require 'gnus-async)
8761     (require 'gnus-art)
8762     ;; Bind the article treatment functions to nil.
8763     (let ((gnus-have-all-headers t)
8764           gnus-article-prepare-hook
8765           gnus-article-decode-hook
8766           gnus-break-pages
8767           gnus-show-mime
8768           (gnus-inhibit-treatment t))
8769       (gnus-summary-select-article nil 'force))))
8770   (gnus-summary-goto-subject gnus-current-article)
8771   (gnus-summary-position-point))
8772
8773 (defun gnus-summary-show-raw-article ()
8774   "Show the raw article without any article massaging functions being run."
8775   (interactive)
8776   (gnus-summary-show-article t))
8777
8778 (defun gnus-summary-verbose-headers (&optional arg)
8779   "Toggle permanent full header display.
8780 If ARG is a positive number, turn header display on.
8781 If ARG is a negative number, turn header display off."
8782   (interactive "P")
8783   (setq gnus-show-all-headers
8784         (cond ((or (not (numberp arg))
8785                    (zerop arg))
8786                (not gnus-show-all-headers))
8787               ((natnump arg)
8788                t)))
8789   (gnus-summary-show-article))
8790
8791 (defun gnus-summary-toggle-header (&optional arg)
8792   "Show the headers if they are hidden, or hide them if they are shown.
8793 If ARG is a positive number, show the entire header.
8794 If ARG is a negative number, hide the unwanted header lines."
8795   (interactive "P")
8796   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8797                      (get-buffer-window gnus-article-buffer t))))
8798     (with-current-buffer gnus-article-buffer
8799       (widen)
8800       (article-narrow-to-head)
8801       (let* ((buffer-read-only nil)
8802              (inhibit-point-motion-hooks t)
8803              (hidden (if (numberp arg)
8804                          (>= arg 0)
8805                        (gnus-article-hidden-text-p 'headers)))
8806              s e)
8807         (delete-region (point-min) (point-max))
8808         (with-current-buffer gnus-original-article-buffer
8809           (goto-char (setq s (point-min)))
8810           (setq e (if (search-forward "\n\n" nil t)
8811                       (1- (point))
8812                     (point-max))))
8813         (insert-buffer-substring gnus-original-article-buffer s e)
8814         ;; In T-gnus, gnus-article-decode-hook doesn't contain
8815         ;; article-decode-encoded-words by default.
8816         (article-decode-encoded-words)
8817         (run-hooks 'gnus-article-decode-hook)
8818         (if hidden
8819             (let ((gnus-treat-hide-headers nil)
8820                   (gnus-treat-hide-boring-headers nil))
8821               (gnus-delete-wash-type 'headers)
8822               (gnus-treat-article 'head))
8823           (gnus-treat-article 'head))
8824         (widen)
8825         (if window
8826             (set-window-start window (goto-char (point-min))))
8827         (if gnus-break-pages
8828             (gnus-narrow-to-page)
8829           (when (gnus-visual-p 'page-marker)
8830             (let ((buffer-read-only nil))
8831               (gnus-remove-text-with-property 'gnus-prev)
8832               (gnus-remove-text-with-property 'gnus-next))))
8833         (gnus-set-mode-line 'article)))))
8834
8835 (defun gnus-summary-show-all-headers ()
8836   "Make all header lines visible."
8837   (interactive)
8838   (gnus-summary-toggle-header 1))
8839
8840 (defun gnus-summary-toggle-mime (&optional arg)
8841   "Toggle MIME processing.
8842 If ARG is a positive number, turn MIME processing on."
8843   (interactive "P")
8844   (setq gnus-show-mime
8845         (if (null arg)
8846             (not gnus-show-mime)
8847           (> (prefix-numeric-value arg) 0)))
8848   (gnus-summary-select-article t 'force))
8849
8850 (defun gnus-summary-caesar-message (&optional arg)
8851   "Caesar rotate the current article by 13.
8852 The numerical prefix specifies how many places to rotate each letter
8853 forward."
8854   (interactive "P")
8855   (gnus-summary-select-article)
8856   (let ((mail-header-separator ""))
8857     (gnus-eval-in-buffer-window gnus-article-buffer
8858       (save-restriction
8859         (widen)
8860         (let ((start (window-start))
8861               buffer-read-only)
8862           (message-caesar-buffer-body arg)
8863           (set-window-start (get-buffer-window (current-buffer)) start))))))
8864
8865 (autoload 'unmorse-region "morse"
8866   "Convert morse coded text in region to ordinary ASCII text."
8867   t)
8868
8869 (defun gnus-summary-morse-message (&optional arg)
8870   "Morse decode the current article."
8871   (interactive "P")
8872   (gnus-summary-select-article)
8873   (let ((mail-header-separator ""))
8874     (gnus-eval-in-buffer-window gnus-article-buffer
8875       (save-excursion
8876         (save-restriction
8877           (widen)
8878           (let ((pos (window-start))
8879                 buffer-read-only)
8880             (goto-char (point-min))
8881             (when (message-goto-body)
8882               (gnus-narrow-to-body))
8883             (goto-char (point-min))
8884             (while (re-search-forward "·" (point-max) t)
8885               (replace-match "."))
8886             (unmorse-region (point-min) (point-max))
8887             (widen)
8888             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8889
8890 (defun gnus-summary-stop-page-breaking ()
8891   "Stop page breaking in the current article."
8892   (interactive)
8893   (gnus-summary-select-article)
8894   (gnus-eval-in-buffer-window gnus-article-buffer
8895     (widen)
8896     (when (gnus-visual-p 'page-marker)
8897       (let ((buffer-read-only nil))
8898         (gnus-remove-text-with-property 'gnus-prev)
8899         (gnus-remove-text-with-property 'gnus-next))
8900       (setq gnus-page-broken nil))))
8901
8902 (defun gnus-summary-move-article (&optional n to-newsgroup
8903                                             select-method action)
8904   "Move the current article to a different newsgroup.
8905 If N is a positive number, move the N next articles.
8906 If N is a negative number, move the N previous articles.
8907 If N is nil and any articles have been marked with the process mark,
8908 move those articles instead.
8909 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8910 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8911 re-spool using this method.
8912
8913 When called interactively with TO-NEWSGROUP being nil, the value of
8914 the variable `gnus-move-split-methods' is used for finding a default
8915 for the target newsgroup.
8916
8917 For this function to work, both the current newsgroup and the
8918 newsgroup that you want to move to have to support the `request-move'
8919 and `request-accept' functions.
8920
8921 ACTION can be either `move' (the default), `crosspost' or `copy'."
8922   (interactive "P")
8923   (unless action
8924     (setq action 'move))
8925   ;; Check whether the source group supports the required functions.
8926   (cond ((and (eq action 'move)
8927               (not (gnus-check-backend-function
8928                     'request-move-article gnus-newsgroup-name)))
8929          (error "The current group does not support article moving"))
8930         ((and (eq action 'crosspost)
8931               (not (gnus-check-backend-function
8932                     'request-replace-article gnus-newsgroup-name)))
8933          (error "The current group does not support article editing")))
8934   (let ((articles (gnus-summary-work-articles n))
8935         (prefix (if (gnus-check-backend-function
8936                      'request-move-article gnus-newsgroup-name)
8937                     (gnus-group-real-prefix gnus-newsgroup-name)
8938                   ""))
8939         (names '((move "Move" "Moving")
8940                  (copy "Copy" "Copying")
8941                  (crosspost "Crosspost" "Crossposting")))
8942         (copy-buf (save-excursion
8943                     (nnheader-set-temp-buffer " *copy article*")))
8944         art-group to-method new-xref article to-groups)
8945     (unless (assq action names)
8946       (error "Unknown action %s" action))
8947     ;; Read the newsgroup name.
8948     (when (and (not to-newsgroup)
8949                (not select-method))
8950       (if (and gnus-move-split-methods
8951                (not
8952                 (and (memq gnus-current-article articles)
8953                      (gnus-buffer-live-p gnus-original-article-buffer))))
8954           ;; When `gnus-move-split-methods' is non-nil, we have to
8955           ;; select an article to give `gnus-read-move-group-name' an
8956           ;; opportunity to suggest an appropriate default.  However,
8957           ;; we needn't render or mark the article.
8958           (let ((gnus-display-mime-function nil)
8959                 (gnus-article-prepare-hook nil)
8960                 (gnus-mark-article-hook nil))
8961             (gnus-summary-select-article nil nil nil (car articles))))
8962       (setq to-newsgroup
8963             (gnus-read-move-group-name
8964              (cadr (assq action names))
8965              (symbol-value (intern (format "gnus-current-%s-group" action)))
8966              articles prefix))
8967       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8968     (setq to-method (or select-method
8969                         (gnus-server-to-method
8970                          (gnus-group-method to-newsgroup))))
8971     ;; Check the method we are to move this article to...
8972     (unless (gnus-check-backend-function
8973              'request-accept-article (car to-method))
8974       (error "%s does not support article copying" (car to-method)))
8975     (unless (gnus-check-server to-method)
8976       (error "Can't open server %s" (car to-method)))
8977     (gnus-message 6 "%s to %s: %s..."
8978                   (caddr (assq action names))
8979                   (or (car select-method) to-newsgroup) articles)
8980     (while articles
8981       (setq article (pop articles))
8982       (setq
8983        art-group
8984        (cond
8985         ;; Move the article.
8986         ((eq action 'move)
8987          ;; Remove this article from future suppression.
8988          (gnus-dup-unsuppress-article article)
8989          (gnus-request-move-article
8990           article                       ; Article to move
8991           gnus-newsgroup-name           ; From newsgroup
8992           (nth 1 (gnus-find-method-for-group
8993                   gnus-newsgroup-name)) ; Server
8994           (list 'gnus-request-accept-article
8995                 to-newsgroup (list 'quote select-method)
8996                 (not articles) t)       ; Accept form
8997           (not articles)))              ; Only save nov last time
8998         ;; Copy the article.
8999         ((eq action 'copy)
9000          (save-excursion
9001            (set-buffer copy-buf)
9002            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9003              (gnus-request-accept-article
9004               to-newsgroup select-method (not articles) t))))
9005         ;; Crosspost the article.
9006         ((eq action 'crosspost)
9007          (let ((xref (message-tokenize-header
9008                       (mail-header-xref (gnus-summary-article-header article))
9009                       " ")))
9010            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9011                                   ":" (number-to-string article)))
9012            (unless xref
9013              (setq xref (list (system-name))))
9014            (setq new-xref
9015                  (concat
9016                   (mapconcat 'identity
9017                              (delete "Xref:" (delete new-xref xref))
9018                              " ")
9019                   " " new-xref))
9020            (save-excursion
9021              (set-buffer copy-buf)
9022              ;; First put the article in the destination group.
9023              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9024              (when (consp (setq art-group
9025                                 (gnus-request-accept-article
9026                                  to-newsgroup select-method (not articles))))
9027                (setq new-xref (concat new-xref " " (car art-group)
9028                                       ":"
9029                                       (number-to-string (cdr art-group))))
9030                ;; Now we have the new Xrefs header, so we insert
9031                ;; it and replace the new article.
9032                (nnheader-replace-header "Xref" new-xref)
9033                (gnus-request-replace-article
9034                 (cdr art-group) to-newsgroup (current-buffer))
9035                art-group))))))
9036       (cond
9037        ((not art-group)
9038         (gnus-message 1 "Couldn't %s article %s: %s"
9039                       (cadr (assq action names)) article
9040                       (nnheader-get-report (car to-method))))
9041        ((eq art-group 'junk)
9042         (when (eq action 'move)
9043           (gnus-summary-mark-article article gnus-canceled-mark)
9044           (gnus-message 4 "Deleted article %s" article)
9045           ;; run the delete hook
9046           (run-hook-with-args 'gnus-summary-article-delete-hook
9047                               action
9048                               (gnus-data-header
9049                                (assoc article (gnus-data-list nil)))
9050                               gnus-newsgroup-name nil
9051                               select-method)))
9052        (t
9053         (let* ((pto-group (gnus-group-prefixed-name
9054                            (car art-group) to-method))
9055                (entry
9056                 (gnus-gethash pto-group gnus-newsrc-hashtb))
9057                (info (nth 2 entry))
9058                (to-group (gnus-info-group info))
9059                to-marks)
9060           ;; Update the group that has been moved to.
9061           (when (and info
9062                      (memq action '(move copy)))
9063             (unless (member to-group to-groups)
9064               (push to-group to-groups))
9065
9066             (unless (memq article gnus-newsgroup-unreads)
9067               (push 'read to-marks)
9068               (gnus-info-set-read
9069                info (gnus-add-to-range (gnus-info-read info)
9070                                        (list (cdr art-group)))))
9071
9072             ;; See whether the article is to be put in the cache.
9073             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9074                              gnus-article-mark-lists
9075                            (delete '(expirable . expire)
9076                                    (copy-sequence gnus-article-mark-lists))))
9077                   (to-article (cdr art-group)))
9078
9079               ;; Enter the article into the cache in the new group,
9080               ;; if that is required.
9081               (when gnus-use-cache
9082                 (gnus-cache-possibly-enter-article
9083                  to-group to-article
9084                  (let ((header (copy-sequence
9085                                 (gnus-summary-article-header article))))
9086                    (mail-header-set-number header to-article)
9087                    header)
9088                  (memq article gnus-newsgroup-marked)
9089                  (memq article gnus-newsgroup-dormant)
9090                  (memq article gnus-newsgroup-unreads)))
9091
9092               (when gnus-preserve-marks
9093                 ;; Copy any marks over to the new group.
9094                 (when (and (equal to-group gnus-newsgroup-name)
9095                            (not (memq article gnus-newsgroup-unreads)))
9096                   ;; Mark this article as read in this group.
9097                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9098                   (setcdr (gnus-active to-group) to-article)
9099                   (setcdr gnus-newsgroup-active to-article))
9100
9101                 (while marks
9102                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9103                     (when (memq article (symbol-value
9104                                          (intern (format "gnus-newsgroup-%s"
9105                                                          (caar marks)))))
9106                       (push (cdar marks) to-marks)
9107                       ;; If the other group is the same as this group,
9108                       ;; then we have to add the mark to the list.
9109                       (when (equal to-group gnus-newsgroup-name)
9110                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9111                              (cons to-article
9112                                    (symbol-value
9113                                     (intern (format "gnus-newsgroup-%s"
9114                                                     (caar marks)))))))
9115                       ;; Copy the marks to other group.
9116                       (gnus-add-marked-articles
9117                        to-group (cdar marks) (list to-article) info)))
9118                   (setq marks (cdr marks)))
9119
9120                 (gnus-request-set-mark
9121                  to-group (list (list (list to-article) 'add to-marks))))
9122
9123               (gnus-dribble-enter
9124                (concat "(gnus-group-set-info '"
9125                        (gnus-prin1-to-string (gnus-get-info to-group))
9126                        ")"))))
9127
9128           ;; Update the Xref header in this article to point to
9129           ;; the new crossposted article we have just created.
9130           (when (eq action 'crosspost)
9131             (save-excursion
9132               (set-buffer copy-buf)
9133               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9134               (nnheader-replace-header "Xref" new-xref)
9135               (gnus-request-replace-article
9136                article gnus-newsgroup-name (current-buffer))))
9137
9138           ;; run the move/copy/crosspost/respool hook
9139           (run-hook-with-args 'gnus-summary-article-move-hook
9140                               action
9141                               (gnus-data-header
9142                                (assoc article (gnus-data-list nil)))
9143                               gnus-newsgroup-name
9144                               to-newsgroup
9145                               select-method))
9146
9147         ;;;!!!Why is this necessary?
9148         (set-buffer gnus-summary-buffer)
9149         
9150         (gnus-summary-goto-subject article)
9151         (when (eq action 'move)
9152           (gnus-summary-mark-article article gnus-canceled-mark))))
9153       (gnus-summary-remove-process-mark article))
9154     ;; Re-activate all groups that have been moved to.
9155     (save-excursion
9156       (set-buffer gnus-group-buffer)
9157       (let ((gnus-group-marked to-groups))
9158         (gnus-group-get-new-news-this-group nil t)))
9159
9160     (gnus-kill-buffer copy-buf)
9161     (gnus-summary-position-point)
9162     (gnus-set-mode-line 'summary)))
9163
9164 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9165   "Move the current article to a different newsgroup.
9166 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9167 When called interactively, if TO-NEWSGROUP is nil, use the value of
9168 the variable `gnus-move-split-methods' for finding a default target
9169 newsgroup.
9170 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9171 re-spool using this method."
9172   (interactive "P")
9173   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9174
9175 (defun gnus-summary-crosspost-article (&optional n)
9176   "Crosspost the current article to some other group."
9177   (interactive "P")
9178   (gnus-summary-move-article n nil nil 'crosspost))
9179
9180 (defcustom gnus-summary-respool-default-method nil
9181   "Default method type for respooling an article.
9182 If nil, use to the current newsgroup method."
9183   :type 'symbol
9184   :group 'gnus-summary-mail)
9185
9186 (defcustom gnus-summary-display-while-building nil
9187   "If non-nil, show and update the summary buffer as it's being built.
9188 If the value is t, update the buffer after every line is inserted.  If
9189 the value is an integer (N), update the display every N lines."
9190   :group 'gnus-thread
9191   :type '(choice (const :tag "off" nil)
9192                  number
9193                  (const :tag "frequently" t)))
9194
9195 (defun gnus-summary-respool-article (&optional n method)
9196   "Respool the current article.
9197 The article will be squeezed through the mail spooling process again,
9198 which means that it will be put in some mail newsgroup or other
9199 depending on `nnmail-split-methods'.
9200 If N is a positive number, respool the N next articles.
9201 If N is a negative number, respool the N previous articles.
9202 If N is nil and any articles have been marked with the process mark,
9203 respool those articles instead.
9204
9205 Respooling can be done both from mail groups and \"real\" newsgroups.
9206 In the former case, the articles in question will be moved from the
9207 current group into whatever groups they are destined to.  In the
9208 latter case, they will be copied into the relevant groups."
9209   (interactive
9210    (list current-prefix-arg
9211          (let* ((methods (gnus-methods-using 'respool))
9212                 (methname
9213                  (symbol-name (or gnus-summary-respool-default-method
9214                                   (car (gnus-find-method-for-group
9215                                         gnus-newsgroup-name)))))
9216                 (method
9217                  (gnus-completing-read-with-default
9218                   methname "What backend do you want to use when respooling?"
9219                   methods nil t nil 'gnus-mail-method-history))
9220                 ms)
9221            (cond
9222             ((zerop (length (setq ms (gnus-servers-using-backend
9223                                       (intern method)))))
9224              (list (intern method) ""))
9225             ((= 1 (length ms))
9226              (car ms))
9227             (t
9228              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9229                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9230                            ms-alist))))))))
9231   (unless method
9232     (error "No method given for respooling"))
9233   (if (assoc (symbol-name
9234               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9235              (gnus-methods-using 'respool))
9236       (gnus-summary-move-article n nil method)
9237     (gnus-summary-copy-article n nil method)))
9238
9239 (defun gnus-summary-import-article (file &optional edit)
9240   "Import an arbitrary file into a mail newsgroup."
9241   (interactive "fImport file: \nP")
9242   (let ((group gnus-newsgroup-name)
9243         (now (current-time))
9244         atts lines group-art)
9245     (unless (gnus-check-backend-function 'request-accept-article group)
9246       (error "%s does not support article importing" group))
9247     (or (file-readable-p file)
9248         (not (file-regular-p file))
9249         (error "Can't read %s" file))
9250     (save-excursion
9251       (set-buffer (gnus-get-buffer-create " *import file*"))
9252       (erase-buffer)
9253       (nnheader-insert-file-contents file)
9254       (goto-char (point-min))
9255       (if (nnheader-article-p)
9256           (save-restriction
9257             (goto-char (point-min))
9258             (search-forward "\n\n" nil t)
9259             (narrow-to-region (point-min) (1- (point)))
9260             (goto-char (point-min))
9261             (unless (re-search-forward "^date:" nil t)
9262               (goto-char (point-max))
9263               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9264         ;; This doesn't look like an article, so we fudge some headers.
9265         (setq atts (file-attributes file)
9266               lines (count-lines (point-min) (point-max)))
9267         (insert "From: " (read-string "From: ") "\n"
9268                 "Subject: " (read-string "Subject: ") "\n"
9269                 "Date: " (message-make-date (nth 5 atts)) "\n"
9270                 "Message-ID: " (message-make-message-id) "\n"
9271                 "Lines: " (int-to-string lines) "\n"
9272                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9273       (setq group-art (gnus-request-accept-article group nil t))
9274       (kill-buffer (current-buffer)))
9275     (setq gnus-newsgroup-active (gnus-activate-group group))
9276     (forward-line 1)
9277     (gnus-summary-goto-article (cdr group-art) nil t)
9278     (when edit
9279       (gnus-summary-edit-article))))
9280
9281 (defun gnus-summary-create-article ()
9282   "Create an article in a mail newsgroup."
9283   (interactive)
9284   (let ((group gnus-newsgroup-name)
9285         (now (current-time))
9286         group-art)
9287     (unless (gnus-check-backend-function 'request-accept-article group)
9288       (error "%s does not support article importing" group))
9289     (save-excursion
9290       (set-buffer (gnus-get-buffer-create " *import file*"))
9291       (erase-buffer)
9292       (goto-char (point-min))
9293       ;; This doesn't look like an article, so we fudge some headers.
9294       (insert "From: " (read-string "From: ") "\n"
9295               "Subject: " (read-string "Subject: ") "\n"
9296               "Date: " (message-make-date now) "\n"
9297               "Message-ID: " (message-make-message-id) "\n")
9298       (setq group-art (gnus-request-accept-article group nil t))
9299       (kill-buffer (current-buffer)))
9300     (setq gnus-newsgroup-active (gnus-activate-group group))
9301     (forward-line 1)
9302     (gnus-summary-goto-article (cdr group-art) nil t)
9303     (gnus-summary-edit-article)))
9304
9305 (defun gnus-summary-article-posted-p ()
9306   "Say whether the current (mail) article is available from news as well.
9307 This will be the case if the article has both been mailed and posted."
9308   (interactive)
9309   (let ((id (mail-header-references (gnus-summary-article-header)))
9310         (gnus-override-method (car (gnus-refer-article-methods))))
9311     (if (gnus-request-head id "")
9312         (gnus-message 2 "The current message was found on %s"
9313                       gnus-override-method)
9314       (gnus-message 2 "The current message couldn't be found on %s"
9315                     gnus-override-method)
9316       nil)))
9317
9318 (defun gnus-summary-expire-articles (&optional now)
9319   "Expire all articles that are marked as expirable in the current group."
9320   (interactive)
9321   (when (and (not gnus-group-is-exiting-without-update-p)
9322              (gnus-check-backend-function
9323               'request-expire-articles gnus-newsgroup-name))
9324     ;; This backend supports expiry.
9325     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9326            (expirable (if total
9327                           (progn
9328                             ;; We need to update the info for
9329                             ;; this group for `gnus-list-of-read-articles'
9330                             ;; to give us the right answer.
9331                             (gnus-run-hooks 'gnus-exit-group-hook)
9332                             (gnus-summary-update-info)
9333                             (gnus-list-of-read-articles gnus-newsgroup-name))
9334                         (setq gnus-newsgroup-expirable
9335                               (sort gnus-newsgroup-expirable '<))))
9336            (expiry-wait (if now 'immediate
9337                           (gnus-group-find-parameter
9338                            gnus-newsgroup-name 'expiry-wait)))
9339            (nnmail-expiry-target
9340             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9341                 nnmail-expiry-target))
9342            es)
9343       (when expirable
9344         ;; There are expirable articles in this group, so we run them
9345         ;; through the expiry process.
9346         (gnus-message 6 "Expiring articles...")
9347         (unless (gnus-check-group gnus-newsgroup-name)
9348           (error "Can't open server for %s" gnus-newsgroup-name))
9349         ;; The list of articles that weren't expired is returned.
9350         (save-excursion
9351           (if expiry-wait
9352               (let ((nnmail-expiry-wait-function nil)
9353                     (nnmail-expiry-wait expiry-wait))
9354                 (setq es (gnus-request-expire-articles
9355                           expirable gnus-newsgroup-name)))
9356             (setq es (gnus-request-expire-articles
9357                       expirable gnus-newsgroup-name)))
9358           (unless total
9359             (setq gnus-newsgroup-expirable es))
9360           ;; We go through the old list of expirable, and mark all
9361           ;; really expired articles as nonexistent.
9362           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9363             (let ((gnus-use-cache nil))
9364               (dolist (article expirable)
9365                 (when (and (not (memq article es))
9366                            (gnus-data-find article))
9367                   (gnus-summary-mark-article article gnus-canceled-mark)
9368                   (run-hook-with-args 'gnus-summary-article-expire-hook
9369                                       'delete
9370                                       (gnus-data-header
9371                                        (assoc article (gnus-data-list nil)))
9372                                       gnus-newsgroup-name
9373                                       nil
9374                                       nil))))))
9375         (gnus-message 6 "Expiring articles...done")))))
9376
9377 (defun gnus-summary-expire-articles-now ()
9378   "Expunge all expirable articles in the current group.
9379 This means that *all* articles that are marked as expirable will be
9380 deleted forever, right now."
9381   (interactive)
9382   (or gnus-expert-user
9383       (gnus-yes-or-no-p
9384        "Are you really, really, really sure you want to delete all these messages? ")
9385       (error "Phew!"))
9386   (gnus-summary-expire-articles t))
9387
9388 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9389 (defun gnus-summary-delete-article (&optional n)
9390   "Delete the N next (mail) articles.
9391 This command actually deletes articles.  This is not a marking
9392 command.  The article will disappear forever from your life, never to
9393 return.
9394
9395 If N is negative, delete backwards.
9396 If N is nil and articles have been marked with the process mark,
9397 delete these instead.
9398
9399 If `gnus-novice-user' is non-nil you will be asked for
9400 confirmation before the articles are deleted."
9401   (interactive "P")
9402   (unless (gnus-check-backend-function 'request-expire-articles
9403                                        gnus-newsgroup-name)
9404     (error "The current newsgroup does not support article deletion"))
9405   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9406     (error "Couldn't open server"))
9407   ;; Compute the list of articles to delete.
9408   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9409         (nnmail-expiry-target 'delete)
9410         not-deleted)
9411     (if (and gnus-novice-user
9412              (not (gnus-yes-or-no-p
9413                    (format "Do you really want to delete %s forever? "
9414                            (if (> (length articles) 1)
9415                                (format "these %s articles" (length articles))
9416                              "this article")))))
9417         ()
9418       ;; Delete the articles.
9419       (setq not-deleted (gnus-request-expire-articles
9420                          articles gnus-newsgroup-name 'force))
9421       (while articles
9422         (gnus-summary-remove-process-mark (car articles))
9423         ;; The backend might not have been able to delete the article
9424         ;; after all.
9425         (unless (memq (car articles) not-deleted)
9426           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9427         (let* ((article (car articles))
9428                (id (mail-header-id (gnus-data-header
9429                                     (assoc article (gnus-data-list nil))))))
9430           (run-hook-with-args 'gnus-summary-article-delete-hook
9431                               'delete id gnus-newsgroup-name nil
9432                               nil))
9433         (setq articles (cdr articles)))
9434       (when not-deleted
9435         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9436     (gnus-summary-position-point)
9437     (gnus-set-mode-line 'summary)
9438     not-deleted))
9439
9440 (defun gnus-summary-edit-article (&optional force)
9441   "Edit the current article.
9442 This will have permanent effect only in mail groups.
9443 If FORCE is non-nil, allow editing of articles even in read-only
9444 groups."
9445   (interactive "P")
9446   (save-excursion
9447     (set-buffer gnus-summary-buffer)
9448     (let ((mail-parse-charset gnus-newsgroup-charset)
9449           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9450       (gnus-set-global-variables)
9451       (when (and (not force)
9452                  (gnus-group-read-only-p))
9453         (error "The current newsgroup does not support article editing"))
9454       (gnus-summary-show-article t)
9455       (gnus-article-edit-article
9456        'ignore
9457        `(lambda (no-highlight)
9458           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9459                 (message-options message-options)
9460                 (message-options-set-recipient)
9461                 (mail-parse-ignored-charsets
9462                  ',gnus-newsgroup-ignored-charsets))
9463             (gnus-summary-edit-article-done
9464              ,(or (mail-header-references gnus-current-headers) "")
9465              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9466
9467 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9468
9469 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9470                                                  no-highlight)
9471   "Make edits to the current article permanent."
9472   (interactive)
9473   (save-excursion
9474     ;; The buffer restriction contains the entire article if it exists.
9475     (when (article-goto-body)
9476       (let ((lines (count-lines (point) (point-max)))
9477             (length (- (point-max) (point)))
9478             (case-fold-search t)
9479             (body (copy-marker (point))))
9480         (goto-char (point-min))
9481         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9482           (delete-region (match-beginning 1) (match-end 1))
9483           (insert (number-to-string length)))
9484         (goto-char (point-min))
9485         (when (re-search-forward
9486                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9487           (delete-region (match-beginning 1) (match-end 1))
9488           (insert (number-to-string length)))
9489         (goto-char (point-min))
9490         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9491           (delete-region (match-beginning 1) (match-end 1))
9492           (insert (number-to-string lines))))))
9493   ;; Replace the article.
9494   (let ((buf (current-buffer)))
9495     (with-temp-buffer
9496       (insert-buffer-substring buf)
9497
9498       (if (and (not read-only)
9499                (not (gnus-request-replace-article
9500                      (cdr gnus-article-current) (car gnus-article-current)
9501                      (current-buffer) t)))
9502           (error "Couldn't replace article")
9503         ;; Update the summary buffer.
9504         (if (and references
9505                  (equal (message-tokenize-header references " ")
9506                         (message-tokenize-header
9507                          (or (message-fetch-field "references") "") " ")))
9508             ;; We only have to update this line.
9509             (save-excursion
9510               (save-restriction
9511                 (message-narrow-to-head)
9512                 (let ((head (buffer-string))
9513                       header)
9514                   (with-temp-buffer
9515                     (insert (format "211 %d Article retrieved.\n"
9516                                     (cdr gnus-article-current)))
9517                     (insert head)
9518                     (insert ".\n")
9519                     (let ((nntp-server-buffer (current-buffer)))
9520                       (setq header (car (gnus-get-newsgroup-headers
9521                                          nil t))))
9522                     (save-excursion
9523                       (set-buffer gnus-summary-buffer)
9524                       (gnus-data-set-header
9525                        (gnus-data-find (cdr gnus-article-current))
9526                        header)
9527                       (gnus-summary-update-article-line
9528                        (cdr gnus-article-current) header)
9529                       (if (gnus-summary-goto-subject
9530                            (cdr gnus-article-current) nil t)
9531                           (gnus-summary-update-secondary-mark
9532                            (cdr gnus-article-current))))))))
9533           ;; Update threads.
9534           (set-buffer (or buffer gnus-summary-buffer))
9535           (gnus-summary-update-article (cdr gnus-article-current))
9536           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9537               (gnus-summary-update-secondary-mark
9538                (cdr gnus-article-current))))
9539         ;; Prettify the article buffer again.
9540         (unless no-highlight
9541           (save-excursion
9542             (set-buffer gnus-article-buffer)
9543             ;;;!!! Fix this -- article should be rehighlighted.
9544             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9545             (set-buffer gnus-original-article-buffer)
9546             (gnus-request-article
9547              (cdr gnus-article-current)
9548              (car gnus-article-current) (current-buffer))))
9549         ;; Prettify the summary buffer line.
9550         (when (gnus-visual-p 'summary-highlight 'highlight)
9551           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9552
9553 (defun gnus-summary-edit-wash (key)
9554   "Perform editing command KEY in the article buffer."
9555   (interactive
9556    (list
9557     (progn
9558       (message "%s" (concat (this-command-keys) "- "))
9559       (read-char))))
9560   (message "")
9561   (gnus-summary-edit-article)
9562   (execute-kbd-macro (concat (this-command-keys) key))
9563   (gnus-article-edit-done))
9564
9565 ;;; Respooling
9566
9567 (defun gnus-summary-respool-query (&optional silent trace)
9568   "Query where the respool algorithm would put this article."
9569   (interactive)
9570   (let (gnus-mark-article-hook)
9571     (gnus-summary-select-article)
9572     (save-excursion
9573       (set-buffer gnus-original-article-buffer)
9574       (let ((groups (nnmail-article-group 'identity trace)))
9575         (unless silent
9576           (if groups
9577               (message "This message would go to %s"
9578                        (mapconcat 'car groups ", "))
9579             (message "This message would go to no groups"))
9580           groups)))))
9581
9582 (defun gnus-summary-respool-trace ()
9583   "Trace where the respool algorithm would put this article.
9584 Display a buffer showing all fancy splitting patterns which matched."
9585   (interactive)
9586   (gnus-summary-respool-query nil t))
9587
9588 ;; Summary marking commands.
9589
9590 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9591   "Mark articles which has the same subject as read, and then select the next.
9592 If UNMARK is positive, remove any kind of mark.
9593 If UNMARK is negative, tick articles."
9594   (interactive "P")
9595   (when unmark
9596     (setq unmark (prefix-numeric-value unmark)))
9597   (let ((count
9598          (gnus-summary-mark-same-subject
9599           (gnus-summary-article-subject) unmark)))
9600     ;; Select next unread article.  If auto-select-same mode, should
9601     ;; select the first unread article.
9602     (gnus-summary-next-article t (and gnus-auto-select-same
9603                                       (gnus-summary-article-subject)))
9604     (gnus-message 7 "%d article%s marked as %s"
9605                   count (if (= count 1) " is" "s are")
9606                   (if unmark "unread" "read"))))
9607
9608 (defun gnus-summary-kill-same-subject (&optional unmark)
9609   "Mark articles which has the same subject as read.
9610 If UNMARK is positive, remove any kind of mark.
9611 If UNMARK is negative, tick articles."
9612   (interactive "P")
9613   (when unmark
9614     (setq unmark (prefix-numeric-value unmark)))
9615   (let ((count
9616          (gnus-summary-mark-same-subject
9617           (gnus-summary-article-subject) unmark)))
9618     ;; If marked as read, go to next unread subject.
9619     (when (null unmark)
9620       ;; Go to next unread subject.
9621       (gnus-summary-next-subject 1 t))
9622     (gnus-message 7 "%d articles are marked as %s"
9623                   count (if unmark "unread" "read"))))
9624
9625 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9626   "Mark articles with same SUBJECT as read, and return marked number.
9627 If optional argument UNMARK is positive, remove any kinds of marks.
9628 If optional argument UNMARK is negative, mark articles as unread instead."
9629   (let ((count 1))
9630     (save-excursion
9631       (cond
9632        ((null unmark)                   ; Mark as read.
9633         (while (and
9634                 (progn
9635                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9636                   (gnus-summary-show-thread) t)
9637                 (gnus-summary-find-subject subject))
9638           (setq count (1+ count))))
9639        ((> unmark 0)                    ; Tick.
9640         (while (and
9641                 (progn
9642                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9643                   (gnus-summary-show-thread) t)
9644                 (gnus-summary-find-subject subject))
9645           (setq count (1+ count))))
9646        (t                               ; Mark as unread.
9647         (while (and
9648                 (progn
9649                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9650                   (gnus-summary-show-thread) t)
9651                 (gnus-summary-find-subject subject))
9652           (setq count (1+ count)))))
9653       (gnus-set-mode-line 'summary)
9654       ;; Return the number of marked articles.
9655       count)))
9656
9657 (defun gnus-summary-mark-as-processable (n &optional unmark)
9658   "Set the process mark on the next N articles.
9659 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9660 the process mark instead.  The difference between N and the actual
9661 number of articles marked is returned."
9662   (interactive "P")
9663   (if (and (null n) (gnus-region-active-p))
9664       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9665     (setq n (prefix-numeric-value n))
9666     (let ((backward (< n 0))
9667           (n (abs n)))
9668       (while (and
9669               (> n 0)
9670               (if unmark
9671                   (gnus-summary-remove-process-mark
9672                    (gnus-summary-article-number))
9673                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9674               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9675         (setq n (1- n)))
9676       (when (/= 0 n)
9677         (gnus-message 7 "No more articles"))
9678       (gnus-summary-recenter)
9679       (gnus-summary-position-point)
9680       n)))
9681
9682 (defun gnus-summary-unmark-as-processable (n)
9683   "Remove the process mark from the next N articles.
9684 If N is negative, unmark backward instead.  The difference between N and
9685 the actual number of articles unmarked is returned."
9686   (interactive "P")
9687   (gnus-summary-mark-as-processable n t))
9688
9689 (defun gnus-summary-unmark-all-processable ()
9690   "Remove the process mark from all articles."
9691   (interactive)
9692   (save-excursion
9693     (while gnus-newsgroup-processable
9694       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9695   (gnus-summary-position-point))
9696
9697 (defun gnus-summary-add-mark (article type)
9698   "Mark ARTICLE with a mark of TYPE."
9699   (let ((vtype (car (assq type gnus-article-mark-lists)))
9700         var)
9701     (if (not vtype)
9702         (error "No such mark type: %s" type)
9703       (setq var (intern (format "gnus-newsgroup-%s" type)))
9704       (set var (cons article (symbol-value var)))
9705       (if (memq type '(processable cached replied forwarded recent saved))
9706           (gnus-summary-update-secondary-mark article)
9707         ;;; !!! This is bogus.  We should find out what primary
9708         ;;; !!! mark we want to set.
9709         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9710
9711 (defun gnus-summary-mark-as-expirable (n)
9712   "Mark N articles forward as expirable.
9713 If N is negative, mark backward instead.  The difference between N and
9714 the actual number of articles marked is returned."
9715   (interactive "p")
9716   (gnus-summary-mark-forward n gnus-expirable-mark))
9717
9718 (defun gnus-summary-mark-as-spam (n)
9719   "Mark N articles forward as spam.
9720 If N is negative, mark backward instead.  The difference between N and
9721 the actual number of articles marked is returned."
9722   (interactive "p")
9723   (gnus-summary-mark-forward n gnus-spam-mark))
9724
9725 (defun gnus-summary-mark-article-as-replied (article)
9726   "Mark ARTICLE as replied to and update the summary line.
9727 ARTICLE can also be a list of articles."
9728   (interactive (list (gnus-summary-article-number)))
9729   (let ((articles (if (listp article) article (list article))))
9730     (dolist (article articles)
9731       (unless (numberp article)
9732         (error "%s is not a number" article))
9733       (push article gnus-newsgroup-replied)
9734       (let ((buffer-read-only nil))
9735         (when (gnus-summary-goto-subject article nil t)
9736           (gnus-summary-update-secondary-mark article))))))
9737
9738 (defun gnus-summary-mark-article-as-forwarded (article)
9739   "Mark ARTICLE as forwarded and update the summary line.
9740 ARTICLE can also be a list of articles."
9741   (let ((articles (if (listp article) article (list article))))
9742     (dolist (article articles)
9743       (push article gnus-newsgroup-forwarded)
9744       (let ((buffer-read-only nil))
9745         (when (gnus-summary-goto-subject article nil t)
9746           (gnus-summary-update-secondary-mark article))))))
9747
9748 (defun gnus-summary-set-bookmark (article)
9749   "Set a bookmark in current article."
9750   (interactive (list (gnus-summary-article-number)))
9751   (when (or (not (get-buffer gnus-article-buffer))
9752             (not gnus-current-article)
9753             (not gnus-article-current)
9754             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9755     (error "No current article selected"))
9756   ;; Remove old bookmark, if one exists.
9757   (gnus-pull article gnus-newsgroup-bookmarks)
9758   ;; Set the new bookmark, which is on the form
9759   ;; (article-number . line-number-in-body).
9760   (push
9761    (cons article
9762          (save-excursion
9763            (set-buffer gnus-article-buffer)
9764            (count-lines
9765             (min (point)
9766                  (save-excursion
9767                    (article-goto-body)
9768                    (point)))
9769             (point))))
9770    gnus-newsgroup-bookmarks)
9771   (gnus-message 6 "A bookmark has been added to the current article."))
9772
9773 (defun gnus-summary-remove-bookmark (article)
9774   "Remove the bookmark from the current article."
9775   (interactive (list (gnus-summary-article-number)))
9776   ;; Remove old bookmark, if one exists.
9777   (if (not (assq article gnus-newsgroup-bookmarks))
9778       (gnus-message 6 "No bookmark in current article.")
9779     (gnus-pull article gnus-newsgroup-bookmarks)
9780     (gnus-message 6 "Removed bookmark.")))
9781
9782 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9783 (defun gnus-summary-mark-as-dormant (n)
9784   "Mark N articles forward as dormant.
9785 If N is negative, mark backward instead.  The difference between N and
9786 the actual number of articles marked is returned."
9787   (interactive "p")
9788   (gnus-summary-mark-forward n gnus-dormant-mark))
9789
9790 (defun gnus-summary-set-process-mark (article)
9791   "Set the process mark on ARTICLE and update the summary line."
9792   (setq gnus-newsgroup-processable
9793         (cons article
9794               (delq article gnus-newsgroup-processable)))
9795   (when (gnus-summary-goto-subject article)
9796     (gnus-summary-show-thread)
9797     (gnus-summary-goto-subject article)
9798     (gnus-summary-update-secondary-mark article)))
9799
9800 (defun gnus-summary-remove-process-mark (article)
9801   "Remove the process mark from ARTICLE and update the summary line."
9802   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9803   (when (gnus-summary-goto-subject article)
9804     (gnus-summary-show-thread)
9805     (gnus-summary-goto-subject article)
9806     (gnus-summary-update-secondary-mark article)))
9807
9808 (defun gnus-summary-set-saved-mark (article)
9809   "Set the process mark on ARTICLE and update the summary line."
9810   (push article gnus-newsgroup-saved)
9811   (when (gnus-summary-goto-subject article)
9812     (gnus-summary-update-secondary-mark article)))
9813
9814 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9815   "Mark N articles as read forwards.
9816 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9817 The difference between N and the actual number of articles marked is
9818 returned.
9819 If NO-EXPIRE, auto-expiry will be inhibited."
9820   (interactive "p")
9821   (gnus-summary-show-thread)
9822   (let ((backward (< n 0))
9823         (gnus-summary-goto-unread
9824          (and gnus-summary-goto-unread
9825               (not (eq gnus-summary-goto-unread 'never))
9826               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9827                                     gnus-ticked-mark gnus-dormant-mark)))))
9828         (n (abs n))
9829         (mark (or mark gnus-del-mark)))
9830     (while (and (> n 0)
9831                 (gnus-summary-mark-article nil mark no-expire)
9832                 (zerop (gnus-summary-next-subject
9833                         (if backward -1 1)
9834                         (and gnus-summary-goto-unread
9835                              (not (eq gnus-summary-goto-unread 'never)))
9836                         t)))
9837       (setq n (1- n)))
9838     (when (/= 0 n)
9839       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9840     (gnus-summary-recenter)
9841     (gnus-summary-position-point)
9842     (gnus-set-mode-line 'summary)
9843     n))
9844
9845 (defun gnus-summary-mark-article-as-read (mark)
9846   "Mark the current article quickly as read with MARK."
9847   (let ((article (gnus-summary-article-number)))
9848     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9849     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9850     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9851     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9852     (push (cons article mark) gnus-newsgroup-reads)
9853     ;; Possibly remove from cache, if that is used.
9854     (when gnus-use-cache
9855       (gnus-cache-enter-remove-article article))
9856     ;; Allow the backend to change the mark.
9857     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9858     ;; Check for auto-expiry.
9859     (when (and gnus-newsgroup-auto-expire
9860                (memq mark gnus-auto-expirable-marks))
9861       (setq mark gnus-expirable-mark)
9862       ;; Let the backend know about the mark change.
9863       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9864       (push article gnus-newsgroup-expirable))
9865     ;; Set the mark in the buffer.
9866     (gnus-summary-update-mark mark 'unread)
9867     t))
9868
9869 (defun gnus-summary-mark-article-as-unread (mark)
9870   "Mark the current article quickly as unread with MARK."
9871   (let* ((article (gnus-summary-article-number))
9872          (old-mark (gnus-summary-article-mark article)))
9873     ;; Allow the backend to change the mark.
9874     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9875     (if (eq mark old-mark)
9876         t
9877       (if (<= article 0)
9878           (progn
9879             (gnus-error 1 "Can't mark negative article numbers")
9880             nil)
9881         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9882         (setq gnus-newsgroup-spam-marked
9883               (delq article gnus-newsgroup-spam-marked))
9884         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9885         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9886         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9887         (cond ((= mark gnus-ticked-mark)
9888                (setq gnus-newsgroup-marked
9889                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9890                                               article)))
9891               ((= mark gnus-spam-mark)
9892                (setq gnus-newsgroup-spam-marked
9893                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9894                                               article)))
9895               ((= mark gnus-dormant-mark)
9896                (setq gnus-newsgroup-dormant
9897                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9898                                               article)))
9899               (t
9900                (setq gnus-newsgroup-unreads
9901                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9902                                               article))))
9903         (gnus-pull article gnus-newsgroup-reads)
9904
9905         ;; See whether the article is to be put in the cache.
9906         (and gnus-use-cache
9907              (vectorp (gnus-summary-article-header article))
9908              (save-excursion
9909                (gnus-cache-possibly-enter-article
9910                 gnus-newsgroup-name article
9911                 (gnus-summary-article-header article)
9912                 (= mark gnus-ticked-mark)
9913                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9914
9915         ;; Fix the mark.
9916         (gnus-summary-update-mark mark 'unread)
9917         t))))
9918
9919 (defun gnus-summary-mark-article (&optional article mark no-expire)
9920   "Mark ARTICLE with MARK.  MARK can be any character.
9921 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9922 `??' (dormant) and `?E' (expirable).
9923 If MARK is nil, then the default character `?r' is used.
9924 If ARTICLE is nil, then the article on the current line will be
9925 marked.
9926 If NO-EXPIRE, auto-expiry will be inhibited."
9927   ;; The mark might be a string.
9928   (when (stringp mark)
9929     (setq mark (aref mark 0)))
9930   ;; If no mark is given, then we check auto-expiring.
9931   (when (null mark)
9932     (setq mark gnus-del-mark))
9933   (when (and (not no-expire)
9934              gnus-newsgroup-auto-expire
9935              (memq mark gnus-auto-expirable-marks))
9936     (setq mark gnus-expirable-mark))
9937   (let ((article (or article (gnus-summary-article-number)))
9938         (old-mark (gnus-summary-article-mark article)))
9939     ;; Allow the backend to change the mark.
9940     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9941     (if (eq mark old-mark)
9942         t
9943       (unless article
9944         (error "No article on current line"))
9945       (if (not (if (or (= mark gnus-unread-mark)
9946                        (= mark gnus-ticked-mark)
9947                        (= mark gnus-spam-mark)
9948                        (= mark gnus-dormant-mark))
9949                    (gnus-mark-article-as-unread article mark)
9950                  (gnus-mark-article-as-read article mark)))
9951           t
9952         ;; See whether the article is to be put in the cache.
9953         (and gnus-use-cache
9954              (not (= mark gnus-canceled-mark))
9955              (vectorp (gnus-summary-article-header article))
9956              (save-excursion
9957                (gnus-cache-possibly-enter-article
9958                 gnus-newsgroup-name article
9959                 (gnus-summary-article-header article)
9960                 (= mark gnus-ticked-mark)
9961                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9962
9963         (when (gnus-summary-goto-subject article nil t)
9964           (let ((buffer-read-only nil))
9965             (gnus-summary-show-thread)
9966             ;; Fix the mark.
9967             (gnus-summary-update-mark mark 'unread)
9968             t))))))
9969
9970 (defun gnus-summary-update-secondary-mark (article)
9971   "Update the secondary (read, process, cache) mark."
9972   (gnus-summary-update-mark
9973    (cond ((memq article gnus-newsgroup-processable)
9974           gnus-process-mark)
9975          ((memq article gnus-newsgroup-cached)
9976           gnus-cached-mark)
9977          ((memq article gnus-newsgroup-replied)
9978           gnus-replied-mark)
9979          ((memq article gnus-newsgroup-forwarded)
9980           gnus-forwarded-mark)
9981          ((memq article gnus-newsgroup-saved)
9982           gnus-saved-mark)
9983          ((memq article gnus-newsgroup-recent)
9984           gnus-recent-mark)
9985          ((memq article gnus-newsgroup-unseen)
9986           gnus-unseen-mark)
9987          (t gnus-no-mark))
9988    'replied)
9989   (when (gnus-visual-p 'summary-highlight 'highlight)
9990     (gnus-run-hooks 'gnus-summary-update-hook))
9991   t)
9992
9993 (defun gnus-summary-update-download-mark (article)
9994   "Update the download mark."
9995   (gnus-summary-update-mark
9996    (cond ((memq article gnus-newsgroup-undownloaded)
9997           gnus-undownloaded-mark)
9998          (gnus-newsgroup-agentized
9999           gnus-downloaded-mark)
10000          (t
10001           gnus-no-mark))
10002    'download)
10003   (gnus-summary-update-line t)
10004   t)
10005
10006 (defun gnus-summary-update-mark (mark type)
10007   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10008         (buffer-read-only nil))
10009     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
10010     (when forward
10011       (when (looking-at "\r")
10012         (incf forward))
10013       (when (<= (+ forward (point)) (point-max))
10014         ;; Go to the right position on the line.
10015         (goto-char (+ forward (point)))
10016         ;; Replace the old mark with the new mark.
10017         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10018         ;; Optionally update the marks by some user rule.
10019         (when (eq type 'unread)
10020           (gnus-data-set-mark
10021            (gnus-data-find (gnus-summary-article-number)) mark)
10022           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10023
10024 (defun gnus-mark-article-as-read (article &optional mark)
10025   "Enter ARTICLE in the pertinent lists and remove it from others."
10026   ;; Make the article expirable.
10027   (let ((mark (or mark gnus-del-mark)))
10028     (setq gnus-newsgroup-expirable
10029           (if (= mark gnus-expirable-mark)
10030               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10031             (delq article gnus-newsgroup-expirable)))
10032     ;; Remove from unread and marked lists.
10033     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10034     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10035     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10036     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10037     (push (cons article mark) gnus-newsgroup-reads)
10038     ;; Possibly remove from cache, if that is used.
10039     (when gnus-use-cache
10040       (gnus-cache-enter-remove-article article))
10041     t))
10042
10043 (defun gnus-mark-article-as-unread (article &optional mark)
10044   "Enter ARTICLE in the pertinent lists and remove it from others."
10045   (let ((mark (or mark gnus-ticked-mark)))
10046     (if (<= article 0)
10047         (progn
10048           (gnus-error 1 "Can't mark negative article numbers")
10049           nil)
10050       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10051             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10052             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10053             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10054             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10055
10056       ;; Unsuppress duplicates?
10057       (when gnus-suppress-duplicates
10058         (gnus-dup-unsuppress-article article))
10059
10060       (cond ((= mark gnus-ticked-mark)
10061              (setq gnus-newsgroup-marked
10062                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10063             ((= mark gnus-spam-mark)
10064              (setq gnus-newsgroup-spam-marked
10065                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10066                                             article)))
10067             ((= mark gnus-dormant-mark)
10068              (setq gnus-newsgroup-dormant
10069                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10070             (t
10071              (setq gnus-newsgroup-unreads
10072                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10073       (gnus-pull article gnus-newsgroup-reads)
10074       t)))
10075
10076 (defalias 'gnus-summary-mark-as-unread-forward
10077   'gnus-summary-tick-article-forward)
10078 (make-obsolete 'gnus-summary-mark-as-unread-forward
10079                'gnus-summary-tick-article-forward)
10080 (defun gnus-summary-tick-article-forward (n)
10081   "Tick N articles forwards.
10082 If N is negative, tick backwards instead.
10083 The difference between N and the number of articles ticked is returned."
10084   (interactive "p")
10085   (gnus-summary-mark-forward n gnus-ticked-mark))
10086
10087 (defalias 'gnus-summary-mark-as-unread-backward
10088   'gnus-summary-tick-article-backward)
10089 (make-obsolete 'gnus-summary-mark-as-unread-backward
10090                'gnus-summary-tick-article-backward)
10091 (defun gnus-summary-tick-article-backward (n)
10092   "Tick N articles backwards.
10093 The difference between N and the number of articles ticked is returned."
10094   (interactive "p")
10095   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10096
10097 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10098 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10099 (defun gnus-summary-tick-article (&optional article clear-mark)
10100   "Mark current article as unread.
10101 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10102 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10103   (interactive)
10104   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10105                                        gnus-ticked-mark)))
10106
10107 (defun gnus-summary-mark-as-read-forward (n)
10108   "Mark N articles as read forwards.
10109 If N is negative, mark backwards instead.
10110 The difference between N and the actual number of articles marked is
10111 returned."
10112   (interactive "p")
10113   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10114
10115 (defun gnus-summary-mark-as-read-backward (n)
10116   "Mark the N articles as read backwards.
10117 The difference between N and the actual number of articles marked is
10118 returned."
10119   (interactive "p")
10120   (gnus-summary-mark-forward
10121    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10122
10123 (defun gnus-summary-mark-as-read (&optional article mark)
10124   "Mark current article as read.
10125 ARTICLE specifies the article to be marked as read.
10126 MARK specifies a string to be inserted at the beginning of the line."
10127   (gnus-summary-mark-article article mark))
10128
10129 (defun gnus-summary-clear-mark-forward (n)
10130   "Clear marks from N articles forward.
10131 If N is negative, clear backward instead.
10132 The difference between N and the number of marks cleared is returned."
10133   (interactive "p")
10134   (gnus-summary-mark-forward n gnus-unread-mark))
10135
10136 (defun gnus-summary-clear-mark-backward (n)
10137   "Clear marks from N articles backward.
10138 The difference between N and the number of marks cleared is returned."
10139   (interactive "p")
10140   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10141
10142 (defun gnus-summary-mark-unread-as-read ()
10143   "Intended to be used by `gnus-summary-mark-article-hook'."
10144   (when (memq gnus-current-article gnus-newsgroup-unreads)
10145     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10146
10147 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10148   "Intended to be used by `gnus-summary-mark-article-hook'."
10149   (let ((mark (gnus-summary-article-mark)))
10150     (when (or (gnus-unread-mark-p mark)
10151               (gnus-read-mark-p mark))
10152       (gnus-summary-mark-article gnus-current-article
10153                                  (or new-mark gnus-read-mark)))))
10154
10155 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10156   "Intended to be used by `gnus-summary-mark-article-hook'."
10157   (let ((mark (gnus-summary-article-mark)))
10158     (when (or (gnus-unread-mark-p mark)
10159               (gnus-read-mark-p mark))
10160       (gnus-summary-mark-article (gnus-summary-article-number)
10161                                  (or new-mark gnus-read-mark)))))
10162
10163 (defun gnus-summary-mark-unread-as-ticked ()
10164   "Intended to be used by `gnus-summary-mark-article-hook'."
10165   (when (memq gnus-current-article gnus-newsgroup-unreads)
10166     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10167
10168 (defun gnus-summary-mark-region-as-read (point mark all)
10169   "Mark all unread articles between point and mark as read.
10170 If given a prefix, mark all articles between point and mark as read,
10171 even ticked and dormant ones."
10172   (interactive "r\nP")
10173   (save-excursion
10174     (let (article)
10175       (goto-char point)
10176       (beginning-of-line)
10177       (while (and
10178               (< (point) mark)
10179               (progn
10180                 (when (or all
10181                           (memq (setq article (gnus-summary-article-number))
10182                                 gnus-newsgroup-unreads))
10183                   (gnus-summary-mark-article article gnus-del-mark))
10184                 t)
10185               (gnus-summary-find-next))))))
10186
10187 (defun gnus-summary-mark-below (score mark)
10188   "Mark articles with score less than SCORE with MARK."
10189   (interactive "P\ncMark: ")
10190   (setq score (if score
10191                   (prefix-numeric-value score)
10192                 (or gnus-summary-default-score 0)))
10193   (save-excursion
10194     (set-buffer gnus-summary-buffer)
10195     (goto-char (point-min))
10196     (while
10197         (progn
10198           (and (< (gnus-summary-article-score) score)
10199                (gnus-summary-mark-article nil mark))
10200           (gnus-summary-find-next)))))
10201
10202 (defun gnus-summary-kill-below (&optional score)
10203   "Mark articles with score below SCORE as read."
10204   (interactive "P")
10205   (gnus-summary-mark-below score gnus-killed-mark))
10206
10207 (defun gnus-summary-clear-above (&optional score)
10208   "Clear all marks from articles with score above SCORE."
10209   (interactive "P")
10210   (gnus-summary-mark-above score gnus-unread-mark))
10211
10212 (defun gnus-summary-tick-above (&optional score)
10213   "Tick all articles with score above SCORE."
10214   (interactive "P")
10215   (gnus-summary-mark-above score gnus-ticked-mark))
10216
10217 (defun gnus-summary-mark-above (score mark)
10218   "Mark articles with score over SCORE with MARK."
10219   (interactive "P\ncMark: ")
10220   (setq score (if score
10221                   (prefix-numeric-value score)
10222                 (or gnus-summary-default-score 0)))
10223   (save-excursion
10224     (set-buffer gnus-summary-buffer)
10225     (goto-char (point-min))
10226     (while (and (progn
10227                   (when (> (gnus-summary-article-score) score)
10228                     (gnus-summary-mark-article nil mark))
10229                   t)
10230                 (gnus-summary-find-next)))))
10231
10232 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10233 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10234 (defun gnus-summary-limit-include-expunged (&optional no-error)
10235   "Display all the hidden articles that were expunged for low scores."
10236   (interactive)
10237   (let ((buffer-read-only nil))
10238     (let ((scored gnus-newsgroup-scored)
10239           headers h)
10240       (while scored
10241         (unless (gnus-summary-article-header (caar scored))
10242           (and (setq h (gnus-number-to-header (caar scored)))
10243                (< (cdar scored) gnus-summary-expunge-below)
10244                (push h headers)))
10245         (setq scored (cdr scored)))
10246       (if (not headers)
10247           (when (not no-error)
10248             (error "No expunged articles hidden"))
10249         (goto-char (point-min))
10250         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10251         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10252         (mapcar (lambda (x) (push (mail-header-number x)
10253                                   gnus-newsgroup-limit))
10254                 headers)
10255         (gnus-summary-prepare-unthreaded (nreverse headers))
10256         (goto-char (point-min))
10257         (gnus-summary-position-point)
10258         t))))
10259
10260 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10261   "Mark all unread articles in this newsgroup as read.
10262 If prefix argument ALL is non-nil, ticked and dormant articles will
10263 also be marked as read.
10264 If QUIETLY is non-nil, no questions will be asked.
10265
10266 If TO-HERE is non-nil, it should be a point in the buffer.  All
10267 articles before (after, if REVERSE is set) this point will be marked
10268 as read.
10269
10270 Note that this function will only catch up the unread article
10271 in the current summary buffer limitation.
10272
10273 The number of articles marked as read is returned."
10274   (interactive "P")
10275   (prog1
10276       (save-excursion
10277         (when (or quietly
10278                   (not gnus-interactive-catchup) ;Without confirmation?
10279                   gnus-expert-user
10280                   (gnus-y-or-n-p
10281                    (if all
10282                        "Mark absolutely all articles as read? "
10283                      "Mark all unread articles as read? ")))
10284           (if (and not-mark
10285                    (not gnus-newsgroup-adaptive)
10286                    (not gnus-newsgroup-auto-expire)
10287                    (not gnus-suppress-duplicates)
10288                    (or (not gnus-use-cache)
10289                        (eq gnus-use-cache 'passive)))
10290               (progn
10291                 (when all
10292                   (setq gnus-newsgroup-marked nil
10293                         gnus-newsgroup-spam-marked nil
10294                         gnus-newsgroup-dormant nil))
10295                 (setq gnus-newsgroup-unreads
10296                       (gnus-sorted-nunion
10297                        (gnus-intersection gnus-newsgroup-unreads
10298                                           gnus-newsgroup-downloadable)
10299                        gnus-newsgroup-unfetched)))
10300             ;; We actually mark all articles as canceled, which we
10301             ;; have to do when using auto-expiry or adaptive scoring.
10302             (gnus-summary-show-all-threads)
10303             (if (and to-here reverse)
10304                 (progn
10305                   (goto-char to-here)
10306                   (gnus-summary-mark-current-read-and-unread-as-read
10307                    gnus-catchup-mark)
10308                   (while (gnus-summary-find-next (not all))
10309                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10310               (when (gnus-summary-first-subject (not all))
10311                 (while (and
10312                         (if to-here (< (point) to-here) t)
10313                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10314                         (gnus-summary-find-next (not all))))))
10315             (gnus-set-mode-line 'summary))
10316           t))
10317     (gnus-summary-position-point)))
10318
10319 (defun gnus-summary-catchup-to-here (&optional all)
10320   "Mark all unticked articles before the current one as read.
10321 If ALL is non-nil, also mark ticked and dormant articles as read."
10322   (interactive "P")
10323   (save-excursion
10324     (gnus-save-hidden-threads
10325       (let ((beg (point)))
10326         ;; We check that there are unread articles.
10327         (when (or all (gnus-summary-find-prev))
10328           (gnus-summary-catchup all t beg)))))
10329   (gnus-summary-position-point))
10330
10331 (defun gnus-summary-catchup-from-here (&optional all)
10332   "Mark all unticked articles after (and including) the current one as read.
10333 If ALL is non-nil, also mark ticked and dormant articles as read."
10334   (interactive "P")
10335   (save-excursion
10336     (gnus-save-hidden-threads
10337       (let ((beg (point)))
10338         ;; We check that there are unread articles.
10339         (when (or all (gnus-summary-find-next))
10340           (gnus-summary-catchup all t beg nil t)))))
10341   (gnus-summary-position-point))
10342
10343 (defun gnus-summary-catchup-all (&optional quietly)
10344   "Mark all articles in this newsgroup as read.
10345 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10346 instead, which marks only unread articles as read."
10347   (interactive "P")
10348   (gnus-summary-catchup t quietly))
10349
10350 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10351   "Mark all unread articles in this group as read, then exit.
10352 If prefix argument ALL is non-nil, all articles are marked as read.
10353 If QUIETLY is non-nil, no questions will be asked."
10354   (interactive "P")
10355   (when (gnus-summary-catchup all quietly nil 'fast)
10356     ;; Select next newsgroup or exit.
10357     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10358              (eq gnus-auto-select-next 'quietly))
10359         (gnus-summary-next-group nil)
10360       (gnus-summary-exit))))
10361
10362 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10363   "Mark all articles in this newsgroup as read, and then exit.
10364 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10365 instead, which marks only unread articles as read."
10366   (interactive "P")
10367   (gnus-summary-catchup-and-exit t quietly))
10368
10369 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10370   "Mark all articles in this group as read and select the next group.
10371 If given a prefix, mark all articles, unread as well as ticked, as
10372 read."
10373   (interactive "P")
10374   (save-excursion
10375     (gnus-summary-catchup all))
10376   (gnus-summary-next-group))
10377
10378 ;;;
10379 ;;; with article
10380 ;;;
10381
10382 (defmacro gnus-with-article (article &rest forms)
10383   "Select ARTICLE and perform FORMS in the original article buffer.
10384 Then replace the article with the result."
10385   `(progn
10386      ;; We don't want the article to be marked as read.
10387      (let (gnus-mark-article-hook)
10388        (gnus-summary-select-article t t nil ,article))
10389      (set-buffer gnus-original-article-buffer)
10390      ,@forms
10391      (if (not (gnus-check-backend-function
10392                'request-replace-article (car gnus-article-current)))
10393          (gnus-message 5 "Read-only group; not replacing")
10394        (unless (gnus-request-replace-article
10395                 ,article (car gnus-article-current)
10396                 (current-buffer) t)
10397          (error "Couldn't replace article")))
10398      ;; The cache and backlog have to be flushed somewhat.
10399      (when gnus-keep-backlog
10400        (gnus-backlog-remove-article
10401         (car gnus-article-current) (cdr gnus-article-current)))
10402      (when gnus-use-cache
10403        (gnus-cache-update-article
10404         (car gnus-article-current) (cdr gnus-article-current)))))
10405
10406 (put 'gnus-with-article 'lisp-indent-function 1)
10407 (put 'gnus-with-article 'edebug-form-spec '(form body))
10408
10409 ;; Thread-based commands.
10410
10411 (defun gnus-summary-articles-in-thread (&optional article)
10412   "Return a list of all articles in the current thread.
10413 If ARTICLE is non-nil, return all articles in the thread that starts
10414 with that article."
10415   (let* ((article (or article (gnus-summary-article-number)))
10416          (data (gnus-data-find-list article))
10417          (top-level (gnus-data-level (car data)))
10418          (top-subject
10419           (cond ((null gnus-thread-operation-ignore-subject)
10420                  (gnus-simplify-subject-re
10421                   (mail-header-subject (gnus-data-header (car data)))))
10422                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10423                  (gnus-simplify-subject-fuzzy
10424                   (mail-header-subject (gnus-data-header (car data)))))
10425                 (t nil)))
10426          (end-point (save-excursion
10427                       (if (gnus-summary-go-to-next-thread)
10428                           (point) (point-max))))
10429          articles)
10430     (while (and data
10431                 (< (gnus-data-pos (car data)) end-point))
10432       (when (or (not top-subject)
10433                 (string= top-subject
10434                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10435                              (gnus-simplify-subject-fuzzy
10436                               (mail-header-subject
10437                                (gnus-data-header (car data))))
10438                            (gnus-simplify-subject-re
10439                             (mail-header-subject
10440                              (gnus-data-header (car data)))))))
10441         (push (gnus-data-number (car data)) articles))
10442       (unless (and (setq data (cdr data))
10443                    (> (gnus-data-level (car data)) top-level))
10444         (setq data nil)))
10445     ;; Return the list of articles.
10446     (nreverse articles)))
10447
10448 (defun gnus-summary-rethread-current ()
10449   "Rethread the thread the current article is part of."
10450   (interactive)
10451   (let* ((gnus-show-threads t)
10452          (article (gnus-summary-article-number))
10453          (id (mail-header-id (gnus-summary-article-header)))
10454          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10455     (unless id
10456       (error "No article on the current line"))
10457     (gnus-rebuild-thread id)
10458     (gnus-summary-goto-subject article)))
10459
10460 (defun gnus-summary-reparent-thread ()
10461   "Make the current article child of the marked (or previous) article.
10462
10463 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10464 is non-nil or the Subject: of both articles are the same."
10465   (interactive)
10466   (unless (not (gnus-group-read-only-p))
10467     (error "The current newsgroup does not support article editing"))
10468   (unless (<= (length gnus-newsgroup-processable) 1)
10469     (error "No more than one article may be marked"))
10470   (save-window-excursion
10471     (let ((gnus-article-buffer " *reparent*")
10472           (current-article (gnus-summary-article-number))
10473           ;; First grab the marked article, otherwise one line up.
10474           (parent-article (if (not (null gnus-newsgroup-processable))
10475                               (car gnus-newsgroup-processable)
10476                             (save-excursion
10477                               (if (eq (forward-line -1) 0)
10478                                   (gnus-summary-article-number)
10479                                 (error "Beginning of summary buffer"))))))
10480       (unless (not (eq current-article parent-article))
10481         (error "An article may not be self-referential"))
10482       (let ((message-id (mail-header-id
10483                          (gnus-summary-article-header parent-article))))
10484         (unless (and message-id (not (equal message-id "")))
10485           (error "No message-id in desired parent"))
10486         (gnus-with-article current-article
10487           (save-restriction
10488             (goto-char (point-min))
10489             (message-narrow-to-head)
10490             (if (re-search-forward "^References: " nil t)
10491                 (progn
10492                   (re-search-forward "^[^ \t]" nil t)
10493                   (forward-line -1)
10494                   (end-of-line)
10495                   (insert " " message-id))
10496               (insert "References: " message-id "\n"))))
10497         (set-buffer gnus-summary-buffer)
10498         (gnus-summary-unmark-all-processable)
10499         (gnus-summary-update-article current-article)
10500         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10501             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10502         (gnus-summary-rethread-current)
10503         (gnus-message 3 "Article %d is now the child of article %d"
10504                       current-article parent-article)))))
10505
10506 (defun gnus-summary-toggle-threads (&optional arg)
10507   "Toggle showing conversation threads.
10508 If ARG is positive number, turn showing conversation threads on."
10509   (interactive "P")
10510   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10511     (setq gnus-show-threads
10512           (if (null arg) (not gnus-show-threads)
10513             (> (prefix-numeric-value arg) 0)))
10514     (gnus-summary-prepare)
10515     (gnus-summary-goto-subject current)
10516     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10517     (gnus-summary-position-point)))
10518
10519 (defun gnus-summary-show-all-threads ()
10520   "Show all threads."
10521   (interactive)
10522   (save-excursion
10523     (let ((buffer-read-only nil))
10524       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10525   (gnus-summary-position-point))
10526
10527 (defun gnus-summary-show-thread ()
10528   "Show thread subtrees.
10529 Returns nil if no thread was there to be shown."
10530   (interactive)
10531   (let ((buffer-read-only nil)
10532         (orig (point))
10533         (end (gnus-point-at-eol))
10534         ;; Leave point at bol
10535         (beg (progn (beginning-of-line) (point))))
10536     (prog1
10537         ;; Any hidden lines here?
10538         (search-forward "\r" end t)
10539       (subst-char-in-region beg end ?\^M ?\n t)
10540       (goto-char orig)
10541       (gnus-summary-position-point))))
10542
10543 (defun gnus-summary-maybe-hide-threads ()
10544   "If requested, hide the threads that should be hidden."
10545   (when (and gnus-show-threads
10546              gnus-thread-hide-subtree)
10547     (gnus-summary-hide-all-threads
10548      (if (or (consp gnus-thread-hide-subtree)
10549              (functionp gnus-thread-hide-subtree))
10550          (gnus-make-predicate gnus-thread-hide-subtree)
10551        nil))))
10552
10553 ;;; Hiding predicates.
10554
10555 (defun gnus-article-unread-p (header)
10556   (memq (mail-header-number header) gnus-newsgroup-unreads))
10557
10558 (defun gnus-article-unseen-p (header)
10559   (memq (mail-header-number header) gnus-newsgroup-unseen))
10560
10561 (defun gnus-map-articles (predicate articles)
10562   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10563   (apply 'gnus-or (mapcar predicate
10564                           (mapcar 'gnus-summary-article-header articles))))
10565
10566 (defun gnus-summary-hide-all-threads (&optional predicate)
10567   "Hide all thread subtrees.
10568 If PREDICATE is supplied, threads that satisfy this predicate
10569 will not be hidden."
10570   (interactive)
10571   (save-excursion
10572     (goto-char (point-min))
10573     (let ((end nil))
10574       (while (not end)
10575         (when (or (not predicate)
10576                   (gnus-map-articles
10577                    predicate (gnus-summary-article-children)))
10578             (gnus-summary-hide-thread))
10579         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10580   (gnus-summary-position-point))
10581
10582 (defun gnus-summary-hide-thread ()
10583   "Hide thread subtrees.
10584 If PREDICATE is supplied, threads that satisfy this predicate
10585 will not be hidden.
10586 Returns nil if no threads were there to be hidden."
10587   (interactive)
10588   (let ((buffer-read-only nil)
10589         (start (point))
10590         (article (gnus-summary-article-number)))
10591     (goto-char start)
10592     ;; Go forward until either the buffer ends or the subthread
10593     ;; ends.
10594     (when (and (not (eobp))
10595                (or (zerop (gnus-summary-next-thread 1 t))
10596                    (goto-char (point-max))))
10597       (prog1
10598           (if (and (> (point) start)
10599                    (search-backward "\n" start t))
10600               (progn
10601                 (subst-char-in-region start (point) ?\n ?\^M)
10602                 (gnus-summary-goto-subject article))
10603             (goto-char start)
10604             nil)))))
10605
10606 (defun gnus-summary-go-to-next-thread (&optional previous)
10607   "Go to the same level (or less) next thread.
10608 If PREVIOUS is non-nil, go to previous thread instead.
10609 Return the article number moved to, or nil if moving was impossible."
10610   (let ((level (gnus-summary-thread-level))
10611         (way (if previous -1 1))
10612         (beg (point)))
10613     (forward-line way)
10614     (while (and (not (eobp))
10615                 (< level (gnus-summary-thread-level)))
10616       (forward-line way))
10617     (if (eobp)
10618         (progn
10619           (goto-char beg)
10620           nil)
10621       (setq beg (point))
10622       (prog1
10623           (gnus-summary-article-number)
10624         (goto-char beg)))))
10625
10626 (defun gnus-summary-next-thread (n &optional silent)
10627   "Go to the same level next N'th thread.
10628 If N is negative, search backward instead.
10629 Returns the difference between N and the number of skips actually
10630 done.
10631
10632 If SILENT, don't output messages."
10633   (interactive "p")
10634   (let ((backward (< n 0))
10635         (n (abs n)))
10636     (while (and (> n 0)
10637                 (gnus-summary-go-to-next-thread backward))
10638       (decf n))
10639     (unless silent
10640       (gnus-summary-position-point))
10641     (when (and (not silent) (/= 0 n))
10642       (gnus-message 7 "No more threads"))
10643     n))
10644
10645 (defun gnus-summary-prev-thread (n)
10646   "Go to the same level previous N'th thread.
10647 Returns the difference between N and the number of skips actually
10648 done."
10649   (interactive "p")
10650   (gnus-summary-next-thread (- n)))
10651
10652 (defun gnus-summary-go-down-thread ()
10653   "Go down one level in the current thread."
10654   (let ((children (gnus-summary-article-children)))
10655     (when children
10656       (gnus-summary-goto-subject (car children)))))
10657
10658 (defun gnus-summary-go-up-thread ()
10659   "Go up one level in the current thread."
10660   (let ((parent (gnus-summary-article-parent)))
10661     (when parent
10662       (gnus-summary-goto-subject parent))))
10663
10664 (defun gnus-summary-down-thread (n)
10665   "Go down thread N steps.
10666 If N is negative, go up instead.
10667 Returns the difference between N and how many steps down that were
10668 taken."
10669   (interactive "p")
10670   (let ((up (< n 0))
10671         (n (abs n)))
10672     (while (and (> n 0)
10673                 (if up (gnus-summary-go-up-thread)
10674                   (gnus-summary-go-down-thread)))
10675       (setq n (1- n)))
10676     (gnus-summary-position-point)
10677     (when (/= 0 n)
10678       (gnus-message 7 "Can't go further"))
10679     n))
10680
10681 (defun gnus-summary-up-thread (n)
10682   "Go up thread N steps.
10683 If N is negative, go down instead.
10684 Returns the difference between N and how many steps down that were
10685 taken."
10686   (interactive "p")
10687   (gnus-summary-down-thread (- n)))
10688
10689 (defun gnus-summary-top-thread ()
10690   "Go to the top of the thread."
10691   (interactive)
10692   (while (gnus-summary-go-up-thread))
10693   (gnus-summary-article-number))
10694
10695 (defun gnus-summary-kill-thread (&optional unmark)
10696   "Mark articles under current thread as read.
10697 If the prefix argument is positive, remove any kinds of marks.
10698 If the prefix argument is negative, tick articles instead."
10699   (interactive "P")
10700   (when unmark
10701     (setq unmark (prefix-numeric-value unmark)))
10702   (let ((articles (gnus-summary-articles-in-thread)))
10703     (save-excursion
10704       ;; Expand the thread.
10705       (gnus-summary-show-thread)
10706       ;; Mark all the articles.
10707       (while articles
10708         (gnus-summary-goto-subject (car articles))
10709         (cond ((null unmark)
10710                (gnus-summary-mark-article-as-read gnus-killed-mark))
10711               ((> unmark 0)
10712                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10713               (t
10714                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10715         (setq articles (cdr articles))))
10716     ;; Hide killed subtrees.
10717     (and (null unmark)
10718          gnus-thread-hide-killed
10719          (gnus-summary-hide-thread))
10720     ;; If marked as read, go to next unread subject.
10721     (when (null unmark)
10722       ;; Go to next unread subject.
10723       (gnus-summary-next-subject 1 t)))
10724   (gnus-set-mode-line 'summary))
10725
10726 ;; Summary sorting commands
10727
10728 (defun gnus-summary-sort-by-number (&optional reverse)
10729   "Sort the summary buffer by article number.
10730 Argument REVERSE means reverse order."
10731   (interactive "P")
10732   (gnus-summary-sort 'number reverse))
10733
10734 (defun gnus-summary-sort-by-random (&optional reverse)
10735   "Randomize the order in the summary buffer.
10736 Argument REVERSE means to randomize in reverse order."
10737   (interactive "P")
10738   (gnus-summary-sort 'random reverse))
10739
10740 (defun gnus-summary-sort-by-author (&optional reverse)
10741   "Sort the summary buffer by author name alphabetically.
10742 If `case-fold-search' is non-nil, case of letters is ignored.
10743 Argument REVERSE means reverse order."
10744   (interactive "P")
10745   (gnus-summary-sort 'author reverse))
10746
10747 (defun gnus-summary-sort-by-subject (&optional reverse)
10748   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10749 If `case-fold-search' is non-nil, case of letters is ignored.
10750 Argument REVERSE means reverse order."
10751   (interactive "P")
10752   (gnus-summary-sort 'subject reverse))
10753
10754 (defun gnus-summary-sort-by-date (&optional reverse)
10755   "Sort the summary buffer by date.
10756 Argument REVERSE means reverse order."
10757   (interactive "P")
10758   (gnus-summary-sort 'date reverse))
10759
10760 (defun gnus-summary-sort-by-score (&optional reverse)
10761   "Sort the summary buffer by score.
10762 Argument REVERSE means reverse order."
10763   (interactive "P")
10764   (gnus-summary-sort 'score reverse))
10765
10766 (defun gnus-summary-sort-by-lines (&optional reverse)
10767   "Sort the summary buffer by the number of lines.
10768 Argument REVERSE means reverse order."
10769   (interactive "P")
10770   (gnus-summary-sort 'lines reverse))
10771
10772 (defun gnus-summary-sort-by-chars (&optional reverse)
10773   "Sort the summary buffer by article length.
10774 Argument REVERSE means reverse order."
10775   (interactive "P")
10776   (gnus-summary-sort 'chars reverse))
10777
10778 (defun gnus-summary-sort-by-original (&optional reverse)
10779   "Sort the summary buffer using the default sorting method.
10780 Argument REVERSE means reverse order."
10781   (interactive "P")
10782   (let* ((buffer-read-only)
10783          (gnus-summary-prepare-hook nil))
10784     ;; We do the sorting by regenerating the threads.
10785     (gnus-summary-prepare)
10786     ;; Hide subthreads if needed.
10787     (gnus-summary-maybe-hide-threads)))
10788
10789 (defun gnus-summary-sort (predicate reverse)
10790   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10791   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10792          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10793          (gnus-thread-sort-functions
10794           (if (not reverse)
10795               thread
10796             `(lambda (t1 t2)
10797                (,thread t2 t1))))
10798          (gnus-sort-gathered-threads-function
10799           gnus-thread-sort-functions)
10800          (gnus-article-sort-functions
10801           (if (not reverse)
10802               article
10803             `(lambda (t1 t2)
10804                (,article t2 t1))))
10805          (buffer-read-only)
10806          (gnus-summary-prepare-hook nil))
10807     ;; We do the sorting by regenerating the threads.
10808     (gnus-summary-prepare)
10809     ;; Hide subthreads if needed.
10810     (gnus-summary-maybe-hide-threads)))
10811
10812 ;; Summary saving commands.
10813
10814 (defun gnus-summary-save-article (&optional n not-saved)
10815   "Save the current article using the default saver function.
10816 If N is a positive number, save the N next articles.
10817 If N is a negative number, save the N previous articles.
10818 If N is nil and any articles have been marked with the process mark,
10819 save those articles instead.
10820 The variable `gnus-default-article-saver' specifies the saver function."
10821   (interactive "P")
10822   (let* ((articles (gnus-summary-work-articles n))
10823          (save-buffer (save-excursion
10824                         (nnheader-set-temp-buffer " *Gnus Save*")))
10825          (num (length articles))
10826          header file)
10827     (dolist (article articles)
10828       (setq header (gnus-summary-article-header article))
10829       (if (not (vectorp header))
10830           ;; This is a pseudo-article.
10831           (if (assq 'name header)
10832               (gnus-copy-file (cdr (assq 'name header)))
10833             (gnus-message 1 "Article %d is unsaveable" article))
10834         ;; This is a real article.
10835         (save-window-excursion
10836           (let ((gnus-display-mime-function nil)
10837                 (gnus-article-prepare-hook nil))
10838             (gnus-summary-select-article t nil nil article)))
10839         (save-excursion
10840           (set-buffer save-buffer)
10841           (erase-buffer)
10842           (insert-buffer-substring gnus-original-article-buffer))
10843         (setq file (gnus-article-save save-buffer file num))
10844         (gnus-summary-remove-process-mark article)
10845         (unless not-saved
10846           (gnus-summary-set-saved-mark article))))
10847     (gnus-kill-buffer save-buffer)
10848     (gnus-summary-position-point)
10849     (gnus-set-mode-line 'summary)
10850     n))
10851
10852 (defun gnus-summary-pipe-output (&optional arg headers)
10853   "Pipe the current article to a subprocess.
10854 If N is a positive number, pipe the N next articles.
10855 If N is a negative number, pipe the N previous articles.
10856 If N is nil and any articles have been marked with the process mark,
10857 pipe those articles instead.
10858 If HEADERS (the symbolic prefix), include the headers, too."
10859   (interactive (gnus-interactive "P\ny"))
10860   (require 'gnus-art)
10861   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10862         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10863     (gnus-summary-save-article arg t))
10864   (let ((buffer (get-buffer "*Shell Command Output*")))
10865     (when (and buffer
10866                (not (zerop (buffer-size buffer))))
10867       (gnus-configure-windows 'pipe))))
10868
10869 (defun gnus-summary-save-article-mail (&optional arg)
10870   "Append the current article to an mail file.
10871 If N is a positive number, save the N next articles.
10872 If N is a negative number, save the N previous articles.
10873 If N is nil and any articles have been marked with the process mark,
10874 save those articles instead."
10875   (interactive "P")
10876   (require 'gnus-art)
10877   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10878     (gnus-summary-save-article arg)))
10879
10880 (defun gnus-summary-save-article-rmail (&optional arg)
10881   "Append the current article to an rmail file.
10882 If N is a positive number, save the N next articles.
10883 If N is a negative number, save the N previous articles.
10884 If N is nil and any articles have been marked with the process mark,
10885 save those articles instead."
10886   (interactive "P")
10887   (require 'gnus-art)
10888   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10889     (gnus-summary-save-article arg)))
10890
10891 (defun gnus-summary-save-article-file (&optional arg)
10892   "Append the current article to a file.
10893 If N is a positive number, save the N next articles.
10894 If N is a negative number, save the N previous articles.
10895 If N is nil and any articles have been marked with the process mark,
10896 save those articles instead."
10897   (interactive "P")
10898   (require 'gnus-art)
10899   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10900     (gnus-summary-save-article arg)))
10901
10902 (defun gnus-summary-write-article-file (&optional arg)
10903   "Write the current article to a file, deleting the previous file.
10904 If N is a positive number, save the N next articles.
10905 If N is a negative number, save the N previous articles.
10906 If N is nil and any articles have been marked with the process mark,
10907 save those articles instead."
10908   (interactive "P")
10909   (require 'gnus-art)
10910   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10911     (gnus-summary-save-article arg)))
10912
10913 (defun gnus-summary-save-article-body-file (&optional arg)
10914   "Append the current article body to a file.
10915 If N is a positive number, save the N next articles.
10916 If N is a negative number, save the N previous articles.
10917 If N is nil and any articles have been marked with the process mark,
10918 save those articles instead."
10919   (interactive "P")
10920   (require 'gnus-art)
10921   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10922     (gnus-summary-save-article arg)))
10923
10924 (defun gnus-summary-muttprint (&optional arg)
10925   "Print the current article using Muttprint.
10926 If N is a positive number, save the N next articles.
10927 If N is a negative number, save the N previous articles.
10928 If N is nil and any articles have been marked with the process mark,
10929 save those articles instead."
10930   (interactive "P")
10931   (require 'gnus-art)
10932   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10933     (gnus-summary-save-article arg t)))
10934
10935 (defun gnus-summary-pipe-message (program)
10936   "Pipe the current article through PROGRAM."
10937   (interactive "sProgram: ")
10938   (gnus-summary-select-article)
10939   (let ((mail-header-separator ""))
10940     (gnus-eval-in-buffer-window gnus-article-buffer
10941       (save-restriction
10942         (widen)
10943         (let ((start (window-start))
10944               buffer-read-only)
10945           (message-pipe-buffer-body program)
10946           (set-window-start (get-buffer-window (current-buffer)) start))))))
10947
10948 (defun gnus-get-split-value (methods)
10949   "Return a value based on the split METHODS."
10950   (let (split-name method result match)
10951     (when methods
10952       (save-excursion
10953         (set-buffer gnus-original-article-buffer)
10954         (save-restriction
10955           (nnheader-narrow-to-headers)
10956           (while (and methods (not split-name))
10957             (goto-char (point-min))
10958             (setq method (pop methods))
10959             (setq match (car method))
10960             (when (cond
10961                    ((stringp match)
10962                     ;; Regular expression.
10963                     (ignore-errors
10964                       (re-search-forward match nil t)))
10965                    ((functionp match)
10966                     ;; Function.
10967                     (save-restriction
10968                       (widen)
10969                       (setq result (funcall match gnus-newsgroup-name))))
10970                    ((consp match)
10971                     ;; Form.
10972                     (save-restriction
10973                       (widen)
10974                       (setq result (eval match)))))
10975               (setq split-name (cdr method))
10976               (cond ((stringp result)
10977                      (push (expand-file-name
10978                             result gnus-article-save-directory)
10979                            split-name))
10980                     ((consp result)
10981                      (setq split-name (append result split-name)))))))))
10982     (nreverse split-name)))
10983
10984 (defun gnus-valid-move-group-p (group)
10985   (and (boundp group)
10986        (symbol-name group)
10987        (symbol-value group)
10988        (gnus-get-function (gnus-find-method-for-group
10989                            (symbol-name group)) 'request-accept-article t)))
10990
10991 (defun gnus-read-move-group-name (prompt default articles prefix)
10992   "Read a group name."
10993   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10994          (minibuffer-confirm-incomplete nil) ; XEmacs
10995          (prom
10996           (format "%s %s to:"
10997                   prompt
10998                   (if (> (length articles) 1)
10999                       (format "these %d articles" (length articles))
11000                     "this article")))
11001          (to-newsgroup
11002           (cond
11003            ((null split-name)
11004             (gnus-completing-read-with-default
11005              default prom
11006              gnus-active-hashtb
11007              'gnus-valid-move-group-p
11008              nil prefix
11009              'gnus-group-history))
11010            ((= 1 (length split-name))
11011             (gnus-completing-read-with-default
11012              (car split-name) prom
11013              gnus-active-hashtb
11014              'gnus-valid-move-group-p
11015              nil nil
11016              'gnus-group-history))
11017            (t
11018             (gnus-completing-read-with-default
11019              nil prom
11020              (mapcar (lambda (el) (list el))
11021                      (nreverse split-name))
11022              nil nil nil
11023              'gnus-group-history))))
11024          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11025     (when to-newsgroup
11026       (if (or (string= to-newsgroup "")
11027               (string= to-newsgroup prefix))
11028           (setq to-newsgroup default))
11029       (unless to-newsgroup
11030         (error "No group name entered"))
11031       (or (gnus-active to-newsgroup)
11032           (gnus-activate-group to-newsgroup nil nil to-method)
11033           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11034                                      to-newsgroup))
11035               (or (and (gnus-request-create-group to-newsgroup to-method)
11036                        (gnus-activate-group
11037                         to-newsgroup nil nil to-method)
11038                        (gnus-subscribe-group to-newsgroup))
11039                   (error "Couldn't create group %s" to-newsgroup)))
11040           (error "No such group: %s" to-newsgroup)))
11041     to-newsgroup))
11042
11043 (defun gnus-summary-save-parts (type dir n &optional reverse)
11044   "Save parts matching TYPE to DIR.
11045 If REVERSE, save parts that do not match TYPE."
11046   (interactive
11047    (list (read-string "Save parts of type: "
11048                       (or (car gnus-summary-save-parts-type-history)
11049                           gnus-summary-save-parts-default-mime)
11050                       'gnus-summary-save-parts-type-history)
11051          (setq gnus-summary-save-parts-last-directory
11052                (read-file-name "Save to directory: "
11053                                gnus-summary-save-parts-last-directory
11054                                nil t))
11055          current-prefix-arg))
11056   (gnus-summary-iterate n
11057     (let ((gnus-display-mime-function nil)
11058           (gnus-inhibit-treatment t))
11059       (gnus-summary-select-article))
11060     (save-excursion
11061       (set-buffer gnus-article-buffer)
11062       (let ((handles (or gnus-article-mime-handles
11063                          (mm-dissect-buffer nil gnus-article-loose-mime)
11064                          (and gnus-article-emulate-mime
11065                               (mm-uu-dissect)))))
11066         (when handles
11067           (gnus-summary-save-parts-1 type dir handles reverse)
11068           (unless gnus-article-mime-handles ;; Don't destroy this case.
11069             (mm-destroy-parts handles)))))))
11070
11071 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11072   (if (stringp (car handle))
11073       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11074               (cdr handle))
11075     (when (if reverse
11076               (not (string-match type (mm-handle-media-type handle)))
11077             (string-match type (mm-handle-media-type handle)))
11078       (let ((file (expand-file-name
11079                    (gnus-map-function
11080                     mm-file-name-rewrite-functions
11081                     (file-name-nondirectory
11082                      (or
11083                       (mail-content-type-get
11084                        (mm-handle-disposition handle) 'filename)
11085                       (mail-content-type-get
11086                        (mm-handle-type handle) 'name)
11087                       (concat gnus-newsgroup-name
11088                               "." (number-to-string
11089                                    (cdr gnus-article-current))))))
11090                    dir)))
11091         (unless (file-exists-p file)
11092           (mm-save-part-to-file handle file))))))
11093
11094 ;; Summary extract commands
11095
11096 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11097   (let ((buffer-read-only nil)
11098         (article (gnus-summary-article-number))
11099         after-article b e)
11100     (unless (gnus-summary-goto-subject article)
11101       (error "No such article: %d" article))
11102     (gnus-summary-position-point)
11103     ;; If all commands are to be bunched up on one line, we collect
11104     ;; them here.
11105     (unless gnus-view-pseudos-separately
11106       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11107             files action)
11108         (while ps
11109           (setq action (cdr (assq 'action (car ps))))
11110           (setq files (list (cdr (assq 'name (car ps)))))
11111           (while (and ps (cdr ps)
11112                       (string= (or action "1")
11113                                (or (cdr (assq 'action (cadr ps))) "2")))
11114             (push (cdr (assq 'name (cadr ps))) files)
11115             (setcdr ps (cddr ps)))
11116           (when files
11117             (when (not (string-match "%s" action))
11118               (push " " files))
11119             (push " " files)
11120             (when (assq 'execute (car ps))
11121               (setcdr (assq 'execute (car ps))
11122                       (funcall (if (string-match "%s" action)
11123                                    'format 'concat)
11124                                action
11125                                (mapconcat
11126                                 (lambda (f)
11127                                   (if (equal f " ")
11128                                       f
11129                                     (gnus-quote-arg-for-sh-or-csh f)))
11130                                 files " ")))))
11131           (setq ps (cdr ps)))))
11132     (if (and gnus-view-pseudos (not not-view))
11133         (while pslist
11134           (when (assq 'execute (car pslist))
11135             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11136                                   (eq gnus-view-pseudos 'not-confirm)))
11137           (setq pslist (cdr pslist)))
11138       (save-excursion
11139         (while pslist
11140           (setq after-article (or (cdr (assq 'article (car pslist)))
11141                                   (gnus-summary-article-number)))
11142           (gnus-summary-goto-subject after-article)
11143           (forward-line 1)
11144           (setq b (point))
11145           (insert "    " (file-name-nondirectory
11146                           (cdr (assq 'name (car pslist))))
11147                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11148           (setq e (point))
11149           (forward-line -1)             ; back to `b'
11150           (gnus-add-text-properties
11151            b (1- e) (list 'gnus-number gnus-reffed-article-number
11152                           gnus-mouse-face-prop gnus-mouse-face))
11153           (gnus-data-enter
11154            after-article gnus-reffed-article-number
11155            gnus-unread-mark b (car pslist) 0 (- e b))
11156           (setq gnus-newsgroup-unreads
11157                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11158                                          gnus-reffed-article-number))
11159           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11160           (setq pslist (cdr pslist)))))))
11161
11162 (defun gnus-pseudos< (p1 p2)
11163   (let ((c1 (cdr (assq 'action p1)))
11164         (c2 (cdr (assq 'action p2))))
11165     (and c1 c2 (string< c1 c2))))
11166
11167 (defun gnus-request-pseudo-article (props)
11168   (cond ((assq 'execute props)
11169          (gnus-execute-command (cdr (assq 'execute props)))))
11170   (let ((gnus-current-article (gnus-summary-article-number)))
11171     (gnus-run-hooks 'gnus-mark-article-hook)))
11172
11173 (defun gnus-execute-command (command &optional automatic)
11174   (save-excursion
11175     (gnus-article-setup-buffer)
11176     (set-buffer gnus-article-buffer)
11177     (setq buffer-read-only nil)
11178     (let ((command (if automatic command
11179                      (read-string "Command: " (cons command 0)))))
11180       (erase-buffer)
11181       (insert "$ " command "\n\n")
11182       (if gnus-view-pseudo-asynchronously
11183           (start-process "gnus-execute" (current-buffer) shell-file-name
11184                          shell-command-switch command)
11185         (call-process shell-file-name nil t nil
11186                       shell-command-switch command)))))
11187
11188 ;; Summary kill commands.
11189
11190 (defun gnus-summary-edit-global-kill (article)
11191   "Edit the \"global\" kill file."
11192   (interactive (list (gnus-summary-article-number)))
11193   (gnus-group-edit-global-kill article))
11194
11195 (defun gnus-summary-edit-local-kill ()
11196   "Edit a local kill file applied to the current newsgroup."
11197   (interactive)
11198   (setq gnus-current-headers (gnus-summary-article-header))
11199   (gnus-group-edit-local-kill
11200    (gnus-summary-article-number) gnus-newsgroup-name))
11201
11202 ;;; Header reading.
11203
11204 (defun gnus-read-header (id &optional header)
11205   "Read the headers of article ID and enter them into the Gnus system."
11206   (let ((group gnus-newsgroup-name)
11207         (gnus-override-method
11208          (or
11209           gnus-override-method
11210           (and (gnus-news-group-p gnus-newsgroup-name)
11211                (car (gnus-refer-article-methods)))))
11212         where)
11213     ;; First we check to see whether the header in question is already
11214     ;; fetched.
11215     (if (stringp id)
11216         ;; This is a Message-ID.
11217         (setq header (or header (gnus-id-to-header id)))
11218       ;; This is an article number.
11219       (setq header (or header (gnus-summary-article-header id))))
11220     (if (and header
11221              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11222         ;; We have found the header.
11223         header
11224       ;; If this is a sparse article, we have to nix out its
11225       ;; previous entry in the thread hashtb.
11226       (when (and header
11227                  (gnus-summary-article-sparse-p (mail-header-number header)))
11228         (let* ((parent (gnus-parent-id (mail-header-references header)))
11229                (thread (and parent (gnus-id-to-thread parent))))
11230           (when thread
11231             (delq (assq header thread) thread))))
11232       ;; We have to really fetch the header to this article.
11233       (save-excursion
11234         (set-buffer nntp-server-buffer)
11235         (when (setq where (gnus-request-head id group))
11236           (nnheader-fold-continuation-lines)
11237           (goto-char (point-max))
11238           (insert ".\n")
11239           (goto-char (point-min))
11240           (insert "211 ")
11241           (princ (cond
11242                   ((numberp id) id)
11243                   ((cdr where) (cdr where))
11244                   (header (mail-header-number header))
11245                   (t gnus-reffed-article-number))
11246                  (current-buffer))
11247           (insert " Article retrieved.\n"))
11248         (if (or (not where)
11249                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11250             ()                          ; Malformed head.
11251           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11252             (when (and (stringp id)
11253                        (not (string= (gnus-group-real-name group)
11254                                      (car where))))
11255               ;; If we fetched by Message-ID and the article came
11256               ;; from a different group, we fudge some bogus article
11257               ;; numbers for this article.
11258               (mail-header-set-number header gnus-reffed-article-number))
11259             (save-excursion
11260               (set-buffer gnus-summary-buffer)
11261               (decf gnus-reffed-article-number)
11262               (gnus-remove-header (mail-header-number header))
11263               (push header gnus-newsgroup-headers)
11264               (setq gnus-current-headers header)
11265               (push (mail-header-number header) gnus-newsgroup-limit)))
11266           header)))))
11267
11268 (defun gnus-remove-header (number)
11269   "Remove header NUMBER from `gnus-newsgroup-headers'."
11270   (if (and gnus-newsgroup-headers
11271            (= number (mail-header-number (car gnus-newsgroup-headers))))
11272       (pop gnus-newsgroup-headers)
11273     (let ((headers gnus-newsgroup-headers))
11274       (while (and (cdr headers)
11275                   (not (= number (mail-header-number (cadr headers)))))
11276         (pop headers))
11277       (when (cdr headers)
11278         (setcdr headers (cddr headers))))))
11279
11280 ;;;
11281 ;;; summary highlights
11282 ;;;
11283
11284 (defun gnus-highlight-selected-summary ()
11285   "Highlight selected article in summary buffer."
11286   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11287   (when gnus-summary-selected-face
11288     (save-excursion
11289       (let* ((beg (gnus-point-at-bol))
11290              (end (gnus-point-at-eol))
11291              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11292              (from (if (get-text-property beg gnus-mouse-face-prop)
11293                        beg
11294                      (or (next-single-property-change
11295                           beg gnus-mouse-face-prop nil end)
11296                          beg)))
11297              (to
11298               (if (= from end)
11299                   (- from 2)
11300                 (or (next-single-property-change
11301                      from gnus-mouse-face-prop nil end)
11302                     end))))
11303         ;; If no mouse-face prop on line we will have to = from = end,
11304         ;; so we highlight the entire line instead.
11305         (when (= (+ to 2) from)
11306           (setq from beg)
11307           (setq to end))
11308         (if gnus-newsgroup-selected-overlay
11309             ;; Move old overlay.
11310             (gnus-move-overlay
11311              gnus-newsgroup-selected-overlay from to (current-buffer))
11312           ;; Create new overlay.
11313           (gnus-overlay-put
11314            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11315            'face gnus-summary-selected-face))))))
11316
11317 (defvar gnus-summary-highlight-line-cached nil)
11318 (defvar gnus-summary-highlight-line-trigger nil)
11319
11320 (defun gnus-summary-highlight-line-0 ()
11321   (if (and (eq gnus-summary-highlight-line-trigger
11322                gnus-summary-highlight)
11323            gnus-summary-highlight-line-cached)
11324       gnus-summary-highlight-line-cached
11325     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11326           gnus-summary-highlight-line-cached
11327           (let* ((cond (list 'cond))
11328                  (c cond)
11329                  (list gnus-summary-highlight))
11330             (while list
11331               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11332                               nil))
11333               (setq c (cdr c)
11334                     list (cdr list)))
11335             (gnus-byte-compile (list 'lambda nil cond))))))
11336
11337 (defun gnus-summary-highlight-line ()
11338   "Highlight current line according to `gnus-summary-highlight'."
11339   (let* ((beg (gnus-point-at-bol))
11340          (article (or (gnus-summary-article-number) gnus-current-article))
11341          (score (or (cdr (assq article
11342                                gnus-newsgroup-scored))
11343                     gnus-summary-default-score 0))
11344          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11345          (inhibit-read-only t)
11346          (default gnus-summary-default-score)
11347          (default-high gnus-summary-default-high-score)
11348          (default-low gnus-summary-default-low-score)
11349          (uncached (and gnus-summary-use-undownloaded-faces
11350                         (memq article gnus-newsgroup-undownloaded))))
11351     (let ((face (funcall (gnus-summary-highlight-line-0))))
11352       (unless (eq face (get-text-property beg 'face))
11353         (gnus-put-text-property-excluding-characters-with-faces
11354          beg (gnus-point-at-eol) 'face
11355          (setq face (if (boundp face) (symbol-value face) face)))
11356         (when gnus-summary-highlight-line-function
11357           (funcall gnus-summary-highlight-line-function article face))))))
11358
11359 (defun gnus-update-read-articles (group unread &optional compute)
11360   "Update the list of read articles in GROUP.
11361 UNREAD is a sorted list."
11362   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11363          (entry (gnus-gethash group gnus-newsrc-hashtb))
11364          (info (nth 2 entry))
11365          (prev 1)
11366          read)
11367     (if (or (not info) (not active))
11368         ;; There is no info on this group if it was, in fact,
11369         ;; killed.  Gnus stores no information on killed groups, so
11370         ;; there's nothing to be done.
11371         ;; One could store the information somewhere temporarily,
11372         ;; perhaps...  Hmmm...
11373         ()
11374       ;; Remove any negative articles numbers.
11375       (while (and unread (< (car unread) 0))
11376         (setq unread (cdr unread)))
11377       ;; Remove any expired article numbers
11378       (while (and unread (< (car unread) (car active)))
11379         (setq unread (cdr unread)))
11380       ;; Compute the ranges of read articles by looking at the list of
11381       ;; unread articles.
11382       (while unread
11383         (when (/= (car unread) prev)
11384           (push (if (= prev (1- (car unread))) prev
11385                   (cons prev (1- (car unread))))
11386                 read))
11387         (setq prev (1+ (car unread)))
11388         (setq unread (cdr unread)))
11389       (when (<= prev (cdr active))
11390         (push (cons prev (cdr active)) read))
11391       (setq read (if (> (length read) 1) (nreverse read) read))
11392       (if compute
11393           read
11394         (save-excursion
11395           (let (setmarkundo)
11396             ;; Propagate the read marks to the backend.
11397             (when (gnus-check-backend-function 'request-set-mark group)
11398               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11399                     (add (gnus-remove-from-range read (gnus-info-read info))))
11400                 (when (or add del)
11401                   (unless (gnus-check-group group)
11402                     (error "Can't open server for %s" group))
11403                   (gnus-request-set-mark
11404                    group (delq nil (list (if add (list add 'add '(read)))
11405                                          (if del (list del 'del '(read))))))
11406                   (setq setmarkundo
11407                         `(gnus-request-set-mark
11408                           ,group
11409                           ',(delq nil (list
11410                                        (if del (list del 'add '(read)))
11411                                        (if add (list add 'del '(read))))))))))
11412             (set-buffer gnus-group-buffer)
11413             (gnus-undo-register
11414               `(progn
11415                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11416                  (gnus-info-set-read ',info ',(gnus-info-read info))
11417                  (gnus-get-unread-articles-in-group ',info
11418                                                     (gnus-active ,group))
11419                  (gnus-group-update-group ,group t)
11420                  ,setmarkundo))))
11421         ;; Enter this list into the group info.
11422         (gnus-info-set-read info read)
11423         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11424         (gnus-get-unread-articles-in-group info (gnus-active group))
11425         t))))
11426
11427 (defun gnus-offer-save-summaries ()
11428   "Offer to save all active summary buffers."
11429   (let (buffers)
11430     ;; Go through all buffers and find all summaries.
11431     (dolist (buffer (buffer-list))
11432       (when (and (setq buffer (buffer-name buffer))
11433                  (string-match "Summary" buffer)
11434                  (save-excursion
11435                    (set-buffer buffer)
11436                    ;; We check that this is, indeed, a summary buffer.
11437                    (and (eq major-mode 'gnus-summary-mode)
11438                         ;; Also make sure this isn't bogus.
11439                         gnus-newsgroup-prepared
11440                         ;; Also make sure that this isn't a
11441                         ;; dead summary buffer.
11442                         (not gnus-dead-summary-mode))))
11443         (push buffer buffers)))
11444     ;; Go through all these summary buffers and offer to save them.
11445     (when buffers
11446       (save-excursion
11447         (map-y-or-n-p
11448          "Update summary buffer %s? "
11449          (lambda (buf)
11450            (switch-to-buffer buf)
11451            (gnus-summary-exit))
11452          buffers)))))
11453
11454
11455 ;;; @ for mime-partial
11456 ;;;
11457
11458 (defun gnus-request-partial-message ()
11459   (save-excursion
11460     (let ((number (gnus-summary-article-number))
11461           (group gnus-newsgroup-name)
11462           (mother gnus-article-buffer))
11463       (set-buffer (get-buffer-create " *Partial Article*"))
11464       (erase-buffer)
11465       (setq mime-preview-buffer mother)
11466       (gnus-request-article-this-buffer number group)
11467       (mime-parse-buffer)
11468       )))
11469
11470 (autoload 'mime-combine-message/partial-pieces-automatically
11471   "mime-partial"
11472   "Internal method to combine message/partial messages automatically.")
11473
11474 (mime-add-condition
11475  'action '((type . message)(subtype . partial)
11476            (major-mode . gnus-original-article-mode)
11477            (method . mime-combine-message/partial-pieces-automatically)
11478            (summary-buffer-exp . gnus-summary-buffer)
11479            (request-partial-message-method . gnus-request-partial-message)
11480            ))
11481
11482
11483 ;;; @ for message/rfc822
11484 ;;;
11485
11486 (defun gnus-mime-extract-message/rfc822 (entity situation)
11487   "Burst a forwarded article."
11488   (save-excursion
11489     (set-buffer gnus-summary-buffer)
11490     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11491                                    gnus-newsgroup-name 'gnus-group-history))
11492            (gnus-group-marked (list group))
11493            article info)
11494       (with-temp-buffer
11495         (mime-insert-entity-content entity)
11496         (setq article (gnus-request-accept-article group)))
11497       (when (and (consp article)
11498                  (numberp (setq article (cdr article))))
11499         (setq info (gnus-get-info group))
11500         (gnus-info-set-read info
11501                             (gnus-remove-from-range (gnus-info-read info)
11502                                                     (list article)))
11503         (when (string-equal group gnus-newsgroup-name)
11504           (forward-line 1)
11505           (let (gnus-show-threads)
11506             (gnus-summary-goto-subject article t))
11507           (gnus-summary-clear-mark-forward 1))
11508         (set-buffer gnus-group-buffer)
11509         (gnus-group-get-new-news-this-group nil t)))))
11510
11511 (mime-add-condition
11512  'action '((type . message)(subtype . rfc822)
11513            (major-mode . gnus-original-article-mode)
11514            (method . gnus-mime-extract-message/rfc822)
11515            (mode . "extract")
11516            ))
11517
11518 (mime-add-condition
11519  'action '((type . message)(subtype . news)
11520            (major-mode . gnus-original-article-mode)
11521            (method . gnus-mime-extract-message/rfc822)
11522            (mode . "extract")
11523            ))
11524
11525 (defun gnus-mime-extract-multipart (entity situation)
11526   (let ((children (mime-entity-children entity))
11527         mime-acting-situation-to-override
11528         f)
11529     (while children
11530       (mime-play-entity (car children)
11531                         (cons (assq 'mode situation)
11532                               mime-acting-situation-to-override))
11533       (setq children (cdr children)))
11534     (if (setq f (cdr (assq 'after-method
11535                            mime-acting-situation-to-override)))
11536         (eval f)
11537       )))
11538
11539 (mime-add-condition
11540  'action '((type . multipart)
11541            (method . gnus-mime-extract-multipart)
11542            (mode . "extract")
11543            )
11544  'with-default)
11545
11546
11547 ;;; @ end
11548 ;;;
11549
11550 (defun gnus-summary-inherit-default-charset ()
11551   "Import `default-mime-charset' from summary buffer.
11552 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11553 of FLIM is used."
11554   (if (buffer-live-p gnus-summary-buffer)
11555       (let (d-m-c d-m-c-u)
11556         (with-current-buffer gnus-summary-buffer
11557           (setq d-m-c (if (local-variable-p 'default-mime-charset
11558                                             gnus-summary-buffer)
11559                           default-mime-charset
11560                         t)
11561                 ;; LIMIT
11562                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11563                                               gnus-summary-buffer)
11564                             (symbol-value 'default-mime-charset-unlimited)
11565                           t)))
11566         (if (eq t d-m-c)
11567             (kill-local-variable 'default-mime-charset)
11568           (set (make-local-variable 'default-mime-charset) d-m-c))
11569         (if (eq t d-m-c-u)
11570             (kill-local-variable 'default-mime-charset-unlimited)
11571           (set (make-local-variable 'default-mime-charset-unlimited)
11572                d-m-c-u)))))
11573
11574 (defun gnus-summary-setup-default-charset ()
11575   "Setup newsgroup default charset."
11576   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11577       (progn
11578         (setq gnus-newsgroup-charset nil)
11579         (set (make-local-variable 'default-mime-charset) nil)
11580         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11581           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11582     (let ((ignored-charsets
11583            (or gnus-newsgroup-ephemeral-ignored-charsets
11584                (append
11585                 (and gnus-newsgroup-name
11586                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11587                 gnus-newsgroup-ignored-charsets)))
11588           charset)
11589       (setq gnus-newsgroup-charset
11590             (or gnus-newsgroup-ephemeral-charset
11591                 (when (and gnus-newsgroup-name
11592                            (setq charset (gnus-parameter-charset
11593                                           gnus-newsgroup-name)))
11594                   (make-local-variable 'default-mime-charset)
11595                   (setq default-mime-charset charset))
11596                 gnus-default-charset))
11597       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11598            ignored-charsets))))
11599
11600 ;;;
11601 ;;; Mime Commands
11602 ;;;
11603
11604 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11605   "Display the current article buffer fully MIME-buttonized.
11606 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11607 treated as multipart/mixed."
11608   (interactive "P")
11609   (require 'gnus-art)
11610   (let ((gnus-unbuttonized-mime-types nil)
11611         (gnus-mime-display-multipart-as-mixed show-all-parts))
11612     (gnus-summary-show-article)))
11613
11614 (defun gnus-summary-repair-multipart (article)
11615   "Add a Content-Type header to a multipart article without one."
11616   (interactive (list (gnus-summary-article-number)))
11617   (gnus-with-article article
11618     (message-narrow-to-head)
11619     (message-remove-header "Mime-Version")
11620     (goto-char (point-max))
11621     (insert "Mime-Version: 1.0\n")
11622     (widen)
11623     (when (search-forward "\n--" nil t)
11624       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11625         (message-narrow-to-head)
11626         (message-remove-header "Content-Type")
11627         (goto-char (point-max))
11628         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11629                         separator))
11630         (widen))))
11631   (let (gnus-mark-article-hook)
11632     (gnus-summary-select-article t t nil article)))
11633
11634 (defun gnus-summary-toggle-display-buttonized ()
11635   "Toggle the buttonizing of the article buffer."
11636   (interactive)
11637   (require 'gnus-art)
11638   (if (setq gnus-inhibit-mime-unbuttonizing
11639             (not gnus-inhibit-mime-unbuttonizing))
11640       (let ((gnus-unbuttonized-mime-types nil))
11641         (gnus-summary-show-article))
11642     (gnus-summary-show-article)))
11643
11644 ;;;
11645 ;;; Intelli-mouse commmands
11646 ;;;
11647
11648 (defun gnus-wheel-summary-scroll (event)
11649   (interactive "e")
11650   (let ((amount (if (memq 'shift (event-modifiers event))
11651                     (car gnus-wheel-scroll-amount)
11652                   (cdr gnus-wheel-scroll-amount)))
11653         (direction (- (* (static-if (featurep 'xemacs)
11654                              (event-button event)
11655                            (cond ((eq 'mouse-4 (event-basic-type event))
11656                                   4)
11657                                  ((eq 'mouse-5 (event-basic-type event))
11658                                   5)))
11659                          2) 9))
11660         edge)
11661     (gnus-summary-scroll-up (* amount direction))
11662     (when (gnus-eval-in-buffer-window gnus-article-buffer
11663             (save-restriction
11664               (widen)
11665               (and (if (< 0 direction)
11666                        (gnus-article-next-page 0)
11667                      (gnus-article-prev-page 0)
11668                      (bobp))
11669                    (if (setq edge (get-text-property
11670                                    (point-min) 'gnus-wheel-edge))
11671                        (setq edge (* edge direction))
11672                      (setq edge -1))
11673                    (or (plusp edge)
11674                        (let ((buffer-read-only nil)
11675                              (inhibit-read-only t))
11676                          (put-text-property (point-min) (point-max)
11677                                             'gnus-wheel-edge direction)
11678                          nil))
11679                    (or (> edge gnus-wheel-edge-resistance)
11680                        (let ((buffer-read-only nil)
11681                              (inhibit-read-only t))
11682                          (put-text-property (point-min) (point-max)
11683                                             'gnus-wheel-edge
11684                                             (* (1+ edge) direction))
11685                          nil))
11686                    (eq last-command 'gnus-wheel-summary-scroll))))
11687       (gnus-summary-next-article nil nil (minusp direction)))))
11688
11689 (defun gnus-wheel-install ()
11690   "Enable mouse wheel support on summary window."
11691   (when gnus-use-wheel
11692     (let ((keys
11693            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11694       (dolist (key keys)
11695         (define-key gnus-summary-mode-map key
11696           'gnus-wheel-summary-scroll)))))
11697
11698 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11699
11700 ;;;
11701 ;;; Traditional PGP commmands
11702 ;;;
11703
11704 (defun gnus-summary-decrypt-article (&optional force)
11705   "Decrypt the current article in traditional PGP way.
11706 This will have permanent effect only in mail groups.
11707 If FORCE is non-nil, allow editing of articles even in read-only
11708 groups."
11709   (interactive "P")
11710   (gnus-summary-select-article t)
11711   (gnus-eval-in-buffer-window gnus-article-buffer
11712     (save-excursion
11713       (save-restriction
11714         (widen)
11715         (goto-char (point-min))
11716         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11717           (error "Not a traditional PGP message!"))
11718         (let ((armor-start (match-beginning 0)))
11719           (if (and (pgg-decrypt-region armor-start (point-max))
11720                    (or force (not (gnus-group-read-only-p))))
11721               (let ((inhibit-read-only t)
11722                     buffer-read-only)
11723                 (delete-region armor-start
11724                                (progn
11725                                  (re-search-forward "^-+END PGP" nil t)
11726                                  (beginning-of-line 2)
11727                                  (point)))
11728                 (insert-buffer-substring pgg-output-buffer))))))))
11729
11730 (defun gnus-summary-verify-article ()
11731   "Verify the current article in traditional PGP way."
11732   (interactive)
11733   (save-excursion
11734     (set-buffer gnus-original-article-buffer)
11735     (goto-char (point-min))
11736     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11737       (error "Not a traditional PGP message!"))
11738     (re-search-forward "^-+END PGP" nil t)
11739     (beginning-of-line 2)
11740     (call-interactively (function pgg-verify-region))))
11741
11742 ;;;
11743 ;;; Generic summary marking commands
11744 ;;;
11745
11746 (defvar gnus-summary-marking-alist
11747   '((read gnus-del-mark "d")
11748     (unread gnus-unread-mark "u")
11749     (ticked gnus-ticked-mark "!")
11750     (dormant gnus-dormant-mark "?")
11751     (expirable gnus-expirable-mark "e"))
11752   "An alist of names/marks/keystrokes.")
11753
11754 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11755 (defvar gnus-summary-mark-map)
11756
11757 (defun gnus-summary-make-all-marking-commands ()
11758   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11759   (dolist (elem gnus-summary-marking-alist)
11760     (apply 'gnus-summary-make-marking-command elem)))
11761
11762 (defun gnus-summary-make-marking-command (name mark keystroke)
11763   (let ((map (make-sparse-keymap)))
11764     (define-key gnus-summary-generic-mark-map keystroke map)
11765     (dolist (lway `((next "next" next nil "n")
11766                     (next-unread "next unread" next t "N")
11767                     (prev "previous" prev nil "p")
11768                     (prev-unread "previous unread" prev t "P")
11769                     (nomove "" nil nil ,keystroke)))
11770       (let ((func (gnus-summary-make-marking-command-1
11771                    mark (car lway) lway name)))
11772         (setq func (eval func))
11773         (define-key map (nth 4 lway) func)))))
11774
11775 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11776   `(defun ,(intern
11777             (format "gnus-summary-put-mark-as-%s%s"
11778                     name (if (eq way 'nomove)
11779                              ""
11780                            (concat "-" (symbol-name way)))))
11781      (n)
11782      ,(format
11783        "Mark the current article as %s%s.
11784 If N, the prefix, then repeat N times.
11785 If N is negative, move in reverse order.
11786 The difference between N and the actual number of articles marked is
11787 returned."
11788        name (car (cdr lway)))
11789      (interactive "p")
11790      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11791
11792 (defun gnus-summary-generic-mark (n mark move unread)
11793   "Mark N articles with MARK."
11794   (unless (eq major-mode 'gnus-summary-mode)
11795     (error "This command can only be used in the summary buffer"))
11796   (gnus-summary-show-thread)
11797   (let ((nummove
11798          (cond
11799           ((eq move 'next) 1)
11800           ((eq move 'prev) -1)
11801           (t 0))))
11802     (if (zerop nummove)
11803         (setq n 1)
11804       (when (< n 0)
11805         (setq n (abs n)
11806               nummove (* -1 nummove))))
11807     (while (and (> n 0)
11808                 (gnus-summary-mark-article nil mark)
11809                 (zerop (gnus-summary-next-subject nummove unread t)))
11810       (setq n (1- n)))
11811     (when (/= 0 n)
11812       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11813     (gnus-summary-recenter)
11814     (gnus-summary-position-point)
11815     (gnus-set-mode-line 'summary)
11816     n))
11817
11818 (defun gnus-summary-insert-articles (articles)
11819   (when (setq articles
11820               (gnus-sorted-difference articles
11821                                       (mapcar (lambda (h)
11822                                                 (mail-header-number h))
11823                                               gnus-newsgroup-headers)))
11824     (setq gnus-newsgroup-headers
11825           (gnus-merge 'list
11826                       gnus-newsgroup-headers
11827                       (gnus-fetch-headers articles)
11828                       'gnus-article-sort-by-number))
11829     ;; Suppress duplicates?
11830     (when gnus-suppress-duplicates
11831       (gnus-dup-suppress-articles))
11832
11833     ;; We might want to build some more threads first.
11834     (when (and gnus-fetch-old-headers
11835                (eq gnus-headers-retrieved-by 'nov))
11836       (if (eq gnus-fetch-old-headers 'invisible)
11837           (gnus-build-all-threads)
11838         (gnus-build-old-threads)))
11839     ;; Let the Gnus agent mark articles as read.
11840     (when gnus-agent
11841       (gnus-agent-get-undownloaded-list))
11842     ;; Remove list identifiers from subject
11843     (when gnus-list-identifiers
11844       (gnus-summary-remove-list-identifiers))
11845     ;; First and last article in this newsgroup.
11846     (when gnus-newsgroup-headers
11847       (setq gnus-newsgroup-begin
11848             (mail-header-number (car gnus-newsgroup-headers))
11849             gnus-newsgroup-end
11850             (mail-header-number
11851              (gnus-last-element gnus-newsgroup-headers))))
11852     (when gnus-use-scoring
11853       (gnus-possibly-score-headers))))
11854
11855 (defun gnus-summary-insert-old-articles (&optional all)
11856   "Insert all old articles in this group.
11857 If ALL is non-nil, already read articles become readable.
11858 If ALL is a number, fetch this number of articles."
11859   (interactive "P")
11860   (prog1
11861       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11862             older len)
11863         (setq older
11864               ;; Some nntp servers lie about their active range.  When
11865               ;; this happens, the active range can be in the millions.
11866               ;; Use a compressed range to avoid creating a huge list.
11867               (gnus-range-difference (list gnus-newsgroup-active) old))
11868         (setq len (gnus-range-length older))
11869         (cond
11870          ((null older) nil)
11871          ((numberp all)
11872           (if (< all len)
11873               (let ((older-range (nreverse older)))
11874                 (setq older nil)
11875
11876                 (while (> all 0)
11877                   (let* ((r (pop older-range))
11878                          (min (if (numberp r) r (car r)))
11879                          (max (if (numberp r) r (cdr r))))
11880                     (while (and (<= min max)
11881                                 (> all 0))
11882                       (push max older)
11883                       (setq all (1- all)
11884                             max (1- max))))))
11885             (setq older (gnus-uncompress-range older))))
11886          (all
11887           (setq older (gnus-uncompress-range older)))
11888          (t
11889           (when (and (numberp gnus-large-newsgroup)
11890                    (> len gnus-large-newsgroup))
11891               (let* ((cursor-in-echo-area nil)
11892                      (initial (gnus-parameter-large-newsgroup-initial
11893                                gnus-newsgroup-name))
11894                      (input
11895                       (read-string
11896                        (format
11897                         "How many articles from %s (%s %d): "
11898                         (gnus-limit-string
11899                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11900                         (if initial "max" "default")
11901                         len)
11902                        (if initial
11903                            (cons (number-to-string initial)
11904                                  0)))))
11905                 (unless (string-match "^[ \t]*$" input)
11906                   (setq all (string-to-number input))
11907                   (if (< all len)
11908                       (let ((older-range (nreverse older)))
11909                         (setq older nil)
11910
11911                         (while (> all 0)
11912                           (let* ((r (pop older-range))
11913                                  (min (if (numberp r) r (car r)))
11914                                  (max (if (numberp r) r (cdr r))))
11915                             (while (and (<= min max)
11916                                         (> all 0))
11917                               (push max older)
11918                               (setq all (1- all)
11919                                     max (1- max))))))))))
11920           (setq older (gnus-uncompress-range older))))
11921         (if (not older)
11922             (message "No old news.")
11923           (gnus-summary-insert-articles older)
11924           (gnus-summary-limit (gnus-sorted-nunion old older))))
11925     (gnus-summary-position-point)))
11926
11927 (defun gnus-summary-insert-new-articles ()
11928   "Insert all new articles in this group."
11929   (interactive)
11930   (prog1
11931       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11932             (old-active gnus-newsgroup-active)
11933             (nnmail-fetched-sources (list t))
11934             i new)
11935         (setq gnus-newsgroup-active
11936               (gnus-activate-group gnus-newsgroup-name 'scan))
11937         (setq i (cdr gnus-newsgroup-active))
11938         (while (> i (cdr old-active))
11939           (push i new)
11940           (decf i))
11941         (if (not new)
11942             (message "No gnus is bad news.")
11943           (gnus-summary-insert-articles new)
11944           (setq gnus-newsgroup-unreads
11945                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11946           (gnus-summary-limit (gnus-sorted-nunion old new))))
11947     (gnus-summary-position-point)))
11948
11949 (gnus-summary-make-all-marking-commands)
11950
11951 (gnus-ems-redefine)
11952
11953 (provide 'gnus-sum)
11954
11955 (run-hooks 'gnus-sum-load-hook)
11956
11957 ;; Local Variables:
11958 ;; coding: iso-8859-1
11959 ;; End:
11960
11961 ;;; gnus-sum.el ends here