Merge from gnus--rel--5.10
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile
31   (require 'cl)
32   (defvar tool-bar-mode))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 (require 'gmm-utils)
42 (require 'mm-decode)
43 (require 'nnoo)
44
45 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
46 (autoload 'gnus-cache-write-active "gnus-cache")
47 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
48 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
49 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
50 (autoload 'mm-uu-dissect "mm-uu")
51 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
52   "Deuglify broken Outlook (Express) articles and redisplay."
53   t)
54 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
55 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
56 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
57
58 (defcustom gnus-kill-summary-on-exit t
59   "*If non-nil, kill the summary buffer when you exit from it.
60 If nil, the summary will become a \"*Dead Summary*\" buffer, and
61 it will be killed sometime later."
62   :group 'gnus-summary-exit
63   :type 'boolean)
64
65 (defcustom gnus-fetch-old-headers nil
66   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
67 If an unread article in the group refers to an older, already
68 read (or just marked as read) article, the old article will not
69 normally be displayed in the Summary buffer.  If this variable is
70 t, Gnus will attempt to grab the headers to the old articles, and
71 thereby build complete threads.  If it has the value `some', all
72 old headers will be fetched but only enough headers to connect
73 otherwise loose threads will be displayed.  This variable can
74 also be a number.  In that case, no more than that number of old
75 headers will be fetched.  If it has the value `invisible', all
76 old headers will be fetched, but none will be displayed.
77
78 The server has to support NOV for any of this to work.
79
80 This feature can seriously impact performance it ignores all
81 locally cached header entries."
82   :group 'gnus-thread
83   :type '(choice (const :tag "off" nil)
84                  (const :tag "on" t)
85                  (const some)
86                  (const invisible)
87                  number
88                  (sexp :menu-tag "other" t)))
89
90 (defcustom gnus-refer-thread-limit 500
91   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
92 If t, fetch all the available old headers."
93   :group 'gnus-thread
94   :type '(choice number
95                  (sexp :menu-tag "other" t)))
96
97 (defcustom gnus-summary-make-false-root 'adopt
98   "*nil means that Gnus won't gather loose threads.
99 If the root of a thread has expired or been read in a previous
100 session, the information necessary to build a complete thread has been
101 lost.  Instead of having many small sub-threads from this original thread
102 scattered all over the summary buffer, Gnus can gather them.
103
104 If non-nil, Gnus will try to gather all loose sub-threads from an
105 original thread into one large thread.
106
107 If this variable is non-nil, it should be one of `none', `adopt',
108 `dummy' or `empty'.
109
110 If this variable is `none', Gnus will not make a false root, but just
111 present the sub-threads after another.
112 If this variable is `dummy', Gnus will create a dummy root that will
113 have all the sub-threads as children.
114 If this variable is `adopt', Gnus will make one of the \"children\"
115 the parent and mark all the step-children as such.
116 If this variable is `empty', the \"children\" are printed with empty
117 subject fields.  (Or rather, they will be printed with a string
118 given by the `gnus-summary-same-subject' variable.)"
119   :group 'gnus-thread
120   :type '(choice (const :tag "off" nil)
121                  (const none)
122                  (const dummy)
123                  (const adopt)
124                  (const empty)))
125
126 (defcustom gnus-summary-make-false-root-always nil
127   "Always make a false dummy root."
128   :version "22.1"
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   :version "22.1"
230   :group 'gnus-score-default
231   :type 'integer)
232
233 (defcustom gnus-summary-default-low-score 0
234   "*Default threshold for a low scored article.
235 An article will be highlighted as low scored if its score is smaller
236 than this score."
237   :version "22.1"
238   :group 'gnus-score-default
239   :type 'integer)
240
241 (defcustom gnus-summary-zcore-fuzz 0
242   "*Fuzziness factor for the zcore in the summary buffer.
243 Articles with scores closer than this to `gnus-summary-default-score'
244 will not be marked."
245   :group 'gnus-summary-format
246   :type 'integer)
247
248 (defcustom gnus-simplify-subject-fuzzy-regexp nil
249   "*Strings to be removed when doing fuzzy matches.
250 This can either be a regular expression or list of regular expressions
251 that will be removed from subject strings if fuzzy subject
252 simplification is selected."
253   :group 'gnus-thread
254   :type '(repeat regexp))
255
256 (defcustom gnus-show-threads t
257   "*If non-nil, display threads in summary mode."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-hide-subtree nil
262   "*If non-nil, hide all threads initially.
263 This can be a predicate specifier which says which threads to hide.
264 If threads are hidden, you have to run the command
265 `gnus-summary-show-thread' by hand or select an article."
266   :group 'gnus-thread
267   :type '(radio (sexp :format "Non-nil\n"
268                       :match (lambda (widget value)
269                                (not (or (consp value) (functionp value))))
270                       :value t)
271                 (const nil)
272                 (sexp :tag "Predicate specifier")))
273
274 (defcustom gnus-thread-hide-killed t
275   "*If non-nil, hide killed threads automatically."
276   :group 'gnus-thread
277   :type 'boolean)
278
279 (defcustom gnus-thread-ignore-subject t
280   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
281 If nil, articles that have different subjects from their parents will
282 start separate threads."
283   :group 'gnus-thread
284   :type 'boolean)
285
286 (defcustom gnus-thread-operation-ignore-subject t
287   "*If non-nil, subjects will be ignored when doing thread commands.
288 This affects commands like `gnus-summary-kill-thread' and
289 `gnus-summary-lower-thread'.
290
291 If this variable is nil, articles in the same thread with different
292 subjects will not be included in the operation in question.  If this
293 variable is `fuzzy', only articles that have subjects that are fuzzily
294 equal will be included."
295   :group 'gnus-thread
296   :type '(choice (const :tag "off" nil)
297                  (const fuzzy)
298                  (sexp :tag "on" t)))
299
300 (defcustom gnus-thread-indent-level 4
301   "*Number that says how much each sub-thread should be indented."
302   :group 'gnus-thread
303   :type 'integer)
304
305 (defcustom gnus-auto-extend-newsgroup t
306   "*If non-nil, extend newsgroup forward and backward when requested."
307   :group 'gnus-summary-choose
308   :type 'boolean)
309
310 (defcustom gnus-auto-select-first t
311   "*If non-nil, select the article under point.
312 Which article this is is controlled by the `gnus-auto-select-subject'
313 variable.
314
315 If you want to prevent automatic selection of articles in some
316 newsgroups, set the variable to nil in `gnus-select-group-hook'."
317   :group 'gnus-group-select
318   :type '(choice (const :tag "none" nil)
319                  (sexp :menu-tag "first" t)))
320
321 (defcustom gnus-auto-select-subject 'unread
322   "*Says what subject to place under point when entering a group.
323
324 This variable can either be the symbols `first' (place point on the
325 first subject), `unread' (place point on the subject line of the first
326 unread article), `best' (place point on the subject line of the
327 higest-scored article), `unseen' (place point on the subject line of
328 the first unseen article), `unseen-or-unread' (place point on the subject
329 line of the first unseen article or, if all article have been seen, on the
330 subject line of the first unread article), or a function to be called to
331 place point on some subject line."
332   :version "22.1"
333   :group 'gnus-group-select
334   :type '(choice (const best)
335                  (const unread)
336                  (const first)
337                  (const unseen)
338                  (const unseen-or-unread)))
339
340 (defcustom gnus-auto-select-next t
341   "*If non-nil, offer to go to the next group from the end of the previous.
342 If the value is t and the next newsgroup is empty, Gnus will exit
343 summary mode and go back to group mode.  If the value is neither nil
344 nor t, Gnus will select the following unread newsgroup.  In
345 particular, if the value is the symbol `quietly', the next unread
346 newsgroup will be selected without any confirmation, and if it is
347 `almost-quietly', the next group will be selected without any
348 confirmation if you are located on the last article in the group.
349 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
350 will go to the next group without confirmation."
351   :group 'gnus-summary-maneuvering
352   :type '(choice (const :tag "off" nil)
353                  (const quietly)
354                  (const almost-quietly)
355                  (const slightly-quietly)
356                  (sexp :menu-tag "on" t)))
357
358 (defcustom gnus-auto-select-same nil
359   "*If non-nil, select the next article with the same subject.
360 If there are no more articles with the same subject, go to
361 the first unread article."
362   :group 'gnus-summary-maneuvering
363   :type 'boolean)
364
365 (defcustom gnus-auto-goto-ignores 'unfetched
366   "*Says how to handle unfetched articles when maneuvering.
367
368 This variable can either be the symbols nil (maneuver to any
369 article), `undownloaded' (maneuvering while unplugged ignores articles
370 that have not been fetched), `always-undownloaded' (maneuvering always
371 ignores articles that have not been fetched), `unfetched' (maneuvering
372 ignores articles whose headers have not been fetched).
373
374 NOTE: The list of unfetched articles will always be nil when plugged
375 and, when unplugged, a subset of the undownloaded article list."
376   :version "22.1"
377   :group 'gnus-summary-maneuvering
378   :type '(choice (const :tag "None" nil)
379                  (const :tag "Undownloaded when unplugged" undownloaded)
380                  (const :tag "Undownloaded" always-undownloaded)
381                  (const :tag "Unfetched" unfetched)))
382
383 (defcustom gnus-summary-check-current nil
384   "*If non-nil, consider the current article when moving.
385 The \"unread\" movement commands will stay on the same line if the
386 current article is unread."
387   :group 'gnus-summary-maneuvering
388   :type 'boolean)
389
390 (defcustom gnus-auto-center-summary 2
391   "*If non-nil, always center the current summary buffer.
392 In particular, if `vertical' do only vertical recentering.  If non-nil
393 and non-`vertical', do both horizontal and vertical recentering."
394   :group 'gnus-summary-maneuvering
395   :type '(choice (const :tag "none" nil)
396                  (const vertical)
397                  (integer :tag "height")
398                  (sexp :menu-tag "both" t)))
399
400 (defvar gnus-auto-center-group t
401   "*If non-nil, always center the group buffer.")
402
403 (defcustom gnus-show-all-headers nil
404   "*If non-nil, don't hide any headers."
405   :group 'gnus-article-hiding
406   :group 'gnus-article-headers
407   :type 'boolean)
408
409 (defcustom gnus-summary-ignore-duplicates nil
410   "*If non-nil, ignore articles with identical Message-ID headers."
411   :group 'gnus-summary
412   :type 'boolean)
413
414 (defcustom gnus-single-article-buffer t
415   "*If non-nil, display all articles in the same buffer.
416 If nil, each group will get its own article buffer."
417   :group 'gnus-article-various
418   :type 'boolean)
419
420 (defcustom gnus-break-pages t
421   "*If non-nil, do page breaking on articles.
422 The page delimiter is specified by the `gnus-page-delimiter'
423 variable."
424   :group 'gnus-article-various
425   :type 'boolean)
426
427 (defcustom gnus-move-split-methods nil
428   "*Variable used to suggest where articles are to be moved to.
429 It uses the same syntax as the `gnus-split-methods' variable.
430 However, whereas `gnus-split-methods' specifies file names as targets,
431 this variable specifies group names."
432   :group 'gnus-summary-mail
433   :type '(repeat (choice (list :value (fun) function)
434                          (cons :value ("" "") regexp (repeat string))
435                          (sexp :value nil))))
436
437 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
438   "Function used to compute default prefix for article move/copy/etc prompts.
439 The function should take one argument, a group name, and return a
440 string with the suggested prefix."
441   :group 'gnus-summary-mail
442   :type 'function)
443
444 ;; FIXME: Although the custom type is `character' for the following variables,
445 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
446
447 (defcustom gnus-unread-mark ?           ;Whitespace
448   "*Mark used for unread articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-ticked-mark ?!
453   "*Mark used for ticked articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-dormant-mark ??
458   "*Mark used for dormant articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-del-mark ?r
463   "*Mark used for del'd articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-read-mark ?R
468   "*Mark used for read articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-expirable-mark ?E
473   "*Mark used for expirable articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-killed-mark ?K
478   "*Mark used for killed articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-spam-mark ?$
483   "*Mark used for spam articles."
484   :version "22.1"
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-souped-mark ?F
489   "*Mark used for souped articles."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-kill-file-mark ?X
494   "*Mark used for articles killed by kill files."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-low-score-mark ?Y
499   "*Mark used for articles with a low score."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-catchup-mark ?C
504   "*Mark used for articles that are caught up."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-replied-mark ?A
509   "*Mark used for articles that have been replied to."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-forwarded-mark ?F
514   "*Mark used for articles that have been forwarded."
515   :version "22.1"
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-recent-mark ?N
520   "*Mark used for articles that are recent."
521   :version "22.1"
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-cached-mark ?*
526   "*Mark used for articles that are in the cache."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-saved-mark ?S
531   "*Mark used for articles that have been saved."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-unseen-mark ?.
536   "*Mark used for articles that haven't been seen."
537   :version "22.1"
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-no-mark ?               ;Whitespace
542   "*Mark used for articles that have no other secondary mark."
543   :version "22.1"
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-ancient-mark ?O
548   "*Mark used for ancient articles."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-sparse-mark ?Q
553   "*Mark used for sparsely reffed articles."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-canceled-mark ?G
558   "*Mark used for canceled articles."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-duplicate-mark ?M
563   "*Mark used for duplicate articles."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-undownloaded-mark ?-
568   "*Mark used for articles that weren't downloaded."
569   :version "22.1"
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-downloaded-mark ?+
574   "*Mark used for articles that were downloaded."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-downloadable-mark ?%
579   "*Mark used for articles that are to be downloaded."
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-unsendable-mark ?=
584   "*Mark used for articles that won't be sent."
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-score-over-mark ?+
589   "*Score mark used for articles with high scores."
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-score-below-mark ?-
594   "*Score mark used for articles with low scores."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-empty-thread-mark ?     ;Whitespace
599   "*There is no thread under the article."
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-not-empty-thread-mark ?=
604   "*There is a thread under the article."
605   :group 'gnus-summary-marks
606   :type 'character)
607
608 (defcustom gnus-view-pseudo-asynchronously nil
609   "*If non-nil, Gnus will view pseudo-articles asynchronously."
610   :group 'gnus-extract-view
611   :type 'boolean)
612
613 (defcustom gnus-auto-expirable-marks
614   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
615         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
616         gnus-souped-mark gnus-duplicate-mark)
617   "*The list of marks converted into expiration if a group is auto-expirable."
618   :version "21.1"
619   :group 'gnus-summary
620   :type '(repeat character))
621
622 (defcustom gnus-inhibit-user-auto-expire t
623   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
624   :version "21.1"
625   :group 'gnus-summary
626   :type 'boolean)
627
628 (defcustom gnus-view-pseudos nil
629   "*If `automatic', pseudo-articles will be viewed automatically.
630 If `not-confirm', pseudos will be viewed automatically, and the user
631 will not be asked to confirm the command."
632   :group 'gnus-extract-view
633   :type '(choice (const :tag "off" nil)
634                  (const automatic)
635                  (const not-confirm)))
636
637 (defcustom gnus-view-pseudos-separately t
638   "*If non-nil, one pseudo-article will be created for each file to be viewed.
639 If nil, all files that use the same viewing command will be given as a
640 list of parameters to that command."
641   :group 'gnus-extract-view
642   :type 'boolean)
643
644 (defcustom gnus-insert-pseudo-articles t
645   "*If non-nil, insert pseudo-articles when decoding articles."
646   :group 'gnus-extract-view
647   :type 'boolean)
648
649 (defcustom gnus-summary-dummy-line-format
650   "   %(:                             :%) %S\n"
651   "*The format specification for the dummy roots in the summary buffer.
652 It works along the same lines as a normal formatting string,
653 with some simple extensions.
654
655 %S  The subject
656
657 General format specifiers can also be used.
658 See `(gnus)Formatting Variables'."
659   :link '(custom-manual "(gnus)Formatting Variables")
660   :group 'gnus-threading
661   :type 'string)
662
663 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
664   "*The format specification for the summary mode line.
665 It works along the same lines as a normal formatting string,
666 with some simple extensions:
667
668 %G  Group name
669 %p  Unprefixed group name
670 %A  Current article number
671 %z  Current article score
672 %V  Gnus version
673 %U  Number of unread articles in the group
674 %e  Number of unselected articles in the group
675 %Z  A string with unread/unselected article counts
676 %g  Shortish group name
677 %S  Subject of the current article
678 %u  User-defined spec
679 %s  Current score file name
680 %d  Number of dormant articles
681 %r  Number of articles that have been marked as read in this session
682 %E  Number of articles expunged by the score files"
683   :group 'gnus-summary-format
684   :type 'string)
685
686 (defcustom gnus-list-identifiers nil
687   "Regexp that matches list identifiers to be removed from subject.
688 This can also be a list of regexps."
689   :version "21.1"
690   :group 'gnus-summary-format
691   :group 'gnus-article-hiding
692   :type '(choice (const :tag "none" nil)
693                  (regexp :value ".*")
694                  (repeat :value (".*") regexp)))
695
696 (defcustom gnus-summary-mark-below 0
697   "*Mark all articles with a score below this variable as read.
698 This variable is local to each summary buffer and usually set by the
699 score file."
700   :group 'gnus-score-default
701   :type 'integer)
702
703 (defun gnus-widget-reversible-match (widget value)
704   "Ignoring WIDGET, convert VALUE to internal form.
705 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
706   ;; (debug value)
707   (or (symbolp value)
708       (and (listp value)
709            (eq (length value) 2)
710            (eq (nth 0 value) 'not)
711            (symbolp (nth 1 value)))))
712
713 (defun gnus-widget-reversible-to-internal (widget value)
714   "Ignoring WIDGET, convert VALUE to internal form.
715 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
716 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
717   ;; (debug value)
718   (if (atom value)
719       (list value nil)
720     (list (nth 1 value) t)))
721
722 (defun gnus-widget-reversible-to-external (widget value)
723   "Ignoring WIDGET, convert VALUE to external form.
724 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
725 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
726   ;; (debug value)
727   (if (nth 1 value)
728       (list 'not (nth 0 value))
729     (nth 0 value)))
730
731 (define-widget 'gnus-widget-reversible 'group
732   "A `group' that convert values."
733   :match 'gnus-widget-reversible-match
734   :value-to-internal 'gnus-widget-reversible-to-internal
735   :value-to-external 'gnus-widget-reversible-to-external)
736
737 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
738   "*List of functions used for sorting articles in the summary buffer.
739
740 Each function takes two articles and returns non-nil if the first
741 article should be sorted before the other.  If you use more than one
742 function, the primary sort function should be the last.  You should
743 probably always include `gnus-article-sort-by-number' in the list of
744 sorting functions -- preferably first.  Also note that sorting by date
745 is often much slower than sorting by number, and the sorting order is
746 very similar.  (Sorting by date means sorting by the time the message
747 was sent, sorting by number means sorting by arrival time.)
748
749 Each item can also be a list `(not F)' where F is a function;
750 this reverses the sort order.
751
752 Ready-made functions include `gnus-article-sort-by-number',
753 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
754 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
755 and `gnus-article-sort-by-score'.
756
757 When threading is turned on, the variable `gnus-thread-sort-functions'
758 controls how articles are sorted."
759   :group 'gnus-summary-sort
760   :type '(repeat (gnus-widget-reversible
761                   (choice (function-item gnus-article-sort-by-number)
762                           (function-item gnus-article-sort-by-author)
763                           (function-item gnus-article-sort-by-subject)
764                           (function-item gnus-article-sort-by-date)
765                           (function-item gnus-article-sort-by-score)
766                           (function-item gnus-article-sort-by-random)
767                           (function :tag "other"))
768                   (boolean :tag "Reverse order"))))
769
770
771 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
772   "*List of functions used for sorting threads in the summary buffer.
773 By default, threads are sorted by article number.
774
775 Each function takes two threads and returns non-nil if the first
776 thread should be sorted before the other.  If you use more than one
777 function, the primary sort function should be the last.  You should
778 probably always include `gnus-thread-sort-by-number' in the list of
779 sorting functions -- preferably first.  Also note that sorting by date
780 is often much slower than sorting by number, and the sorting order is
781 very similar.  (Sorting by date means sorting by the time the message
782 was sent, sorting by number means sorting by arrival time.)
783
784 Each list item can also be a list `(not F)' where F is a
785 function; this specifies reversed sort order.
786
787 Ready-made functions include `gnus-thread-sort-by-number',
788 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
789 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
790 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
791 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
792 and `gnus-thread-sort-by-total-score' (see
793 `gnus-thread-score-function').
794
795 When threading is turned off, the variable
796 `gnus-article-sort-functions' controls how articles are sorted."
797   :group 'gnus-summary-sort
798   :type '(repeat
799           (gnus-widget-reversible
800            (choice (function-item gnus-thread-sort-by-number)
801                    (function-item gnus-thread-sort-by-author)
802                    (function-item gnus-thread-sort-by-recipient)
803                    (function-item gnus-thread-sort-by-subject)
804                    (function-item gnus-thread-sort-by-date)
805                    (function-item gnus-thread-sort-by-score)
806                    (function-item gnus-thread-sort-by-most-recent-number)
807                    (function-item gnus-thread-sort-by-most-recent-date)
808                    (function-item gnus-thread-sort-by-random)
809                    (function-item gnus-thread-sort-by-total-score)
810                    (function :tag "other"))
811            (boolean :tag "Reverse order"))))
812
813 (defcustom gnus-thread-score-function '+
814   "*Function used for calculating the total score of a thread.
815
816 The function is called with the scores of the article and each
817 subthread and should then return the score of the thread.
818
819 Some functions you can use are `+', `max', or `min'."
820   :group 'gnus-summary-sort
821   :type 'function)
822
823 (defcustom gnus-summary-expunge-below nil
824   "All articles that have a score less than this variable will be expunged.
825 This variable is local to the summary buffers."
826   :group 'gnus-score-default
827   :type '(choice (const :tag "off" nil)
828                  integer))
829
830 (defcustom gnus-thread-expunge-below nil
831   "All threads that have a total score less than this variable will be expunged.
832 See `gnus-thread-score-function' for en explanation of what a
833 \"thread score\" is.
834
835 This variable is local to the summary buffers."
836   :group 'gnus-threading
837   :group 'gnus-score-default
838   :type '(choice (const :tag "off" nil)
839                  integer))
840
841 (defcustom gnus-summary-mode-hook nil
842   "*A hook for Gnus summary mode.
843 This hook is run before any variables are set in the summary buffer."
844   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
845   :group 'gnus-summary-various
846   :type 'hook)
847
848 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
849 (when (featurep 'xemacs)
850   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
851   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
852   (add-hook 'gnus-summary-mode-hook
853             'gnus-xmas-switch-horizontal-scrollbar-off))
854
855 (defcustom gnus-summary-menu-hook nil
856   "*Hook run after the creation of the summary mode menu."
857   :group 'gnus-summary-visual
858   :type 'hook)
859
860 (defcustom gnus-summary-exit-hook nil
861   "*A hook called on exit from the summary buffer.
862 It will be called with point in the group buffer."
863   :group 'gnus-summary-exit
864   :type 'hook)
865
866 (defcustom gnus-summary-prepare-hook nil
867   "*A hook called after the summary buffer has been generated.
868 If you want to modify the summary buffer, you can use this hook."
869   :group 'gnus-summary-various
870   :type 'hook)
871
872 (defcustom gnus-summary-prepared-hook nil
873   "*A hook called as the last thing after the summary buffer has been generated."
874   :group 'gnus-summary-various
875   :type 'hook)
876
877 (defcustom gnus-summary-generate-hook nil
878   "*A hook run just before generating the summary buffer.
879 This hook is commonly used to customize threading variables and the
880 like."
881   :group 'gnus-summary-various
882   :type 'hook)
883
884 (defcustom gnus-select-group-hook nil
885   "*A hook called when a newsgroup is selected.
886
887 If you'd like to simplify subjects like the
888 `gnus-summary-next-same-subject' command does, you can use the
889 following hook:
890
891  (add-hook gnus-select-group-hook
892            (lambda ()
893              (mapcar (lambda (header)
894                        (mail-header-set-subject
895                         header
896                         (gnus-simplify-subject
897                          (mail-header-subject header) 're-only)))
898                      gnus-newsgroup-headers)))"
899   :group 'gnus-group-select
900   :type 'hook)
901
902 (defcustom gnus-select-article-hook nil
903   "*A hook called when an article is selected."
904   :group 'gnus-summary-choose
905   :options '(gnus-agent-fetch-selected-article)
906   :type 'hook)
907
908 (defcustom gnus-visual-mark-article-hook
909   (list 'gnus-highlight-selected-summary)
910   "*Hook run after selecting an article in the summary buffer.
911 It is meant to be used for highlighting the article in some way.  It
912 is not run if `gnus-visual' is nil."
913   :group 'gnus-summary-visual
914   :type 'hook)
915
916 (defcustom gnus-parse-headers-hook nil
917   "*A hook called before parsing the headers."
918   :group 'gnus-various
919   :type 'hook)
920
921 (defcustom gnus-exit-group-hook nil
922   "*A hook called when exiting summary mode.
923 This hook is not called from the non-updating exit commands like `Q'."
924   :group 'gnus-various
925   :type 'hook)
926
927 (defcustom gnus-summary-update-hook
928   (list 'gnus-summary-highlight-line)
929   "*A hook called when a summary line is changed.
930 The hook will not be called if `gnus-visual' is nil.
931
932 The default function `gnus-summary-highlight-line' will
933 highlight the line according to the `gnus-summary-highlight'
934 variable."
935   :group 'gnus-summary-visual
936   :type 'hook)
937
938 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
939   "*A hook called when an article is selected for the first time.
940 The hook is intended to mark an article as read (or unread)
941 automatically when it is selected."
942   :group 'gnus-summary-choose
943   :type 'hook)
944
945 (defcustom gnus-group-no-more-groups-hook nil
946   "*A hook run when returning to group mode having no more (unread) groups."
947   :group 'gnus-group-select
948   :type 'hook)
949
950 (defcustom gnus-ps-print-hook nil
951   "*A hook run before ps-printing something from Gnus."
952   :group 'gnus-summary
953   :type 'hook)
954
955 (defcustom gnus-summary-article-move-hook nil
956   "*A hook called after an article is moved, copied, respooled, or crossposted."
957   :version "22.1"
958   :group 'gnus-summary
959   :type 'hook)
960
961 (defcustom gnus-summary-article-delete-hook nil
962   "*A hook called after an article is deleted."
963   :version "22.1"
964   :group 'gnus-summary
965   :type 'hook)
966
967 (defcustom gnus-summary-article-expire-hook nil
968   "*A hook called after an article is expired."
969   :version "22.1"
970   :group 'gnus-summary
971   :type 'hook)
972
973 (defcustom gnus-summary-display-arrow
974   (and (fboundp 'display-graphic-p)
975        (display-graphic-p))
976   "*If non-nil, display an arrow highlighting the current article."
977   :version "22.1"
978   :group 'gnus-summary
979   :type 'boolean)
980
981 (defcustom gnus-summary-selected-face 'gnus-summary-selected
982   "Face used for highlighting the current article in the summary buffer."
983   :group 'gnus-summary-visual
984   :type 'face)
985
986 (defvar gnus-tmp-downloaded nil)
987
988 (defcustom gnus-summary-highlight
989   '(((eq mark gnus-canceled-mark)
990      . gnus-summary-cancelled)
991     ((and uncached (> score default-high))
992      . gnus-summary-high-undownloaded)
993     ((and uncached (< score default-low))
994      . gnus-summary-low-undownloaded)
995     (uncached
996      . gnus-summary-normal-undownloaded)
997     ((and (> score default-high)
998           (or (eq mark gnus-dormant-mark)
999               (eq mark gnus-ticked-mark)))
1000      . gnus-summary-high-ticked)
1001     ((and (< score default-low)
1002           (or (eq mark gnus-dormant-mark)
1003               (eq mark gnus-ticked-mark)))
1004      . gnus-summary-low-ticked)
1005     ((or (eq mark gnus-dormant-mark)
1006          (eq mark gnus-ticked-mark))
1007      . gnus-summary-normal-ticked)
1008     ((and (> score default-high) (eq mark gnus-ancient-mark))
1009      . gnus-summary-high-ancient)
1010     ((and (< score default-low) (eq mark gnus-ancient-mark))
1011      . gnus-summary-low-ancient)
1012     ((eq mark gnus-ancient-mark)
1013      . gnus-summary-normal-ancient)
1014     ((and (> score default-high) (eq mark gnus-unread-mark))
1015      . gnus-summary-high-unread)
1016     ((and (< score default-low) (eq mark gnus-unread-mark))
1017      . gnus-summary-low-unread)
1018     ((eq mark gnus-unread-mark)
1019      . gnus-summary-normal-unread)
1020     ((> score default-high)
1021      . gnus-summary-high-read)
1022     ((< score default-low)
1023      . gnus-summary-low-read)
1024     (t
1025      . gnus-summary-normal-read))
1026   "*Controls the highlighting of summary buffer lines.
1027
1028 A list of (FORM . FACE) pairs.  When deciding how a a particular
1029 summary line should be displayed, each form is evaluated.  The content
1030 of the face field after the first true form is used.  You can change
1031 how those summary lines are displayed, by editing the face field.
1032
1033 You can use the following variables in the FORM field.
1034
1035 score:        The article's score
1036 default:      The default article score.
1037 default-high: The default score for high scored articles.
1038 default-low:  The default score for low scored articles.
1039 below:        The score below which articles are automatically marked as read.
1040 mark:         The article's mark.
1041 uncached:     Non-nil if the article is uncached."
1042   :group 'gnus-summary-visual
1043   :type '(repeat (cons (sexp :tag "Form" nil)
1044                        face)))
1045
1046 (defcustom gnus-alter-header-function nil
1047   "Function called to allow alteration of article header structures.
1048 The function is called with one parameter, the article header vector,
1049 which it may alter in any way."
1050   :type '(choice (const :tag "None" nil)
1051                  function)
1052   :group 'gnus-summary)
1053
1054 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1055   "Function used to decode a string with encoded words.")
1056
1057 (defvar gnus-decode-encoded-address-function
1058   'mail-decode-encoded-address-string
1059   "Function used to decode addresses with encoded words.")
1060
1061 (defcustom gnus-extra-headers '(To Newsgroups)
1062   "*Extra headers to parse."
1063   :version "21.1"
1064   :group 'gnus-summary
1065   :type '(repeat symbol))
1066
1067 (defcustom gnus-ignored-from-addresses
1068   (and user-mail-address
1069        (not (string= user-mail-address ""))
1070        (regexp-quote user-mail-address))
1071   "*Regexp of From headers that may be suppressed in favor of To headers."
1072   :version "21.1"
1073   :group 'gnus-summary
1074   :type 'regexp)
1075
1076 (defcustom gnus-summary-to-prefix "-> "
1077   "*String prefixed to the To field in the summary line when
1078 using `gnus-ignored-from-addresses'."
1079   :version "22.1"
1080   :group 'gnus-summary
1081   :type 'string)
1082
1083 (defcustom gnus-summary-newsgroup-prefix "=> "
1084   "*String prefixed to the Newsgroup field in the summary
1085 line when using `gnus-ignored-from-addresses'."
1086   :version "22.1"
1087   :group 'gnus-summary
1088   :type 'string)
1089
1090 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1091   "List of charsets that should be ignored.
1092 When these charsets are used in the \"charset\" parameter, the
1093 default charset will be used instead."
1094   :version "21.1"
1095   :type '(repeat symbol)
1096   :group 'gnus-charset)
1097
1098 (gnus-define-group-parameter
1099  ignored-charsets
1100  :type list
1101  :function-document
1102  "Return the ignored charsets of GROUP."
1103  :variable gnus-group-ignored-charsets-alist
1104  :variable-default
1105  '(("alt\\.chinese\\.text" iso-8859-1))
1106  :variable-document
1107  "Alist of regexps (to match group names) and charsets that should be ignored.
1108 When these charsets are used in the \"charset\" parameter, the
1109 default charset will be used instead."
1110  :variable-group gnus-charset
1111  :variable-type '(repeat (cons (regexp :tag "Group")
1112                                (repeat symbol)))
1113  :parameter-type '(choice :tag "Ignored charsets"
1114                           :value nil
1115                           (repeat (symbol)))
1116  :parameter-document       "\
1117 List of charsets that should be ignored.
1118
1119 When these charsets are used in the \"charset\" parameter, the
1120 default charset will be used instead.")
1121
1122 (defcustom gnus-group-highlight-words-alist nil
1123   "Alist of group regexps and highlight regexps.
1124 This variable uses the same syntax as `gnus-emphasis-alist'."
1125   :version "21.1"
1126   :type '(repeat (cons (regexp :tag "Group")
1127                        (repeat (list (regexp :tag "Highlight regexp")
1128                                      (number :tag "Group for entire word" 0)
1129                                      (number :tag "Group for displayed part" 0)
1130                                      (symbol :tag "Face"
1131                                              gnus-emphasis-highlight-words)))))
1132   :group 'gnus-summary-visual)
1133
1134 (defcustom gnus-summary-show-article-charset-alist
1135   nil
1136   "Alist of number and charset.
1137 The article will be shown with the charset corresponding to the
1138 numbered argument.
1139 For example: ((1 . cn-gb-2312) (2 . big5))."
1140   :version "21.1"
1141   :type '(repeat (cons (number :tag "Argument" 1)
1142                        (symbol :tag "Charset")))
1143   :group 'gnus-charset)
1144
1145 (defcustom gnus-preserve-marks t
1146   "Whether marks are preserved when moving, copying and respooling messages."
1147   :version "21.1"
1148   :type 'boolean
1149   :group 'gnus-summary-marks)
1150
1151 (defcustom gnus-alter-articles-to-read-function nil
1152   "Function to be called to alter the list of articles to be selected."
1153   :type '(choice (const nil) function)
1154   :group 'gnus-summary)
1155
1156 (defcustom gnus-orphan-score nil
1157   "*All orphans get this score added.  Set in the score file."
1158   :group 'gnus-score-default
1159   :type '(choice (const nil)
1160                  integer))
1161
1162 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1163   "*A regexp to match MIME parts when saving multiple parts of a
1164 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1165 This regexp will be used by default when prompting the user for which
1166 type of files to save."
1167   :group 'gnus-summary
1168   :type 'regexp)
1169
1170 (defcustom gnus-read-all-available-headers nil
1171   "Whether Gnus should parse all headers made available to it.
1172 This is mostly relevant for slow back ends where the user may
1173 wish to widen the summary buffer to include all headers
1174 that were fetched.  Say, for nnultimate groups."
1175   :version "22.1"
1176   :group 'gnus-summary
1177   :type '(choice boolean regexp))
1178
1179 (defcustom gnus-summary-muttprint-program "muttprint"
1180   "Command (and optional arguments) used to run Muttprint."
1181   :version "22.1"
1182   :group 'gnus-summary
1183   :type 'string)
1184
1185 (defcustom gnus-article-loose-mime t
1186   "If non-nil, don't require MIME-Version header.
1187 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1188 supply the MIME-Version header or deliberately strip it from the mail.
1189 If non-nil (the default), Gnus will treat some articles as MIME
1190 even if the MIME-Version header is missing."
1191   :version "22.1"
1192   :type 'boolean
1193   :group 'gnus-article-mime)
1194
1195 (defcustom gnus-article-emulate-mime t
1196   "If non-nil, use MIME emulation for uuencode and the like.
1197 This means that Gnus will search message bodies for text that look
1198 like uuencoded bits, yEncoded bits, and so on, and present that using
1199 the normal Gnus MIME machinery."
1200   :version "22.1"
1201   :type 'boolean
1202   :group 'gnus-article-mime)
1203
1204 ;;; Internal variables
1205
1206 (defvar gnus-summary-display-cache nil)
1207 (defvar gnus-article-mime-handles nil)
1208 (defvar gnus-article-decoded-p nil)
1209 (defvar gnus-article-charset nil)
1210 (defvar gnus-article-ignored-charsets nil)
1211 (defvar gnus-scores-exclude-files nil)
1212 (defvar gnus-page-broken nil)
1213
1214 (defvar gnus-original-article nil)
1215 (defvar gnus-article-internal-prepare-hook nil)
1216 (defvar gnus-newsgroup-process-stack nil)
1217
1218 (defvar gnus-thread-indent-array nil)
1219 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1220 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1221   "Function called to sort the articles within a thread after it has been gathered together.")
1222
1223 (defvar gnus-summary-save-parts-type-history nil)
1224 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1225
1226 ;; Avoid highlighting in kill files.
1227 (defvar gnus-summary-inhibit-highlight nil)
1228 (defvar gnus-newsgroup-selected-overlay nil)
1229 (defvar gnus-inhibit-limiting nil)
1230 (defvar gnus-newsgroup-adaptive-score-file nil)
1231 (defvar gnus-current-score-file nil)
1232 (defvar gnus-current-move-group nil)
1233 (defvar gnus-current-copy-group nil)
1234 (defvar gnus-current-crosspost-group nil)
1235 (defvar gnus-newsgroup-display nil)
1236
1237 (defvar gnus-newsgroup-dependencies nil)
1238 (defvar gnus-newsgroup-adaptive nil)
1239 (defvar gnus-summary-display-article-function nil)
1240 (defvar gnus-summary-highlight-line-function nil
1241   "Function called after highlighting a summary line.")
1242
1243 (defvar gnus-summary-line-format-alist
1244   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1245     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1246     (?s gnus-tmp-subject-or-nil ?s)
1247     (?n gnus-tmp-name ?s)
1248     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1249         ?s)
1250     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1251             gnus-tmp-from) ?s)
1252     (?F gnus-tmp-from ?s)
1253     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1254     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1255     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1256     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1257     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1258     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1259     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1260     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1261     (?L gnus-tmp-lines ?s)
1262     (?O gnus-tmp-downloaded ?c)
1263     (?I gnus-tmp-indentation ?s)
1264     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1265     (?R gnus-tmp-replied ?c)
1266     (?\[ gnus-tmp-opening-bracket ?c)
1267     (?\] gnus-tmp-closing-bracket ?c)
1268     (?\> (make-string gnus-tmp-level ? ) ?s)
1269     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1270     (?i gnus-tmp-score ?d)
1271     (?z gnus-tmp-score-char ?c)
1272     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1273     (?U gnus-tmp-unread ?c)
1274     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1275         ?s)
1276     (?t (gnus-summary-number-of-articles-in-thread
1277          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1278         ?d)
1279     (?e (gnus-summary-number-of-articles-in-thread
1280          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1281         ?c)
1282     (?u gnus-tmp-user-defined ?s)
1283     (?P (gnus-pick-line-number) ?d)
1284     (?B gnus-tmp-thread-tree-header-string ?s)
1285     (user-date (gnus-user-date
1286                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1287   "An alist of format specifications that can appear in summary lines.
1288 These are paired with what variables they correspond with, along with
1289 the type of the variable (string, integer, character, etc).")
1290
1291 (defvar gnus-summary-dummy-line-format-alist
1292   `((?S gnus-tmp-subject ?s)
1293     (?N gnus-tmp-number ?d)
1294     (?u gnus-tmp-user-defined ?s)))
1295
1296 (defvar gnus-summary-mode-line-format-alist
1297   `((?G gnus-tmp-group-name ?s)
1298     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1299     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1300     (?A gnus-tmp-article-number ?d)
1301     (?Z gnus-tmp-unread-and-unselected ?s)
1302     (?V gnus-version ?s)
1303     (?U gnus-tmp-unread-and-unticked ?d)
1304     (?S gnus-tmp-subject ?s)
1305     (?e gnus-tmp-unselected ?d)
1306     (?u gnus-tmp-user-defined ?s)
1307     (?d (length gnus-newsgroup-dormant) ?d)
1308     (?t (length gnus-newsgroup-marked) ?d)
1309     (?h (length gnus-newsgroup-spam-marked) ?d)
1310     (?r (length gnus-newsgroup-reads) ?d)
1311     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1312     (?E gnus-newsgroup-expunged-tally ?d)
1313     (?s (gnus-current-score-file-nondirectory) ?s)))
1314
1315 (defvar gnus-last-search-regexp nil
1316   "Default regexp for article search command.")
1317
1318 (defvar gnus-last-shell-command nil
1319   "Default shell command on article.")
1320
1321 (defvar gnus-newsgroup-agentized nil
1322   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1323 (defvar gnus-newsgroup-begin nil)
1324 (defvar gnus-newsgroup-end nil)
1325 (defvar gnus-newsgroup-last-rmail nil)
1326 (defvar gnus-newsgroup-last-mail nil)
1327 (defvar gnus-newsgroup-last-folder nil)
1328 (defvar gnus-newsgroup-last-file nil)
1329 (defvar gnus-newsgroup-last-directory nil)
1330 (defvar gnus-newsgroup-auto-expire nil)
1331 (defvar gnus-newsgroup-active nil)
1332
1333 (defvar gnus-newsgroup-data nil)
1334 (defvar gnus-newsgroup-data-reverse nil)
1335 (defvar gnus-newsgroup-limit nil)
1336 (defvar gnus-newsgroup-limits nil)
1337 (defvar gnus-summary-use-undownloaded-faces nil)
1338
1339 (defvar gnus-newsgroup-unreads nil
1340   "Sorted list of unread articles in the current newsgroup.")
1341
1342 (defvar gnus-newsgroup-unselected nil
1343   "Sorted list of unselected unread articles in the current newsgroup.")
1344
1345 (defvar gnus-newsgroup-reads nil
1346   "Alist of read articles and article marks in the current newsgroup.")
1347
1348 (defvar gnus-newsgroup-expunged-tally nil)
1349
1350 (defvar gnus-newsgroup-marked nil
1351   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1352
1353 (defvar gnus-newsgroup-spam-marked nil
1354   "List of ranges of articles that have been marked as spam.")
1355
1356 (defvar gnus-newsgroup-killed nil
1357   "List of ranges of articles that have been through the scoring process.")
1358
1359 (defvar gnus-newsgroup-cached nil
1360   "Sorted list of articles that come from the article cache.")
1361
1362 (defvar gnus-newsgroup-saved nil
1363   "List of articles that have been saved.")
1364
1365 (defvar gnus-newsgroup-kill-headers nil)
1366
1367 (defvar gnus-newsgroup-replied nil
1368   "List of articles that have been replied to in the current newsgroup.")
1369
1370 (defvar gnus-newsgroup-forwarded nil
1371   "List of articles that have been forwarded in the current newsgroup.")
1372
1373 (defvar gnus-newsgroup-recent nil
1374   "List of articles that have are recent in the current newsgroup.")
1375
1376 (defvar gnus-newsgroup-expirable nil
1377   "Sorted list of articles in the current newsgroup that can be expired.")
1378
1379 (defvar gnus-newsgroup-processable nil
1380   "List of articles in the current newsgroup that can be processed.")
1381
1382 (defvar gnus-newsgroup-downloadable nil
1383   "Sorted list of articles in the current newsgroup that can be processed.")
1384
1385 (defvar gnus-newsgroup-unfetched nil
1386   "Sorted list of articles in the current newsgroup whose headers have
1387 not been fetched into the agent.
1388
1389 This list will always be a subset of gnus-newsgroup-undownloaded.")
1390
1391 (defvar gnus-newsgroup-undownloaded nil
1392   "List of articles in the current newsgroup that haven't been downloaded.")
1393
1394 (defvar gnus-newsgroup-unsendable nil
1395   "List of articles in the current newsgroup that won't be sent.")
1396
1397 (defvar gnus-newsgroup-bookmarks nil
1398   "List of articles in the current newsgroup that have bookmarks.")
1399
1400 (defvar gnus-newsgroup-dormant nil
1401   "Sorted list of dormant articles in the current newsgroup.")
1402
1403 (defvar gnus-newsgroup-unseen nil
1404   "List of unseen articles in the current newsgroup.")
1405
1406 (defvar gnus-newsgroup-seen nil
1407   "Range of seen articles in the current newsgroup.")
1408
1409 (defvar gnus-newsgroup-articles nil
1410   "List of articles in the current newsgroup.")
1411
1412 (defvar gnus-newsgroup-scored nil
1413   "List of scored articles in the current newsgroup.")
1414
1415 (defvar gnus-newsgroup-headers nil
1416   "List of article headers in the current newsgroup.")
1417
1418 (defvar gnus-newsgroup-threads nil)
1419
1420 (defvar gnus-newsgroup-prepared nil
1421   "Whether the current group has been prepared properly.")
1422
1423 (defvar gnus-newsgroup-ancient nil
1424   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1425
1426 (defvar gnus-newsgroup-sparse nil)
1427
1428 (defvar gnus-current-article nil)
1429 (defvar gnus-article-current nil)
1430 (defvar gnus-current-headers nil)
1431 (defvar gnus-have-all-headers nil)
1432 (defvar gnus-last-article nil)
1433 (defvar gnus-newsgroup-history nil)
1434 (defvar gnus-newsgroup-charset nil)
1435 (defvar gnus-newsgroup-ephemeral-charset nil)
1436 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1437
1438 (defvar gnus-article-before-search nil)
1439
1440 (defvar gnus-summary-local-variables
1441   '(gnus-newsgroup-name
1442     gnus-newsgroup-begin gnus-newsgroup-end
1443     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1444     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1445     gnus-newsgroup-last-directory
1446     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1447     gnus-newsgroup-unselected gnus-newsgroup-marked
1448     gnus-newsgroup-spam-marked
1449     gnus-newsgroup-reads gnus-newsgroup-saved
1450     gnus-newsgroup-replied gnus-newsgroup-forwarded
1451     gnus-newsgroup-recent
1452     gnus-newsgroup-expirable
1453     gnus-newsgroup-processable gnus-newsgroup-killed
1454     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1455     gnus-newsgroup-unfetched
1456     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1457     gnus-newsgroup-seen gnus-newsgroup-articles
1458     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1459     gnus-newsgroup-headers gnus-newsgroup-threads
1460     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1461     gnus-current-article gnus-current-headers gnus-have-all-headers
1462     gnus-last-article gnus-article-internal-prepare-hook
1463     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1464     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1465     gnus-thread-expunge-below
1466     gnus-score-alist gnus-current-score-file
1467     (gnus-summary-expunge-below . global)
1468     (gnus-summary-mark-below . global)
1469     (gnus-orphan-score . global)
1470     gnus-newsgroup-active gnus-scores-exclude-files
1471     gnus-newsgroup-history gnus-newsgroup-ancient
1472     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1473     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1474     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1475     (gnus-newsgroup-expunged-tally . 0)
1476     gnus-cache-removable-articles gnus-newsgroup-cached
1477     gnus-newsgroup-data gnus-newsgroup-data-reverse
1478     gnus-newsgroup-limit gnus-newsgroup-limits
1479     gnus-newsgroup-charset gnus-newsgroup-display
1480     gnus-summary-use-undownloaded-faces)
1481   "Variables that are buffer-local to the summary buffers.")
1482
1483 (defvar gnus-newsgroup-variables nil
1484   "A list of variables that have separate values in different newsgroups.
1485 A list of newsgroup (summary buffer) local variables, or cons of
1486 variables and their default expressions to be evalled (when the default
1487 values are not nil), that should be made global while the summary buffer
1488 is active.
1489
1490 Note: The default expressions will be evaluated (using function `eval')
1491 before assignment to the local variable rather than just assigned to it.
1492 If the default expression is the symbol `global', that symbol will not
1493 be evaluated but the global value of the local variable will be used
1494 instead.
1495
1496 These variables can be used to set variables in the group parameters
1497 while still allowing them to affect operations done in other buffers.
1498 For example:
1499
1500 \(setq gnus-newsgroup-variables
1501      '(message-use-followup-to
1502        (gnus-visible-headers .
1503          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1504 ")
1505
1506 ;; Byte-compiler warning.
1507 (eval-when-compile
1508   ;; Bind features so that require will believe that gnus-sum has
1509   ;; already been loaded (avoids infinite recursion)
1510   (let ((features (cons 'gnus-sum features)))
1511     ;; Several of the declarations in gnus-sum are needed to load the
1512     ;; following files. Right now, these definitions have been
1513     ;; compiled but not defined (evaluated).  We could either do a
1514     ;; eval-and-compile about all of the declarations or evaluate the
1515     ;; source file.
1516     (if (boundp 'gnus-newsgroup-variables)
1517         nil
1518       (load "gnus-sum.el" t t t))
1519     (require 'gnus)
1520     (require 'gnus-art)))
1521
1522 ;; MIME stuff.
1523
1524 (defvar gnus-decode-encoded-word-methods
1525   '(mail-decode-encoded-word-string)
1526   "List of methods used to decode encoded words.
1527
1528 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1529 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1530 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1531 whose names match REGEXP.
1532
1533 For example:
1534 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1535  mail-decode-encoded-word-string
1536  (\"chinese\" . rfc1843-decode-string))")
1537
1538 (defvar gnus-decode-encoded-word-methods-cache nil)
1539
1540 (defun gnus-multi-decode-encoded-word-string (string)
1541   "Apply the functions from `gnus-encoded-word-methods' that match."
1542   (unless (and gnus-decode-encoded-word-methods-cache
1543                (eq gnus-newsgroup-name
1544                    (car gnus-decode-encoded-word-methods-cache)))
1545     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1546     (mapcar (lambda (x)
1547               (if (symbolp x)
1548                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1549                 (if (and gnus-newsgroup-name
1550                          (string-match (car x) gnus-newsgroup-name))
1551                     (nconc gnus-decode-encoded-word-methods-cache
1552                            (list (cdr x))))))
1553             gnus-decode-encoded-word-methods))
1554   (let ((xlist gnus-decode-encoded-word-methods-cache))
1555     (pop xlist)
1556     (while xlist
1557       (setq string (funcall (pop xlist) string))))
1558   string)
1559
1560 ;; Subject simplification.
1561
1562 (defun gnus-simplify-whitespace (str)
1563   "Remove excessive whitespace from STR."
1564   ;; Multiple spaces.
1565   (while (string-match "[ \t][ \t]+" str)
1566     (setq str (concat (substring str 0 (match-beginning 0))
1567                         " "
1568                         (substring str (match-end 0)))))
1569   ;; Leading spaces.
1570   (when (string-match "^[ \t]+" str)
1571     (setq str (substring str (match-end 0))))
1572   ;; Trailing spaces.
1573   (when (string-match "[ \t]+$" str)
1574     (setq str (substring str 0 (match-beginning 0))))
1575   str)
1576
1577 (defun gnus-simplify-all-whitespace (str)
1578   "Remove all whitespace from STR."
1579   (while (string-match "[ \t\n]+" str)
1580     (setq str (replace-match "" nil nil str)))
1581   str)
1582
1583 (defsubst gnus-simplify-subject-re (subject)
1584   "Remove \"Re:\" from subject lines."
1585   (if (string-match message-subject-re-regexp subject)
1586       (substring subject (match-end 0))
1587     subject))
1588
1589 (defun gnus-simplify-subject (subject &optional re-only)
1590   "Remove `Re:' and words in parentheses.
1591 If RE-ONLY is non-nil, strip leading `Re:'s only."
1592   (let ((case-fold-search t))           ;Ignore case.
1593     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1594     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1595       (setq subject (substring subject (match-end 0))))
1596     ;; Remove uninteresting prefixes.
1597     (when (and (not re-only)
1598                gnus-simplify-ignored-prefixes
1599                (string-match gnus-simplify-ignored-prefixes subject))
1600       (setq subject (substring subject (match-end 0))))
1601     ;; Remove words in parentheses from end.
1602     (unless re-only
1603       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1604         (setq subject (substring subject 0 (match-beginning 0)))))
1605     ;; Return subject string.
1606     subject))
1607
1608 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1609 ;; all whitespace.
1610 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1611   (goto-char (point-min))
1612   (while (re-search-forward regexp nil t)
1613     (replace-match (or newtext ""))))
1614
1615 (defun gnus-simplify-buffer-fuzzy ()
1616   "Simplify string in the buffer fuzzily.
1617 The string in the accessible portion of the current buffer is simplified.
1618 It is assumed to be a single-line subject.
1619 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1620 matter is removed.  Additional things can be deleted by setting
1621 `gnus-simplify-subject-fuzzy-regexp'."
1622   (let ((case-fold-search t)
1623         (modified-tick))
1624     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1625
1626     (while (not (eq modified-tick (buffer-modified-tick)))
1627       (setq modified-tick (buffer-modified-tick))
1628       (cond
1629        ((listp gnus-simplify-subject-fuzzy-regexp)
1630         (mapcar 'gnus-simplify-buffer-fuzzy-step
1631                 gnus-simplify-subject-fuzzy-regexp))
1632        (gnus-simplify-subject-fuzzy-regexp
1633         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1634       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1635       (gnus-simplify-buffer-fuzzy-step
1636        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1637       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1638
1639     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1640     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1641     (gnus-simplify-buffer-fuzzy-step " $")
1642     (gnus-simplify-buffer-fuzzy-step "^ +")))
1643
1644 (defun gnus-simplify-subject-fuzzy (subject)
1645   "Simplify a subject string fuzzily.
1646 See `gnus-simplify-buffer-fuzzy' for details."
1647   (save-excursion
1648     (gnus-set-work-buffer)
1649     (let ((case-fold-search t))
1650       ;; Remove uninteresting prefixes.
1651       (when (and gnus-simplify-ignored-prefixes
1652                  (string-match gnus-simplify-ignored-prefixes subject))
1653         (setq subject (substring subject (match-end 0))))
1654       (insert subject)
1655       (inline (gnus-simplify-buffer-fuzzy))
1656       (buffer-string))))
1657
1658 (defsubst gnus-simplify-subject-fully (subject)
1659   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1660   (cond
1661    (gnus-simplify-subject-functions
1662     (gnus-map-function gnus-simplify-subject-functions subject))
1663    ((null gnus-summary-gather-subject-limit)
1664     (gnus-simplify-subject-re subject))
1665    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1666     (gnus-simplify-subject-fuzzy subject))
1667    ((numberp gnus-summary-gather-subject-limit)
1668     (gnus-limit-string (gnus-simplify-subject-re subject)
1669                        gnus-summary-gather-subject-limit))
1670    (t
1671     subject)))
1672
1673 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1674   "Check whether two subjects are equal.
1675 If optional argument SIMPLE-FIRST is t, first argument is already
1676 simplified."
1677   (cond
1678    ((null simple-first)
1679     (equal (gnus-simplify-subject-fully s1)
1680            (gnus-simplify-subject-fully s2)))
1681    (t
1682     (equal s1
1683            (gnus-simplify-subject-fully s2)))))
1684
1685 (defun gnus-summary-bubble-group ()
1686   "Increase the score of the current group.
1687 This is a handy function to add to `gnus-summary-exit-hook' to
1688 increase the score of each group you read."
1689   (gnus-group-add-score gnus-newsgroup-name))
1690
1691 \f
1692 ;;;
1693 ;;; Gnus summary mode
1694 ;;;
1695
1696 (put 'gnus-summary-mode 'mode-class 'special)
1697
1698 (defvar gnus-article-commands-menu)
1699
1700 ;; Non-orthogonal keys
1701
1702 (gnus-define-keys gnus-summary-mode-map
1703   " " gnus-summary-next-page
1704   "\177" gnus-summary-prev-page
1705   [delete] gnus-summary-prev-page
1706   [backspace] gnus-summary-prev-page
1707   "\r" gnus-summary-scroll-up
1708   "\M-\r" gnus-summary-scroll-down
1709   "n" gnus-summary-next-unread-article
1710   "p" gnus-summary-prev-unread-article
1711   "N" gnus-summary-next-article
1712   "P" gnus-summary-prev-article
1713   "\M-\C-n" gnus-summary-next-same-subject
1714   "\M-\C-p" gnus-summary-prev-same-subject
1715   "\M-n" gnus-summary-next-unread-subject
1716   "\M-p" gnus-summary-prev-unread-subject
1717   "." gnus-summary-first-unread-article
1718   "," gnus-summary-best-unread-article
1719   "\M-s" gnus-summary-search-article-forward
1720   "\M-r" gnus-summary-search-article-backward
1721   "\M-S" gnus-summary-repeat-search-article-forward
1722   "\M-R" gnus-summary-repeat-search-article-backward
1723   "<" gnus-summary-beginning-of-article
1724   ">" gnus-summary-end-of-article
1725   "j" gnus-summary-goto-article
1726   "^" gnus-summary-refer-parent-article
1727   "\M-^" gnus-summary-refer-article
1728   "u" gnus-summary-tick-article-forward
1729   "!" gnus-summary-tick-article-forward
1730   "U" gnus-summary-tick-article-backward
1731   "d" gnus-summary-mark-as-read-forward
1732   "D" gnus-summary-mark-as-read-backward
1733   "E" gnus-summary-mark-as-expirable
1734   "\M-u" gnus-summary-clear-mark-forward
1735   "\M-U" gnus-summary-clear-mark-backward
1736   "k" gnus-summary-kill-same-subject-and-select
1737   "\C-k" gnus-summary-kill-same-subject
1738   "\M-\C-k" gnus-summary-kill-thread
1739   "\M-\C-l" gnus-summary-lower-thread
1740   "e" gnus-summary-edit-article
1741   "#" gnus-summary-mark-as-processable
1742   "\M-#" gnus-summary-unmark-as-processable
1743   "\M-\C-t" gnus-summary-toggle-threads
1744   "\M-\C-s" gnus-summary-show-thread
1745   "\M-\C-h" gnus-summary-hide-thread
1746   "\M-\C-f" gnus-summary-next-thread
1747   "\M-\C-b" gnus-summary-prev-thread
1748   [(meta down)] gnus-summary-next-thread
1749   [(meta up)] gnus-summary-prev-thread
1750   "\M-\C-u" gnus-summary-up-thread
1751   "\M-\C-d" gnus-summary-down-thread
1752   "&" gnus-summary-execute-command
1753   "c" gnus-summary-catchup-and-exit
1754   "\C-w" gnus-summary-mark-region-as-read
1755   "\C-t" gnus-summary-toggle-truncation
1756   "?" gnus-summary-mark-as-dormant
1757   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1758   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1759   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1760   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1761   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1762   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1763   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1764   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1765   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1766   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1767   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1768   "=" gnus-summary-expand-window
1769   "\C-x\C-s" gnus-summary-reselect-current-group
1770   "\M-g" gnus-summary-rescan-group
1771   "w" gnus-summary-stop-page-breaking
1772   "\C-c\C-r" gnus-summary-caesar-message
1773   "f" gnus-summary-followup
1774   "F" gnus-summary-followup-with-original
1775   "C" gnus-summary-cancel-article
1776   "r" gnus-summary-reply
1777   "R" gnus-summary-reply-with-original
1778   "\C-c\C-f" gnus-summary-mail-forward
1779   "o" gnus-summary-save-article
1780   "\C-o" gnus-summary-save-article-mail
1781   "|" gnus-summary-pipe-output
1782   "\M-k" gnus-summary-edit-local-kill
1783   "\M-K" gnus-summary-edit-global-kill
1784   ;; "V" gnus-version
1785   "\C-c\C-d" gnus-summary-describe-group
1786   "q" gnus-summary-exit
1787   "Q" gnus-summary-exit-no-update
1788   "\C-c\C-i" gnus-info-find-node
1789   gnus-mouse-2 gnus-mouse-pick-article
1790   [follow-link] mouse-face
1791   "m" gnus-summary-mail-other-window
1792   "a" gnus-summary-post-news
1793   "i" gnus-summary-news-other-window
1794   "x" gnus-summary-limit-to-unread
1795   "s" gnus-summary-isearch-article
1796   "t" gnus-summary-toggle-header
1797   "g" gnus-summary-show-article
1798   "l" gnus-summary-goto-last-article
1799   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1800   "\C-d" gnus-summary-enter-digest-group
1801   "\M-\C-d" gnus-summary-read-document
1802   "\M-\C-e" gnus-summary-edit-parameters
1803   "\M-\C-a" gnus-summary-customize-parameters
1804   "\C-c\C-b" gnus-bug
1805   "*" gnus-cache-enter-article
1806   "\M-*" gnus-cache-remove-article
1807   "\M-&" gnus-summary-universal-argument
1808   "\C-l" gnus-recenter
1809   "I" gnus-summary-increase-score
1810   "L" gnus-summary-lower-score
1811   "\M-i" gnus-symbolic-argument
1812   "h" gnus-summary-select-article-buffer
1813
1814   "b" gnus-article-view-part
1815   "\M-t" gnus-summary-toggle-display-buttonized
1816
1817   "V" gnus-summary-score-map
1818   "X" gnus-uu-extract-map
1819   "S" gnus-summary-send-map)
1820
1821 ;; Sort of orthogonal keymap
1822 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1823   "t" gnus-summary-tick-article-forward
1824   "!" gnus-summary-tick-article-forward
1825   "d" gnus-summary-mark-as-read-forward
1826   "r" gnus-summary-mark-as-read-forward
1827   "c" gnus-summary-clear-mark-forward
1828   " " gnus-summary-clear-mark-forward
1829   "e" gnus-summary-mark-as-expirable
1830   "x" gnus-summary-mark-as-expirable
1831   "?" gnus-summary-mark-as-dormant
1832   "b" gnus-summary-set-bookmark
1833   "B" gnus-summary-remove-bookmark
1834   "#" gnus-summary-mark-as-processable
1835   "\M-#" gnus-summary-unmark-as-processable
1836   "S" gnus-summary-limit-include-expunged
1837   "C" gnus-summary-catchup
1838   "H" gnus-summary-catchup-to-here
1839   "h" gnus-summary-catchup-from-here
1840   "\C-c" gnus-summary-catchup-all
1841   "k" gnus-summary-kill-same-subject-and-select
1842   "K" gnus-summary-kill-same-subject
1843   "P" gnus-uu-mark-map)
1844
1845 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1846   "c" gnus-summary-clear-above
1847   "u" gnus-summary-tick-above
1848   "m" gnus-summary-mark-above
1849   "k" gnus-summary-kill-below)
1850
1851 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1852   "/" gnus-summary-limit-to-subject
1853   "n" gnus-summary-limit-to-articles
1854   "b" gnus-summary-limit-to-bodies
1855   "h" gnus-summary-limit-to-headers
1856   "w" gnus-summary-pop-limit
1857   "s" gnus-summary-limit-to-subject
1858   "a" gnus-summary-limit-to-author
1859   "u" gnus-summary-limit-to-unread
1860   "m" gnus-summary-limit-to-marks
1861   "M" gnus-summary-limit-exclude-marks
1862   "v" gnus-summary-limit-to-score
1863   "*" gnus-summary-limit-include-cached
1864   "D" gnus-summary-limit-include-dormant
1865   "T" gnus-summary-limit-include-thread
1866   "d" gnus-summary-limit-exclude-dormant
1867   "t" gnus-summary-limit-to-age
1868   "." gnus-summary-limit-to-unseen
1869   "x" gnus-summary-limit-to-extra
1870   "p" gnus-summary-limit-to-display-predicate
1871   "E" gnus-summary-limit-include-expunged
1872   "c" gnus-summary-limit-exclude-childless-dormant
1873   "C" gnus-summary-limit-mark-excluded-as-read
1874   "o" gnus-summary-insert-old-articles
1875   "N" gnus-summary-insert-new-articles
1876   "r" gnus-summary-limit-to-replied
1877   "R" gnus-summary-limit-to-recipient)
1878
1879 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1880   "n" gnus-summary-next-unread-article
1881   "p" gnus-summary-prev-unread-article
1882   "N" gnus-summary-next-article
1883   "P" gnus-summary-prev-article
1884   "\C-n" gnus-summary-next-same-subject
1885   "\C-p" gnus-summary-prev-same-subject
1886   "\M-n" gnus-summary-next-unread-subject
1887   "\M-p" gnus-summary-prev-unread-subject
1888   "f" gnus-summary-first-unread-article
1889   "b" gnus-summary-best-unread-article
1890   "j" gnus-summary-goto-article
1891   "g" gnus-summary-goto-subject
1892   "l" gnus-summary-goto-last-article
1893   "o" gnus-summary-pop-article)
1894
1895 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1896   "k" gnus-summary-kill-thread
1897   "l" gnus-summary-lower-thread
1898   "i" gnus-summary-raise-thread
1899   "T" gnus-summary-toggle-threads
1900   "t" gnus-summary-rethread-current
1901   "^" gnus-summary-reparent-thread
1902   "\M-^" gnus-summary-reparent-children
1903   "s" gnus-summary-show-thread
1904   "S" gnus-summary-show-all-threads
1905   "h" gnus-summary-hide-thread
1906   "H" gnus-summary-hide-all-threads
1907   "n" gnus-summary-next-thread
1908   "p" gnus-summary-prev-thread
1909   "u" gnus-summary-up-thread
1910   "o" gnus-summary-top-thread
1911   "d" gnus-summary-down-thread
1912   "#" gnus-uu-mark-thread
1913   "\M-#" gnus-uu-unmark-thread)
1914
1915 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1916   "g" gnus-summary-prepare
1917   "c" gnus-summary-insert-cached-articles
1918   "d" gnus-summary-insert-dormant-articles)
1919
1920 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1921   "c" gnus-summary-catchup-and-exit
1922   "C" gnus-summary-catchup-all-and-exit
1923   "E" gnus-summary-exit-no-update
1924   "Q" gnus-summary-exit
1925   "Z" gnus-summary-exit
1926   "n" gnus-summary-catchup-and-goto-next-group
1927   "p" gnus-summary-catchup-and-goto-prev-group
1928   "R" gnus-summary-reselect-current-group
1929   "G" gnus-summary-rescan-group
1930   "N" gnus-summary-next-group
1931   "s" gnus-summary-save-newsrc
1932   "P" gnus-summary-prev-group)
1933
1934 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1935   " " gnus-summary-next-page
1936   "n" gnus-summary-next-page
1937   "\177" gnus-summary-prev-page
1938   [delete] gnus-summary-prev-page
1939   "p" gnus-summary-prev-page
1940   "\r" gnus-summary-scroll-up
1941   "\M-\r" gnus-summary-scroll-down
1942   "<" gnus-summary-beginning-of-article
1943   ">" gnus-summary-end-of-article
1944   "b" gnus-summary-beginning-of-article
1945   "e" gnus-summary-end-of-article
1946   "^" gnus-summary-refer-parent-article
1947   "r" gnus-summary-refer-parent-article
1948   "D" gnus-summary-enter-digest-group
1949   "R" gnus-summary-refer-references
1950   "T" gnus-summary-refer-thread
1951   "g" gnus-summary-show-article
1952   "s" gnus-summary-isearch-article
1953   "P" gnus-summary-print-article
1954   "M" gnus-mailing-list-insinuate
1955   "t" gnus-article-babel)
1956
1957 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1958   "b" gnus-article-add-buttons
1959   "B" gnus-article-add-buttons-to-head
1960   "o" gnus-article-treat-overstrike
1961   "e" gnus-article-emphasize
1962   "w" gnus-article-fill-cited-article
1963   "Q" gnus-article-fill-long-lines
1964   "L" gnus-article-toggle-truncate-lines
1965   "C" gnus-article-capitalize-sentences
1966   "c" gnus-article-remove-cr
1967   "q" gnus-article-de-quoted-unreadable
1968   "6" gnus-article-de-base64-unreadable
1969   "Z" gnus-article-decode-HZ
1970   "A" gnus-article-treat-ansi-sequences
1971   "h" gnus-article-wash-html
1972   "u" gnus-article-unsplit-urls
1973   "s" gnus-summary-force-verify-and-decrypt
1974   "f" gnus-article-display-x-face
1975   "l" gnus-summary-stop-page-breaking
1976   "r" gnus-summary-caesar-message
1977   "m" gnus-summary-morse-message
1978   "t" gnus-summary-toggle-header
1979   "g" gnus-treat-smiley
1980   "v" gnus-summary-verbose-headers
1981   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1982   "p" gnus-article-verify-x-pgp-sig
1983   "d" gnus-article-treat-dumbquotes
1984   "i" gnus-summary-idna-message)
1985
1986 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1987   ;; mnemonic: deuglif*Y*
1988   "u" gnus-article-outlook-unwrap-lines
1989   "a" gnus-article-outlook-repair-attribution
1990   "c" gnus-article-outlook-rearrange-citation
1991   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1992
1993 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1994   "a" gnus-article-hide
1995   "h" gnus-article-hide-headers
1996   "b" gnus-article-hide-boring-headers
1997   "s" gnus-article-hide-signature
1998   "c" gnus-article-hide-citation
1999   "C" gnus-article-hide-citation-in-followups
2000   "l" gnus-article-hide-list-identifiers
2001   "B" gnus-article-strip-banner
2002   "P" gnus-article-hide-pem
2003   "\C-c" gnus-article-hide-citation-maybe)
2004
2005 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2006   "a" gnus-article-highlight
2007   "h" gnus-article-highlight-headers
2008   "c" gnus-article-highlight-citation
2009   "s" gnus-article-highlight-signature)
2010
2011 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2012   "f" gnus-article-treat-fold-headers
2013   "u" gnus-article-treat-unfold-headers
2014   "n" gnus-article-treat-fold-newsgroups)
2015
2016 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2017   "x" gnus-article-display-x-face
2018   "d" gnus-article-display-face
2019   "s" gnus-treat-smiley
2020   "D" gnus-article-remove-images
2021   "f" gnus-treat-from-picon
2022   "m" gnus-treat-mail-picon
2023   "n" gnus-treat-newsgroups-picon)
2024
2025 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2026   "w" gnus-article-decode-mime-words
2027   "c" gnus-article-decode-charset
2028   "v" gnus-mime-view-all-parts
2029   "b" gnus-article-view-part)
2030
2031 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2032   "z" gnus-article-date-ut
2033   "u" gnus-article-date-ut
2034   "l" gnus-article-date-local
2035   "p" gnus-article-date-english
2036   "e" gnus-article-date-lapsed
2037   "o" gnus-article-date-original
2038   "i" gnus-article-date-iso8601
2039   "s" gnus-article-date-user)
2040
2041 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2042   "t" gnus-article-remove-trailing-blank-lines
2043   "l" gnus-article-strip-leading-blank-lines
2044   "m" gnus-article-strip-multiple-blank-lines
2045   "a" gnus-article-strip-blank-lines
2046   "A" gnus-article-strip-all-blank-lines
2047   "s" gnus-article-strip-leading-space
2048   "e" gnus-article-strip-trailing-space
2049   "w" gnus-article-remove-leading-whitespace)
2050
2051 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2052   "v" gnus-version
2053   "f" gnus-summary-fetch-faq
2054   "d" gnus-summary-describe-group
2055   "h" gnus-summary-describe-briefly
2056   "i" gnus-info-find-node
2057   "c" gnus-group-fetch-charter
2058   "C" gnus-group-fetch-control)
2059
2060 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2061   "e" gnus-summary-expire-articles
2062   "\M-\C-e" gnus-summary-expire-articles-now
2063   "\177" gnus-summary-delete-article
2064   [delete] gnus-summary-delete-article
2065   [backspace] gnus-summary-delete-article
2066   "m" gnus-summary-move-article
2067   "r" gnus-summary-respool-article
2068   "w" gnus-summary-edit-article
2069   "c" gnus-summary-copy-article
2070   "B" gnus-summary-crosspost-article
2071   "q" gnus-summary-respool-query
2072   "t" gnus-summary-respool-trace
2073   "i" gnus-summary-import-article
2074   "I" gnus-summary-create-article
2075   "p" gnus-summary-article-posted-p)
2076
2077 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2078   "o" gnus-summary-save-article
2079   "m" gnus-summary-save-article-mail
2080   "F" gnus-summary-write-article-file
2081   "r" gnus-summary-save-article-rmail
2082   "f" gnus-summary-save-article-file
2083   "b" gnus-summary-save-article-body-file
2084   "B" gnus-summary-write-article-body-file
2085   "h" gnus-summary-save-article-folder
2086   "v" gnus-summary-save-article-vm
2087   "p" gnus-summary-pipe-output
2088   "P" gnus-summary-muttprint
2089   "s" gnus-soup-add-article)
2090
2091 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2092   "b" gnus-summary-display-buttonized
2093   "m" gnus-summary-repair-multipart
2094   "v" gnus-article-view-part
2095   "o" gnus-article-save-part
2096   "O" gnus-article-save-part-and-strip
2097   "r" gnus-article-replace-part
2098   "d" gnus-article-delete-part
2099   "t" gnus-article-view-part-as-type
2100   "j" gnus-article-jump-to-part
2101   "c" gnus-article-copy-part
2102   "C" gnus-article-view-part-as-charset
2103   "e" gnus-article-view-part-externally
2104   "H" gnus-article-browse-html-article
2105   "E" gnus-article-encrypt-body
2106   "i" gnus-article-inline-part
2107   "|" gnus-article-pipe-part)
2108
2109 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2110   "p" gnus-summary-mark-as-processable
2111   "u" gnus-summary-unmark-as-processable
2112   "U" gnus-summary-unmark-all-processable
2113   "v" gnus-uu-mark-over
2114   "s" gnus-uu-mark-series
2115   "r" gnus-uu-mark-region
2116   "g" gnus-uu-unmark-region
2117   "R" gnus-uu-mark-by-regexp
2118   "G" gnus-uu-unmark-by-regexp
2119   "t" gnus-uu-mark-thread
2120   "T" gnus-uu-unmark-thread
2121   "a" gnus-uu-mark-all
2122   "b" gnus-uu-mark-buffer
2123   "S" gnus-uu-mark-sparse
2124   "k" gnus-summary-kill-process-mark
2125   "y" gnus-summary-yank-process-mark
2126   "w" gnus-summary-save-process-mark
2127   "i" gnus-uu-invert-processable)
2128
2129 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2130   ;;"x" gnus-uu-extract-any
2131   "m" gnus-summary-save-parts
2132   "u" gnus-uu-decode-uu
2133   "U" gnus-uu-decode-uu-and-save
2134   "s" gnus-uu-decode-unshar
2135   "S" gnus-uu-decode-unshar-and-save
2136   "o" gnus-uu-decode-save
2137   "O" gnus-uu-decode-save
2138   "b" gnus-uu-decode-binhex
2139   "B" gnus-uu-decode-binhex
2140   "p" gnus-uu-decode-postscript
2141   "P" gnus-uu-decode-postscript-and-save)
2142
2143 (gnus-define-keys
2144     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2145   "u" gnus-uu-decode-uu-view
2146   "U" gnus-uu-decode-uu-and-save-view
2147   "s" gnus-uu-decode-unshar-view
2148   "S" gnus-uu-decode-unshar-and-save-view
2149   "o" gnus-uu-decode-save-view
2150   "O" gnus-uu-decode-save-view
2151   "b" gnus-uu-decode-binhex-view
2152   "B" gnus-uu-decode-binhex-view
2153   "p" gnus-uu-decode-postscript-view
2154   "P" gnus-uu-decode-postscript-and-save-view)
2155
2156 (defvar gnus-article-post-menu nil)
2157
2158 (defconst gnus-summary-menu-maxlen 20)
2159
2160 (defun gnus-summary-menu-split (menu)
2161   ;; If we have lots of elements, divide them into groups of 20
2162   ;; and make a pane (or submenu) for each one.
2163   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2164       (let ((menu menu) sublists next
2165             (i 1))
2166         (while menu
2167           ;; Pull off the next gnus-summary-menu-maxlen elements
2168           ;; and make them the next element of sublist.
2169           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2170           (if next
2171               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2172                       nil))
2173           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2174                                              (aref (car (last menu)) 0)) menu)
2175                                sublists))
2176           (setq i (1+ i))
2177           (setq menu next))
2178         (nreverse sublists))
2179     ;; Few elements--put them all in one pane.
2180     menu))
2181
2182 (defun gnus-summary-make-menu-bar ()
2183   (gnus-turn-off-edit-menu 'summary)
2184
2185   (unless (boundp 'gnus-summary-misc-menu)
2186
2187     (easy-menu-define
2188       gnus-summary-kill-menu gnus-summary-mode-map ""
2189       (cons
2190        "Score"
2191        (nconc
2192         (list
2193          ["Customize" gnus-score-customize t])
2194         (gnus-make-score-map 'increase)
2195         (gnus-make-score-map 'lower)
2196         '(("Mark"
2197            ["Kill below" gnus-summary-kill-below t]
2198            ["Mark above" gnus-summary-mark-above t]
2199            ["Tick above" gnus-summary-tick-above t]
2200            ["Clear above" gnus-summary-clear-above t])
2201           ["Current score" gnus-summary-current-score t]
2202           ["Set score" gnus-summary-set-score t]
2203           ["Switch current score file..." gnus-score-change-score-file t]
2204           ["Set mark below..." gnus-score-set-mark-below t]
2205           ["Set expunge below..." gnus-score-set-expunge-below t]
2206           ["Edit current score file" gnus-score-edit-current-scores t]
2207           ["Edit score file" gnus-score-edit-file t]
2208           ["Trace score" gnus-score-find-trace t]
2209           ["Find words" gnus-score-find-favourite-words t]
2210           ["Rescore buffer" gnus-summary-rescore t]
2211           ["Increase score..." gnus-summary-increase-score t]
2212           ["Lower score..." gnus-summary-lower-score t]))))
2213
2214     ;; Define both the Article menu in the summary buffer and the
2215     ;; equivalent Commands menu in the article buffer here for
2216     ;; consistency.
2217     (let ((innards
2218            `(("Hide"
2219               ["All" gnus-article-hide t]
2220               ["Headers" gnus-article-hide-headers t]
2221               ["Signature" gnus-article-hide-signature t]
2222               ["Citation" gnus-article-hide-citation t]
2223               ["List identifiers" gnus-article-hide-list-identifiers t]
2224               ["Banner" gnus-article-strip-banner t]
2225               ["Boring headers" gnus-article-hide-boring-headers t])
2226              ("Highlight"
2227               ["All" gnus-article-highlight t]
2228               ["Headers" gnus-article-highlight-headers t]
2229               ["Signature" gnus-article-highlight-signature t]
2230               ["Citation" gnus-article-highlight-citation t])
2231              ("MIME"
2232               ["Words" gnus-article-decode-mime-words t]
2233               ["Charset" gnus-article-decode-charset t]
2234               ["QP" gnus-article-de-quoted-unreadable t]
2235               ["Base64" gnus-article-de-base64-unreadable t]
2236               ["View MIME buttons" gnus-summary-display-buttonized t]
2237               ["View all" gnus-mime-view-all-parts t]
2238               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2239               ["Encrypt body" gnus-article-encrypt-body
2240                :active (not (gnus-group-read-only-p))
2241                ,@(if (featurep 'xemacs) nil
2242                    '(:help "Encrypt the message body on disk"))]
2243               ["Extract all parts..." gnus-summary-save-parts t]
2244               ("Multipart"
2245                ["Repair multipart" gnus-summary-repair-multipart t]
2246                ["Pipe part..." gnus-article-pipe-part t]
2247                ["Inline part" gnus-article-inline-part t]
2248                ["View part as type..." gnus-article-view-part-as-type t]
2249                ["Encrypt body" gnus-article-encrypt-body
2250                 :active (not (gnus-group-read-only-p))
2251                ,@(if (featurep 'xemacs) nil
2252                    '(:help "Encrypt the message body on disk"))]
2253                ["View part externally" gnus-article-view-part-externally t]
2254                ["View HTML parts in browser" gnus-article-browse-html-article t]
2255                ["View part with charset..." gnus-article-view-part-as-charset t]
2256                ["Copy part" gnus-article-copy-part t]
2257                ["Save part..." gnus-article-save-part t]
2258                ["View part" gnus-article-view-part t]))
2259              ("Date"
2260               ["Local" gnus-article-date-local t]
2261               ["ISO8601" gnus-article-date-iso8601 t]
2262               ["UT" gnus-article-date-ut t]
2263               ["Original" gnus-article-date-original t]
2264               ["Lapsed" gnus-article-date-lapsed t]
2265               ["User-defined" gnus-article-date-user t])
2266              ("Display"
2267               ["Remove images" gnus-article-remove-images t]
2268               ["Toggle smiley" gnus-treat-smiley t]
2269               ["Show X-Face" gnus-article-display-x-face t]
2270               ["Show picons in From" gnus-treat-from-picon t]
2271               ["Show picons in mail headers" gnus-treat-mail-picon t]
2272               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2273               ("View as different encoding"
2274                ,@(gnus-summary-menu-split
2275                   (mapcar
2276                    (lambda (cs)
2277                      ;; Since easymenu under Emacs doesn't allow
2278                      ;; lambda forms for menu commands, we should
2279                      ;; provide intern'ed function symbols.
2280                      (let ((command (intern (format "\
2281 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2282                        (fset command
2283                              `(lambda ()
2284                                 (interactive)
2285                                 (let ((gnus-summary-show-article-charset-alist
2286                                        '((1 . ,cs))))
2287                                   (gnus-summary-show-article 1))))
2288                        `[,(symbol-name cs) ,command t]))
2289                    (sort (if (fboundp 'coding-system-list)
2290                              (coding-system-list)
2291                            (mapcar 'car mm-mime-mule-charset-alist))
2292                          'string<)))))
2293              ("Washing"
2294               ("Remove Blanks"
2295                ["Leading" gnus-article-strip-leading-blank-lines t]
2296                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2297                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2298                ["All of the above" gnus-article-strip-blank-lines t]
2299                ["All" gnus-article-strip-all-blank-lines t]
2300                ["Leading space" gnus-article-strip-leading-space t]
2301                ["Trailing space" gnus-article-strip-trailing-space t]
2302                ["Leading space in headers"
2303                 gnus-article-remove-leading-whitespace t])
2304               ["Overstrike" gnus-article-treat-overstrike t]
2305               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2306               ["Emphasis" gnus-article-emphasize t]
2307               ["Word wrap" gnus-article-fill-cited-article t]
2308               ["Fill long lines" gnus-article-fill-long-lines t]
2309               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2310               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2311               ["Remove CR" gnus-article-remove-cr t]
2312               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2313               ["Base64" gnus-article-de-base64-unreadable t]
2314               ["Rot 13" gnus-summary-caesar-message
2315                ,@(if (featurep 'xemacs) '(t)
2316                    '(:help "\"Caesar rotate\" article by 13"))]
2317               ["De-IDNA" gnus-summary-idna-message t]
2318               ["Morse decode" gnus-summary-morse-message t]
2319               ["Unix pipe..." gnus-summary-pipe-message t]
2320               ["Add buttons" gnus-article-add-buttons t]
2321               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2322               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2323               ["Verbose header" gnus-summary-verbose-headers t]
2324               ["Toggle header" gnus-summary-toggle-header t]
2325               ["Unfold headers" gnus-article-treat-unfold-headers t]
2326               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2327               ["Html" gnus-article-wash-html t]
2328               ["Unsplit URLs" gnus-article-unsplit-urls t]
2329               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2330               ["Decode HZ" gnus-article-decode-HZ t]
2331               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2332               ("(Outlook) Deuglify"
2333                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2334                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2335                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2336                ["Full (Outlook) deuglify"
2337                 gnus-article-outlook-deuglify-article t])
2338               )
2339              ("Output"
2340               ["Save in default format..." gnus-summary-save-article
2341                ,@(if (featurep 'xemacs) '(t)
2342                    '(:help "Save article using default method"))]
2343               ["Save in file..." gnus-summary-save-article-file
2344                ,@(if (featurep 'xemacs) '(t)
2345                    '(:help "Save article in file"))]
2346               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2347               ["Save in MH folder..." gnus-summary-save-article-folder t]
2348               ["Save in VM folder..." gnus-summary-save-article-vm t]
2349               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2350               ["Save body in file..." gnus-summary-save-article-body-file t]
2351               ["Pipe through a filter..." gnus-summary-pipe-output t]
2352               ["Add to SOUP packet" gnus-soup-add-article t]
2353               ["Print with Muttprint..." gnus-summary-muttprint t]
2354               ["Print" gnus-summary-print-article
2355                ,@(if (featurep 'xemacs) '(t)
2356                    '(:help "Generate and print a PostScript image"))])
2357              ("Copy, move,... (Backend)"
2358               ,@(if (featurep 'xemacs) nil
2359                   '(:help "Copying, moving, expiring articles..."))
2360               ["Respool article..." gnus-summary-respool-article t]
2361               ["Move article..." gnus-summary-move-article
2362                (gnus-check-backend-function
2363                 'request-move-article gnus-newsgroup-name)]
2364               ["Copy article..." gnus-summary-copy-article t]
2365               ["Crosspost article..." gnus-summary-crosspost-article
2366                (gnus-check-backend-function
2367                 'request-replace-article gnus-newsgroup-name)]
2368               ["Import file..." gnus-summary-import-article
2369                (gnus-check-backend-function
2370                 'request-accept-article gnus-newsgroup-name)]
2371               ["Create article..." gnus-summary-create-article
2372                (gnus-check-backend-function
2373                 'request-accept-article gnus-newsgroup-name)]
2374               ["Check if posted" gnus-summary-article-posted-p t]
2375               ["Edit article" gnus-summary-edit-article
2376                (not (gnus-group-read-only-p))]
2377               ["Delete article" gnus-summary-delete-article
2378                (gnus-check-backend-function
2379                 'request-expire-articles gnus-newsgroup-name)]
2380               ["Query respool" gnus-summary-respool-query t]
2381               ["Trace respool" gnus-summary-respool-trace t]
2382               ["Delete expirable articles" gnus-summary-expire-articles-now
2383                (gnus-check-backend-function
2384                 'request-expire-articles gnus-newsgroup-name)])
2385              ("Extract"
2386               ["Uudecode" gnus-uu-decode-uu
2387                ,@(if (featurep 'xemacs) '(t)
2388                    '(:help "Decode uuencoded article(s)"))]
2389               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2390               ["Unshar" gnus-uu-decode-unshar t]
2391               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2392               ["Save" gnus-uu-decode-save t]
2393               ["Binhex" gnus-uu-decode-binhex t]
2394               ["Postscript" gnus-uu-decode-postscript t]
2395               ["All MIME parts" gnus-summary-save-parts t])
2396              ("Cache"
2397               ["Enter article" gnus-cache-enter-article t]
2398               ["Remove article" gnus-cache-remove-article t])
2399              ["Translate" gnus-article-babel t]
2400              ["Select article buffer" gnus-summary-select-article-buffer t]
2401              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2402              ["Isearch article..." gnus-summary-isearch-article t]
2403              ["Beginning of the article" gnus-summary-beginning-of-article t]
2404              ["End of the article" gnus-summary-end-of-article t]
2405              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2406              ["Fetch referenced articles" gnus-summary-refer-references t]
2407              ["Fetch current thread" gnus-summary-refer-thread t]
2408              ["Fetch article with id..." gnus-summary-refer-article t]
2409              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2410              ["Redisplay" gnus-summary-show-article t]
2411              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2412       (easy-menu-define
2413         gnus-summary-article-menu gnus-summary-mode-map ""
2414         (cons "Article" innards))
2415
2416       (if (not (keymapp gnus-summary-article-menu))
2417           (easy-menu-define
2418             gnus-article-commands-menu gnus-article-mode-map ""
2419             (cons "Commands" innards))
2420         ;; in Emacs, don't share menu.
2421         (setq gnus-article-commands-menu
2422               (copy-keymap gnus-summary-article-menu))
2423         (define-key gnus-article-mode-map [menu-bar commands]
2424           (cons "Commands" gnus-article-commands-menu))))
2425
2426     (easy-menu-define
2427       gnus-summary-thread-menu gnus-summary-mode-map ""
2428       '("Threads"
2429         ["Find all messages in thread" gnus-summary-refer-thread t]
2430         ["Toggle threading" gnus-summary-toggle-threads t]
2431         ["Hide threads" gnus-summary-hide-all-threads t]
2432         ["Show threads" gnus-summary-show-all-threads t]
2433         ["Hide thread" gnus-summary-hide-thread t]
2434         ["Show thread" gnus-summary-show-thread t]
2435         ["Go to next thread" gnus-summary-next-thread t]
2436         ["Go to previous thread" gnus-summary-prev-thread t]
2437         ["Go down thread" gnus-summary-down-thread t]
2438         ["Go up thread" gnus-summary-up-thread t]
2439         ["Top of thread" gnus-summary-top-thread t]
2440         ["Mark thread as read" gnus-summary-kill-thread t]
2441         ["Lower thread score" gnus-summary-lower-thread t]
2442         ["Raise thread score" gnus-summary-raise-thread t]
2443         ["Rethread current" gnus-summary-rethread-current t]))
2444
2445     (easy-menu-define
2446       gnus-summary-post-menu gnus-summary-mode-map ""
2447       `("Post"
2448         ["Send a message (mail or news)" gnus-summary-post-news
2449          ,@(if (featurep 'xemacs) '(t)
2450              '(:help "Compose a new message (mail or news)"))]
2451         ["Followup" gnus-summary-followup
2452          ,@(if (featurep 'xemacs) '(t)
2453              '(:help "Post followup to this article"))]
2454         ["Followup and yank" gnus-summary-followup-with-original
2455          ,@(if (featurep 'xemacs) '(t)
2456              '(:help "Post followup to this article, quoting its contents"))]
2457         ["Supersede article" gnus-summary-supersede-article t]
2458         ["Cancel article" gnus-summary-cancel-article
2459          ,@(if (featurep 'xemacs) '(t)
2460              '(:help "Cancel an article you posted"))]
2461         ["Reply" gnus-summary-reply t]
2462         ["Reply and yank" gnus-summary-reply-with-original t]
2463         ["Wide reply" gnus-summary-wide-reply t]
2464         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2465          ,@(if (featurep 'xemacs) '(t)
2466              '(:help "Mail a reply, quoting this article"))]
2467         ["Very wide reply" gnus-summary-very-wide-reply t]
2468         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2469          ,@(if (featurep 'xemacs) '(t)
2470              '(:help "Mail a very wide reply, quoting this article"))]
2471         ["Mail forward" gnus-summary-mail-forward t]
2472         ["Post forward" gnus-summary-post-forward t]
2473         ["Digest and mail" gnus-uu-digest-mail-forward t]
2474         ["Digest and post" gnus-uu-digest-post-forward t]
2475         ["Resend message" gnus-summary-resend-message t]
2476         ["Resend message edit" gnus-summary-resend-message-edit t]
2477         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2478         ["Send a mail" gnus-summary-mail-other-window t]
2479         ["Create a local message" gnus-summary-news-other-window t]
2480         ["Uuencode and post" gnus-uu-post-news
2481          ,@(if (featurep 'xemacs) '(t)
2482              '(:help "Post a uuencoded article"))]
2483         ["Followup via news" gnus-summary-followup-to-mail t]
2484         ["Followup via news and yank"
2485          gnus-summary-followup-to-mail-with-original t]
2486         ;;("Draft"
2487         ;;["Send" gnus-summary-send-draft t]
2488         ;;["Send bounced" gnus-resend-bounced-mail t])
2489         ))
2490
2491     (cond
2492      ((not (keymapp gnus-summary-post-menu))
2493       (setq gnus-article-post-menu gnus-summary-post-menu))
2494      ((not gnus-article-post-menu)
2495       ;; Don't share post menu.
2496       (setq gnus-article-post-menu
2497             (copy-keymap gnus-summary-post-menu))))
2498     (define-key gnus-article-mode-map [menu-bar post]
2499       (cons "Post" gnus-article-post-menu))
2500
2501     (easy-menu-define
2502       gnus-summary-misc-menu gnus-summary-mode-map ""
2503       `("Gnus"
2504         ("Mark Read"
2505          ["Mark as read" gnus-summary-mark-as-read-forward t]
2506          ["Mark same subject and select"
2507           gnus-summary-kill-same-subject-and-select t]
2508          ["Mark same subject" gnus-summary-kill-same-subject t]
2509          ["Catchup" gnus-summary-catchup
2510           ,@(if (featurep 'xemacs) '(t)
2511               '(:help "Mark unread articles in this group as read"))]
2512          ["Catchup all" gnus-summary-catchup-all t]
2513          ["Catchup to here" gnus-summary-catchup-to-here t]
2514          ["Catchup from here" gnus-summary-catchup-from-here t]
2515          ["Catchup region" gnus-summary-mark-region-as-read
2516           (gnus-mark-active-p)]
2517          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2518         ("Mark Various"
2519          ["Tick" gnus-summary-tick-article-forward t]
2520          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2521          ["Remove marks" gnus-summary-clear-mark-forward t]
2522          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2523          ["Set bookmark" gnus-summary-set-bookmark t]
2524          ["Remove bookmark" gnus-summary-remove-bookmark t])
2525         ("Limit to"
2526          ["Marks..." gnus-summary-limit-to-marks t]
2527          ["Subject..." gnus-summary-limit-to-subject t]
2528          ["Author..." gnus-summary-limit-to-author t]
2529          ["Recipient..." gnus-summary-limit-to-recipient t]
2530          ["Age..." gnus-summary-limit-to-age t]
2531          ["Extra..." gnus-summary-limit-to-extra t]
2532          ["Score..." gnus-summary-limit-to-score t]
2533          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2534          ["Unread" gnus-summary-limit-to-unread t]
2535          ["Unseen" gnus-summary-limit-to-unseen t]
2536          ["Replied" gnus-summary-limit-to-replied t]
2537          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2538          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2539          ["Pop limit" gnus-summary-pop-limit t]
2540          ["Show dormant" gnus-summary-limit-include-dormant t]
2541          ["Hide childless dormant"
2542           gnus-summary-limit-exclude-childless-dormant t]
2543          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2544          ["Hide marked" gnus-summary-limit-exclude-marks t]
2545          ["Show expunged" gnus-summary-limit-include-expunged t])
2546         ("Process Mark"
2547          ["Set mark" gnus-summary-mark-as-processable t]
2548          ["Remove mark" gnus-summary-unmark-as-processable t]
2549          ["Remove all marks" gnus-summary-unmark-all-processable t]
2550          ["Invert marks" gnus-uu-invert-processable t]
2551          ["Mark above" gnus-uu-mark-over t]
2552          ["Mark series" gnus-uu-mark-series t]
2553          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2554          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2555          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2556          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2557          ["Mark all" gnus-uu-mark-all t]
2558          ["Mark buffer" gnus-uu-mark-buffer t]
2559          ["Mark sparse" gnus-uu-mark-sparse t]
2560          ["Mark thread" gnus-uu-mark-thread t]
2561          ["Unmark thread" gnus-uu-unmark-thread t]
2562          ("Process Mark Sets"
2563           ["Kill" gnus-summary-kill-process-mark t]
2564           ["Yank" gnus-summary-yank-process-mark
2565            gnus-newsgroup-process-stack]
2566           ["Save" gnus-summary-save-process-mark t]
2567           ["Run command on marked..." gnus-summary-universal-argument t]))
2568         ("Scroll article"
2569          ["Page forward" gnus-summary-next-page
2570           ,@(if (featurep 'xemacs) '(t)
2571               '(:help "Show next page of article"))]
2572          ["Page backward" gnus-summary-prev-page
2573           ,@(if (featurep 'xemacs) '(t)
2574               '(:help "Show previous page of article"))]
2575          ["Line forward" gnus-summary-scroll-up t])
2576         ("Move"
2577          ["Next unread article" gnus-summary-next-unread-article t]
2578          ["Previous unread article" gnus-summary-prev-unread-article t]
2579          ["Next article" gnus-summary-next-article t]
2580          ["Previous article" gnus-summary-prev-article t]
2581          ["Next unread subject" gnus-summary-next-unread-subject t]
2582          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2583          ["Next article same subject" gnus-summary-next-same-subject t]
2584          ["Previous article same subject" gnus-summary-prev-same-subject t]
2585          ["First unread article" gnus-summary-first-unread-article t]
2586          ["Best unread article" gnus-summary-best-unread-article t]
2587          ["Go to subject number..." gnus-summary-goto-subject t]
2588          ["Go to article number..." gnus-summary-goto-article t]
2589          ["Go to the last article" gnus-summary-goto-last-article t]
2590          ["Pop article off history" gnus-summary-pop-article t])
2591         ("Sort"
2592          ["Sort by number" gnus-summary-sort-by-number t]
2593          ["Sort by author" gnus-summary-sort-by-author t]
2594          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2595          ["Sort by subject" gnus-summary-sort-by-subject t]
2596          ["Sort by date" gnus-summary-sort-by-date t]
2597          ["Sort by score" gnus-summary-sort-by-score t]
2598          ["Sort by lines" gnus-summary-sort-by-lines t]
2599          ["Sort by characters" gnus-summary-sort-by-chars t]
2600          ["Randomize" gnus-summary-sort-by-random t]
2601          ["Original sort" gnus-summary-sort-by-original t])
2602         ("Help"
2603          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2604          ["Describe group" gnus-summary-describe-group t]
2605          ["Fetch charter" gnus-group-fetch-charter
2606           ,@(if (featurep 'xemacs) nil
2607               '(:help "Display the charter of the current group"))]
2608          ["Fetch control message" gnus-group-fetch-control
2609           ,@(if (featurep 'xemacs) nil
2610               '(:help "Display the archived control message for the current group"))]
2611          ["Read manual" gnus-info-find-node t])
2612         ("Modes"
2613          ["Pick and read" gnus-pick-mode t]
2614          ["Binary" gnus-binary-mode t])
2615         ("Regeneration"
2616          ["Regenerate" gnus-summary-prepare t]
2617          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2618          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2619          ["Toggle threading" gnus-summary-toggle-threads t])
2620         ["See old articles" gnus-summary-insert-old-articles t]
2621         ["See new articles" gnus-summary-insert-new-articles t]
2622         ["Filter articles..." gnus-summary-execute-command t]
2623         ["Run command on articles..." gnus-summary-universal-argument t]
2624         ["Search articles forward..." gnus-summary-search-article-forward t]
2625         ["Search articles backward..." gnus-summary-search-article-backward t]
2626         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2627         ["Expand window" gnus-summary-expand-window t]
2628         ["Expire expirable articles" gnus-summary-expire-articles
2629          (gnus-check-backend-function
2630           'request-expire-articles gnus-newsgroup-name)]
2631         ["Edit local kill file" gnus-summary-edit-local-kill t]
2632         ["Edit main kill file" gnus-summary-edit-global-kill t]
2633         ["Edit group parameters" gnus-summary-edit-parameters t]
2634         ["Customize group parameters" gnus-summary-customize-parameters t]
2635         ["Send a bug report" gnus-bug t]
2636         ("Exit"
2637          ["Catchup and exit" gnus-summary-catchup-and-exit
2638           ,@(if (featurep 'xemacs) '(t)
2639               '(:help "Mark unread articles in this group as read, then exit"))]
2640          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2641          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2642          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2643          ["Exit group" gnus-summary-exit
2644           ,@(if (featurep 'xemacs) '(t)
2645               '(:help "Exit current group, return to group selection mode"))]
2646          ["Exit group without updating" gnus-summary-exit-no-update t]
2647          ["Exit and goto next group" gnus-summary-next-group t]
2648          ["Exit and goto prev group" gnus-summary-prev-group t]
2649          ["Reselect group" gnus-summary-reselect-current-group t]
2650          ["Rescan group" gnus-summary-rescan-group t]
2651          ["Update dribble" gnus-summary-save-newsrc t])))
2652
2653     (gnus-run-hooks 'gnus-summary-menu-hook)))
2654
2655 (defvar gnus-summary-tool-bar-map nil)
2656
2657 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2658 ;; affect _new_ message buffers.  We might add a function that walks thru all
2659 ;; summary-mode buffers and force the update.
2660 (defun gnus-summary-tool-bar-update (&optional symbol value)
2661   "Update summary mode toolbar.
2662 Setter function for custom variables."
2663   (setq-default gnus-summary-tool-bar-map nil)
2664   (when symbol
2665     ;; When used as ":set" function:
2666     (set-default symbol value))
2667   (when (gnus-buffer-live-p gnus-summary-buffer)
2668     (with-current-buffer gnus-summary-buffer
2669       (gnus-summary-make-tool-bar))))
2670
2671 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2672                                      'gnus-summary-tool-bar-gnome
2673                                    'gnus-summary-tool-bar-retro)
2674   "Specifies the Gnus summary tool bar.
2675
2676 It can be either a list or a symbol refering to a list.  See
2677 `gmm-tool-bar-from-list' for the format of the list.  The
2678 default key map is `gnus-summary-mode-map'.
2679
2680 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2681 `gnus-summary-tool-bar-retro'."
2682   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2683                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2684                  (repeat :tag "User defined list" gmm-tool-bar-item)
2685                  (symbol))
2686   :version "23.0" ;; No Gnus
2687   :initialize 'custom-initialize-default
2688   :set 'gnus-summary-tool-bar-update
2689   :group 'gnus-summary)
2690
2691 (defcustom gnus-summary-tool-bar-gnome
2692   '((gnus-summary-post-news "mail/compose" nil)
2693     (gnus-summary-insert-new-articles "mail/inbox" nil
2694                                       :visible (or (not gnus-agent)
2695                                                    gnus-plugged))
2696     (gnus-summary-reply-with-original "mail/reply")
2697     (gnus-summary-reply "mail/reply" nil :visible nil)
2698     (gnus-summary-followup-with-original "mail/reply-all")
2699     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2700     (gnus-summary-mail-forward "mail/forward")
2701     (gnus-summary-save-article "mail/save")
2702     (gnus-summary-search-article-forward "search" nil :visible nil)
2703     (gnus-summary-print-article "print")
2704     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2705     ;; Some new commands that may need more suitable icons:
2706     (gnus-summary-save-newsrc "save" nil :visible nil)
2707     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2708     (gnus-summary-prev-article "left-arrow")
2709     (gnus-summary-next-article "right-arrow")
2710     (gnus-summary-next-page "next-page")
2711     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2712     ;;
2713     ;; Maybe some sort-by-... could be added:
2714     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2715     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2716     (gnus-summary-mark-as-expirable
2717      "delete" nil
2718      :visible (gnus-check-backend-function 'request-expire-articles
2719                                            gnus-newsgroup-name))
2720     (gnus-summary-mark-as-spam
2721      "mail/spam" t
2722      :visible (and (fboundp 'spam-group-ham-contents-p)
2723                    (spam-group-ham-contents-p gnus-newsgroup-name))
2724      :help "Mark as spam")
2725     (gnus-summary-mark-as-read-forward
2726      "mail/not-spam" nil
2727      :visible (and (fboundp 'spam-group-spam-contents-p)
2728                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2729     ;;
2730     (gnus-summary-exit "exit")
2731     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2732     (gnus-info-find-node "help"))
2733   "List of functions for the summary tool bar (GNOME style).
2734
2735 See `gmm-tool-bar-from-list' for the format of the list."
2736   :type '(repeat gmm-tool-bar-item)
2737   :version "23.0" ;; No Gnus
2738   :initialize 'custom-initialize-default
2739   :set 'gnus-summary-tool-bar-update
2740   :group 'gnus-summary)
2741
2742 (defcustom gnus-summary-tool-bar-retro
2743   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2744     (gnus-summary-next-unread-article "gnus/next-ur")
2745     (gnus-summary-post-news "gnus/post")
2746     (gnus-summary-followup-with-original "gnus/fuwo")
2747     (gnus-summary-followup "gnus/followup")
2748     (gnus-summary-reply-with-original "gnus/reply-wo")
2749     (gnus-summary-reply "gnus/reply")
2750     (gnus-summary-caesar-message "gnus/rot13")
2751     (gnus-uu-decode-uu "gnus/uu-decode")
2752     (gnus-summary-save-article-file "gnus/save-aif")
2753     (gnus-summary-save-article "gnus/save-art")
2754     (gnus-uu-post-news "gnus/uu-post")
2755     (gnus-summary-catchup "gnus/catchup")
2756     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2757     (gnus-summary-exit "gnus/exit-summ")
2758     ;; Some new command that may need more suitable icons:
2759     (gnus-summary-print-article "gnus/print" nil :visible nil)
2760     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2761     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2762     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2763     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2764     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2765     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2766     ;;
2767     (gnus-info-find-node "gnus/help" nil :visible nil))
2768   "List of functions for the summary tool bar (retro look).
2769
2770 See `gmm-tool-bar-from-list' for the format of the list."
2771   :type '(repeat gmm-tool-bar-item)
2772   :version "23.0" ;; No Gnus
2773   :initialize 'custom-initialize-default
2774   :set 'gnus-summary-tool-bar-update
2775   :group 'gnus-summary)
2776
2777 (defcustom gnus-summary-tool-bar-zap-list t
2778   "List of icon items from the global tool bar.
2779 These items are not displayed in the Gnus summary mode tool bar.
2780
2781 See `gmm-tool-bar-from-list' for the format of the list."
2782   :type 'gmm-tool-bar-zap-list
2783   :version "23.0" ;; No Gnus
2784   :initialize 'custom-initialize-default
2785   :set 'gnus-summary-tool-bar-update
2786   :group 'gnus-summary)
2787
2788 (defvar image-load-path)
2789
2790 (defun gnus-summary-make-tool-bar (&optional force)
2791   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2792 When FORCE, rebuild the tool bar."
2793   (when (and (not (featurep 'xemacs))
2794              (boundp 'tool-bar-mode)
2795              tool-bar-mode
2796              (or (not gnus-summary-tool-bar-map) force))
2797     (let* ((load-path
2798             (gmm-image-load-path-for-library "gnus"
2799                                              "mail/save.xpm"
2800                                              nil t))
2801            (image-load-path (cons (car load-path)
2802                                   (when (boundp 'image-load-path)
2803                                     image-load-path)))
2804            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2805                                         gnus-summary-tool-bar-zap-list
2806                                         'gnus-summary-mode-map)))
2807       (when map
2808         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2809         ;; uses it's value.
2810         (setq gnus-summary-tool-bar-map map))))
2811   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2812
2813 (defun gnus-score-set-default (var value)
2814   "A version of set that updates the GNU Emacs menu-bar."
2815   (set var value)
2816   ;; It is the message that forces the active status to be updated.
2817   (message ""))
2818
2819 (defun gnus-make-score-map (type)
2820   "Make a summary score map of type TYPE."
2821   (if t
2822       nil
2823     (let ((headers '(("author" "from" string)
2824                      ("subject" "subject" string)
2825                      ("article body" "body" string)
2826                      ("article head" "head" string)
2827                      ("xref" "xref" string)
2828                      ("extra header" "extra" string)
2829                      ("lines" "lines" number)
2830                      ("followups to author" "followup" string)))
2831           (types '((number ("less than" <)
2832                            ("greater than" >)
2833                            ("equal" =))
2834                    (string ("substring" s)
2835                            ("exact string" e)
2836                            ("fuzzy string" f)
2837                            ("regexp" r))))
2838           (perms '(("temporary" (current-time-string))
2839                    ("permanent" nil)
2840                    ("immediate" now)))
2841           header)
2842       (list
2843        (apply
2844         'nconc
2845         (list
2846          (if (eq type 'lower)
2847              "Lower score"
2848            "Increase score"))
2849         (let (outh)
2850           (while headers
2851             (setq header (car headers))
2852             (setq outh
2853                   (cons
2854                    (apply
2855                     'nconc
2856                     (list (car header))
2857                     (let ((ts (cdr (assoc (nth 2 header) types)))
2858                           outt)
2859                       (while ts
2860                         (setq outt
2861                               (cons
2862                                (apply
2863                                 'nconc
2864                                 (list (caar ts))
2865                                 (let ((ps perms)
2866                                       outp)
2867                                   (while ps
2868                                     (setq outp
2869                                           (cons
2870                                            (vector
2871                                             (caar ps)
2872                                             (list
2873                                              'gnus-summary-score-entry
2874                                              (nth 1 header)
2875                                              (if (or (string= (nth 1 header)
2876                                                               "head")
2877                                                      (string= (nth 1 header)
2878                                                               "body"))
2879                                                  ""
2880                                                (list 'gnus-summary-header
2881                                                      (nth 1 header)))
2882                                              (list 'quote (nth 1 (car ts)))
2883                                              (list 'gnus-score-delta-default
2884                                                    nil)
2885                                              (nth 1 (car ps))
2886                                              t)
2887                                             t)
2888                                            outp))
2889                                     (setq ps (cdr ps)))
2890                                   (list (nreverse outp))))
2891                                outt))
2892                         (setq ts (cdr ts)))
2893                       (list (nreverse outt))))
2894                    outh))
2895             (setq headers (cdr headers)))
2896           (list (nreverse outh))))))))
2897
2898 \f
2899
2900 (defun gnus-summary-mode (&optional group)
2901   "Major mode for reading articles.
2902
2903 All normal editing commands are switched off.
2904 \\<gnus-summary-mode-map>
2905 Each line in this buffer represents one article.  To read an
2906 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2907 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2908 respectively.
2909
2910 You can also post articles and send mail from this buffer.  To
2911 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2912 of an article, type `\\[gnus-summary-reply]'.
2913
2914 There are approx. one gazillion commands you can execute in this
2915 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2916
2917 The following commands are available:
2918
2919 \\{gnus-summary-mode-map}"
2920   (interactive)
2921   (kill-all-local-variables)
2922   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2923     (gnus-summary-make-local-variables))
2924   (gnus-summary-make-local-variables)
2925   (setq gnus-newsgroup-name group)
2926   (when (gnus-visual-p 'summary-menu 'menu)
2927     (gnus-summary-make-menu-bar)
2928     (gnus-summary-make-tool-bar))
2929   (gnus-make-thread-indent-array)
2930   (gnus-simplify-mode-line)
2931   (setq major-mode 'gnus-summary-mode)
2932   (setq mode-name "Summary")
2933   (make-local-variable 'minor-mode-alist)
2934   (use-local-map gnus-summary-mode-map)
2935   (buffer-disable-undo)
2936   (setq buffer-read-only t              ;Disable modification
2937         show-trailing-whitespace nil)
2938   (setq truncate-lines t)
2939   (setq selective-display t)
2940   (setq selective-display-ellipses t)   ;Display `...'
2941   (gnus-summary-set-display-table)
2942   (gnus-set-default-directory)
2943   (make-local-variable 'gnus-summary-line-format)
2944   (make-local-variable 'gnus-summary-line-format-spec)
2945   (make-local-variable 'gnus-summary-dummy-line-format)
2946   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2947   (make-local-variable 'gnus-summary-mark-positions)
2948   (gnus-make-local-hook 'pre-command-hook)
2949   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2950   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2951   (turn-on-gnus-mailing-list-mode)
2952   (mm-enable-multibyte)
2953   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2954   (gnus-update-summary-mark-positions))
2955
2956 (defun gnus-summary-make-local-variables ()
2957   "Make all the local summary buffer variables."
2958   (let (global)
2959     (dolist (local gnus-summary-local-variables)
2960       (if (consp local)
2961           (progn
2962             (if (eq (cdr local) 'global)
2963                 ;; Copy the global value of the variable.
2964                 (setq global (symbol-value (car local)))
2965               ;; Use the value from the list.
2966               (setq global (eval (cdr local))))
2967             (set (make-local-variable (car local)) global))
2968         ;; Simple nil-valued local variable.
2969         (set (make-local-variable local) nil)))))
2970
2971 (defun gnus-summary-clear-local-variables ()
2972   (let ((locals gnus-summary-local-variables))
2973     (while locals
2974       (if (consp (car locals))
2975           (and (symbolp (caar locals))
2976                (set (caar locals) nil))
2977         (and (symbolp (car locals))
2978              (set (car locals) nil)))
2979       (setq locals (cdr locals)))))
2980
2981 ;; Summary data functions.
2982
2983 (defmacro gnus-data-number (data)
2984   `(car ,data))
2985
2986 (defmacro gnus-data-set-number (data number)
2987   `(setcar ,data ,number))
2988
2989 (defmacro gnus-data-mark (data)
2990   `(nth 1 ,data))
2991
2992 (defmacro gnus-data-set-mark (data mark)
2993   `(setcar (nthcdr 1 ,data) ,mark))
2994
2995 (defmacro gnus-data-pos (data)
2996   `(nth 2 ,data))
2997
2998 (defmacro gnus-data-set-pos (data pos)
2999   `(setcar (nthcdr 2 ,data) ,pos))
3000
3001 (defmacro gnus-data-header (data)
3002   `(nth 3 ,data))
3003
3004 (defmacro gnus-data-set-header (data header)
3005   `(setf (nth 3 ,data) ,header))
3006
3007 (defmacro gnus-data-level (data)
3008   `(nth 4 ,data))
3009
3010 (defmacro gnus-data-unread-p (data)
3011   `(= (nth 1 ,data) gnus-unread-mark))
3012
3013 (defmacro gnus-data-read-p (data)
3014   `(/= (nth 1 ,data) gnus-unread-mark))
3015
3016 (defmacro gnus-data-pseudo-p (data)
3017   `(consp (nth 3 ,data)))
3018
3019 (defmacro gnus-data-find (number)
3020   `(assq ,number gnus-newsgroup-data))
3021
3022 (defmacro gnus-data-find-list (number &optional data)
3023   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3024      (memq (assq ,number bdata)
3025            bdata)))
3026
3027 (defmacro gnus-data-make (number mark pos header level)
3028   `(list ,number ,mark ,pos ,header ,level))
3029
3030 (defun gnus-data-enter (after-article number mark pos header level offset)
3031   (let ((data (gnus-data-find-list after-article)))
3032     (unless data
3033       (error "No such article: %d" after-article))
3034     (setcdr data (cons (gnus-data-make number mark pos header level)
3035                        (cdr data)))
3036     (setq gnus-newsgroup-data-reverse nil)
3037     (gnus-data-update-list (cddr data) offset)))
3038
3039 (defun gnus-data-enter-list (after-article list &optional offset)
3040   (when list
3041     (let ((data (and after-article (gnus-data-find-list after-article)))
3042           (ilist list))
3043       (if (not (or data
3044                    after-article))
3045           (let ((odata gnus-newsgroup-data))
3046             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3047             (when offset
3048               (gnus-data-update-list odata offset)))
3049         ;; Find the last element in the list to be spliced into the main
3050         ;; list.
3051         (setq list (last list))
3052         (if (not data)
3053             (progn
3054               (setcdr list gnus-newsgroup-data)
3055               (setq gnus-newsgroup-data ilist)
3056               (when offset
3057                 (gnus-data-update-list (cdr list) offset)))
3058           (setcdr list (cdr data))
3059           (setcdr data ilist)
3060           (when offset
3061             (gnus-data-update-list (cdr list) offset))))
3062       (setq gnus-newsgroup-data-reverse nil))))
3063
3064 (defun gnus-data-remove (article &optional offset)
3065   (let ((data gnus-newsgroup-data))
3066     (if (= (gnus-data-number (car data)) article)
3067         (progn
3068           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3069                 gnus-newsgroup-data-reverse nil)
3070           (when offset
3071             (gnus-data-update-list gnus-newsgroup-data offset)))
3072       (while (cdr data)
3073         (when (= (gnus-data-number (cadr data)) article)
3074           (setcdr data (cddr data))
3075           (when offset
3076             (gnus-data-update-list (cdr data) offset))
3077           (setq data nil
3078                 gnus-newsgroup-data-reverse nil))
3079         (setq data (cdr data))))))
3080
3081 (defmacro gnus-data-list (backward)
3082   `(if ,backward
3083        (or gnus-newsgroup-data-reverse
3084            (setq gnus-newsgroup-data-reverse
3085                  (reverse gnus-newsgroup-data)))
3086      gnus-newsgroup-data))
3087
3088 (defun gnus-data-update-list (data offset)
3089   "Add OFFSET to the POS of all data entries in DATA."
3090   (setq gnus-newsgroup-data-reverse nil)
3091   (while data
3092     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3093     (setq data (cdr data))))
3094
3095 (defun gnus-summary-article-pseudo-p (article)
3096   "Say whether this article is a pseudo article or not."
3097   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3098
3099 (defmacro gnus-summary-article-sparse-p (article)
3100   "Say whether this article is a sparse article or not."
3101   `(memq ,article gnus-newsgroup-sparse))
3102
3103 (defmacro gnus-summary-article-ancient-p (article)
3104   "Say whether this article is a sparse article or not."
3105   `(memq ,article gnus-newsgroup-ancient))
3106
3107 (defun gnus-article-parent-p (number)
3108   "Say whether this article is a parent or not."
3109   (let ((data (gnus-data-find-list number)))
3110     (and (cdr data)              ; There has to be an article after...
3111          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3112             (gnus-data-level (nth 1 data))))))
3113
3114 (defun gnus-article-children (number)
3115   "Return a list of all children to NUMBER."
3116   (let* ((data (gnus-data-find-list number))
3117          (level (gnus-data-level (car data)))
3118          children)
3119     (setq data (cdr data))
3120     (while (and data
3121                 (= (gnus-data-level (car data)) (1+ level)))
3122       (push (gnus-data-number (car data)) children)
3123       (setq data (cdr data)))
3124     children))
3125
3126 (defmacro gnus-summary-skip-intangible ()
3127   "If the current article is intangible, then jump to a different article."
3128   '(let ((to (get-text-property (point) 'gnus-intangible)))
3129      (and to (gnus-summary-goto-subject to))))
3130
3131 (defmacro gnus-summary-article-intangible-p ()
3132   "Say whether this article is intangible or not."
3133   '(get-text-property (point) 'gnus-intangible))
3134
3135 (defun gnus-article-read-p (article)
3136   "Say whether ARTICLE is read or not."
3137   (not (or (memq article gnus-newsgroup-marked)
3138            (memq article gnus-newsgroup-spam-marked)
3139            (memq article gnus-newsgroup-unreads)
3140            (memq article gnus-newsgroup-unselected)
3141            (memq article gnus-newsgroup-dormant))))
3142
3143 ;; Some summary mode macros.
3144
3145 (defmacro gnus-summary-article-number ()
3146   "The article number of the article on the current line.
3147 If there isn't an article number here, then we return the current
3148 article number."
3149   '(progn
3150      (gnus-summary-skip-intangible)
3151      (or (get-text-property (point) 'gnus-number)
3152          (gnus-summary-last-subject))))
3153
3154 (defmacro gnus-summary-article-header (&optional number)
3155   "Return the header of article NUMBER."
3156   `(gnus-data-header (gnus-data-find
3157                       ,(or number '(gnus-summary-article-number)))))
3158
3159 (defmacro gnus-summary-thread-level (&optional number)
3160   "Return the level of thread that starts with article NUMBER."
3161   `(if (and (eq gnus-summary-make-false-root 'dummy)
3162             (get-text-property (point) 'gnus-intangible))
3163        0
3164      (gnus-data-level (gnus-data-find
3165                        ,(or number '(gnus-summary-article-number))))))
3166
3167 (defmacro gnus-summary-article-mark (&optional number)
3168   "Return the mark of article NUMBER."
3169   `(gnus-data-mark (gnus-data-find
3170                     ,(or number '(gnus-summary-article-number)))))
3171
3172 (defmacro gnus-summary-article-pos (&optional number)
3173   "Return the position of the line of article NUMBER."
3174   `(gnus-data-pos (gnus-data-find
3175                    ,(or number '(gnus-summary-article-number)))))
3176
3177 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3178 (defmacro gnus-summary-article-subject (&optional number)
3179   "Return current subject string or nil if nothing."
3180   `(let ((headers
3181           ,(if number
3182                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3183              '(gnus-data-header (assq (gnus-summary-article-number)
3184                                       gnus-newsgroup-data)))))
3185      (and headers
3186           (vectorp headers)
3187           (mail-header-subject headers))))
3188
3189 (defmacro gnus-summary-article-score (&optional number)
3190   "Return current article score."
3191   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3192                   gnus-newsgroup-scored))
3193        gnus-summary-default-score 0))
3194
3195 (defun gnus-summary-article-children (&optional number)
3196   "Return a list of article numbers that are children of article NUMBER."
3197   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3198          (level (gnus-data-level (car data)))
3199          l children)
3200     (while (and (setq data (cdr data))
3201                 (> (setq l (gnus-data-level (car data))) level))
3202       (and (= (1+ level) l)
3203            (push (gnus-data-number (car data))
3204                  children)))
3205     (nreverse children)))
3206
3207 (defun gnus-summary-article-parent (&optional number)
3208   "Return the article number of the parent of article NUMBER."
3209   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3210                                     (gnus-data-list t)))
3211          (level (gnus-data-level (car data))))
3212     (if (zerop level)
3213         ()                              ; This is a root.
3214       ;; We search until we find an article with a level less than
3215       ;; this one.  That function has to be the parent.
3216       (while (and (setq data (cdr data))
3217                   (not (< (gnus-data-level (car data)) level))))
3218       (and data (gnus-data-number (car data))))))
3219
3220 (defun gnus-unread-mark-p (mark)
3221   "Say whether MARK is the unread mark."
3222   (= mark gnus-unread-mark))
3223
3224 (defun gnus-read-mark-p (mark)
3225   "Say whether MARK is one of the marks that mark as read.
3226 This is all marks except unread, ticked, dormant, and expirable."
3227   (not (or (= mark gnus-unread-mark)
3228            (= mark gnus-ticked-mark)
3229            (= mark gnus-spam-mark)
3230            (= mark gnus-dormant-mark)
3231            (= mark gnus-expirable-mark))))
3232
3233 (defmacro gnus-article-mark (number)
3234   "Return the MARK of article NUMBER.
3235 This macro should only be used when computing the mark the \"first\"
3236 time; i.e., when generating the summary lines.  After that,
3237 `gnus-summary-article-mark' should be used to examine the
3238 marks of articles."
3239   `(cond
3240     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3241     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3242     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3243     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3244     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3245     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3246     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3247     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3248            gnus-ancient-mark))))
3249
3250 ;; Saving hidden threads.
3251
3252 (defmacro gnus-save-hidden-threads (&rest forms)
3253   "Save hidden threads, eval FORMS, and restore the hidden threads."
3254   (let ((config (make-symbol "config")))
3255     `(let ((,config (gnus-hidden-threads-configuration)))
3256        (unwind-protect
3257            (save-excursion
3258              ,@forms)
3259          (gnus-restore-hidden-threads-configuration ,config)))))
3260 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3261 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3262
3263 (defun gnus-data-compute-positions ()
3264   "Compute the positions of all articles."
3265   (setq gnus-newsgroup-data-reverse nil)
3266   (let ((data gnus-newsgroup-data))
3267     (save-excursion
3268       (gnus-save-hidden-threads
3269         (gnus-summary-show-all-threads)
3270         (goto-char (point-min))
3271         (while data
3272           (while (get-text-property (point) 'gnus-intangible)
3273             (forward-line 1))
3274           (gnus-data-set-pos (car data) (+ (point) 3))
3275           (setq data (cdr data))
3276           (forward-line 1))))))
3277
3278 (defun gnus-hidden-threads-configuration ()
3279   "Return the current hidden threads configuration."
3280   (save-excursion
3281     (let (config)
3282       (goto-char (point-min))
3283       (while (search-forward "\r" nil t)
3284         (push (1- (point)) config))
3285       config)))
3286
3287 (defun gnus-restore-hidden-threads-configuration (config)
3288   "Restore hidden threads configuration from CONFIG."
3289   (save-excursion
3290     (let (point buffer-read-only)
3291       (while (setq point (pop config))
3292         (when (and (< point (point-max))
3293                    (goto-char point)
3294                    (eq (char-after) ?\n))
3295           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3296
3297 ;; Various summary mode internalish functions.
3298
3299 (defun gnus-mouse-pick-article (e)
3300   (interactive "e")
3301   (mouse-set-point e)
3302   (gnus-summary-next-page nil t))
3303
3304 (defun gnus-summary-set-display-table ()
3305   "Change the display table.
3306 Odd characters have a tendency to mess
3307 up nicely formatted displays - we make all possible glyphs
3308 display only a single character."
3309
3310   ;; We start from the standard display table, if any.
3311   (let ((table (or (copy-sequence standard-display-table)
3312                    (make-display-table)))
3313         (i 32))
3314     ;; Nix out all the control chars...
3315     (while (>= (setq i (1- i)) 0)
3316       (aset table i [??]))
3317    ;; ... but not newline and cr, of course.  (cr is necessary for the
3318     ;; selective display).
3319     (aset table ?\n nil)
3320     (aset table ?\r nil)
3321     ;; We keep TAB as well.
3322     (aset table ?\t nil)
3323     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3324     ;; Emacs 23 (unicode), that are not set already.
3325     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3326                  160
3327                256)))
3328       (while (>= (setq i (1- i)) 127)
3329         ;; Only modify if the entry is nil.
3330         (unless (aref table i)
3331           (aset table i [??]))))
3332     (setq buffer-display-table table)))
3333
3334 (defun gnus-summary-set-article-display-arrow (pos)
3335   "Update the overlay arrow to point to line at position POS."
3336   (when (and gnus-summary-display-arrow
3337              (boundp 'overlay-arrow-position)
3338              (boundp 'overlay-arrow-string))
3339     (save-excursion
3340       (goto-char pos)
3341       (beginning-of-line)
3342       (unless overlay-arrow-position
3343         (setq overlay-arrow-position (make-marker)))
3344       (setq overlay-arrow-string "=>"
3345             overlay-arrow-position (set-marker overlay-arrow-position
3346                                                (point)
3347                                                (current-buffer))))))
3348
3349 (defun gnus-summary-setup-buffer (group)
3350   "Initialize summary buffer."
3351   (let ((buffer (gnus-summary-buffer-name group))
3352         (dead-name (concat "*Dead Summary "
3353                            (gnus-group-decoded-name group) "*")))
3354     ;; If a dead summary buffer exists, we kill it.
3355     (when (gnus-buffer-live-p dead-name)
3356       (gnus-kill-buffer dead-name))
3357     (if (get-buffer buffer)
3358         (progn
3359           (set-buffer buffer)
3360           (setq gnus-summary-buffer (current-buffer))
3361           (not gnus-newsgroup-prepared))
3362       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3363       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3364       (gnus-summary-mode group)
3365       (when gnus-carpal
3366         (gnus-carpal-setup-buffer 'summary))
3367       (when (gnus-group-quit-config group)
3368         (set (make-local-variable 'gnus-single-article-buffer) nil))
3369       (make-local-variable 'gnus-article-buffer)
3370       (make-local-variable 'gnus-article-current)
3371       (make-local-variable 'gnus-original-article-buffer)
3372       (setq gnus-newsgroup-name group)
3373       ;; Set any local variables in the group parameters.
3374       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3375       t)))
3376
3377 (defun gnus-set-global-variables ()
3378   "Set the global equivalents of the buffer-local variables.
3379 They are set to the latest values they had.  These reflect the summary
3380 buffer that was in action when the last article was fetched."
3381   (when (eq major-mode 'gnus-summary-mode)
3382     (setq gnus-summary-buffer (current-buffer))
3383     (let ((name gnus-newsgroup-name)
3384           (marked gnus-newsgroup-marked)
3385           (spam gnus-newsgroup-spam-marked)
3386           (unread gnus-newsgroup-unreads)
3387           (headers gnus-current-headers)
3388           (data gnus-newsgroup-data)
3389           (summary gnus-summary-buffer)
3390           (article-buffer gnus-article-buffer)
3391           (original gnus-original-article-buffer)
3392           (gac gnus-article-current)
3393           (reffed gnus-reffed-article-number)
3394           (score-file gnus-current-score-file)
3395           (default-charset gnus-newsgroup-charset)
3396           vlist)
3397       (let ((locals gnus-newsgroup-variables))
3398         (while locals
3399           (if (consp (car locals))
3400               (push (eval (caar locals)) vlist)
3401             (push (eval (car locals)) vlist))
3402           (setq locals (cdr locals)))
3403         (setq vlist (nreverse vlist)))
3404       (with-current-buffer gnus-group-buffer
3405         (setq gnus-newsgroup-name name
3406               gnus-newsgroup-marked marked
3407               gnus-newsgroup-spam-marked spam
3408               gnus-newsgroup-unreads unread
3409               gnus-current-headers headers
3410               gnus-newsgroup-data data
3411               gnus-article-current gac
3412               gnus-summary-buffer summary
3413               gnus-article-buffer article-buffer
3414               gnus-original-article-buffer original
3415               gnus-reffed-article-number reffed
3416               gnus-current-score-file score-file
3417               gnus-newsgroup-charset default-charset)
3418         (let ((locals gnus-newsgroup-variables))
3419           (while locals
3420             (if (consp (car locals))
3421                 (set (caar locals) (pop vlist))
3422               (set (car locals) (pop vlist)))
3423             (setq locals (cdr locals))))
3424         ;; The article buffer also has local variables.
3425         (when (gnus-buffer-live-p gnus-article-buffer)
3426           (set-buffer gnus-article-buffer)
3427           (setq gnus-summary-buffer summary))))))
3428
3429 (defun gnus-summary-article-unread-p (article)
3430   "Say whether ARTICLE is unread or not."
3431   (memq article gnus-newsgroup-unreads))
3432
3433 (defun gnus-summary-first-article-p (&optional article)
3434   "Return whether ARTICLE is the first article in the buffer."
3435   (if (not (setq article (or article (gnus-summary-article-number))))
3436       nil
3437     (eq article (caar gnus-newsgroup-data))))
3438
3439 (defun gnus-summary-last-article-p (&optional article)
3440   "Return whether ARTICLE is the last article in the buffer."
3441   (if (not (setq article (or article (gnus-summary-article-number))))
3442       ;; All non-existent numbers are the last article.  :-)
3443       t
3444     (not (cdr (gnus-data-find-list article)))))
3445
3446 (defun gnus-make-thread-indent-array (&optional n)
3447   (when (or n
3448             (progn (setq n 200) nil)
3449             (null gnus-thread-indent-array)
3450             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3451     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3452           gnus-thread-indent-array-level gnus-thread-indent-level)
3453     (while (>= n 0)
3454       (aset gnus-thread-indent-array n
3455             (make-string (* n gnus-thread-indent-level) ?\s))
3456       (setq n (1- n)))))
3457
3458 (defun gnus-update-summary-mark-positions ()
3459   "Compute where the summary marks are to go."
3460   (save-excursion
3461     (when (gnus-buffer-exists-p gnus-summary-buffer)
3462       (set-buffer gnus-summary-buffer))
3463     (let ((spec gnus-summary-line-format-spec)
3464           pos)
3465       (save-excursion
3466         (gnus-set-work-buffer)
3467         (let ((gnus-tmp-unread ?Z)
3468               (gnus-replied-mark ?Z)
3469               (gnus-score-below-mark ?Z)
3470               (gnus-score-over-mark ?Z)
3471               (gnus-undownloaded-mark ?Z)
3472               (gnus-summary-line-format-spec spec)
3473               (gnus-newsgroup-downloadable '(0))
3474               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3475               case-fold-search ignores)
3476           ;; Here, all marks are bound to Z.
3477           (gnus-summary-insert-line header
3478                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3479           (goto-char (point-min))
3480           ;; Memorize the positions of the same characters as dummy marks.
3481           (while (re-search-forward "[A-D]" nil t)
3482             (push (point) ignores))
3483           (erase-buffer)
3484           ;; We use A-D as dummy marks in order to know column positions
3485           ;; where marks should be inserted.
3486           (setq gnus-tmp-unread ?A
3487                 gnus-replied-mark ?B
3488                 gnus-score-below-mark ?C
3489                 gnus-score-over-mark ?C
3490                 gnus-undownloaded-mark ?D)
3491           (gnus-summary-insert-line header
3492                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3493           ;; Ignore characters which aren't dummy marks.
3494           (dolist (p ignores)
3495             (delete-region (goto-char (1- p)) p)
3496             (insert ?Z))
3497           (goto-char (point-min))
3498           (setq pos (list (cons 'unread
3499                                 (and (search-forward "A" nil t)
3500                                      (- (point) (point-min) 1)))))
3501           (goto-char (point-min))
3502           (push (cons 'replied (and (search-forward "B" nil t)
3503                                     (- (point) (point-min) 1)))
3504                 pos)
3505           (goto-char (point-min))
3506           (push (cons 'score (and (search-forward "C" nil t)
3507                                   (- (point) (point-min) 1)))
3508                 pos)
3509           (goto-char (point-min))
3510           (push (cons 'download (and (search-forward "D" nil t)
3511                                      (- (point) (point-min) 1)))
3512                 pos)))
3513       (setq gnus-summary-mark-positions pos))))
3514
3515 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3516   "Insert a dummy root in the summary buffer."
3517   (beginning-of-line)
3518   (gnus-add-text-properties
3519    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3520    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3521
3522 (defun gnus-summary-extract-address-component (from)
3523   (or (car (funcall gnus-extract-address-components from))
3524       from))
3525
3526 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3527   (let ((mail-parse-charset gnus-newsgroup-charset)
3528         ; Is it really necessary to do this next part for each summary line?
3529         ; Luckily, doesn't seem to slow things down much.
3530         (mail-parse-ignored-charsets
3531          (with-current-buffer gnus-summary-buffer
3532            gnus-newsgroup-ignored-charsets)))
3533     (or
3534      (and gnus-ignored-from-addresses
3535           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3536           (let ((extra-headers (mail-header-extra header))
3537                 to
3538                 newsgroups)
3539             (cond
3540              ((setq to (cdr (assq 'To extra-headers)))
3541               (concat gnus-summary-to-prefix
3542                       (inline
3543                         (gnus-summary-extract-address-component
3544                          (funcall gnus-decode-encoded-address-function to)))))
3545              ((setq newsgroups
3546                     (or
3547                      (cdr (assq 'Newsgroups extra-headers))
3548                      (and
3549                       (memq 'Newsgroups gnus-extra-headers)
3550                       (eq (car (gnus-find-method-for-group
3551                                 gnus-newsgroup-name)) 'nntp)
3552                       (gnus-group-real-name gnus-newsgroup-name))))
3553               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3554      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3555
3556 (defun gnus-summary-insert-line (gnus-tmp-header
3557                                  gnus-tmp-level gnus-tmp-current
3558                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3559                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3560                                  &optional gnus-tmp-dummy gnus-tmp-score
3561                                  gnus-tmp-process)
3562   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3563       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3564                                           gnus-tmp-level)))
3565   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3566          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3567          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3568          (gnus-tmp-score-char
3569           (if (or (null gnus-summary-default-score)
3570                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3571                       gnus-summary-zcore-fuzz))
3572               ?                         ;Whitespace
3573             (if (< gnus-tmp-score gnus-summary-default-score)
3574                 gnus-score-below-mark gnus-score-over-mark)))
3575          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3576          (gnus-tmp-replied
3577           (cond (gnus-tmp-process gnus-process-mark)
3578                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3579                  gnus-cached-mark)
3580                 (gnus-tmp-replied gnus-replied-mark)
3581                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3582                  gnus-forwarded-mark)
3583                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3584                  gnus-saved-mark)
3585                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3586                  gnus-recent-mark)
3587                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3588                  gnus-unseen-mark)
3589                 (t gnus-no-mark)))
3590          (gnus-tmp-downloaded
3591           (cond (undownloaded
3592                  gnus-undownloaded-mark)
3593                 (gnus-newsgroup-agentized
3594                  gnus-downloaded-mark)
3595                 (t
3596                  gnus-no-mark)))
3597          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3598          (gnus-tmp-name
3599           (cond
3600            ((string-match "<[^>]+> *$" gnus-tmp-from)
3601             (let ((beg (match-beginning 0)))
3602               (or (and (string-match "^\".+\"" gnus-tmp-from)
3603                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3604                   (substring gnus-tmp-from 0 beg))))
3605            ((string-match "(.+)" gnus-tmp-from)
3606             (substring gnus-tmp-from
3607                        (1+ (match-beginning 0)) (1- (match-end 0))))
3608            (t gnus-tmp-from)))
3609          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3610          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3611          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3612          (buffer-read-only nil))
3613     (when (string= gnus-tmp-name "")
3614       (setq gnus-tmp-name gnus-tmp-from))
3615     (unless (numberp gnus-tmp-lines)
3616       (setq gnus-tmp-lines -1))
3617     (if (= gnus-tmp-lines -1)
3618         (setq gnus-tmp-lines "?")
3619       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3620       (gnus-put-text-property
3621      (point)
3622      (progn (eval gnus-summary-line-format-spec) (point))
3623        'gnus-number gnus-tmp-number)
3624     (when (gnus-visual-p 'summary-highlight 'highlight)
3625       (forward-line -1)
3626       (gnus-run-hooks 'gnus-summary-update-hook)
3627       (forward-line 1))))
3628
3629 (defun gnus-summary-update-line (&optional dont-update)
3630   "Update summary line after change."
3631   (when (and gnus-summary-default-score
3632              (not gnus-summary-inhibit-highlight))
3633     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3634            (article (gnus-summary-article-number))
3635            (score (gnus-summary-article-score article)))
3636       (unless dont-update
3637         (if (and gnus-summary-mark-below
3638                  (< (gnus-summary-article-score)
3639                     gnus-summary-mark-below))
3640             ;; This article has a low score, so we mark it as read.
3641             (when (memq article gnus-newsgroup-unreads)
3642               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3643           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3644             ;; This article was previously marked as read on account
3645             ;; of a low score, but now it has risen, so we mark it as
3646             ;; unread.
3647             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3648         (gnus-summary-update-mark
3649          (if (or (null gnus-summary-default-score)
3650                  (<= (abs (- score gnus-summary-default-score))
3651                      gnus-summary-zcore-fuzz))
3652              ?                          ;Whitespace
3653            (if (< score gnus-summary-default-score)
3654                gnus-score-below-mark gnus-score-over-mark))
3655          'score))
3656       ;; Do visual highlighting.
3657       (when (gnus-visual-p 'summary-highlight 'highlight)
3658         (gnus-run-hooks 'gnus-summary-update-hook)))))
3659
3660 (defvar gnus-tmp-new-adopts nil)
3661
3662 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3663   "Return the number of articles in THREAD.
3664 This may be 0 in some cases -- if none of the articles in
3665 the thread are to be displayed."
3666   (let* ((number
3667          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3668           (cond
3669            ((not (listp thread))
3670             1)
3671            ((and (consp thread) (cdr thread))
3672             (apply
3673              '+ 1 (mapcar
3674                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3675            ((null thread)
3676             1)
3677            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3678             1)
3679            (t 0))))
3680     (when (and level (zerop level) gnus-tmp-new-adopts)
3681       (incf number
3682             (apply '+ (mapcar
3683                        'gnus-summary-number-of-articles-in-thread
3684                        gnus-tmp-new-adopts))))
3685     (if char
3686         (if (> number 1) gnus-not-empty-thread-mark
3687           gnus-empty-thread-mark)
3688       number)))
3689
3690 (defsubst gnus-summary-line-message-size (head)
3691   "Return pretty-printed version of message size.
3692 This function is intended to be used in
3693 `gnus-summary-line-format-alist'."
3694   (let ((c (or (mail-header-chars head) -1)))
3695     (cond ((< c 0) "n/a")               ; chars not available
3696           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3697           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3698           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3699           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3700
3701
3702 (defun gnus-summary-set-local-parameters (group)
3703   "Go through the local params of GROUP and set all variable specs in that list."
3704   (let ((vars '(quit-config)))          ; Ignore quit-config.
3705     (dolist (elem (gnus-group-find-parameter group))
3706       (and (consp elem)                 ; Has to be a cons.
3707            (consp (cdr elem))           ; The cdr has to be a list.
3708            (symbolp (car elem))         ; Has to be a symbol in there.
3709            (not (memq (car elem) vars))
3710            (ignore-errors               ; So we set it.
3711              (push (car elem) vars)
3712              (make-local-variable (car elem))
3713              (set (car elem) (eval (nth 1 elem))))))))
3714
3715 (defun gnus-summary-read-group (group &optional show-all no-article
3716                                       kill-buffer no-display backward
3717                                       select-articles)
3718   "Start reading news in newsgroup GROUP.
3719 If SHOW-ALL is non-nil, already read articles are also listed.
3720 If NO-ARTICLE is non-nil, no article is selected initially.
3721 If NO-DISPLAY, don't generate a summary buffer."
3722   (let (result)
3723     (while (and group
3724                 (null (setq result
3725                             (let ((gnus-auto-select-next nil))
3726                               (or (gnus-summary-read-group-1
3727                                    group show-all no-article
3728                                    kill-buffer no-display
3729                                    select-articles)
3730                                   (setq show-all nil
3731                                         select-articles nil)))))
3732                 (eq gnus-auto-select-next 'quietly))
3733       (set-buffer gnus-group-buffer)
3734       ;; The entry function called above goes to the next
3735       ;; group automatically, so we go two groups back
3736       ;; if we are searching for the previous group.
3737       (when backward
3738         (gnus-group-prev-unread-group 2))
3739       (if (not (equal group (gnus-group-group-name)))
3740           (setq group (gnus-group-group-name))
3741         (setq group nil)))
3742     result))
3743
3744 (defun gnus-summary-read-group-1 (group show-all no-article
3745                                         kill-buffer no-display
3746                                         &optional select-articles)
3747   ;; Killed foreign groups can't be entered.
3748   ;;  (when (and (not (gnus-group-native-p group))
3749   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3750   ;;    (error "Dead non-native groups can't be entered"))
3751   (gnus-message 5 "Retrieving newsgroup: %s..."
3752                 (gnus-group-decoded-name group))
3753   (let* ((new-group (gnus-summary-setup-buffer group))
3754          (quit-config (gnus-group-quit-config group))
3755          (did-select (and new-group (gnus-select-newsgroup
3756                                      group show-all select-articles))))
3757     (cond
3758      ;; This summary buffer exists already, so we just select it.
3759      ((not new-group)
3760       (gnus-set-global-variables)
3761       (when kill-buffer
3762         (gnus-kill-or-deaden-summary kill-buffer))
3763       (gnus-configure-windows 'summary 'force)
3764       (gnus-set-mode-line 'summary)
3765       (gnus-summary-position-point)
3766       (message "")
3767       t)
3768      ;; We couldn't select this group.
3769      ((null did-select)
3770       (when (and (eq major-mode 'gnus-summary-mode)
3771                  (not (equal (current-buffer) kill-buffer)))
3772         (kill-buffer (current-buffer))
3773         (if (not quit-config)
3774             (progn
3775               ;; Update the info -- marks might need to be removed,
3776               ;; for instance.
3777               (gnus-summary-update-info)
3778               (set-buffer gnus-group-buffer)
3779               (gnus-group-jump-to-group group)
3780               (gnus-group-next-unread-group 1))
3781           (gnus-handle-ephemeral-exit quit-config)))
3782       (let ((grpinfo (gnus-get-info group)))
3783         (if (null (gnus-info-read grpinfo))
3784             (gnus-message 3 "Group %s contains no messages"
3785                           (gnus-group-decoded-name group))
3786           (gnus-message 3 "Can't select group")))
3787       nil)
3788      ;; The user did a `C-g' while prompting for number of articles,
3789      ;; so we exit this group.
3790      ((eq did-select 'quit)
3791       (and (eq major-mode 'gnus-summary-mode)
3792            (not (equal (current-buffer) kill-buffer))
3793            (kill-buffer (current-buffer)))
3794       (when kill-buffer
3795         (gnus-kill-or-deaden-summary kill-buffer))
3796       (if (not quit-config)
3797           (progn
3798             (set-buffer gnus-group-buffer)
3799             (gnus-group-jump-to-group group)
3800             (gnus-group-next-unread-group 1)
3801             (gnus-configure-windows 'group 'force))
3802         (gnus-handle-ephemeral-exit quit-config))
3803       ;; Finally signal the quit.
3804       (signal 'quit nil))
3805      ;; The group was successfully selected.
3806      (t
3807       (gnus-set-global-variables)
3808       ;; Save the active value in effect when the group was entered.
3809       (setq gnus-newsgroup-active
3810             (gnus-copy-sequence
3811              (gnus-active gnus-newsgroup-name)))
3812       ;; You can change the summary buffer in some way with this hook.
3813       (gnus-run-hooks 'gnus-select-group-hook)
3814       (when (memq 'summary (gnus-update-format-specifications
3815                             nil 'summary 'summary-mode 'summary-dummy))
3816         ;; The format specification for the summary line was updated,
3817         ;; so we need to update the mark positions as well.
3818         (gnus-update-summary-mark-positions))
3819       ;; Do score processing.
3820       (when gnus-use-scoring
3821         (gnus-possibly-score-headers))
3822       ;; Check whether to fill in the gaps in the threads.
3823       (when gnus-build-sparse-threads
3824         (gnus-build-sparse-threads))
3825       ;; Find the initial limit.
3826       (if show-all
3827           (let ((gnus-newsgroup-dormant nil))
3828             (gnus-summary-initial-limit show-all))
3829         (gnus-summary-initial-limit show-all))
3830       ;; Generate the summary buffer.
3831       (unless no-display
3832         (gnus-summary-prepare))
3833       (when gnus-use-trees
3834         (gnus-tree-open group)
3835         (setq gnus-summary-highlight-line-function
3836               'gnus-tree-highlight-article))
3837       ;; If the summary buffer is empty, but there are some low-scored
3838       ;; articles or some excluded dormants, we include these in the
3839       ;; buffer.
3840       (when (and (zerop (buffer-size))
3841                  (not no-display))
3842         (cond (gnus-newsgroup-dormant
3843                (gnus-summary-limit-include-dormant))
3844               ((and gnus-newsgroup-scored show-all)
3845                (gnus-summary-limit-include-expunged t))))
3846       ;; Function `gnus-apply-kill-file' must be called in this hook.
3847       (gnus-run-hooks 'gnus-apply-kill-hook)
3848       (if (and (zerop (buffer-size))
3849                (not no-display))
3850           (progn
3851             ;; This newsgroup is empty.
3852             (gnus-summary-catchup-and-exit nil t)
3853             (gnus-message 6 "No unread news")
3854             (when kill-buffer
3855               (gnus-kill-or-deaden-summary kill-buffer))
3856             ;; Return nil from this function.
3857             nil)
3858         ;; Hide conversation thread subtrees.  We cannot do this in
3859         ;; gnus-summary-prepare-hook since kill processing may not
3860         ;; work with hidden articles.
3861         (gnus-summary-maybe-hide-threads)
3862         (when kill-buffer
3863           (gnus-kill-or-deaden-summary kill-buffer))
3864         (gnus-summary-auto-select-subject)
3865         ;; Show first unread article if requested.
3866         (if (and (not no-article)
3867                  (not no-display)
3868                  gnus-newsgroup-unreads
3869                  gnus-auto-select-first)
3870             (progn
3871               (gnus-configure-windows 'summary)
3872               (let ((art (gnus-summary-article-number)))
3873                 (unless (and (not gnus-plugged)
3874                              (or (memq art gnus-newsgroup-undownloaded)
3875                                  (memq art gnus-newsgroup-downloadable)))
3876                   (gnus-summary-goto-article art))))
3877           ;; Don't select any articles.
3878           (gnus-summary-position-point)
3879           (gnus-configure-windows 'summary 'force)
3880           (gnus-set-mode-line 'summary))
3881         (when (and gnus-auto-center-group
3882                    (get-buffer-window gnus-group-buffer t))
3883           ;; Gotta use windows, because recenter does weird stuff if
3884           ;; the current buffer ain't the displayed window.
3885           (let ((owin (selected-window)))
3886             (select-window (get-buffer-window gnus-group-buffer t))
3887             (when (gnus-group-goto-group group)
3888               (recenter))
3889             (select-window owin)))
3890         ;; Mark this buffer as "prepared".
3891         (setq gnus-newsgroup-prepared t)
3892         (gnus-run-hooks 'gnus-summary-prepared-hook)
3893         (unless (gnus-ephemeral-group-p group)
3894           (gnus-group-update-group group))
3895         t)))))
3896
3897 (defun gnus-summary-auto-select-subject ()
3898   "Select the subject line on initial group entry."
3899   (goto-char (point-min))
3900   (cond
3901    ((eq gnus-auto-select-subject 'best)
3902     (gnus-summary-best-unread-subject))
3903    ((eq gnus-auto-select-subject 'unread)
3904     (gnus-summary-first-unread-subject))
3905    ((eq gnus-auto-select-subject 'unseen)
3906     (gnus-summary-first-unseen-subject))
3907    ((eq gnus-auto-select-subject 'unseen-or-unread)
3908     (gnus-summary-first-unseen-or-unread-subject))
3909    ((eq gnus-auto-select-subject 'first)
3910     ;; Do nothing.
3911     )
3912    ((functionp gnus-auto-select-subject)
3913     (funcall gnus-auto-select-subject))))
3914
3915 (defun gnus-summary-prepare ()
3916   "Generate the summary buffer."
3917   (interactive)
3918   (let ((buffer-read-only nil))
3919     (erase-buffer)
3920     (setq gnus-newsgroup-data nil
3921           gnus-newsgroup-data-reverse nil)
3922     (gnus-run-hooks 'gnus-summary-generate-hook)
3923     ;; Generate the buffer, either with threads or without.
3924     (when gnus-newsgroup-headers
3925       (gnus-summary-prepare-threads
3926        (if gnus-show-threads
3927            (gnus-sort-gathered-threads
3928             (funcall gnus-summary-thread-gathering-function
3929                      (gnus-sort-threads
3930                       (gnus-cut-threads (gnus-make-threads)))))
3931          ;; Unthreaded display.
3932          (gnus-sort-articles gnus-newsgroup-headers))))
3933     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3934     ;; Call hooks for modifying summary buffer.
3935     (goto-char (point-min))
3936     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3937
3938 (defsubst gnus-general-simplify-subject (subject)
3939   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3940   (setq subject
3941         (cond
3942          ;; Truncate the subject.
3943          (gnus-simplify-subject-functions
3944           (gnus-map-function gnus-simplify-subject-functions subject))
3945          ((numberp gnus-summary-gather-subject-limit)
3946           (setq subject (gnus-simplify-subject-re subject))
3947           (if (> (length subject) gnus-summary-gather-subject-limit)
3948               (substring subject 0 gnus-summary-gather-subject-limit)
3949             subject))
3950          ;; Fuzzily simplify it.
3951          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3952           (gnus-simplify-subject-fuzzy subject))
3953          ;; Just remove the leading "Re:".
3954          (t
3955           (gnus-simplify-subject-re subject))))
3956
3957   (if (and gnus-summary-gather-exclude-subject
3958            (string-match gnus-summary-gather-exclude-subject subject))
3959       nil                         ; This article shouldn't be gathered
3960     subject))
3961
3962 (defun gnus-summary-simplify-subject-query ()
3963   "Query where the respool algorithm would put this article."
3964   (interactive)
3965   (gnus-summary-select-article)
3966   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3967
3968 (defun gnus-gather-threads-by-subject (threads)
3969   "Gather threads by looking at Subject headers."
3970   (if (not gnus-summary-make-false-root)
3971       threads
3972     (let ((hashtb (gnus-make-hashtable 1024))
3973           (prev threads)
3974           (result threads)
3975           subject hthread whole-subject)
3976       (while threads
3977         (setq subject (gnus-general-simplify-subject
3978                        (setq whole-subject (mail-header-subject
3979                                             (caar threads)))))
3980         (when subject
3981           (if (setq hthread (gnus-gethash subject hashtb))
3982               (progn
3983                 ;; We enter a dummy root into the thread, if we
3984                 ;; haven't done that already.
3985                 (unless (stringp (caar hthread))
3986                   (setcar hthread (list whole-subject (car hthread))))
3987                 ;; We add this new gathered thread to this gathered
3988                 ;; thread.
3989                 (setcdr (car hthread)
3990                         (nconc (cdar hthread) (list (car threads))))
3991                 ;; Remove it from the list of threads.
3992                 (setcdr prev (cdr threads))
3993                 (setq threads prev))
3994             ;; Enter this thread into the hash table.
3995             (gnus-sethash subject
3996                           (if gnus-summary-make-false-root-always
3997                               (progn
3998                                 ;; If you want a dummy root above all
3999                                 ;; threads...
4000                                 (setcar threads (list whole-subject
4001                                                       (car threads)))
4002                                 threads)
4003                             threads)
4004                           hashtb)))
4005         (setq prev threads)
4006         (setq threads (cdr threads)))
4007       result)))
4008
4009 (defun gnus-gather-threads-by-references (threads)
4010   "Gather threads by looking at References headers."
4011   (let ((idhashtb (gnus-make-hashtable 1024))
4012         (thhashtb (gnus-make-hashtable 1024))
4013         (prev threads)
4014         (result threads)
4015         ids references id gthread gid entered ref)
4016     (while threads
4017       (when (setq references (mail-header-references (caar threads)))
4018         (setq id (mail-header-id (caar threads))
4019               ids (inline (gnus-split-references references))
4020               entered nil)
4021         (while (setq ref (pop ids))
4022           (setq ids (delete ref ids))
4023           (if (not (setq gid (gnus-gethash ref idhashtb)))
4024               (progn
4025                 (gnus-sethash ref id idhashtb)
4026                 (gnus-sethash id threads thhashtb))
4027             (setq gthread (gnus-gethash gid thhashtb))
4028             (unless entered
4029               ;; We enter a dummy root into the thread, if we
4030               ;; haven't done that already.
4031               (unless (stringp (caar gthread))
4032                 (setcar gthread (list (mail-header-subject (caar gthread))
4033                                       (car gthread))))
4034               ;; We add this new gathered thread to this gathered
4035               ;; thread.
4036               (setcdr (car gthread)
4037                       (nconc (cdar gthread) (list (car threads)))))
4038             ;; Add it into the thread hash table.
4039             (gnus-sethash id gthread thhashtb)
4040             (setq entered t)
4041             ;; Remove it from the list of threads.
4042             (setcdr prev (cdr threads))
4043             (setq threads prev))))
4044       (setq prev threads)
4045       (setq threads (cdr threads)))
4046     result))
4047
4048 (defun gnus-sort-gathered-threads (threads)
4049   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4050   (let ((result threads))
4051     (while threads
4052       (when (stringp (caar threads))
4053         (setcdr (car threads)
4054                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4055       (setq threads (cdr threads)))
4056     result))
4057
4058 (defun gnus-thread-loop-p (root thread)
4059   "Say whether ROOT is in THREAD."
4060   (let ((stack (list thread))
4061         (infloop 0)
4062         th)
4063     (while (setq thread (pop stack))
4064       (setq th (cdr thread))
4065       (while (and th
4066                   (not (eq (caar th) root)))
4067         (pop th))
4068       (if th
4069           ;; We have found a loop.
4070           (let (ref-dep)
4071             (setcdr thread (delq (car th) (cdr thread)))
4072             (if (boundp (setq ref-dep (intern "none"
4073                                               gnus-newsgroup-dependencies)))
4074                 (setcdr (symbol-value ref-dep)
4075                         (nconc (cdr (symbol-value ref-dep))
4076                                (list (car th))))
4077               (set ref-dep (list nil (car th))))
4078             (setq infloop 1
4079                   stack nil))
4080         ;; Push all the subthreads onto the stack.
4081         (push (cdr thread) stack)))
4082     infloop))
4083
4084 (defun gnus-make-threads ()
4085   "Go through the dependency hashtb and find the roots.  Return all threads."
4086   (let (threads)
4087     (while (catch 'infloop
4088              (mapatoms
4089               (lambda (refs)
4090                 ;; Deal with self-referencing References loops.
4091                 (when (and (car (symbol-value refs))
4092                            (not (zerop
4093                                  (apply
4094                                   '+
4095                                   (mapcar
4096                                    (lambda (thread)
4097                                      (gnus-thread-loop-p
4098                                       (car (symbol-value refs)) thread))
4099                                    (cdr (symbol-value refs)))))))
4100                   (setq threads nil)
4101                   (throw 'infloop t))
4102                 (unless (car (symbol-value refs))
4103                   ;; These threads do not refer back to any other
4104                   ;; articles, so they're roots.
4105                   (setq threads (append (cdr (symbol-value refs)) threads))))
4106               gnus-newsgroup-dependencies)))
4107     threads))
4108
4109 ;; Build the thread tree.
4110 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4111   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4112
4113 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4114 if it was already present.
4115
4116 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4117 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4118 Message-IDs will be renamed to a unique Message-ID before being
4119 entered.
4120
4121 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4122   (let* ((id (mail-header-id header))
4123          (id-dep (and id (intern id dependencies)))
4124          parent-id ref ref-dep ref-header replaced)
4125     ;; Enter this `header' in the `dependencies' table.
4126     (cond
4127      ((not id-dep)
4128       (setq header nil))
4129      ;; The first two cases do the normal part: enter a new `header'
4130      ;; in the `dependencies' table.
4131      ((not (boundp id-dep))
4132       (set id-dep (list header)))
4133      ((null (car (symbol-value id-dep)))
4134       (setcar (symbol-value id-dep) header))
4135
4136      ;; From here the `header' was already present in the
4137      ;; `dependencies' table.
4138      (force-new
4139       ;; Overrides an existing entry;
4140       ;; just set the header part of the entry.
4141       (setcar (symbol-value id-dep) header)
4142       (setq replaced t))
4143
4144      ;; Renames the existing `header' to a unique Message-ID.
4145      ((not gnus-summary-ignore-duplicates)
4146       ;; An article with this Message-ID has already been seen.
4147       ;; We rename the Message-ID.
4148       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4149            (list header))
4150       (mail-header-set-id header id))
4151
4152      ;; The last case ignores an existing entry, except it adds any
4153      ;; additional Xrefs (in case the two articles came from different
4154      ;; servers.
4155      ;; Also sets `header' to `nil' meaning that the `dependencies'
4156      ;; table was *not* modified.
4157      (t
4158       (mail-header-set-xref
4159        (car (symbol-value id-dep))
4160        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4161                    "")
4162                (or (mail-header-xref header) "")))
4163       (setq header nil)))
4164
4165     (when (and header (not replaced))
4166       ;; First check that we are not creating a References loop.
4167       (setq parent-id (gnus-parent-id (mail-header-references header)))
4168       (setq ref parent-id)
4169       (while (and ref
4170                   (setq ref-dep (intern-soft ref dependencies))
4171                   (boundp ref-dep)
4172                   (setq ref-header (car (symbol-value ref-dep))))
4173         (if (string= id ref)
4174             ;; Yuk!  This is a reference loop.  Make the article be a
4175             ;; root article.
4176             (progn
4177               (mail-header-set-references (car (symbol-value id-dep)) "none")
4178               (setq ref nil)
4179               (setq parent-id nil))
4180           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4181       (setq ref-dep (intern (or parent-id "none") dependencies))
4182       (if (boundp ref-dep)
4183           (setcdr (symbol-value ref-dep)
4184                   (nconc (cdr (symbol-value ref-dep))
4185                          (list (symbol-value id-dep))))
4186         (set ref-dep (list nil (symbol-value id-dep)))))
4187     header))
4188
4189 (defun gnus-extract-message-id-from-in-reply-to (string)
4190   (if (string-match "<[^>]+>" string)
4191       (substring string (match-beginning 0) (match-end 0))
4192     nil))
4193
4194 (defun gnus-build-sparse-threads ()
4195   (let ((headers gnus-newsgroup-headers)
4196         (mail-parse-charset gnus-newsgroup-charset)
4197         (gnus-summary-ignore-duplicates t)
4198         header references generation relations
4199         subject child end new-child date)
4200     ;; First we create an alist of generations/relations, where
4201     ;; generations is how much we trust the relation, and the relation
4202     ;; is parent/child.
4203     (gnus-message 7 "Making sparse threads...")
4204     (save-excursion
4205       (nnheader-set-temp-buffer " *gnus sparse threads*")
4206       (while (setq header (pop headers))
4207         (when (and (setq references (mail-header-references header))
4208                    (not (string= references "")))
4209           (insert references)
4210           (setq child (mail-header-id header)
4211                 subject (mail-header-subject header)
4212                 date (mail-header-date header)
4213                 generation 0)
4214           (while (search-backward ">" nil t)
4215             (setq end (1+ (point)))
4216             (when (search-backward "<" nil t)
4217               (setq new-child (buffer-substring (point) end))
4218               (push (list (incf generation)
4219                           child (setq child new-child)
4220                           subject date)
4221                     relations)))
4222           (when child
4223             (push (list (1+ generation) child nil subject) relations))
4224           (erase-buffer)))
4225       (kill-buffer (current-buffer)))
4226     ;; Sort over trustworthiness.
4227     (mapcar
4228      (lambda (relation)
4229        (when (gnus-dependencies-add-header
4230               (make-full-mail-header
4231                gnus-reffed-article-number
4232                (nth 3 relation) "" (or (nth 4 relation) "")
4233                (nth 1 relation)
4234                (or (nth 2 relation) "") 0 0 "")
4235               gnus-newsgroup-dependencies nil)
4236          (push gnus-reffed-article-number gnus-newsgroup-limit)
4237          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4238          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4239                gnus-newsgroup-reads)
4240          (decf gnus-reffed-article-number)))
4241      (sort relations 'car-less-than-car))
4242     (gnus-message 7 "Making sparse threads...done")))
4243
4244 (defun gnus-build-old-threads ()
4245   ;; Look at all the articles that refer back to old articles, and
4246   ;; fetch the headers for the articles that aren't there.  This will
4247   ;; build complete threads - if the roots haven't been expired by the
4248   ;; server, that is.
4249   (let ((mail-parse-charset gnus-newsgroup-charset)
4250         id heads)
4251     (mapatoms
4252      (lambda (refs)
4253        (when (not (car (symbol-value refs)))
4254          (setq heads (cdr (symbol-value refs)))
4255          (while heads
4256            (if (memq (mail-header-number (caar heads))
4257                      gnus-newsgroup-dormant)
4258                (setq heads (cdr heads))
4259              (setq id (symbol-name refs))
4260              (while (and (setq id (gnus-build-get-header id))
4261                          (not (car (gnus-id-to-thread id)))))
4262              (setq heads nil)))))
4263      gnus-newsgroup-dependencies)))
4264
4265 (defsubst gnus-remove-odd-characters (string)
4266   "Translate STRING into something that doesn't contain weird characters."
4267   (mm-subst-char-in-string
4268    ?\r ?\-
4269    (mm-subst-char-in-string ?\n ?\- string t) t))
4270
4271 ;; This function has to be called with point after the article number
4272 ;; on the beginning of the line.
4273 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4274   (let ((eol (point-at-eol))
4275         (buffer (current-buffer))
4276         header references in-reply-to)
4277
4278     ;; overview: [num subject from date id refs chars lines misc]
4279     (unwind-protect
4280         (let (x)
4281           (narrow-to-region (point) eol)
4282           (unless (eobp)
4283             (forward-char))
4284
4285           (setq header
4286                 (make-full-mail-header
4287                  number                 ; number
4288                  (condition-case ()     ; subject
4289                      (gnus-remove-odd-characters
4290                       (funcall gnus-decode-encoded-word-function
4291                                (setq x (nnheader-nov-field))))
4292                    (error x))
4293                  (condition-case ()     ; from
4294                      (gnus-remove-odd-characters
4295                       (funcall gnus-decode-encoded-address-function
4296                                (setq x (nnheader-nov-field))))
4297                    (error x))
4298                  (nnheader-nov-field)   ; date
4299                  (nnheader-nov-read-message-id number)  ; id
4300                  (setq references (nnheader-nov-field)) ; refs
4301                  (nnheader-nov-read-integer) ; chars
4302                  (nnheader-nov-read-integer) ; lines
4303                  (unless (eobp)
4304                    (if (looking-at "Xref: ")
4305                        (goto-char (match-end 0)))
4306                    (nnheader-nov-field)) ; Xref
4307                  (nnheader-nov-parse-extra)))) ; extra
4308
4309       (widen))
4310
4311     (when (and (string= references "")
4312                (setq in-reply-to (mail-header-extra header))
4313                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4314       (mail-header-set-references
4315        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4316
4317     (when gnus-alter-header-function
4318       (funcall gnus-alter-header-function header))
4319     (gnus-dependencies-add-header header dependencies force-new)))
4320
4321 (defun gnus-build-get-header (id)
4322   "Look through the buffer of NOV lines and find the header to ID.
4323 Enter this line into the dependencies hash table, and return
4324 the id of the parent article (if any)."
4325   (let ((deps gnus-newsgroup-dependencies)
4326         found header)
4327     (prog1
4328         (save-excursion
4329           (set-buffer nntp-server-buffer)
4330           (let ((case-fold-search nil))
4331             (goto-char (point-min))
4332             (while (and (not found)
4333                         (search-forward id nil t))
4334               (beginning-of-line)
4335               (setq found (looking-at
4336                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4337                                    (regexp-quote id))))
4338               (or found (beginning-of-line 2)))
4339             (when found
4340               (beginning-of-line)
4341               (and
4342                (setq header (gnus-nov-parse-line
4343                              (read (current-buffer)) deps))
4344                (gnus-parent-id (mail-header-references header))))))
4345       (when header
4346         (let ((number (mail-header-number header)))
4347           (push number gnus-newsgroup-limit)
4348           (push header gnus-newsgroup-headers)
4349           (if (memq number gnus-newsgroup-unselected)
4350               (progn
4351                 (setq gnus-newsgroup-unreads
4352                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4353                                                number))
4354                 (setq gnus-newsgroup-unselected
4355                       (delq number gnus-newsgroup-unselected)))
4356             (push number gnus-newsgroup-ancient)))))))
4357
4358 (defun gnus-build-all-threads ()
4359   "Read all the headers."
4360   (let ((gnus-summary-ignore-duplicates t)
4361         (mail-parse-charset gnus-newsgroup-charset)
4362         (dependencies gnus-newsgroup-dependencies)
4363         header article)
4364     (save-excursion
4365       (set-buffer nntp-server-buffer)
4366       (let ((case-fold-search nil))
4367         (goto-char (point-min))
4368         (while (not (eobp))
4369           (ignore-errors
4370             (setq article (read (current-buffer))
4371                   header (gnus-nov-parse-line article dependencies)))
4372           (when header
4373             (with-current-buffer gnus-summary-buffer
4374               (push header gnus-newsgroup-headers)
4375               (if (memq (setq article (mail-header-number header))
4376                         gnus-newsgroup-unselected)
4377                   (progn
4378                     (setq gnus-newsgroup-unreads
4379                           (gnus-add-to-sorted-list
4380                            gnus-newsgroup-unreads article))
4381                     (setq gnus-newsgroup-unselected
4382                           (delq article gnus-newsgroup-unselected)))
4383                 (push article gnus-newsgroup-ancient)))
4384             (forward-line 1)))))))
4385
4386 (defun gnus-summary-update-article-line (article header)
4387   "Update the line for ARTICLE using HEADER."
4388   (let* ((id (mail-header-id header))
4389          (thread (gnus-id-to-thread id)))
4390     (unless thread
4391       (error "Article in no thread"))
4392     ;; Update the thread.
4393     (setcar thread header)
4394     (gnus-summary-goto-subject article)
4395     (let* ((datal (gnus-data-find-list article))
4396            (data (car datal))
4397            (buffer-read-only nil)
4398            (level (gnus-summary-thread-level)))
4399       (gnus-delete-line)
4400       (let ((inserted (- (point)
4401                          (progn
4402                            (gnus-summary-insert-line
4403                             header level nil
4404                             (memq article gnus-newsgroup-undownloaded)
4405                             (gnus-article-mark article)
4406                             (memq article gnus-newsgroup-replied)
4407                             (memq article gnus-newsgroup-expirable)
4408                             ;; Only insert the Subject string when it's different
4409                             ;; from the previous Subject string.
4410                             (if (and
4411                                  gnus-show-threads
4412                                  (gnus-subject-equal
4413                                   (condition-case ()
4414                                       (mail-header-subject
4415                                        (gnus-data-header
4416                                         (cadr
4417                                          (gnus-data-find-list
4418                                           article
4419                                           (gnus-data-list t)))))
4420                                     ;; Error on the side of excessive subjects.
4421                                     (error ""))
4422                                   (mail-header-subject header)))
4423                                 ""
4424                               (mail-header-subject header))
4425                             nil (cdr (assq article gnus-newsgroup-scored))
4426                             (memq article gnus-newsgroup-processable))
4427                            (point)))))
4428         (when (cdr datal)
4429           (gnus-data-update-list
4430            (cdr datal)
4431            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4432
4433 (defun gnus-summary-update-article (article &optional iheader)
4434   "Update ARTICLE in the summary buffer."
4435   (set-buffer gnus-summary-buffer)
4436   (let* ((header (gnus-summary-article-header article))
4437          (id (mail-header-id header))
4438          (data (gnus-data-find article))
4439          (thread (gnus-id-to-thread id))
4440          (references (mail-header-references header))
4441          (parent
4442           (gnus-id-to-thread
4443            (or (gnus-parent-id
4444                 (when (and references
4445                            (not (equal "" references)))
4446                   references))
4447                "none")))
4448          (buffer-read-only nil)
4449          (old (car thread)))
4450     (when thread
4451       (unless iheader
4452         (setcar thread nil)
4453         (when parent
4454           (delq thread parent)))
4455       (if (gnus-summary-insert-subject id header)
4456           ;; Set the (possibly) new article number in the data structure.
4457           (gnus-data-set-number data (gnus-id-to-article id))
4458         (setcar thread old)
4459         nil))))
4460
4461 (defun gnus-rebuild-thread (id &optional line)
4462   "Rebuild the thread containing ID.
4463 If LINE, insert the rebuilt thread starting on line LINE."
4464   (let ((buffer-read-only nil)
4465         old-pos current thread data)
4466     (if (not gnus-show-threads)
4467         (setq thread (list (car (gnus-id-to-thread id))))
4468       ;; Get the thread this article is part of.
4469       (setq thread (gnus-remove-thread id)))
4470     (setq old-pos (point-at-bol))
4471     (setq current (save-excursion
4472                     (and (re-search-backward "[\r\n]" nil t)
4473                          (gnus-summary-article-number))))
4474     ;; If this is a gathered thread, we have to go some re-gathering.
4475     (when (stringp (car thread))
4476       (let ((subject (car thread))
4477             roots thr)
4478         (setq thread (cdr thread))
4479         (while thread
4480           (unless (memq (setq thr (gnus-id-to-thread
4481                                    (gnus-root-id
4482                                     (mail-header-id (caar thread)))))
4483                         roots)
4484             (push thr roots))
4485           (setq thread (cdr thread)))
4486         ;; We now have all (unique) roots.
4487         (if (= (length roots) 1)
4488             ;; All the loose roots are now one solid root.
4489             (setq thread (car roots))
4490           (setq thread (cons subject (gnus-sort-threads roots))))))
4491     (let (threads)
4492       ;; We then insert this thread into the summary buffer.
4493       (when line
4494         (goto-char (point-min))
4495         (forward-line (1- line)))
4496       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4497         (if gnus-show-threads
4498             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4499           (gnus-summary-prepare-unthreaded thread))
4500         (setq data (nreverse gnus-newsgroup-data))
4501         (setq threads gnus-newsgroup-threads))
4502       ;; We splice the new data into the data structure.
4503       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4504       ;;!!! then we want to insert at the beginning of the buffer.
4505       ;;!!! That happens to be true with Gnus now, but that may
4506       ;;!!! change in the future.  Perhaps.
4507       (gnus-data-enter-list
4508        (if line nil current) data (- (point) old-pos))
4509       (setq gnus-newsgroup-threads
4510             (nconc threads gnus-newsgroup-threads))
4511       (gnus-data-compute-positions))))
4512
4513 (defun gnus-number-to-header (number)
4514   "Return the header for article NUMBER."
4515   (let ((headers gnus-newsgroup-headers))
4516     (while (and headers
4517                 (not (= number (mail-header-number (car headers)))))
4518       (pop headers))
4519     (when headers
4520       (car headers))))
4521
4522 (defun gnus-parent-headers (in-headers &optional generation)
4523   "Return the headers of the GENERATIONeth parent of HEADERS."
4524   (unless generation
4525     (setq generation 1))
4526   (let ((parent t)
4527         (headers in-headers)
4528         references)
4529     (while (and parent
4530                 (not (zerop generation))
4531                 (setq references (mail-header-references headers)))
4532       (setq headers (if (and references
4533                              (setq parent (gnus-parent-id references)))
4534                         (car (gnus-id-to-thread parent))
4535                       nil))
4536       (decf generation))
4537     (and (not (eq headers in-headers))
4538          headers)))
4539
4540 (defun gnus-id-to-thread (id)
4541   "Return the (sub-)thread where ID appears."
4542   (gnus-gethash id gnus-newsgroup-dependencies))
4543
4544 (defun gnus-id-to-article (id)
4545   "Return the article number of ID."
4546   (let ((thread (gnus-id-to-thread id)))
4547     (when (and thread
4548                (car thread))
4549       (mail-header-number (car thread)))))
4550
4551 (defun gnus-id-to-header (id)
4552   "Return the article headers of ID."
4553   (car (gnus-id-to-thread id)))
4554
4555 (defun gnus-article-displayed-root-p (article)
4556   "Say whether ARTICLE is a root(ish) article."
4557   (let ((level (gnus-summary-thread-level article))
4558         (refs (mail-header-references  (gnus-summary-article-header article)))
4559         particle)
4560     (cond
4561      ((null level) nil)
4562      ((zerop level) t)
4563      ((null refs) t)
4564      ((null (gnus-parent-id refs)) t)
4565      ((and (= 1 level)
4566            (null (setq particle (gnus-id-to-article
4567                                  (gnus-parent-id refs))))
4568            (null (gnus-summary-thread-level particle)))))))
4569
4570 (defun gnus-root-id (id)
4571   "Return the id of the root of the thread where ID appears."
4572   (let (last-id prev)
4573     (while (and id (setq prev (car (gnus-id-to-thread id))))
4574       (setq last-id id
4575             id (gnus-parent-id (mail-header-references prev))))
4576     last-id))
4577
4578 (defun gnus-articles-in-thread (thread)
4579   "Return the list of articles in THREAD."
4580   (cons (mail-header-number (car thread))
4581         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4582
4583 (defun gnus-remove-thread (id &optional dont-remove)
4584   "Remove the thread that has ID in it."
4585   (let (headers thread last-id)
4586     ;; First go up in this thread until we find the root.
4587     (setq last-id (gnus-root-id id)
4588           headers (message-flatten-list (gnus-id-to-thread last-id)))
4589     ;; We have now found the real root of this thread.  It might have
4590     ;; been gathered into some loose thread, so we have to search
4591     ;; through the threads to find the thread we wanted.
4592     (let ((threads gnus-newsgroup-threads)
4593           sub)
4594       (while threads
4595         (setq sub (car threads))
4596         (if (stringp (car sub))
4597             ;; This is a gathered thread, so we look at the roots
4598             ;; below it to find whether this article is in this
4599             ;; gathered root.
4600             (progn
4601               (setq sub (cdr sub))
4602               (while sub
4603                 (when (member (caar sub) headers)
4604                   (setq thread (car threads)
4605                         threads nil
4606                         sub nil))
4607                 (setq sub (cdr sub))))
4608           ;; It's an ordinary thread, so we check it.
4609           (when (eq (car sub) (car headers))
4610             (setq thread sub
4611                   threads nil)))
4612         (setq threads (cdr threads)))
4613       ;; If this article is in no thread, then it's a root.
4614       (if thread
4615           (unless dont-remove
4616             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4617         (setq thread (gnus-id-to-thread last-id)))
4618       (when thread
4619         (prog1
4620             thread                      ; We return this thread.
4621           (unless dont-remove
4622             (if (stringp (car thread))
4623                 (progn
4624                   ;; If we use dummy roots, then we have to remove the
4625                   ;; dummy root as well.
4626                   (when (eq gnus-summary-make-false-root 'dummy)
4627                     ;; We go to the dummy root by going to
4628                     ;; the first sub-"thread", and then one line up.
4629                     (gnus-summary-goto-article
4630                      (mail-header-number (caadr thread)))
4631                     (forward-line -1)
4632                     (gnus-delete-line)
4633                     (gnus-data-compute-positions))
4634                   (setq thread (cdr thread))
4635                   (while thread
4636                     (gnus-remove-thread-1 (car thread))
4637                     (setq thread (cdr thread))))
4638               (gnus-remove-thread-1 thread))))))))
4639
4640 (defun gnus-remove-thread-1 (thread)
4641   "Remove the thread THREAD recursively."
4642   (let ((number (mail-header-number (pop thread)))
4643         d)
4644     (setq thread (reverse thread))
4645     (while thread
4646       (gnus-remove-thread-1 (pop thread)))
4647     (when (setq d (gnus-data-find number))
4648       (goto-char (gnus-data-pos d))
4649       (gnus-summary-show-thread)
4650       (gnus-data-remove
4651        number
4652        (- (point-at-bol)
4653           (prog1
4654               (1+ (point-at-eol))
4655             (gnus-delete-line)))))))
4656
4657 (defun gnus-sort-threads-recursive (threads func)
4658   (sort (mapcar (lambda (thread)
4659                   (cons (car thread)
4660                         (and (cdr thread)
4661                              (gnus-sort-threads-recursive (cdr thread) func))))
4662                 threads) func))
4663
4664 (defun gnus-sort-threads-loop (threads func)
4665   (let* ((superthread (cons nil threads))
4666          (stack (list (cons superthread threads)))
4667          remaining-threads thread)
4668     (while stack
4669       (setq remaining-threads (cdr (car stack)))
4670       (if remaining-threads
4671           (progn (setq thread (car remaining-threads))
4672                  (setcdr (car stack) (cdr remaining-threads))
4673                  (if (cdr thread)
4674                      (push (cons thread (cdr thread)) stack)))
4675         (setq thread (caar stack))
4676         (setcdr thread (sort (cdr thread) func))
4677         (pop stack)))
4678     (cdr superthread)))
4679
4680 (defun gnus-sort-threads (threads)
4681   "Sort THREADS."
4682   (if (not gnus-thread-sort-functions)
4683       threads
4684     (gnus-message 8 "Sorting threads...")
4685     (prog1
4686         (condition-case nil
4687             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4688               (gnus-sort-threads-recursive
4689                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4690           ;; Even after binding max-lisp-eval-depth, the recursive
4691           ;; sorter might fail for very long threads.  In that case,
4692           ;; try using a (less well-tested) non-recursive sorter.
4693           (error (gnus-sort-threads-loop
4694                   threads (gnus-make-sort-function
4695                            gnus-thread-sort-functions))))
4696       (gnus-message 8 "Sorting threads...done"))))
4697
4698 (defun gnus-sort-articles (articles)
4699   "Sort ARTICLES."
4700   (when gnus-article-sort-functions
4701     (gnus-message 7 "Sorting articles...")
4702     (prog1
4703         (setq gnus-newsgroup-headers
4704               (sort articles (gnus-make-sort-function
4705                               gnus-article-sort-functions)))
4706       (gnus-message 7 "Sorting articles...done"))))
4707
4708 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4709 (defmacro gnus-thread-header (thread)
4710   "Return header of first article in THREAD.
4711 Note that THREAD must never, ever be anything else than a variable -
4712 using some other form will lead to serious barfage."
4713   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4714   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4715   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4716         (vector thread) 2))
4717
4718 (defsubst gnus-article-sort-by-number (h1 h2)
4719   "Sort articles by article number."
4720   (< (mail-header-number h1)
4721      (mail-header-number h2)))
4722
4723 (defun gnus-thread-sort-by-number (h1 h2)
4724   "Sort threads by root article number."
4725   (gnus-article-sort-by-number
4726    (gnus-thread-header h1) (gnus-thread-header h2)))
4727
4728 (defsubst gnus-article-sort-by-random (h1 h2)
4729   "Sort articles by article number."
4730   (zerop (random 2)))
4731
4732 (defun gnus-thread-sort-by-random (h1 h2)
4733   "Sort threads by root article number."
4734   (gnus-article-sort-by-random
4735    (gnus-thread-header h1) (gnus-thread-header h2)))
4736
4737 (defsubst gnus-article-sort-by-lines (h1 h2)
4738   "Sort articles by article Lines header."
4739   (< (mail-header-lines h1)
4740      (mail-header-lines h2)))
4741
4742 (defun gnus-thread-sort-by-lines (h1 h2)
4743   "Sort threads by root article Lines header."
4744   (gnus-article-sort-by-lines
4745    (gnus-thread-header h1) (gnus-thread-header h2)))
4746
4747 (defsubst gnus-article-sort-by-chars (h1 h2)
4748   "Sort articles by octet length."
4749   (< (mail-header-chars h1)
4750      (mail-header-chars h2)))
4751
4752 (defun gnus-thread-sort-by-chars (h1 h2)
4753   "Sort threads by root article octet length."
4754   (gnus-article-sort-by-chars
4755    (gnus-thread-header h1) (gnus-thread-header h2)))
4756
4757 (defsubst gnus-article-sort-by-author (h1 h2)
4758   "Sort articles by root author."
4759   (string-lessp
4760    (let ((extract (funcall
4761                    gnus-extract-address-components
4762                    (mail-header-from h1))))
4763      (or (car extract) (cadr extract) ""))
4764    (let ((extract (funcall
4765                    gnus-extract-address-components
4766                    (mail-header-from h2))))
4767      (or (car extract) (cadr extract) ""))))
4768
4769 (defun gnus-thread-sort-by-author (h1 h2)
4770   "Sort threads by root author."
4771   (gnus-article-sort-by-author
4772    (gnus-thread-header h1)  (gnus-thread-header h2)))
4773
4774 (defsubst gnus-article-sort-by-recipient (h1 h2)
4775   "Sort articles by recipient."
4776   (string-lessp
4777    (let ((extract (funcall
4778                    gnus-extract-address-components
4779                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4780      (or (car extract) (cadr extract)))
4781    (let ((extract (funcall
4782                    gnus-extract-address-components
4783                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4784      (or (car extract) (cadr extract)))))
4785
4786 (defun gnus-thread-sort-by-recipient (h1 h2)
4787   "Sort threads by root recipient."
4788   (gnus-article-sort-by-recipient
4789    (gnus-thread-header h1) (gnus-thread-header h2)))
4790
4791 (defsubst gnus-article-sort-by-subject (h1 h2)
4792   "Sort articles by root subject."
4793   (string-lessp
4794    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4795    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4796
4797 (defun gnus-thread-sort-by-subject (h1 h2)
4798   "Sort threads by root subject."
4799   (gnus-article-sort-by-subject
4800    (gnus-thread-header h1) (gnus-thread-header h2)))
4801
4802 (defsubst gnus-article-sort-by-date (h1 h2)
4803   "Sort articles by root article date."
4804   (time-less-p
4805    (gnus-date-get-time (mail-header-date h1))
4806    (gnus-date-get-time (mail-header-date h2))))
4807
4808 (defun gnus-thread-sort-by-date (h1 h2)
4809   "Sort threads by root article date."
4810   (gnus-article-sort-by-date
4811    (gnus-thread-header h1) (gnus-thread-header h2)))
4812
4813 (defsubst gnus-article-sort-by-score (h1 h2)
4814   "Sort articles by root article score.
4815 Unscored articles will be counted as having a score of zero."
4816   (> (or (cdr (assq (mail-header-number h1)
4817                     gnus-newsgroup-scored))
4818          gnus-summary-default-score 0)
4819      (or (cdr (assq (mail-header-number h2)
4820                     gnus-newsgroup-scored))
4821          gnus-summary-default-score 0)))
4822
4823 (defun gnus-thread-sort-by-score (h1 h2)
4824   "Sort threads by root article score."
4825   (gnus-article-sort-by-score
4826    (gnus-thread-header h1) (gnus-thread-header h2)))
4827
4828 (defun gnus-thread-sort-by-total-score (h1 h2)
4829   "Sort threads by the sum of all scores in the thread.
4830 Unscored articles will be counted as having a score of zero."
4831   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4832
4833 (defun gnus-thread-total-score (thread)
4834   ;; This function find the total score of THREAD.
4835   (cond
4836    ((null thread)
4837     0)
4838    ((consp thread)
4839     (if (stringp (car thread))
4840         (apply gnus-thread-score-function 0
4841                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4842       (gnus-thread-total-score-1 thread)))
4843    (t
4844     (gnus-thread-total-score-1 (list thread)))))
4845
4846 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4847   "Sort threads such that the thread with the most recently arrived article comes first."
4848   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4849
4850 (defun gnus-thread-highest-number (thread)
4851   "Return the highest article number in THREAD."
4852   (apply 'max (mapcar (lambda (header)
4853                         (mail-header-number header))
4854                       (message-flatten-list thread))))
4855
4856 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4857   "Sort threads such that the thread with the most recently dated article comes first."
4858   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4859
4860 (defun gnus-thread-latest-date (thread)
4861   "Return the highest article date in THREAD."
4862   (let ((previous-time 0))
4863     (apply 'max
4864            (mapcar
4865             (lambda (header)
4866               (setq previous-time
4867                     (condition-case ()
4868                         (time-to-seconds (mail-header-parse-date
4869                                           (mail-header-date header)))
4870                       (error previous-time))))
4871             (sort
4872              (message-flatten-list thread)
4873              (lambda (h1 h2)
4874                (< (mail-header-number h1)
4875                   (mail-header-number h2))))))))
4876
4877 (defun gnus-thread-total-score-1 (root)
4878   ;; This function find the total score of the thread below ROOT.
4879   (setq root (car root))
4880   (apply gnus-thread-score-function
4881          (or (append
4882               (mapcar 'gnus-thread-total-score
4883                       (cdr (gnus-id-to-thread (mail-header-id root))))
4884               (when (> (mail-header-number root) 0)
4885                 (list (or (cdr (assq (mail-header-number root)
4886                                      gnus-newsgroup-scored))
4887                           gnus-summary-default-score 0))))
4888              (list gnus-summary-default-score)
4889              '(0))))
4890
4891 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4892 (defvar gnus-tmp-prev-subject nil)
4893 (defvar gnus-tmp-false-parent nil)
4894 (defvar gnus-tmp-root-expunged nil)
4895 (defvar gnus-tmp-dummy-line nil)
4896
4897 (eval-when-compile (defvar gnus-tmp-header))
4898 (defun gnus-extra-header (type &optional header)
4899   "Return the extra header of TYPE."
4900   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4901       ""))
4902
4903 (defvar gnus-tmp-thread-tree-header-string "")
4904
4905 (defcustom gnus-sum-thread-tree-root "> "
4906   "With %B spec, used for the root of a thread.
4907 If nil, use subject instead."
4908   :version "22.1"
4909   :type '(radio (const :format "%v  " nil) string)
4910   :group 'gnus-thread)
4911
4912 (defcustom gnus-sum-thread-tree-false-root "> "
4913   "With %B spec, used for a false root of a thread.
4914 If nil, use subject instead."
4915   :version "22.1"
4916   :type '(radio (const :format "%v  " nil) string)
4917   :group 'gnus-thread)
4918
4919 (defcustom gnus-sum-thread-tree-single-indent ""
4920   "With %B spec, used for a thread with just one message.
4921 If nil, use subject instead."
4922   :version "22.1"
4923   :type '(radio (const :format "%v  " nil) string)
4924   :group 'gnus-thread)
4925
4926 (defcustom gnus-sum-thread-tree-vertical "| "
4927   "With %B spec, used for drawing a vertical line."
4928   :version "22.1"
4929   :type 'string
4930   :group 'gnus-thread)
4931
4932 (defcustom gnus-sum-thread-tree-indent "  "
4933   "With %B spec, used for indenting."
4934   :version "22.1"
4935   :type 'string
4936   :group 'gnus-thread)
4937
4938 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4939   "With %B spec, used for a leaf with brothers."
4940   :version "22.1"
4941   :type 'string
4942   :group 'gnus-thread)
4943
4944 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4945   "With %B spec, used for a leaf without brothers."
4946   :version "22.1"
4947   :type 'string
4948   :group 'gnus-thread)
4949
4950 (defun gnus-summary-prepare-threads (threads)
4951   "Prepare summary buffer from THREADS and indentation LEVEL.
4952 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4953 or a straight list of headers."
4954   (gnus-message 7 "Generating summary...")
4955
4956   (setq gnus-newsgroup-threads threads)
4957   (beginning-of-line)
4958
4959   (let ((gnus-tmp-level 0)
4960         (default-score (or gnus-summary-default-score 0))
4961         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4962         (building-line-count gnus-summary-display-while-building)
4963         (building-count (integerp gnus-summary-display-while-building))
4964         thread number subject stack state gnus-tmp-gathered beg-match
4965         new-roots gnus-tmp-new-adopts thread-end simp-subject
4966         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4967         gnus-tmp-replied gnus-tmp-subject-or-nil
4968         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4969         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4970         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4971         tree-stack)
4972
4973     (setq gnus-tmp-prev-subject nil
4974           gnus-tmp-thread-tree-header-string "")
4975
4976     (if (vectorp (car threads))
4977         ;; If this is a straight (sic) list of headers, then a
4978         ;; threaded summary display isn't required, so we just create
4979         ;; an unthreaded one.
4980         (gnus-summary-prepare-unthreaded threads)
4981
4982       ;; Do the threaded display.
4983
4984       (if gnus-summary-display-while-building
4985           (switch-to-buffer (buffer-name)))
4986       (while (or threads stack gnus-tmp-new-adopts new-roots)
4987
4988         (if (and (= gnus-tmp-level 0)
4989                  (or (not stack)
4990                      (= (caar stack) 0))
4991                  (not gnus-tmp-false-parent)
4992                  (or gnus-tmp-new-adopts new-roots))
4993             (if gnus-tmp-new-adopts
4994                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4995                       thread (list (car gnus-tmp-new-adopts))
4996                       gnus-tmp-header (caar thread)
4997                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4998               (when new-roots
4999                 (setq thread (list (car new-roots))
5000                       gnus-tmp-header (caar thread)
5001                       new-roots (cdr new-roots))))
5002
5003           (if threads
5004               ;; If there are some threads, we do them before the
5005               ;; threads on the stack.
5006               (setq thread threads
5007                     gnus-tmp-header (caar thread))
5008             ;; There were no current threads, so we pop something off
5009             ;; the stack.
5010             (setq state (car stack)
5011                   gnus-tmp-level (car state)
5012                   tree-stack (cadr state)
5013                   thread (caddr state)
5014                   stack (cdr stack)
5015                   gnus-tmp-header (caar thread))))
5016
5017         (setq gnus-tmp-false-parent nil)
5018         (setq gnus-tmp-root-expunged nil)
5019         (setq thread-end nil)
5020
5021         (if (stringp gnus-tmp-header)
5022             ;; The header is a dummy root.
5023             (cond
5024              ((eq gnus-summary-make-false-root 'adopt)
5025               ;; We let the first article adopt the rest.
5026               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5027                                                (cddar thread)))
5028               (setq gnus-tmp-gathered
5029                     (nconc (mapcar
5030                             (lambda (h) (mail-header-number (car h)))
5031                             (cddar thread))
5032                            gnus-tmp-gathered))
5033               (setq thread (cons (list (caar thread)
5034                                        (cadar thread))
5035                                  (cdr thread)))
5036               (setq gnus-tmp-level -1
5037                     gnus-tmp-false-parent t))
5038              ((eq gnus-summary-make-false-root 'empty)
5039               ;; We print adopted articles with empty subject fields.
5040               (setq gnus-tmp-gathered
5041                     (nconc (mapcar
5042                             (lambda (h) (mail-header-number (car h)))
5043                             (cddar thread))
5044                            gnus-tmp-gathered))
5045               (setq gnus-tmp-level -1))
5046              ((eq gnus-summary-make-false-root 'dummy)
5047               ;; We remember that we probably want to output a dummy
5048               ;; root.
5049               (setq gnus-tmp-dummy-line gnus-tmp-header)
5050               (setq gnus-tmp-prev-subject gnus-tmp-header))
5051              (t
5052               ;; We do not make a root for the gathered
5053               ;; sub-threads at all.
5054               (setq gnus-tmp-level -1)))
5055
5056           (setq number (mail-header-number gnus-tmp-header)
5057                 subject (mail-header-subject gnus-tmp-header)
5058                 simp-subject (gnus-simplify-subject-fully subject))
5059
5060           (cond
5061            ;; If the thread has changed subject, we might want to make
5062            ;; this subthread into a root.
5063            ((and (null gnus-thread-ignore-subject)
5064                  (not (zerop gnus-tmp-level))
5065                  gnus-tmp-prev-subject
5066                  (not (string= gnus-tmp-prev-subject simp-subject)))
5067             (setq new-roots (nconc new-roots (list (car thread)))
5068                   thread-end t
5069                   gnus-tmp-header nil))
5070            ;; If the article lies outside the current limit,
5071            ;; then we do not display it.
5072            ((not (memq number gnus-newsgroup-limit))
5073             (setq gnus-tmp-gathered
5074                   (nconc (mapcar
5075                           (lambda (h) (mail-header-number (car h)))
5076                           (cdar thread))
5077                          gnus-tmp-gathered))
5078             (setq gnus-tmp-new-adopts (if (cdar thread)
5079                                           (append gnus-tmp-new-adopts
5080                                                   (cdar thread))
5081                                         gnus-tmp-new-adopts)
5082                   thread-end t
5083                   gnus-tmp-header nil)
5084             (when (zerop gnus-tmp-level)
5085               (setq gnus-tmp-root-expunged t)))
5086            ;; Perhaps this article is to be marked as read?
5087            ((and gnus-summary-mark-below
5088                  (< (or (cdr (assq number gnus-newsgroup-scored))
5089                         default-score)
5090                     gnus-summary-mark-below)
5091                  ;; Don't touch sparse articles.
5092                  (not (gnus-summary-article-sparse-p number))
5093                  (not (gnus-summary-article-ancient-p number)))
5094             (setq gnus-newsgroup-unreads
5095                   (delq number gnus-newsgroup-unreads))
5096             (if gnus-newsgroup-auto-expire
5097                 (setq gnus-newsgroup-expirable
5098                       (gnus-add-to-sorted-list
5099                        gnus-newsgroup-expirable number))
5100               (push (cons number gnus-low-score-mark)
5101                     gnus-newsgroup-reads))))
5102
5103           (when gnus-tmp-header
5104             ;; We may have an old dummy line to output before this
5105             ;; article.
5106             (when (and gnus-tmp-dummy-line
5107                        (gnus-subject-equal
5108                         gnus-tmp-dummy-line
5109                         (mail-header-subject gnus-tmp-header)))
5110               (gnus-summary-insert-dummy-line
5111                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5112               (setq gnus-tmp-dummy-line nil))
5113
5114             ;; Compute the mark.
5115             (setq gnus-tmp-unread (gnus-article-mark number))
5116
5117             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5118                                   gnus-tmp-header gnus-tmp-level)
5119                   gnus-newsgroup-data)
5120
5121             ;; Actually insert the line.
5122             (setq
5123              gnus-tmp-subject-or-nil
5124              (cond
5125               ((and gnus-thread-ignore-subject
5126                     gnus-tmp-prev-subject
5127                     (not (string= gnus-tmp-prev-subject simp-subject)))
5128                subject)
5129               ((zerop gnus-tmp-level)
5130                (if (and (eq gnus-summary-make-false-root 'empty)
5131                         (memq number gnus-tmp-gathered)
5132                         gnus-tmp-prev-subject
5133                         (string= gnus-tmp-prev-subject simp-subject))
5134                    gnus-summary-same-subject
5135                  subject))
5136               (t gnus-summary-same-subject)))
5137             (if (and (eq gnus-summary-make-false-root 'adopt)
5138                      (= gnus-tmp-level 1)
5139                      (memq number gnus-tmp-gathered))
5140                 (setq gnus-tmp-opening-bracket ?\<
5141                       gnus-tmp-closing-bracket ?\>)
5142               (setq gnus-tmp-opening-bracket ?\[
5143                     gnus-tmp-closing-bracket ?\]))
5144             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5145                 (gnus-make-thread-indent-array
5146                  (max (* 2 (length gnus-thread-indent-array))
5147                       gnus-tmp-level)))
5148             (setq
5149              gnus-tmp-indentation
5150              (aref gnus-thread-indent-array gnus-tmp-level)
5151              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5152              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5153                                 gnus-summary-default-score 0)
5154              gnus-tmp-score-char
5155              (if (or (null gnus-summary-default-score)
5156                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5157                          gnus-summary-zcore-fuzz))
5158                  ?                      ;Whitespace
5159                (if (< gnus-tmp-score gnus-summary-default-score)
5160                    gnus-score-below-mark gnus-score-over-mark))
5161              gnus-tmp-replied
5162              (cond ((memq number gnus-newsgroup-processable)
5163                     gnus-process-mark)
5164                    ((memq number gnus-newsgroup-cached)
5165                     gnus-cached-mark)
5166                    ((memq number gnus-newsgroup-replied)
5167                     gnus-replied-mark)
5168                    ((memq number gnus-newsgroup-forwarded)
5169                     gnus-forwarded-mark)
5170                    ((memq number gnus-newsgroup-saved)
5171                     gnus-saved-mark)
5172                    ((memq number gnus-newsgroup-recent)
5173                     gnus-recent-mark)
5174                    ((memq number gnus-newsgroup-unseen)
5175                     gnus-unseen-mark)
5176                    (t gnus-no-mark))
5177              gnus-tmp-downloaded
5178              (cond ((memq number gnus-newsgroup-undownloaded)
5179                     gnus-undownloaded-mark)
5180                    (gnus-newsgroup-agentized
5181                     gnus-downloaded-mark)
5182                    (t
5183                     gnus-no-mark))
5184              gnus-tmp-from (mail-header-from gnus-tmp-header)
5185              gnus-tmp-name
5186              (cond
5187               ((string-match "<[^>]+> *$" gnus-tmp-from)
5188                (setq beg-match (match-beginning 0))
5189                (or (and (string-match "^\".+\"" gnus-tmp-from)
5190                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5191                    (substring gnus-tmp-from 0 beg-match)))
5192               ((string-match "(.+)" gnus-tmp-from)
5193                (substring gnus-tmp-from
5194                           (1+ (match-beginning 0)) (1- (match-end 0))))
5195               (t gnus-tmp-from))
5196
5197              ;; Do the %B string
5198              gnus-tmp-thread-tree-header-string
5199              (cond
5200               ((not gnus-show-threads) "")
5201               ((zerop gnus-tmp-level)
5202                (cond ((cdar thread)
5203                       (or gnus-sum-thread-tree-root subject))
5204                      (gnus-tmp-new-adopts
5205                       (or gnus-sum-thread-tree-false-root subject))
5206                      (t
5207                       (or gnus-sum-thread-tree-single-indent subject))))
5208               (t
5209                (concat (apply 'concat
5210                               (mapcar (lambda (item)
5211                                         (if (= item 1)
5212                                             gnus-sum-thread-tree-vertical
5213                                           gnus-sum-thread-tree-indent))
5214                                       (cdr (reverse tree-stack))))
5215                        (if (nth 1 thread)
5216                            gnus-sum-thread-tree-leaf-with-other
5217                          gnus-sum-thread-tree-single-leaf)))))
5218             (when (string= gnus-tmp-name "")
5219               (setq gnus-tmp-name gnus-tmp-from))
5220             (unless (numberp gnus-tmp-lines)
5221               (setq gnus-tmp-lines -1))
5222             (if (= gnus-tmp-lines -1)
5223                 (setq gnus-tmp-lines "?")
5224               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5225               (gnus-put-text-property
5226              (point)
5227              (progn (eval gnus-summary-line-format-spec) (point))
5228                'gnus-number number)
5229             (when gnus-visual-p
5230               (forward-line -1)
5231               (gnus-run-hooks 'gnus-summary-update-hook)
5232               (forward-line 1))
5233
5234             (setq gnus-tmp-prev-subject simp-subject)))
5235
5236         (when (nth 1 thread)
5237           (push (list (max 0 gnus-tmp-level)
5238                       (copy-sequence tree-stack)
5239                       (nthcdr 1 thread))
5240                 stack))
5241         (push (if (nth 1 thread) 1 0) tree-stack)
5242         (incf gnus-tmp-level)
5243         (setq threads (if thread-end nil (cdar thread)))
5244         (if gnus-summary-display-while-building
5245             (if building-count
5246                 (progn
5247                   ;; use a set frequency
5248                   (setq building-line-count (1- building-line-count))
5249                   (when (= building-line-count 0)
5250                     (sit-for 0)
5251                     (setq building-line-count
5252                           gnus-summary-display-while-building)))
5253               ;; always
5254               (sit-for 0)))
5255         (unless threads
5256           (setq gnus-tmp-level 0)))))
5257   (gnus-message 7 "Generating summary...done"))
5258
5259 (defun gnus-summary-prepare-unthreaded (headers)
5260   "Generate an unthreaded summary buffer based on HEADERS."
5261   (let (header number mark)
5262
5263     (beginning-of-line)
5264
5265     (while headers
5266       ;; We may have to root out some bad articles...
5267       (when (memq (setq number (mail-header-number
5268                                 (setq header (pop headers))))
5269                   gnus-newsgroup-limit)
5270         ;; Mark article as read when it has a low score.
5271         (when (and gnus-summary-mark-below
5272                    (< (or (cdr (assq number gnus-newsgroup-scored))
5273                           gnus-summary-default-score 0)
5274                       gnus-summary-mark-below)
5275                    (not (gnus-summary-article-ancient-p number)))
5276           (setq gnus-newsgroup-unreads
5277                 (delq number gnus-newsgroup-unreads))
5278           (if gnus-newsgroup-auto-expire
5279               (push number gnus-newsgroup-expirable)
5280             (push (cons number gnus-low-score-mark)
5281                   gnus-newsgroup-reads)))
5282
5283         (setq mark (gnus-article-mark number))
5284         (push (gnus-data-make number mark (1+ (point)) header 0)
5285               gnus-newsgroup-data)
5286         (gnus-summary-insert-line
5287          header 0 number
5288          (memq number gnus-newsgroup-undownloaded)
5289          mark (memq number gnus-newsgroup-replied)
5290          (memq number gnus-newsgroup-expirable)
5291          (mail-header-subject header) nil
5292          (cdr (assq number gnus-newsgroup-scored))
5293          (memq number gnus-newsgroup-processable))))))
5294
5295 (defun gnus-summary-remove-list-identifiers ()
5296   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5297   (let ((regexp (if (consp gnus-list-identifiers)
5298                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5299                   gnus-list-identifiers))
5300         changed subject)
5301     (when regexp
5302       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5303       (dolist (header gnus-newsgroup-headers)
5304         (setq subject (mail-header-subject header)
5305               changed nil)
5306         (while (string-match regexp subject)
5307           (setq subject
5308                 (concat (substring subject 0 (match-beginning 1))
5309                         (substring subject (match-end 0)))
5310                 changed t))
5311         (when changed
5312           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5313             (setq subject
5314                   (concat (substring subject 0 (match-beginning 1))
5315                           (substring subject (match-end 1)))))
5316           (mail-header-set-subject header subject))))))
5317
5318 (defun gnus-fetch-headers (articles)
5319   "Fetch headers of ARTICLES."
5320   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5321     (gnus-message 5 "Fetching headers for %s..." name)
5322     (prog1
5323         (if (eq 'nov
5324                 (setq gnus-headers-retrieved-by
5325                       (gnus-retrieve-headers
5326                        articles gnus-newsgroup-name
5327                        ;; We might want to fetch old headers, but
5328                        ;; not if there is only 1 article.
5329                        (and (or (and
5330                                  (not (eq gnus-fetch-old-headers 'some))
5331                                  (not (numberp gnus-fetch-old-headers)))
5332                                 (> (length articles) 1))
5333                             gnus-fetch-old-headers))))
5334             (gnus-get-newsgroup-headers-xover
5335              articles nil nil gnus-newsgroup-name t)
5336           (gnus-get-newsgroup-headers))
5337       (gnus-message 5 "Fetching headers for %s...done" name))))
5338
5339 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5340   "Select newsgroup GROUP.
5341 If READ-ALL is non-nil, all articles in the group are selected.
5342 If SELECT-ARTICLES, only select those articles from GROUP."
5343   (let* ((entry (gnus-group-entry group))
5344          ;;!!! Dirty hack; should be removed.
5345          (gnus-summary-ignore-duplicates
5346           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5347               t
5348             gnus-summary-ignore-duplicates))
5349          (info (nth 2 entry))
5350          articles fetched-articles cached)
5351
5352     (unless (gnus-check-server
5353              (set (make-local-variable 'gnus-current-select-method)
5354                   (gnus-find-method-for-group group)))
5355       (error "Couldn't open server"))
5356
5357     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5358         (gnus-activate-group group)     ; Or we can activate it...
5359         (progn                          ; Or we bug out.
5360           (when (equal major-mode 'gnus-summary-mode)
5361             (gnus-kill-buffer (current-buffer)))
5362           (error "Couldn't activate group %s: %s"
5363                  (gnus-group-decoded-name group) (gnus-status-message group))))
5364
5365     (unless (gnus-request-group group t)
5366       (when (equal major-mode 'gnus-summary-mode)
5367         (gnus-kill-buffer (current-buffer)))
5368       (error "Couldn't request group %s: %s"
5369              (gnus-group-decoded-name group) (gnus-status-message group)))
5370
5371     (when gnus-agent
5372       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5373
5374       (setq gnus-summary-use-undownloaded-faces
5375             (gnus-agent-find-parameter
5376              group
5377              'agent-enable-undownloaded-faces)))
5378
5379     (setq gnus-newsgroup-name group
5380           gnus-newsgroup-unselected nil
5381           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5382
5383     (let ((display (gnus-group-find-parameter group 'display)))
5384       (setq gnus-newsgroup-display
5385             (cond
5386              ((not (zerop (or (car-safe read-all) 0)))
5387               ;; The user entered the group with C-u SPC/RET, let's show
5388               ;; all articles.
5389               'gnus-not-ignore)
5390              ((eq display 'all)
5391               'gnus-not-ignore)
5392              ((arrayp display)
5393               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5394              ((numberp display)
5395               ;; The following is probably the "correct" solution, but
5396               ;; it makes Gnus fetch all headers and then limit the
5397               ;; articles (which is slow), so instead we hack the
5398               ;; select-articles parameter instead. -- Simon Josefsson
5399               ;; <jas@kth.se>
5400               ;;
5401               ;; (gnus-byte-compile
5402               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5403               ;;                         display)))))
5404               (setq select-articles
5405                     (gnus-uncompress-range
5406                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5407                              (if (> tmp 0)
5408                                  tmp
5409                                1))
5410                            (cdr (gnus-active group)))))
5411               nil)
5412              (t
5413               nil))))
5414
5415     (gnus-summary-setup-default-charset)
5416
5417     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5418     (when (gnus-virtual-group-p group)
5419       (setq cached gnus-newsgroup-cached))
5420
5421     (setq gnus-newsgroup-unreads
5422           (gnus-sorted-ndifference
5423            (gnus-sorted-ndifference gnus-newsgroup-unreads
5424                                     gnus-newsgroup-marked)
5425            gnus-newsgroup-dormant))
5426
5427     (setq gnus-newsgroup-processable nil)
5428
5429     (gnus-update-read-articles group gnus-newsgroup-unreads)
5430
5431     ;; Adjust and set lists of article marks.
5432     (when info
5433       (gnus-adjust-marked-articles info))
5434     (if (setq articles select-articles)
5435         (setq gnus-newsgroup-unselected
5436               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5437       (setq articles (gnus-articles-to-read group read-all)))
5438
5439     (cond
5440      ((null articles)
5441       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5442       'quit)
5443      ((eq articles 0) nil)
5444      (t
5445       ;; Init the dependencies hash table.
5446       (setq gnus-newsgroup-dependencies
5447             (gnus-make-hashtable (length articles)))
5448       (gnus-set-global-variables)
5449       ;; Retrieve the headers and read them in.
5450
5451       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5452
5453       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5454       (when cached
5455         (setq gnus-newsgroup-cached cached))
5456
5457       ;; Suppress duplicates?
5458       (when gnus-suppress-duplicates
5459         (gnus-dup-suppress-articles))
5460
5461       ;; Set the initial limit.
5462       (setq gnus-newsgroup-limit (copy-sequence articles))
5463       ;; Remove canceled articles from the list of unread articles.
5464       (setq fetched-articles
5465             (mapcar (lambda (headers) (mail-header-number headers))
5466                     gnus-newsgroup-headers))
5467       (setq gnus-newsgroup-articles fetched-articles)
5468       (setq gnus-newsgroup-unreads
5469             (gnus-sorted-nintersection
5470              gnus-newsgroup-unreads fetched-articles))
5471       (gnus-compute-unseen-list)
5472
5473       ;; Removed marked articles that do not exist.
5474       (gnus-update-missing-marks
5475        (gnus-sorted-difference articles fetched-articles))
5476       ;; We might want to build some more threads first.
5477       (when (and gnus-fetch-old-headers
5478                  (eq gnus-headers-retrieved-by 'nov))
5479         (if (eq gnus-fetch-old-headers 'invisible)
5480             (gnus-build-all-threads)
5481           (gnus-build-old-threads)))
5482       ;; Let the Gnus agent mark articles as read.
5483       (when gnus-agent
5484         (gnus-agent-get-undownloaded-list))
5485       ;; Remove list identifiers from subject
5486       (when gnus-list-identifiers
5487         (gnus-summary-remove-list-identifiers))
5488       ;; Check whether auto-expire is to be done in this group.
5489       (setq gnus-newsgroup-auto-expire
5490             (gnus-group-auto-expirable-p group))
5491       ;; Set up the article buffer now, if necessary.
5492       (unless (and gnus-single-article-buffer
5493                    (equal gnus-article-buffer "*Article*"))
5494         (gnus-article-setup-buffer))
5495       ;; First and last article in this newsgroup.
5496       (when gnus-newsgroup-headers
5497         (setq gnus-newsgroup-begin
5498               (mail-header-number (car gnus-newsgroup-headers))
5499               gnus-newsgroup-end
5500               (mail-header-number
5501                (gnus-last-element gnus-newsgroup-headers))))
5502       ;; GROUP is successfully selected.
5503       (or gnus-newsgroup-headers t)))))
5504
5505 (defun gnus-compute-unseen-list ()
5506   ;; The `seen' marks are treated specially.
5507   (if (not gnus-newsgroup-seen)
5508       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5509     (setq gnus-newsgroup-unseen
5510           (gnus-inverse-list-range-intersection
5511            gnus-newsgroup-articles gnus-newsgroup-seen))))
5512
5513 (defun gnus-summary-display-make-predicate (display)
5514   (require 'gnus-agent)
5515   (when (= (length display) 1)
5516     (setq display (car display)))
5517   (unless gnus-summary-display-cache
5518     (dolist (elem (append '((unread . unread)
5519                             (read . read)
5520                             (unseen . unseen))
5521                           gnus-article-mark-lists))
5522       (push (cons (cdr elem)
5523                   (gnus-byte-compile
5524                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5525             gnus-summary-display-cache)))
5526   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5527         (gnus-category-predicate-cache gnus-summary-display-cache))
5528     (gnus-get-predicate display)))
5529
5530 ;; Uses the dynamically bound `number' variable.
5531 (eval-when-compile
5532   (defvar number))
5533 (defun gnus-article-marked-p (type &optional article)
5534   (let ((article (or article number)))
5535     (cond
5536      ((eq type 'tick)
5537       (memq article gnus-newsgroup-marked))
5538      ((eq type 'spam)
5539       (memq article gnus-newsgroup-spam-marked))
5540      ((eq type 'unsend)
5541       (memq article gnus-newsgroup-unsendable))
5542      ((eq type 'undownload)
5543       (memq article gnus-newsgroup-undownloaded))
5544      ((eq type 'download)
5545       (memq article gnus-newsgroup-downloadable))
5546      ((eq type 'unread)
5547       (memq article gnus-newsgroup-unreads))
5548      ((eq type 'read)
5549       (memq article gnus-newsgroup-reads))
5550      ((eq type 'dormant)
5551       (memq article gnus-newsgroup-dormant) )
5552      ((eq type 'expire)
5553       (memq article gnus-newsgroup-expirable))
5554      ((eq type 'reply)
5555       (memq article gnus-newsgroup-replied))
5556      ((eq type 'killed)
5557       (memq article gnus-newsgroup-killed))
5558      ((eq type 'bookmark)
5559       (assq article gnus-newsgroup-bookmarks))
5560      ((eq type 'score)
5561       (assq article gnus-newsgroup-scored))
5562      ((eq type 'save)
5563       (memq article gnus-newsgroup-saved))
5564      ((eq type 'cache)
5565       (memq article gnus-newsgroup-cached))
5566      ((eq type 'forward)
5567       (memq article gnus-newsgroup-forwarded))
5568      ((eq type 'seen)
5569       (not (memq article gnus-newsgroup-unseen)))
5570      ((eq type 'recent)
5571       (memq article gnus-newsgroup-recent))
5572      (t t))))
5573
5574 (defun gnus-articles-to-read (group &optional read-all)
5575   "Find out what articles the user wants to read."
5576   (let* ((articles
5577           ;; Select all articles if `read-all' is non-nil, or if there
5578           ;; are no unread articles.
5579           (if (or read-all
5580                   (and (zerop (length gnus-newsgroup-marked))
5581                        (zerop (length gnus-newsgroup-unreads)))
5582                   ;; Fetch all if the predicate is non-nil.
5583                   gnus-newsgroup-display)
5584               ;; We want to select the headers for all the articles in
5585               ;; the group, so we select either all the active
5586               ;; articles in the group, or (if that's nil), the
5587               ;; articles in the cache.
5588               (or
5589                (gnus-uncompress-range (gnus-active group))
5590                (gnus-cache-articles-in-group group))
5591             ;; Select only the "normal" subset of articles.
5592             (gnus-sorted-nunion
5593              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5594              gnus-newsgroup-unreads)))
5595          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5596          (scored (length scored-list))
5597          (number (length articles))
5598          (marked (+ (length gnus-newsgroup-marked)
5599                     (length gnus-newsgroup-dormant)))
5600          (select
5601           (cond
5602            ((numberp read-all)
5603             read-all)
5604            ((numberp gnus-newsgroup-display)
5605             gnus-newsgroup-display)
5606            (t
5607             (condition-case ()
5608                 (cond
5609                  ((and (or (<= scored marked) (= scored number))
5610                        (numberp gnus-large-newsgroup)
5611                        (> number gnus-large-newsgroup))
5612                   (let* ((cursor-in-echo-area nil)
5613                          (initial (gnus-parameter-large-newsgroup-initial
5614                                    gnus-newsgroup-name))
5615                          (input
5616                           (read-string
5617                            (format
5618                             "How many articles from %s (%s %d): "
5619                             (gnus-limit-string
5620                              (gnus-group-decoded-name gnus-newsgroup-name)
5621                              35)
5622                             (if initial "max" "default")
5623                             number)
5624                            (if initial
5625                                (cons (number-to-string initial)
5626                                      0)))))
5627                     (if (string-match "^[ \t]*$" input) number input)))
5628                  ((and (> scored marked) (< scored number)
5629                        (> (- scored number) 20))
5630                   (let ((input
5631                          (read-string
5632                           (format "%s %s (%d scored, %d total): "
5633                                   "How many articles from"
5634                                   (gnus-group-decoded-name group)
5635                                   scored number))))
5636                     (if (string-match "^[ \t]*$" input)
5637                         number input)))
5638                  (t number))
5639               (quit
5640                (message "Quit getting the articles to read")
5641                nil))))))
5642     (setq select (if (stringp select) (string-to-number select) select))
5643     (if (or (null select) (zerop select))
5644         select
5645       (if (and (not (zerop scored)) (<= (abs select) scored))
5646           (progn
5647             (setq articles (sort scored-list '<))
5648             (setq number (length articles)))
5649         (setq articles (copy-sequence articles)))
5650
5651       (when (< (abs select) number)
5652         (if (< select 0)
5653             ;; Select the N oldest articles.
5654             (setcdr (nthcdr (1- (abs select)) articles) nil)
5655           ;; Select the N most recent articles.
5656           (setq articles (nthcdr (- number select) articles))))
5657       (setq gnus-newsgroup-unselected
5658             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5659       (when gnus-alter-articles-to-read-function
5660         (setq articles
5661               (sort
5662                (funcall gnus-alter-articles-to-read-function
5663                         gnus-newsgroup-name articles)
5664                '<)))
5665       articles)))
5666
5667 (defun gnus-killed-articles (killed articles)
5668   (let (out)
5669     (while articles
5670       (when (inline (gnus-member-of-range (car articles) killed))
5671         (push (car articles) out))
5672       (setq articles (cdr articles)))
5673     out))
5674
5675 (defun gnus-uncompress-marks (marks)
5676   "Uncompress the mark ranges in MARKS."
5677   (let ((uncompressed '(score bookmark))
5678         out)
5679     (while marks
5680       (if (memq (caar marks) uncompressed)
5681           (push (car marks) out)
5682         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5683       (setq marks (cdr marks)))
5684     out))
5685
5686 (defun gnus-article-mark-to-type (mark)
5687   "Return the type of MARK."
5688   (or (cadr (assq mark gnus-article-special-mark-lists))
5689       'list))
5690
5691 (defun gnus-article-unpropagatable-p (mark)
5692   "Return whether MARK should be propagated to back end."
5693   (memq mark gnus-article-unpropagated-mark-lists))
5694
5695 (defun gnus-adjust-marked-articles (info)
5696   "Set all article lists and remove all marks that are no longer valid."
5697   (let* ((marked-lists (gnus-info-marks info))
5698          (active (gnus-active (gnus-info-group info)))
5699          (min (car active))
5700          (max (cdr active))
5701          (types gnus-article-mark-lists)
5702          marks var articles article mark mark-type
5703          bgn end)
5704
5705     (dolist (marks marked-lists)
5706       (setq mark (car marks)
5707             mark-type (gnus-article-mark-to-type mark)
5708             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5709
5710       ;; We set the variable according to the type of the marks list,
5711       ;; and then adjust the marks to a subset of the active articles.
5712       (cond
5713        ;; Adjust "simple" lists - compressed yet unsorted
5714        ((eq mark-type 'list)
5715         ;; Simultaneously uncompress and clip to active range
5716         ;; See gnus-uncompress-range for a description of possible marks
5717         (let (l lh)
5718           (if (not (cadr marks))
5719               (set var nil)
5720             (setq articles (if (numberp (cddr marks))
5721                                (list (cdr marks))
5722                              (cdr marks))
5723                   lh (cons nil nil)
5724                   l lh)
5725
5726             (while (setq article (pop articles))
5727               (cond ((consp article)
5728                      (setq bgn (max (car article) min)
5729                            end (min (cdr article) max))
5730                      (while (<= bgn end)
5731                        (setq l (setcdr l (cons bgn nil))
5732                              bgn (1+ bgn))))
5733                     ((and (<= min article)
5734                           (>= max article))
5735                      (setq l (setcdr l (cons article nil))))))
5736             (set var (cdr lh)))))
5737        ;; Adjust assocs.
5738        ((eq mark-type 'tuple)
5739         (set var (setq articles (cdr marks)))
5740         (when (not (listp (cdr (symbol-value var))))
5741           (set var (list (symbol-value var))))
5742         (when (not (listp (cdr articles)))
5743           (setq articles (list articles)))
5744         (while articles
5745           (when (or (not (consp (setq article (pop articles))))
5746                     (< (car article) min)
5747                     (> (car article) max))
5748             (set var (delq article (symbol-value var))))))
5749        ;; Adjust ranges (sloppily).
5750        ((eq mark-type 'range)
5751         (cond
5752          ((eq mark 'seen)
5753           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5754           ;; It should be (seen (NUM1 . NUM2)).
5755           (when (numberp (cddr marks))
5756             (setcdr marks (list (cdr marks))))
5757           (setq articles (cdr marks))
5758           (while (and articles
5759                       (or (and (consp (car articles))
5760                                (> min (cdar articles)))
5761                           (and (numberp (car articles))
5762                                (> min (car articles)))))
5763             (pop articles))
5764           (set var articles))))))))
5765
5766 (defun gnus-update-missing-marks (missing)
5767   "Go through the list of MISSING articles and remove them from the mark lists."
5768   (when missing
5769     (let (var m)
5770       ;; Go through all types.
5771       (dolist (elem gnus-article-mark-lists)
5772         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5773           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5774           (when (symbol-value var)
5775             ;; This list has articles.  So we delete all missing
5776             ;; articles from it.
5777             (setq m missing)
5778             (while m
5779               (set var (delq (pop m) (symbol-value var))))))))))
5780
5781 (defun gnus-update-marks ()
5782   "Enter the various lists of marked articles into the newsgroup info list."
5783   (let ((types gnus-article-mark-lists)
5784         (info (gnus-get-info gnus-newsgroup-name))
5785         type list newmarked symbol delta-marks)
5786     (when info
5787       ;; Add all marks lists to the list of marks lists.
5788       (while (setq type (pop types))
5789         (setq list (symbol-value
5790                     (setq symbol
5791                           (intern (format "gnus-newsgroup-%s" (car type))))))
5792
5793         (when list
5794           ;; Get rid of the entries of the articles that have the
5795           ;; default score.
5796           (when (and (eq (cdr type) 'score)
5797                      gnus-save-score
5798                      list)
5799             (let* ((arts list)
5800                    (prev (cons nil list))
5801                    (all prev))
5802               (while arts
5803                 (if (or (not (consp (car arts)))
5804                         (= (cdar arts) gnus-summary-default-score))
5805                     (setcdr prev (cdr arts))
5806                   (setq prev arts))
5807                 (setq arts (cdr arts)))
5808               (setq list (cdr all)))))
5809
5810         (when (eq (cdr type) 'seen)
5811           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5812
5813         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5814           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5815
5816         (when (and (gnus-check-backend-function
5817                     'request-set-mark gnus-newsgroup-name)
5818                    (not (gnus-article-unpropagatable-p (cdr type))))
5819           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5820                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5821                  (add (gnus-remove-from-range
5822                        (gnus-copy-sequence list) old)))
5823             (when add
5824               (push (list add 'add (list (cdr type))) delta-marks))
5825             (when del
5826               (push (list del 'del (list (cdr type))) delta-marks))))
5827
5828         (when list
5829           (push (cons (cdr type) list) newmarked)))
5830
5831       (when delta-marks
5832         (unless (gnus-check-group gnus-newsgroup-name)
5833           (error "Can't open server for %s" gnus-newsgroup-name))
5834         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5835
5836       ;; Enter these new marks into the info of the group.
5837       (if (nthcdr 3 info)
5838           (setcar (nthcdr 3 info) newmarked)
5839         ;; Add the marks lists to the end of the info.
5840         (when newmarked
5841           (setcdr (nthcdr 2 info) (list newmarked))))
5842
5843       ;; Cut off the end of the info if there's nothing else there.
5844       (let ((i 5))
5845         (while (and (> i 2)
5846                     (not (nth i info)))
5847           (when (nthcdr (decf i) info)
5848             (setcdr (nthcdr i info) nil)))))))
5849
5850 (defun gnus-set-mode-line (where)
5851   "Set the mode line of the article or summary buffers.
5852 If WHERE is `summary', the summary mode line format will be used."
5853   ;; Is this mode line one we keep updated?
5854   (when (and (memq where gnus-updated-mode-lines)
5855              (symbol-value
5856               (intern (format "gnus-%s-mode-line-format-spec" where))))
5857     (let (mode-string)
5858       (save-excursion
5859         ;; We evaluate this in the summary buffer since these
5860         ;; variables are buffer-local to that buffer.
5861         (set-buffer gnus-summary-buffer)
5862        ;; We bind all these variables that are used in the `eval' form
5863         ;; below.
5864         (let* ((mformat (symbol-value
5865                          (intern
5866                           (format "gnus-%s-mode-line-format-spec" where))))
5867                (gnus-tmp-group-name (gnus-mode-string-quote
5868                                      (gnus-group-decoded-name
5869                                       gnus-newsgroup-name)))
5870                (gnus-tmp-article-number (or gnus-current-article 0))
5871                (gnus-tmp-unread gnus-newsgroup-unreads)
5872                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5873                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5874                (gnus-tmp-unread-and-unselected
5875                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5876                             (zerop gnus-tmp-unselected))
5877                        "")
5878                       ((zerop gnus-tmp-unselected)
5879                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5880                       (t (format "{%d(+%d) more}"
5881                                  gnus-tmp-unread-and-unticked
5882                                  gnus-tmp-unselected))))
5883                (gnus-tmp-subject
5884                 (if (and gnus-current-headers
5885                          (vectorp gnus-current-headers))
5886                     (gnus-mode-string-quote
5887                      (mail-header-subject gnus-current-headers))
5888                   ""))
5889                bufname-length max-len
5890                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5891           (setq mode-string (eval mformat))
5892           (setq bufname-length (if (string-match "%b" mode-string)
5893                                    (- (length
5894                                        (buffer-name
5895                                         (if (eq where 'summary)
5896                                             nil
5897                                           (get-buffer gnus-article-buffer))))
5898                                       2)
5899                                  0))
5900           (setq max-len (max 4 (if gnus-mode-non-string-length
5901                                    (- (window-width)
5902                                       gnus-mode-non-string-length
5903                                       bufname-length)
5904                                  (length mode-string))))
5905           ;; We might have to chop a bit of the string off...
5906           (when (> (length mode-string) max-len)
5907             (setq mode-string
5908                   (concat (truncate-string-to-width mode-string (- max-len 3))
5909                           "...")))
5910           ;; Pad the mode string a bit.
5911           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5912       ;; Update the mode line.
5913       (setq mode-line-buffer-identification
5914             (gnus-mode-line-buffer-identification (list mode-string)))
5915       (set-buffer-modified-p t))))
5916
5917 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5918   "Go through the HEADERS list and add all Xrefs to a hash table.
5919 The resulting hash table is returned, or nil if no Xrefs were found."
5920   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5921          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5922          (xref-hashtb (gnus-make-hashtable))
5923          start group entry number xrefs header)
5924     (while headers
5925       (setq header (pop headers))
5926       (when (and (setq xrefs (mail-header-xref header))
5927                  (not (memq (setq number (mail-header-number header))
5928                             unreads)))
5929         (setq start 0)
5930         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5931           (setq start (match-end 0))
5932           (setq group (if prefix
5933                           (concat prefix (substring xrefs (match-beginning 1)
5934                                                     (match-end 1)))
5935                         (substring xrefs (match-beginning 1) (match-end 1))))
5936           (setq number
5937                 (string-to-number (substring xrefs (match-beginning 2)
5938                                           (match-end 2))))
5939           (if (setq entry (gnus-gethash group xref-hashtb))
5940               (setcdr entry (cons number (cdr entry)))
5941             (gnus-sethash group (cons number nil) xref-hashtb)))))
5942     (and start xref-hashtb)))
5943
5944 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5945   "Look through all the headers and mark the Xrefs as read."
5946   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5947         name info xref-hashtb idlist method nth4)
5948     (save-excursion
5949       (set-buffer gnus-group-buffer)
5950       (when (setq xref-hashtb
5951                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5952         (mapatoms
5953          (lambda (group)
5954            (unless (string= from-newsgroup (setq name (symbol-name group)))
5955              (setq idlist (symbol-value group))
5956              ;; Dead groups are not updated.
5957              (and (prog1
5958                       (setq info (gnus-get-info name))
5959                     (when (stringp (setq nth4 (gnus-info-method info)))
5960                       (setq nth4 (gnus-server-to-method nth4))))
5961                   ;; Only do the xrefs if the group has the same
5962                   ;; select method as the group we have just read.
5963                   (or (gnus-methods-equal-p
5964                        nth4 (gnus-find-method-for-group from-newsgroup))
5965                       virtual
5966                       (equal nth4 (setq method (gnus-find-method-for-group
5967                                                 from-newsgroup)))
5968                       (and (equal (car nth4) (car method))
5969                            (equal (nth 1 nth4) (nth 1 method))))
5970                   gnus-use-cross-reference
5971                   (or (not (eq gnus-use-cross-reference t))
5972                       virtual
5973                       ;; Only do cross-references on subscribed
5974                       ;; groups, if that is what is wanted.
5975                       (<= (gnus-info-level info) gnus-level-subscribed))
5976                   (gnus-group-make-articles-read name idlist))))
5977          xref-hashtb)))))
5978
5979 (defun gnus-compute-read-articles (group articles)
5980   (let* ((entry (gnus-group-entry group))
5981          (info (nth 2 entry))
5982          (active (gnus-active group))
5983          ninfo)
5984     (when entry
5985       ;; First peel off all invalid article numbers.
5986       (when active
5987         (let ((ids articles)
5988               id first)
5989           (while (setq id (pop ids))
5990             (when (and first (> id (cdr active)))
5991               ;; We'll end up in this situation in one particular
5992               ;; obscure situation.  If you re-scan a group and get
5993               ;; a new article that is cross-posted to a different
5994               ;; group that has not been re-scanned, you might get
5995               ;; crossposted article that has a higher number than
5996               ;; Gnus believes possible.  So we re-activate this
5997               ;; group as well.  This might mean doing the
5998               ;; crossposting thingy will *increase* the number
5999               ;; of articles in some groups.  Tsk, tsk.
6000               (setq active (or (gnus-activate-group group) active)))
6001             (when (or (> id (cdr active))
6002                       (< id (car active)))
6003               (setq articles (delq id articles))))))
6004       ;; If the read list is nil, we init it.
6005       (if (and active
6006                (null (gnus-info-read info))
6007                (> (car active) 1))
6008           (setq ninfo (cons 1 (1- (car active))))
6009         (setq ninfo (gnus-info-read info)))
6010       ;; Then we add the read articles to the range.
6011       (gnus-add-to-range
6012        ninfo (setq articles (sort articles '<))))))
6013
6014 (defun gnus-group-make-articles-read (group articles)
6015   "Update the info of GROUP to say that ARTICLES are read."
6016   (let* ((num 0)
6017          (entry (gnus-group-entry group))
6018          (info (nth 2 entry))
6019          (active (gnus-active group))
6020          range)
6021     (when entry
6022       (setq range (gnus-compute-read-articles group articles))
6023       (with-current-buffer gnus-group-buffer
6024         (gnus-undo-register
6025           `(progn
6026              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6027              (gnus-info-set-read ',info ',(gnus-info-read info))
6028              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6029              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6030              (gnus-group-update-group ,group t))))
6031       ;; Add the read articles to the range.
6032       (gnus-info-set-read info range)
6033       (gnus-request-set-mark group (list (list range 'add '(read))))
6034       ;; Then we have to re-compute how many unread
6035       ;; articles there are in this group.
6036       (when active
6037         (cond
6038          ((not range)
6039           (setq num (- (1+ (cdr active)) (car active))))
6040          ((not (listp (cdr range)))
6041           (setq num (- (cdr active) (- (1+ (cdr range))
6042                                        (car range)))))
6043          (t
6044           (while range
6045             (if (numberp (car range))
6046                 (setq num (1+ num))
6047               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6048             (setq range (cdr range)))
6049           (setq num (- (cdr active) num))))
6050         ;; Update the number of unread articles.
6051         (setcar entry num)
6052         ;; Update the group buffer.
6053         (unless (gnus-ephemeral-group-p group)
6054           (gnus-group-update-group group t))))))
6055
6056 (defvar gnus-newsgroup-none-id 0)
6057
6058 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6059   (let ((cur nntp-server-buffer)
6060         (dependencies
6061          (or dependencies
6062              (with-current-buffer gnus-summary-buffer
6063                gnus-newsgroup-dependencies)))
6064         headers id end ref number
6065         (mail-parse-charset gnus-newsgroup-charset)
6066         (mail-parse-ignored-charsets
6067          (save-excursion (condition-case nil
6068                              (set-buffer gnus-summary-buffer)
6069                            (error))
6070                          gnus-newsgroup-ignored-charsets)))
6071     (save-excursion
6072       (set-buffer nntp-server-buffer)
6073       ;; Translate all TAB characters into SPACE characters.
6074       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6075       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6076       (ietf-drums-unfold-fws)
6077       (gnus-run-hooks 'gnus-parse-headers-hook)
6078       (let ((case-fold-search t)
6079             in-reply-to header p lines chars)
6080         (goto-char (point-min))
6081         ;; Search to the beginning of the next header.  Error messages
6082         ;; do not begin with 2 or 3.
6083         (while (re-search-forward "^[23][0-9]+ " nil t)
6084           (setq id nil
6085                 ref nil)
6086           ;; This implementation of this function, with nine
6087           ;; search-forwards instead of the one re-search-forward and
6088           ;; a case (which basically was the old function) is actually
6089           ;; about twice as fast, even though it looks messier.  You
6090           ;; can't have everything, I guess.  Speed and elegance
6091           ;; doesn't always go hand in hand.
6092           (setq
6093            header
6094            (vector
6095             ;; Number.
6096             (prog1
6097                 (setq number (read cur))
6098               (end-of-line)
6099               (setq p (point))
6100               (narrow-to-region (point)
6101                                 (or (and (search-forward "\n.\n" nil t)
6102                                          (- (point) 2))
6103                                     (point))))
6104             ;; Subject.
6105             (progn
6106               (goto-char p)
6107               (if (search-forward "\nsubject:" nil t)
6108                   (funcall gnus-decode-encoded-word-function
6109                            (nnheader-header-value))
6110                 "(none)"))
6111             ;; From.
6112             (progn
6113               (goto-char p)
6114               (if (search-forward "\nfrom:" nil t)
6115                   (funcall gnus-decode-encoded-address-function
6116                            (nnheader-header-value))
6117                 "(nobody)"))
6118             ;; Date.
6119             (progn
6120               (goto-char p)
6121               (if (search-forward "\ndate:" nil t)
6122                   (nnheader-header-value) ""))
6123             ;; Message-ID.
6124             (progn
6125               (goto-char p)
6126               (setq id (if (re-search-forward
6127                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6128                            ;; We do it this way to make sure the Message-ID
6129                            ;; is (somewhat) syntactically valid.
6130                            (buffer-substring (match-beginning 1)
6131                                              (match-end 1))
6132                          ;; If there was no message-id, we just fake one
6133                          ;; to make subsequent routines simpler.
6134                          (nnheader-generate-fake-message-id number))))
6135             ;; References.
6136             (progn
6137               (goto-char p)
6138               (if (search-forward "\nreferences:" nil t)
6139                   (progn
6140                     (setq end (point))
6141                     (prog1
6142                         (nnheader-header-value)
6143                       (setq ref
6144                             (buffer-substring
6145                              (progn
6146                                (end-of-line)
6147                                (search-backward ">" end t)
6148                                (1+ (point)))
6149                              (progn
6150                                (search-backward "<" end t)
6151                                (point))))))
6152                 ;; Get the references from the in-reply-to header if there
6153                 ;; were no references and the in-reply-to header looks
6154                 ;; promising.
6155                 (if (and (search-forward "\nin-reply-to:" nil t)
6156                          (setq in-reply-to (nnheader-header-value))
6157                          (string-match "<[^>]+>" in-reply-to))
6158                     (let (ref2)
6159                       (setq ref (substring in-reply-to (match-beginning 0)
6160                                            (match-end 0)))
6161                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6162                         (setq ref2 (substring in-reply-to (match-beginning 0)
6163                                               (match-end 0)))
6164                         (when (> (length ref2) (length ref))
6165                           (setq ref ref2)))
6166                       ref)
6167                   (setq ref nil))))
6168             ;; Chars.
6169             (progn
6170               (goto-char p)
6171               (if (search-forward "\nchars: " nil t)
6172                   (if (numberp (setq chars (ignore-errors (read cur))))
6173                       chars -1)
6174                 -1))
6175             ;; Lines.
6176             (progn
6177               (goto-char p)
6178               (if (search-forward "\nlines: " nil t)
6179                   (if (numberp (setq lines (ignore-errors (read cur))))
6180                       lines -1)
6181                 -1))
6182             ;; Xref.
6183             (progn
6184               (goto-char p)
6185               (and (search-forward "\nxref:" nil t)
6186                    (nnheader-header-value)))
6187             ;; Extra.
6188             (when gnus-extra-headers
6189               (let ((extra gnus-extra-headers)
6190                     out)
6191                 (while extra
6192                   (goto-char p)
6193                   (when (search-forward
6194                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6195                     (push (cons (car extra) (nnheader-header-value))
6196                           out))
6197                   (pop extra))
6198                 out))))
6199           (when (equal id ref)
6200             (setq ref nil))
6201
6202           (when gnus-alter-header-function
6203             (funcall gnus-alter-header-function header)
6204             (setq id (mail-header-id header)
6205                   ref (gnus-parent-id (mail-header-references header))))
6206
6207           (when (setq header
6208                       (gnus-dependencies-add-header
6209                        header dependencies force-new))
6210             (push header headers))
6211           (goto-char (point-max))
6212           (widen))
6213         (nreverse headers)))))
6214
6215 ;; Goes through the xover lines and returns a list of vectors
6216 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6217                                                   force-new dependencies
6218                                                   group also-fetch-heads)
6219   "Parse the news overview data in the server buffer.
6220 Return a list of headers that match SEQUENCE (see
6221 `nntp-retrieve-headers')."
6222   ;; Get the Xref when the users reads the articles since most/some
6223   ;; NNTP servers do not include Xrefs when using XOVER.
6224   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6225   (let ((mail-parse-charset gnus-newsgroup-charset)
6226         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6227         (cur nntp-server-buffer)
6228         (dependencies (or dependencies gnus-newsgroup-dependencies))
6229         (allp (cond
6230                ((eq gnus-read-all-available-headers t)
6231                 t)
6232                ((and (stringp gnus-read-all-available-headers)
6233                      group)
6234                 (string-match gnus-read-all-available-headers group))
6235                (t
6236                 nil)))
6237         number headers header)
6238     (save-excursion
6239       (set-buffer nntp-server-buffer)
6240       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6241       ;; Allow the user to mangle the headers before parsing them.
6242       (gnus-run-hooks 'gnus-parse-headers-hook)
6243       (goto-char (point-min))
6244       (gnus-parse-without-error
6245         (while (and (or sequence allp)
6246                     (not (eobp)))
6247           (setq number (read cur))
6248           (when (not allp)
6249             (while (and sequence
6250                         (< (car sequence) number))
6251               (setq sequence (cdr sequence))))
6252           (when (and (or allp
6253                          (and sequence
6254                               (eq number (car sequence))))
6255                      (progn
6256                        (setq sequence (cdr sequence))
6257                        (setq header (inline
6258                                       (gnus-nov-parse-line
6259                                        number dependencies force-new)))))
6260             (push header headers))
6261           (forward-line 1)))
6262       ;; A common bug in inn is that if you have posted an article and
6263       ;; then retrieves the active file, it will answer correctly --
6264       ;; the new article is included.  However, a NOV entry for the
6265       ;; article may not have been generated yet, so this may fail.
6266       ;; We work around this problem by retrieving the last few
6267       ;; headers using HEAD.
6268       (if (or (not also-fetch-heads)
6269               (not sequence))
6270           ;; We (probably) got all the headers.
6271           (nreverse headers)
6272         (let ((gnus-nov-is-evil t))
6273           (nconc
6274            (nreverse headers)
6275            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6276              (gnus-get-newsgroup-headers))))))))
6277
6278 (defun gnus-article-get-xrefs ()
6279   "Fill in the Xref value in `gnus-current-headers', if necessary.
6280 This is meant to be called in `gnus-article-internal-prepare-hook'."
6281   (let ((headers (with-current-buffer gnus-summary-buffer
6282                    gnus-current-headers)))
6283     (or (not gnus-use-cross-reference)
6284         (not headers)
6285         (and (mail-header-xref headers)
6286              (not (string= (mail-header-xref headers) "")))
6287         (let ((case-fold-search t)
6288               xref)
6289           (save-restriction
6290             (nnheader-narrow-to-headers)
6291             (goto-char (point-min))
6292             (when (or (and (not (eobp))
6293                            (eq (downcase (char-after)) ?x)
6294                            (looking-at "Xref:"))
6295                       (search-forward "\nXref:" nil t))
6296               (goto-char (1+ (match-end 0)))
6297               (setq xref (buffer-substring (point) (point-at-eol)))
6298               (mail-header-set-xref headers xref)))))))
6299
6300 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6301   "Find article ID and insert the summary line for that article.
6302 OLD-HEADER can either be a header or a line number to insert
6303 the subject line on."
6304   (let* ((line (and (numberp old-header) old-header))
6305          (old-header (and (vectorp old-header) old-header))
6306          (header (cond ((and old-header use-old-header)
6307                         old-header)
6308                        ((and (numberp id)
6309                              (gnus-number-to-header id))
6310                         (gnus-number-to-header id))
6311                        (t
6312                         (gnus-read-header id))))
6313          (number (and (numberp id) id))
6314          d)
6315     (when header
6316       ;; Rebuild the thread that this article is part of and go to the
6317       ;; article we have fetched.
6318       (when (and (not gnus-show-threads)
6319                  old-header)
6320         (when (and number
6321                    (setq d (gnus-data-find (mail-header-number old-header))))
6322           (goto-char (gnus-data-pos d))
6323           (gnus-data-remove
6324            number
6325            (- (point-at-bol)
6326               (prog1
6327                   (1+ (point-at-eol))
6328                 (gnus-delete-line))))))
6329       ;; Remove list identifiers from subject.
6330       (when gnus-list-identifiers
6331         (let ((gnus-newsgroup-headers (list header)))
6332           (gnus-summary-remove-list-identifiers)))
6333       (when old-header
6334         (mail-header-set-number header (mail-header-number old-header)))
6335       (setq gnus-newsgroup-sparse
6336             (delq (setq number (mail-header-number header))
6337                   gnus-newsgroup-sparse))
6338       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6339       (push number gnus-newsgroup-limit)
6340       (gnus-rebuild-thread (mail-header-id header) line)
6341       (gnus-summary-goto-subject number nil t))
6342     (when (and (numberp number)
6343                (> number 0))
6344       ;; We have to update the boundaries even if we can't fetch the
6345       ;; article if ID is a number -- so that the next `P' or `N'
6346       ;; command will fetch the previous (or next) article even
6347       ;; if the one we tried to fetch this time has been canceled.
6348       (when (> number gnus-newsgroup-end)
6349         (setq gnus-newsgroup-end number))
6350       (when (< number gnus-newsgroup-begin)
6351         (setq gnus-newsgroup-begin number))
6352       (setq gnus-newsgroup-unselected
6353             (delq number gnus-newsgroup-unselected)))
6354     ;; Report back a success?
6355     (and header (mail-header-number header))))
6356
6357 ;;; Process/prefix in the summary buffer
6358
6359 (defun gnus-summary-work-articles (n)
6360   "Return a list of articles to be worked upon.
6361 The prefix argument, the list of process marked articles, and the
6362 current article will be taken into consideration."
6363   (save-excursion
6364     (set-buffer gnus-summary-buffer)
6365     (cond
6366      (n
6367       ;; A numerical prefix has been given.
6368       (setq n (prefix-numeric-value n))
6369       (let ((backward (< n 0))
6370             (n (abs (prefix-numeric-value n)))
6371             articles article)
6372         (save-excursion
6373           (while
6374               (and (> n 0)
6375                    (push (setq article (gnus-summary-article-number))
6376                          articles)
6377                    (if backward
6378                        (gnus-summary-find-prev nil article)
6379                      (gnus-summary-find-next nil article)))
6380             (decf n)))
6381         (nreverse articles)))
6382      ((and (gnus-region-active-p) (mark))
6383       (message "region active")
6384       ;; Work on the region between point and mark.
6385       (let ((max (max (point) (mark)))
6386             articles article)
6387         (save-excursion
6388           (goto-char (min (point) (mark)))
6389           (while
6390               (and
6391                (push (setq article (gnus-summary-article-number)) articles)
6392                (gnus-summary-find-next nil article)
6393                (< (point) max)))
6394           (nreverse articles))))
6395      (gnus-newsgroup-processable
6396       ;; There are process-marked articles present.
6397       ;; Save current state.
6398       (gnus-summary-save-process-mark)
6399       ;; Return the list.
6400       (reverse gnus-newsgroup-processable))
6401      (t
6402       ;; Just return the current article.
6403       (list (gnus-summary-article-number))))))
6404
6405 (defmacro gnus-summary-iterate (arg &rest forms)
6406   "Iterate over the process/prefixed articles and do FORMS.
6407 ARG is the interactive prefix given to the command.  FORMS will be
6408 executed with point over the summary line of the articles."
6409   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6410     `(let ((,articles (gnus-summary-work-articles ,arg)))
6411        (while ,articles
6412          (gnus-summary-goto-subject (car ,articles))
6413          ,@forms
6414          (pop ,articles)))))
6415
6416 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6417 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6418
6419 (defun gnus-summary-save-process-mark ()
6420   "Push the current set of process marked articles on the stack."
6421   (interactive)
6422   (push (copy-sequence gnus-newsgroup-processable)
6423         gnus-newsgroup-process-stack))
6424
6425 (defun gnus-summary-kill-process-mark ()
6426   "Push the current set of process marked articles on the stack and unmark."
6427   (interactive)
6428   (gnus-summary-save-process-mark)
6429   (gnus-summary-unmark-all-processable))
6430
6431 (defun gnus-summary-yank-process-mark ()
6432   "Pop the last process mark state off the stack and restore it."
6433   (interactive)
6434   (unless gnus-newsgroup-process-stack
6435     (error "Empty mark stack"))
6436   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6437
6438 (defun gnus-summary-process-mark-set (set)
6439   "Make SET into the current process marked articles."
6440   (gnus-summary-unmark-all-processable)
6441   (mapc 'gnus-summary-set-process-mark set))
6442
6443 ;;; Searching and stuff
6444
6445 (defun gnus-summary-search-group (&optional backward use-level)
6446   "Search for next unread newsgroup.
6447 If optional argument BACKWARD is non-nil, search backward instead."
6448   (save-excursion
6449     (set-buffer gnus-group-buffer)
6450     (when (gnus-group-search-forward
6451            backward nil (if use-level (gnus-group-group-level) nil))
6452       (gnus-group-group-name))))
6453
6454 (defun gnus-summary-best-group (&optional exclude-group)
6455   "Find the name of the best unread group.
6456 If EXCLUDE-GROUP, do not go to this group."
6457   (with-current-buffer gnus-group-buffer
6458     (save-excursion
6459       (gnus-group-best-unread-group exclude-group))))
6460
6461 (defun gnus-summary-find-next (&optional unread article backward)
6462   (if backward
6463       (gnus-summary-find-prev unread article)
6464     (let* ((dummy (gnus-summary-article-intangible-p))
6465            (article (or article (gnus-summary-article-number)))
6466            (data (gnus-data-find-list article))
6467            result)
6468       (when (and (not dummy)
6469                  (or (not gnus-summary-check-current)
6470                      (not unread)
6471                      (not (gnus-data-unread-p (car data)))))
6472         (setq data (cdr data)))
6473       (when (setq result
6474                   (if unread
6475                       (progn
6476                         (while data
6477                           (unless (memq (gnus-data-number (car data))
6478                                         (cond
6479                                          ((eq gnus-auto-goto-ignores
6480                                               'always-undownloaded)
6481                                           gnus-newsgroup-undownloaded)
6482                                          (gnus-plugged
6483                                           nil)
6484                                          ((eq gnus-auto-goto-ignores
6485                                               'unfetched)
6486                                           gnus-newsgroup-unfetched)
6487                                          ((eq gnus-auto-goto-ignores
6488                                               'undownloaded)
6489                                           gnus-newsgroup-undownloaded)))
6490                             (when (gnus-data-unread-p (car data))
6491                               (setq result (car data)
6492                                     data nil)))
6493                           (setq data (cdr data)))
6494                         result)
6495                     (car data)))
6496         (goto-char (gnus-data-pos result))
6497         (gnus-data-number result)))))
6498
6499 (defun gnus-summary-find-prev (&optional unread article)
6500   (let* ((eobp (eobp))
6501          (article (or article (gnus-summary-article-number)))
6502          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6503          result)
6504     (when (and (not eobp)
6505                (or (not gnus-summary-check-current)
6506                    (not unread)
6507                    (not (gnus-data-unread-p (car data)))))
6508       (setq data (cdr data)))
6509     (when (setq result
6510                 (if unread
6511                     (progn
6512                       (while data
6513                         (unless (memq (gnus-data-number (car data))
6514                                       (cond
6515                                        ((eq gnus-auto-goto-ignores
6516                                             'always-undownloaded)
6517                                         gnus-newsgroup-undownloaded)
6518                                        (gnus-plugged
6519                                         nil)
6520                                        ((eq gnus-auto-goto-ignores
6521                                             'unfetched)
6522                                         gnus-newsgroup-unfetched)
6523                                        ((eq gnus-auto-goto-ignores
6524                                             'undownloaded)
6525                                         gnus-newsgroup-undownloaded)))
6526                           (when (gnus-data-unread-p (car data))
6527                             (setq result (car data)
6528                                   data nil)))
6529                         (setq data (cdr data)))
6530                       result)
6531                   (car data)))
6532       (goto-char (gnus-data-pos result))
6533       (gnus-data-number result))))
6534
6535 (defun gnus-summary-find-subject (subject &optional unread backward article)
6536   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6537          (article (or article (gnus-summary-article-number)))
6538          (articles (gnus-data-list backward))
6539          (arts (gnus-data-find-list article articles))
6540          result)
6541     (when (or (not gnus-summary-check-current)
6542               (not unread)
6543               (not (gnus-data-unread-p (car arts))))
6544       (setq arts (cdr arts)))
6545     (while arts
6546       (and (or (not unread)
6547                (gnus-data-unread-p (car arts)))
6548            (vectorp (gnus-data-header (car arts)))
6549            (gnus-subject-equal
6550             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6551            (setq result (car arts)
6552                  arts nil))
6553       (setq arts (cdr arts)))
6554     (and result
6555          (goto-char (gnus-data-pos result))
6556          (gnus-data-number result))))
6557
6558 (defun gnus-summary-search-forward (&optional unread subject backward)
6559   "Search forward for an article.
6560 If UNREAD, look for unread articles.  If SUBJECT, look for
6561 articles with that subject.  If BACKWARD, search backward instead."
6562   (cond (subject (gnus-summary-find-subject subject unread backward))
6563         (backward (gnus-summary-find-prev unread))
6564         (t (gnus-summary-find-next unread))))
6565
6566 (defun gnus-recenter (&optional n)
6567   "Center point in window and redisplay frame.
6568 Also do horizontal recentering."
6569   (interactive "P")
6570   (when (and gnus-auto-center-summary
6571              (not (eq gnus-auto-center-summary 'vertical)))
6572     (gnus-horizontal-recenter))
6573   (recenter n))
6574
6575 (defun gnus-summary-recenter ()
6576   "Center point in the summary window.
6577 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6578 displayed, no centering will be performed."
6579   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6580   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6581   (interactive)
6582   ;; The user has to want it.
6583   (when gnus-auto-center-summary
6584     (let* ((top (cond ((< (window-height) 4) 0)
6585                       ((< (window-height) 7) 1)
6586                       (t (if (numberp gnus-auto-center-summary)
6587                              gnus-auto-center-summary
6588                            (/ (1- (window-height)) 2)))))
6589            (height (1- (window-height)))
6590            (bottom (save-excursion (goto-char (point-max))
6591                                    (forward-line (- height))
6592                                    (point)))
6593            (window (get-buffer-window (current-buffer))))
6594       (when (get-buffer-window gnus-article-buffer)
6595         ;; Only do recentering when the article buffer is displayed,
6596         ;; Set the window start to either `bottom', which is the biggest
6597         ;; possible valid number, or the second line from the top,
6598         ;; whichever is the least.
6599         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6600           (message "%s" top-pos)
6601           (if (> bottom top-pos)
6602               ;; Keep the second line from the top visible
6603               (set-window-start window top-pos)
6604             ;; Try to keep the bottom line visible; if it's partially
6605             ;; obscured, either scroll one more line to make it fully
6606             ;; visible, or revert to using TOP-POS.
6607             (save-excursion
6608               (goto-char (point-max))
6609               (forward-line -1)
6610               (let ((last-line-start (point)))
6611                 (goto-char bottom)
6612                 (set-window-start window (point) t)
6613                 (when (not (pos-visible-in-window-p last-line-start window))
6614                   (forward-line 1)
6615                   (set-window-start window (min (point) top-pos) t)))))))
6616       ;; Do horizontal recentering while we're at it.
6617       (when (and (get-buffer-window (current-buffer) t)
6618                  (not (eq gnus-auto-center-summary 'vertical)))
6619         (let ((selected (selected-window)))
6620           (select-window (get-buffer-window (current-buffer) t))
6621           (gnus-summary-position-point)
6622           (gnus-horizontal-recenter)
6623           (select-window selected))))))
6624
6625 (defun gnus-summary-jump-to-group (newsgroup)
6626   "Move point to NEWSGROUP in group mode buffer."
6627   ;; Keep update point of group mode buffer if visible.
6628   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6629       (save-window-excursion
6630         ;; Take care of tree window mode.
6631         (when (get-buffer-window gnus-group-buffer)
6632           (pop-to-buffer gnus-group-buffer))
6633         (gnus-group-jump-to-group newsgroup))
6634     (save-excursion
6635       ;; Take care of tree window mode.
6636       (if (get-buffer-window gnus-group-buffer)
6637           (pop-to-buffer gnus-group-buffer)
6638         (set-buffer gnus-group-buffer))
6639       (gnus-group-jump-to-group newsgroup))))
6640
6641 ;; This function returns a list of article numbers based on the
6642 ;; difference between the ranges of read articles in this group and
6643 ;; the range of active articles.
6644 (defun gnus-list-of-unread-articles (group)
6645   (let* ((read (gnus-info-read (gnus-get-info group)))
6646          (active (or (gnus-active group) (gnus-activate-group group)))
6647          (last (or (cdr active)
6648                    (error "Group %s couldn't be activated " group)))
6649          first nlast unread)
6650     ;; If none are read, then all are unread.
6651     (if (not read)
6652         (setq first (car active))
6653       ;; If the range of read articles is a single range, then the
6654       ;; first unread article is the article after the last read
6655       ;; article.  Sounds logical, doesn't it?
6656       (if (and (not (listp (cdr read)))
6657                (or (< (car read) (car active))
6658                    (progn (setq read (list read))
6659                           nil)))
6660           (setq first (max (car active) (1+ (cdr read))))
6661         ;; `read' is a list of ranges.
6662         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6663                                   (caar read)))
6664                   1)
6665           (setq first (car active)))
6666         (while read
6667           (when first
6668             (while (< first nlast)
6669               (setq unread (cons first unread)
6670                     first (1+ first))))
6671           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6672           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6673           (setq read (cdr read)))))
6674     ;; And add the last unread articles.
6675     (while (<= first last)
6676       (setq unread (cons first unread)
6677             first (1+ first)))
6678     ;; Return the list of unread articles.
6679     (delq 0 (nreverse unread))))
6680
6681 (defun gnus-list-of-read-articles (group)
6682   "Return a list of unread, unticked and non-dormant articles."
6683   (let* ((info (gnus-get-info group))
6684          (marked (gnus-info-marks info))
6685          (active (gnus-active group)))
6686     (and info active
6687          (gnus-list-range-difference
6688           (gnus-list-range-difference
6689            (gnus-sorted-complement
6690             (gnus-uncompress-range active)
6691             (gnus-list-of-unread-articles group))
6692            (cdr (assq 'dormant marked)))
6693           (cdr (assq 'tick marked))))))
6694
6695 ;; This function returns a sequence of article numbers based on the
6696 ;; difference between the ranges of read articles in this group and
6697 ;; the range of active articles.
6698 (defun gnus-sequence-of-unread-articles (group)
6699   (let* ((read (gnus-info-read (gnus-get-info group)))
6700          (active (or (gnus-active group) (gnus-activate-group group)))
6701          (last (cdr active))
6702          first nlast unread)
6703     ;; If none are read, then all are unread.
6704     (if (not read)
6705         (setq first (car active))
6706       ;; If the range of read articles is a single range, then the
6707       ;; first unread article is the article after the last read
6708       ;; article.  Sounds logical, doesn't it?
6709       (if (and (not (listp (cdr read)))
6710                (or (< (car read) (car active))
6711                    (progn (setq read (list read))
6712                           nil)))
6713           (setq first (max (car active) (1+ (cdr read))))
6714         ;; `read' is a list of ranges.
6715         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6716                                   (caar read)))
6717                   1)
6718           (setq first (car active)))
6719         (while read
6720           (when first
6721             (push (cons first nlast) unread))
6722           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6723           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6724           (setq read (cdr read)))))
6725     ;; And add the last unread articles.
6726     (cond ((not (and first last))
6727            nil)
6728           ((< first last)
6729            (push (cons first last) unread))
6730           ((= first last)
6731            (push first unread)))
6732     ;; Return the sequence of unread articles.
6733     (delq 0 (nreverse unread))))
6734
6735 ;; Various summary commands
6736
6737 (defun gnus-summary-select-article-buffer ()
6738   "Reconfigure windows to show article buffer."
6739   (interactive)
6740   (if (not (gnus-buffer-live-p gnus-article-buffer))
6741       (error "There is no article buffer for this summary buffer")
6742     (gnus-configure-windows 'article)
6743     (select-window (get-buffer-window gnus-article-buffer))))
6744
6745 (defun gnus-summary-universal-argument (arg)
6746   "Perform any operation on all articles that are process/prefixed."
6747   (interactive "P")
6748   (let ((articles (gnus-summary-work-articles arg))
6749         func article)
6750     (if (eq
6751          (setq
6752           func
6753           (key-binding
6754            (read-key-sequence
6755             (substitute-command-keys
6756              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6757          'undefined)
6758         (gnus-error 1 "Undefined key")
6759       (save-excursion
6760         (while articles
6761           (gnus-summary-goto-subject (setq article (pop articles)))
6762           (let (gnus-newsgroup-processable)
6763             (command-execute func))
6764           (gnus-summary-remove-process-mark article)))))
6765   (gnus-summary-position-point))
6766
6767 (defun gnus-summary-toggle-truncation (&optional arg)
6768   "Toggle truncation of summary lines.
6769 With ARG, turn line truncation on if ARG is positive."
6770   (interactive "P")
6771   (setq truncate-lines
6772         (if (null arg) (not truncate-lines)
6773           (> (prefix-numeric-value arg) 0)))
6774   (redraw-display))
6775
6776 (defun gnus-summary-find-for-reselect ()
6777   "Return the number of an article to stay on across a reselect.
6778 The current article is considered, then following articles, then previous
6779 articles.  An article is sought which is not cancelled and isn't a temporary
6780 insertion from another group.  If there's no such then return a dummy 0."
6781   (let (found)
6782     (dolist (rev '(nil t))
6783       (unless found      ; don't demand the reverse list if we don't need it
6784         (let ((data (gnus-data-find-list
6785                      (gnus-summary-article-number) (gnus-data-list rev))))
6786           (while (and data (not found))
6787             (if (and (< 0 (gnus-data-number (car data)))
6788                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6789                 (setq found (gnus-data-number (car data))))
6790             (setq data (cdr data))))))
6791     (or found 0)))
6792
6793 (defun gnus-summary-reselect-current-group (&optional all rescan)
6794   "Exit and then reselect the current newsgroup.
6795 The prefix argument ALL means to select all articles."
6796   (interactive "P")
6797   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6798     (error "Ephemeral groups can't be reselected"))
6799   (let ((current-subject (gnus-summary-find-for-reselect))
6800         (group gnus-newsgroup-name))
6801     (setq gnus-newsgroup-begin nil)
6802     (gnus-summary-exit nil 'leave-hidden)
6803     ;; We have to adjust the point of group mode buffer because
6804     ;; point was moved to the next unread newsgroup by exiting.
6805     (gnus-summary-jump-to-group group)
6806     (when rescan
6807       (save-excursion
6808         (gnus-group-get-new-news-this-group 1)))
6809     (gnus-group-read-group all t)
6810     (gnus-summary-goto-subject current-subject nil t)))
6811
6812 (defun gnus-summary-rescan-group (&optional all)
6813   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6814   (interactive "P")
6815   (gnus-summary-reselect-current-group all t))
6816
6817 (defun gnus-summary-update-info (&optional non-destructive)
6818   (save-excursion
6819     (let ((group gnus-newsgroup-name))
6820       (when group
6821         (when gnus-newsgroup-kill-headers
6822           (setq gnus-newsgroup-killed
6823                 (gnus-compress-sequence
6824                  (gnus-sorted-union
6825                   (gnus-list-range-intersection
6826                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6827                   gnus-newsgroup-unreads)
6828                  t)))
6829         (unless (listp (cdr gnus-newsgroup-killed))
6830           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6831         (let ((headers gnus-newsgroup-headers))
6832           ;; Set the new ranges of read articles.
6833           (with-current-buffer gnus-group-buffer
6834             (gnus-undo-force-boundary))
6835           (gnus-update-read-articles
6836            group (gnus-sorted-union
6837                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6838           ;; Set the current article marks.
6839           (let ((gnus-newsgroup-scored
6840                  (if (and (not gnus-save-score)
6841                           (not non-destructive))
6842                      nil
6843                    gnus-newsgroup-scored)))
6844             (save-excursion
6845               (gnus-update-marks)))
6846           ;; Do the cross-ref thing.
6847           (when gnus-use-cross-reference
6848             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6849           ;; Do not switch windows but change the buffer to work.
6850           (set-buffer gnus-group-buffer)
6851           (unless (gnus-ephemeral-group-p group)
6852             (gnus-group-update-group group)))))))
6853
6854 (defun gnus-summary-save-newsrc (&optional force)
6855   "Save the current number of read/marked articles in the dribble buffer.
6856 The dribble buffer will then be saved.
6857 If FORCE (the prefix), also save the .newsrc file(s)."
6858   (interactive "P")
6859   (gnus-summary-update-info t)
6860   (if force
6861       (gnus-save-newsrc-file)
6862     (gnus-dribble-save)))
6863
6864 (defun gnus-summary-exit (&optional temporary leave-hidden)
6865   "Exit reading current newsgroup, and then return to group selection mode.
6866 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6867   (interactive)
6868   (gnus-set-global-variables)
6869   (when (gnus-buffer-live-p gnus-article-buffer)
6870     (save-excursion
6871       (set-buffer gnus-article-buffer)
6872       (mm-destroy-parts gnus-article-mime-handles)
6873       ;; Set it to nil for safety reason.
6874       (setq gnus-article-mime-handle-alist nil)
6875       (setq gnus-article-mime-handles nil)))
6876   (gnus-kill-save-kill-buffer)
6877   (gnus-async-halt-prefetch)
6878   (let* ((group gnus-newsgroup-name)
6879          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6880          (gnus-group-is-exiting-p t)
6881          (mode major-mode)
6882          (group-point nil)
6883          (buf (current-buffer)))
6884     (unless quit-config
6885       ;; Do adaptive scoring, and possibly save score files.
6886       (when gnus-newsgroup-adaptive
6887         (gnus-score-adaptive))
6888       (when gnus-use-scoring
6889         (gnus-score-save)))
6890     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6891     ;; If we have several article buffers, we kill them at exit.
6892     (unless gnus-single-article-buffer
6893       (gnus-kill-buffer gnus-article-buffer)
6894       (gnus-kill-buffer gnus-original-article-buffer)
6895       (setq gnus-article-current nil))
6896     (when gnus-use-cache
6897       (gnus-cache-possibly-remove-articles)
6898       (gnus-cache-save-buffers))
6899     (gnus-async-prefetch-remove-group group)
6900     (when gnus-suppress-duplicates
6901       (gnus-dup-enter-articles))
6902     (when gnus-use-trees
6903       (gnus-tree-close group))
6904     (when gnus-use-cache
6905       (gnus-cache-write-active))
6906     ;; Remove entries for this group.
6907     (nnmail-purge-split-history (gnus-group-real-name group))
6908     ;; Make all changes in this group permanent.
6909     (unless quit-config
6910       (gnus-run-hooks 'gnus-exit-group-hook)
6911       (gnus-summary-update-info))
6912     (gnus-close-group group)
6913     ;; Make sure where we were, and go to next newsgroup.
6914     (set-buffer gnus-group-buffer)
6915     (unless quit-config
6916       (gnus-group-jump-to-group group))
6917     (gnus-run-hooks 'gnus-summary-exit-hook)
6918     (unless (or quit-config
6919                 ;; If this group has disappeared from the summary
6920                 ;; buffer, don't skip forwards.
6921                 (not (string= group (gnus-group-group-name))))
6922       (gnus-group-next-unread-group 1))
6923     (setq group-point (point))
6924     (if temporary
6925         nil                             ;Nothing to do.
6926       ;; If we have several article buffers, we kill them at exit.
6927       (unless gnus-single-article-buffer
6928         (gnus-kill-buffer gnus-article-buffer)
6929         (gnus-kill-buffer gnus-original-article-buffer)
6930         (setq gnus-article-current nil))
6931       (set-buffer buf)
6932       (if (not gnus-kill-summary-on-exit)
6933           (progn
6934             (gnus-deaden-summary)
6935             (setq mode nil))
6936         ;; We set all buffer-local variables to nil.  It is unclear why
6937         ;; this is needed, but if we don't, buffer-local variables are
6938         ;; not garbage-collected, it seems.  This would the lead to en
6939         ;; ever-growing Emacs.
6940         (gnus-summary-clear-local-variables)
6941         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6942           (gnus-summary-clear-local-variables))
6943         (when (get-buffer gnus-article-buffer)
6944           (bury-buffer gnus-article-buffer))
6945         ;; Return to group mode buffer.
6946         (when (eq mode 'gnus-summary-mode)
6947           (gnus-kill-buffer buf)))
6948       (setq gnus-current-select-method gnus-select-method)
6949       (set-buffer gnus-group-buffer)
6950       (if quit-config
6951           (gnus-handle-ephemeral-exit quit-config)
6952         (goto-char group-point)
6953         ;; If gnus-group-buffer is already displayed, make sure we also move
6954         ;; the cursor in the window that displays it.
6955         (let ((win (get-buffer-window (current-buffer) 0)))
6956           (if win (set-window-point win (point))))
6957         (unless leave-hidden
6958           (gnus-configure-windows 'group 'force)))
6959       ;; Clear the current group name.
6960       (unless quit-config
6961         (setq gnus-newsgroup-name nil)))))
6962
6963 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6964 (defun gnus-summary-exit-no-update (&optional no-questions)
6965   "Quit reading current newsgroup without updating read article info."
6966   (interactive)
6967   (let* ((group gnus-newsgroup-name)
6968          (gnus-group-is-exiting-p t)
6969          (gnus-group-is-exiting-without-update-p t)
6970          (quit-config (gnus-group-quit-config group)))
6971     (when (or no-questions
6972               gnus-expert-user
6973               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6974       (gnus-async-halt-prefetch)
6975       (run-hooks 'gnus-summary-prepare-exit-hook)
6976       (when (gnus-buffer-live-p gnus-article-buffer)
6977         (save-excursion
6978           (set-buffer gnus-article-buffer)
6979           (mm-destroy-parts gnus-article-mime-handles)
6980           ;; Set it to nil for safety reason.
6981           (setq gnus-article-mime-handle-alist nil)
6982           (setq gnus-article-mime-handles nil)))
6983       ;; If we have several article buffers, we kill them at exit.
6984       (unless gnus-single-article-buffer
6985         (gnus-kill-buffer gnus-article-buffer)
6986         (gnus-kill-buffer gnus-original-article-buffer)
6987         (setq gnus-article-current nil))
6988       (if (not gnus-kill-summary-on-exit)
6989           (gnus-deaden-summary)
6990         (gnus-close-group group)
6991         (gnus-summary-clear-local-variables)
6992         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6993           (gnus-summary-clear-local-variables))
6994         (gnus-kill-buffer gnus-summary-buffer))
6995       (unless gnus-single-article-buffer
6996         (setq gnus-article-current nil))
6997       (when gnus-use-trees
6998         (gnus-tree-close group))
6999       (gnus-async-prefetch-remove-group group)
7000       (when (get-buffer gnus-article-buffer)
7001         (bury-buffer gnus-article-buffer))
7002       ;; Return to the group buffer.
7003       (gnus-configure-windows 'group 'force)
7004       ;; Clear the current group name.
7005       (setq gnus-newsgroup-name nil)
7006       (unless (gnus-ephemeral-group-p group)
7007         (gnus-group-update-group group))
7008       (when (equal (gnus-group-group-name) group)
7009         (gnus-group-next-unread-group 1))
7010       (when quit-config
7011         (gnus-handle-ephemeral-exit quit-config)))))
7012
7013 (defun gnus-handle-ephemeral-exit (quit-config)
7014   "Handle movement when leaving an ephemeral group.
7015 The state which existed when entering the ephemeral is reset."
7016   (if (not (buffer-name (car quit-config)))
7017       (gnus-configure-windows 'group 'force)
7018     (set-buffer (car quit-config))
7019     (cond ((eq major-mode 'gnus-summary-mode)
7020            (gnus-set-global-variables))
7021           ((eq major-mode 'gnus-article-mode)
7022            (save-excursion
7023              ;; The `gnus-summary-buffer' variable may point
7024              ;; to the old summary buffer when using a single
7025              ;; article buffer.
7026              (unless (gnus-buffer-live-p gnus-summary-buffer)
7027                (set-buffer gnus-group-buffer))
7028              (set-buffer gnus-summary-buffer)
7029              (gnus-set-global-variables))))
7030     (if (or (eq (cdr quit-config) 'article)
7031             (eq (cdr quit-config) 'pick))
7032         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7033             (gnus-configure-windows 'pick 'force)
7034           (gnus-configure-windows (cdr quit-config) 'force))
7035       (gnus-configure-windows (cdr quit-config) 'force))
7036     (when (eq major-mode 'gnus-summary-mode)
7037       (gnus-summary-recenter)
7038       (gnus-summary-position-point))))
7039
7040 ;;; Dead summaries.
7041
7042 (defvar gnus-dead-summary-mode-map nil)
7043
7044 (unless gnus-dead-summary-mode-map
7045   (setq gnus-dead-summary-mode-map (make-keymap))
7046   (suppress-keymap gnus-dead-summary-mode-map)
7047   (substitute-key-definition
7048    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7049   (dolist (key '("\C-d" "\r" "\177" [delete]))
7050     (define-key gnus-dead-summary-mode-map
7051       key 'gnus-summary-wake-up-the-dead))
7052   (dolist (key '("q" "Q"))
7053     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7054
7055 (defvar gnus-dead-summary-mode nil
7056   "Minor mode for Gnus summary buffers.")
7057
7058 (defun gnus-dead-summary-mode (&optional arg)
7059   "Minor mode for Gnus summary buffers."
7060   (interactive "P")
7061   (when (eq major-mode 'gnus-summary-mode)
7062     (make-local-variable 'gnus-dead-summary-mode)
7063     (setq gnus-dead-summary-mode
7064           (if (null arg) (not gnus-dead-summary-mode)
7065             (> (prefix-numeric-value arg) 0)))
7066     (when gnus-dead-summary-mode
7067       (add-minor-mode
7068        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7069
7070 (defun gnus-deaden-summary ()
7071   "Make the current summary buffer into a dead summary buffer."
7072   ;; Kill any previous dead summary buffer.
7073   (when (and gnus-dead-summary
7074              (buffer-name gnus-dead-summary))
7075     (with-current-buffer gnus-dead-summary
7076       (when gnus-dead-summary-mode
7077         (kill-buffer (current-buffer)))))
7078   ;; Make this the current dead summary.
7079   (setq gnus-dead-summary (current-buffer))
7080   (gnus-dead-summary-mode 1)
7081   (let ((name (buffer-name)))
7082     (when (string-match "Summary" name)
7083       (rename-buffer
7084        (concat (substring name 0 (match-beginning 0)) "Dead "
7085                (substring name (match-beginning 0)))
7086        t)
7087       (bury-buffer))))
7088
7089 (defun gnus-kill-or-deaden-summary (buffer)
7090   "Kill or deaden the summary BUFFER."
7091   (save-excursion
7092     (when (and (buffer-name buffer)
7093                (not gnus-single-article-buffer))
7094       (with-current-buffer buffer
7095         (gnus-kill-buffer gnus-article-buffer)
7096         (gnus-kill-buffer gnus-original-article-buffer)))
7097     (cond
7098      ;; Kill the buffer.
7099      (gnus-kill-summary-on-exit
7100       (when (and gnus-use-trees
7101                  (gnus-buffer-exists-p buffer))
7102         (save-excursion
7103           (set-buffer buffer)
7104           (gnus-tree-close gnus-newsgroup-name)))
7105       (gnus-kill-buffer buffer))
7106      ;; Deaden the buffer.
7107      ((gnus-buffer-exists-p buffer)
7108       (save-excursion
7109         (set-buffer buffer)
7110         (gnus-deaden-summary))))))
7111
7112 (defun gnus-summary-wake-up-the-dead (&rest args)
7113   "Wake up the dead summary buffer."
7114   (interactive)
7115   (gnus-dead-summary-mode -1)
7116   (let ((name (buffer-name)))
7117     (when (string-match "Dead " name)
7118       (rename-buffer
7119        (concat (substring name 0 (match-beginning 0))
7120                (substring name (match-end 0)))
7121        t)))
7122   (gnus-message 3 "This dead summary is now alive again"))
7123
7124 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7125 (defun gnus-summary-fetch-faq (&optional faq-dir)
7126   "Fetch the FAQ for the current group.
7127 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7128 in."
7129   (interactive
7130    (list
7131     (when current-prefix-arg
7132       (completing-read
7133        "FAQ dir: " (and (listp gnus-group-faq-directory)
7134                         (mapcar 'list
7135                                 gnus-group-faq-directory))))))
7136   (let (gnus-faq-buffer)
7137     (when (setq gnus-faq-buffer
7138                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7139       (gnus-configure-windows 'summary-faq))))
7140
7141 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7142 (defun gnus-summary-describe-group (&optional force)
7143   "Describe the current newsgroup."
7144   (interactive "P")
7145   (gnus-group-describe-group force gnus-newsgroup-name))
7146
7147 (defun gnus-summary-describe-briefly ()
7148   "Describe summary mode commands briefly."
7149   (interactive)
7150   (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")))
7151
7152 ;; Walking around group mode buffer from summary mode.
7153
7154 (defun gnus-summary-next-group (&optional no-article target-group backward)
7155   "Exit current newsgroup and then select next unread newsgroup.
7156 If prefix argument NO-ARTICLE is non-nil, no article is selected
7157 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7158 previous group instead."
7159   (interactive "P")
7160   ;; Stop pre-fetching.
7161   (gnus-async-halt-prefetch)
7162   (let ((current-group gnus-newsgroup-name)
7163         (current-buffer (current-buffer))
7164         entered)
7165     ;; First we semi-exit this group to update Xrefs and all variables.
7166     ;; We can't do a real exit, because the window conf must remain
7167     ;; the same in case the user is prompted for info, and we don't
7168     ;; want the window conf to change before that...
7169     (gnus-summary-exit t)
7170     (while (not entered)
7171       ;; Then we find what group we are supposed to enter.
7172       (set-buffer gnus-group-buffer)
7173       (gnus-group-jump-to-group current-group)
7174       (setq target-group
7175             (or target-group
7176                 (if (eq gnus-keep-same-level 'best)
7177                     (gnus-summary-best-group gnus-newsgroup-name)
7178                   (gnus-summary-search-group backward gnus-keep-same-level))))
7179       (if (not target-group)
7180           ;; There are no further groups, so we return to the group
7181           ;; buffer.
7182           (progn
7183             (gnus-message 5 "Returning to the group buffer")
7184             (setq entered t)
7185             (when (gnus-buffer-live-p current-buffer)
7186               (set-buffer current-buffer)
7187               (gnus-summary-exit))
7188             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7189         ;; We try to enter the target group.
7190         (gnus-group-jump-to-group target-group)
7191         (let ((unreads (gnus-group-group-unread)))
7192           (if (and (or (eq t unreads)
7193                        (and unreads (not (zerop unreads))))
7194                    (gnus-summary-read-group
7195                     target-group nil no-article
7196                     (and (buffer-name current-buffer) current-buffer)
7197                     nil backward))
7198               (setq entered t)
7199             (setq current-group target-group
7200                   target-group nil)))))))
7201
7202 (defun gnus-summary-prev-group (&optional no-article)
7203   "Exit current newsgroup and then select previous unread newsgroup.
7204 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7205   (interactive "P")
7206   (gnus-summary-next-group no-article nil t))
7207
7208 ;; Walking around summary lines.
7209
7210 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7211   "Go to the first subject satisfying any non-nil constraint.
7212 If UNREAD is non-nil, the article should be unread.
7213 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7214 If UNSEEN is non-nil, the article should be unseen.
7215 Returns the article selected or nil if there are no matching articles."
7216   (interactive "P")
7217   (cond
7218    ;; Empty summary.
7219    ((null gnus-newsgroup-data)
7220     (gnus-message 3 "No articles in the group")
7221     nil)
7222    ;; Pick the first article.
7223    ((not (or unread undownloaded unseen))
7224     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7225     (gnus-data-number (car gnus-newsgroup-data)))
7226    ;; Find the first unread article.
7227    (t
7228     (let ((data gnus-newsgroup-data))
7229       (while (and data
7230                   (let ((num (gnus-data-number (car data))))
7231                     (or (memq num gnus-newsgroup-unfetched)
7232                         (not (or (and unread
7233                                       (memq num gnus-newsgroup-unreads))
7234                                  (and undownloaded
7235                                       (memq num gnus-newsgroup-undownloaded))
7236                                  (and unseen
7237                                       (memq num gnus-newsgroup-unseen)))))))
7238         (setq data (cdr data)))
7239       (prog1
7240           (if data
7241               (progn
7242                 (goto-char (gnus-data-pos (car data)))
7243                 (gnus-data-number (car data)))
7244             (gnus-message 3 "No more%s articles"
7245                           (let* ((r (when unread " unread"))
7246                                  (d (when undownloaded " undownloaded"))
7247                                  (s (when unseen " unseen"))
7248                                  (l (delq nil (list r d s))))
7249                             (cond ((= 3 (length l))
7250                                    (concat r "," d ", or" s))
7251                                   ((= 2 (length l))
7252                                    (concat (car l) ", or" (cadr l)))
7253                                   ((= 1 (length l))
7254                                    (car l))
7255                                   (t
7256                                    ""))))
7257             nil
7258             )
7259         (gnus-summary-position-point))))))
7260
7261 (defun gnus-summary-next-subject (n &optional unread dont-display)
7262   "Go to next N'th summary line.
7263 If N is negative, go to the previous N'th subject line.
7264 If UNREAD is non-nil, only unread articles are selected.
7265 The difference between N and the actual number of steps taken is
7266 returned."
7267   (interactive "p")
7268   (let ((backward (< n 0))
7269         (n (abs n)))
7270     (while (and (> n 0)
7271                 (if backward
7272                     (gnus-summary-find-prev unread)
7273                   (gnus-summary-find-next unread)))
7274       (unless (zerop (setq n (1- n)))
7275         (gnus-summary-show-thread)))
7276     (when (/= 0 n)
7277       (gnus-message 7 "No more%s articles"
7278                     (if unread " unread" "")))
7279     (unless dont-display
7280       (gnus-summary-recenter)
7281       (gnus-summary-position-point))
7282     n))
7283
7284 (defun gnus-summary-next-unread-subject (n)
7285   "Go to next N'th unread summary line."
7286   (interactive "p")
7287   (gnus-summary-next-subject n t))
7288
7289 (defun gnus-summary-prev-subject (n &optional unread)
7290   "Go to previous N'th summary line.
7291 If optional argument UNREAD is non-nil, only unread article is selected."
7292   (interactive "p")
7293   (gnus-summary-next-subject (- n) unread))
7294
7295 (defun gnus-summary-prev-unread-subject (n)
7296   "Go to previous N'th unread summary line."
7297   (interactive "p")
7298   (gnus-summary-next-subject (- n) t))
7299
7300 (defun gnus-summary-goto-subjects (articles)
7301   "Insert the subject header for ARTICLES in the current buffer."
7302   (save-excursion
7303     (dolist (article articles)
7304       (gnus-summary-goto-subject article t)))
7305   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7306   (gnus-summary-position-point))
7307
7308 (defun gnus-summary-goto-subject (article &optional force silent)
7309   "Go the subject line of ARTICLE.
7310 If FORCE, also allow jumping to articles not currently shown."
7311   (interactive "nArticle number: ")
7312   (unless (numberp article)
7313     (error "Article %s is not a number" article))
7314   (let ((b (point))
7315         (data (gnus-data-find article)))
7316     ;; We read in the article if we have to.
7317     (and (not data)
7318          force
7319          (gnus-summary-insert-subject
7320           article
7321           (if (or (numberp force) (vectorp force)) force)
7322           t)
7323          (setq data (gnus-data-find article)))
7324     (goto-char b)
7325     (if (not data)
7326         (progn
7327           (unless silent
7328             (gnus-message 3 "Can't find article %d" article))
7329           nil)
7330       (let ((pt (gnus-data-pos data)))
7331         (goto-char pt)
7332         (gnus-summary-set-article-display-arrow pt))
7333       (gnus-summary-position-point)
7334       article)))
7335
7336 ;; Walking around summary lines with displaying articles.
7337
7338 (defun gnus-summary-expand-window (&optional arg)
7339   "Make the summary buffer take up the entire Emacs frame.
7340 Given a prefix, will force an `article' buffer configuration."
7341   (interactive "P")
7342   (if arg
7343       (gnus-configure-windows 'article 'force)
7344     (gnus-configure-windows 'summary 'force)))
7345
7346 (defun gnus-summary-display-article (article &optional all-header)
7347   "Display ARTICLE in article buffer."
7348   (when (gnus-buffer-live-p gnus-article-buffer)
7349     (with-current-buffer gnus-article-buffer
7350       (mm-enable-multibyte)))
7351   (gnus-set-global-variables)
7352   (when (gnus-buffer-live-p gnus-article-buffer)
7353     (with-current-buffer gnus-article-buffer
7354       (setq gnus-article-charset gnus-newsgroup-charset)
7355       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7356       (mm-enable-multibyte)))
7357   (if (null article)
7358       nil
7359     (prog1
7360         (if gnus-summary-display-article-function
7361             (funcall gnus-summary-display-article-function article all-header)
7362           (gnus-article-prepare article all-header))
7363       (gnus-run-hooks 'gnus-select-article-hook)
7364       (when (and gnus-current-article
7365                  (not (zerop gnus-current-article)))
7366         (gnus-summary-goto-subject gnus-current-article))
7367       (gnus-summary-recenter)
7368       (when (and gnus-use-trees gnus-show-threads)
7369         (gnus-possibly-generate-tree article)
7370         (gnus-highlight-selected-tree article))
7371       ;; Successfully display article.
7372       (gnus-article-set-window-start
7373        (cdr (assq article gnus-newsgroup-bookmarks))))))
7374
7375 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7376   "Select the current article.
7377 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7378 non-nil, the article will be re-fetched even if it already present in
7379 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7380 be displayed."
7381   ;; Make sure we are in the summary buffer to work around bbdb bug.
7382   (unless (eq major-mode 'gnus-summary-mode)
7383     (set-buffer gnus-summary-buffer))
7384   (let ((article (or article (gnus-summary-article-number)))
7385         (all-headers (not (not all-headers))) ;Must be t or nil.
7386         gnus-summary-display-article-function)
7387     (and (not pseudo)
7388          (gnus-summary-article-pseudo-p article)
7389          (error "This is a pseudo-article"))
7390     (save-excursion
7391       (set-buffer gnus-summary-buffer)
7392       (if (or (and gnus-single-article-buffer
7393                    (or (null gnus-current-article)
7394                        (null gnus-article-current)
7395                        (null (get-buffer gnus-article-buffer))
7396                        (not (eq article (cdr gnus-article-current)))
7397                        (not (equal (car gnus-article-current)
7398                                    gnus-newsgroup-name))))
7399               (and (not gnus-single-article-buffer)
7400                    (or (null gnus-current-article)
7401                        (not (eq gnus-current-article article))))
7402               force)
7403           ;; The requested article is different from the current article.
7404           (progn
7405             (gnus-summary-display-article article all-headers)
7406             (when (gnus-buffer-live-p gnus-article-buffer)
7407               (with-current-buffer gnus-article-buffer
7408                 (if (not gnus-article-decoded-p) ;; a local variable
7409                     (mm-disable-multibyte))))
7410             (gnus-article-set-window-start
7411              (cdr (assq article gnus-newsgroup-bookmarks)))
7412             article)
7413         'old))))
7414
7415 (defun gnus-summary-force-verify-and-decrypt ()
7416   "Display buttons for signed/encrypted parts and verify/decrypt them."
7417   (interactive)
7418   (let ((mm-verify-option 'known)
7419         (mm-decrypt-option 'known)
7420         (gnus-article-emulate-mime t)
7421         (gnus-buttonized-mime-types (append (list "multipart/signed"
7422                                                   "multipart/encrypted")
7423                                             gnus-buttonized-mime-types)))
7424     (gnus-summary-select-article nil 'force)))
7425
7426 (defun gnus-summary-set-current-mark (&optional current-mark)
7427   "Obsolete function."
7428   nil)
7429
7430 (defun gnus-summary-next-article (&optional unread subject backward push)
7431   "Select the next article.
7432 If UNREAD, only unread articles are selected.
7433 If SUBJECT, only articles with SUBJECT are selected.
7434 If BACKWARD, the previous article is selected instead of the next."
7435   (interactive "P")
7436   (cond
7437    ;; Is there such an article?
7438    ((and (gnus-summary-search-forward unread subject backward)
7439          (or (gnus-summary-display-article (gnus-summary-article-number))
7440              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7441     (gnus-summary-position-point))
7442    ;; If not, we try the first unread, if that is wanted.
7443    ((and subject
7444          gnus-auto-select-same
7445          (gnus-summary-first-unread-article))
7446     (gnus-summary-position-point)
7447     (gnus-message 6 "Wrapped"))
7448    ;; Try to get next/previous article not displayed in this group.
7449    ((and gnus-auto-extend-newsgroup
7450          (not unread) (not subject))
7451     (gnus-summary-goto-article
7452      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7453      nil (count-lines (point-min) (point))))
7454    ;; Go to next/previous group.
7455    (t
7456     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7457       (gnus-summary-jump-to-group gnus-newsgroup-name))
7458     (let ((cmd last-command-char)
7459           (point
7460            (with-current-buffer gnus-group-buffer
7461              (point)))
7462           (group
7463            (if (eq gnus-keep-same-level 'best)
7464                (gnus-summary-best-group gnus-newsgroup-name)
7465              (gnus-summary-search-group backward gnus-keep-same-level))))
7466       ;; For some reason, the group window gets selected.  We change
7467       ;; it back.
7468       (select-window (get-buffer-window (current-buffer)))
7469       ;; Select next unread newsgroup automagically.
7470       (cond
7471        ((or (not gnus-auto-select-next)
7472             (not cmd))
7473         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7474        ((or (eq gnus-auto-select-next 'quietly)
7475             (and (eq gnus-auto-select-next 'slightly-quietly)
7476                  push)
7477             (and (eq gnus-auto-select-next 'almost-quietly)
7478                  (gnus-summary-last-article-p)))
7479         ;; Select quietly.
7480         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7481             (gnus-summary-exit)
7482           (gnus-message 7 "No more%s articles (%s)..."
7483                         (if unread " unread" "")
7484                         (if group (concat "selecting " group)
7485                           "exiting"))
7486           (gnus-summary-next-group nil group backward)))
7487        (t
7488         (when (gnus-key-press-event-p last-input-event)
7489           (gnus-summary-walk-group-buffer
7490            gnus-newsgroup-name cmd unread backward point))))))))
7491
7492 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7493   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7494                       (?\C-p (gnus-group-prev-unread-group 1))))
7495         (cursor-in-echo-area t)
7496         keve key group ended prompt)
7497     (save-excursion
7498       (set-buffer gnus-group-buffer)
7499       (goto-char start)
7500       (setq group
7501             (if (eq gnus-keep-same-level 'best)
7502                 (gnus-summary-best-group gnus-newsgroup-name)
7503               (gnus-summary-search-group backward gnus-keep-same-level))))
7504     (while (not ended)
7505       (setq prompt
7506             (format
7507              "No more%s articles%s " (if unread " unread" "")
7508              (if (and group
7509                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7510                  (format " (Type %s for %s [%s])"
7511                          (single-key-description cmd)
7512                          (gnus-group-decoded-name group)
7513                          (gnus-group-unread group))
7514                (format " (Type %s to exit %s)"
7515                        (single-key-description cmd)
7516                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7517       ;; Confirm auto selection.
7518       (setq key (car (setq keve (gnus-read-event-char prompt)))
7519             ended t)
7520       (cond
7521        ((assq key keystrokes)
7522         (let ((obuf (current-buffer)))
7523           (switch-to-buffer gnus-group-buffer)
7524           (when group
7525             (gnus-group-jump-to-group group))
7526           (eval (cadr (assq key keystrokes)))
7527           (setq group (gnus-group-group-name))
7528           (switch-to-buffer obuf))
7529         (setq ended nil))
7530        ((equal key cmd)
7531         (if (or (not group)
7532                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7533             (gnus-summary-exit)
7534           (gnus-summary-next-group nil group backward)))
7535        (t
7536         (push (cdr keve) unread-command-events))))))
7537
7538 (defun gnus-summary-next-unread-article ()
7539   "Select unread article after current one."
7540   (interactive)
7541   (gnus-summary-next-article
7542    (or (not (eq gnus-summary-goto-unread 'never))
7543        (gnus-summary-last-article-p (gnus-summary-article-number)))
7544    (and gnus-auto-select-same
7545         (gnus-summary-article-subject))))
7546
7547 (defun gnus-summary-prev-article (&optional unread subject)
7548   "Select the article after the current one.
7549 If UNREAD is non-nil, only unread articles are selected."
7550   (interactive "P")
7551   (gnus-summary-next-article unread subject t))
7552
7553 (defun gnus-summary-prev-unread-article ()
7554   "Select unread article before current one."
7555   (interactive)
7556   (gnus-summary-prev-article
7557    (or (not (eq gnus-summary-goto-unread 'never))
7558        (gnus-summary-first-article-p (gnus-summary-article-number)))
7559    (and gnus-auto-select-same
7560         (gnus-summary-article-subject))))
7561
7562 (defun gnus-summary-next-page (&optional lines circular stop)
7563   "Show next page of the selected article.
7564 If at the end of the current article, select the next article.
7565 LINES says how many lines should be scrolled up.
7566
7567 If CIRCULAR is non-nil, go to the start of the article instead of
7568 selecting the next article when reaching the end of the current
7569 article.
7570
7571 If STOP is non-nil, just stop when reaching the end of the message.
7572
7573 Also see the variable `gnus-article-skip-boring'."
7574   (interactive "P")
7575   (setq gnus-summary-buffer (current-buffer))
7576   (gnus-set-global-variables)
7577   (let ((article (gnus-summary-article-number))
7578         (article-window (get-buffer-window gnus-article-buffer t))
7579         endp)
7580     ;; If the buffer is empty, we have no article.
7581     (unless article
7582       (error "No article to select"))
7583     (gnus-configure-windows 'article)
7584     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7585         (if (and (eq gnus-summary-goto-unread 'never)
7586                  (not (gnus-summary-last-article-p article)))
7587             (gnus-summary-next-article)
7588           (gnus-summary-next-unread-article))
7589       (if (or (null gnus-current-article)
7590               (null gnus-article-current)
7591               (/= article (cdr gnus-article-current))
7592               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7593           ;; Selected subject is different from current article's.
7594           (gnus-summary-display-article article)
7595         (when article-window
7596           (gnus-eval-in-buffer-window gnus-article-buffer
7597             (setq endp (or (gnus-article-next-page lines)
7598                            (gnus-article-only-boring-p))))
7599           (when endp
7600             (cond (stop
7601                    (gnus-message 3 "End of message"))
7602                   (circular
7603                    (gnus-summary-beginning-of-article))
7604                   (lines
7605                    (gnus-message 3 "End of message"))
7606                   ((null lines)
7607                    (if (and (eq gnus-summary-goto-unread 'never)
7608                             (not (gnus-summary-last-article-p article)))
7609                        (gnus-summary-next-article)
7610                      (gnus-summary-next-unread-article))))))))
7611     (gnus-summary-recenter)
7612     (gnus-summary-position-point)))
7613
7614 (defun gnus-summary-prev-page (&optional lines move)
7615   "Show previous page of selected article.
7616 Argument LINES specifies lines to be scrolled down.
7617 If MOVE, move to the previous unread article if point is at
7618 the beginning of the buffer."
7619   (interactive "P")
7620   (let ((article (gnus-summary-article-number))
7621         (article-window (get-buffer-window gnus-article-buffer t))
7622         endp)
7623     (gnus-configure-windows 'article)
7624     (if (or (null gnus-current-article)
7625             (null gnus-article-current)
7626             (/= article (cdr gnus-article-current))
7627             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7628         ;; Selected subject is different from current article's.
7629         (gnus-summary-display-article article)
7630       (gnus-summary-recenter)
7631       (when article-window
7632         (gnus-eval-in-buffer-window gnus-article-buffer
7633           (setq endp (gnus-article-prev-page lines)))
7634         (when (and move endp)
7635           (cond (lines
7636                  (gnus-message 3 "Beginning of message"))
7637                 ((null lines)
7638                  (if (and (eq gnus-summary-goto-unread 'never)
7639                           (not (gnus-summary-first-article-p article)))
7640                      (gnus-summary-prev-article)
7641                    (gnus-summary-prev-unread-article))))))))
7642   (gnus-summary-position-point))
7643
7644 (defun gnus-summary-prev-page-or-article (&optional lines)
7645   "Show previous page of selected article.
7646 Argument LINES specifies lines to be scrolled down.
7647 If at the beginning of the article, go to the next article."
7648   (interactive "P")
7649   (gnus-summary-prev-page lines t))
7650
7651 (defun gnus-summary-scroll-up (lines)
7652   "Scroll up (or down) one line current article.
7653 Argument LINES specifies lines to be scrolled up (or down if negative)."
7654   (interactive "p")
7655   (gnus-configure-windows 'article)
7656   (gnus-summary-show-thread)
7657   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7658     (gnus-eval-in-buffer-window gnus-article-buffer
7659       (cond ((> lines 0)
7660              (when (gnus-article-next-page lines)
7661                (gnus-message 3 "End of message")))
7662             ((< lines 0)
7663              (gnus-article-prev-page (- lines))))))
7664   (gnus-summary-recenter)
7665   (gnus-summary-position-point))
7666
7667 (defun gnus-summary-scroll-down (lines)
7668   "Scroll down (or up) one line current article.
7669 Argument LINES specifies lines to be scrolled down (or up if negative)."
7670   (interactive "p")
7671   (gnus-summary-scroll-up (- lines)))
7672
7673 (defun gnus-summary-next-same-subject ()
7674   "Select next article which has the same subject as current one."
7675   (interactive)
7676   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7677
7678 (defun gnus-summary-prev-same-subject ()
7679   "Select previous article which has the same subject as current one."
7680   (interactive)
7681   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7682
7683 (defun gnus-summary-next-unread-same-subject ()
7684   "Select next unread article which has the same subject as current one."
7685   (interactive)
7686   (gnus-summary-next-article t (gnus-summary-article-subject)))
7687
7688 (defun gnus-summary-prev-unread-same-subject ()
7689   "Select previous unread article which has the same subject as current one."
7690   (interactive)
7691   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7692
7693 (defun gnus-summary-first-unread-article ()
7694   "Select the first unread article.
7695 Return nil if there are no unread articles."
7696   (interactive)
7697   (prog1
7698       (when (gnus-summary-first-subject t)
7699         (gnus-summary-show-thread)
7700         (gnus-summary-first-subject t)
7701         (gnus-summary-display-article (gnus-summary-article-number)))
7702     (gnus-summary-position-point)))
7703
7704 (defun gnus-summary-first-unread-subject ()
7705   "Place the point on the subject line of the first unread article.
7706 Return nil if there are no unread articles."
7707   (interactive)
7708   (prog1
7709       (when (gnus-summary-first-subject t)
7710         (gnus-summary-show-thread)
7711         (gnus-summary-first-subject t))
7712     (gnus-summary-position-point)))
7713
7714 (defun gnus-summary-first-unseen-subject ()
7715   "Place the point on the subject line of the first unseen article.
7716 Return nil if there are no unseen articles."
7717   (interactive)
7718   (prog1
7719       (when (gnus-summary-first-subject nil nil t)
7720         (gnus-summary-show-thread)
7721         (gnus-summary-first-subject nil nil t))
7722     (gnus-summary-position-point)))
7723
7724 (defun gnus-summary-first-unseen-or-unread-subject ()
7725   "Place the point on the subject line of the first unseen article or,
7726 if all article have been seen, on the subject line of the first unread
7727 article."
7728   (interactive)
7729   (prog1
7730       (unless (when (gnus-summary-first-subject nil nil t)
7731                 (gnus-summary-show-thread)
7732                 (gnus-summary-first-subject nil nil t))
7733         (when (gnus-summary-first-subject t)
7734           (gnus-summary-show-thread)
7735           (gnus-summary-first-subject t)))
7736     (gnus-summary-position-point)))
7737
7738 (defun gnus-summary-first-article ()
7739   "Select the first article.
7740 Return nil if there are no articles."
7741   (interactive)
7742   (prog1
7743       (when (gnus-summary-first-subject)
7744         (gnus-summary-show-thread)
7745         (gnus-summary-first-subject)
7746         (gnus-summary-display-article (gnus-summary-article-number)))
7747     (gnus-summary-position-point)))
7748
7749 (defun gnus-summary-best-unread-article (&optional arg)
7750   "Select the unread article with the highest score.
7751 If given a prefix argument, select the next unread article that has a
7752 score higher than the default score."
7753   (interactive "P")
7754   (let ((article (if arg
7755                      (gnus-summary-better-unread-subject)
7756                    (gnus-summary-best-unread-subject))))
7757     (if article
7758         (gnus-summary-goto-article article)
7759       (error "No unread articles"))))
7760
7761 (defun gnus-summary-best-unread-subject ()
7762   "Select the unread subject with the highest score."
7763   (interactive)
7764   (let ((best -1000000)
7765         (data gnus-newsgroup-data)
7766         article score)
7767     (while data
7768       (and (gnus-data-unread-p (car data))
7769            (> (setq score
7770                     (gnus-summary-article-score (gnus-data-number (car data))))
7771               best)
7772            (setq best score
7773                  article (gnus-data-number (car data))))
7774       (setq data (cdr data)))
7775     (when article
7776       (gnus-summary-goto-subject article))
7777     (gnus-summary-position-point)
7778     article))
7779
7780 (defun gnus-summary-better-unread-subject ()
7781   "Select the first unread subject that has a score over the default score."
7782   (interactive)
7783   (let ((data gnus-newsgroup-data)
7784         article score)
7785     (while (and (setq article (gnus-data-number (car data)))
7786                 (or (gnus-data-read-p (car data))
7787                     (not (> (gnus-summary-article-score article)
7788                             gnus-summary-default-score))))
7789       (setq data (cdr data)))
7790     (when article
7791       (gnus-summary-goto-subject article))
7792     (gnus-summary-position-point)
7793     article))
7794
7795 (defun gnus-summary-last-subject ()
7796   "Go to the last displayed subject line in the group."
7797   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7798     (when article
7799       (gnus-summary-goto-subject article))))
7800
7801 (defun gnus-summary-goto-article (article &optional all-headers force)
7802   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7803 If ALL-HEADERS is non-nil, no header lines are hidden.
7804 If FORCE, go to the article even if it isn't displayed.  If FORCE
7805 is a number, it is the line the article is to be displayed on."
7806   (interactive
7807    (list
7808     (completing-read
7809      "Article number or Message-ID: "
7810      (mapcar (lambda (number) (list (int-to-string number)))
7811              gnus-newsgroup-limit))
7812     current-prefix-arg
7813     t))
7814   (prog1
7815       (if (and (stringp article)
7816                (string-match "@\\|%40" article))
7817           (gnus-summary-refer-article article)
7818         (when (stringp article)
7819           (setq article (string-to-number article)))
7820         (if (gnus-summary-goto-subject article force)
7821             (gnus-summary-display-article article all-headers)
7822           (gnus-message 4 "Couldn't go to article %s" article) nil))
7823     (gnus-summary-position-point)))
7824
7825 (defun gnus-summary-goto-last-article ()
7826   "Go to the previously read article."
7827   (interactive)
7828   (prog1
7829       (when gnus-last-article
7830         (gnus-summary-goto-article gnus-last-article nil t))
7831     (gnus-summary-position-point)))
7832
7833 (defun gnus-summary-pop-article (number)
7834   "Pop one article off the history and go to the previous.
7835 NUMBER articles will be popped off."
7836   (interactive "p")
7837   (let (to)
7838     (setq gnus-newsgroup-history
7839           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7840     (if to
7841         (gnus-summary-goto-article (car to) nil t)
7842       (error "Article history empty")))
7843   (gnus-summary-position-point))
7844
7845 ;; Summary commands and functions for limiting the summary buffer.
7846
7847 (defun gnus-summary-limit-to-articles (n)
7848   "Limit the summary buffer to the next N articles.
7849 If not given a prefix, use the process marked articles instead."
7850   (interactive "P")
7851   (prog1
7852       (let ((articles (gnus-summary-work-articles n)))
7853         (setq gnus-newsgroup-processable nil)
7854         (gnus-summary-limit articles))
7855     (gnus-summary-position-point)))
7856
7857 (defun gnus-summary-pop-limit (&optional total)
7858   "Restore the previous limit.
7859 If given a prefix, remove all limits."
7860   (interactive "P")
7861   (when total
7862     (setq gnus-newsgroup-limits
7863           (list (mapcar (lambda (h) (mail-header-number h))
7864                         gnus-newsgroup-headers))))
7865   (unless gnus-newsgroup-limits
7866     (error "No limit to pop"))
7867   (prog1
7868       (gnus-summary-limit nil 'pop)
7869     (gnus-summary-position-point)))
7870
7871 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7872   "Limit the summary buffer to articles that have subjects that match a regexp.
7873 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7874   (interactive
7875    (list (read-string (if current-prefix-arg
7876                           "Exclude subject (regexp): "
7877                         "Limit to subject (regexp): "))
7878          nil current-prefix-arg))
7879   (unless header
7880     (setq header "subject"))
7881   (when (not (equal "" subject))
7882     (prog1
7883         (let ((articles (gnus-summary-find-matching
7884                          (or header "subject") subject 'all nil nil
7885                          not-matching)))
7886           (unless articles
7887             (error "Found no matches for \"%s\"" subject))
7888           (gnus-summary-limit articles))
7889       (gnus-summary-position-point))))
7890
7891 (defun gnus-summary-limit-to-author (from &optional not-matching)
7892   "Limit the summary buffer to articles that have authors that match a regexp.
7893 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7894   (interactive
7895    (list (read-string (if current-prefix-arg
7896                           "Exclude author (regexp): "
7897                         "Limit to author (regexp): "))
7898          current-prefix-arg))
7899   (gnus-summary-limit-to-subject from "from" not-matching))
7900
7901 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7902   "Limit the summary buffer to articles with the given RECIPIENT.
7903
7904 If NOT-MATCHING, exclude RECIPIENT.
7905
7906 To and Cc headers are checked.  You need to include them in
7907 `nnmail-extra-headers'."
7908   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7909   (interactive
7910    (list (read-string (format "%s recipient (regexp): "
7911                               (if current-prefix-arg "Exclude" "Limit to")))
7912          current-prefix-arg))
7913   (when (not (equal "" recipient))
7914     (prog1 (let* ((to
7915                    (if (memq 'To nnmail-extra-headers)
7916                        (gnus-summary-find-matching
7917                         (cons 'extra 'To) recipient 'all nil nil
7918                         not-matching)
7919                      (gnus-message
7920                       1 "`To' isn't present in `nnmail-extra-headers'")
7921                      (sit-for 1)
7922                      nil))
7923                   (cc
7924                    (if (memq 'Cc nnmail-extra-headers)
7925                        (gnus-summary-find-matching
7926                         (cons 'extra 'Cc) recipient 'all nil nil
7927                         not-matching)
7928                      (gnus-message
7929                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7930                      (sit-for 1)
7931                      nil))
7932                   (articles
7933                    (if not-matching
7934                        ;; We need the numbers that are in both lists:
7935                        (mapcar (lambda (a)
7936                                  (and (memq a to) a))
7937                                cc)
7938                      (nconc to cc))))
7939              (unless articles
7940                (error "Found no matches for \"%s\"" recipient))
7941              (gnus-summary-limit articles))
7942       (gnus-summary-position-point))))
7943
7944 (defun gnus-summary-limit-strange-charsets-predicate (header)
7945   (let ((string (concat (mail-header-subject header)
7946                         (mail-header-from header)))
7947         charset found)
7948     (dotimes (i (1- (length string)))
7949       (setq charset (format "%s" (char-charset (aref string (1+ i)))))
7950       (when (string-match "unicode\\|big\\|japanese" charset)
7951         (setq found t)))
7952     found))
7953
7954 (defun gnus-summary-limit-to-predicate (predicate)
7955   "Limit to articles where PREDICATE returns non-nil.
7956 PREDICATE will be called with the header structures of the
7957 articles."
7958   (let ((articles nil)
7959         (case-fold-search t))
7960     (dolist (header gnus-newsgroup-headers)
7961       (when (funcall predicate header)
7962         (push (mail-header-number header) articles)))
7963     (gnus-summary-limit (nreverse articles))))
7964
7965 (defun gnus-summary-limit-to-age (age &optional younger-p)
7966   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7967 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7968 articles that are younger than AGE days."
7969   (interactive
7970    (let ((younger current-prefix-arg)
7971          (days-got nil)
7972          days)
7973      (while (not days-got)
7974        (setq days (if younger
7975                       (read-string "Limit to articles younger than (in days, older when negative): ")
7976                     (read-string
7977                      "Limit to articles older than (in days, younger when negative): ")))
7978        (when (> (length days) 0)
7979          (setq days (read days)))
7980        (if (numberp days)
7981            (progn
7982              (setq days-got t)
7983              (when (< days 0)
7984                (setq younger (not younger))
7985                (setq days (* days -1))))
7986          (message "Please enter a number.")
7987          (sleep-for 1)))
7988      (list days younger)))
7989   (prog1
7990       (let ((data gnus-newsgroup-data)
7991             (cutoff (days-to-time age))
7992             articles d date is-younger)
7993         (while (setq d (pop data))
7994           (when (and (vectorp (gnus-data-header d))
7995                      (setq date (mail-header-date (gnus-data-header d))))
7996             (setq is-younger (time-less-p
7997                               (time-since (condition-case ()
7998                                               (date-to-time date)
7999                                             (error '(0 0))))
8000                               cutoff))
8001             (when (if younger-p
8002                       is-younger
8003                     (not is-younger))
8004               (push (gnus-data-number d) articles))))
8005         (gnus-summary-limit (nreverse articles)))
8006     (gnus-summary-position-point)))
8007
8008 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8009   "Limit the summary buffer to articles that match an 'extra' header."
8010   (interactive
8011    (let ((header
8012           (intern
8013            (gnus-completing-read-with-default
8014             (symbol-name (car gnus-extra-headers))
8015             (if current-prefix-arg
8016                 "Exclude extra header"
8017               "Limit extra header")
8018             (mapcar (lambda (x)
8019                       (cons (symbol-name x) x))
8020                     gnus-extra-headers)
8021             nil
8022             t))))
8023      (list header
8024            (read-string (format "%s header %s (regexp): "
8025                                 (if current-prefix-arg "Exclude" "Limit to")
8026                                 header))
8027            current-prefix-arg)))
8028   (when (not (equal "" regexp))
8029     (prog1
8030         (let ((articles (gnus-summary-find-matching
8031                          (cons 'extra header) regexp 'all nil nil
8032                          not-matching)))
8033           (unless articles
8034             (error "Found no matches for \"%s\"" regexp))
8035           (gnus-summary-limit articles))
8036       (gnus-summary-position-point))))
8037
8038 (defun gnus-summary-limit-to-display-predicate ()
8039   "Limit the summary buffer to the predicated in the `display' group parameter."
8040   (interactive)
8041   (unless gnus-newsgroup-display
8042     (error "There is no `display' group parameter"))
8043   (let (articles)
8044     (dolist (number gnus-newsgroup-articles)
8045       (when (funcall gnus-newsgroup-display)
8046         (push number articles)))
8047     (gnus-summary-limit articles))
8048   (gnus-summary-position-point))
8049
8050 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8051 (make-obsolete
8052  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8053
8054 (defun gnus-summary-limit-to-unread (&optional all)
8055   "Limit the summary buffer to articles that are not marked as read.
8056 If ALL is non-nil, limit strictly to unread articles."
8057   (interactive "P")
8058   (if all
8059       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8060     (gnus-summary-limit-to-marks
8061      ;; Concat all the marks that say that an article is read and have
8062      ;; those removed.
8063      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8064            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8065            gnus-low-score-mark gnus-expirable-mark
8066            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8067            gnus-duplicate-mark gnus-souped-mark)
8068      'reverse)))
8069
8070 (defun gnus-summary-limit-to-headers (match &optional reverse)
8071   "Limit the summary buffer to articles that have headers that match MATCH.
8072 If REVERSE (the prefix), limit to articles that don't match."
8073   (interactive "sMatch headers (regexp): \nP")
8074   (gnus-summary-limit-to-bodies match reverse t))
8075
8076 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8077   "Limit the summary buffer to articles that have bodies that match MATCH.
8078 If REVERSE (the prefix), limit to articles that don't match."
8079   (interactive "sMatch body (regexp): \nP")
8080   (let ((articles nil)
8081         (gnus-select-article-hook nil)  ;Disable hook.
8082         (gnus-article-prepare-hook nil)
8083         (gnus-use-article-prefetch nil)
8084         (gnus-keep-backlog nil)
8085         (gnus-break-pages nil)
8086         (gnus-summary-display-arrow nil)
8087         (gnus-updated-mode-lines nil)
8088         (gnus-auto-center-summary nil)
8089         (gnus-display-mime-function nil))
8090     (dolist (data gnus-newsgroup-data)
8091       (let (gnus-mark-article-hook)
8092         (gnus-summary-select-article t t nil (gnus-data-number data)))
8093       (save-excursion
8094         (set-buffer gnus-article-buffer)
8095         (article-goto-body)
8096         (let* ((case-fold-search t)
8097                (found (if headersp
8098                           (re-search-backward match nil t)
8099                         (re-search-forward match nil t))))
8100           (when (or (and found
8101                          (not reverse))
8102                     (and (not found)
8103                          reverse))
8104             (push (gnus-data-number data) articles)))))
8105     (if (not articles)
8106         (message "No messages matched")
8107       (gnus-summary-limit articles)))
8108   (gnus-summary-position-point))
8109
8110 (defun gnus-summary-limit-to-replied (&optional unreplied)
8111   "Limit the summary buffer to replied articles.
8112 If UNREPLIED (the prefix), limit to unreplied articles."
8113   (interactive "P")
8114   (if unreplied
8115       (gnus-summary-limit
8116        (gnus-set-difference gnus-newsgroup-articles
8117         gnus-newsgroup-replied))
8118     (gnus-summary-limit gnus-newsgroup-replied))
8119   (gnus-summary-position-point))
8120
8121 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8122 (make-obsolete 'gnus-summary-delete-marked-with
8123                'gnus-summary-limit-exclude-marks)
8124
8125 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8126   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8127 If REVERSE, limit the summary buffer to articles that are marked
8128 with MARKS.  MARKS can either be a string of marks or a list of marks.
8129 Returns how many articles were removed."
8130   (interactive "sMarks: ")
8131   (gnus-summary-limit-to-marks marks t))
8132
8133 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8134   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8135 If REVERSE (the prefix), limit the summary buffer to articles that are
8136 not marked with MARKS.  MARKS can either be a string of marks or a
8137 list of marks.
8138 Returns how many articles were removed."
8139   (interactive "sMarks: \nP")
8140   (prog1
8141       (let ((data gnus-newsgroup-data)
8142             (marks (if (listp marks) marks
8143                      (append marks nil))) ; Transform to list.
8144             articles)
8145         (while data
8146           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8147                   (memq (gnus-data-mark (car data)) marks))
8148             (push (gnus-data-number (car data)) articles))
8149           (setq data (cdr data)))
8150         (gnus-summary-limit articles))
8151     (gnus-summary-position-point)))
8152
8153 (defun gnus-summary-limit-to-score (score)
8154   "Limit to articles with score at or above SCORE."
8155   (interactive "NLimit to articles with score of at least: ")
8156   (let ((data gnus-newsgroup-data)
8157         articles)
8158     (while data
8159       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8160                 score)
8161         (push (gnus-data-number (car data)) articles))
8162       (setq data (cdr data)))
8163     (prog1
8164         (gnus-summary-limit articles)
8165       (gnus-summary-position-point))))
8166
8167 (defun gnus-summary-limit-to-unseen ()
8168   "Limit to unseen articles."
8169   (interactive)
8170   (prog1
8171       (gnus-summary-limit gnus-newsgroup-unseen)
8172     (gnus-summary-position-point)))
8173
8174 (defun gnus-summary-limit-include-thread (id)
8175   "Display all the hidden articles that is in the thread with ID in it.
8176 When called interactively, ID is the Message-ID of the current
8177 article."
8178   (interactive (list (mail-header-id (gnus-summary-article-header))))
8179   (let ((articles (gnus-articles-in-thread
8180                    (gnus-id-to-thread (gnus-root-id id)))))
8181     (prog1
8182         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8183       (gnus-summary-limit-include-matching-articles
8184        "subject"
8185        (regexp-quote (gnus-simplify-subject-re
8186                       (mail-header-subject (gnus-id-to-header id)))))
8187       (gnus-summary-position-point))))
8188
8189 (defun gnus-summary-limit-include-matching-articles (header regexp)
8190   "Display all the hidden articles that have HEADERs that match REGEXP."
8191   (interactive (list (read-string "Match on header: ")
8192                      (read-string "Regexp: ")))
8193   (let ((articles (gnus-find-matching-articles header regexp)))
8194     (prog1
8195         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8196       (gnus-summary-position-point))))
8197
8198 (defun gnus-summary-insert-dormant-articles ()
8199   "Insert all the dormant articles for this group into the current buffer."
8200   (interactive)
8201   (let ((gnus-verbose (max 6 gnus-verbose)))
8202     (if (not gnus-newsgroup-dormant)
8203         (gnus-message 3 "No cached articles for this group")
8204       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8205
8206 (defun gnus-summary-limit-include-dormant ()
8207   "Display all the hidden articles that are marked as dormant.
8208 Note that this command only works on a subset of the articles currently
8209 fetched for this group."
8210   (interactive)
8211   (unless gnus-newsgroup-dormant
8212     (error "There are no dormant articles in this group"))
8213   (prog1
8214       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8215     (gnus-summary-position-point)))
8216
8217 (defun gnus-summary-limit-exclude-dormant ()
8218   "Hide all dormant articles."
8219   (interactive)
8220   (prog1
8221       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8222     (gnus-summary-position-point)))
8223
8224 (defun gnus-summary-limit-exclude-childless-dormant ()
8225   "Hide all dormant articles that have no children."
8226   (interactive)
8227   (let ((data (gnus-data-list t))
8228         articles d children)
8229     ;; Find all articles that are either not dormant or have
8230     ;; children.
8231     (while (setq d (pop data))
8232       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8233                 (and (setq children
8234                            (gnus-article-children (gnus-data-number d)))
8235                      (let (found)
8236                        (while children
8237                          (when (memq (car children) articles)
8238                            (setq children nil
8239                                  found t))
8240                          (pop children))
8241                        found)))
8242         (push (gnus-data-number d) articles)))
8243     ;; Do the limiting.
8244     (prog1
8245         (gnus-summary-limit articles)
8246       (gnus-summary-position-point))))
8247
8248 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8249   "Mark all unread excluded articles as read.
8250 If ALL, mark even excluded ticked and dormants as read."
8251   (interactive "P")
8252   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8253   (let ((articles (gnus-sorted-ndifference
8254                    (sort
8255                     (mapcar (lambda (h) (mail-header-number h))
8256                             gnus-newsgroup-headers)
8257                     '<)
8258                    gnus-newsgroup-limit))
8259         article)
8260     (setq gnus-newsgroup-unreads
8261           (gnus-sorted-intersection gnus-newsgroup-unreads
8262                                     gnus-newsgroup-limit))
8263     (if all
8264         (setq gnus-newsgroup-dormant nil
8265               gnus-newsgroup-marked nil
8266               gnus-newsgroup-reads
8267               (nconc
8268                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8269                gnus-newsgroup-reads))
8270       (while (setq article (pop articles))
8271         (unless (or (memq article gnus-newsgroup-dormant)
8272                     (memq article gnus-newsgroup-marked))
8273           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8274
8275 (defun gnus-summary-limit (articles &optional pop)
8276   (if pop
8277       ;; We pop the previous limit off the stack and use that.
8278       (setq articles (car gnus-newsgroup-limits)
8279             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8280     ;; We use the new limit, so we push the old limit on the stack.
8281     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8282   ;; Set the limit.
8283   (setq gnus-newsgroup-limit articles)
8284   (let ((total (length gnus-newsgroup-data))
8285         (data (gnus-data-find-list (gnus-summary-article-number)))
8286         (gnus-summary-mark-below nil)   ; Inhibit this.
8287         found)
8288     ;; This will do all the work of generating the new summary buffer
8289     ;; according to the new limit.
8290     (gnus-summary-prepare)
8291     ;; Hide any threads, possibly.
8292     (gnus-summary-maybe-hide-threads)
8293     ;; Try to return to the article you were at, or one in the
8294     ;; neighborhood.
8295     (when data
8296       ;; We try to find some article after the current one.
8297       (while data
8298         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8299           (setq data nil
8300                 found t))
8301         (setq data (cdr data))))
8302     (unless found
8303       ;; If there is no data, that means that we were after the last
8304       ;; article.  The same goes when we can't find any articles
8305       ;; after the current one.
8306       (goto-char (point-max))
8307       (gnus-summary-find-prev))
8308     (gnus-set-mode-line 'summary)
8309     ;; We return how many articles were removed from the summary
8310     ;; buffer as a result of the new limit.
8311     (- total (length gnus-newsgroup-data))))
8312
8313 (defsubst gnus-invisible-cut-children (threads)
8314   (let ((num 0))
8315     (while threads
8316       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8317         (incf num))
8318       (pop threads))
8319     (< num 2)))
8320
8321 (defsubst gnus-cut-thread (thread)
8322   "Go forwards in the thread until we find an article that we want to display."
8323   (when (or (eq gnus-fetch-old-headers 'some)
8324             (eq gnus-fetch-old-headers 'invisible)
8325             (numberp gnus-fetch-old-headers)
8326             (eq gnus-build-sparse-threads 'some)
8327             (eq gnus-build-sparse-threads 'more))
8328     ;; Deal with old-fetched headers and sparse threads.
8329     (while (and
8330             thread
8331             (or
8332              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8333              (gnus-summary-article-ancient-p
8334               (mail-header-number (car thread))))
8335             (if (or (<= (length (cdr thread)) 1)
8336                     (eq gnus-fetch-old-headers 'invisible))
8337                 (setq gnus-newsgroup-limit
8338                       (delq (mail-header-number (car thread))
8339                             gnus-newsgroup-limit)
8340                       thread (cadr thread))
8341               (when (gnus-invisible-cut-children (cdr thread))
8342                 (let ((th (cdr thread)))
8343                   (while th
8344                     (if (memq (mail-header-number (caar th))
8345                               gnus-newsgroup-limit)
8346                         (setq thread (car th)
8347                               th nil)
8348                       (setq th (cdr th))))))))))
8349   thread)
8350
8351 (defun gnus-cut-threads (threads)
8352   "Cut off all uninteresting articles from the beginning of THREADS."
8353   (when (or (eq gnus-fetch-old-headers 'some)
8354             (eq gnus-fetch-old-headers 'invisible)
8355             (numberp gnus-fetch-old-headers)
8356             (eq gnus-build-sparse-threads 'some)
8357             (eq gnus-build-sparse-threads 'more))
8358     (let ((th threads))
8359       (while th
8360         (setcar th (gnus-cut-thread (car th)))
8361         (setq th (cdr th)))))
8362   ;; Remove nixed out threads.
8363   (delq nil threads))
8364
8365 (defun gnus-summary-initial-limit (&optional show-if-empty)
8366   "Figure out what the initial limit is supposed to be on group entry.
8367 This entails weeding out unwanted dormants, low-scored articles,
8368 fetch-old-headers verbiage, and so on."
8369   ;; Most groups have nothing to remove.
8370   (if (or gnus-inhibit-limiting
8371           (and (null gnus-newsgroup-dormant)
8372                (eq gnus-newsgroup-display 'gnus-not-ignore)
8373                (not (eq gnus-fetch-old-headers 'some))
8374                (not (numberp gnus-fetch-old-headers))
8375                (not (eq gnus-fetch-old-headers 'invisible))
8376                (null gnus-summary-expunge-below)
8377                (not (eq gnus-build-sparse-threads 'some))
8378                (not (eq gnus-build-sparse-threads 'more))
8379                (null gnus-thread-expunge-below)
8380                (not gnus-use-nocem)))
8381       ()                                ; Do nothing.
8382     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8383     (setq gnus-newsgroup-limit nil)
8384     (mapatoms
8385      (lambda (node)
8386        (unless (car (symbol-value node))
8387          ;; These threads have no parents -- they are roots.
8388          (let ((nodes (cdr (symbol-value node)))
8389                thread)
8390            (while nodes
8391              (if (and gnus-thread-expunge-below
8392                       (< (gnus-thread-total-score (car nodes))
8393                          gnus-thread-expunge-below))
8394                  (gnus-expunge-thread (pop nodes))
8395                (setq thread (pop nodes))
8396                (gnus-summary-limit-children thread))))))
8397      gnus-newsgroup-dependencies)
8398     ;; If this limitation resulted in an empty group, we might
8399     ;; pop the previous limit and use it instead.
8400     (when (and (not gnus-newsgroup-limit)
8401                show-if-empty)
8402       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8403     gnus-newsgroup-limit))
8404
8405 (defun gnus-summary-limit-children (thread)
8406   "Return 1 if this subthread is visible and 0 if it is not."
8407   ;; First we get the number of visible children to this thread.  This
8408   ;; is done by recursing down the thread using this function, so this
8409   ;; will really go down to a leaf article first, before slowly
8410   ;; working its way up towards the root.
8411   (when thread
8412     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8413            (children
8414            (if (cdr thread)
8415                (apply '+ (mapcar 'gnus-summary-limit-children
8416                                  (cdr thread)))
8417              0))
8418           (number (mail-header-number (car thread)))
8419           score)
8420       (if (and
8421            (not (memq number gnus-newsgroup-marked))
8422            (or
8423             ;; If this article is dormant and has absolutely no visible
8424             ;; children, then this article isn't visible.
8425             (and (memq number gnus-newsgroup-dormant)
8426                  (zerop children))
8427             ;; If this is "fetch-old-headered" and there is no
8428             ;; visible children, then we don't want this article.
8429             (and (or (eq gnus-fetch-old-headers 'some)
8430                      (numberp gnus-fetch-old-headers))
8431                  (gnus-summary-article-ancient-p number)
8432                  (zerop children))
8433             ;; If this is "fetch-old-headered" and `invisible', then
8434             ;; we don't want this article.
8435             (and (eq gnus-fetch-old-headers 'invisible)
8436                  (gnus-summary-article-ancient-p number))
8437             ;; If this is a sparsely inserted article with no children,
8438             ;; we don't want it.
8439             (and (eq gnus-build-sparse-threads 'some)
8440                  (gnus-summary-article-sparse-p number)
8441                  (zerop children))
8442             ;; If we use expunging, and this article is really
8443             ;; low-scored, then we don't want this article.
8444             (when (and gnus-summary-expunge-below
8445                        (< (setq score
8446                                 (or (cdr (assq number gnus-newsgroup-scored))
8447                                     gnus-summary-default-score))
8448                           gnus-summary-expunge-below))
8449               ;; We increase the expunge-tally here, but that has
8450               ;; nothing to do with the limits, really.
8451               (incf gnus-newsgroup-expunged-tally)
8452               ;; We also mark as read here, if that's wanted.
8453               (when (and gnus-summary-mark-below
8454                          (< score gnus-summary-mark-below))
8455                 (setq gnus-newsgroup-unreads
8456                       (delq number gnus-newsgroup-unreads))
8457                 (if gnus-newsgroup-auto-expire
8458                     (push number gnus-newsgroup-expirable)
8459                   (push (cons number gnus-low-score-mark)
8460                         gnus-newsgroup-reads)))
8461               t)
8462             ;; Do the `display' group parameter.
8463             (and gnus-newsgroup-display
8464                  (not (funcall gnus-newsgroup-display)))
8465             ;; Check NoCeM things.
8466             (when (and gnus-use-nocem
8467                        (gnus-nocem-unwanted-article-p
8468                         (mail-header-id (car thread))))
8469               (setq gnus-newsgroup-unreads
8470                     (delq number gnus-newsgroup-unreads))
8471               t)))
8472           ;; Nope, invisible article.
8473           0
8474         ;; Ok, this article is to be visible, so we add it to the limit
8475         ;; and return 1.
8476         (push number gnus-newsgroup-limit)
8477         1))))
8478
8479 (defun gnus-expunge-thread (thread)
8480   "Mark all articles in THREAD as read."
8481   (let* ((number (mail-header-number (car thread))))
8482     (incf gnus-newsgroup-expunged-tally)
8483     ;; We also mark as read here, if that's wanted.
8484     (setq gnus-newsgroup-unreads
8485           (delq number gnus-newsgroup-unreads))
8486     (if gnus-newsgroup-auto-expire
8487         (push number gnus-newsgroup-expirable)
8488       (push (cons number gnus-low-score-mark)
8489             gnus-newsgroup-reads)))
8490   ;; Go recursively through all subthreads.
8491   (mapcar 'gnus-expunge-thread (cdr thread)))
8492
8493 ;; Summary article oriented commands
8494
8495 (defun gnus-summary-refer-parent-article (n)
8496   "Refer parent article N times.
8497 If N is negative, go to ancestor -N instead.
8498 The difference between N and the number of articles fetched is returned."
8499   (interactive "p")
8500   (let ((skip 1)
8501         error header ref)
8502     (when (not (natnump n))
8503       (setq skip (abs n)
8504             n 1))
8505     (while (and (> n 0)
8506                 (not error))
8507       (setq header (gnus-summary-article-header))
8508       (if (and (eq (mail-header-number header)
8509                    (cdr gnus-article-current))
8510                (equal gnus-newsgroup-name
8511                       (car gnus-article-current)))
8512           ;; If we try to find the parent of the currently
8513           ;; displayed article, then we take a look at the actual
8514           ;; References header, since this is slightly more
8515           ;; reliable than the References field we got from the
8516           ;; server.
8517           (save-excursion
8518             (set-buffer gnus-original-article-buffer)
8519             (nnheader-narrow-to-headers)
8520             (unless (setq ref (message-fetch-field "references"))
8521               (when (setq ref (message-fetch-field "in-reply-to"))
8522                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8523             (widen))
8524         (setq ref
8525               ;; It's not the current article, so we take a bet on
8526               ;; the value we got from the server.
8527               (mail-header-references header)))
8528       (if (and ref
8529                (not (equal ref "")))
8530           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8531             (gnus-message 1 "Couldn't find parent"))
8532         (gnus-message 1 "No references in article %d"
8533                       (gnus-summary-article-number))
8534         (setq error t))
8535       (decf n))
8536     (gnus-summary-position-point)
8537     n))
8538
8539 (defun gnus-summary-refer-references ()
8540   "Fetch all articles mentioned in the References header.
8541 Return the number of articles fetched."
8542   (interactive)
8543   (let ((ref (mail-header-references (gnus-summary-article-header)))
8544         (current (gnus-summary-article-number))
8545         (n 0))
8546     (if (or (not ref)
8547             (equal ref ""))
8548         (error "No References in the current article")
8549       ;; For each Message-ID in the References header...
8550       (while (string-match "<[^>]*>" ref)
8551         (incf n)
8552         ;; ... fetch that article.
8553         (gnus-summary-refer-article
8554          (prog1 (match-string 0 ref)
8555            (setq ref (substring ref (match-end 0))))))
8556       (gnus-summary-goto-subject current)
8557       (gnus-summary-position-point)
8558       n)))
8559
8560 (defun gnus-summary-refer-thread (&optional limit)
8561   "Fetch all articles in the current thread.
8562 If LIMIT (the numerical prefix), fetch that many old headers instead
8563 of what's specified by the `gnus-refer-thread-limit' variable."
8564   (interactive "P")
8565   (let ((id (mail-header-id (gnus-summary-article-header)))
8566         (limit (if limit (prefix-numeric-value limit)
8567                  gnus-refer-thread-limit)))
8568     (unless (eq gnus-fetch-old-headers 'invisible)
8569       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8570       ;; Retrieve the headers and read them in.
8571       (if (eq (if (numberp limit)
8572                   (gnus-retrieve-headers
8573                    (list (min
8574                           (+ (mail-header-number
8575                               (gnus-summary-article-header))
8576                              limit)
8577                           gnus-newsgroup-end))
8578                    gnus-newsgroup-name (* limit 2))
8579                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8580                 ;; headers.
8581                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8582                                        gnus-newsgroup-name limit))
8583               'nov)
8584           (gnus-build-all-threads)
8585         (error "Can't fetch thread from back ends that don't support NOV"))
8586       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8587     (gnus-summary-limit-include-thread id)))
8588
8589 (defun gnus-summary-refer-article (message-id)
8590   "Fetch an article specified by MESSAGE-ID."
8591   (interactive "sMessage-ID: ")
8592   (when (and (stringp message-id)
8593              (not (zerop (length message-id))))
8594     (setq message-id (gnus-replace-in-string message-id " " ""))
8595     ;; Construct the correct Message-ID if necessary.
8596     ;; Suggested by tale@pawl.rpi.edu.
8597     (unless (string-match "^<" message-id)
8598       (setq message-id (concat "<" message-id)))
8599     (unless (string-match ">$" message-id)
8600       (setq message-id (concat message-id ">")))
8601     ;; People often post MIDs from URLs, so unhex it:
8602     (unless (string-match "@" message-id)
8603       (setq message-id (gnus-url-unhex-string message-id)))
8604     (let* ((header (gnus-id-to-header message-id))
8605            (sparse (and header
8606                         (gnus-summary-article-sparse-p
8607                          (mail-header-number header))
8608                         (memq (mail-header-number header)
8609                               gnus-newsgroup-limit)))
8610            number)
8611       (cond
8612        ;; If the article is present in the buffer we just go to it.
8613        ((and header
8614              (or (not (gnus-summary-article-sparse-p
8615                        (mail-header-number header)))
8616                  sparse))
8617         (prog1
8618             (gnus-summary-goto-article
8619              (mail-header-number header) nil t)
8620           (when sparse
8621             (gnus-summary-update-article (mail-header-number header)))))
8622        (t
8623         ;; We fetch the article.
8624         (catch 'found
8625           (dolist (gnus-override-method (gnus-refer-article-methods))
8626             (when (and (gnus-check-server gnus-override-method)
8627                        ;; Fetch the header,
8628                        (setq number (gnus-summary-insert-subject message-id)))
8629               ;; and display the article.
8630               (gnus-summary-select-article nil nil nil number)
8631               (throw 'found t)))
8632           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8633
8634 (defun gnus-refer-article-methods ()
8635   "Return a list of referable methods."
8636   (cond
8637    ;; No method, so we default to current and native.
8638    ((null gnus-refer-article-method)
8639     (list gnus-current-select-method gnus-select-method))
8640    ;; Current.
8641    ((eq 'current gnus-refer-article-method)
8642     (list gnus-current-select-method))
8643    ;; List of select methods.
8644    ((not (and (symbolp (car gnus-refer-article-method))
8645               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8646     (let (out)
8647       (dolist (method gnus-refer-article-method)
8648         (push (if (eq 'current method)
8649                   gnus-current-select-method
8650                 method)
8651               out))
8652       (nreverse out)))
8653    ;; One single select method.
8654    (t
8655     (list gnus-refer-article-method))))
8656
8657 (defun gnus-summary-edit-parameters ()
8658   "Edit the group parameters of the current group."
8659   (interactive)
8660   (gnus-group-edit-group gnus-newsgroup-name 'params))
8661
8662 (defun gnus-summary-customize-parameters ()
8663   "Customize the group parameters of the current group."
8664   (interactive)
8665   (gnus-group-customize gnus-newsgroup-name))
8666
8667 (defun gnus-summary-enter-digest-group (&optional force)
8668   "Enter an nndoc group based on the current article.
8669 If FORCE, force a digest interpretation.  If not, try
8670 to guess what the document format is."
8671   (interactive "P")
8672   (let ((conf gnus-current-window-configuration))
8673     (save-window-excursion
8674       (save-excursion
8675         (let (gnus-article-prepare-hook
8676               gnus-display-mime-function
8677               gnus-break-pages)
8678           (gnus-summary-select-article))))
8679     (setq gnus-current-window-configuration conf)
8680     (let* ((name (format "%s-%d"
8681                          (gnus-group-prefixed-name
8682                           gnus-newsgroup-name (list 'nndoc ""))
8683                          (with-current-buffer gnus-summary-buffer
8684                            gnus-current-article)))
8685            (ogroup gnus-newsgroup-name)
8686            (params (append (gnus-info-params (gnus-get-info ogroup))
8687                            (list (cons 'to-group ogroup))
8688                            (list (cons 'parent-group ogroup))
8689                            (list (cons 'save-article-group ogroup))))
8690            (case-fold-search t)
8691            (buf (current-buffer))
8692            dig to-address)
8693       (save-excursion
8694         (set-buffer gnus-original-article-buffer)
8695         ;; Have the digest group inherit the main mail address of
8696         ;; the parent article.
8697         (when (setq to-address (or (gnus-fetch-field "reply-to")
8698                                    (gnus-fetch-field "from")))
8699           (setq params
8700                 (append
8701                  (list (cons 'to-address
8702                              (funcall gnus-decode-encoded-address-function
8703                                       to-address))))))
8704         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8705         (insert-buffer-substring gnus-original-article-buffer)
8706         ;; Remove lines that may lead nndoc to misinterpret the
8707         ;; document type.
8708         (narrow-to-region
8709          (goto-char (point-min))
8710          (or (search-forward "\n\n" nil t) (point)))
8711         (goto-char (point-min))
8712         (delete-matching-lines "^Path:\\|^From ")
8713         (widen))
8714       (unwind-protect
8715           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8716                     (gnus-newsgroup-ephemeral-ignored-charsets
8717                      gnus-newsgroup-ignored-charsets))
8718                 (gnus-group-read-ephemeral-group
8719                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8720                               (nndoc-article-type
8721                                ,(if force 'mbox 'guess)))
8722                  t nil nil nil
8723                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8724                                                         "ADAPT")))))
8725               ;; Make all postings to this group go to the parent group.
8726               (nconc (gnus-info-params (gnus-get-info name))
8727                      params)
8728             ;; Couldn't select this doc group.
8729             (switch-to-buffer buf)
8730             (gnus-set-global-variables)
8731             (gnus-configure-windows 'summary)
8732             (gnus-message 3 "Article couldn't be entered?"))
8733         (kill-buffer dig)))))
8734
8735 (defun gnus-summary-read-document (n)
8736   "Open a new group based on the current article(s).
8737 This will allow you to read digests and other similar
8738 documents as newsgroups.
8739 Obeys the standard process/prefix convention."
8740   (interactive "P")
8741   (let* ((ogroup gnus-newsgroup-name)
8742          (params (append (gnus-info-params (gnus-get-info ogroup))
8743                          (list (cons 'to-group ogroup))))
8744          group egroup groups vgroup)
8745     (dolist (article (gnus-summary-work-articles n))
8746       (setq group (format "%s-%d" gnus-newsgroup-name article))
8747       (gnus-summary-remove-process-mark article)
8748       (when (gnus-summary-display-article article)
8749         (save-excursion
8750           (with-temp-buffer
8751             (insert-buffer-substring gnus-original-article-buffer)
8752             ;; Remove some headers that may lead nndoc to make
8753             ;; the wrong guess.
8754             (message-narrow-to-head)
8755             (goto-char (point-min))
8756             (delete-matching-lines "^Path:\\|^From ")
8757             (widen)
8758             (if (setq egroup
8759                       (gnus-group-read-ephemeral-group
8760                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8761                                      (nndoc-article-type guess))
8762                        t nil t))
8763                 (progn
8764             ;; Make all postings to this group go to the parent group.
8765                   (nconc (gnus-info-params (gnus-get-info egroup))
8766                          params)
8767                   (push egroup groups))
8768               ;; Couldn't select this doc group.
8769               (gnus-error 3 "Article couldn't be entered"))))))
8770     ;; Now we have selected all the documents.
8771     (cond
8772      ((not groups)
8773       (error "None of the articles could be interpreted as documents"))
8774      ((gnus-group-read-ephemeral-group
8775        (setq vgroup (format
8776                      "nnvirtual:%s-%s" gnus-newsgroup-name
8777                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8778        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8779        t
8780        (cons (current-buffer) 'summary)))
8781      (t
8782       (error "Couldn't select virtual nndoc group")))))
8783
8784 (defun gnus-summary-isearch-article (&optional regexp-p)
8785   "Do incremental search forward on the current article.
8786 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8787   (interactive "P")
8788   (gnus-summary-select-article)
8789   (gnus-configure-windows 'article)
8790   (gnus-eval-in-buffer-window gnus-article-buffer
8791     (save-restriction
8792       (widen)
8793       (isearch-forward regexp-p))))
8794
8795 (defun gnus-summary-repeat-search-article-forward ()
8796   "Repeat the previous search forwards."
8797   (interactive)
8798   (unless gnus-last-search-regexp
8799     (error "No previous search"))
8800   (gnus-summary-search-article-forward gnus-last-search-regexp))
8801
8802 (defun gnus-summary-repeat-search-article-backward ()
8803   "Repeat the previous search backwards."
8804   (interactive)
8805   (unless gnus-last-search-regexp
8806     (error "No previous search"))
8807   (gnus-summary-search-article-forward gnus-last-search-regexp t))
8808
8809 (defun gnus-summary-search-article-forward (regexp &optional backward)
8810   "Search for an article containing REGEXP forward.
8811 If BACKWARD, search backward instead."
8812   (interactive
8813    (list (read-string
8814           (format "Search article %s (regexp%s): "
8815                   (if current-prefix-arg "backward" "forward")
8816                   (if gnus-last-search-regexp
8817                       (concat ", default " gnus-last-search-regexp)
8818                     "")))
8819          current-prefix-arg))
8820   (if (string-equal regexp "")
8821       (setq regexp (or gnus-last-search-regexp ""))
8822     (setq gnus-last-search-regexp regexp)
8823     (setq gnus-article-before-search gnus-current-article))
8824   ;; Intentionally set gnus-last-article.
8825   (setq gnus-last-article gnus-article-before-search)
8826   (let ((gnus-last-article gnus-last-article))
8827     (if (gnus-summary-search-article regexp backward)
8828         (gnus-summary-show-thread)
8829       (signal 'search-failed (list regexp)))))
8830
8831 (defun gnus-summary-search-article-backward (regexp)
8832   "Search for an article containing REGEXP backward."
8833   (interactive
8834    (list (read-string
8835           (format "Search article backward (regexp%s): "
8836                   (if gnus-last-search-regexp
8837                       (concat ", default " gnus-last-search-regexp)
8838                     "")))))
8839   (gnus-summary-search-article-forward regexp 'backward))
8840
8841 (defun gnus-summary-search-article (regexp &optional backward)
8842   "Search for an article containing REGEXP.
8843 Optional argument BACKWARD means do search for backward.
8844 `gnus-select-article-hook' is not called during the search."
8845   ;; We have to require this here to make sure that the following
8846   ;; dynamic binding isn't shadowed by autoloading.
8847   (require 'gnus-async)
8848   (require 'gnus-art)
8849   (let ((gnus-select-article-hook nil)  ;Disable hook.
8850         (gnus-article-prepare-hook nil)
8851         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8852         (gnus-use-article-prefetch nil)
8853         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8854         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8855         (gnus-visual nil)
8856         (gnus-keep-backlog nil)
8857         (gnus-break-pages nil)
8858         (gnus-summary-display-arrow nil)
8859         (gnus-updated-mode-lines nil)
8860         (gnus-auto-center-summary nil)
8861         (sum (current-buffer))
8862         (gnus-display-mime-function nil)
8863         (found nil)
8864         point)
8865     (gnus-save-hidden-threads
8866       (gnus-summary-select-article)
8867       (set-buffer gnus-article-buffer)
8868       (goto-char (window-point (get-buffer-window (current-buffer))))
8869       (when backward
8870         (forward-line -1))
8871       (while (not found)
8872         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8873         (if (if backward
8874                 (re-search-backward regexp nil t)
8875               (re-search-forward regexp nil t))
8876             ;; We found the regexp.
8877             (progn
8878               (setq found 'found)
8879               (beginning-of-line)
8880               (set-window-start
8881                (get-buffer-window (current-buffer))
8882                (point))
8883               (forward-line 1)
8884               (set-window-point
8885                (get-buffer-window (current-buffer))
8886                (point))
8887               (set-buffer sum)
8888               (setq point (point)))
8889           ;; We didn't find it, so we go to the next article.
8890           (set-buffer sum)
8891           (setq found 'not)
8892           (while (eq found 'not)
8893             (if (not (if backward (gnus-summary-find-prev)
8894                        (gnus-summary-find-next)))
8895                 ;; No more articles.
8896                 (setq found t)
8897               ;; Select the next article and adjust point.
8898               (unless (gnus-summary-article-sparse-p
8899                        (gnus-summary-article-number))
8900                 (setq found nil)
8901                 (gnus-summary-select-article)
8902                 (set-buffer gnus-article-buffer)
8903                 (widen)
8904                 (goto-char (if backward (point-max) (point-min))))))))
8905       (gnus-message 7 ""))
8906     ;; Return whether we found the regexp.
8907     (when (eq found 'found)
8908       (goto-char point)
8909       (gnus-summary-show-thread)
8910       (gnus-summary-goto-subject gnus-current-article)
8911       (gnus-summary-position-point)
8912       t)))
8913
8914 (defun gnus-find-matching-articles (header regexp)
8915   "Return a list of all articles that match REGEXP on HEADER.
8916 This search includes all articles in the current group that Gnus has
8917 fetched headers for, whether they are displayed or not."
8918   (let ((articles nil)
8919         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8920         (case-fold-search t))
8921     (dolist (header gnus-newsgroup-headers)
8922       (when (string-match regexp (funcall func header))
8923         (push (mail-header-number header) articles)))
8924     (nreverse articles)))
8925
8926 (defun gnus-summary-find-matching (header regexp &optional backward unread
8927                                           not-case-fold not-matching)
8928   "Return a list of all articles that match REGEXP on HEADER.
8929 The search stars on the current article and goes forwards unless
8930 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8931 If UNREAD is non-nil, only unread articles will
8932 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8933 in the comparisons. If NOT-MATCHING, return a list of all articles that
8934 not match REGEXP on HEADER."
8935   (let ((case-fold-search (not not-case-fold))
8936         articles d func)
8937     (if (consp header)
8938         (if (eq (car header) 'extra)
8939             (setq func
8940                   `(lambda (h)
8941                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8942                          "")))
8943           (error "%s is an invalid header" header))
8944       (unless (fboundp (intern (concat "mail-header-" header)))
8945         (error "%s is not a valid header" header))
8946       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8947     (dolist (d (if (eq backward 'all)
8948                    gnus-newsgroup-data
8949                  (gnus-data-find-list
8950                   (gnus-summary-article-number)
8951                   (gnus-data-list backward))))
8952       (when (and (or (not unread)       ; We want all articles...
8953                      (gnus-data-unread-p d)) ; Or just unreads.
8954                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8955                  (if not-matching
8956                      (not (string-match
8957                            regexp
8958                            (funcall func (gnus-data-header d))))
8959                    (string-match regexp
8960                                  (funcall func (gnus-data-header d)))))
8961         (push (gnus-data-number d) articles))) ; Success!
8962     (nreverse articles)))
8963
8964 (defun gnus-summary-execute-command (header regexp command &optional backward)
8965   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8966 If HEADER is an empty string (or nil), the match is done on the entire
8967 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8968   (interactive
8969    (list (let ((completion-ignore-case t))
8970            (completing-read
8971             "Header name: "
8972             (mapcar (lambda (header) (list (format "%s" header)))
8973                     (append
8974                      '("Number" "Subject" "From" "Lines" "Date"
8975                        "Message-ID" "Xref" "References" "Body")
8976                      gnus-extra-headers))
8977             nil 'require-match))
8978          (read-string "Regexp: ")
8979          (read-key-sequence "Command: ")
8980          current-prefix-arg))
8981   (when (equal header "Body")
8982     (setq header ""))
8983   ;; Hidden thread subtrees must be searched as well.
8984   (gnus-summary-show-all-threads)
8985   ;; We don't want to change current point nor window configuration.
8986   (save-excursion
8987     (save-window-excursion
8988       (let (gnus-visual
8989             gnus-treat-strip-trailing-blank-lines
8990             gnus-treat-strip-leading-blank-lines
8991             gnus-treat-strip-multiple-blank-lines
8992             gnus-treat-hide-boring-headers
8993             gnus-treat-fold-newsgroups
8994             gnus-article-prepare-hook)
8995         (gnus-message 6 "Executing %s..." (key-description command))
8996         ;; We'd like to execute COMMAND interactively so as to give arguments.
8997         (gnus-execute header regexp
8998                       `(call-interactively ',(key-binding command))
8999                       backward)
9000         (gnus-message 6 "Executing %s...done" (key-description command))))))
9001
9002 (defun gnus-summary-beginning-of-article ()
9003   "Scroll the article back to the beginning."
9004   (interactive)
9005   (gnus-summary-select-article)
9006   (gnus-configure-windows 'article)
9007   (gnus-eval-in-buffer-window gnus-article-buffer
9008     (widen)
9009     (goto-char (point-min))
9010     (when gnus-break-pages
9011       (gnus-narrow-to-page))))
9012
9013 (defun gnus-summary-end-of-article ()
9014   "Scroll to the end of the article."
9015   (interactive)
9016   (gnus-summary-select-article)
9017   (gnus-configure-windows 'article)
9018   (gnus-eval-in-buffer-window gnus-article-buffer
9019     (widen)
9020     (goto-char (point-max))
9021     (recenter -3)
9022     (when gnus-break-pages
9023       (when (re-search-backward page-delimiter nil t)
9024         (narrow-to-region (match-end 0) (point-max)))
9025       (gnus-narrow-to-page))))
9026
9027 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9028   "Truncate to LEN and quote all \"(\"'s in STRING."
9029   (gnus-replace-in-string (if (and len (> (length string) len))
9030                               (substring string 0 len)
9031                             string)
9032                           "[()]" "\\\\\\&"))
9033
9034 (defun gnus-summary-print-article (&optional filename n)
9035   "Generate and print a PostScript image of the process-marked (mail) articles.
9036
9037 If used interactively, print the current article if none are
9038 process-marked.  With prefix arg, prompt the user for the name of the
9039 file to save in.
9040
9041 When used from Lisp, accept two optional args FILENAME and N.  N means
9042 to print the next N articles.  If N is negative, print the N previous
9043 articles.  If N is nil and articles have been marked with the process
9044 mark, print these instead.
9045
9046 If the optional first argument FILENAME is nil, send the image to the
9047 printer.  If FILENAME is a string, save the PostScript image in a file with
9048 that name.  If FILENAME is a number, prompt the user for the name of the file
9049 to save in."
9050   (interactive (list (ps-print-preprint current-prefix-arg)))
9051   (dolist (article (gnus-summary-work-articles n))
9052     (gnus-summary-select-article nil nil 'pseudo article)
9053     (gnus-eval-in-buffer-window gnus-article-buffer
9054       (gnus-print-buffer))
9055     (gnus-summary-remove-process-mark article))
9056   (ps-despool filename))
9057
9058 (defun gnus-print-buffer ()
9059   (let ((buffer (generate-new-buffer " *print*")))
9060     (unwind-protect
9061         (progn
9062           (copy-to-buffer buffer (point-min) (point-max))
9063           (set-buffer buffer)
9064           (gnus-remove-text-with-property 'gnus-decoration)
9065           (when (gnus-visual-p 'article-highlight 'highlight)
9066             ;; Copy-to-buffer doesn't copy overlay.  So redo
9067             ;; highlight.
9068             (let ((gnus-article-buffer buffer))
9069               (gnus-article-highlight-citation t)
9070               (gnus-article-highlight-signature)
9071               (gnus-article-emphasize)
9072               (gnus-article-delete-invisible-text)))
9073           (let ((ps-left-header
9074                  (list
9075                   (concat "("
9076                           (gnus-summary-print-truncate-and-quote
9077                            (mail-header-subject gnus-current-headers)
9078                            66) ")")
9079                   (concat "("
9080                           (gnus-summary-print-truncate-and-quote
9081                            (mail-header-from gnus-current-headers)
9082                            45) ")")))
9083                 (ps-right-header
9084                  (list
9085                   "/pagenumberstring load"
9086                   (concat "("
9087                           (mail-header-date gnus-current-headers) ")"))))
9088             (gnus-run-hooks 'gnus-ps-print-hook)
9089             (save-excursion
9090               (if window-system
9091                   (ps-spool-buffer-with-faces)
9092                 (ps-spool-buffer)))))
9093       (kill-buffer buffer))))
9094
9095 (defun gnus-summary-show-article (&optional arg)
9096   "Force redisplaying of the current article.
9097 If ARG (the prefix) is a number, show the article with the charset
9098 defined in `gnus-summary-show-article-charset-alist', or the charset
9099 input.
9100 If ARG (the prefix) is non-nil and not a number, show the raw article
9101 without any article massaging functions being run.  Normally, the key
9102 strokes are `C-u g'."
9103   (interactive "P")
9104   (cond
9105    ((numberp arg)
9106     (gnus-summary-show-article t)
9107     (let ((gnus-newsgroup-charset
9108            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9109                (mm-read-coding-system
9110                 "View as charset: " ;; actually it is coding system.
9111                 (with-current-buffer gnus-article-buffer
9112                   (mm-detect-coding-region (point) (point-max))))))
9113           (gnus-newsgroup-ignored-charsets 'gnus-all))
9114       (gnus-summary-select-article nil 'force)
9115       (let ((deps gnus-newsgroup-dependencies)
9116             head header lines)
9117         (save-excursion
9118           (set-buffer gnus-original-article-buffer)
9119           (save-restriction
9120             (message-narrow-to-head)
9121             (setq head (buffer-string))
9122             (goto-char (point-min))
9123             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9124               (goto-char (point-max))
9125               (widen)
9126               (setq lines (1- (count-lines (point) (point-max))))))
9127           (with-temp-buffer
9128             (insert (format "211 %d Article retrieved.\n"
9129                             (cdr gnus-article-current)))
9130             (insert head)
9131             (if lines (insert (format "Lines: %d\n" lines)))
9132             (insert ".\n")
9133             (let ((nntp-server-buffer (current-buffer)))
9134               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9135         (gnus-data-set-header
9136          (gnus-data-find (cdr gnus-article-current))
9137          header)
9138         (gnus-summary-update-article-line
9139          (cdr gnus-article-current) header)
9140         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9141           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9142    ((not arg)
9143     ;; Select the article the normal way.
9144     (gnus-summary-select-article nil 'force))
9145    (t
9146     ;; We have to require this here to make sure that the following
9147     ;; dynamic binding isn't shadowed by autoloading.
9148     (require 'gnus-async)
9149     (require 'gnus-art)
9150     ;; Bind the article treatment functions to nil.
9151     (let ((gnus-have-all-headers t)
9152           gnus-article-prepare-hook
9153           gnus-article-decode-hook
9154           gnus-display-mime-function
9155           gnus-break-pages)
9156       ;; Destroy any MIME parts.
9157       (when (gnus-buffer-live-p gnus-article-buffer)
9158         (save-excursion
9159           (set-buffer gnus-article-buffer)
9160           (mm-destroy-parts gnus-article-mime-handles)
9161           ;; Set it to nil for safety reason.
9162           (setq gnus-article-mime-handle-alist nil)
9163           (setq gnus-article-mime-handles nil)))
9164       (gnus-summary-select-article nil 'force))))
9165   (gnus-summary-goto-subject gnus-current-article)
9166   (gnus-summary-position-point))
9167
9168 (defun gnus-summary-show-raw-article ()
9169   "Show the raw article without any article massaging functions being run."
9170   (interactive)
9171   (gnus-summary-show-article t))
9172
9173 (defun gnus-summary-verbose-headers (&optional arg)
9174   "Toggle permanent full header display.
9175 If ARG is a positive number, turn header display on.
9176 If ARG is a negative number, turn header display off."
9177   (interactive "P")
9178   (setq gnus-show-all-headers
9179         (cond ((or (not (numberp arg))
9180                    (zerop arg))
9181                (not gnus-show-all-headers))
9182               ((natnump arg)
9183                t)))
9184   (gnus-summary-show-article))
9185
9186 (defun gnus-summary-toggle-header (&optional arg)
9187   "Show the headers if they are hidden, or hide them if they are shown.
9188 If ARG is a positive number, show the entire header.
9189 If ARG is a negative number, hide the unwanted header lines."
9190   (interactive "P")
9191   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9192                      (get-buffer-window gnus-article-buffer t))))
9193     (with-current-buffer gnus-article-buffer
9194       (widen)
9195       (article-narrow-to-head)
9196       (let* ((buffer-read-only nil)
9197              (inhibit-point-motion-hooks t)
9198              (hidden (if (numberp arg)
9199                          (>= arg 0)
9200                        (or (not (looking-at "[^ \t\n]+:"))
9201                            (gnus-article-hidden-text-p 'headers))))
9202              s e)
9203         (delete-region (point-min) (point-max))
9204         (with-current-buffer gnus-original-article-buffer
9205           (goto-char (setq s (point-min)))
9206           (setq e (if (search-forward "\n\n" nil t)
9207                       (1- (point))
9208                     (point-max))))
9209         (insert-buffer-substring gnus-original-article-buffer s e)
9210         (run-hooks 'gnus-article-decode-hook)
9211         (if hidden
9212             (let ((gnus-treat-hide-headers nil)
9213                   (gnus-treat-hide-boring-headers nil))
9214               (gnus-delete-wash-type 'headers)
9215               (gnus-treat-article 'head))
9216           (gnus-treat-article 'head))
9217         (widen)
9218         (if window
9219             (set-window-start window (goto-char (point-min))))
9220         (if gnus-break-pages
9221             (gnus-narrow-to-page)
9222           (when (gnus-visual-p 'page-marker)
9223             (let ((buffer-read-only nil))
9224               (gnus-remove-text-with-property 'gnus-prev)
9225               (gnus-remove-text-with-property 'gnus-next))))
9226         (gnus-set-mode-line 'article)))))
9227
9228 (defun gnus-summary-show-all-headers ()
9229   "Make all header lines visible."
9230   (interactive)
9231   (gnus-summary-toggle-header 1))
9232
9233 (defun gnus-summary-caesar-message (&optional arg)
9234   "Caesar rotate the current article by 13.
9235 With a non-numerical prefix, also rotate headers.  A numerical
9236 prefix specifies how many places to rotate each letter forward."
9237   (interactive "P")
9238   (gnus-summary-select-article)
9239   (let ((mail-header-separator ""))
9240     (gnus-eval-in-buffer-window gnus-article-buffer
9241       (save-restriction
9242         (widen)
9243         (let ((start (window-start))
9244               buffer-read-only)
9245           (if (equal arg '(4))
9246               (message-caesar-buffer-body nil t)
9247             (message-caesar-buffer-body arg))
9248           (set-window-start (get-buffer-window (current-buffer)) start)))))
9249   ;; Create buttons and stuff...
9250   (gnus-treat-article nil))
9251
9252 (defun gnus-summary-idna-message (&optional arg)
9253   "Decode IDNA encoded domain names in the current articles.
9254 IDNA encoded domain names looks like `xn--bar'.  If a string
9255 remain unencoded after running this function, it is likely an
9256 invalid IDNA string (`xn--bar' is invalid).
9257
9258 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9259 installed for this command to work."
9260   (interactive "P")
9261   (if (not (and (condition-case nil (require 'idna)
9262                   (file-error))
9263                 (mm-coding-system-p 'utf-8)
9264                 (executable-find (symbol-value 'idna-program))))
9265       (gnus-message
9266        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9267     (gnus-summary-select-article)
9268     (let ((mail-header-separator ""))
9269       (gnus-eval-in-buffer-window gnus-article-buffer
9270         (save-restriction
9271           (widen)
9272           (let ((start (window-start))
9273                 buffer-read-only)
9274             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9275               (replace-match (idna-to-unicode (match-string 1))))
9276             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9277
9278 (defun gnus-summary-morse-message (&optional arg)
9279   "Morse decode the current article."
9280   (interactive "P")
9281   (gnus-summary-select-article)
9282   (let ((mail-header-separator ""))
9283     (gnus-eval-in-buffer-window gnus-article-buffer
9284       (save-excursion
9285         (save-restriction
9286           (widen)
9287           (let ((pos (window-start))
9288                 buffer-read-only)
9289             (goto-char (point-min))
9290             (when (message-goto-body)
9291               (gnus-narrow-to-body))
9292             (goto-char (point-min))
9293             (while (search-forward "·" (point-max) t)
9294               (replace-match "."))
9295             (unmorse-region (point-min) (point-max))
9296             (widen)
9297             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9298
9299 (defun gnus-summary-stop-page-breaking ()
9300   "Stop page breaking in the current article."
9301   (interactive)
9302   (gnus-summary-select-article)
9303   (gnus-eval-in-buffer-window gnus-article-buffer
9304     (widen)
9305     (when (gnus-visual-p 'page-marker)
9306       (let ((buffer-read-only nil))
9307         (gnus-remove-text-with-property 'gnus-prev)
9308         (gnus-remove-text-with-property 'gnus-next))
9309       (setq gnus-page-broken nil))))
9310
9311 (defun gnus-summary-move-article (&optional n to-newsgroup
9312                                             select-method action)
9313   "Move the current article to a different newsgroup.
9314 If N is a positive number, move the N next articles.
9315 If N is a negative number, move the N previous articles.
9316 If N is nil and any articles have been marked with the process mark,
9317 move those articles instead.
9318 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9319 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9320 re-spool using this method.
9321
9322 When called interactively with TO-NEWSGROUP being nil, the value of
9323 the variable `gnus-move-split-methods' is used for finding a default
9324 for the target newsgroup.
9325
9326 For this function to work, both the current newsgroup and the
9327 newsgroup that you want to move to have to support the `request-move'
9328 and `request-accept' functions.
9329
9330 ACTION can be either `move' (the default), `crosspost' or `copy'."
9331   (interactive "P")
9332   (unless action
9333     (setq action 'move))
9334   ;; Check whether the source group supports the required functions.
9335   (cond ((and (eq action 'move)
9336               (not (gnus-check-backend-function
9337                     'request-move-article gnus-newsgroup-name)))
9338          (error "The current group does not support article moving"))
9339         ((and (eq action 'crosspost)
9340               (not (gnus-check-backend-function
9341                     'request-replace-article gnus-newsgroup-name)))
9342          (error "The current group does not support article editing")))
9343   (let ((articles (gnus-summary-work-articles n))
9344         (prefix (if (gnus-check-backend-function
9345                      'request-move-article gnus-newsgroup-name)
9346                     (funcall gnus-move-group-prefix-function
9347                              gnus-newsgroup-name)
9348                   ""))
9349         (names '((move "Move" "Moving")
9350                  (copy "Copy" "Copying")
9351                  (crosspost "Crosspost" "Crossposting")))
9352         (copy-buf (save-excursion
9353                     (nnheader-set-temp-buffer " *copy article*")))
9354         art-group to-method new-xref article to-groups articles-to-update-marks)
9355     (unless (assq action names)
9356       (error "Unknown action %s" action))
9357     ;; Read the newsgroup name.
9358     (when (and (not to-newsgroup)
9359                (not select-method))
9360       (if (and gnus-move-split-methods
9361                (not
9362                 (and (memq gnus-current-article articles)
9363                      (gnus-buffer-live-p gnus-original-article-buffer))))
9364           ;; When `gnus-move-split-methods' is non-nil, we have to
9365           ;; select an article to give `gnus-read-move-group-name' an
9366           ;; opportunity to suggest an appropriate default.  However,
9367           ;; we needn't render or mark the article.
9368           (let ((gnus-display-mime-function nil)
9369                 (gnus-article-prepare-hook nil)
9370                 (gnus-mark-article-hook nil))
9371             (gnus-summary-select-article nil nil nil (car articles))))
9372       (setq to-newsgroup
9373             (gnus-read-move-group-name
9374              (cadr (assq action names))
9375              (symbol-value (intern (format "gnus-current-%s-group" action)))
9376              articles prefix))
9377       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9378     (setq to-method (or select-method
9379                         (gnus-server-to-method
9380                          (gnus-group-method to-newsgroup))))
9381     ;; Check the method we are to move this article to...
9382     (unless (gnus-check-backend-function
9383              'request-accept-article (car to-method))
9384       (error "%s does not support article copying" (car to-method)))
9385     (unless (gnus-check-server to-method)
9386       (error "Can't open server %s" (car to-method)))
9387     (gnus-message 6 "%s to %s: %s..."
9388                   (caddr (assq action names))
9389                   (or (car select-method) to-newsgroup) articles)
9390     (while articles
9391       (setq article (pop articles))
9392       (setq
9393        art-group
9394        (cond
9395         ;; Move the article.
9396         ((eq action 'move)
9397          ;; Remove this article from future suppression.
9398          (gnus-dup-unsuppress-article article)
9399          (let* ((from-method (gnus-find-method-for-group
9400                               gnus-newsgroup-name))
9401                 (to-method (gnus-find-method-for-group
9402                             to-newsgroup))
9403                 (move-is-internal (gnus-method-equal from-method to-method)))
9404            (gnus-request-move-article
9405             article                     ; Article to move
9406             gnus-newsgroup-name         ; From newsgroup
9407             (nth 1 (gnus-find-method-for-group
9408                     gnus-newsgroup-name)) ; Server
9409             (list 'gnus-request-accept-article
9410                   to-newsgroup (list 'quote select-method)
9411                   (not articles) t)     ; Accept form
9412             (not articles)              ; Only save nov last time
9413             move-is-internal)))         ; is this move internal?
9414         ;; Copy the article.
9415         ((eq action 'copy)
9416          (save-excursion
9417            (set-buffer copy-buf)
9418            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9419              (save-restriction
9420                (nnheader-narrow-to-headers)
9421                (dolist (hdr gnus-copy-article-ignored-headers)
9422                  (message-remove-header hdr t)))
9423              (gnus-request-accept-article
9424               to-newsgroup select-method (not articles) t))))
9425         ;; Crosspost the article.
9426         ((eq action 'crosspost)
9427          (let ((xref (message-tokenize-header
9428                       (mail-header-xref (gnus-summary-article-header article))
9429                       " ")))
9430            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9431                                   ":" (number-to-string article)))
9432            (unless xref
9433              (setq xref (list (system-name))))
9434            (setq new-xref
9435                  (concat
9436                   (mapconcat 'identity
9437                              (delete "Xref:" (delete new-xref xref))
9438                              " ")
9439                   " " new-xref))
9440            (save-excursion
9441              (set-buffer copy-buf)
9442              ;; First put the article in the destination group.
9443              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9444              (when (consp (setq art-group
9445                                 (gnus-request-accept-article
9446                                  to-newsgroup select-method (not articles) t)))
9447                (setq new-xref (concat new-xref " " (car art-group)
9448                                       ":"
9449                                       (number-to-string (cdr art-group))))
9450                ;; Now we have the new Xrefs header, so we insert
9451                ;; it and replace the new article.
9452                (nnheader-replace-header "Xref" new-xref)
9453                (gnus-request-replace-article
9454                 (cdr art-group) to-newsgroup (current-buffer) t)
9455                art-group))))))
9456       (cond
9457        ((not art-group)
9458         (gnus-message 1 "Couldn't %s article %s: %s"
9459                       (cadr (assq action names)) article
9460                       (nnheader-get-report (car to-method))))
9461        ((eq art-group 'junk)
9462         (when (eq action 'move)
9463           (gnus-summary-mark-article article gnus-canceled-mark)
9464           (gnus-message 4 "Deleted article %s" article)
9465           ;; run the delete hook
9466           (run-hook-with-args 'gnus-summary-article-delete-hook
9467                               action
9468                               (gnus-data-header
9469                                (assoc article (gnus-data-list nil)))
9470                               gnus-newsgroup-name nil
9471                               select-method)))
9472        (t
9473         (let* ((pto-group (gnus-group-prefixed-name
9474                            (car art-group) to-method))
9475                (info (gnus-get-info pto-group))
9476                (to-group (gnus-info-group info))
9477                to-marks)
9478           ;; Update the group that has been moved to.
9479           (when (and info
9480                      (memq action '(move copy)))
9481             (unless (member to-group to-groups)
9482               (push to-group to-groups))
9483
9484             (unless (memq article gnus-newsgroup-unreads)
9485               (push 'read to-marks)
9486               (gnus-info-set-read
9487                info (gnus-add-to-range (gnus-info-read info)
9488                                        (list (cdr art-group)))))
9489
9490             ;; See whether the article is to be put in the cache.
9491             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9492                              gnus-article-mark-lists
9493                            (delete '(expirable . expire)
9494                                    (copy-sequence gnus-article-mark-lists))))
9495                   (to-article (cdr art-group)))
9496
9497               ;; Enter the article into the cache in the new group,
9498               ;; if that is required.
9499               (when gnus-use-cache
9500                 (gnus-cache-possibly-enter-article
9501                  to-group to-article
9502                  (memq article gnus-newsgroup-marked)
9503                  (memq article gnus-newsgroup-dormant)
9504                  (memq article gnus-newsgroup-unreads)))
9505
9506               (when gnus-preserve-marks
9507                 ;; Copy any marks over to the new group.
9508                 (when (and (equal to-group gnus-newsgroup-name)
9509                            (not (memq article gnus-newsgroup-unreads)))
9510                   ;; Mark this article as read in this group.
9511                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9512                   (setcdr (gnus-active to-group) to-article)
9513                   (setcdr gnus-newsgroup-active to-article))
9514
9515                 (while marks
9516                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9517                     (when (memq article (symbol-value
9518                                          (intern (format "gnus-newsgroup-%s"
9519                                                          (caar marks)))))
9520                       (push (cdar marks) to-marks)
9521                       ;; If the other group is the same as this group,
9522                       ;; then we have to add the mark to the list.
9523                       (when (equal to-group gnus-newsgroup-name)
9524                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9525                              (cons to-article
9526                                    (symbol-value
9527                                     (intern (format "gnus-newsgroup-%s"
9528                                                     (caar marks)))))))
9529                       ;; Copy the marks to other group.
9530                       (gnus-add-marked-articles
9531                        to-group (cdar marks) (list to-article) info)))
9532                   (setq marks (cdr marks)))
9533
9534                 (gnus-request-set-mark
9535                  to-group (list (list (list to-article) 'add to-marks))))
9536
9537               (gnus-dribble-enter
9538                (concat "(gnus-group-set-info '"
9539                        (gnus-prin1-to-string (gnus-get-info to-group))
9540                        ")"))))
9541
9542           ;; Update the Xref header in this article to point to
9543           ;; the new crossposted article we have just created.
9544           (when (eq action 'crosspost)
9545             (save-excursion
9546               (set-buffer copy-buf)
9547               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9548               (nnheader-replace-header "Xref" new-xref)
9549               (gnus-request-replace-article
9550                article gnus-newsgroup-name (current-buffer) t)))
9551
9552           ;; run the move/copy/crosspost/respool hook
9553           (run-hook-with-args 'gnus-summary-article-move-hook
9554                               action
9555                               (gnus-data-header
9556                                (assoc article (gnus-data-list nil)))
9557                               gnus-newsgroup-name
9558                               to-newsgroup
9559                               select-method))
9560
9561         ;;;!!!Why is this necessary?
9562         (set-buffer gnus-summary-buffer)
9563
9564         (gnus-summary-goto-subject article)
9565         (when (eq action 'move)
9566           (gnus-summary-mark-article article gnus-canceled-mark))))
9567       (push article articles-to-update-marks))
9568
9569     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9570     ;; Re-activate all groups that have been moved to.
9571     (save-excursion
9572       (set-buffer gnus-group-buffer)
9573       (let ((gnus-group-marked to-groups))
9574         (gnus-group-get-new-news-this-group nil t)))
9575
9576     (gnus-kill-buffer copy-buf)
9577     (gnus-summary-position-point)
9578     (gnus-set-mode-line 'summary)))
9579
9580 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9581   "Copy the current article to some other group.
9582 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9583 When called interactively, if TO-NEWSGROUP is nil, use the value of
9584 the variable `gnus-move-split-methods' for finding a default target
9585 newsgroup.
9586 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9587 re-spool using this method."
9588   (interactive "P")
9589   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9590
9591 (defun gnus-summary-crosspost-article (&optional n)
9592   "Crosspost the current article to some other group."
9593   (interactive "P")
9594   (gnus-summary-move-article n nil nil 'crosspost))
9595
9596 (defcustom gnus-summary-respool-default-method nil
9597   "Default method type for respooling an article.
9598 If nil, use to the current newsgroup method."
9599   :type 'symbol
9600   :group 'gnus-summary-mail)
9601
9602 (defcustom gnus-summary-display-while-building nil
9603   "If non-nil, show and update the summary buffer as it's being built.
9604 If the value is t, update the buffer after every line is inserted.  If
9605 the value is an integer (N), update the display every N lines."
9606   :version "22.1"
9607   :group 'gnus-thread
9608   :type '(choice (const :tag "off" nil)
9609                  number
9610                  (const :tag "frequently" t)))
9611
9612 (defun gnus-summary-respool-article (&optional n method)
9613   "Respool the current article.
9614 The article will be squeezed through the mail spooling process again,
9615 which means that it will be put in some mail newsgroup or other
9616 depending on `nnmail-split-methods'.
9617 If N is a positive number, respool the N next articles.
9618 If N is a negative number, respool the N previous articles.
9619 If N is nil and any articles have been marked with the process mark,
9620 respool those articles instead.
9621
9622 Respooling can be done both from mail groups and \"real\" newsgroups.
9623 In the former case, the articles in question will be moved from the
9624 current group into whatever groups they are destined to.  In the
9625 latter case, they will be copied into the relevant groups."
9626   (interactive
9627    (list current-prefix-arg
9628          (let* ((methods (gnus-methods-using 'respool))
9629                 (methname
9630                  (symbol-name (or gnus-summary-respool-default-method
9631                                   (car (gnus-find-method-for-group
9632                                         gnus-newsgroup-name)))))
9633                 (method
9634                  (gnus-completing-read-with-default
9635                   methname "Backend to use when respooling"
9636                   methods nil t nil 'gnus-mail-method-history))
9637                 ms)
9638            (cond
9639             ((zerop (length (setq ms (gnus-servers-using-backend
9640                                       (intern method)))))
9641              (list (intern method) ""))
9642             ((= 1 (length ms))
9643              (car ms))
9644             (t
9645              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9646                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9647                            ms-alist))))))))
9648   (unless method
9649     (error "No method given for respooling"))
9650   (if (assoc (symbol-name
9651               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9652              (gnus-methods-using 'respool))
9653       (gnus-summary-move-article n nil method)
9654     (gnus-summary-copy-article n nil method)))
9655
9656 (defun gnus-summary-import-article (file &optional edit)
9657   "Import an arbitrary file into a mail newsgroup."
9658   (interactive "fImport file: \nP")
9659   (let ((group gnus-newsgroup-name)
9660         (now (current-time))
9661         atts lines group-art)
9662     (unless (gnus-check-backend-function 'request-accept-article group)
9663       (error "%s does not support article importing" group))
9664     (or (file-readable-p file)
9665         (not (file-regular-p file))
9666         (error "Can't read %s" file))
9667     (save-excursion
9668       (set-buffer (gnus-get-buffer-create " *import file*"))
9669       (erase-buffer)
9670       (nnheader-insert-file-contents file)
9671       (goto-char (point-min))
9672       (if (nnheader-article-p)
9673           (save-restriction
9674             (goto-char (point-min))
9675             (search-forward "\n\n" nil t)
9676             (narrow-to-region (point-min) (1- (point)))
9677             (goto-char (point-min))
9678             (unless (re-search-forward "^date:" nil t)
9679               (goto-char (point-max))
9680               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9681        ;; This doesn't look like an article, so we fudge some headers.
9682         (setq atts (file-attributes file)
9683               lines (count-lines (point-min) (point-max)))
9684         (insert "From: " (read-string "From: ") "\n"
9685                 "Subject: " (read-string "Subject: ") "\n"
9686                 "Date: " (message-make-date (nth 5 atts)) "\n"
9687                 "Message-ID: " (message-make-message-id) "\n"
9688                 "Lines: " (int-to-string lines) "\n"
9689                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9690       (setq group-art (gnus-request-accept-article group nil t))
9691       (kill-buffer (current-buffer)))
9692     (setq gnus-newsgroup-active (gnus-activate-group group))
9693     (forward-line 1)
9694     (gnus-summary-goto-article (cdr group-art) nil t)
9695     (when edit
9696       (gnus-summary-edit-article))))
9697
9698 (defun gnus-summary-create-article ()
9699   "Create an article in a mail newsgroup."
9700   (interactive)
9701   (let ((group gnus-newsgroup-name)
9702         (now (current-time))
9703         group-art)
9704     (unless (gnus-check-backend-function 'request-accept-article group)
9705       (error "%s does not support article importing" group))
9706     (save-excursion
9707       (set-buffer (gnus-get-buffer-create " *import file*"))
9708       (erase-buffer)
9709       (goto-char (point-min))
9710       ;; This doesn't look like an article, so we fudge some headers.
9711       (insert "From: " (read-string "From: ") "\n"
9712               "Subject: " (read-string "Subject: ") "\n"
9713               "Date: " (message-make-date now) "\n"
9714               "Message-ID: " (message-make-message-id) "\n")
9715       (setq group-art (gnus-request-accept-article group nil t))
9716       (kill-buffer (current-buffer)))
9717     (setq gnus-newsgroup-active (gnus-activate-group group))
9718     (forward-line 1)
9719     (gnus-summary-goto-article (cdr group-art) nil t)
9720     (gnus-summary-edit-article)))
9721
9722 (defun gnus-summary-article-posted-p ()
9723   "Say whether the current (mail) article is available from news as well.
9724 This will be the case if the article has both been mailed and posted."
9725   (interactive)
9726   (let ((id (mail-header-references (gnus-summary-article-header)))
9727         (gnus-override-method (car (gnus-refer-article-methods))))
9728     (if (gnus-request-head id "")
9729         (gnus-message 2 "The current message was found on %s"
9730                       gnus-override-method)
9731       (gnus-message 2 "The current message couldn't be found on %s"
9732                     gnus-override-method)
9733       nil)))
9734
9735 (defun gnus-summary-expire-articles (&optional now)
9736   "Expire all articles that are marked as expirable in the current group."
9737   (interactive)
9738   (when (and (not gnus-group-is-exiting-without-update-p)
9739              (gnus-check-backend-function
9740               'request-expire-articles gnus-newsgroup-name))
9741     ;; This backend supports expiry.
9742     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9743            (expirable (if total
9744                           (progn
9745                             ;; We need to update the info for
9746                             ;; this group for `gnus-list-of-read-articles'
9747                             ;; to give us the right answer.
9748                             (gnus-run-hooks 'gnus-exit-group-hook)
9749                             (gnus-summary-update-info)
9750                             (gnus-list-of-read-articles gnus-newsgroup-name))
9751                         (setq gnus-newsgroup-expirable
9752                               (sort gnus-newsgroup-expirable '<))))
9753            (expiry-wait (if now 'immediate
9754                           (gnus-group-find-parameter
9755                            gnus-newsgroup-name 'expiry-wait)))
9756            (nnmail-expiry-target
9757             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9758                 nnmail-expiry-target))
9759            es)
9760       (when expirable
9761         ;; There are expirable articles in this group, so we run them
9762         ;; through the expiry process.
9763         (gnus-message 6 "Expiring articles...")
9764         (unless (gnus-check-group gnus-newsgroup-name)
9765           (error "Can't open server for %s" gnus-newsgroup-name))
9766         ;; The list of articles that weren't expired is returned.
9767         (save-excursion
9768           (if expiry-wait
9769               (let ((nnmail-expiry-wait-function nil)
9770                     (nnmail-expiry-wait expiry-wait))
9771                 (setq es (gnus-request-expire-articles
9772                           expirable gnus-newsgroup-name)))
9773             (setq es (gnus-request-expire-articles
9774                       expirable gnus-newsgroup-name)))
9775           (unless total
9776             (setq gnus-newsgroup-expirable es))
9777           ;; We go through the old list of expirable, and mark all
9778           ;; really expired articles as nonexistent.
9779           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9780             (let ((gnus-use-cache nil))
9781               (dolist (article expirable)
9782                 (when (and (not (memq article es))
9783                            (gnus-data-find article))
9784                   (gnus-summary-mark-article article gnus-canceled-mark)
9785                   (run-hook-with-args 'gnus-summary-article-expire-hook
9786                                       'delete
9787                                       (gnus-data-header
9788                                        (assoc article (gnus-data-list nil)))
9789                                       gnus-newsgroup-name
9790                                       nil
9791                                       nil))))))
9792         (gnus-message 6 "Expiring articles...done")))))
9793
9794 (defun gnus-summary-expire-articles-now ()
9795   "Expunge all expirable articles in the current group.
9796 This means that *all* articles that are marked as expirable will be
9797 deleted forever, right now."
9798   (interactive)
9799   (or gnus-expert-user
9800       (gnus-yes-or-no-p
9801        "Are you really, really sure you want to delete all expirable messages? ")
9802       (error "Phew!"))
9803   (gnus-summary-expire-articles t))
9804
9805 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9806 (defun gnus-summary-delete-article (&optional n)
9807   "Delete the N next (mail) articles.
9808 This command actually deletes articles.  This is not a marking
9809 command.  The article will disappear forever from your life, never to
9810 return.
9811
9812 If N is negative, delete backwards.
9813 If N is nil and articles have been marked with the process mark,
9814 delete these instead.
9815
9816 If `gnus-novice-user' is non-nil you will be asked for
9817 confirmation before the articles are deleted."
9818   (interactive "P")
9819   (unless (gnus-check-backend-function 'request-expire-articles
9820                                        gnus-newsgroup-name)
9821     (error "The current newsgroup does not support article deletion"))
9822   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9823     (error "Couldn't open server"))
9824   ;; Compute the list of articles to delete.
9825   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9826         (nnmail-expiry-target 'delete)
9827         not-deleted)
9828     (if (and gnus-novice-user
9829              (not (gnus-yes-or-no-p
9830                    (format "Do you really want to delete %s forever? "
9831                            (if (> (length articles) 1)
9832                                (format "these %s articles" (length articles))
9833                              "this article")))))
9834         ()
9835       ;; Delete the articles.
9836       (setq not-deleted (gnus-request-expire-articles
9837                          articles gnus-newsgroup-name 'force))
9838       (while articles
9839         (gnus-summary-remove-process-mark (car articles))
9840         ;; The backend might not have been able to delete the article
9841         ;; after all.
9842         (unless (memq (car articles) not-deleted)
9843           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9844         (let* ((article (car articles))
9845                (ghead  (gnus-data-header
9846                                     (assoc article (gnus-data-list nil)))))
9847           (run-hook-with-args 'gnus-summary-article-delete-hook
9848                               'delete ghead gnus-newsgroup-name nil
9849                               nil))
9850         (setq articles (cdr articles)))
9851       (when not-deleted
9852         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9853     (gnus-summary-position-point)
9854     (gnus-set-mode-line 'summary)
9855     not-deleted))
9856
9857 (defun gnus-summary-edit-article (&optional arg)
9858   "Edit the current article.
9859 This will have permanent effect only in mail groups.
9860 If ARG is nil, edit the decoded articles.
9861 If ARG is 1, edit the raw articles.
9862 If ARG is 2, edit the raw articles even in read-only groups.
9863 If ARG is 3, edit the articles with the current handles.
9864 Otherwise, allow editing of articles even in read-only
9865 groups."
9866   (interactive "P")
9867   (let (force raw current-handles)
9868     (cond
9869      ((null arg))
9870      ((eq arg 1)
9871       (setq raw t))
9872      ((eq arg 2)
9873       (setq raw t
9874             force t))
9875      ((eq arg 3)
9876       (setq current-handles
9877             (and (gnus-buffer-live-p gnus-article-buffer)
9878                  (with-current-buffer gnus-article-buffer
9879                    (prog1
9880                        gnus-article-mime-handles
9881                      (setq gnus-article-mime-handles nil))))))
9882      (t
9883       (setq force t)))
9884     (when (and raw (not force)
9885                (member gnus-newsgroup-name '("nndraft:delayed"
9886                                              "nndraft:drafts"
9887                                              "nndraft:queue")))
9888       (error "Can't edit the raw article in group %s"
9889              gnus-newsgroup-name))
9890     (save-excursion
9891       (set-buffer gnus-summary-buffer)
9892       (let ((mail-parse-charset gnus-newsgroup-charset)
9893             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9894         (gnus-set-global-variables)
9895         (when (and (not force)
9896                    (gnus-group-read-only-p))
9897           (error "The current newsgroup does not support article editing"))
9898         (gnus-summary-show-article t)
9899         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9900           (with-current-buffer gnus-article-buffer
9901             (mm-enable-multibyte)))
9902         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9903             (setq raw t))
9904         (gnus-article-edit-article
9905          (if raw 'ignore
9906            `(lambda ()
9907               (let ((mbl mml-buffer-list))
9908                 (setq mml-buffer-list nil)
9909                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9910                   (mime-to-mml ,'current-handles))
9911                 (let ((mbl1 mml-buffer-list))
9912                   (setq mml-buffer-list mbl)
9913                   (set (make-local-variable 'mml-buffer-list) mbl1))
9914                 (gnus-make-local-hook 'kill-buffer-hook)
9915                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9916          `(lambda (no-highlight)
9917             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9918                   (message-options message-options)
9919                   (message-options-set-recipient)
9920                   (mail-parse-ignored-charsets
9921                    ',gnus-newsgroup-ignored-charsets))
9922               ,(if (not raw) '(progn
9923                                 (mml-to-mime)
9924                                 (mml-destroy-buffers)
9925                                 (remove-hook 'kill-buffer-hook
9926                                              'mml-destroy-buffers t)
9927                                 (kill-local-variable 'mml-buffer-list)))
9928               (gnus-summary-edit-article-done
9929                ,(or (mail-header-references gnus-current-headers) "")
9930                ,(gnus-group-read-only-p)
9931                ,gnus-summary-buffer no-highlight))))))))
9932
9933 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9934
9935 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9936                                                  no-highlight)
9937   "Make edits to the current article permanent."
9938   (interactive)
9939   (save-excursion
9940    ;; The buffer restriction contains the entire article if it exists.
9941     (when (article-goto-body)
9942       (let ((lines (count-lines (point) (point-max)))
9943             (length (- (point-max) (point)))
9944             (case-fold-search t)
9945             (body (copy-marker (point))))
9946         (goto-char (point-min))
9947         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9948           (delete-region (match-beginning 1) (match-end 1))
9949           (insert (number-to-string length)))
9950         (goto-char (point-min))
9951         (when (re-search-forward
9952                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9953           (delete-region (match-beginning 1) (match-end 1))
9954           (insert (number-to-string length)))
9955         (goto-char (point-min))
9956         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9957           (delete-region (match-beginning 1) (match-end 1))
9958           (insert (number-to-string lines))))))
9959   ;; Replace the article.
9960   (let ((buf (current-buffer)))
9961     (with-temp-buffer
9962       (insert-buffer-substring buf)
9963
9964       (if (and (not read-only)
9965                (not (gnus-request-replace-article
9966                      (cdr gnus-article-current) (car gnus-article-current)
9967                      (current-buffer) t)))
9968           (error "Couldn't replace article")
9969         ;; Update the summary buffer.
9970         (if (and references
9971                  (equal (message-tokenize-header references " ")
9972                         (message-tokenize-header
9973                          (or (message-fetch-field "references") "") " ")))
9974             ;; We only have to update this line.
9975             (save-excursion
9976               (save-restriction
9977                 (message-narrow-to-head)
9978                 (let ((head (buffer-substring-no-properties
9979                              (point-min) (point-max)))
9980                       header)
9981                   (with-temp-buffer
9982                     (insert (format "211 %d Article retrieved.\n"
9983                                     (cdr gnus-article-current)))
9984                     (insert head)
9985                     (insert ".\n")
9986                     (let ((nntp-server-buffer (current-buffer)))
9987                       (setq header (car (gnus-get-newsgroup-headers
9988                                          nil t))))
9989                     (save-excursion
9990                       (set-buffer gnus-summary-buffer)
9991                       (gnus-data-set-header
9992                        (gnus-data-find (cdr gnus-article-current))
9993                        header)
9994                       (gnus-summary-update-article-line
9995                        (cdr gnus-article-current) header)
9996                       (if (gnus-summary-goto-subject
9997                            (cdr gnus-article-current) nil t)
9998                           (gnus-summary-update-secondary-mark
9999                            (cdr gnus-article-current))))))))
10000           ;; Update threads.
10001           (set-buffer (or buffer gnus-summary-buffer))
10002           (gnus-summary-update-article (cdr gnus-article-current))
10003           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10004               (gnus-summary-update-secondary-mark
10005                (cdr gnus-article-current))))
10006         ;; Prettify the article buffer again.
10007         (unless no-highlight
10008           (save-excursion
10009             (set-buffer gnus-article-buffer)
10010             ;;;!!! Fix this -- article should be rehighlighted.
10011             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10012             (set-buffer gnus-original-article-buffer)
10013             (gnus-request-article
10014              (cdr gnus-article-current)
10015              (car gnus-article-current) (current-buffer))))
10016         ;; Prettify the summary buffer line.
10017         (when (gnus-visual-p 'summary-highlight 'highlight)
10018           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10019
10020 (defun gnus-summary-edit-wash (key)
10021   "Perform editing command KEY in the article buffer."
10022   (interactive
10023    (list
10024     (progn
10025       (message "%s" (concat (this-command-keys) "- "))
10026       (read-char))))
10027   (message "")
10028   (gnus-summary-edit-article)
10029   (execute-kbd-macro (concat (this-command-keys) key))
10030   (gnus-article-edit-done))
10031
10032 ;;; Respooling
10033
10034 (defun gnus-summary-respool-query (&optional silent trace)
10035   "Query where the respool algorithm would put this article."
10036   (interactive)
10037   (let (gnus-mark-article-hook)
10038     (gnus-summary-select-article)
10039     (save-excursion
10040       (set-buffer gnus-original-article-buffer)
10041       (let ((groups (nnmail-article-group 'identity trace)))
10042         (unless silent
10043           (if groups
10044               (message "This message would go to %s"
10045                        (mapconcat 'car groups ", "))
10046             (message "This message would go to no groups"))
10047           groups)))))
10048
10049 (defun gnus-summary-respool-trace ()
10050   "Trace where the respool algorithm would put this article.
10051 Display a buffer showing all fancy splitting patterns which matched."
10052   (interactive)
10053   (gnus-summary-respool-query nil t))
10054
10055 ;; Summary marking commands.
10056
10057 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10058   "Mark articles which has the same subject as read, and then select the next.
10059 If UNMARK is positive, remove any kind of mark.
10060 If UNMARK is negative, tick articles."
10061   (interactive "P")
10062   (when unmark
10063     (setq unmark (prefix-numeric-value unmark)))
10064   (let ((count
10065          (gnus-summary-mark-same-subject
10066           (gnus-summary-article-subject) unmark)))
10067     ;; Select next unread article.  If auto-select-same mode, should
10068     ;; select the first unread article.
10069     (gnus-summary-next-article t (and gnus-auto-select-same
10070                                       (gnus-summary-article-subject)))
10071     (gnus-message 7 "%d article%s marked as %s"
10072                   count (if (= count 1) " is" "s are")
10073                   (if unmark "unread" "read"))))
10074
10075 (defun gnus-summary-kill-same-subject (&optional unmark)
10076   "Mark articles which has the same subject as read.
10077 If UNMARK is positive, remove any kind of mark.
10078 If UNMARK is negative, tick articles."
10079   (interactive "P")
10080   (when unmark
10081     (setq unmark (prefix-numeric-value unmark)))
10082   (let ((count
10083          (gnus-summary-mark-same-subject
10084           (gnus-summary-article-subject) unmark)))
10085     ;; If marked as read, go to next unread subject.
10086     (when (null unmark)
10087       ;; Go to next unread subject.
10088       (gnus-summary-next-subject 1 t))
10089     (gnus-message 7 "%d articles are marked as %s"
10090                   count (if unmark "unread" "read"))))
10091
10092 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10093   "Mark articles with same SUBJECT as read, and return marked number.
10094 If optional argument UNMARK is positive, remove any kinds of marks.
10095 If optional argument UNMARK is negative, mark articles as unread instead."
10096   (let ((count 1))
10097     (save-excursion
10098       (cond
10099        ((null unmark)                   ; Mark as read.
10100         (while (and
10101                 (progn
10102                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10103                   (gnus-summary-show-thread) t)
10104                 (gnus-summary-find-subject subject))
10105           (setq count (1+ count))))
10106        ((> unmark 0)                    ; Tick.
10107         (while (and
10108                 (progn
10109                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10110                   (gnus-summary-show-thread) t)
10111                 (gnus-summary-find-subject subject))
10112           (setq count (1+ count))))
10113        (t                               ; Mark as unread.
10114         (while (and
10115                 (progn
10116                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10117                   (gnus-summary-show-thread) t)
10118                 (gnus-summary-find-subject subject))
10119           (setq count (1+ count)))))
10120       (gnus-set-mode-line 'summary)
10121       ;; Return the number of marked articles.
10122       count)))
10123
10124 (defun gnus-summary-mark-as-processable (n &optional unmark)
10125   "Set the process mark on the next N articles.
10126 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10127 the process mark instead.  The difference between N and the actual
10128 number of articles marked is returned."
10129   (interactive "P")
10130   (if (and (null n) (gnus-region-active-p))
10131       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10132     (setq n (prefix-numeric-value n))
10133     (let ((backward (< n 0))
10134           (n (abs n)))
10135       (while (and
10136               (> n 0)
10137               (if unmark
10138                   (gnus-summary-remove-process-mark
10139                    (gnus-summary-article-number))
10140                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10141               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10142         (setq n (1- n)))
10143       (when (/= 0 n)
10144         (gnus-message 7 "No more articles"))
10145       (gnus-summary-recenter)
10146       (gnus-summary-position-point)
10147       n)))
10148
10149 (defun gnus-summary-unmark-as-processable (n)
10150   "Remove the process mark from the next N articles.
10151 If N is negative, unmark backward instead.  The difference between N and
10152 the actual number of articles unmarked is returned."
10153   (interactive "P")
10154   (gnus-summary-mark-as-processable n t))
10155
10156 (defun gnus-summary-unmark-all-processable ()
10157   "Remove the process mark from all articles."
10158   (interactive)
10159   (save-excursion
10160     (while gnus-newsgroup-processable
10161       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10162   (gnus-summary-position-point))
10163
10164 (defun gnus-summary-add-mark (article type)
10165   "Mark ARTICLE with a mark of TYPE."
10166   (let ((vtype (car (assq type gnus-article-mark-lists)))
10167         var)
10168     (if (not vtype)
10169         (error "No such mark type: %s" type)
10170       (setq var (intern (format "gnus-newsgroup-%s" type)))
10171       (set var (cons article (symbol-value var)))
10172       (if (memq type '(processable cached replied forwarded recent saved))
10173           (gnus-summary-update-secondary-mark article)
10174         ;;; !!! This is bogus.  We should find out what primary
10175         ;;; !!! mark we want to set.
10176         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10177
10178 (defun gnus-summary-mark-as-expirable (n)
10179   "Mark N articles forward as expirable.
10180 If N is negative, mark backward instead.  The difference between N and
10181 the actual number of articles marked is returned."
10182   (interactive "p")
10183   (gnus-summary-mark-forward n gnus-expirable-mark))
10184
10185 (defun gnus-summary-mark-as-spam (n)
10186   "Mark N articles forward as spam.
10187 If N is negative, mark backward instead.  The difference between N and
10188 the actual number of articles marked is returned."
10189   (interactive "p")
10190   (gnus-summary-mark-forward n gnus-spam-mark))
10191
10192 (defun gnus-summary-mark-article-as-replied (article)
10193   "Mark ARTICLE as replied to and update the summary line.
10194 ARTICLE can also be a list of articles."
10195   (interactive (list (gnus-summary-article-number)))
10196   (let ((articles (if (listp article) article (list article))))
10197     (dolist (article articles)
10198       (unless (numberp article)
10199         (error "%s is not a number" article))
10200       (push article gnus-newsgroup-replied)
10201       (let ((buffer-read-only nil))
10202         (when (gnus-summary-goto-subject article nil t)
10203           (gnus-summary-update-secondary-mark article))))))
10204
10205 (defun gnus-summary-mark-article-as-forwarded (article)
10206   "Mark ARTICLE as forwarded and update the summary line.
10207 ARTICLE can also be a list of articles."
10208   (let ((articles (if (listp article) article (list article))))
10209     (dolist (article articles)
10210       (push article gnus-newsgroup-forwarded)
10211       (let ((buffer-read-only nil))
10212         (when (gnus-summary-goto-subject article nil t)
10213           (gnus-summary-update-secondary-mark article))))))
10214
10215 (defun gnus-summary-set-bookmark (article)
10216   "Set a bookmark in current article."
10217   (interactive (list (gnus-summary-article-number)))
10218   (when (or (not (get-buffer gnus-article-buffer))
10219             (not gnus-current-article)
10220             (not gnus-article-current)
10221             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10222     (error "No current article selected"))
10223   ;; Remove old bookmark, if one exists.
10224   (gnus-pull article gnus-newsgroup-bookmarks)
10225   ;; Set the new bookmark, which is on the form
10226   ;; (article-number . line-number-in-body).
10227   (push
10228    (cons article
10229          (with-current-buffer gnus-article-buffer
10230            (count-lines
10231             (min (point)
10232                  (save-excursion
10233                    (article-goto-body)
10234                    (point)))
10235             (point))))
10236    gnus-newsgroup-bookmarks)
10237   (gnus-message 6 "A bookmark has been added to the current article."))
10238
10239 (defun gnus-summary-remove-bookmark (article)
10240   "Remove the bookmark from the current article."
10241   (interactive (list (gnus-summary-article-number)))
10242   ;; Remove old bookmark, if one exists.
10243   (if (not (assq article gnus-newsgroup-bookmarks))
10244       (gnus-message 6 "No bookmark in current article.")
10245     (gnus-pull article gnus-newsgroup-bookmarks)
10246     (gnus-message 6 "Removed bookmark.")))
10247
10248 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10249 (defun gnus-summary-mark-as-dormant (n)
10250   "Mark N articles forward as dormant.
10251 If N is negative, mark backward instead.  The difference between N and
10252 the actual number of articles marked is returned."
10253   (interactive "p")
10254   (gnus-summary-mark-forward n gnus-dormant-mark))
10255
10256 (defun gnus-summary-set-process-mark (article)
10257   "Set the process mark on ARTICLE and update the summary line."
10258   (setq gnus-newsgroup-processable
10259         (cons article
10260               (delq article gnus-newsgroup-processable)))
10261   (when (gnus-summary-goto-subject article)
10262     (gnus-summary-show-thread)
10263     (gnus-summary-goto-subject article)
10264     (gnus-summary-update-secondary-mark article)))
10265
10266 (defun gnus-summary-remove-process-mark (&rest articles)
10267   "Remove the process mark from ARTICLES and update the summary line."
10268   (dolist (article articles)
10269     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10270     (when (gnus-summary-goto-subject article)
10271       (gnus-summary-show-thread)
10272       (gnus-summary-goto-subject article)
10273       (gnus-summary-update-secondary-mark article)))
10274   t)
10275
10276 (defun gnus-summary-set-saved-mark (article)
10277   "Set the process mark on ARTICLE and update the summary line."
10278   (push article gnus-newsgroup-saved)
10279   (when (gnus-summary-goto-subject article)
10280     (gnus-summary-update-secondary-mark article)))
10281
10282 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10283   "Mark N articles as read forwards.
10284 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10285 The difference between N and the actual number of articles marked is
10286 returned.
10287 If NO-EXPIRE, auto-expiry will be inhibited."
10288   (interactive "p")
10289   (gnus-summary-show-thread)
10290   (let ((backward (< n 0))
10291         (gnus-summary-goto-unread
10292          (and gnus-summary-goto-unread
10293               (not (eq gnus-summary-goto-unread 'never))
10294               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10295                                     gnus-ticked-mark gnus-dormant-mark)))))
10296         (n (abs n))
10297         (mark (or mark gnus-del-mark)))
10298     (while (and (> n 0)
10299                 (gnus-summary-mark-article nil mark no-expire)
10300                 (zerop (gnus-summary-next-subject
10301                         (if backward -1 1)
10302                         (and gnus-summary-goto-unread
10303                              (not (eq gnus-summary-goto-unread 'never)))
10304                         t)))
10305       (setq n (1- n)))
10306     (when (/= 0 n)
10307       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10308     (gnus-summary-recenter)
10309     (gnus-summary-position-point)
10310     (gnus-set-mode-line 'summary)
10311     n))
10312
10313 (defun gnus-summary-mark-article-as-read (mark)
10314   "Mark the current article quickly as read with MARK."
10315   (let ((article (gnus-summary-article-number)))
10316     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10317     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10318     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10319     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10320     (push (cons article mark) gnus-newsgroup-reads)
10321     ;; Possibly remove from cache, if that is used.
10322     (when gnus-use-cache
10323       (gnus-cache-enter-remove-article article))
10324     ;; Allow the backend to change the mark.
10325     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10326     ;; Check for auto-expiry.
10327     (when (and gnus-newsgroup-auto-expire
10328                (memq mark gnus-auto-expirable-marks))
10329       (setq mark gnus-expirable-mark)
10330       ;; Let the backend know about the mark change.
10331       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10332       (push article gnus-newsgroup-expirable))
10333     ;; Set the mark in the buffer.
10334     (gnus-summary-update-mark mark 'unread)
10335     t))
10336
10337 (defun gnus-summary-mark-article-as-unread (mark)
10338   "Mark the current article quickly as unread with MARK."
10339   (let* ((article (gnus-summary-article-number))
10340          (old-mark (gnus-summary-article-mark article)))
10341     ;; Allow the backend to change the mark.
10342     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10343     (if (eq mark old-mark)
10344         t
10345       (if (<= article 0)
10346           (progn
10347             (gnus-error 1 "Can't mark negative article numbers")
10348             nil)
10349         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10350         (setq gnus-newsgroup-spam-marked
10351               (delq article gnus-newsgroup-spam-marked))
10352         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10353         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10354         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10355         (cond ((= mark gnus-ticked-mark)
10356                (setq gnus-newsgroup-marked
10357                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10358                                               article)))
10359               ((= mark gnus-spam-mark)
10360                (setq gnus-newsgroup-spam-marked
10361                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10362                                               article)))
10363               ((= mark gnus-dormant-mark)
10364                (setq gnus-newsgroup-dormant
10365                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10366                                               article)))
10367               (t
10368                (setq gnus-newsgroup-unreads
10369                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10370                                               article))))
10371         (gnus-pull article gnus-newsgroup-reads)
10372
10373         ;; See whether the article is to be put in the cache.
10374         (and gnus-use-cache
10375              (vectorp (gnus-summary-article-header article))
10376              (save-excursion
10377                (gnus-cache-possibly-enter-article
10378                 gnus-newsgroup-name article
10379                 (= mark gnus-ticked-mark)
10380                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10381
10382         ;; Fix the mark.
10383         (gnus-summary-update-mark mark 'unread)
10384         t))))
10385
10386 (defun gnus-summary-mark-article (&optional article mark no-expire)
10387   "Mark ARTICLE with MARK.  MARK can be any character.
10388 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10389 `??' (dormant) and `?E' (expirable).
10390 If MARK is nil, then the default character `?r' is used.
10391 If ARTICLE is nil, then the article on the current line will be
10392 marked.
10393 If NO-EXPIRE, auto-expiry will be inhibited."
10394   ;; The mark might be a string.
10395   (when (stringp mark)
10396     (setq mark (aref mark 0)))
10397   ;; If no mark is given, then we check auto-expiring.
10398   (when (null mark)
10399     (setq mark gnus-del-mark))
10400   (when (and (not no-expire)
10401              gnus-newsgroup-auto-expire
10402              (memq mark gnus-auto-expirable-marks))
10403     (setq mark gnus-expirable-mark))
10404   (let ((article (or article (gnus-summary-article-number)))
10405         (old-mark (gnus-summary-article-mark article)))
10406     ;; Allow the backend to change the mark.
10407     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10408     (if (eq mark old-mark)
10409         t
10410       (unless article
10411         (error "No article on current line"))
10412       (if (not (if (or (= mark gnus-unread-mark)
10413                        (= mark gnus-ticked-mark)
10414                        (= mark gnus-spam-mark)
10415                        (= mark gnus-dormant-mark))
10416                    (gnus-mark-article-as-unread article mark)
10417                  (gnus-mark-article-as-read article mark)))
10418           t
10419         ;; See whether the article is to be put in the cache.
10420         (and gnus-use-cache
10421              (not (= mark gnus-canceled-mark))
10422              (vectorp (gnus-summary-article-header article))
10423              (save-excursion
10424                (gnus-cache-possibly-enter-article
10425                 gnus-newsgroup-name article
10426                 (= mark gnus-ticked-mark)
10427                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10428
10429         (when (gnus-summary-goto-subject article nil t)
10430           (let ((buffer-read-only nil))
10431             (gnus-summary-show-thread)
10432             ;; Fix the mark.
10433             (gnus-summary-update-mark mark 'unread)
10434             t))))))
10435
10436 (defun gnus-summary-update-secondary-mark (article)
10437   "Update the secondary (read, process, cache) mark."
10438   (gnus-summary-update-mark
10439    (cond ((memq article gnus-newsgroup-processable)
10440           gnus-process-mark)
10441          ((memq article gnus-newsgroup-cached)
10442           gnus-cached-mark)
10443          ((memq article gnus-newsgroup-replied)
10444           gnus-replied-mark)
10445          ((memq article gnus-newsgroup-forwarded)
10446           gnus-forwarded-mark)
10447          ((memq article gnus-newsgroup-saved)
10448           gnus-saved-mark)
10449          ((memq article gnus-newsgroup-recent)
10450           gnus-recent-mark)
10451          ((memq article gnus-newsgroup-unseen)
10452           gnus-unseen-mark)
10453          (t gnus-no-mark))
10454    'replied)
10455   (when (gnus-visual-p 'summary-highlight 'highlight)
10456     (gnus-run-hooks 'gnus-summary-update-hook))
10457   t)
10458
10459 (defun gnus-summary-update-download-mark (article)
10460   "Update the download mark."
10461   (gnus-summary-update-mark
10462    (cond ((memq article gnus-newsgroup-undownloaded)
10463           gnus-undownloaded-mark)
10464          (gnus-newsgroup-agentized
10465           gnus-downloaded-mark)
10466          (t
10467           gnus-no-mark))
10468    'download)
10469   (gnus-summary-update-line t)
10470   t)
10471
10472 (defun gnus-summary-update-mark (mark type)
10473   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10474         (buffer-read-only nil))
10475     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10476     (when forward
10477       (when (looking-at "\r")
10478         (incf forward))
10479       (when (<= (+ forward (point)) (point-max))
10480         ;; Go to the right position on the line.
10481         (goto-char (+ forward (point)))
10482         ;; Replace the old mark with the new mark.
10483         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10484         ;; Optionally update the marks by some user rule.
10485         (when (eq type 'unread)
10486           (gnus-data-set-mark
10487            (gnus-data-find (gnus-summary-article-number)) mark)
10488           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10489
10490 (defun gnus-mark-article-as-read (article &optional mark)
10491   "Enter ARTICLE in the pertinent lists and remove it from others."
10492   ;; Make the article expirable.
10493   (let ((mark (or mark gnus-del-mark)))
10494     (setq gnus-newsgroup-expirable
10495           (if (= mark gnus-expirable-mark)
10496               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10497             (delq article gnus-newsgroup-expirable)))
10498     ;; Remove from unread and marked lists.
10499     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10500     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10501     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10502     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10503     (push (cons article mark) gnus-newsgroup-reads)
10504     ;; Possibly remove from cache, if that is used.
10505     (when gnus-use-cache
10506       (gnus-cache-enter-remove-article article))
10507     t))
10508
10509 (defun gnus-mark-article-as-unread (article &optional mark)
10510   "Enter ARTICLE in the pertinent lists and remove it from others."
10511   (let ((mark (or mark gnus-ticked-mark)))
10512     (if (<= article 0)
10513         (progn
10514           (gnus-error 1 "Can't mark negative article numbers")
10515           nil)
10516       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10517             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10518             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10519             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10520             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10521
10522       ;; Unsuppress duplicates?
10523       (when gnus-suppress-duplicates
10524         (gnus-dup-unsuppress-article article))
10525
10526       (cond ((= mark gnus-ticked-mark)
10527              (setq gnus-newsgroup-marked
10528                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10529             ((= mark gnus-spam-mark)
10530              (setq gnus-newsgroup-spam-marked
10531                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10532                                             article)))
10533             ((= mark gnus-dormant-mark)
10534              (setq gnus-newsgroup-dormant
10535                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10536             (t
10537              (setq gnus-newsgroup-unreads
10538                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10539       (gnus-pull article gnus-newsgroup-reads)
10540       t)))
10541
10542 (defalias 'gnus-summary-mark-as-unread-forward
10543   'gnus-summary-tick-article-forward)
10544 (make-obsolete 'gnus-summary-mark-as-unread-forward
10545                'gnus-summary-tick-article-forward)
10546 (defun gnus-summary-tick-article-forward (n)
10547   "Tick N articles forwards.
10548 If N is negative, tick backwards instead.
10549 The difference between N and the number of articles ticked is returned."
10550   (interactive "p")
10551   (gnus-summary-mark-forward n gnus-ticked-mark))
10552
10553 (defalias 'gnus-summary-mark-as-unread-backward
10554   'gnus-summary-tick-article-backward)
10555 (make-obsolete 'gnus-summary-mark-as-unread-backward
10556                'gnus-summary-tick-article-backward)
10557 (defun gnus-summary-tick-article-backward (n)
10558   "Tick N articles backwards.
10559 The difference between N and the number of articles ticked is returned."
10560   (interactive "p")
10561   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10562
10563 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10564 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10565 (defun gnus-summary-tick-article (&optional article clear-mark)
10566   "Mark current article as unread.
10567 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10568 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10569   (interactive)
10570   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10571                                        gnus-ticked-mark)))
10572
10573 (defun gnus-summary-mark-as-read-forward (n)
10574   "Mark N articles as read forwards.
10575 If N is negative, mark backwards instead.
10576 The difference between N and the actual number of articles marked is
10577 returned."
10578   (interactive "p")
10579   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10580
10581 (defun gnus-summary-mark-as-read-backward (n)
10582   "Mark the N articles as read backwards.
10583 The difference between N and the actual number of articles marked is
10584 returned."
10585   (interactive "p")
10586   (gnus-summary-mark-forward
10587    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10588
10589 (defun gnus-summary-mark-as-read (&optional article mark)
10590   "Mark current article as read.
10591 ARTICLE specifies the article to be marked as read.
10592 MARK specifies a string to be inserted at the beginning of the line."
10593   (gnus-summary-mark-article article mark))
10594
10595 (defun gnus-summary-clear-mark-forward (n)
10596   "Clear marks from N articles forward.
10597 If N is negative, clear backward instead.
10598 The difference between N and the number of marks cleared is returned."
10599   (interactive "p")
10600   (gnus-summary-mark-forward n gnus-unread-mark))
10601
10602 (defun gnus-summary-clear-mark-backward (n)
10603   "Clear marks from N articles backward.
10604 The difference between N and the number of marks cleared is returned."
10605   (interactive "p")
10606   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10607
10608 (defun gnus-summary-mark-unread-as-read ()
10609   "Intended to be used by `gnus-summary-mark-article-hook'."
10610   (when (memq gnus-current-article gnus-newsgroup-unreads)
10611     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10612
10613 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10614   "Intended to be used by `gnus-summary-mark-article-hook'."
10615   (let ((mark (gnus-summary-article-mark)))
10616     (when (or (gnus-unread-mark-p mark)
10617               (gnus-read-mark-p mark))
10618       (gnus-summary-mark-article gnus-current-article
10619                                  (or new-mark gnus-read-mark)))))
10620
10621 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10622   "Intended to be used by `gnus-summary-mark-article-hook'."
10623   (let ((mark (gnus-summary-article-mark)))
10624     (when (or (gnus-unread-mark-p mark)
10625               (gnus-read-mark-p mark))
10626       (gnus-summary-mark-article (gnus-summary-article-number)
10627                                  (or new-mark gnus-read-mark)))))
10628
10629 (defun gnus-summary-mark-unread-as-ticked ()
10630   "Intended to be used by `gnus-summary-mark-article-hook'."
10631   (when (memq gnus-current-article gnus-newsgroup-unreads)
10632     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10633
10634 (defun gnus-summary-mark-region-as-read (point mark all)
10635   "Mark all unread articles between point and mark as read.
10636 If given a prefix, mark all articles between point and mark as read,
10637 even ticked and dormant ones."
10638   (interactive "r\nP")
10639   (save-excursion
10640     (let (article)
10641       (goto-char point)
10642       (beginning-of-line)
10643       (while (and
10644               (< (point) mark)
10645               (progn
10646                 (when (or all
10647                           (memq (setq article (gnus-summary-article-number))
10648                                 gnus-newsgroup-unreads))
10649                   (gnus-summary-mark-article article gnus-del-mark))
10650                 t)
10651               (gnus-summary-find-next))))))
10652
10653 (defun gnus-summary-mark-below (score mark)
10654   "Mark articles with score less than SCORE with MARK."
10655   (interactive "P\ncMark: ")
10656   (setq score (if score
10657                   (prefix-numeric-value score)
10658                 (or gnus-summary-default-score 0)))
10659   (save-excursion
10660     (set-buffer gnus-summary-buffer)
10661     (goto-char (point-min))
10662     (while
10663         (progn
10664           (and (< (gnus-summary-article-score) score)
10665                (gnus-summary-mark-article nil mark))
10666           (gnus-summary-find-next)))))
10667
10668 (defun gnus-summary-kill-below (&optional score)
10669   "Mark articles with score below SCORE as read."
10670   (interactive "P")
10671   (gnus-summary-mark-below score gnus-killed-mark))
10672
10673 (defun gnus-summary-clear-above (&optional score)
10674   "Clear all marks from articles with score above SCORE."
10675   (interactive "P")
10676   (gnus-summary-mark-above score gnus-unread-mark))
10677
10678 (defun gnus-summary-tick-above (&optional score)
10679   "Tick all articles with score above SCORE."
10680   (interactive "P")
10681   (gnus-summary-mark-above score gnus-ticked-mark))
10682
10683 (defun gnus-summary-mark-above (score mark)
10684   "Mark articles with score over SCORE with MARK."
10685   (interactive "P\ncMark: ")
10686   (setq score (if score
10687                   (prefix-numeric-value score)
10688                 (or gnus-summary-default-score 0)))
10689   (save-excursion
10690     (set-buffer gnus-summary-buffer)
10691     (goto-char (point-min))
10692     (while (and (progn
10693                   (when (> (gnus-summary-article-score) score)
10694                     (gnus-summary-mark-article nil mark))
10695                   t)
10696                 (gnus-summary-find-next)))))
10697
10698 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10699 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10700 (defun gnus-summary-limit-include-expunged (&optional no-error)
10701   "Display all the hidden articles that were expunged for low scores."
10702   (interactive)
10703   (let ((buffer-read-only nil))
10704     (let ((scored gnus-newsgroup-scored)
10705           headers h)
10706       (while scored
10707         (unless (gnus-summary-article-header (caar scored))
10708           (and (setq h (gnus-number-to-header (caar scored)))
10709                (< (cdar scored) gnus-summary-expunge-below)
10710                (push h headers)))
10711         (setq scored (cdr scored)))
10712       (if (not headers)
10713           (when (not no-error)
10714             (error "No expunged articles hidden"))
10715         (goto-char (point-min))
10716         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10717         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10718         (mapcar (lambda (x) (push (mail-header-number x)
10719                                   gnus-newsgroup-limit))
10720                 headers)
10721         (gnus-summary-prepare-unthreaded (nreverse headers))
10722         (goto-char (point-min))
10723         (gnus-summary-position-point)
10724         t))))
10725
10726 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10727   "Mark all unread articles in this newsgroup as read.
10728 If prefix argument ALL is non-nil, ticked and dormant articles will
10729 also be marked as read.
10730 If QUIETLY is non-nil, no questions will be asked.
10731
10732 If TO-HERE is non-nil, it should be a point in the buffer.  All
10733 articles before (after, if REVERSE is set) this point will be marked
10734 as read.
10735
10736 Note that this function will only catch up the unread article
10737 in the current summary buffer limitation.
10738
10739 The number of articles marked as read is returned."
10740   (interactive "P")
10741   (prog1
10742       (save-excursion
10743         (when (or quietly
10744                   (not gnus-interactive-catchup) ;Without confirmation?
10745                   gnus-expert-user
10746                   (gnus-y-or-n-p
10747                    (if all
10748                        "Mark absolutely all articles as read? "
10749                      "Mark all unread articles as read? ")))
10750           (if (and not-mark
10751                    (not gnus-newsgroup-adaptive)
10752                    (not gnus-newsgroup-auto-expire)
10753                    (not gnus-suppress-duplicates)
10754                    (or (not gnus-use-cache)
10755                        (eq gnus-use-cache 'passive)))
10756               (progn
10757                 (when all
10758                   (setq gnus-newsgroup-marked nil
10759                         gnus-newsgroup-spam-marked nil
10760                         gnus-newsgroup-dormant nil))
10761                 (setq gnus-newsgroup-unreads
10762                       (gnus-sorted-nunion
10763                        (gnus-sorted-intersection gnus-newsgroup-unreads
10764                                                  gnus-newsgroup-downloadable)
10765                        gnus-newsgroup-unfetched)))
10766             ;; We actually mark all articles as canceled, which we
10767             ;; have to do when using auto-expiry or adaptive scoring.
10768             (gnus-summary-show-all-threads)
10769             (if (and to-here reverse)
10770                 (progn
10771                   (goto-char to-here)
10772                   (gnus-summary-mark-current-read-and-unread-as-read
10773                    gnus-catchup-mark)
10774                   (while (gnus-summary-find-next (not all))
10775                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10776               (when (gnus-summary-first-subject (not all))
10777                 (while (and
10778                         (if to-here (< (point) to-here) t)
10779                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10780                         (gnus-summary-find-next (not all))))))
10781             (gnus-set-mode-line 'summary))
10782           t))
10783     (gnus-summary-position-point)))
10784
10785 (defun gnus-summary-catchup-to-here (&optional all)
10786   "Mark all unticked articles before the current one as read.
10787 If ALL is non-nil, also mark ticked and dormant articles as read."
10788   (interactive "P")
10789   (save-excursion
10790     (gnus-save-hidden-threads
10791       (let ((beg (point)))
10792         ;; We check that there are unread articles.
10793         (when (or all (gnus-summary-find-prev))
10794           (gnus-summary-catchup all t beg)))))
10795   (gnus-summary-position-point))
10796
10797 (defun gnus-summary-catchup-from-here (&optional all)
10798   "Mark all unticked articles after (and including) the current one as read.
10799 If ALL is non-nil, also mark ticked and dormant articles as read."
10800   (interactive "P")
10801   (save-excursion
10802     (gnus-save-hidden-threads
10803       (let ((beg (point)))
10804         ;; We check that there are unread articles.
10805         (when (or all (gnus-summary-find-next))
10806           (gnus-summary-catchup all t beg nil t)))))
10807   (gnus-summary-position-point))
10808
10809 (defun gnus-summary-catchup-all (&optional quietly)
10810   "Mark all articles in this newsgroup as read.
10811 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10812 instead, which marks only unread articles as read."
10813   (interactive "P")
10814   (gnus-summary-catchup t quietly))
10815
10816 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10817   "Mark all unread articles in this group as read, then exit.
10818 If prefix argument ALL is non-nil, all articles are marked as read.
10819 If QUIETLY is non-nil, no questions will be asked."
10820   (interactive "P")
10821   (when (gnus-summary-catchup all quietly nil 'fast)
10822     ;; Select next newsgroup or exit.
10823     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10824              (eq gnus-auto-select-next 'quietly))
10825         (gnus-summary-next-group nil)
10826       (gnus-summary-exit))))
10827
10828 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10829   "Mark all articles in this newsgroup as read, and then exit.
10830 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10831 instead, which marks only unread articles as read."
10832   (interactive "P")
10833   (gnus-summary-catchup-and-exit t quietly))
10834
10835 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10836   "Mark all articles in this group as read and select the next group.
10837 If given a prefix, mark all articles, unread as well as ticked, as
10838 read."
10839   (interactive "P")
10840   (save-excursion
10841     (gnus-summary-catchup all))
10842   (gnus-summary-next-group))
10843
10844 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
10845   "Mark all articles in this group as read and select the previous group.
10846 If given a prefix, mark all articles, unread as well as ticked, as
10847 read."
10848   (interactive "P")
10849   (save-excursion
10850     (gnus-summary-catchup all))
10851   (gnus-summary-next-group nil nil t))
10852
10853 ;;;
10854 ;;; with article
10855 ;;;
10856
10857 (defmacro gnus-with-article (article &rest forms)
10858   "Select ARTICLE and perform FORMS in the original article buffer.
10859 Then replace the article with the result."
10860   `(progn
10861      ;; We don't want the article to be marked as read.
10862      (let (gnus-mark-article-hook)
10863        (gnus-summary-select-article t t nil ,article))
10864      (set-buffer gnus-original-article-buffer)
10865      ,@forms
10866      (if (not (gnus-check-backend-function
10867                'request-replace-article (car gnus-article-current)))
10868          (gnus-message 5 "Read-only group; not replacing")
10869        (unless (gnus-request-replace-article
10870                 ,article (car gnus-article-current)
10871                 (current-buffer) t)
10872          (error "Couldn't replace article")))
10873      ;; The cache and backlog have to be flushed somewhat.
10874      (when gnus-keep-backlog
10875        (gnus-backlog-remove-article
10876         (car gnus-article-current) (cdr gnus-article-current)))
10877      (when gnus-use-cache
10878        (gnus-cache-update-article
10879         (car gnus-article-current) (cdr gnus-article-current)))))
10880
10881 (put 'gnus-with-article 'lisp-indent-function 1)
10882 (put 'gnus-with-article 'edebug-form-spec '(form body))
10883
10884 ;; Thread-based commands.
10885
10886 (defun gnus-summary-articles-in-thread (&optional article)
10887   "Return a list of all articles in the current thread.
10888 If ARTICLE is non-nil, return all articles in the thread that starts
10889 with that article."
10890   (let* ((article (or article (gnus-summary-article-number)))
10891          (data (gnus-data-find-list article))
10892          (top-level (gnus-data-level (car data)))
10893          (top-subject
10894           (cond ((null gnus-thread-operation-ignore-subject)
10895                  (gnus-simplify-subject-re
10896                   (mail-header-subject (gnus-data-header (car data)))))
10897                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10898                  (gnus-simplify-subject-fuzzy
10899                   (mail-header-subject (gnus-data-header (car data)))))
10900                 (t nil)))
10901          (end-point (save-excursion
10902                       (if (gnus-summary-go-to-next-thread)
10903                           (point) (point-max))))
10904          articles)
10905     (while (and data
10906                 (< (gnus-data-pos (car data)) end-point))
10907       (when (or (not top-subject)
10908                 (string= top-subject
10909                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10910                              (gnus-simplify-subject-fuzzy
10911                               (mail-header-subject
10912                                (gnus-data-header (car data))))
10913                            (gnus-simplify-subject-re
10914                             (mail-header-subject
10915                              (gnus-data-header (car data)))))))
10916         (push (gnus-data-number (car data)) articles))
10917       (unless (and (setq data (cdr data))
10918                    (> (gnus-data-level (car data)) top-level))
10919         (setq data nil)))
10920     ;; Return the list of articles.
10921     (nreverse articles)))
10922
10923 (defun gnus-summary-rethread-current ()
10924   "Rethread the thread the current article is part of."
10925   (interactive)
10926   (let* ((gnus-show-threads t)
10927          (article (gnus-summary-article-number))
10928          (id (mail-header-id (gnus-summary-article-header)))
10929          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10930     (unless id
10931       (error "No article on the current line"))
10932     (gnus-rebuild-thread id)
10933     (gnus-summary-goto-subject article)))
10934
10935 (defun gnus-summary-reparent-thread ()
10936   "Make the current article child of the marked (or previous) article.
10937
10938 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10939 is non-nil or the Subject: of both articles are the same."
10940   (interactive)
10941   (unless (not (gnus-group-read-only-p))
10942     (error "The current newsgroup does not support article editing"))
10943   (unless (<= (length gnus-newsgroup-processable) 1)
10944     (error "No more than one article may be marked"))
10945   (let ((child (gnus-summary-article-number))
10946         ;; First grab the marked article, otherwise one line up.
10947         (parent (if (not (null gnus-newsgroup-processable))
10948                     (car gnus-newsgroup-processable)
10949                   (save-excursion
10950                     (if (eq (forward-line -1) 0)
10951                         (gnus-summary-article-number)
10952                       (error "Beginning of summary buffer"))))))
10953     (gnus-summary-reparent-children parent (list child))))
10954
10955 (defun gnus-summary-reparent-children (parent children)
10956   "Make PARENT the parent of CHILDREN.
10957 When called interactively, PARENT is is current article and
10958 CHILDREN are the process-marked articles."
10959   (interactive
10960    (list (gnus-summary-article-number)
10961          (gnus-summary-work-articles nil)))
10962   (dolist (child children)
10963     (save-window-excursion
10964       (let ((gnus-article-buffer " *reparent*"))
10965         (unless (not (eq parent child))
10966           (error "An article may not be self-referential"))
10967         (let ((message-id (mail-header-id
10968                            (gnus-summary-article-header parent))))
10969           (unless (and message-id (not (equal message-id "")))
10970             (error "No message-id in desired parent"))
10971           (gnus-with-article child
10972             (save-restriction
10973               (goto-char (point-min))
10974               (message-narrow-to-head)
10975               (if (re-search-forward "^References: " nil t)
10976                   (progn
10977                     (re-search-forward "^[^ \t]" nil t)
10978                     (forward-line -1)
10979                     (end-of-line)
10980                     (insert " " message-id))
10981                 (insert "References: " message-id "\n"))))
10982           (set-buffer gnus-summary-buffer)
10983           (gnus-summary-unmark-all-processable)
10984           (gnus-summary-update-article child)
10985           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10986             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10987           (gnus-summary-rethread-current)
10988           (gnus-message 3 "Article %d is now the child of article %d"
10989                         child parent))))))
10990
10991 (defun gnus-summary-toggle-threads (&optional arg)
10992   "Toggle showing conversation threads.
10993 If ARG is positive number, turn showing conversation threads on."
10994   (interactive "P")
10995   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10996     (setq gnus-show-threads
10997           (if (null arg) (not gnus-show-threads)
10998             (> (prefix-numeric-value arg) 0)))
10999     (gnus-summary-prepare)
11000     (gnus-summary-goto-subject current)
11001     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11002     (gnus-summary-position-point)))
11003
11004 (defun gnus-summary-show-all-threads ()
11005   "Show all threads."
11006   (interactive)
11007   (save-excursion
11008     (let ((buffer-read-only nil))
11009       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
11010   (gnus-summary-position-point))
11011
11012 (defun gnus-summary-show-thread ()
11013   "Show thread subtrees.
11014 Returns nil if no thread was there to be shown."
11015   (interactive)
11016   (let ((buffer-read-only nil)
11017         (orig (point))
11018         (end (point-at-eol))
11019         ;; Leave point at bol
11020         (beg (progn (beginning-of-line) (point))))
11021     (prog1
11022         ;; Any hidden lines here?
11023         (search-forward "\r" end t)
11024       (subst-char-in-region beg end ?\^M ?\n t)
11025       (goto-char orig)
11026       (gnus-summary-position-point))))
11027
11028 (defun gnus-summary-maybe-hide-threads ()
11029   "If requested, hide the threads that should be hidden."
11030   (when (and gnus-show-threads
11031              gnus-thread-hide-subtree)
11032     (gnus-summary-hide-all-threads
11033      (if (or (consp gnus-thread-hide-subtree)
11034              (functionp gnus-thread-hide-subtree))
11035          (gnus-make-predicate gnus-thread-hide-subtree)
11036        nil))))
11037
11038 ;;; Hiding predicates.
11039
11040 (defun gnus-article-unread-p (header)
11041   (memq (mail-header-number header) gnus-newsgroup-unreads))
11042
11043 (defun gnus-article-unseen-p (header)
11044   (memq (mail-header-number header) gnus-newsgroup-unseen))
11045
11046 (defun gnus-map-articles (predicate articles)
11047   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11048   (apply 'gnus-or (mapcar predicate
11049                           (mapcar (lambda (number)
11050                                     (gnus-summary-article-header number))
11051                                   articles))))
11052
11053 (defun gnus-summary-hide-all-threads (&optional predicate)
11054   "Hide all thread subtrees.
11055 If PREDICATE is supplied, threads that satisfy this predicate
11056 will not be hidden."
11057   (interactive)
11058   (save-excursion
11059     (goto-char (point-min))
11060     (let ((end nil))
11061       (while (not end)
11062         (when (or (not predicate)
11063                   (gnus-map-articles
11064                    predicate (gnus-summary-article-children)))
11065             (gnus-summary-hide-thread))
11066         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11067   (gnus-summary-position-point))
11068
11069 (defun gnus-summary-hide-thread ()
11070   "Hide thread subtrees.
11071 If PREDICATE is supplied, threads that satisfy this predicate
11072 will not be hidden.
11073 Returns nil if no threads were there to be hidden."
11074   (interactive)
11075   (let ((buffer-read-only nil)
11076         (start (point))
11077         (article (gnus-summary-article-number)))
11078     (goto-char start)
11079     ;; Go forward until either the buffer ends or the subthread
11080     ;; ends.
11081     (when (and (not (eobp))
11082                (or (zerop (gnus-summary-next-thread 1 t))
11083                    (goto-char (point-max))))
11084       (prog1
11085           (if (and (> (point) start)
11086                    (search-backward "\n" start t))
11087               (progn
11088                 (subst-char-in-region start (point) ?\n ?\^M)
11089                 (gnus-summary-goto-subject article))
11090             (goto-char start)
11091             nil)))))
11092
11093 (defun gnus-summary-go-to-next-thread (&optional previous)
11094   "Go to the same level (or less) next thread.
11095 If PREVIOUS is non-nil, go to previous thread instead.
11096 Return the article number moved to, or nil if moving was impossible."
11097   (let ((level (gnus-summary-thread-level))
11098         (way (if previous -1 1))
11099         (beg (point)))
11100     (forward-line way)
11101     (while (and (not (eobp))
11102                 (< level (gnus-summary-thread-level)))
11103       (forward-line way))
11104     (if (eobp)
11105         (progn
11106           (goto-char beg)
11107           nil)
11108       (setq beg (point))
11109       (prog1
11110           (gnus-summary-article-number)
11111         (goto-char beg)))))
11112
11113 (defun gnus-summary-next-thread (n &optional silent)
11114   "Go to the same level next N'th thread.
11115 If N is negative, search backward instead.
11116 Returns the difference between N and the number of skips actually
11117 done.
11118
11119 If SILENT, don't output messages."
11120   (interactive "p")
11121   (let ((backward (< n 0))
11122         (n (abs n)))
11123     (while (and (> n 0)
11124                 (gnus-summary-go-to-next-thread backward))
11125       (decf n))
11126     (unless silent
11127       (gnus-summary-position-point))
11128     (when (and (not silent) (/= 0 n))
11129       (gnus-message 7 "No more threads"))
11130     n))
11131
11132 (defun gnus-summary-prev-thread (n)
11133   "Go to the same level previous N'th thread.
11134 Returns the difference between N and the number of skips actually
11135 done."
11136   (interactive "p")
11137   (gnus-summary-next-thread (- n)))
11138
11139 (defun gnus-summary-go-down-thread ()
11140   "Go down one level in the current thread."
11141   (let ((children (gnus-summary-article-children)))
11142     (when children
11143       (gnus-summary-goto-subject (car children)))))
11144
11145 (defun gnus-summary-go-up-thread ()
11146   "Go up one level in the current thread."
11147   (let ((parent (gnus-summary-article-parent)))
11148     (when parent
11149       (gnus-summary-goto-subject parent))))
11150
11151 (defun gnus-summary-down-thread (n)
11152   "Go down thread N steps.
11153 If N is negative, go up instead.
11154 Returns the difference between N and how many steps down that were
11155 taken."
11156   (interactive "p")
11157   (let ((up (< n 0))
11158         (n (abs n)))
11159     (while (and (> n 0)
11160                 (if up (gnus-summary-go-up-thread)
11161                   (gnus-summary-go-down-thread)))
11162       (setq n (1- n)))
11163     (gnus-summary-position-point)
11164     (when (/= 0 n)
11165       (gnus-message 7 "Can't go further"))
11166     n))
11167
11168 (defun gnus-summary-up-thread (n)
11169   "Go up thread N steps.
11170 If N is negative, go down instead.
11171 Returns the difference between N and how many steps down that were
11172 taken."
11173   (interactive "p")
11174   (gnus-summary-down-thread (- n)))
11175
11176 (defun gnus-summary-top-thread ()
11177   "Go to the top of the thread."
11178   (interactive)
11179   (while (gnus-summary-go-up-thread))
11180   (gnus-summary-article-number))
11181
11182 (defun gnus-summary-kill-thread (&optional unmark)
11183   "Mark articles under current thread as read.
11184 If the prefix argument is positive, remove any kinds of marks.
11185 If the prefix argument is negative, tick articles instead."
11186   (interactive "P")
11187   (when unmark
11188     (setq unmark (prefix-numeric-value unmark)))
11189   (let ((articles (gnus-summary-articles-in-thread)))
11190     (save-excursion
11191       ;; Expand the thread.
11192       (gnus-summary-show-thread)
11193       ;; Mark all the articles.
11194       (while articles
11195         (gnus-summary-goto-subject (car articles))
11196         (cond ((null unmark)
11197                (gnus-summary-mark-article-as-read gnus-killed-mark))
11198               ((> unmark 0)
11199                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11200               (t
11201                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11202         (setq articles (cdr articles))))
11203     ;; Hide killed subtrees.
11204     (and (null unmark)
11205          gnus-thread-hide-killed
11206          (gnus-summary-hide-thread))
11207     ;; If marked as read, go to next unread subject.
11208     (when (null unmark)
11209       ;; Go to next unread subject.
11210       (gnus-summary-next-subject 1 t)))
11211   (gnus-set-mode-line 'summary))
11212
11213 ;; Summary sorting commands
11214
11215 (defun gnus-summary-sort-by-number (&optional reverse)
11216   "Sort the summary buffer by article number.
11217 Argument REVERSE means reverse order."
11218   (interactive "P")
11219   (gnus-summary-sort 'number reverse))
11220
11221 (defun gnus-summary-sort-by-random (&optional reverse)
11222   "Randomize the order in the summary buffer.
11223 Argument REVERSE means to randomize in reverse order."
11224   (interactive "P")
11225   (gnus-summary-sort 'random reverse))
11226
11227 (defun gnus-summary-sort-by-author (&optional reverse)
11228   "Sort the summary buffer by author name alphabetically.
11229 If `case-fold-search' is non-nil, case of letters is ignored.
11230 Argument REVERSE means reverse order."
11231   (interactive "P")
11232   (gnus-summary-sort 'author reverse))
11233
11234 (defun gnus-summary-sort-by-recipient (&optional reverse)
11235   "Sort the summary buffer by recipient name alphabetically.
11236 If `case-fold-search' is non-nil, case of letters is ignored.
11237 Argument REVERSE means reverse order."
11238   (interactive "P")
11239   (gnus-summary-sort 'recipient reverse))
11240
11241 (defun gnus-summary-sort-by-subject (&optional reverse)
11242   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11243 If `case-fold-search' is non-nil, case of letters is ignored.
11244 Argument REVERSE means reverse order."
11245   (interactive "P")
11246   (gnus-summary-sort 'subject reverse))
11247
11248 (defun gnus-summary-sort-by-date (&optional reverse)
11249   "Sort the summary buffer by date.
11250 Argument REVERSE means reverse order."
11251   (interactive "P")
11252   (gnus-summary-sort 'date reverse))
11253
11254 (defun gnus-summary-sort-by-score (&optional reverse)
11255   "Sort the summary buffer by score.
11256 Argument REVERSE means reverse order."
11257   (interactive "P")
11258   (gnus-summary-sort 'score reverse))
11259
11260 (defun gnus-summary-sort-by-lines (&optional reverse)
11261   "Sort the summary buffer by the number of lines.
11262 Argument REVERSE means reverse order."
11263   (interactive "P")
11264   (gnus-summary-sort 'lines reverse))
11265
11266 (defun gnus-summary-sort-by-chars (&optional reverse)
11267   "Sort the summary buffer by article length.
11268 Argument REVERSE means reverse order."
11269   (interactive "P")
11270   (gnus-summary-sort 'chars reverse))
11271
11272 (defun gnus-summary-sort-by-original (&optional reverse)
11273   "Sort the summary buffer using the default sorting method.
11274 Argument REVERSE means reverse order."
11275   (interactive "P")
11276   (let* ((buffer-read-only)
11277          (gnus-summary-prepare-hook nil))
11278     ;; We do the sorting by regenerating the threads.
11279     (gnus-summary-prepare)
11280     ;; Hide subthreads if needed.
11281     (gnus-summary-maybe-hide-threads)))
11282
11283 (defun gnus-summary-sort (predicate reverse)
11284   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11285   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11286          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11287          (gnus-thread-sort-functions
11288           (if (not reverse)
11289               thread
11290             `(lambda (t1 t2)
11291                (,thread t2 t1))))
11292          (gnus-sort-gathered-threads-function
11293           gnus-thread-sort-functions)
11294          (gnus-article-sort-functions
11295           (if (not reverse)
11296               article
11297             `(lambda (t1 t2)
11298                (,article t2 t1))))
11299          (buffer-read-only)
11300          (gnus-summary-prepare-hook nil))
11301     ;; We do the sorting by regenerating the threads.
11302     (gnus-summary-prepare)
11303     ;; Hide subthreads if needed.
11304     (gnus-summary-maybe-hide-threads)))
11305
11306 ;; Summary saving commands.
11307
11308 (defun gnus-summary-save-article (&optional n not-saved)
11309   "Save the current article using the default saver function.
11310 If N is a positive number, save the N next articles.
11311 If N is a negative number, save the N previous articles.
11312 If N is nil and any articles have been marked with the process mark,
11313 save those articles instead.
11314 The variable `gnus-default-article-saver' specifies the saver function.
11315
11316 If the optional second argument NOT-SAVED is non-nil, articles saved
11317 will not be marked as saved."
11318   (interactive "P")
11319   (require 'gnus-art)
11320   (let* ((articles (gnus-summary-work-articles n))
11321          (save-buffer (save-excursion
11322                         (nnheader-set-temp-buffer " *Gnus Save*")))
11323          (num (length articles))
11324          ;; Whether to save decoded articles or raw articles.
11325          (decode (when gnus-article-save-coding-system
11326                    (get gnus-default-article-saver :decode)))
11327          ;; When saving many articles in a single file, use the other
11328          ;; function to save articles other than the first one.
11329          (saver2 (get gnus-default-article-saver :function))
11330          (gnus-prompt-before-saving (if saver2
11331                                         t
11332                                       gnus-prompt-before-saving))
11333          (gnus-default-article-saver gnus-default-article-saver)
11334          header file)
11335     (dolist (article articles)
11336       (setq header (gnus-summary-article-header article))
11337       (if (not (vectorp header))
11338           ;; This is a pseudo-article.
11339           (if (assq 'name header)
11340               (gnus-copy-file (cdr (assq 'name header)))
11341             (gnus-message 1 "Article %d is unsaveable" article))
11342         ;; This is a real article.
11343         (save-window-excursion
11344           (let ((gnus-display-mime-function (when decode
11345                                               gnus-display-mime-function))
11346                 (gnus-article-prepare-hook (when decode
11347                                              gnus-article-prepare-hook)))
11348             (gnus-summary-select-article t nil nil article)
11349             (gnus-summary-goto-subject article)))
11350         (save-excursion
11351           (set-buffer save-buffer)
11352           (erase-buffer)
11353           (insert-buffer-substring (if decode
11354                                        gnus-article-buffer
11355                                      gnus-original-article-buffer)))
11356         (setq file (gnus-article-save save-buffer file num))
11357         (gnus-summary-remove-process-mark article)
11358         (unless not-saved
11359           (gnus-summary-set-saved-mark article)))
11360       (when saver2
11361         (setq gnus-default-article-saver saver2
11362               saver2 nil)))
11363     (gnus-kill-buffer save-buffer)
11364     (gnus-summary-position-point)
11365     (gnus-set-mode-line 'summary)
11366     n))
11367
11368 (defun gnus-summary-pipe-output (&optional arg headers)
11369   "Pipe the current article to a subprocess.
11370 If N is a positive number, pipe the N next articles.
11371 If N is a negative number, pipe the N previous articles.
11372 If N is nil and any articles have been marked with the process mark,
11373 pipe those articles instead.
11374 If HEADERS (the symbolic prefix), include the headers, too."
11375   (interactive (gnus-interactive "P\ny"))
11376   (require 'gnus-art)
11377   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11378         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11379     (gnus-summary-save-article arg t))
11380   (let ((buffer (get-buffer "*Shell Command Output*")))
11381     (when (and buffer
11382                (not (zerop (buffer-size buffer))))
11383       (gnus-configure-windows 'pipe))))
11384
11385 (defun gnus-summary-save-article-mail (&optional arg)
11386   "Append the current article to a Unix mail box file.
11387 If N is a positive number, save the N next articles.
11388 If N is a negative number, save the N previous articles.
11389 If N is nil and any articles have been marked with the process mark,
11390 save those articles instead."
11391   (interactive "P")
11392   (require 'gnus-art)
11393   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11394     (gnus-summary-save-article arg)))
11395
11396 (defun gnus-summary-save-article-rmail (&optional arg)
11397   "Append the current article to an rmail file.
11398 If N is a positive number, save the N next articles.
11399 If N is a negative number, save the N previous articles.
11400 If N is nil and any articles have been marked with the process mark,
11401 save those articles instead."
11402   (interactive "P")
11403   (require 'gnus-art)
11404   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11405     (gnus-summary-save-article arg)))
11406
11407 (defun gnus-summary-save-article-file (&optional arg)
11408   "Append the current article to a file.
11409 If N is a positive number, save the N next articles.
11410 If N is a negative number, save the N previous articles.
11411 If N is nil and any articles have been marked with the process mark,
11412 save those articles instead."
11413   (interactive "P")
11414   (require 'gnus-art)
11415   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11416     (gnus-summary-save-article arg)))
11417
11418 (defun gnus-summary-write-article-file (&optional arg)
11419   "Write the current article to a file, deleting the previous file.
11420 If N is a positive number, save the N next articles.
11421 If N is a negative number, save the N previous articles.
11422 If N is nil and any articles have been marked with the process mark,
11423 save those articles instead."
11424   (interactive "P")
11425   (require 'gnus-art)
11426   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11427     (gnus-summary-save-article arg)))
11428
11429 (defun gnus-summary-save-article-body-file (&optional arg)
11430   "Append the current article body to a file.
11431 If N is a positive number, save the N next articles.
11432 If N is a negative number, save the N previous articles.
11433 If N is nil and any articles have been marked with the process mark,
11434 save those articles instead."
11435   (interactive "P")
11436   (require 'gnus-art)
11437   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11438     (gnus-summary-save-article arg)))
11439
11440 (defun gnus-summary-write-article-body-file (&optional arg)
11441   "Write the current article body to a file, deleting the previous file.
11442 If N is a positive number, save the N next articles.
11443 If N is a negative number, save the N previous articles.
11444 If N is nil and any articles have been marked with the process mark,
11445 save those articles instead."
11446   (interactive "P")
11447   (require 'gnus-art)
11448   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11449     (gnus-summary-save-article arg)))
11450
11451 (defun gnus-summary-muttprint (&optional arg)
11452   "Print the current article using Muttprint.
11453 If N is a positive number, save the N next articles.
11454 If N is a negative number, save the N previous articles.
11455 If N is nil and any articles have been marked with the process mark,
11456 save those articles instead."
11457   (interactive "P")
11458   (require 'gnus-art)
11459   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11460     (gnus-summary-save-article arg t)))
11461
11462 (defun gnus-summary-pipe-message (program)
11463   "Pipe the current article through PROGRAM."
11464   (interactive "sProgram: ")
11465   (gnus-summary-select-article)
11466   (let ((mail-header-separator ""))
11467     (gnus-eval-in-buffer-window gnus-article-buffer
11468       (save-restriction
11469         (widen)
11470         (let ((start (window-start))
11471               buffer-read-only)
11472           (message-pipe-buffer-body program)
11473           (set-window-start (get-buffer-window (current-buffer)) start))))))
11474
11475 (defun gnus-get-split-value (methods)
11476   "Return a value based on the split METHODS."
11477   (let (split-name method result match)
11478     (when methods
11479       (save-excursion
11480         (set-buffer gnus-original-article-buffer)
11481         (save-restriction
11482           (nnheader-narrow-to-headers)
11483           (while (and methods (not split-name))
11484             (goto-char (point-min))
11485             (setq method (pop methods))
11486             (setq match (car method))
11487             (when (cond
11488                    ((stringp match)
11489                     ;; Regular expression.
11490                     (ignore-errors
11491                       (re-search-forward match nil t)))
11492                    ((functionp match)
11493                     ;; Function.
11494                     (save-restriction
11495                       (widen)
11496                       (setq result (funcall match gnus-newsgroup-name))))
11497                    ((consp match)
11498                     ;; Form.
11499                     (save-restriction
11500                       (widen)
11501                       (setq result (eval match)))))
11502               (setq split-name (cdr method))
11503               (cond ((stringp result)
11504                      (push (expand-file-name
11505                             result gnus-article-save-directory)
11506                            split-name))
11507                     ((consp result)
11508                      (setq split-name (append result split-name)))))))))
11509     (nreverse split-name)))
11510
11511 (defun gnus-valid-move-group-p (group)
11512   (and (boundp group)
11513        (symbol-name group)
11514        (symbol-value group)
11515        (gnus-get-function (gnus-find-method-for-group
11516                            (symbol-name group)) 'request-accept-article t)))
11517
11518 (defun gnus-read-move-group-name (prompt default articles prefix)
11519   "Read a group name."
11520   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11521          (minibuffer-confirm-incomplete nil) ; XEmacs
11522          (prom
11523           (format "%s %s to"
11524                   prompt
11525                   (if (> (length articles) 1)
11526                       (format "these %d articles" (length articles))
11527                     "this article")))
11528          (to-newsgroup
11529           (cond
11530            ((null split-name)
11531             (gnus-completing-read-with-default
11532              default prom
11533              gnus-active-hashtb
11534              'gnus-valid-move-group-p
11535              nil prefix
11536              'gnus-group-history))
11537            ((= 1 (length split-name))
11538             (gnus-completing-read-with-default
11539              (car split-name) prom
11540              gnus-active-hashtb
11541              'gnus-valid-move-group-p
11542              nil nil
11543              'gnus-group-history))
11544            (t
11545             (gnus-completing-read-with-default
11546              nil prom
11547              (mapcar 'list (nreverse split-name))
11548              nil nil nil
11549              'gnus-group-history))))
11550          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11551     (when to-newsgroup
11552       (if (or (string= to-newsgroup "")
11553               (string= to-newsgroup prefix))
11554           (setq to-newsgroup default))
11555       (unless to-newsgroup
11556         (error "No group name entered"))
11557       (or (gnus-active to-newsgroup)
11558           (gnus-activate-group to-newsgroup nil nil to-method)
11559           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11560                                      to-newsgroup))
11561               (or (and (gnus-request-create-group to-newsgroup to-method)
11562                        (gnus-activate-group
11563                         to-newsgroup nil nil to-method)
11564                        (gnus-subscribe-group to-newsgroup))
11565                   (error "Couldn't create group %s" to-newsgroup)))
11566           (error "No such group: %s" to-newsgroup)))
11567     to-newsgroup))
11568
11569 (defvar gnus-summary-save-parts-counter)
11570
11571 (defun gnus-summary-save-parts (type dir n &optional reverse)
11572   "Save parts matching TYPE to DIR.
11573 If REVERSE, save parts that do not match TYPE."
11574   (interactive
11575    (list (read-string "Save parts of type: "
11576                       (or (car gnus-summary-save-parts-type-history)
11577                           gnus-summary-save-parts-default-mime)
11578                       'gnus-summary-save-parts-type-history)
11579          (setq gnus-summary-save-parts-last-directory
11580                (read-file-name "Save to directory: "
11581                                gnus-summary-save-parts-last-directory
11582                                nil t))
11583          current-prefix-arg))
11584   (gnus-summary-iterate n
11585     (let ((gnus-display-mime-function nil)
11586           (gnus-inhibit-treatment t))
11587       (gnus-summary-select-article))
11588     (save-excursion
11589       (set-buffer gnus-article-buffer)
11590       (let ((handles (or gnus-article-mime-handles
11591                          (mm-dissect-buffer nil gnus-article-loose-mime)
11592                          (and gnus-article-emulate-mime
11593                               (mm-uu-dissect))))
11594             (gnus-summary-save-parts-counter 1))
11595         (when handles
11596           (gnus-summary-save-parts-1 type dir handles reverse)
11597           (unless gnus-article-mime-handles ;; Don't destroy this case.
11598             (mm-destroy-parts handles)))))))
11599
11600 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11601   (if (stringp (car handle))
11602       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11603               (cdr handle))
11604     (when (if reverse
11605               (not (string-match type (mm-handle-media-type handle)))
11606             (string-match type (mm-handle-media-type handle)))
11607       (let ((file (expand-file-name
11608                    (gnus-map-function
11609                     mm-file-name-rewrite-functions
11610                     (file-name-nondirectory
11611                      (or
11612                       (mail-content-type-get
11613                        (mm-handle-disposition handle) 'filename)
11614                       (mail-content-type-get
11615                        (mm-handle-type handle) 'name)
11616                       (format "%s.%d.%d" gnus-newsgroup-name
11617                               (cdr gnus-article-current)
11618                               gnus-summary-save-parts-counter))))
11619                    dir)))
11620         (incf gnus-summary-save-parts-counter)
11621         (unless (file-exists-p file)
11622           (mm-save-part-to-file handle file))))))
11623
11624 ;; Summary extract commands
11625
11626 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11627   (let ((buffer-read-only nil)
11628         (article (gnus-summary-article-number))
11629         after-article b e)
11630     (unless (gnus-summary-goto-subject article)
11631       (error "No such article: %d" article))
11632     (gnus-summary-position-point)
11633     ;; If all commands are to be bunched up on one line, we collect
11634     ;; them here.
11635     (unless gnus-view-pseudos-separately
11636       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11637             files action)
11638         (while ps
11639           (setq action (cdr (assq 'action (car ps))))
11640           (setq files (list (cdr (assq 'name (car ps)))))
11641           (while (and ps (cdr ps)
11642                       (string= (or action "1")
11643                                (or (cdr (assq 'action (cadr ps))) "2")))
11644             (push (cdr (assq 'name (cadr ps))) files)
11645             (setcdr ps (cddr ps)))
11646           (when files
11647             (when (not (string-match "%s" action))
11648               (push " " files))
11649             (push " " files)
11650             (when (assq 'execute (car ps))
11651               (setcdr (assq 'execute (car ps))
11652                       (funcall (if (string-match "%s" action)
11653                                    'format 'concat)
11654                                action
11655                                (mapconcat
11656                                 (lambda (f)
11657                                   (if (equal f " ")
11658                                       f
11659                                     (shell-quote-argument f)))
11660                                 files " ")))))
11661           (setq ps (cdr ps)))))
11662     (if (and gnus-view-pseudos (not not-view))
11663         (while pslist
11664           (when (assq 'execute (car pslist))
11665             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11666                                   (eq gnus-view-pseudos 'not-confirm)))
11667           (setq pslist (cdr pslist)))
11668       (save-excursion
11669         (while pslist
11670           (setq after-article (or (cdr (assq 'article (car pslist)))
11671                                   (gnus-summary-article-number)))
11672           (gnus-summary-goto-subject after-article)
11673           (forward-line 1)
11674           (setq b (point))
11675           (insert "    " (file-name-nondirectory
11676                           (cdr (assq 'name (car pslist))))
11677                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11678           (setq e (point))
11679           (forward-line -1)             ; back to `b'
11680           (gnus-add-text-properties
11681            b (1- e) (list 'gnus-number gnus-reffed-article-number
11682                           gnus-mouse-face-prop gnus-mouse-face))
11683           (gnus-data-enter
11684            after-article gnus-reffed-article-number
11685            gnus-unread-mark b (car pslist) 0 (- e b))
11686           (setq gnus-newsgroup-unreads
11687                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11688                                          gnus-reffed-article-number))
11689           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11690           (setq pslist (cdr pslist)))))))
11691
11692 (defun gnus-pseudos< (p1 p2)
11693   (let ((c1 (cdr (assq 'action p1)))
11694         (c2 (cdr (assq 'action p2))))
11695     (and c1 c2 (string< c1 c2))))
11696
11697 (defun gnus-request-pseudo-article (props)
11698   (cond ((assq 'execute props)
11699          (gnus-execute-command (cdr (assq 'execute props)))))
11700   (let ((gnus-current-article (gnus-summary-article-number)))
11701     (gnus-run-hooks 'gnus-mark-article-hook)))
11702
11703 (defun gnus-execute-command (command &optional automatic)
11704   (save-excursion
11705     (gnus-article-setup-buffer)
11706     (set-buffer gnus-article-buffer)
11707     (setq buffer-read-only nil)
11708     (let ((command (if automatic command
11709                      (read-string "Command: " (cons command 0)))))
11710       (erase-buffer)
11711       (insert "$ " command "\n\n")
11712       (if gnus-view-pseudo-asynchronously
11713           (start-process "gnus-execute" (current-buffer) shell-file-name
11714                          shell-command-switch command)
11715         (call-process shell-file-name nil t nil
11716                       shell-command-switch command)))))
11717
11718 ;; Summary kill commands.
11719
11720 (defun gnus-summary-edit-global-kill (article)
11721   "Edit the \"global\" kill file."
11722   (interactive (list (gnus-summary-article-number)))
11723   (gnus-group-edit-global-kill article))
11724
11725 (defun gnus-summary-edit-local-kill ()
11726   "Edit a local kill file applied to the current newsgroup."
11727   (interactive)
11728   (setq gnus-current-headers (gnus-summary-article-header))
11729   (gnus-group-edit-local-kill
11730    (gnus-summary-article-number) gnus-newsgroup-name))
11731
11732 ;;; Header reading.
11733
11734 (defun gnus-read-header (id &optional header)
11735   "Read the headers of article ID and enter them into the Gnus system."
11736   (let ((group gnus-newsgroup-name)
11737         (gnus-override-method
11738          (or
11739           gnus-override-method
11740           (and (gnus-news-group-p gnus-newsgroup-name)
11741                (car (gnus-refer-article-methods)))))
11742         where)
11743     ;; First we check to see whether the header in question is already
11744     ;; fetched.
11745     (if (stringp id)
11746         ;; This is a Message-ID.
11747         (setq header (or header (gnus-id-to-header id)))
11748       ;; This is an article number.
11749       (setq header (or header (gnus-summary-article-header id))))
11750     (if (and header
11751              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11752         ;; We have found the header.
11753         header
11754       ;; We have to really fetch the header to this article.
11755       (save-excursion
11756         (set-buffer nntp-server-buffer)
11757         (when (setq where (gnus-request-head id group))
11758           (nnheader-fold-continuation-lines)
11759           (goto-char (point-max))
11760           (insert ".\n")
11761           (goto-char (point-min))
11762           (insert "211 ")
11763           (princ (cond
11764                   ((numberp id) id)
11765                   ((cdr where) (cdr where))
11766                   (header (mail-header-number header))
11767                   (t gnus-reffed-article-number))
11768                  (current-buffer))
11769           (insert " Article retrieved.\n"))
11770         (if (or (not where)
11771                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11772             ()                          ; Malformed head.
11773           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11774             (when (and (stringp id)
11775                        (or
11776                         (not (string= (gnus-group-real-name group)
11777                                       (car where)))
11778                         (not (gnus-server-equal gnus-override-method
11779                                                 (gnus-group-method group)))))
11780               ;; If we fetched by Message-ID and the article came from
11781               ;; a different group (or server), we fudge some bogus
11782               ;; article numbers for this article.
11783               (mail-header-set-number header gnus-reffed-article-number))
11784             (save-excursion
11785               (set-buffer gnus-summary-buffer)
11786               (decf gnus-reffed-article-number)
11787               (gnus-remove-header (mail-header-number header))
11788               (push header gnus-newsgroup-headers)
11789               (setq gnus-current-headers header)
11790               (push (mail-header-number header) gnus-newsgroup-limit)))
11791           header)))))
11792
11793 (defun gnus-remove-header (number)
11794   "Remove header NUMBER from `gnus-newsgroup-headers'."
11795   (if (and gnus-newsgroup-headers
11796            (= number (mail-header-number (car gnus-newsgroup-headers))))
11797       (pop gnus-newsgroup-headers)
11798     (let ((headers gnus-newsgroup-headers))
11799       (while (and (cdr headers)
11800                   (not (= number (mail-header-number (cadr headers)))))
11801         (pop headers))
11802       (when (cdr headers)
11803         (setcdr headers (cddr headers))))))
11804
11805 ;;;
11806 ;;; summary highlights
11807 ;;;
11808
11809 (defun gnus-highlight-selected-summary ()
11810   "Highlight selected article in summary buffer."
11811   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11812   (when gnus-summary-selected-face
11813     (save-excursion
11814       (let* ((beg (point-at-bol))
11815              (end (point-at-eol))
11816              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11817              (from (if (get-text-property beg gnus-mouse-face-prop)
11818                        beg
11819                      (or (next-single-property-change
11820                           beg gnus-mouse-face-prop nil end)
11821                          beg)))
11822              (to
11823               (if (= from end)
11824                   (- from 2)
11825                 (or (next-single-property-change
11826                      from gnus-mouse-face-prop nil end)
11827                     end))))
11828         ;; If no mouse-face prop on line we will have to = from = end,
11829         ;; so we highlight the entire line instead.
11830         (when (= (+ to 2) from)
11831           (setq from beg)
11832           (setq to end))
11833         (if gnus-newsgroup-selected-overlay
11834             ;; Move old overlay.
11835             (gnus-move-overlay
11836              gnus-newsgroup-selected-overlay from to (current-buffer))
11837           ;; Create new overlay.
11838           (gnus-overlay-put
11839            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11840            'face gnus-summary-selected-face))))))
11841
11842 (defvar gnus-summary-highlight-line-cached nil)
11843 (defvar gnus-summary-highlight-line-trigger nil)
11844
11845 (defun gnus-summary-highlight-line-0 ()
11846   (if (and (eq gnus-summary-highlight-line-trigger
11847                gnus-summary-highlight)
11848            gnus-summary-highlight-line-cached)
11849       gnus-summary-highlight-line-cached
11850     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11851           gnus-summary-highlight-line-cached
11852           (let* ((cond (list 'cond))
11853                  (c cond)
11854                  (list gnus-summary-highlight))
11855             (while list
11856               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11857                               nil))
11858               (setq c (cdr c)
11859                     list (cdr list)))
11860             (gnus-byte-compile (list 'lambda nil cond))))))
11861
11862 (defun gnus-summary-highlight-line ()
11863   "Highlight current line according to `gnus-summary-highlight'."
11864   (let* ((beg (point-at-bol))
11865          (article (or (gnus-summary-article-number) gnus-current-article))
11866          (score (or (cdr (assq article
11867                                gnus-newsgroup-scored))
11868                     gnus-summary-default-score 0))
11869          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11870          (inhibit-read-only t)
11871          (default gnus-summary-default-score)
11872          (default-high gnus-summary-default-high-score)
11873          (default-low gnus-summary-default-low-score)
11874          (uncached (and gnus-summary-use-undownloaded-faces
11875                         (memq article gnus-newsgroup-undownloaded)
11876                         (not (memq article gnus-newsgroup-cached)))))
11877     (let ((face (funcall (gnus-summary-highlight-line-0))))
11878       (unless (eq face (get-text-property beg 'face))
11879         (gnus-put-text-property-excluding-characters-with-faces
11880          beg (point-at-eol) 'face
11881          (setq face (if (boundp face) (symbol-value face) face)))
11882         (when gnus-summary-highlight-line-function
11883           (funcall gnus-summary-highlight-line-function article face))))))
11884
11885 (defun gnus-update-read-articles (group unread &optional compute)
11886   "Update the list of read articles in GROUP.
11887 UNREAD is a sorted list."
11888   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11889         (info (gnus-get-info group))
11890         (prev 1)
11891         read)
11892     (if (or (not info) (not active))
11893         ;; There is no info on this group if it was, in fact,
11894         ;; killed.  Gnus stores no information on killed groups, so
11895         ;; there's nothing to be done.
11896         ;; One could store the information somewhere temporarily,
11897         ;; perhaps...  Hmmm...
11898         ()
11899       ;; Remove any negative articles numbers.
11900       (while (and unread (< (car unread) 0))
11901         (setq unread (cdr unread)))
11902       ;; Remove any expired article numbers
11903       (while (and unread (< (car unread) (car active)))
11904         (setq unread (cdr unread)))
11905       ;; Compute the ranges of read articles by looking at the list of
11906       ;; unread articles.
11907       (while unread
11908         (when (/= (car unread) prev)
11909           (push (if (= prev (1- (car unread))) prev
11910                   (cons prev (1- (car unread))))
11911                 read))
11912         (setq prev (1+ (car unread)))
11913         (setq unread (cdr unread)))
11914       (when (<= prev (cdr active))
11915         (push (cons prev (cdr active)) read))
11916       (setq read (if (> (length read) 1) (nreverse read) read))
11917       (if compute
11918           read
11919         (save-excursion
11920           (let (setmarkundo)
11921             ;; Propagate the read marks to the backend.
11922             (when (gnus-check-backend-function 'request-set-mark group)
11923               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11924                     (add (gnus-remove-from-range read (gnus-info-read info))))
11925                 (when (or add del)
11926                   (unless (gnus-check-group group)
11927                     (error "Can't open server for %s" group))
11928                   (gnus-request-set-mark
11929                    group (delq nil (list (if add (list add 'add '(read)))
11930                                          (if del (list del 'del '(read))))))
11931                   (setq setmarkundo
11932                         `(gnus-request-set-mark
11933                           ,group
11934                           ',(delq nil (list
11935                                        (if del (list del 'add '(read)))
11936                                        (if add (list add 'del '(read))))))))))
11937             (set-buffer gnus-group-buffer)
11938             (gnus-undo-register
11939               `(progn
11940                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11941                  (gnus-info-set-read ',info ',(gnus-info-read info))
11942                  (gnus-get-unread-articles-in-group ',info
11943                                                     (gnus-active ,group))
11944                  (gnus-group-update-group ,group t)
11945                  ,setmarkundo))))
11946         ;; Enter this list into the group info.
11947         (gnus-info-set-read info read)
11948         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11949         (gnus-get-unread-articles-in-group info (gnus-active group))
11950         t))))
11951
11952 (defun gnus-offer-save-summaries ()
11953   "Offer to save all active summary buffers."
11954   (let (buffers)
11955     ;; Go through all buffers and find all summaries.
11956     (dolist (buffer (buffer-list))
11957       (when (and (setq buffer (buffer-name buffer))
11958                  (string-match "Summary" buffer)
11959                  (with-current-buffer buffer
11960                    ;; We check that this is, indeed, a summary buffer.
11961                    (and (eq major-mode 'gnus-summary-mode)
11962                         ;; Also make sure this isn't bogus.
11963                         gnus-newsgroup-prepared
11964                         ;; Also make sure that this isn't a
11965                         ;; dead summary buffer.
11966                         (not gnus-dead-summary-mode))))
11967         (push buffer buffers)))
11968     ;; Go through all these summary buffers and offer to save them.
11969     (when buffers
11970       (save-excursion
11971         (map-y-or-n-p
11972          "Update summary buffer %s? "
11973          (lambda (buf)
11974            (switch-to-buffer buf)
11975            (gnus-summary-exit))
11976          buffers)))))
11977
11978 (defun gnus-summary-setup-default-charset ()
11979   "Setup newsgroup default charset."
11980   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11981       (setq gnus-newsgroup-charset nil)
11982     (let* ((ignored-charsets
11983             (or gnus-newsgroup-ephemeral-ignored-charsets
11984                 (append
11985                  (and gnus-newsgroup-name
11986                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11987                  gnus-newsgroup-ignored-charsets))))
11988       (setq gnus-newsgroup-charset
11989             (or gnus-newsgroup-ephemeral-charset
11990                 (and gnus-newsgroup-name
11991                      (gnus-parameter-charset gnus-newsgroup-name))
11992                 gnus-default-charset))
11993       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11994            ignored-charsets))))
11995
11996 ;;;
11997 ;;; Mime Commands
11998 ;;;
11999
12000 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12001   "Display the current article buffer fully MIME-buttonized.
12002 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12003 treated as multipart/mixed."
12004   (interactive "P")
12005   (require 'gnus-art)
12006   (let ((gnus-unbuttonized-mime-types nil)
12007         (gnus-mime-display-multipart-as-mixed show-all-parts))
12008     (gnus-summary-show-article)))
12009
12010 (defun gnus-summary-repair-multipart (article)
12011   "Add a Content-Type header to a multipart article without one."
12012   (interactive (list (gnus-summary-article-number)))
12013   (gnus-with-article article
12014     (message-narrow-to-head)
12015     (message-remove-header "Mime-Version")
12016     (goto-char (point-max))
12017     (insert "Mime-Version: 1.0\n")
12018     (widen)
12019     (when (search-forward "\n--" nil t)
12020       (let ((separator (buffer-substring (point) (point-at-eol))))
12021         (message-narrow-to-head)
12022         (message-remove-header "Content-Type")
12023         (goto-char (point-max))
12024         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12025                         separator))
12026         (widen))))
12027   (let (gnus-mark-article-hook)
12028     (gnus-summary-select-article t t nil article)))
12029
12030 (defun gnus-summary-toggle-display-buttonized ()
12031   "Toggle the buttonizing of the article buffer."
12032   (interactive)
12033   (require 'gnus-art)
12034   (if (setq gnus-inhibit-mime-unbuttonizing
12035             (not gnus-inhibit-mime-unbuttonizing))
12036       (let ((gnus-unbuttonized-mime-types nil))
12037         (gnus-summary-show-article))
12038     (gnus-summary-show-article)))
12039
12040 ;;;
12041 ;;; Generic summary marking commands
12042 ;;;
12043
12044 (defvar gnus-summary-marking-alist
12045   '((read gnus-del-mark "d")
12046     (unread gnus-unread-mark "u")
12047     (ticked gnus-ticked-mark "!")
12048     (dormant gnus-dormant-mark "?")
12049     (expirable gnus-expirable-mark "e"))
12050   "An alist of names/marks/keystrokes.")
12051
12052 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12053 (defvar gnus-summary-mark-map)
12054
12055 (defun gnus-summary-make-all-marking-commands ()
12056   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12057   (dolist (elem gnus-summary-marking-alist)
12058     (apply 'gnus-summary-make-marking-command elem)))
12059
12060 (defun gnus-summary-make-marking-command (name mark keystroke)
12061   (let ((map (make-sparse-keymap)))
12062     (define-key gnus-summary-generic-mark-map keystroke map)
12063     (dolist (lway `((next "next" next nil "n")
12064                     (next-unread "next unread" next t "N")
12065                     (prev "previous" prev nil "p")
12066                     (prev-unread "previous unread" prev t "P")
12067                     (nomove "" nil nil ,keystroke)))
12068       (let ((func (gnus-summary-make-marking-command-1
12069                    mark (car lway) lway name)))
12070         (setq func (eval func))
12071         (define-key map (nth 4 lway) func)))))
12072
12073 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12074   `(defun ,(intern
12075             (format "gnus-summary-put-mark-as-%s%s"
12076                     name (if (eq way 'nomove)
12077                              ""
12078                            (concat "-" (symbol-name way)))))
12079      (n)
12080      ,(format
12081        "Mark the current article as %s%s.
12082 If N, the prefix, then repeat N times.
12083 If N is negative, move in reverse order.
12084 The difference between N and the actual number of articles marked is
12085 returned."
12086        name (cadr lway))
12087      (interactive "p")
12088      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12089
12090 (defun gnus-summary-generic-mark (n mark move unread)
12091   "Mark N articles with MARK."
12092   (unless (eq major-mode 'gnus-summary-mode)
12093     (error "This command can only be used in the summary buffer"))
12094   (gnus-summary-show-thread)
12095   (let ((nummove
12096          (cond
12097           ((eq move 'next) 1)
12098           ((eq move 'prev) -1)
12099           (t 0))))
12100     (if (zerop nummove)
12101         (setq n 1)
12102       (when (< n 0)
12103         (setq n (abs n)
12104               nummove (* -1 nummove))))
12105     (while (and (> n 0)
12106                 (gnus-summary-mark-article nil mark)
12107                 (zerop (gnus-summary-next-subject nummove unread t)))
12108       (setq n (1- n)))
12109     (when (/= 0 n)
12110       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12111     (gnus-summary-recenter)
12112     (gnus-summary-position-point)
12113     (gnus-set-mode-line 'summary)
12114     n))
12115
12116 (defun gnus-summary-insert-articles (articles)
12117   (when (setq articles
12118               (gnus-sorted-difference articles
12119                                       (mapcar (lambda (h)
12120                                                 (mail-header-number h))
12121                                               gnus-newsgroup-headers)))
12122     (setq gnus-newsgroup-headers
12123           (gnus-merge 'list
12124                       gnus-newsgroup-headers
12125                       (gnus-fetch-headers articles)
12126                       'gnus-article-sort-by-number))
12127     ;; Suppress duplicates?
12128     (when gnus-suppress-duplicates
12129       (gnus-dup-suppress-articles))
12130
12131     ;; We might want to build some more threads first.
12132     (when (and gnus-fetch-old-headers
12133                (eq gnus-headers-retrieved-by 'nov))
12134       (if (eq gnus-fetch-old-headers 'invisible)
12135           (gnus-build-all-threads)
12136         (gnus-build-old-threads)))
12137     ;; Let the Gnus agent mark articles as read.
12138     (when gnus-agent
12139       (gnus-agent-get-undownloaded-list))
12140     ;; Remove list identifiers from subject
12141     (when gnus-list-identifiers
12142       (gnus-summary-remove-list-identifiers))
12143     ;; First and last article in this newsgroup.
12144     (when gnus-newsgroup-headers
12145       (setq gnus-newsgroup-begin
12146             (mail-header-number (car gnus-newsgroup-headers))
12147             gnus-newsgroup-end
12148             (mail-header-number
12149              (gnus-last-element gnus-newsgroup-headers))))
12150     (when gnus-use-scoring
12151       (gnus-possibly-score-headers))))
12152
12153 (defun gnus-summary-insert-old-articles (&optional all)
12154   "Insert all old articles in this group.
12155 If ALL is non-nil, already read articles become readable.
12156 If ALL is a number, fetch this number of articles."
12157   (interactive "P")
12158   (prog1
12159       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12160             older len)
12161         (setq older
12162               ;; Some nntp servers lie about their active range.  When
12163               ;; this happens, the active range can be in the millions.
12164               ;; Use a compressed range to avoid creating a huge list.
12165               (gnus-range-difference (list gnus-newsgroup-active) old))
12166         (setq len (gnus-range-length older))
12167         (cond
12168          ((null older) nil)
12169          ((numberp all)
12170           (if (< all len)
12171               (let ((older-range (nreverse older)))
12172                 (setq older nil)
12173
12174                 (while (> all 0)
12175                   (let* ((r (pop older-range))
12176                          (min (if (numberp r) r (car r)))
12177                          (max (if (numberp r) r (cdr r))))
12178                     (while (and (<= min max)
12179                                 (> all 0))
12180                       (push max older)
12181                       (setq all (1- all)
12182                             max (1- max))))))
12183             (setq older (gnus-uncompress-range older))))
12184          (all
12185           (setq older (gnus-uncompress-range older)))
12186          (t
12187           (when (and (numberp gnus-large-newsgroup)
12188                    (> len gnus-large-newsgroup))
12189               (let* ((cursor-in-echo-area nil)
12190                      (initial (gnus-parameter-large-newsgroup-initial
12191                                gnus-newsgroup-name))
12192                      (input
12193                       (read-string
12194                        (format
12195                         "How many articles from %s (%s %d): "
12196                         (gnus-limit-string
12197                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12198                         (if initial "max" "default")
12199                         len)
12200                        (if initial
12201                            (cons (number-to-string initial)
12202                                  0)))))
12203                 (unless (string-match "^[ \t]*$" input)
12204                   (setq all (string-to-number input))
12205                   (if (< all len)
12206                       (let ((older-range (nreverse older)))
12207                         (setq older nil)
12208
12209                         (while (> all 0)
12210                           (let* ((r (pop older-range))
12211                                  (min (if (numberp r) r (car r)))
12212                                  (max (if (numberp r) r (cdr r))))
12213                             (while (and (<= min max)
12214                                         (> all 0))
12215                               (push max older)
12216                               (setq all (1- all)
12217                                     max (1- max))))))))))
12218           (setq older (gnus-uncompress-range older))))
12219         (if (not older)
12220             (message "No old news.")
12221           (gnus-summary-insert-articles older)
12222           (gnus-summary-limit (gnus-sorted-nunion old older))))
12223     (gnus-summary-position-point)))
12224
12225 (defun gnus-summary-insert-new-articles ()
12226   "Insert all new articles in this group."
12227   (interactive)
12228   (prog1
12229       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12230             (old-active gnus-newsgroup-active)
12231             (nnmail-fetched-sources (list t))
12232             i new)
12233         (setq gnus-newsgroup-active
12234               (gnus-activate-group gnus-newsgroup-name 'scan))
12235         (setq i (cdr gnus-newsgroup-active))
12236         (while (> i (cdr old-active))
12237           (push i new)
12238           (decf i))
12239         (if (not new)
12240             (message "No gnus is bad news")
12241           (gnus-summary-insert-articles new)
12242           (setq gnus-newsgroup-unreads
12243                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12244           (gnus-summary-limit (gnus-sorted-nunion old new))))
12245     (gnus-summary-position-point)))
12246
12247 (gnus-summary-make-all-marking-commands)
12248
12249 (gnus-ems-redefine)
12250
12251 (provide 'gnus-sum)
12252
12253 (run-hooks 'gnus-sum-load-hook)
12254
12255 ;; Local Variables:
12256 ;; coding: iso-8859-1
12257 ;; End:
12258
12259 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12260 ;;; gnus-sum.el ends here