2006-04-23 Andreas Seltenreich <andreas+ding@gate450.dyndns.org> (tiny change)
[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   "Variable that says which function should be used to decode a string with encoded words.")
1056
1057 (defcustom gnus-extra-headers '(To Newsgroups)
1058   "*Extra headers to parse."
1059   :version "21.1"
1060   :group 'gnus-summary
1061   :type '(repeat symbol))
1062
1063 (defcustom gnus-ignored-from-addresses
1064   (and user-mail-address  
1065        (not (string= user-mail-address ""))
1066        (regexp-quote user-mail-address))
1067   "*Regexp of From headers that may be suppressed in favor of To headers."
1068   :version "21.1"
1069   :group 'gnus-summary
1070   :type 'regexp)
1071
1072 (defcustom gnus-summary-to-prefix "-> "
1073   "*String prefixed to the To field in the summary line when
1074 using `gnus-ignored-from-addresses'."
1075   :version "22.1"
1076   :group 'gnus-summary
1077   :type 'string)
1078
1079 (defcustom gnus-summary-newsgroup-prefix "=> "
1080   "*String prefixed to the Newsgroup field in the summary
1081 line when using `gnus-ignored-from-addresses'."
1082   :version "22.1"
1083   :group 'gnus-summary
1084   :type 'string)
1085
1086 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1087   "List of charsets that should be ignored.
1088 When these charsets are used in the \"charset\" parameter, the
1089 default charset will be used instead."
1090   :version "21.1"
1091   :type '(repeat symbol)
1092   :group 'gnus-charset)
1093
1094 (gnus-define-group-parameter
1095  ignored-charsets
1096  :type list
1097  :function-document
1098  "Return the ignored charsets of GROUP."
1099  :variable gnus-group-ignored-charsets-alist
1100  :variable-default
1101  '(("alt\\.chinese\\.text" iso-8859-1))
1102  :variable-document
1103  "Alist of regexps (to match group names) and charsets that should be ignored.
1104 When these charsets are used in the \"charset\" parameter, the
1105 default charset will be used instead."
1106  :variable-group gnus-charset
1107  :variable-type '(repeat (cons (regexp :tag "Group")
1108                                (repeat symbol)))
1109  :parameter-type '(choice :tag "Ignored charsets"
1110                           :value nil
1111                           (repeat (symbol)))
1112  :parameter-document       "\
1113 List of charsets that should be ignored.
1114
1115 When these charsets are used in the \"charset\" parameter, the
1116 default charset will be used instead.")
1117
1118 (defcustom gnus-group-highlight-words-alist nil
1119   "Alist of group regexps and highlight regexps.
1120 This variable uses the same syntax as `gnus-emphasis-alist'."
1121   :version "21.1"
1122   :type '(repeat (cons (regexp :tag "Group")
1123                        (repeat (list (regexp :tag "Highlight regexp")
1124                                      (number :tag "Group for entire word" 0)
1125                                      (number :tag "Group for displayed part" 0)
1126                                      (symbol :tag "Face"
1127                                              gnus-emphasis-highlight-words)))))
1128   :group 'gnus-summary-visual)
1129
1130 (defcustom gnus-summary-show-article-charset-alist
1131   nil
1132   "Alist of number and charset.
1133 The article will be shown with the charset corresponding to the
1134 numbered argument.
1135 For example: ((1 . cn-gb-2312) (2 . big5))."
1136   :version "21.1"
1137   :type '(repeat (cons (number :tag "Argument" 1)
1138                        (symbol :tag "Charset")))
1139   :group 'gnus-charset)
1140
1141 (defcustom gnus-preserve-marks t
1142   "Whether marks are preserved when moving, copying and respooling messages."
1143   :version "21.1"
1144   :type 'boolean
1145   :group 'gnus-summary-marks)
1146
1147 (defcustom gnus-alter-articles-to-read-function nil
1148   "Function to be called to alter the list of articles to be selected."
1149   :type '(choice (const nil) function)
1150   :group 'gnus-summary)
1151
1152 (defcustom gnus-orphan-score nil
1153   "*All orphans get this score added.  Set in the score file."
1154   :group 'gnus-score-default
1155   :type '(choice (const nil)
1156                  integer))
1157
1158 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1159   "*A regexp to match MIME parts when saving multiple parts of a
1160 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1161 This regexp will be used by default when prompting the user for which
1162 type of files to save."
1163   :group 'gnus-summary
1164   :type 'regexp)
1165
1166 (defcustom gnus-read-all-available-headers nil
1167   "Whether Gnus should parse all headers made available to it.
1168 This is mostly relevant for slow back ends where the user may
1169 wish to widen the summary buffer to include all headers
1170 that were fetched.  Say, for nnultimate groups."
1171   :version "22.1"
1172   :group 'gnus-summary
1173   :type '(choice boolean regexp))
1174
1175 (defcustom gnus-summary-muttprint-program "muttprint"
1176   "Command (and optional arguments) used to run Muttprint."
1177   :version "22.1"
1178   :group 'gnus-summary
1179   :type 'string)
1180
1181 (defcustom gnus-article-loose-mime t
1182   "If non-nil, don't require MIME-Version header.
1183 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1184 supply the MIME-Version header or deliberately strip it from the mail.
1185 If non-nil (the default), Gnus will treat some articles as MIME
1186 even if the MIME-Version header is missing."
1187   :version "22.1"
1188   :type 'boolean
1189   :group 'gnus-article-mime)
1190
1191 (defcustom gnus-article-emulate-mime t
1192   "If non-nil, use MIME emulation for uuencode and the like.
1193 This means that Gnus will search message bodies for text that look
1194 like uuencoded bits, yEncoded bits, and so on, and present that using
1195 the normal Gnus MIME machinery."
1196   :version "22.1"
1197   :type 'boolean
1198   :group 'gnus-article-mime)
1199
1200 ;;; Internal variables
1201
1202 (defvar gnus-summary-display-cache nil)
1203 (defvar gnus-article-mime-handles nil)
1204 (defvar gnus-article-decoded-p nil)
1205 (defvar gnus-article-charset nil)
1206 (defvar gnus-article-ignored-charsets nil)
1207 (defvar gnus-scores-exclude-files nil)
1208 (defvar gnus-page-broken nil)
1209
1210 (defvar gnus-original-article nil)
1211 (defvar gnus-article-internal-prepare-hook nil)
1212 (defvar gnus-newsgroup-process-stack nil)
1213
1214 (defvar gnus-thread-indent-array nil)
1215 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1216 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1217   "Function called to sort the articles within a thread after it has been gathered together.")
1218
1219 (defvar gnus-summary-save-parts-type-history nil)
1220 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1221
1222 ;; Avoid highlighting in kill files.
1223 (defvar gnus-summary-inhibit-highlight nil)
1224 (defvar gnus-newsgroup-selected-overlay nil)
1225 (defvar gnus-inhibit-limiting nil)
1226 (defvar gnus-newsgroup-adaptive-score-file nil)
1227 (defvar gnus-current-score-file nil)
1228 (defvar gnus-current-move-group nil)
1229 (defvar gnus-current-copy-group nil)
1230 (defvar gnus-current-crosspost-group nil)
1231 (defvar gnus-newsgroup-display nil)
1232
1233 (defvar gnus-newsgroup-dependencies nil)
1234 (defvar gnus-newsgroup-adaptive nil)
1235 (defvar gnus-summary-display-article-function nil)
1236 (defvar gnus-summary-highlight-line-function nil
1237   "Function called after highlighting a summary line.")
1238
1239 (defvar gnus-summary-line-format-alist
1240   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1241     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1242     (?s gnus-tmp-subject-or-nil ?s)
1243     (?n gnus-tmp-name ?s)
1244     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1245         ?s)
1246     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1247             gnus-tmp-from) ?s)
1248     (?F gnus-tmp-from ?s)
1249     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1250     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1251     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1252     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1253     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1254     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1255     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1256     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1257     (?L gnus-tmp-lines ?s)
1258     (?O gnus-tmp-downloaded ?c)
1259     (?I gnus-tmp-indentation ?s)
1260     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1261     (?R gnus-tmp-replied ?c)
1262     (?\[ gnus-tmp-opening-bracket ?c)
1263     (?\] gnus-tmp-closing-bracket ?c)
1264     (?\> (make-string gnus-tmp-level ? ) ?s)
1265     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1266     (?i gnus-tmp-score ?d)
1267     (?z gnus-tmp-score-char ?c)
1268     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1269     (?U gnus-tmp-unread ?c)
1270     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1271         ?s)
1272     (?t (gnus-summary-number-of-articles-in-thread
1273          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1274         ?d)
1275     (?e (gnus-summary-number-of-articles-in-thread
1276          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1277         ?c)
1278     (?u gnus-tmp-user-defined ?s)
1279     (?P (gnus-pick-line-number) ?d)
1280     (?B gnus-tmp-thread-tree-header-string ?s)
1281     (user-date (gnus-user-date
1282                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1283   "An alist of format specifications that can appear in summary lines.
1284 These are paired with what variables they correspond with, along with
1285 the type of the variable (string, integer, character, etc).")
1286
1287 (defvar gnus-summary-dummy-line-format-alist
1288   `((?S gnus-tmp-subject ?s)
1289     (?N gnus-tmp-number ?d)
1290     (?u gnus-tmp-user-defined ?s)))
1291
1292 (defvar gnus-summary-mode-line-format-alist
1293   `((?G gnus-tmp-group-name ?s)
1294     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1295     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1296     (?A gnus-tmp-article-number ?d)
1297     (?Z gnus-tmp-unread-and-unselected ?s)
1298     (?V gnus-version ?s)
1299     (?U gnus-tmp-unread-and-unticked ?d)
1300     (?S gnus-tmp-subject ?s)
1301     (?e gnus-tmp-unselected ?d)
1302     (?u gnus-tmp-user-defined ?s)
1303     (?d (length gnus-newsgroup-dormant) ?d)
1304     (?t (length gnus-newsgroup-marked) ?d)
1305     (?h (length gnus-newsgroup-spam-marked) ?d)
1306     (?r (length gnus-newsgroup-reads) ?d)
1307     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1308     (?E gnus-newsgroup-expunged-tally ?d)
1309     (?s (gnus-current-score-file-nondirectory) ?s)))
1310
1311 (defvar gnus-last-search-regexp nil
1312   "Default regexp for article search command.")
1313
1314 (defvar gnus-last-shell-command nil
1315   "Default shell command on article.")
1316
1317 (defvar gnus-newsgroup-agentized nil
1318   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1319 (defvar gnus-newsgroup-begin nil)
1320 (defvar gnus-newsgroup-end nil)
1321 (defvar gnus-newsgroup-last-rmail nil)
1322 (defvar gnus-newsgroup-last-mail nil)
1323 (defvar gnus-newsgroup-last-folder nil)
1324 (defvar gnus-newsgroup-last-file nil)
1325 (defvar gnus-newsgroup-auto-expire nil)
1326 (defvar gnus-newsgroup-active nil)
1327
1328 (defvar gnus-newsgroup-data nil)
1329 (defvar gnus-newsgroup-data-reverse nil)
1330 (defvar gnus-newsgroup-limit nil)
1331 (defvar gnus-newsgroup-limits nil)
1332 (defvar gnus-summary-use-undownloaded-faces nil)
1333
1334 (defvar gnus-newsgroup-unreads nil
1335   "Sorted list of unread articles in the current newsgroup.")
1336
1337 (defvar gnus-newsgroup-unselected nil
1338   "Sorted list of unselected unread articles in the current newsgroup.")
1339
1340 (defvar gnus-newsgroup-reads nil
1341   "Alist of read articles and article marks in the current newsgroup.")
1342
1343 (defvar gnus-newsgroup-expunged-tally nil)
1344
1345 (defvar gnus-newsgroup-marked nil
1346   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1347
1348 (defvar gnus-newsgroup-spam-marked nil
1349   "List of ranges of articles that have been marked as spam.")
1350
1351 (defvar gnus-newsgroup-killed nil
1352   "List of ranges of articles that have been through the scoring process.")
1353
1354 (defvar gnus-newsgroup-cached nil
1355   "Sorted list of articles that come from the article cache.")
1356
1357 (defvar gnus-newsgroup-saved nil
1358   "List of articles that have been saved.")
1359
1360 (defvar gnus-newsgroup-kill-headers nil)
1361
1362 (defvar gnus-newsgroup-replied nil
1363   "List of articles that have been replied to in the current newsgroup.")
1364
1365 (defvar gnus-newsgroup-forwarded nil
1366   "List of articles that have been forwarded in the current newsgroup.")
1367
1368 (defvar gnus-newsgroup-recent nil
1369   "List of articles that have are recent in the current newsgroup.")
1370
1371 (defvar gnus-newsgroup-expirable nil
1372   "Sorted list of articles in the current newsgroup that can be expired.")
1373
1374 (defvar gnus-newsgroup-processable nil
1375   "List of articles in the current newsgroup that can be processed.")
1376
1377 (defvar gnus-newsgroup-downloadable nil
1378   "Sorted list of articles in the current newsgroup that can be processed.")
1379
1380 (defvar gnus-newsgroup-unfetched nil
1381   "Sorted list of articles in the current newsgroup whose headers have
1382 not been fetched into the agent.
1383
1384 This list will always be a subset of gnus-newsgroup-undownloaded.")
1385
1386 (defvar gnus-newsgroup-undownloaded nil
1387   "List of articles in the current newsgroup that haven't been downloaded.")
1388
1389 (defvar gnus-newsgroup-unsendable nil
1390   "List of articles in the current newsgroup that won't be sent.")
1391
1392 (defvar gnus-newsgroup-bookmarks nil
1393   "List of articles in the current newsgroup that have bookmarks.")
1394
1395 (defvar gnus-newsgroup-dormant nil
1396   "Sorted list of dormant articles in the current newsgroup.")
1397
1398 (defvar gnus-newsgroup-unseen nil
1399   "List of unseen articles in the current newsgroup.")
1400
1401 (defvar gnus-newsgroup-seen nil
1402   "Range of seen articles in the current newsgroup.")
1403
1404 (defvar gnus-newsgroup-articles nil
1405   "List of articles in the current newsgroup.")
1406
1407 (defvar gnus-newsgroup-scored nil
1408   "List of scored articles in the current newsgroup.")
1409
1410 (defvar gnus-newsgroup-headers nil
1411   "List of article headers in the current newsgroup.")
1412
1413 (defvar gnus-newsgroup-threads nil)
1414
1415 (defvar gnus-newsgroup-prepared nil
1416   "Whether the current group has been prepared properly.")
1417
1418 (defvar gnus-newsgroup-ancient nil
1419   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1420
1421 (defvar gnus-newsgroup-sparse nil)
1422
1423 (defvar gnus-current-article nil)
1424 (defvar gnus-article-current nil)
1425 (defvar gnus-current-headers nil)
1426 (defvar gnus-have-all-headers nil)
1427 (defvar gnus-last-article nil)
1428 (defvar gnus-newsgroup-history nil)
1429 (defvar gnus-newsgroup-charset nil)
1430 (defvar gnus-newsgroup-ephemeral-charset nil)
1431 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1432
1433 (defvar gnus-article-before-search nil)
1434
1435 (defvar gnus-summary-local-variables
1436   '(gnus-newsgroup-name
1437     gnus-newsgroup-begin gnus-newsgroup-end
1438     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1439     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1440     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1441     gnus-newsgroup-unselected gnus-newsgroup-marked
1442     gnus-newsgroup-spam-marked
1443     gnus-newsgroup-reads gnus-newsgroup-saved
1444     gnus-newsgroup-replied gnus-newsgroup-forwarded
1445     gnus-newsgroup-recent
1446     gnus-newsgroup-expirable
1447     gnus-newsgroup-processable gnus-newsgroup-killed
1448     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1449     gnus-newsgroup-unfetched
1450     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1451     gnus-newsgroup-seen gnus-newsgroup-articles
1452     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1453     gnus-newsgroup-headers gnus-newsgroup-threads
1454     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1455     gnus-current-article gnus-current-headers gnus-have-all-headers
1456     gnus-last-article gnus-article-internal-prepare-hook
1457     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1458     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1459     gnus-thread-expunge-below
1460     gnus-score-alist gnus-current-score-file
1461     (gnus-summary-expunge-below . global)
1462     (gnus-summary-mark-below . global)
1463     (gnus-orphan-score . global)
1464     gnus-newsgroup-active gnus-scores-exclude-files
1465     gnus-newsgroup-history gnus-newsgroup-ancient
1466     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1467     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1468     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1469     (gnus-newsgroup-expunged-tally . 0)
1470     gnus-cache-removable-articles gnus-newsgroup-cached
1471     gnus-newsgroup-data gnus-newsgroup-data-reverse
1472     gnus-newsgroup-limit gnus-newsgroup-limits
1473     gnus-newsgroup-charset gnus-newsgroup-display
1474     gnus-summary-use-undownloaded-faces)
1475   "Variables that are buffer-local to the summary buffers.")
1476
1477 (defvar gnus-newsgroup-variables nil
1478   "A list of variables that have separate values in different newsgroups.
1479 A list of newsgroup (summary buffer) local variables, or cons of
1480 variables and their default expressions to be evalled (when the default
1481 values are not nil), that should be made global while the summary buffer
1482 is active.
1483
1484 Note: The default expressions will be evaluated (using function `eval')
1485 before assignment to the local variable rather than just assigned to it.
1486 If the default expression is the symbol `global', that symbol will not
1487 be evaluated but the global value of the local variable will be used
1488 instead.
1489
1490 These variables can be used to set variables in the group parameters
1491 while still allowing them to affect operations done in other buffers.
1492 For example:
1493
1494 \(setq gnus-newsgroup-variables
1495      '(message-use-followup-to
1496        (gnus-visible-headers .
1497          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1498 ")
1499
1500 ;; Byte-compiler warning.
1501 (eval-when-compile
1502   ;; Bind features so that require will believe that gnus-sum has
1503   ;; already been loaded (avoids infinite recursion)
1504   (let ((features (cons 'gnus-sum features)))
1505     ;; Several of the declarations in gnus-sum are needed to load the
1506     ;; following files. Right now, these definitions have been
1507     ;; compiled but not defined (evaluated).  We could either do a
1508     ;; eval-and-compile about all of the declarations or evaluate the
1509     ;; source file.
1510     (if (boundp 'gnus-newsgroup-variables)
1511         nil
1512       (load "gnus-sum.el" t t t))
1513     (require 'gnus)
1514     (require 'gnus-art)))
1515
1516 ;; MIME stuff.
1517
1518 (defvar gnus-decode-encoded-word-methods
1519   '(mail-decode-encoded-word-string)
1520   "List of methods used to decode encoded words.
1521
1522 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1523 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1524 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1525 whose names match REGEXP.
1526
1527 For example:
1528 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1529  mail-decode-encoded-word-string
1530  (\"chinese\" . rfc1843-decode-string))")
1531
1532 (defvar gnus-decode-encoded-word-methods-cache nil)
1533
1534 (defun gnus-multi-decode-encoded-word-string (string)
1535   "Apply the functions from `gnus-encoded-word-methods' that match."
1536   (unless (and gnus-decode-encoded-word-methods-cache
1537                (eq gnus-newsgroup-name
1538                    (car gnus-decode-encoded-word-methods-cache)))
1539     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1540     (mapcar (lambda (x)
1541               (if (symbolp x)
1542                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1543                 (if (and gnus-newsgroup-name
1544                          (string-match (car x) gnus-newsgroup-name))
1545                     (nconc gnus-decode-encoded-word-methods-cache
1546                            (list (cdr x))))))
1547             gnus-decode-encoded-word-methods))
1548   (let ((xlist gnus-decode-encoded-word-methods-cache))
1549     (pop xlist)
1550     (while xlist
1551       (setq string (funcall (pop xlist) string))))
1552   string)
1553
1554 ;; Subject simplification.
1555
1556 (defun gnus-simplify-whitespace (str)
1557   "Remove excessive whitespace from STR."
1558   ;; Multiple spaces.
1559   (while (string-match "[ \t][ \t]+" str)
1560     (setq str (concat (substring str 0 (match-beginning 0))
1561                         " "
1562                         (substring str (match-end 0)))))
1563   ;; Leading spaces.
1564   (when (string-match "^[ \t]+" str)
1565     (setq str (substring str (match-end 0))))
1566   ;; Trailing spaces.
1567   (when (string-match "[ \t]+$" str)
1568     (setq str (substring str 0 (match-beginning 0))))
1569   str)
1570
1571 (defun gnus-simplify-all-whitespace (str)
1572   "Remove all whitespace from STR."
1573   (while (string-match "[ \t\n]+" str)
1574     (setq str (replace-match "" nil nil str)))
1575   str)
1576
1577 (defsubst gnus-simplify-subject-re (subject)
1578   "Remove \"Re:\" from subject lines."
1579   (if (string-match message-subject-re-regexp subject)
1580       (substring subject (match-end 0))
1581     subject))
1582
1583 (defun gnus-simplify-subject (subject &optional re-only)
1584   "Remove `Re:' and words in parentheses.
1585 If RE-ONLY is non-nil, strip leading `Re:'s only."
1586   (let ((case-fold-search t))           ;Ignore case.
1587     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1588     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1589       (setq subject (substring subject (match-end 0))))
1590     ;; Remove uninteresting prefixes.
1591     (when (and (not re-only)
1592                gnus-simplify-ignored-prefixes
1593                (string-match gnus-simplify-ignored-prefixes subject))
1594       (setq subject (substring subject (match-end 0))))
1595     ;; Remove words in parentheses from end.
1596     (unless re-only
1597       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1598         (setq subject (substring subject 0 (match-beginning 0)))))
1599     ;; Return subject string.
1600     subject))
1601
1602 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1603 ;; all whitespace.
1604 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1605   (goto-char (point-min))
1606   (while (re-search-forward regexp nil t)
1607     (replace-match (or newtext ""))))
1608
1609 (defun gnus-simplify-buffer-fuzzy ()
1610   "Simplify string in the buffer fuzzily.
1611 The string in the accessible portion of the current buffer is simplified.
1612 It is assumed to be a single-line subject.
1613 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1614 matter is removed.  Additional things can be deleted by setting
1615 `gnus-simplify-subject-fuzzy-regexp'."
1616   (let ((case-fold-search t)
1617         (modified-tick))
1618     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1619
1620     (while (not (eq modified-tick (buffer-modified-tick)))
1621       (setq modified-tick (buffer-modified-tick))
1622       (cond
1623        ((listp gnus-simplify-subject-fuzzy-regexp)
1624         (mapcar 'gnus-simplify-buffer-fuzzy-step
1625                 gnus-simplify-subject-fuzzy-regexp))
1626        (gnus-simplify-subject-fuzzy-regexp
1627         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1628       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1629       (gnus-simplify-buffer-fuzzy-step
1630        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1631       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1632
1633     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1634     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1635     (gnus-simplify-buffer-fuzzy-step " $")
1636     (gnus-simplify-buffer-fuzzy-step "^ +")))
1637
1638 (defun gnus-simplify-subject-fuzzy (subject)
1639   "Simplify a subject string fuzzily.
1640 See `gnus-simplify-buffer-fuzzy' for details."
1641   (save-excursion
1642     (gnus-set-work-buffer)
1643     (let ((case-fold-search t))
1644       ;; Remove uninteresting prefixes.
1645       (when (and gnus-simplify-ignored-prefixes
1646                  (string-match gnus-simplify-ignored-prefixes subject))
1647         (setq subject (substring subject (match-end 0))))
1648       (insert subject)
1649       (inline (gnus-simplify-buffer-fuzzy))
1650       (buffer-string))))
1651
1652 (defsubst gnus-simplify-subject-fully (subject)
1653   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1654   (cond
1655    (gnus-simplify-subject-functions
1656     (gnus-map-function gnus-simplify-subject-functions subject))
1657    ((null gnus-summary-gather-subject-limit)
1658     (gnus-simplify-subject-re subject))
1659    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1660     (gnus-simplify-subject-fuzzy subject))
1661    ((numberp gnus-summary-gather-subject-limit)
1662     (gnus-limit-string (gnus-simplify-subject-re subject)
1663                        gnus-summary-gather-subject-limit))
1664    (t
1665     subject)))
1666
1667 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1668   "Check whether two subjects are equal.
1669 If optional argument SIMPLE-FIRST is t, first argument is already
1670 simplified."
1671   (cond
1672    ((null simple-first)
1673     (equal (gnus-simplify-subject-fully s1)
1674            (gnus-simplify-subject-fully s2)))
1675    (t
1676     (equal s1
1677            (gnus-simplify-subject-fully s2)))))
1678
1679 (defun gnus-summary-bubble-group ()
1680   "Increase the score of the current group.
1681 This is a handy function to add to `gnus-summary-exit-hook' to
1682 increase the score of each group you read."
1683   (gnus-group-add-score gnus-newsgroup-name))
1684
1685 \f
1686 ;;;
1687 ;;; Gnus summary mode
1688 ;;;
1689
1690 (put 'gnus-summary-mode 'mode-class 'special)
1691
1692 (defvar gnus-article-commands-menu)
1693
1694 ;; Non-orthogonal keys
1695
1696 (gnus-define-keys gnus-summary-mode-map
1697   " " gnus-summary-next-page
1698   "\177" gnus-summary-prev-page
1699   [delete] gnus-summary-prev-page
1700   [backspace] gnus-summary-prev-page
1701   "\r" gnus-summary-scroll-up
1702   "\M-\r" gnus-summary-scroll-down
1703   "n" gnus-summary-next-unread-article
1704   "p" gnus-summary-prev-unread-article
1705   "N" gnus-summary-next-article
1706   "P" gnus-summary-prev-article
1707   "\M-\C-n" gnus-summary-next-same-subject
1708   "\M-\C-p" gnus-summary-prev-same-subject
1709   "\M-n" gnus-summary-next-unread-subject
1710   "\M-p" gnus-summary-prev-unread-subject
1711   "." gnus-summary-first-unread-article
1712   "," gnus-summary-best-unread-article
1713   "\M-s" gnus-summary-search-article-forward
1714   "\M-r" gnus-summary-search-article-backward
1715   "\M-S" gnus-summary-repeat-search-article-forward
1716   "\M-R" gnus-summary-repeat-search-article-backward
1717   "<" gnus-summary-beginning-of-article
1718   ">" gnus-summary-end-of-article
1719   "j" gnus-summary-goto-article
1720   "^" gnus-summary-refer-parent-article
1721   "\M-^" gnus-summary-refer-article
1722   "u" gnus-summary-tick-article-forward
1723   "!" gnus-summary-tick-article-forward
1724   "U" gnus-summary-tick-article-backward
1725   "d" gnus-summary-mark-as-read-forward
1726   "D" gnus-summary-mark-as-read-backward
1727   "E" gnus-summary-mark-as-expirable
1728   "\M-u" gnus-summary-clear-mark-forward
1729   "\M-U" gnus-summary-clear-mark-backward
1730   "k" gnus-summary-kill-same-subject-and-select
1731   "\C-k" gnus-summary-kill-same-subject
1732   "\M-\C-k" gnus-summary-kill-thread
1733   "\M-\C-l" gnus-summary-lower-thread
1734   "e" gnus-summary-edit-article
1735   "#" gnus-summary-mark-as-processable
1736   "\M-#" gnus-summary-unmark-as-processable
1737   "\M-\C-t" gnus-summary-toggle-threads
1738   "\M-\C-s" gnus-summary-show-thread
1739   "\M-\C-h" gnus-summary-hide-thread
1740   "\M-\C-f" gnus-summary-next-thread
1741   "\M-\C-b" gnus-summary-prev-thread
1742   [(meta down)] gnus-summary-next-thread
1743   [(meta up)] gnus-summary-prev-thread
1744   "\M-\C-u" gnus-summary-up-thread
1745   "\M-\C-d" gnus-summary-down-thread
1746   "&" gnus-summary-execute-command
1747   "c" gnus-summary-catchup-and-exit
1748   "\C-w" gnus-summary-mark-region-as-read
1749   "\C-t" gnus-summary-toggle-truncation
1750   "?" gnus-summary-mark-as-dormant
1751   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1752   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1753   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1754   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1755   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1756   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1757   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1758   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1759   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1760   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1761   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1762   "=" gnus-summary-expand-window
1763   "\C-x\C-s" gnus-summary-reselect-current-group
1764   "\M-g" gnus-summary-rescan-group
1765   "w" gnus-summary-stop-page-breaking
1766   "\C-c\C-r" gnus-summary-caesar-message
1767   "f" gnus-summary-followup
1768   "F" gnus-summary-followup-with-original
1769   "C" gnus-summary-cancel-article
1770   "r" gnus-summary-reply
1771   "R" gnus-summary-reply-with-original
1772   "\C-c\C-f" gnus-summary-mail-forward
1773   "o" gnus-summary-save-article
1774   "\C-o" gnus-summary-save-article-mail
1775   "|" gnus-summary-pipe-output
1776   "\M-k" gnus-summary-edit-local-kill
1777   "\M-K" gnus-summary-edit-global-kill
1778   ;; "V" gnus-version
1779   "\C-c\C-d" gnus-summary-describe-group
1780   "q" gnus-summary-exit
1781   "Q" gnus-summary-exit-no-update
1782   "\C-c\C-i" gnus-info-find-node
1783   gnus-mouse-2 gnus-mouse-pick-article
1784   [follow-link] mouse-face
1785   "m" gnus-summary-mail-other-window
1786   "a" gnus-summary-post-news
1787   "i" gnus-summary-news-other-window
1788   "x" gnus-summary-limit-to-unread
1789   "s" gnus-summary-isearch-article
1790   "t" gnus-summary-toggle-header
1791   "g" gnus-summary-show-article
1792   "l" gnus-summary-goto-last-article
1793   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1794   "\C-d" gnus-summary-enter-digest-group
1795   "\M-\C-d" gnus-summary-read-document
1796   "\M-\C-e" gnus-summary-edit-parameters
1797   "\M-\C-a" gnus-summary-customize-parameters
1798   "\C-c\C-b" gnus-bug
1799   "*" gnus-cache-enter-article
1800   "\M-*" gnus-cache-remove-article
1801   "\M-&" gnus-summary-universal-argument
1802   "\C-l" gnus-recenter
1803   "I" gnus-summary-increase-score
1804   "L" gnus-summary-lower-score
1805   "\M-i" gnus-symbolic-argument
1806   "h" gnus-summary-select-article-buffer
1807
1808   "b" gnus-article-view-part
1809   "\M-t" gnus-summary-toggle-display-buttonized
1810
1811   "V" gnus-summary-score-map
1812   "X" gnus-uu-extract-map
1813   "S" gnus-summary-send-map)
1814
1815 ;; Sort of orthogonal keymap
1816 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1817   "t" gnus-summary-tick-article-forward
1818   "!" gnus-summary-tick-article-forward
1819   "d" gnus-summary-mark-as-read-forward
1820   "r" gnus-summary-mark-as-read-forward
1821   "c" gnus-summary-clear-mark-forward
1822   " " gnus-summary-clear-mark-forward
1823   "e" gnus-summary-mark-as-expirable
1824   "x" gnus-summary-mark-as-expirable
1825   "?" gnus-summary-mark-as-dormant
1826   "b" gnus-summary-set-bookmark
1827   "B" gnus-summary-remove-bookmark
1828   "#" gnus-summary-mark-as-processable
1829   "\M-#" gnus-summary-unmark-as-processable
1830   "S" gnus-summary-limit-include-expunged
1831   "C" gnus-summary-catchup
1832   "H" gnus-summary-catchup-to-here
1833   "h" gnus-summary-catchup-from-here
1834   "\C-c" gnus-summary-catchup-all
1835   "k" gnus-summary-kill-same-subject-and-select
1836   "K" gnus-summary-kill-same-subject
1837   "P" gnus-uu-mark-map)
1838
1839 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1840   "c" gnus-summary-clear-above
1841   "u" gnus-summary-tick-above
1842   "m" gnus-summary-mark-above
1843   "k" gnus-summary-kill-below)
1844
1845 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1846   "/" gnus-summary-limit-to-subject
1847   "n" gnus-summary-limit-to-articles
1848   "b" gnus-summary-limit-to-bodies
1849   "w" gnus-summary-pop-limit
1850   "s" gnus-summary-limit-to-subject
1851   "a" gnus-summary-limit-to-author
1852   "u" gnus-summary-limit-to-unread
1853   "m" gnus-summary-limit-to-marks
1854   "M" gnus-summary-limit-exclude-marks
1855   "v" gnus-summary-limit-to-score
1856   "*" gnus-summary-limit-include-cached
1857   "D" gnus-summary-limit-include-dormant
1858   "T" gnus-summary-limit-include-thread
1859   "d" gnus-summary-limit-exclude-dormant
1860   "t" gnus-summary-limit-to-age
1861   "." gnus-summary-limit-to-unseen
1862   "x" gnus-summary-limit-to-extra
1863   "p" gnus-summary-limit-to-display-predicate
1864   "E" gnus-summary-limit-include-expunged
1865   "c" gnus-summary-limit-exclude-childless-dormant
1866   "C" gnus-summary-limit-mark-excluded-as-read
1867   "o" gnus-summary-insert-old-articles
1868   "N" gnus-summary-insert-new-articles
1869   "r" gnus-summary-limit-to-replied
1870   "R" gnus-summary-limit-to-recipient)
1871
1872 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1873   "n" gnus-summary-next-unread-article
1874   "p" gnus-summary-prev-unread-article
1875   "N" gnus-summary-next-article
1876   "P" gnus-summary-prev-article
1877   "\C-n" gnus-summary-next-same-subject
1878   "\C-p" gnus-summary-prev-same-subject
1879   "\M-n" gnus-summary-next-unread-subject
1880   "\M-p" gnus-summary-prev-unread-subject
1881   "f" gnus-summary-first-unread-article
1882   "b" gnus-summary-best-unread-article
1883   "j" gnus-summary-goto-article
1884   "g" gnus-summary-goto-subject
1885   "l" gnus-summary-goto-last-article
1886   "o" gnus-summary-pop-article)
1887
1888 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1889   "k" gnus-summary-kill-thread
1890   "l" gnus-summary-lower-thread
1891   "i" gnus-summary-raise-thread
1892   "T" gnus-summary-toggle-threads
1893   "t" gnus-summary-rethread-current
1894   "^" gnus-summary-reparent-thread
1895   "\M-^" gnus-summary-reparent-children
1896   "s" gnus-summary-show-thread
1897   "S" gnus-summary-show-all-threads
1898   "h" gnus-summary-hide-thread
1899   "H" gnus-summary-hide-all-threads
1900   "n" gnus-summary-next-thread
1901   "p" gnus-summary-prev-thread
1902   "u" gnus-summary-up-thread
1903   "o" gnus-summary-top-thread
1904   "d" gnus-summary-down-thread
1905   "#" gnus-uu-mark-thread
1906   "\M-#" gnus-uu-unmark-thread)
1907
1908 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1909   "g" gnus-summary-prepare
1910   "c" gnus-summary-insert-cached-articles
1911   "d" gnus-summary-insert-dormant-articles)
1912
1913 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1914   "c" gnus-summary-catchup-and-exit
1915   "C" gnus-summary-catchup-all-and-exit
1916   "E" gnus-summary-exit-no-update
1917   "Q" gnus-summary-exit
1918   "Z" gnus-summary-exit
1919   "n" gnus-summary-catchup-and-goto-next-group
1920   "p" gnus-summary-catchup-and-goto-prev-group
1921   "R" gnus-summary-reselect-current-group
1922   "G" gnus-summary-rescan-group
1923   "N" gnus-summary-next-group
1924   "s" gnus-summary-save-newsrc
1925   "P" gnus-summary-prev-group)
1926
1927 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1928   " " gnus-summary-next-page
1929   "n" gnus-summary-next-page
1930   "\177" gnus-summary-prev-page
1931   [delete] gnus-summary-prev-page
1932   "p" gnus-summary-prev-page
1933   "\r" gnus-summary-scroll-up
1934   "\M-\r" gnus-summary-scroll-down
1935   "<" gnus-summary-beginning-of-article
1936   ">" gnus-summary-end-of-article
1937   "b" gnus-summary-beginning-of-article
1938   "e" gnus-summary-end-of-article
1939   "^" gnus-summary-refer-parent-article
1940   "r" gnus-summary-refer-parent-article
1941   "D" gnus-summary-enter-digest-group
1942   "R" gnus-summary-refer-references
1943   "T" gnus-summary-refer-thread
1944   "g" gnus-summary-show-article
1945   "s" gnus-summary-isearch-article
1946   "P" gnus-summary-print-article
1947   "M" gnus-mailing-list-insinuate
1948   "t" gnus-article-babel)
1949
1950 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1951   "b" gnus-article-add-buttons
1952   "B" gnus-article-add-buttons-to-head
1953   "o" gnus-article-treat-overstrike
1954   "e" gnus-article-emphasize
1955   "w" gnus-article-fill-cited-article
1956   "Q" gnus-article-fill-long-lines
1957   "C" gnus-article-capitalize-sentences
1958   "c" gnus-article-remove-cr
1959   "q" gnus-article-de-quoted-unreadable
1960   "6" gnus-article-de-base64-unreadable
1961   "Z" gnus-article-decode-HZ
1962   "A" gnus-article-treat-ansi-sequences
1963   "h" gnus-article-wash-html
1964   "u" gnus-article-unsplit-urls
1965   "s" gnus-summary-force-verify-and-decrypt
1966   "f" gnus-article-display-x-face
1967   "l" gnus-summary-stop-page-breaking
1968   "r" gnus-summary-caesar-message
1969   "m" gnus-summary-morse-message
1970   "t" gnus-summary-toggle-header
1971   "g" gnus-treat-smiley
1972   "v" gnus-summary-verbose-headers
1973   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1974   "p" gnus-article-verify-x-pgp-sig
1975   "d" gnus-article-treat-dumbquotes
1976   "i" gnus-summary-idna-message)
1977
1978 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1979   ;; mnemonic: deuglif*Y*
1980   "u" gnus-article-outlook-unwrap-lines
1981   "a" gnus-article-outlook-repair-attribution
1982   "c" gnus-article-outlook-rearrange-citation
1983   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1984
1985 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1986   "a" gnus-article-hide
1987   "h" gnus-article-hide-headers
1988   "b" gnus-article-hide-boring-headers
1989   "s" gnus-article-hide-signature
1990   "c" gnus-article-hide-citation
1991   "C" gnus-article-hide-citation-in-followups
1992   "l" gnus-article-hide-list-identifiers
1993   "B" gnus-article-strip-banner
1994   "P" gnus-article-hide-pem
1995   "\C-c" gnus-article-hide-citation-maybe)
1996
1997 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1998   "a" gnus-article-highlight
1999   "h" gnus-article-highlight-headers
2000   "c" gnus-article-highlight-citation
2001   "s" gnus-article-highlight-signature)
2002
2003 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2004   "f" gnus-article-treat-fold-headers
2005   "u" gnus-article-treat-unfold-headers
2006   "n" gnus-article-treat-fold-newsgroups)
2007
2008 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2009   "x" gnus-article-display-x-face
2010   "d" gnus-article-display-face
2011   "s" gnus-treat-smiley
2012   "D" gnus-article-remove-images
2013   "f" gnus-treat-from-picon
2014   "m" gnus-treat-mail-picon
2015   "n" gnus-treat-newsgroups-picon)
2016
2017 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2018   "w" gnus-article-decode-mime-words
2019   "c" gnus-article-decode-charset
2020   "v" gnus-mime-view-all-parts
2021   "b" gnus-article-view-part)
2022
2023 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2024   "z" gnus-article-date-ut
2025   "u" gnus-article-date-ut
2026   "l" gnus-article-date-local
2027   "p" gnus-article-date-english
2028   "e" gnus-article-date-lapsed
2029   "o" gnus-article-date-original
2030   "i" gnus-article-date-iso8601
2031   "s" gnus-article-date-user)
2032
2033 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2034   "t" gnus-article-remove-trailing-blank-lines
2035   "l" gnus-article-strip-leading-blank-lines
2036   "m" gnus-article-strip-multiple-blank-lines
2037   "a" gnus-article-strip-blank-lines
2038   "A" gnus-article-strip-all-blank-lines
2039   "s" gnus-article-strip-leading-space
2040   "e" gnus-article-strip-trailing-space
2041   "w" gnus-article-remove-leading-whitespace)
2042
2043 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2044   "v" gnus-version
2045   "f" gnus-summary-fetch-faq
2046   "d" gnus-summary-describe-group
2047   "h" gnus-summary-describe-briefly
2048   "i" gnus-info-find-node
2049   "c" gnus-group-fetch-charter
2050   "C" gnus-group-fetch-control)
2051
2052 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2053   "e" gnus-summary-expire-articles
2054   "\M-\C-e" gnus-summary-expire-articles-now
2055   "\177" gnus-summary-delete-article
2056   [delete] gnus-summary-delete-article
2057   [backspace] gnus-summary-delete-article
2058   "m" gnus-summary-move-article
2059   "r" gnus-summary-respool-article
2060   "w" gnus-summary-edit-article
2061   "c" gnus-summary-copy-article
2062   "B" gnus-summary-crosspost-article
2063   "q" gnus-summary-respool-query
2064   "t" gnus-summary-respool-trace
2065   "i" gnus-summary-import-article
2066   "I" gnus-summary-create-article
2067   "p" gnus-summary-article-posted-p)
2068
2069 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2070   "o" gnus-summary-save-article
2071   "m" gnus-summary-save-article-mail
2072   "F" gnus-summary-write-article-file
2073   "r" gnus-summary-save-article-rmail
2074   "f" gnus-summary-save-article-file
2075   "b" gnus-summary-save-article-body-file
2076   "h" gnus-summary-save-article-folder
2077   "v" gnus-summary-save-article-vm
2078   "p" gnus-summary-pipe-output
2079   "P" gnus-summary-muttprint
2080   "s" gnus-soup-add-article)
2081
2082 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2083   "b" gnus-summary-display-buttonized
2084   "m" gnus-summary-repair-multipart
2085   "v" gnus-article-view-part
2086   "o" gnus-article-save-part
2087   "O" gnus-article-save-part-and-strip
2088   "r" gnus-article-replace-part
2089   "d" gnus-article-delete-part
2090   "t" gnus-article-view-part-as-type
2091   "j" gnus-article-jump-to-part
2092   "c" gnus-article-copy-part
2093   "C" gnus-article-view-part-as-charset
2094   "e" gnus-article-view-part-externally
2095   "E" gnus-article-encrypt-body
2096   "i" gnus-article-inline-part
2097   "|" gnus-article-pipe-part)
2098
2099 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2100   "p" gnus-summary-mark-as-processable
2101   "u" gnus-summary-unmark-as-processable
2102   "U" gnus-summary-unmark-all-processable
2103   "v" gnus-uu-mark-over
2104   "s" gnus-uu-mark-series
2105   "r" gnus-uu-mark-region
2106   "g" gnus-uu-unmark-region
2107   "R" gnus-uu-mark-by-regexp
2108   "G" gnus-uu-unmark-by-regexp
2109   "t" gnus-uu-mark-thread
2110   "T" gnus-uu-unmark-thread
2111   "a" gnus-uu-mark-all
2112   "b" gnus-uu-mark-buffer
2113   "S" gnus-uu-mark-sparse
2114   "k" gnus-summary-kill-process-mark
2115   "y" gnus-summary-yank-process-mark
2116   "w" gnus-summary-save-process-mark
2117   "i" gnus-uu-invert-processable)
2118
2119 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2120   ;;"x" gnus-uu-extract-any
2121   "m" gnus-summary-save-parts
2122   "u" gnus-uu-decode-uu
2123   "U" gnus-uu-decode-uu-and-save
2124   "s" gnus-uu-decode-unshar
2125   "S" gnus-uu-decode-unshar-and-save
2126   "o" gnus-uu-decode-save
2127   "O" gnus-uu-decode-save
2128   "b" gnus-uu-decode-binhex
2129   "B" gnus-uu-decode-binhex
2130   "p" gnus-uu-decode-postscript
2131   "P" gnus-uu-decode-postscript-and-save)
2132
2133 (gnus-define-keys
2134     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2135   "u" gnus-uu-decode-uu-view
2136   "U" gnus-uu-decode-uu-and-save-view
2137   "s" gnus-uu-decode-unshar-view
2138   "S" gnus-uu-decode-unshar-and-save-view
2139   "o" gnus-uu-decode-save-view
2140   "O" gnus-uu-decode-save-view
2141   "b" gnus-uu-decode-binhex-view
2142   "B" gnus-uu-decode-binhex-view
2143   "p" gnus-uu-decode-postscript-view
2144   "P" gnus-uu-decode-postscript-and-save-view)
2145
2146 (defvar gnus-article-post-menu nil)
2147
2148 (defconst gnus-summary-menu-maxlen 20)
2149
2150 (defun gnus-summary-menu-split (menu)
2151   ;; If we have lots of elements, divide them into groups of 20
2152   ;; and make a pane (or submenu) for each one.
2153   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2154       (let ((menu menu) sublists next
2155             (i 1))
2156         (while menu
2157           ;; Pull off the next gnus-summary-menu-maxlen elements
2158           ;; and make them the next element of sublist.
2159           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2160           (if next
2161               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2162                       nil))
2163           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2164                                              (aref (car (last menu)) 0)) menu)
2165                                sublists))
2166           (setq i (1+ i))
2167           (setq menu next))
2168         (nreverse sublists))
2169     ;; Few elements--put them all in one pane.
2170     menu))
2171
2172 (defun gnus-summary-make-menu-bar ()
2173   (gnus-turn-off-edit-menu 'summary)
2174
2175   (unless (boundp 'gnus-summary-misc-menu)
2176
2177     (easy-menu-define
2178       gnus-summary-kill-menu gnus-summary-mode-map ""
2179       (cons
2180        "Score"
2181        (nconc
2182         (list
2183          ["Customize" gnus-score-customize t])
2184         (gnus-make-score-map 'increase)
2185         (gnus-make-score-map 'lower)
2186         '(("Mark"
2187            ["Kill below" gnus-summary-kill-below t]
2188            ["Mark above" gnus-summary-mark-above t]
2189            ["Tick above" gnus-summary-tick-above t]
2190            ["Clear above" gnus-summary-clear-above t])
2191           ["Current score" gnus-summary-current-score t]
2192           ["Set score" gnus-summary-set-score t]
2193           ["Switch current score file..." gnus-score-change-score-file t]
2194           ["Set mark below..." gnus-score-set-mark-below t]
2195           ["Set expunge below..." gnus-score-set-expunge-below t]
2196           ["Edit current score file" gnus-score-edit-current-scores t]
2197           ["Edit score file" gnus-score-edit-file t]
2198           ["Trace score" gnus-score-find-trace t]
2199           ["Find words" gnus-score-find-favourite-words t]
2200           ["Rescore buffer" gnus-summary-rescore t]
2201           ["Increase score..." gnus-summary-increase-score t]
2202           ["Lower score..." gnus-summary-lower-score t]))))
2203
2204     ;; Define both the Article menu in the summary buffer and the
2205     ;; equivalent Commands menu in the article buffer here for
2206     ;; consistency.
2207     (let ((innards
2208            `(("Hide"
2209               ["All" gnus-article-hide t]
2210               ["Headers" gnus-article-hide-headers t]
2211               ["Signature" gnus-article-hide-signature t]
2212               ["Citation" gnus-article-hide-citation t]
2213               ["List identifiers" gnus-article-hide-list-identifiers t]
2214               ["Banner" gnus-article-strip-banner t]
2215               ["Boring headers" gnus-article-hide-boring-headers t])
2216              ("Highlight"
2217               ["All" gnus-article-highlight t]
2218               ["Headers" gnus-article-highlight-headers t]
2219               ["Signature" gnus-article-highlight-signature t]
2220               ["Citation" gnus-article-highlight-citation t])
2221              ("MIME"
2222               ["Words" gnus-article-decode-mime-words t]
2223               ["Charset" gnus-article-decode-charset t]
2224               ["QP" gnus-article-de-quoted-unreadable t]
2225               ["Base64" gnus-article-de-base64-unreadable t]
2226               ["View MIME buttons" gnus-summary-display-buttonized t]
2227               ["View all" gnus-mime-view-all-parts t]
2228               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2229               ["Encrypt body" gnus-article-encrypt-body
2230                :active (not (gnus-group-read-only-p))
2231                ,@(if (featurep 'xemacs) nil
2232                    '(:help "Encrypt the message body on disk"))]
2233               ["Extract all parts..." gnus-summary-save-parts t]
2234               ("Multipart"
2235                ["Repair multipart" gnus-summary-repair-multipart t]
2236                ["Pipe part..." gnus-article-pipe-part t]
2237                ["Inline part" gnus-article-inline-part t]
2238                ["View part as type..." gnus-article-view-part-as-type 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                ["View part externally" gnus-article-view-part-externally t]
2244                ["View part with charset..." gnus-article-view-part-as-charset t]
2245                ["Copy part" gnus-article-copy-part t]
2246                ["Save part..." gnus-article-save-part t]
2247                ["View part" gnus-article-view-part t]))
2248              ("Date"
2249               ["Local" gnus-article-date-local t]
2250               ["ISO8601" gnus-article-date-iso8601 t]
2251               ["UT" gnus-article-date-ut t]
2252               ["Original" gnus-article-date-original t]
2253               ["Lapsed" gnus-article-date-lapsed t]
2254               ["User-defined" gnus-article-date-user t])
2255              ("Display"
2256               ["Remove images" gnus-article-remove-images t]
2257               ["Toggle smiley" gnus-treat-smiley t]
2258               ["Show X-Face" gnus-article-display-x-face t]
2259               ["Show picons in From" gnus-treat-from-picon t]
2260               ["Show picons in mail headers" gnus-treat-mail-picon t]
2261               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2262               ("View as different encoding"
2263                ,@(gnus-summary-menu-split
2264                   (mapcar
2265                    (lambda (cs)
2266                      ;; Since easymenu under Emacs doesn't allow
2267                      ;; lambda forms for menu commands, we should
2268                      ;; provide intern'ed function symbols.
2269                      (let ((command (intern (format "\
2270 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2271                        (fset command
2272                              `(lambda ()
2273                                 (interactive)
2274                                 (let ((gnus-summary-show-article-charset-alist
2275                                        '((1 . ,cs))))
2276                                   (gnus-summary-show-article 1))))
2277                        `[,(symbol-name cs) ,command t]))
2278                    (sort (if (fboundp 'coding-system-list)
2279                              (coding-system-list)
2280                            (mapcar 'car mm-mime-mule-charset-alist))
2281                          'string<)))))
2282              ("Washing"
2283               ("Remove Blanks"
2284                ["Leading" gnus-article-strip-leading-blank-lines t]
2285                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2286                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2287                ["All of the above" gnus-article-strip-blank-lines t]
2288                ["All" gnus-article-strip-all-blank-lines t]
2289                ["Leading space" gnus-article-strip-leading-space t]
2290                ["Trailing space" gnus-article-strip-trailing-space t]
2291                ["Leading space in headers"
2292                 gnus-article-remove-leading-whitespace t])
2293               ["Overstrike" gnus-article-treat-overstrike t]
2294               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2295               ["Emphasis" gnus-article-emphasize t]
2296               ["Word wrap" gnus-article-fill-cited-article t]
2297               ["Fill long lines" gnus-article-fill-long-lines t]
2298               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2299               ["Remove CR" gnus-article-remove-cr t]
2300               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2301               ["Base64" gnus-article-de-base64-unreadable t]
2302               ["Rot 13" gnus-summary-caesar-message
2303                ,@(if (featurep 'xemacs) '(t)
2304                    '(:help "\"Caesar rotate\" article by 13"))]
2305               ["De-IDNA" gnus-summary-idna-message t]
2306               ["Morse decode" gnus-summary-morse-message t]
2307               ["Unix pipe..." gnus-summary-pipe-message t]
2308               ["Add buttons" gnus-article-add-buttons t]
2309               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2310               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2311               ["Verbose header" gnus-summary-verbose-headers t]
2312               ["Toggle header" gnus-summary-toggle-header t]
2313               ["Unfold headers" gnus-article-treat-unfold-headers t]
2314               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2315               ["Html" gnus-article-wash-html t]
2316               ["Unsplit URLs" gnus-article-unsplit-urls t]
2317               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2318               ["Decode HZ" gnus-article-decode-HZ t]
2319               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2320               ("(Outlook) Deuglify"
2321                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2322                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2323                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2324                ["Full (Outlook) deuglify"
2325                 gnus-article-outlook-deuglify-article t])
2326               )
2327              ("Output"
2328               ["Save in default format..." gnus-summary-save-article
2329                ,@(if (featurep 'xemacs) '(t)
2330                    '(:help "Save article using default method"))]
2331               ["Save in file..." gnus-summary-save-article-file
2332                ,@(if (featurep 'xemacs) '(t)
2333                    '(:help "Save article in file"))]
2334               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2335               ["Save in MH folder..." gnus-summary-save-article-folder t]
2336               ["Save in VM folder..." gnus-summary-save-article-vm t]
2337               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2338               ["Save body in file..." gnus-summary-save-article-body-file t]
2339               ["Pipe through a filter..." gnus-summary-pipe-output t]
2340               ["Add to SOUP packet" gnus-soup-add-article t]
2341               ["Print with Muttprint..." gnus-summary-muttprint t]
2342               ["Print" gnus-summary-print-article
2343                ,@(if (featurep 'xemacs) '(t)
2344                    '(:help "Generate and print a PostScript image"))])
2345              ("Copy, move,... (Backend)"
2346               ,@(if (featurep 'xemacs) nil
2347                   '(:help "Copying, moving, expiring articles..."))
2348               ["Respool article..." gnus-summary-respool-article t]
2349               ["Move article..." gnus-summary-move-article
2350                (gnus-check-backend-function
2351                 'request-move-article gnus-newsgroup-name)]
2352               ["Copy article..." gnus-summary-copy-article t]
2353               ["Crosspost article..." gnus-summary-crosspost-article
2354                (gnus-check-backend-function
2355                 'request-replace-article gnus-newsgroup-name)]
2356               ["Import file..." gnus-summary-import-article
2357                (gnus-check-backend-function
2358                 'request-accept-article gnus-newsgroup-name)]
2359               ["Create article..." gnus-summary-create-article
2360                (gnus-check-backend-function
2361                 'request-accept-article gnus-newsgroup-name)]
2362               ["Check if posted" gnus-summary-article-posted-p t]
2363               ["Edit article" gnus-summary-edit-article
2364                (not (gnus-group-read-only-p))]
2365               ["Delete article" gnus-summary-delete-article
2366                (gnus-check-backend-function
2367                 'request-expire-articles gnus-newsgroup-name)]
2368               ["Query respool" gnus-summary-respool-query t]
2369               ["Trace respool" gnus-summary-respool-trace t]
2370               ["Delete expirable articles" gnus-summary-expire-articles-now
2371                (gnus-check-backend-function
2372                 'request-expire-articles gnus-newsgroup-name)])
2373              ("Extract"
2374               ["Uudecode" gnus-uu-decode-uu
2375                ,@(if (featurep 'xemacs) '(t)
2376                    '(:help "Decode uuencoded article(s)"))]
2377               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2378               ["Unshar" gnus-uu-decode-unshar t]
2379               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2380               ["Save" gnus-uu-decode-save t]
2381               ["Binhex" gnus-uu-decode-binhex t]
2382               ["Postscript" gnus-uu-decode-postscript t]
2383               ["All MIME parts" gnus-summary-save-parts t])
2384              ("Cache"
2385               ["Enter article" gnus-cache-enter-article t]
2386               ["Remove article" gnus-cache-remove-article t])
2387              ["Translate" gnus-article-babel t]
2388              ["Select article buffer" gnus-summary-select-article-buffer t]
2389              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2390              ["Isearch article..." gnus-summary-isearch-article t]
2391              ["Beginning of the article" gnus-summary-beginning-of-article t]
2392              ["End of the article" gnus-summary-end-of-article t]
2393              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2394              ["Fetch referenced articles" gnus-summary-refer-references t]
2395              ["Fetch current thread" gnus-summary-refer-thread t]
2396              ["Fetch article with id..." gnus-summary-refer-article t]
2397              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2398              ["Redisplay" gnus-summary-show-article t]
2399              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2400       (easy-menu-define
2401         gnus-summary-article-menu gnus-summary-mode-map ""
2402         (cons "Article" innards))
2403
2404       (if (not (keymapp gnus-summary-article-menu))
2405           (easy-menu-define
2406             gnus-article-commands-menu gnus-article-mode-map ""
2407             (cons "Commands" innards))
2408         ;; in Emacs, don't share menu.
2409         (setq gnus-article-commands-menu
2410               (copy-keymap gnus-summary-article-menu))
2411         (define-key gnus-article-mode-map [menu-bar commands]
2412           (cons "Commands" gnus-article-commands-menu))))
2413
2414     (easy-menu-define
2415       gnus-summary-thread-menu gnus-summary-mode-map ""
2416       '("Threads"
2417         ["Find all messages in thread" gnus-summary-refer-thread t]
2418         ["Toggle threading" gnus-summary-toggle-threads t]
2419         ["Hide threads" gnus-summary-hide-all-threads t]
2420         ["Show threads" gnus-summary-show-all-threads t]
2421         ["Hide thread" gnus-summary-hide-thread t]
2422         ["Show thread" gnus-summary-show-thread t]
2423         ["Go to next thread" gnus-summary-next-thread t]
2424         ["Go to previous thread" gnus-summary-prev-thread t]
2425         ["Go down thread" gnus-summary-down-thread t]
2426         ["Go up thread" gnus-summary-up-thread t]
2427         ["Top of thread" gnus-summary-top-thread t]
2428         ["Mark thread as read" gnus-summary-kill-thread t]
2429         ["Lower thread score" gnus-summary-lower-thread t]
2430         ["Raise thread score" gnus-summary-raise-thread t]
2431         ["Rethread current" gnus-summary-rethread-current t]))
2432
2433     (easy-menu-define
2434       gnus-summary-post-menu gnus-summary-mode-map ""
2435       `("Post"
2436         ["Send a message (mail or news)" gnus-summary-post-news
2437          ,@(if (featurep 'xemacs) '(t)
2438              '(:help "Compose a new message (mail or news)"))]
2439         ["Followup" gnus-summary-followup
2440          ,@(if (featurep 'xemacs) '(t)
2441              '(:help "Post followup to this article"))]
2442         ["Followup and yank" gnus-summary-followup-with-original
2443          ,@(if (featurep 'xemacs) '(t)
2444              '(:help "Post followup to this article, quoting its contents"))]
2445         ["Supersede article" gnus-summary-supersede-article t]
2446         ["Cancel article" gnus-summary-cancel-article
2447          ,@(if (featurep 'xemacs) '(t)
2448              '(:help "Cancel an article you posted"))]
2449         ["Reply" gnus-summary-reply t]
2450         ["Reply and yank" gnus-summary-reply-with-original t]
2451         ["Wide reply" gnus-summary-wide-reply t]
2452         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2453          ,@(if (featurep 'xemacs) '(t)
2454              '(:help "Mail a reply, quoting this article"))]
2455         ["Very wide reply" gnus-summary-very-wide-reply t]
2456         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2457          ,@(if (featurep 'xemacs) '(t)
2458              '(:help "Mail a very wide reply, quoting this article"))]
2459         ["Mail forward" gnus-summary-mail-forward t]
2460         ["Post forward" gnus-summary-post-forward t]
2461         ["Digest and mail" gnus-uu-digest-mail-forward t]
2462         ["Digest and post" gnus-uu-digest-post-forward t]
2463         ["Resend message" gnus-summary-resend-message t]
2464         ["Resend message edit" gnus-summary-resend-message-edit t]
2465         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2466         ["Send a mail" gnus-summary-mail-other-window t]
2467         ["Create a local message" gnus-summary-news-other-window t]
2468         ["Uuencode and post" gnus-uu-post-news
2469          ,@(if (featurep 'xemacs) '(t)
2470              '(:help "Post a uuencoded article"))]
2471         ["Followup via news" gnus-summary-followup-to-mail t]
2472         ["Followup via news and yank"
2473          gnus-summary-followup-to-mail-with-original t]
2474         ;;("Draft"
2475         ;;["Send" gnus-summary-send-draft t]
2476         ;;["Send bounced" gnus-resend-bounced-mail t])
2477         ))
2478
2479     (cond
2480      ((not (keymapp gnus-summary-post-menu))
2481       (setq gnus-article-post-menu gnus-summary-post-menu))
2482      ((not gnus-article-post-menu)
2483       ;; Don't share post menu.
2484       (setq gnus-article-post-menu
2485             (copy-keymap gnus-summary-post-menu))))
2486     (define-key gnus-article-mode-map [menu-bar post]
2487       (cons "Post" gnus-article-post-menu))
2488
2489     (easy-menu-define
2490       gnus-summary-misc-menu gnus-summary-mode-map ""
2491       `("Gnus"
2492         ("Mark Read"
2493          ["Mark as read" gnus-summary-mark-as-read-forward t]
2494          ["Mark same subject and select"
2495           gnus-summary-kill-same-subject-and-select t]
2496          ["Mark same subject" gnus-summary-kill-same-subject t]
2497          ["Catchup" gnus-summary-catchup
2498           ,@(if (featurep 'xemacs) '(t)
2499               '(:help "Mark unread articles in this group as read"))]
2500          ["Catchup all" gnus-summary-catchup-all t]
2501          ["Catchup to here" gnus-summary-catchup-to-here t]
2502          ["Catchup from here" gnus-summary-catchup-from-here t]
2503          ["Catchup region" gnus-summary-mark-region-as-read
2504           (gnus-mark-active-p)]
2505          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2506         ("Mark Various"
2507          ["Tick" gnus-summary-tick-article-forward t]
2508          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2509          ["Remove marks" gnus-summary-clear-mark-forward t]
2510          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2511          ["Set bookmark" gnus-summary-set-bookmark t]
2512          ["Remove bookmark" gnus-summary-remove-bookmark t])
2513         ("Limit to"
2514          ["Marks..." gnus-summary-limit-to-marks t]
2515          ["Subject..." gnus-summary-limit-to-subject t]
2516          ["Author..." gnus-summary-limit-to-author t]
2517          ["Recipient..." gnus-summary-limit-to-recipient t]
2518          ["Age..." gnus-summary-limit-to-age t]
2519          ["Extra..." gnus-summary-limit-to-extra t]
2520          ["Score..." gnus-summary-limit-to-score t]
2521          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2522          ["Unread" gnus-summary-limit-to-unread t]
2523          ["Unseen" gnus-summary-limit-to-unseen t]
2524          ["Replied" gnus-summary-limit-to-replied t]
2525          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2526          ["Next articles" gnus-summary-limit-to-articles t]
2527          ["Pop limit" gnus-summary-pop-limit t]
2528          ["Show dormant" gnus-summary-limit-include-dormant t]
2529          ["Hide childless dormant"
2530           gnus-summary-limit-exclude-childless-dormant t]
2531          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2532          ["Hide marked" gnus-summary-limit-exclude-marks t]
2533          ["Show expunged" gnus-summary-limit-include-expunged t])
2534         ("Process Mark"
2535          ["Set mark" gnus-summary-mark-as-processable t]
2536          ["Remove mark" gnus-summary-unmark-as-processable t]
2537          ["Remove all marks" gnus-summary-unmark-all-processable t]
2538          ["Invert marks" gnus-uu-invert-processable t]
2539          ["Mark above" gnus-uu-mark-over t]
2540          ["Mark series" gnus-uu-mark-series t]
2541          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2542          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2543          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2544          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2545          ["Mark all" gnus-uu-mark-all t]
2546          ["Mark buffer" gnus-uu-mark-buffer t]
2547          ["Mark sparse" gnus-uu-mark-sparse t]
2548          ["Mark thread" gnus-uu-mark-thread t]
2549          ["Unmark thread" gnus-uu-unmark-thread t]
2550          ("Process Mark Sets"
2551           ["Kill" gnus-summary-kill-process-mark t]
2552           ["Yank" gnus-summary-yank-process-mark
2553            gnus-newsgroup-process-stack]
2554           ["Save" gnus-summary-save-process-mark t]
2555           ["Run command on marked..." gnus-summary-universal-argument t]))
2556         ("Scroll article"
2557          ["Page forward" gnus-summary-next-page
2558           ,@(if (featurep 'xemacs) '(t)
2559               '(:help "Show next page of article"))]
2560          ["Page backward" gnus-summary-prev-page
2561           ,@(if (featurep 'xemacs) '(t)
2562               '(:help "Show previous page of article"))]
2563          ["Line forward" gnus-summary-scroll-up t])
2564         ("Move"
2565          ["Next unread article" gnus-summary-next-unread-article t]
2566          ["Previous unread article" gnus-summary-prev-unread-article t]
2567          ["Next article" gnus-summary-next-article t]
2568          ["Previous article" gnus-summary-prev-article t]
2569          ["Next unread subject" gnus-summary-next-unread-subject t]
2570          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2571          ["Next article same subject" gnus-summary-next-same-subject t]
2572          ["Previous article same subject" gnus-summary-prev-same-subject t]
2573          ["First unread article" gnus-summary-first-unread-article t]
2574          ["Best unread article" gnus-summary-best-unread-article t]
2575          ["Go to subject number..." gnus-summary-goto-subject t]
2576          ["Go to article number..." gnus-summary-goto-article t]
2577          ["Go to the last article" gnus-summary-goto-last-article t]
2578          ["Pop article off history" gnus-summary-pop-article t])
2579         ("Sort"
2580          ["Sort by number" gnus-summary-sort-by-number t]
2581          ["Sort by author" gnus-summary-sort-by-author t]
2582          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2583          ["Sort by subject" gnus-summary-sort-by-subject t]
2584          ["Sort by date" gnus-summary-sort-by-date t]
2585          ["Sort by score" gnus-summary-sort-by-score t]
2586          ["Sort by lines" gnus-summary-sort-by-lines t]
2587          ["Sort by characters" gnus-summary-sort-by-chars t]
2588          ["Randomize" gnus-summary-sort-by-random t]
2589          ["Original sort" gnus-summary-sort-by-original t])
2590         ("Help"
2591          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2592          ["Describe group" gnus-summary-describe-group t]
2593          ["Fetch charter" gnus-group-fetch-charter
2594           ,@(if (featurep 'xemacs) nil
2595               '(:help "Display the charter of the current group"))]
2596          ["Fetch control message" gnus-group-fetch-control
2597           ,@(if (featurep 'xemacs) nil
2598               '(:help "Display the archived control message for the current group"))]
2599          ["Read manual" gnus-info-find-node t])
2600         ("Modes"
2601          ["Pick and read" gnus-pick-mode t]
2602          ["Binary" gnus-binary-mode t])
2603         ("Regeneration"
2604          ["Regenerate" gnus-summary-prepare t]
2605          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2606          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2607          ["Toggle threading" gnus-summary-toggle-threads t])
2608         ["See old articles" gnus-summary-insert-old-articles t]
2609         ["See new articles" gnus-summary-insert-new-articles t]
2610         ["Filter articles..." gnus-summary-execute-command t]
2611         ["Run command on articles..." gnus-summary-universal-argument t]
2612         ["Search articles forward..." gnus-summary-search-article-forward t]
2613         ["Search articles backward..." gnus-summary-search-article-backward t]
2614         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2615         ["Expand window" gnus-summary-expand-window t]
2616         ["Expire expirable articles" gnus-summary-expire-articles
2617          (gnus-check-backend-function
2618           'request-expire-articles gnus-newsgroup-name)]
2619         ["Edit local kill file" gnus-summary-edit-local-kill t]
2620         ["Edit main kill file" gnus-summary-edit-global-kill t]
2621         ["Edit group parameters" gnus-summary-edit-parameters t]
2622         ["Customize group parameters" gnus-summary-customize-parameters t]
2623         ["Send a bug report" gnus-bug t]
2624         ("Exit"
2625          ["Catchup and exit" gnus-summary-catchup-and-exit
2626           ,@(if (featurep 'xemacs) '(t)
2627               '(:help "Mark unread articles in this group as read, then exit"))]
2628          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2629          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2630          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2631          ["Exit group" gnus-summary-exit
2632           ,@(if (featurep 'xemacs) '(t)
2633               '(:help "Exit current group, return to group selection mode"))]
2634          ["Exit group without updating" gnus-summary-exit-no-update t]
2635          ["Exit and goto next group" gnus-summary-next-group t]
2636          ["Exit and goto prev group" gnus-summary-prev-group t]
2637          ["Reselect group" gnus-summary-reselect-current-group t]
2638          ["Rescan group" gnus-summary-rescan-group t]
2639          ["Update dribble" gnus-summary-save-newsrc t])))
2640
2641     (gnus-run-hooks 'gnus-summary-menu-hook)))
2642
2643 (defvar gnus-summary-tool-bar-map nil)
2644
2645 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2646 ;; affect _new_ message buffers.  We might add a function that walks thru all
2647 ;; summary-mode buffers and force the update.
2648 (defun gnus-summary-tool-bar-update (&optional symbol value)
2649   "Update summary mode toolbar.
2650 Setter function for custom variables."
2651   (setq-default gnus-summary-tool-bar-map nil)
2652   (when symbol
2653     ;; When used as ":set" function:
2654     (set-default symbol value))
2655   (when (gnus-buffer-live-p gnus-summary-buffer)
2656     (with-current-buffer gnus-summary-buffer
2657       (gnus-summary-make-tool-bar))))
2658
2659 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2660                                      'gnus-summary-tool-bar-gnome
2661                                    'gnus-summary-tool-bar-retro)
2662   "Specifies the Gnus summary tool bar.
2663
2664 It can be either a list or a symbol refering to a list.  See
2665 `gmm-tool-bar-from-list' for the format of the list.  The
2666 default key map is `gnus-summary-mode-map'.
2667
2668 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2669 `gnus-summary-tool-bar-retro'."
2670   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2671                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2672                  (repeat :tag "User defined list" gmm-tool-bar-item)
2673                  (symbol))
2674   :version "23.0" ;; No Gnus
2675   :initialize 'custom-initialize-default
2676   :set 'gnus-summary-tool-bar-update
2677   :group 'gnus-summary)
2678
2679 (defcustom gnus-summary-tool-bar-gnome
2680   '((gnus-summary-post-news "mail/compose" nil)
2681     (gnus-summary-insert-new-articles "mail/inbox" nil
2682                                       :visible (or (not gnus-agent)
2683                                                    gnus-plugged))
2684     (gnus-summary-reply-with-original "mail/reply")
2685     (gnus-summary-reply "mail/reply" nil :visible nil)
2686     (gnus-summary-followup-with-original "mail/reply-all")
2687     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2688     (gnus-summary-mail-forward "mail/forward")
2689     (gnus-summary-save-article "mail/save")
2690     (gnus-summary-search-article-forward "search" nil :visible nil)
2691     (gnus-summary-print-article "print")
2692     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2693     ;; Some new commands that may need more suitable icons:
2694     (gnus-summary-save-newsrc "save" nil :visible nil)
2695     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2696     (gnus-summary-prev-article "left-arrow")
2697     (gnus-summary-next-article "right-arrow")
2698     (gnus-summary-next-page "next-page")
2699     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2700     ;;
2701     ;; Maybe some sort-by-... could be added:
2702     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2703     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2704     (gnus-summary-mark-as-expirable
2705      "delete" nil
2706      :visible (gnus-check-backend-function 'request-expire-articles
2707                                            gnus-newsgroup-name))
2708     (gnus-summary-mark-as-spam
2709      "mail/spam" t
2710      :visible (and (fboundp 'spam-group-ham-contents-p)
2711                    (spam-group-ham-contents-p gnus-newsgroup-name))
2712      :help "Mark as spam")
2713     (gnus-summary-mark-as-read-forward
2714      "mail/not-spam" nil
2715      :visible (and (fboundp 'spam-group-spam-contents-p)
2716                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2717     ;;
2718     (gnus-summary-exit "exit")
2719     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2720     (gnus-info-find-node "help"))
2721   "List of functions for the summary tool bar (GNOME style).
2722
2723 See `gmm-tool-bar-from-list' for the format of the list."
2724   :type '(repeat gmm-tool-bar-item)
2725   :version "23.0" ;; No Gnus
2726   :initialize 'custom-initialize-default
2727   :set 'gnus-summary-tool-bar-update
2728   :group 'gnus-summary)
2729
2730 (defcustom gnus-summary-tool-bar-retro
2731   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2732     (gnus-summary-next-unread-article "gnus/next-ur")
2733     (gnus-summary-post-news "gnus/post")
2734     (gnus-summary-followup-with-original "gnus/fuwo")
2735     (gnus-summary-followup "gnus/followup")
2736     (gnus-summary-reply-with-original "gnus/reply-wo")
2737     (gnus-summary-reply "gnus/reply")
2738     (gnus-summary-caesar-message "gnus/rot13")
2739     (gnus-uu-decode-uu "gnus/uu-decode")
2740     (gnus-summary-save-article-file "gnus/save-aif")
2741     (gnus-summary-save-article "gnus/save-art")
2742     (gnus-uu-post-news "gnus/uu-post")
2743     (gnus-summary-catchup "gnus/catchup")
2744     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2745     (gnus-summary-exit "gnus/exit-summ")
2746     ;; Some new command that may need more suitable icons:
2747     (gnus-summary-print-article "gnus/print" nil :visible nil)
2748     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2749     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2750     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2751     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2752     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2753     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2754     ;;
2755     (gnus-info-find-node "gnus/help" nil :visible nil))
2756   "List of functions for the summary tool bar (retro look).
2757
2758 See `gmm-tool-bar-from-list' for the format of the list."
2759   :type '(repeat gmm-tool-bar-item)
2760   :version "23.0" ;; No Gnus
2761   :initialize 'custom-initialize-default
2762   :set 'gnus-summary-tool-bar-update
2763   :group 'gnus-summary)
2764
2765 (defcustom gnus-summary-tool-bar-zap-list t
2766   "List of icon items from the global tool bar.
2767 These items are not displayed in the Gnus summary mode tool bar.
2768
2769 See `gmm-tool-bar-from-list' for the format of the list."
2770   :type 'gmm-tool-bar-zap-list
2771   :version "23.0" ;; No Gnus
2772   :initialize 'custom-initialize-default
2773   :set 'gnus-summary-tool-bar-update
2774   :group 'gnus-summary)
2775
2776 (defvar image-load-path)
2777
2778 (defun gnus-summary-make-tool-bar (&optional force)
2779   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2780 When FORCE, rebuild the tool bar."
2781   (when (and (not (featurep 'xemacs))
2782              (boundp 'tool-bar-mode)
2783              tool-bar-mode
2784              (or (not gnus-summary-tool-bar-map) force))
2785     (let* ((load-path
2786             (gmm-image-load-path-for-library "gnus"
2787                                              "mail/save.xpm"
2788                                              nil t))
2789            (image-load-path (cons (car load-path)
2790                                   (when (boundp 'image-load-path)
2791                                     image-load-path)))
2792            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2793                                         gnus-summary-tool-bar-zap-list
2794                                         'gnus-summary-mode-map)))
2795       (when map
2796         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2797         ;; uses it's value.
2798         (setq gnus-summary-tool-bar-map map))))
2799   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2800
2801 (defun gnus-score-set-default (var value)
2802   "A version of set that updates the GNU Emacs menu-bar."
2803   (set var value)
2804   ;; It is the message that forces the active status to be updated.
2805   (message ""))
2806
2807 (defun gnus-make-score-map (type)
2808   "Make a summary score map of type TYPE."
2809   (if t
2810       nil
2811     (let ((headers '(("author" "from" string)
2812                      ("subject" "subject" string)
2813                      ("article body" "body" string)
2814                      ("article head" "head" string)
2815                      ("xref" "xref" string)
2816                      ("extra header" "extra" string)
2817                      ("lines" "lines" number)
2818                      ("followups to author" "followup" string)))
2819           (types '((number ("less than" <)
2820                            ("greater than" >)
2821                            ("equal" =))
2822                    (string ("substring" s)
2823                            ("exact string" e)
2824                            ("fuzzy string" f)
2825                            ("regexp" r))))
2826           (perms '(("temporary" (current-time-string))
2827                    ("permanent" nil)
2828                    ("immediate" now)))
2829           header)
2830       (list
2831        (apply
2832         'nconc
2833         (list
2834          (if (eq type 'lower)
2835              "Lower score"
2836            "Increase score"))
2837         (let (outh)
2838           (while headers
2839             (setq header (car headers))
2840             (setq outh
2841                   (cons
2842                    (apply
2843                     'nconc
2844                     (list (car header))
2845                     (let ((ts (cdr (assoc (nth 2 header) types)))
2846                           outt)
2847                       (while ts
2848                         (setq outt
2849                               (cons
2850                                (apply
2851                                 'nconc
2852                                 (list (caar ts))
2853                                 (let ((ps perms)
2854                                       outp)
2855                                   (while ps
2856                                     (setq outp
2857                                           (cons
2858                                            (vector
2859                                             (caar ps)
2860                                             (list
2861                                              'gnus-summary-score-entry
2862                                              (nth 1 header)
2863                                              (if (or (string= (nth 1 header)
2864                                                               "head")
2865                                                      (string= (nth 1 header)
2866                                                               "body"))
2867                                                  ""
2868                                                (list 'gnus-summary-header
2869                                                      (nth 1 header)))
2870                                              (list 'quote (nth 1 (car ts)))
2871                                              (list 'gnus-score-delta-default
2872                                                    nil)
2873                                              (nth 1 (car ps))
2874                                              t)
2875                                             t)
2876                                            outp))
2877                                     (setq ps (cdr ps)))
2878                                   (list (nreverse outp))))
2879                                outt))
2880                         (setq ts (cdr ts)))
2881                       (list (nreverse outt))))
2882                    outh))
2883             (setq headers (cdr headers)))
2884           (list (nreverse outh))))))))
2885
2886 \f
2887
2888 (defun gnus-summary-mode (&optional group)
2889   "Major mode for reading articles.
2890
2891 All normal editing commands are switched off.
2892 \\<gnus-summary-mode-map>
2893 Each line in this buffer represents one article.  To read an
2894 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2895 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2896 respectively.
2897
2898 You can also post articles and send mail from this buffer.  To
2899 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2900 of an article, type `\\[gnus-summary-reply]'.
2901
2902 There are approx. one gazillion commands you can execute in this
2903 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2904
2905 The following commands are available:
2906
2907 \\{gnus-summary-mode-map}"
2908   (interactive)
2909   (kill-all-local-variables)
2910   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2911     (gnus-summary-make-local-variables))
2912   (gnus-summary-make-local-variables)
2913   (setq gnus-newsgroup-name group)
2914   (when (gnus-visual-p 'summary-menu 'menu)
2915     (gnus-summary-make-menu-bar)
2916     (gnus-summary-make-tool-bar))
2917   (gnus-make-thread-indent-array)
2918   (gnus-simplify-mode-line)
2919   (setq major-mode 'gnus-summary-mode)
2920   (setq mode-name "Summary")
2921   (make-local-variable 'minor-mode-alist)
2922   (use-local-map gnus-summary-mode-map)
2923   (buffer-disable-undo)
2924   (setq buffer-read-only t              ;Disable modification
2925         show-trailing-whitespace nil)
2926   (setq truncate-lines t)
2927   (setq selective-display t)
2928   (setq selective-display-ellipses t)   ;Display `...'
2929   (gnus-summary-set-display-table)
2930   (gnus-set-default-directory)
2931   (make-local-variable 'gnus-summary-line-format)
2932   (make-local-variable 'gnus-summary-line-format-spec)
2933   (make-local-variable 'gnus-summary-dummy-line-format)
2934   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2935   (make-local-variable 'gnus-summary-mark-positions)
2936   (gnus-make-local-hook 'pre-command-hook)
2937   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2938   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2939   (turn-on-gnus-mailing-list-mode)
2940   (mm-enable-multibyte)
2941   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2942   (gnus-update-summary-mark-positions))
2943
2944 (defun gnus-summary-make-local-variables ()
2945   "Make all the local summary buffer variables."
2946   (let (global)
2947     (dolist (local gnus-summary-local-variables)
2948       (if (consp local)
2949           (progn
2950             (if (eq (cdr local) 'global)
2951                 ;; Copy the global value of the variable.
2952                 (setq global (symbol-value (car local)))
2953               ;; Use the value from the list.
2954               (setq global (eval (cdr local))))
2955             (set (make-local-variable (car local)) global))
2956         ;; Simple nil-valued local variable.
2957         (set (make-local-variable local) nil)))))
2958
2959 (defun gnus-summary-clear-local-variables ()
2960   (let ((locals gnus-summary-local-variables))
2961     (while locals
2962       (if (consp (car locals))
2963           (and (symbolp (caar locals))
2964                (set (caar locals) nil))
2965         (and (symbolp (car locals))
2966              (set (car locals) nil)))
2967       (setq locals (cdr locals)))))
2968
2969 ;; Summary data functions.
2970
2971 (defmacro gnus-data-number (data)
2972   `(car ,data))
2973
2974 (defmacro gnus-data-set-number (data number)
2975   `(setcar ,data ,number))
2976
2977 (defmacro gnus-data-mark (data)
2978   `(nth 1 ,data))
2979
2980 (defmacro gnus-data-set-mark (data mark)
2981   `(setcar (nthcdr 1 ,data) ,mark))
2982
2983 (defmacro gnus-data-pos (data)
2984   `(nth 2 ,data))
2985
2986 (defmacro gnus-data-set-pos (data pos)
2987   `(setcar (nthcdr 2 ,data) ,pos))
2988
2989 (defmacro gnus-data-header (data)
2990   `(nth 3 ,data))
2991
2992 (defmacro gnus-data-set-header (data header)
2993   `(setf (nth 3 ,data) ,header))
2994
2995 (defmacro gnus-data-level (data)
2996   `(nth 4 ,data))
2997
2998 (defmacro gnus-data-unread-p (data)
2999   `(= (nth 1 ,data) gnus-unread-mark))
3000
3001 (defmacro gnus-data-read-p (data)
3002   `(/= (nth 1 ,data) gnus-unread-mark))
3003
3004 (defmacro gnus-data-pseudo-p (data)
3005   `(consp (nth 3 ,data)))
3006
3007 (defmacro gnus-data-find (number)
3008   `(assq ,number gnus-newsgroup-data))
3009
3010 (defmacro gnus-data-find-list (number &optional data)
3011   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3012      (memq (assq ,number bdata)
3013            bdata)))
3014
3015 (defmacro gnus-data-make (number mark pos header level)
3016   `(list ,number ,mark ,pos ,header ,level))
3017
3018 (defun gnus-data-enter (after-article number mark pos header level offset)
3019   (let ((data (gnus-data-find-list after-article)))
3020     (unless data
3021       (error "No such article: %d" after-article))
3022     (setcdr data (cons (gnus-data-make number mark pos header level)
3023                        (cdr data)))
3024     (setq gnus-newsgroup-data-reverse nil)
3025     (gnus-data-update-list (cddr data) offset)))
3026
3027 (defun gnus-data-enter-list (after-article list &optional offset)
3028   (when list
3029     (let ((data (and after-article (gnus-data-find-list after-article)))
3030           (ilist list))
3031       (if (not (or data
3032                    after-article))
3033           (let ((odata gnus-newsgroup-data))
3034             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3035             (when offset
3036               (gnus-data-update-list odata offset)))
3037         ;; Find the last element in the list to be spliced into the main
3038         ;; list.
3039         (setq list (last list))
3040         (if (not data)
3041             (progn
3042               (setcdr list gnus-newsgroup-data)
3043               (setq gnus-newsgroup-data ilist)
3044               (when offset
3045                 (gnus-data-update-list (cdr list) offset)))
3046           (setcdr list (cdr data))
3047           (setcdr data ilist)
3048           (when offset
3049             (gnus-data-update-list (cdr list) offset))))
3050       (setq gnus-newsgroup-data-reverse nil))))
3051
3052 (defun gnus-data-remove (article &optional offset)
3053   (let ((data gnus-newsgroup-data))
3054     (if (= (gnus-data-number (car data)) article)
3055         (progn
3056           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3057                 gnus-newsgroup-data-reverse nil)
3058           (when offset
3059             (gnus-data-update-list gnus-newsgroup-data offset)))
3060       (while (cdr data)
3061         (when (= (gnus-data-number (cadr data)) article)
3062           (setcdr data (cddr data))
3063           (when offset
3064             (gnus-data-update-list (cdr data) offset))
3065           (setq data nil
3066                 gnus-newsgroup-data-reverse nil))
3067         (setq data (cdr data))))))
3068
3069 (defmacro gnus-data-list (backward)
3070   `(if ,backward
3071        (or gnus-newsgroup-data-reverse
3072            (setq gnus-newsgroup-data-reverse
3073                  (reverse gnus-newsgroup-data)))
3074      gnus-newsgroup-data))
3075
3076 (defun gnus-data-update-list (data offset)
3077   "Add OFFSET to the POS of all data entries in DATA."
3078   (setq gnus-newsgroup-data-reverse nil)
3079   (while data
3080     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3081     (setq data (cdr data))))
3082
3083 (defun gnus-summary-article-pseudo-p (article)
3084   "Say whether this article is a pseudo article or not."
3085   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3086
3087 (defmacro gnus-summary-article-sparse-p (article)
3088   "Say whether this article is a sparse article or not."
3089   `(memq ,article gnus-newsgroup-sparse))
3090
3091 (defmacro gnus-summary-article-ancient-p (article)
3092   "Say whether this article is a sparse article or not."
3093   `(memq ,article gnus-newsgroup-ancient))
3094
3095 (defun gnus-article-parent-p (number)
3096   "Say whether this article is a parent or not."
3097   (let ((data (gnus-data-find-list number)))
3098     (and (cdr data)              ; There has to be an article after...
3099          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3100             (gnus-data-level (nth 1 data))))))
3101
3102 (defun gnus-article-children (number)
3103   "Return a list of all children to NUMBER."
3104   (let* ((data (gnus-data-find-list number))
3105          (level (gnus-data-level (car data)))
3106          children)
3107     (setq data (cdr data))
3108     (while (and data
3109                 (= (gnus-data-level (car data)) (1+ level)))
3110       (push (gnus-data-number (car data)) children)
3111       (setq data (cdr data)))
3112     children))
3113
3114 (defmacro gnus-summary-skip-intangible ()
3115   "If the current article is intangible, then jump to a different article."
3116   '(let ((to (get-text-property (point) 'gnus-intangible)))
3117      (and to (gnus-summary-goto-subject to))))
3118
3119 (defmacro gnus-summary-article-intangible-p ()
3120   "Say whether this article is intangible or not."
3121   '(get-text-property (point) 'gnus-intangible))
3122
3123 (defun gnus-article-read-p (article)
3124   "Say whether ARTICLE is read or not."
3125   (not (or (memq article gnus-newsgroup-marked)
3126            (memq article gnus-newsgroup-spam-marked)
3127            (memq article gnus-newsgroup-unreads)
3128            (memq article gnus-newsgroup-unselected)
3129            (memq article gnus-newsgroup-dormant))))
3130
3131 ;; Some summary mode macros.
3132
3133 (defmacro gnus-summary-article-number ()
3134   "The article number of the article on the current line.
3135 If there isn't an article number here, then we return the current
3136 article number."
3137   '(progn
3138      (gnus-summary-skip-intangible)
3139      (or (get-text-property (point) 'gnus-number)
3140          (gnus-summary-last-subject))))
3141
3142 (defmacro gnus-summary-article-header (&optional number)
3143   "Return the header of article NUMBER."
3144   `(gnus-data-header (gnus-data-find
3145                       ,(or number '(gnus-summary-article-number)))))
3146
3147 (defmacro gnus-summary-thread-level (&optional number)
3148   "Return the level of thread that starts with article NUMBER."
3149   `(if (and (eq gnus-summary-make-false-root 'dummy)
3150             (get-text-property (point) 'gnus-intangible))
3151        0
3152      (gnus-data-level (gnus-data-find
3153                        ,(or number '(gnus-summary-article-number))))))
3154
3155 (defmacro gnus-summary-article-mark (&optional number)
3156   "Return the mark of article NUMBER."
3157   `(gnus-data-mark (gnus-data-find
3158                     ,(or number '(gnus-summary-article-number)))))
3159
3160 (defmacro gnus-summary-article-pos (&optional number)
3161   "Return the position of the line of article NUMBER."
3162   `(gnus-data-pos (gnus-data-find
3163                    ,(or number '(gnus-summary-article-number)))))
3164
3165 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3166 (defmacro gnus-summary-article-subject (&optional number)
3167   "Return current subject string or nil if nothing."
3168   `(let ((headers
3169           ,(if number
3170                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3171              '(gnus-data-header (assq (gnus-summary-article-number)
3172                                       gnus-newsgroup-data)))))
3173      (and headers
3174           (vectorp headers)
3175           (mail-header-subject headers))))
3176
3177 (defmacro gnus-summary-article-score (&optional number)
3178   "Return current article score."
3179   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3180                   gnus-newsgroup-scored))
3181        gnus-summary-default-score 0))
3182
3183 (defun gnus-summary-article-children (&optional number)
3184   "Return a list of article numbers that are children of article NUMBER."
3185   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3186          (level (gnus-data-level (car data)))
3187          l children)
3188     (while (and (setq data (cdr data))
3189                 (> (setq l (gnus-data-level (car data))) level))
3190       (and (= (1+ level) l)
3191            (push (gnus-data-number (car data))
3192                  children)))
3193     (nreverse children)))
3194
3195 (defun gnus-summary-article-parent (&optional number)
3196   "Return the article number of the parent of article NUMBER."
3197   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3198                                     (gnus-data-list t)))
3199          (level (gnus-data-level (car data))))
3200     (if (zerop level)
3201         ()                              ; This is a root.
3202       ;; We search until we find an article with a level less than
3203       ;; this one.  That function has to be the parent.
3204       (while (and (setq data (cdr data))
3205                   (not (< (gnus-data-level (car data)) level))))
3206       (and data (gnus-data-number (car data))))))
3207
3208 (defun gnus-unread-mark-p (mark)
3209   "Say whether MARK is the unread mark."
3210   (= mark gnus-unread-mark))
3211
3212 (defun gnus-read-mark-p (mark)
3213   "Say whether MARK is one of the marks that mark as read.
3214 This is all marks except unread, ticked, dormant, and expirable."
3215   (not (or (= mark gnus-unread-mark)
3216            (= mark gnus-ticked-mark)
3217            (= mark gnus-spam-mark)
3218            (= mark gnus-dormant-mark)
3219            (= mark gnus-expirable-mark))))
3220
3221 (defmacro gnus-article-mark (number)
3222   "Return the MARK of article NUMBER.
3223 This macro should only be used when computing the mark the \"first\"
3224 time; i.e., when generating the summary lines.  After that,
3225 `gnus-summary-article-mark' should be used to examine the
3226 marks of articles."
3227   `(cond
3228     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3229     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3230     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3231     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3232     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3233     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3234     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3235     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3236            gnus-ancient-mark))))
3237
3238 ;; Saving hidden threads.
3239
3240 (defmacro gnus-save-hidden-threads (&rest forms)
3241   "Save hidden threads, eval FORMS, and restore the hidden threads."
3242   (let ((config (make-symbol "config")))
3243     `(let ((,config (gnus-hidden-threads-configuration)))
3244        (unwind-protect
3245            (save-excursion
3246              ,@forms)
3247          (gnus-restore-hidden-threads-configuration ,config)))))
3248 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3249 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3250
3251 (defun gnus-data-compute-positions ()
3252   "Compute the positions of all articles."
3253   (setq gnus-newsgroup-data-reverse nil)
3254   (let ((data gnus-newsgroup-data))
3255     (save-excursion
3256       (gnus-save-hidden-threads
3257         (gnus-summary-show-all-threads)
3258         (goto-char (point-min))
3259         (while data
3260           (while (get-text-property (point) 'gnus-intangible)
3261             (forward-line 1))
3262           (gnus-data-set-pos (car data) (+ (point) 3))
3263           (setq data (cdr data))
3264           (forward-line 1))))))
3265
3266 (defun gnus-hidden-threads-configuration ()
3267   "Return the current hidden threads configuration."
3268   (save-excursion
3269     (let (config)
3270       (goto-char (point-min))
3271       (while (search-forward "\r" nil t)
3272         (push (1- (point)) config))
3273       config)))
3274
3275 (defun gnus-restore-hidden-threads-configuration (config)
3276   "Restore hidden threads configuration from CONFIG."
3277   (save-excursion
3278     (let (point buffer-read-only)
3279       (while (setq point (pop config))
3280         (when (and (< point (point-max))
3281                    (goto-char point)
3282                    (eq (char-after) ?\n))
3283           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3284
3285 ;; Various summary mode internalish functions.
3286
3287 (defun gnus-mouse-pick-article (e)
3288   (interactive "e")
3289   (mouse-set-point e)
3290   (gnus-summary-next-page nil t))
3291
3292 (defun gnus-summary-set-display-table ()
3293   "Change the display table.
3294 Odd characters have a tendency to mess
3295 up nicely formatted displays - we make all possible glyphs
3296 display only a single character."
3297
3298   ;; We start from the standard display table, if any.
3299   (let ((table (or (copy-sequence standard-display-table)
3300                    (make-display-table)))
3301         (i 32))
3302     ;; Nix out all the control chars...
3303     (while (>= (setq i (1- i)) 0)
3304       (aset table i [??]))
3305    ;; ... but not newline and cr, of course.  (cr is necessary for the
3306     ;; selective display).
3307     (aset table ?\n nil)
3308     (aset table ?\r nil)
3309     ;; We keep TAB as well.
3310     (aset table ?\t nil)
3311     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3312     ;; Emacs 23 (unicode), that are not set already.
3313     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3314                  160
3315                256)))
3316       (while (>= (setq i (1- i)) 127)
3317         ;; Only modify if the entry is nil.
3318         (unless (aref table i)
3319           (aset table i [??]))))
3320     (setq buffer-display-table table)))
3321
3322 (defun gnus-summary-set-article-display-arrow (pos)
3323   "Update the overlay arrow to point to line at position POS."
3324   (when (and gnus-summary-display-arrow
3325              (boundp 'overlay-arrow-position)
3326              (boundp 'overlay-arrow-string))
3327     (save-excursion
3328       (goto-char pos)
3329       (beginning-of-line)
3330       (unless overlay-arrow-position
3331         (setq overlay-arrow-position (make-marker)))
3332       (setq overlay-arrow-string "=>"
3333             overlay-arrow-position (set-marker overlay-arrow-position
3334                                                (point)
3335                                                (current-buffer))))))
3336
3337 (defun gnus-summary-setup-buffer (group)
3338   "Initialize summary buffer."
3339   (let ((buffer (gnus-summary-buffer-name group))
3340         (dead-name (concat "*Dead Summary "
3341                            (gnus-group-decoded-name group) "*")))
3342     ;; If a dead summary buffer exists, we kill it.
3343     (when (gnus-buffer-live-p dead-name)
3344       (gnus-kill-buffer dead-name))
3345     (if (get-buffer buffer)
3346         (progn
3347           (set-buffer buffer)
3348           (setq gnus-summary-buffer (current-buffer))
3349           (not gnus-newsgroup-prepared))
3350       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3351       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3352       (gnus-summary-mode group)
3353       (when gnus-carpal
3354         (gnus-carpal-setup-buffer 'summary))
3355       (unless gnus-single-article-buffer
3356         (make-local-variable 'gnus-article-buffer)
3357         (make-local-variable 'gnus-article-current)
3358         (make-local-variable 'gnus-original-article-buffer))
3359       (setq gnus-newsgroup-name group)
3360       ;; Set any local variables in the group parameters.
3361       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3362       t)))
3363
3364 (defun gnus-set-global-variables ()
3365   "Set the global equivalents of the buffer-local variables.
3366 They are set to the latest values they had.  These reflect the summary
3367 buffer that was in action when the last article was fetched."
3368   (when (eq major-mode 'gnus-summary-mode)
3369     (setq gnus-summary-buffer (current-buffer))
3370     (let ((name gnus-newsgroup-name)
3371           (marked gnus-newsgroup-marked)
3372           (spam gnus-newsgroup-spam-marked)
3373           (unread gnus-newsgroup-unreads)
3374           (headers gnus-current-headers)
3375           (data gnus-newsgroup-data)
3376           (summary gnus-summary-buffer)
3377           (article-buffer gnus-article-buffer)
3378           (original gnus-original-article-buffer)
3379           (gac gnus-article-current)
3380           (reffed gnus-reffed-article-number)
3381           (score-file gnus-current-score-file)
3382           (default-charset gnus-newsgroup-charset)
3383           vlist)
3384       (let ((locals gnus-newsgroup-variables))
3385         (while locals
3386           (if (consp (car locals))
3387               (push (eval (caar locals)) vlist)
3388             (push (eval (car locals)) vlist))
3389           (setq locals (cdr locals)))
3390         (setq vlist (nreverse vlist)))
3391       (with-current-buffer gnus-group-buffer
3392         (setq gnus-newsgroup-name name
3393               gnus-newsgroup-marked marked
3394               gnus-newsgroup-spam-marked spam
3395               gnus-newsgroup-unreads unread
3396               gnus-current-headers headers
3397               gnus-newsgroup-data data
3398               gnus-article-current gac
3399               gnus-summary-buffer summary
3400               gnus-article-buffer article-buffer
3401               gnus-original-article-buffer original
3402               gnus-reffed-article-number reffed
3403               gnus-current-score-file score-file
3404               gnus-newsgroup-charset default-charset)
3405         (let ((locals gnus-newsgroup-variables))
3406           (while locals
3407             (if (consp (car locals))
3408                 (set (caar locals) (pop vlist))
3409               (set (car locals) (pop vlist)))
3410             (setq locals (cdr locals))))
3411         ;; The article buffer also has local variables.
3412         (when (gnus-buffer-live-p gnus-article-buffer)
3413           (set-buffer gnus-article-buffer)
3414           (setq gnus-summary-buffer summary))))))
3415
3416 (defun gnus-summary-article-unread-p (article)
3417   "Say whether ARTICLE is unread or not."
3418   (memq article gnus-newsgroup-unreads))
3419
3420 (defun gnus-summary-first-article-p (&optional article)
3421   "Return whether ARTICLE is the first article in the buffer."
3422   (if (not (setq article (or article (gnus-summary-article-number))))
3423       nil
3424     (eq article (caar gnus-newsgroup-data))))
3425
3426 (defun gnus-summary-last-article-p (&optional article)
3427   "Return whether ARTICLE is the last article in the buffer."
3428   (if (not (setq article (or article (gnus-summary-article-number))))
3429       ;; All non-existent numbers are the last article.  :-)
3430       t
3431     (not (cdr (gnus-data-find-list article)))))
3432
3433 (defun gnus-make-thread-indent-array ()
3434   (let ((n 200))
3435     (unless (and gnus-thread-indent-array
3436                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3437       (setq gnus-thread-indent-array (make-vector 201 "")
3438             gnus-thread-indent-array-level gnus-thread-indent-level)
3439       (while (>= n 0)
3440         (aset gnus-thread-indent-array n
3441               (make-string (* n gnus-thread-indent-level) ? ))
3442         (setq n (1- n))))))
3443
3444 (defun gnus-update-summary-mark-positions ()
3445   "Compute where the summary marks are to go."
3446   (save-excursion
3447     (when (gnus-buffer-exists-p gnus-summary-buffer)
3448       (set-buffer gnus-summary-buffer))
3449     (let ((spec gnus-summary-line-format-spec)
3450           pos)
3451       (save-excursion
3452         (gnus-set-work-buffer)
3453         (let ((gnus-tmp-unread ?Z)
3454               (gnus-replied-mark ?Z)
3455               (gnus-score-below-mark ?Z)
3456               (gnus-score-over-mark ?Z)
3457               (gnus-undownloaded-mark ?Z)
3458               (gnus-summary-line-format-spec spec)
3459               (gnus-newsgroup-downloadable '(0))
3460               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3461               case-fold-search ignores)
3462           ;; Here, all marks are bound to Z.
3463           (gnus-summary-insert-line header
3464                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3465           (goto-char (point-min))
3466           ;; Memorize the positions of the same characters as dummy marks.
3467           (while (re-search-forward "[A-D]" nil t)
3468             (push (point) ignores))
3469           (erase-buffer)
3470           ;; We use A-D as dummy marks in order to know column positions
3471           ;; where marks should be inserted.
3472           (setq gnus-tmp-unread ?A
3473                 gnus-replied-mark ?B
3474                 gnus-score-below-mark ?C
3475                 gnus-score-over-mark ?C
3476                 gnus-undownloaded-mark ?D)
3477           (gnus-summary-insert-line header
3478                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3479           ;; Ignore characters which aren't dummy marks.
3480           (dolist (p ignores)
3481             (delete-region (goto-char (1- p)) p)
3482             (insert ?Z))
3483           (goto-char (point-min))
3484           (setq pos (list (cons 'unread
3485                                 (and (search-forward "A" nil t)
3486                                      (- (point) (point-min) 1)))))
3487           (goto-char (point-min))
3488           (push (cons 'replied (and (search-forward "B" nil t)
3489                                     (- (point) (point-min) 1)))
3490                 pos)
3491           (goto-char (point-min))
3492           (push (cons 'score (and (search-forward "C" nil t)
3493                                   (- (point) (point-min) 1)))
3494                 pos)
3495           (goto-char (point-min))
3496           (push (cons 'download (and (search-forward "D" nil t)
3497                                      (- (point) (point-min) 1)))
3498                 pos)))
3499       (setq gnus-summary-mark-positions pos))))
3500
3501 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3502   "Insert a dummy root in the summary buffer."
3503   (beginning-of-line)
3504   (gnus-add-text-properties
3505    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3506    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3507
3508 (defun gnus-summary-extract-address-component (from)
3509   (or (car (funcall gnus-extract-address-components from))
3510       from))
3511
3512 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3513   (let ((mail-parse-charset gnus-newsgroup-charset)
3514         ; Is it really necessary to do this next part for each summary line?
3515         ; Luckily, doesn't seem to slow things down much.
3516         (mail-parse-ignored-charsets
3517          (with-current-buffer gnus-summary-buffer
3518            gnus-newsgroup-ignored-charsets)))
3519     (or
3520      (and gnus-ignored-from-addresses
3521           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3522           (let ((extra-headers (mail-header-extra header))
3523                 to
3524                 newsgroups)
3525             (cond
3526              ((setq to (cdr (assq 'To extra-headers)))
3527               (concat gnus-summary-to-prefix
3528                       (inline
3529                         (gnus-summary-extract-address-component
3530                          (funcall gnus-decode-encoded-word-function to)))))
3531              ((setq newsgroups
3532                     (or
3533                      (cdr (assq 'Newsgroups extra-headers))
3534                      (and
3535                       (memq 'Newsgroups gnus-extra-headers)
3536                       (eq (car (gnus-find-method-for-group
3537                                 gnus-newsgroup-name)) 'nntp)
3538                       (gnus-group-real-name gnus-newsgroup-name))))
3539               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3540      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3541
3542 (defun gnus-summary-insert-line (gnus-tmp-header
3543                                  gnus-tmp-level gnus-tmp-current
3544                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3545                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3546                                  &optional gnus-tmp-dummy gnus-tmp-score
3547                                  gnus-tmp-process)
3548   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3549          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3550          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3551          (gnus-tmp-score-char
3552           (if (or (null gnus-summary-default-score)
3553                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3554                       gnus-summary-zcore-fuzz))
3555               ?                         ;Whitespace
3556             (if (< gnus-tmp-score gnus-summary-default-score)
3557                 gnus-score-below-mark gnus-score-over-mark)))
3558          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3559          (gnus-tmp-replied
3560           (cond (gnus-tmp-process gnus-process-mark)
3561                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3562                  gnus-cached-mark)
3563                 (gnus-tmp-replied gnus-replied-mark)
3564                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3565                  gnus-forwarded-mark)
3566                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3567                  gnus-saved-mark)
3568                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3569                  gnus-recent-mark)
3570                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3571                  gnus-unseen-mark)
3572                 (t gnus-no-mark)))
3573          (gnus-tmp-downloaded
3574           (cond (undownloaded
3575                  gnus-undownloaded-mark)
3576                 (gnus-newsgroup-agentized
3577                  gnus-downloaded-mark)
3578                 (t
3579                  gnus-no-mark)))
3580          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3581          (gnus-tmp-name
3582           (cond
3583            ((string-match "<[^>]+> *$" gnus-tmp-from)
3584             (let ((beg (match-beginning 0)))
3585               (or (and (string-match "^\".+\"" gnus-tmp-from)
3586                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3587                   (substring gnus-tmp-from 0 beg))))
3588            ((string-match "(.+)" gnus-tmp-from)
3589             (substring gnus-tmp-from
3590                        (1+ (match-beginning 0)) (1- (match-end 0))))
3591            (t gnus-tmp-from)))
3592          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3593          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3594          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3595          (buffer-read-only nil))
3596     (when (string= gnus-tmp-name "")
3597       (setq gnus-tmp-name gnus-tmp-from))
3598     (unless (numberp gnus-tmp-lines)
3599       (setq gnus-tmp-lines -1))
3600     (if (= gnus-tmp-lines -1)
3601         (setq gnus-tmp-lines "?")
3602       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3603       (gnus-put-text-property
3604      (point)
3605      (progn (eval gnus-summary-line-format-spec) (point))
3606        'gnus-number gnus-tmp-number)
3607     (when (gnus-visual-p 'summary-highlight 'highlight)
3608       (forward-line -1)
3609       (gnus-run-hooks 'gnus-summary-update-hook)
3610       (forward-line 1))))
3611
3612 (defun gnus-summary-update-line (&optional dont-update)
3613   "Update summary line after change."
3614   (when (and gnus-summary-default-score
3615              (not gnus-summary-inhibit-highlight))
3616     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3617            (article (gnus-summary-article-number))
3618            (score (gnus-summary-article-score article)))
3619       (unless dont-update
3620         (if (and gnus-summary-mark-below
3621                  (< (gnus-summary-article-score)
3622                     gnus-summary-mark-below))
3623             ;; This article has a low score, so we mark it as read.
3624             (when (memq article gnus-newsgroup-unreads)
3625               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3626           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3627             ;; This article was previously marked as read on account
3628             ;; of a low score, but now it has risen, so we mark it as
3629             ;; unread.
3630             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3631         (gnus-summary-update-mark
3632          (if (or (null gnus-summary-default-score)
3633                  (<= (abs (- score gnus-summary-default-score))
3634                      gnus-summary-zcore-fuzz))
3635              ?                          ;Whitespace
3636            (if (< score gnus-summary-default-score)
3637                gnus-score-below-mark gnus-score-over-mark))
3638          'score))
3639       ;; Do visual highlighting.
3640       (when (gnus-visual-p 'summary-highlight 'highlight)
3641         (gnus-run-hooks 'gnus-summary-update-hook)))))
3642
3643 (defvar gnus-tmp-new-adopts nil)
3644
3645 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3646   "Return the number of articles in THREAD.
3647 This may be 0 in some cases -- if none of the articles in
3648 the thread are to be displayed."
3649   (let* ((number
3650          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3651           (cond
3652            ((not (listp thread))
3653             1)
3654            ((and (consp thread) (cdr thread))
3655             (apply
3656              '+ 1 (mapcar
3657                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3658            ((null thread)
3659             1)
3660            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3661             1)
3662            (t 0))))
3663     (when (and level (zerop level) gnus-tmp-new-adopts)
3664       (incf number
3665             (apply '+ (mapcar
3666                        'gnus-summary-number-of-articles-in-thread
3667                        gnus-tmp-new-adopts))))
3668     (if char
3669         (if (> number 1) gnus-not-empty-thread-mark
3670           gnus-empty-thread-mark)
3671       number)))
3672
3673 (defsubst gnus-summary-line-message-size (head)
3674   "Return pretty-printed version of message size.
3675 This function is intended to be used in
3676 `gnus-summary-line-format-alist'."
3677   (let ((c (or (mail-header-chars head) -1)))
3678     (cond ((< c 0) "n/a")               ; chars not available
3679           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3680           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3681           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3682           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3683
3684
3685 (defun gnus-summary-set-local-parameters (group)
3686   "Go through the local params of GROUP and set all variable specs in that list."
3687   (let ((vars '(quit-config)))          ; Ignore quit-config.
3688     (dolist (elem (gnus-group-find-parameter group))
3689       (and (consp elem)                 ; Has to be a cons.
3690            (consp (cdr elem))           ; The cdr has to be a list.
3691            (symbolp (car elem))         ; Has to be a symbol in there.
3692            (not (memq (car elem) vars))
3693            (ignore-errors               ; So we set it.
3694              (push (car elem) vars)
3695              (make-local-variable (car elem))
3696              (set (car elem) (eval (nth 1 elem))))))))
3697
3698 (defun gnus-summary-read-group (group &optional show-all no-article
3699                                       kill-buffer no-display backward
3700                                       select-articles)
3701   "Start reading news in newsgroup GROUP.
3702 If SHOW-ALL is non-nil, already read articles are also listed.
3703 If NO-ARTICLE is non-nil, no article is selected initially.
3704 If NO-DISPLAY, don't generate a summary buffer."
3705   (let (result)
3706     (while (and group
3707                 (null (setq result
3708                             (let ((gnus-auto-select-next nil))
3709                               (or (gnus-summary-read-group-1
3710                                    group show-all no-article
3711                                    kill-buffer no-display
3712                                    select-articles)
3713                                   (setq show-all nil
3714                                         select-articles nil)))))
3715                 (eq gnus-auto-select-next 'quietly))
3716       (set-buffer gnus-group-buffer)
3717       ;; The entry function called above goes to the next
3718       ;; group automatically, so we go two groups back
3719       ;; if we are searching for the previous group.
3720       (when backward
3721         (gnus-group-prev-unread-group 2))
3722       (if (not (equal group (gnus-group-group-name)))
3723           (setq group (gnus-group-group-name))
3724         (setq group nil)))
3725     result))
3726
3727 (defun gnus-summary-read-group-1 (group show-all no-article
3728                                         kill-buffer no-display
3729                                         &optional select-articles)
3730   ;; Killed foreign groups can't be entered.
3731   ;;  (when (and (not (gnus-group-native-p group))
3732   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3733   ;;    (error "Dead non-native groups can't be entered"))
3734   (gnus-message 5 "Retrieving newsgroup: %s..."
3735                 (gnus-group-decoded-name group))
3736   (let* ((new-group (gnus-summary-setup-buffer group))
3737          (quit-config (gnus-group-quit-config group))
3738          (did-select (and new-group (gnus-select-newsgroup
3739                                      group show-all select-articles))))
3740     (cond
3741      ;; This summary buffer exists already, so we just select it.
3742      ((not new-group)
3743       (gnus-set-global-variables)
3744       (when kill-buffer
3745         (gnus-kill-or-deaden-summary kill-buffer))
3746       (gnus-configure-windows 'summary 'force)
3747       (gnus-set-mode-line 'summary)
3748       (gnus-summary-position-point)
3749       (message "")
3750       t)
3751      ;; We couldn't select this group.
3752      ((null did-select)
3753       (when (and (eq major-mode 'gnus-summary-mode)
3754                  (not (equal (current-buffer) kill-buffer)))
3755         (kill-buffer (current-buffer))
3756         (if (not quit-config)
3757             (progn
3758               ;; Update the info -- marks might need to be removed,
3759               ;; for instance.
3760               (gnus-summary-update-info)
3761               (set-buffer gnus-group-buffer)
3762               (gnus-group-jump-to-group group)
3763               (gnus-group-next-unread-group 1))
3764           (gnus-handle-ephemeral-exit quit-config)))
3765       (let ((grpinfo (gnus-get-info group)))
3766         (if (null (gnus-info-read grpinfo))
3767             (gnus-message 3 "Group %s contains no messages"
3768                           (gnus-group-decoded-name group))
3769           (gnus-message 3 "Can't select group")))
3770       nil)
3771      ;; The user did a `C-g' while prompting for number of articles,
3772      ;; so we exit this group.
3773      ((eq did-select 'quit)
3774       (and (eq major-mode 'gnus-summary-mode)
3775            (not (equal (current-buffer) kill-buffer))
3776            (kill-buffer (current-buffer)))
3777       (when kill-buffer
3778         (gnus-kill-or-deaden-summary kill-buffer))
3779       (if (not quit-config)
3780           (progn
3781             (set-buffer gnus-group-buffer)
3782             (gnus-group-jump-to-group group)
3783             (gnus-group-next-unread-group 1)
3784             (gnus-configure-windows 'group 'force))
3785         (gnus-handle-ephemeral-exit quit-config))
3786       ;; Finally signal the quit.
3787       (signal 'quit nil))
3788      ;; The group was successfully selected.
3789      (t
3790       (gnus-set-global-variables)
3791       ;; Save the active value in effect when the group was entered.
3792       (setq gnus-newsgroup-active
3793             (gnus-copy-sequence
3794              (gnus-active gnus-newsgroup-name)))
3795       ;; You can change the summary buffer in some way with this hook.
3796       (gnus-run-hooks 'gnus-select-group-hook)
3797       (when (memq 'summary (gnus-update-format-specifications
3798                             nil 'summary 'summary-mode 'summary-dummy))
3799         ;; The format specification for the summary line was updated,
3800         ;; so we need to update the mark positions as well.
3801         (gnus-update-summary-mark-positions))
3802       ;; Do score processing.
3803       (when gnus-use-scoring
3804         (gnus-possibly-score-headers))
3805       ;; Check whether to fill in the gaps in the threads.
3806       (when gnus-build-sparse-threads
3807         (gnus-build-sparse-threads))
3808       ;; Find the initial limit.
3809       (if show-all
3810           (let ((gnus-newsgroup-dormant nil))
3811             (gnus-summary-initial-limit show-all))
3812         (gnus-summary-initial-limit show-all))
3813       ;; Generate the summary buffer.
3814       (unless no-display
3815         (gnus-summary-prepare))
3816       (when gnus-use-trees
3817         (gnus-tree-open group)
3818         (setq gnus-summary-highlight-line-function
3819               'gnus-tree-highlight-article))
3820       ;; If the summary buffer is empty, but there are some low-scored
3821       ;; articles or some excluded dormants, we include these in the
3822       ;; buffer.
3823       (when (and (zerop (buffer-size))
3824                  (not no-display))
3825         (cond (gnus-newsgroup-dormant
3826                (gnus-summary-limit-include-dormant))
3827               ((and gnus-newsgroup-scored show-all)
3828                (gnus-summary-limit-include-expunged t))))
3829       ;; Function `gnus-apply-kill-file' must be called in this hook.
3830       (gnus-run-hooks 'gnus-apply-kill-hook)
3831       (if (and (zerop (buffer-size))
3832                (not no-display))
3833           (progn
3834             ;; This newsgroup is empty.
3835             (gnus-summary-catchup-and-exit nil t)
3836             (gnus-message 6 "No unread news")
3837             (when kill-buffer
3838               (gnus-kill-or-deaden-summary kill-buffer))
3839             ;; Return nil from this function.
3840             nil)
3841         ;; Hide conversation thread subtrees.  We cannot do this in
3842         ;; gnus-summary-prepare-hook since kill processing may not
3843         ;; work with hidden articles.
3844         (gnus-summary-maybe-hide-threads)
3845         (when kill-buffer
3846           (gnus-kill-or-deaden-summary kill-buffer))
3847         (gnus-summary-auto-select-subject)
3848         ;; Show first unread article if requested.
3849         (if (and (not no-article)
3850                  (not no-display)
3851                  gnus-newsgroup-unreads
3852                  gnus-auto-select-first)
3853             (progn
3854               (gnus-configure-windows 'summary)
3855               (let ((art (gnus-summary-article-number)))
3856                 (unless (and (not gnus-plugged)
3857                              (or (memq art gnus-newsgroup-undownloaded)
3858                                  (memq art gnus-newsgroup-downloadable)))
3859                   (gnus-summary-goto-article art))))
3860           ;; Don't select any articles.
3861           (gnus-summary-position-point)
3862           (gnus-configure-windows 'summary 'force)
3863           (gnus-set-mode-line 'summary))
3864         (when (and gnus-auto-center-group
3865                    (get-buffer-window gnus-group-buffer t))
3866           ;; Gotta use windows, because recenter does weird stuff if
3867           ;; the current buffer ain't the displayed window.
3868           (let ((owin (selected-window)))
3869             (select-window (get-buffer-window gnus-group-buffer t))
3870             (when (gnus-group-goto-group group)
3871               (recenter))
3872             (select-window owin)))
3873         ;; Mark this buffer as "prepared".
3874         (setq gnus-newsgroup-prepared t)
3875         (gnus-run-hooks 'gnus-summary-prepared-hook)
3876         (unless (gnus-ephemeral-group-p group)
3877           (gnus-group-update-group group))
3878         t)))))
3879
3880 (defun gnus-summary-auto-select-subject ()
3881   "Select the subject line on initial group entry."
3882   (goto-char (point-min))
3883   (cond
3884    ((eq gnus-auto-select-subject 'best)
3885     (gnus-summary-best-unread-subject))
3886    ((eq gnus-auto-select-subject 'unread)
3887     (gnus-summary-first-unread-subject))
3888    ((eq gnus-auto-select-subject 'unseen)
3889     (gnus-summary-first-unseen-subject))
3890    ((eq gnus-auto-select-subject 'unseen-or-unread)
3891     (gnus-summary-first-unseen-or-unread-subject))
3892    ((eq gnus-auto-select-subject 'first)
3893     ;; Do nothing.
3894     )
3895    ((functionp gnus-auto-select-subject)
3896     (funcall gnus-auto-select-subject))))
3897
3898 (defun gnus-summary-prepare ()
3899   "Generate the summary buffer."
3900   (interactive)
3901   (let ((buffer-read-only nil))
3902     (erase-buffer)
3903     (setq gnus-newsgroup-data nil
3904           gnus-newsgroup-data-reverse nil)
3905     (gnus-run-hooks 'gnus-summary-generate-hook)
3906     ;; Generate the buffer, either with threads or without.
3907     (when gnus-newsgroup-headers
3908       (gnus-summary-prepare-threads
3909        (if gnus-show-threads
3910            (gnus-sort-gathered-threads
3911             (funcall gnus-summary-thread-gathering-function
3912                      (gnus-sort-threads
3913                       (gnus-cut-threads (gnus-make-threads)))))
3914          ;; Unthreaded display.
3915          (gnus-sort-articles gnus-newsgroup-headers))))
3916     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3917     ;; Call hooks for modifying summary buffer.
3918     (goto-char (point-min))
3919     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3920
3921 (defsubst gnus-general-simplify-subject (subject)
3922   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3923   (setq subject
3924         (cond
3925          ;; Truncate the subject.
3926          (gnus-simplify-subject-functions
3927           (gnus-map-function gnus-simplify-subject-functions subject))
3928          ((numberp gnus-summary-gather-subject-limit)
3929           (setq subject (gnus-simplify-subject-re subject))
3930           (if (> (length subject) gnus-summary-gather-subject-limit)
3931               (substring subject 0 gnus-summary-gather-subject-limit)
3932             subject))
3933          ;; Fuzzily simplify it.
3934          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3935           (gnus-simplify-subject-fuzzy subject))
3936          ;; Just remove the leading "Re:".
3937          (t
3938           (gnus-simplify-subject-re subject))))
3939
3940   (if (and gnus-summary-gather-exclude-subject
3941            (string-match gnus-summary-gather-exclude-subject subject))
3942       nil                         ; This article shouldn't be gathered
3943     subject))
3944
3945 (defun gnus-summary-simplify-subject-query ()
3946   "Query where the respool algorithm would put this article."
3947   (interactive)
3948   (gnus-summary-select-article)
3949   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3950
3951 (defun gnus-gather-threads-by-subject (threads)
3952   "Gather threads by looking at Subject headers."
3953   (if (not gnus-summary-make-false-root)
3954       threads
3955     (let ((hashtb (gnus-make-hashtable 1024))
3956           (prev threads)
3957           (result threads)
3958           subject hthread whole-subject)
3959       (while threads
3960         (setq subject (gnus-general-simplify-subject
3961                        (setq whole-subject (mail-header-subject
3962                                             (caar threads)))))
3963         (when subject
3964           (if (setq hthread (gnus-gethash subject hashtb))
3965               (progn
3966                 ;; We enter a dummy root into the thread, if we
3967                 ;; haven't done that already.
3968                 (unless (stringp (caar hthread))
3969                   (setcar hthread (list whole-subject (car hthread))))
3970                 ;; We add this new gathered thread to this gathered
3971                 ;; thread.
3972                 (setcdr (car hthread)
3973                         (nconc (cdar hthread) (list (car threads))))
3974                 ;; Remove it from the list of threads.
3975                 (setcdr prev (cdr threads))
3976                 (setq threads prev))
3977             ;; Enter this thread into the hash table.
3978             (gnus-sethash subject
3979                           (if gnus-summary-make-false-root-always
3980                               (progn
3981                                 ;; If you want a dummy root above all
3982                                 ;; threads...
3983                                 (setcar threads (list whole-subject
3984                                                       (car threads)))
3985                                 threads)
3986                             threads)
3987                           hashtb)))
3988         (setq prev threads)
3989         (setq threads (cdr threads)))
3990       result)))
3991
3992 (defun gnus-gather-threads-by-references (threads)
3993   "Gather threads by looking at References headers."
3994   (let ((idhashtb (gnus-make-hashtable 1024))
3995         (thhashtb (gnus-make-hashtable 1024))
3996         (prev threads)
3997         (result threads)
3998         ids references id gthread gid entered ref)
3999     (while threads
4000       (when (setq references (mail-header-references (caar threads)))
4001         (setq id (mail-header-id (caar threads))
4002               ids (inline (gnus-split-references references))
4003               entered nil)
4004         (while (setq ref (pop ids))
4005           (setq ids (delete ref ids))
4006           (if (not (setq gid (gnus-gethash ref idhashtb)))
4007               (progn
4008                 (gnus-sethash ref id idhashtb)
4009                 (gnus-sethash id threads thhashtb))
4010             (setq gthread (gnus-gethash gid thhashtb))
4011             (unless entered
4012               ;; We enter a dummy root into the thread, if we
4013               ;; haven't done that already.
4014               (unless (stringp (caar gthread))
4015                 (setcar gthread (list (mail-header-subject (caar gthread))
4016                                       (car gthread))))
4017               ;; We add this new gathered thread to this gathered
4018               ;; thread.
4019               (setcdr (car gthread)
4020                       (nconc (cdar gthread) (list (car threads)))))
4021             ;; Add it into the thread hash table.
4022             (gnus-sethash id gthread thhashtb)
4023             (setq entered t)
4024             ;; Remove it from the list of threads.
4025             (setcdr prev (cdr threads))
4026             (setq threads prev))))
4027       (setq prev threads)
4028       (setq threads (cdr threads)))
4029     result))
4030
4031 (defun gnus-sort-gathered-threads (threads)
4032   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4033   (let ((result threads))
4034     (while threads
4035       (when (stringp (caar threads))
4036         (setcdr (car threads)
4037                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4038       (setq threads (cdr threads)))
4039     result))
4040
4041 (defun gnus-thread-loop-p (root thread)
4042   "Say whether ROOT is in THREAD."
4043   (let ((stack (list thread))
4044         (infloop 0)
4045         th)
4046     (while (setq thread (pop stack))
4047       (setq th (cdr thread))
4048       (while (and th
4049                   (not (eq (caar th) root)))
4050         (pop th))
4051       (if th
4052           ;; We have found a loop.
4053           (let (ref-dep)
4054             (setcdr thread (delq (car th) (cdr thread)))
4055             (if (boundp (setq ref-dep (intern "none"
4056                                               gnus-newsgroup-dependencies)))
4057                 (setcdr (symbol-value ref-dep)
4058                         (nconc (cdr (symbol-value ref-dep))
4059                                (list (car th))))
4060               (set ref-dep (list nil (car th))))
4061             (setq infloop 1
4062                   stack nil))
4063         ;; Push all the subthreads onto the stack.
4064         (push (cdr thread) stack)))
4065     infloop))
4066
4067 (defun gnus-make-threads ()
4068   "Go through the dependency hashtb and find the roots.  Return all threads."
4069   (let (threads)
4070     (while (catch 'infloop
4071              (mapatoms
4072               (lambda (refs)
4073                 ;; Deal with self-referencing References loops.
4074                 (when (and (car (symbol-value refs))
4075                            (not (zerop
4076                                  (apply
4077                                   '+
4078                                   (mapcar
4079                                    (lambda (thread)
4080                                      (gnus-thread-loop-p
4081                                       (car (symbol-value refs)) thread))
4082                                    (cdr (symbol-value refs)))))))
4083                   (setq threads nil)
4084                   (throw 'infloop t))
4085                 (unless (car (symbol-value refs))
4086                   ;; These threads do not refer back to any other
4087                   ;; articles, so they're roots.
4088                   (setq threads (append (cdr (symbol-value refs)) threads))))
4089               gnus-newsgroup-dependencies)))
4090     threads))
4091
4092 ;; Build the thread tree.
4093 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4094   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4095
4096 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4097 if it was already present.
4098
4099 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4100 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4101 Message-IDs will be renamed to a unique Message-ID before being
4102 entered.
4103
4104 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4105   (let* ((id (mail-header-id header))
4106          (id-dep (and id (intern id dependencies)))
4107          parent-id ref ref-dep ref-header replaced)
4108     ;; Enter this `header' in the `dependencies' table.
4109     (cond
4110      ((not id-dep)
4111       (setq header nil))
4112      ;; The first two cases do the normal part: enter a new `header'
4113      ;; in the `dependencies' table.
4114      ((not (boundp id-dep))
4115       (set id-dep (list header)))
4116      ((null (car (symbol-value id-dep)))
4117       (setcar (symbol-value id-dep) header))
4118
4119      ;; From here the `header' was already present in the
4120      ;; `dependencies' table.
4121      (force-new
4122       ;; Overrides an existing entry;
4123       ;; just set the header part of the entry.
4124       (setcar (symbol-value id-dep) header)
4125       (setq replaced t))
4126
4127      ;; Renames the existing `header' to a unique Message-ID.
4128      ((not gnus-summary-ignore-duplicates)
4129       ;; An article with this Message-ID has already been seen.
4130       ;; We rename the Message-ID.
4131       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4132            (list header))
4133       (mail-header-set-id header id))
4134
4135      ;; The last case ignores an existing entry, except it adds any
4136      ;; additional Xrefs (in case the two articles came from different
4137      ;; servers.
4138      ;; Also sets `header' to `nil' meaning that the `dependencies'
4139      ;; table was *not* modified.
4140      (t
4141       (mail-header-set-xref
4142        (car (symbol-value id-dep))
4143        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4144                    "")
4145                (or (mail-header-xref header) "")))
4146       (setq header nil)))
4147
4148     (when (and header (not replaced))
4149       ;; First check that we are not creating a References loop.
4150       (setq parent-id (gnus-parent-id (mail-header-references header)))
4151       (setq ref parent-id)
4152       (while (and ref
4153                   (setq ref-dep (intern-soft ref dependencies))
4154                   (boundp ref-dep)
4155                   (setq ref-header (car (symbol-value ref-dep))))
4156         (if (string= id ref)
4157             ;; Yuk!  This is a reference loop.  Make the article be a
4158             ;; root article.
4159             (progn
4160               (mail-header-set-references (car (symbol-value id-dep)) "none")
4161               (setq ref nil)
4162               (setq parent-id nil))
4163           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4164       (setq ref-dep (intern (or parent-id "none") dependencies))
4165       (if (boundp ref-dep)
4166           (setcdr (symbol-value ref-dep)
4167                   (nconc (cdr (symbol-value ref-dep))
4168                          (list (symbol-value id-dep))))
4169         (set ref-dep (list nil (symbol-value id-dep)))))
4170     header))
4171
4172 (defun gnus-extract-message-id-from-in-reply-to (string)
4173   (if (string-match "<[^>]+>" string)
4174       (substring string (match-beginning 0) (match-end 0))
4175     nil))
4176
4177 (defun gnus-build-sparse-threads ()
4178   (let ((headers gnus-newsgroup-headers)
4179         (mail-parse-charset gnus-newsgroup-charset)
4180         (gnus-summary-ignore-duplicates t)
4181         header references generation relations
4182         subject child end new-child date)
4183     ;; First we create an alist of generations/relations, where
4184     ;; generations is how much we trust the relation, and the relation
4185     ;; is parent/child.
4186     (gnus-message 7 "Making sparse threads...")
4187     (save-excursion
4188       (nnheader-set-temp-buffer " *gnus sparse threads*")
4189       (while (setq header (pop headers))
4190         (when (and (setq references (mail-header-references header))
4191                    (not (string= references "")))
4192           (insert references)
4193           (setq child (mail-header-id header)
4194                 subject (mail-header-subject header)
4195                 date (mail-header-date header)
4196                 generation 0)
4197           (while (search-backward ">" nil t)
4198             (setq end (1+ (point)))
4199             (when (search-backward "<" nil t)
4200               (setq new-child (buffer-substring (point) end))
4201               (push (list (incf generation)
4202                           child (setq child new-child)
4203                           subject date)
4204                     relations)))
4205           (when child
4206             (push (list (1+ generation) child nil subject) relations))
4207           (erase-buffer)))
4208       (kill-buffer (current-buffer)))
4209     ;; Sort over trustworthiness.
4210     (mapcar
4211      (lambda (relation)
4212        (when (gnus-dependencies-add-header
4213               (make-full-mail-header
4214                gnus-reffed-article-number
4215                (nth 3 relation) "" (or (nth 4 relation) "")
4216                (nth 1 relation)
4217                (or (nth 2 relation) "") 0 0 "")
4218               gnus-newsgroup-dependencies nil)
4219          (push gnus-reffed-article-number gnus-newsgroup-limit)
4220          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4221          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4222                gnus-newsgroup-reads)
4223          (decf gnus-reffed-article-number)))
4224      (sort relations 'car-less-than-car))
4225     (gnus-message 7 "Making sparse threads...done")))
4226
4227 (defun gnus-build-old-threads ()
4228   ;; Look at all the articles that refer back to old articles, and
4229   ;; fetch the headers for the articles that aren't there.  This will
4230   ;; build complete threads - if the roots haven't been expired by the
4231   ;; server, that is.
4232   (let ((mail-parse-charset gnus-newsgroup-charset)
4233         id heads)
4234     (mapatoms
4235      (lambda (refs)
4236        (when (not (car (symbol-value refs)))
4237          (setq heads (cdr (symbol-value refs)))
4238          (while heads
4239            (if (memq (mail-header-number (caar heads))
4240                      gnus-newsgroup-dormant)
4241                (setq heads (cdr heads))
4242              (setq id (symbol-name refs))
4243              (while (and (setq id (gnus-build-get-header id))
4244                          (not (car (gnus-id-to-thread id)))))
4245              (setq heads nil)))))
4246      gnus-newsgroup-dependencies)))
4247
4248 (defsubst gnus-remove-odd-characters (string)
4249   "Translate STRING into something that doesn't contain weird characters."
4250   (mm-subst-char-in-string
4251    ?\r ?\-
4252    (mm-subst-char-in-string ?\n ?\- string t) t))
4253
4254 ;; This function has to be called with point after the article number
4255 ;; on the beginning of the line.
4256 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4257   (let ((eol (point-at-eol))
4258         (buffer (current-buffer))
4259         header references in-reply-to)
4260
4261     ;; overview: [num subject from date id refs chars lines misc]
4262     (unwind-protect
4263         (let (x)
4264           (narrow-to-region (point) eol)
4265           (unless (eobp)
4266             (forward-char))
4267
4268           (setq header
4269                 (make-full-mail-header
4270                  number                 ; number
4271                  (condition-case ()     ; subject
4272                      (gnus-remove-odd-characters
4273                       (funcall gnus-decode-encoded-word-function
4274                                (setq x (nnheader-nov-field))))
4275                    (error x))
4276                  (condition-case ()     ; from
4277                      (gnus-remove-odd-characters
4278                       (funcall gnus-decode-encoded-word-function
4279                                (setq x (nnheader-nov-field))))
4280                    (error x))
4281                  (nnheader-nov-field)   ; date
4282                  (nnheader-nov-read-message-id number)  ; id
4283                  (setq references (nnheader-nov-field)) ; refs
4284                  (nnheader-nov-read-integer) ; chars
4285                  (nnheader-nov-read-integer) ; lines
4286                  (unless (eobp)
4287                    (if (looking-at "Xref: ")
4288                        (goto-char (match-end 0)))
4289                    (nnheader-nov-field)) ; Xref
4290                  (nnheader-nov-parse-extra)))) ; extra
4291
4292       (widen))
4293
4294     (when (and (string= references "")
4295                (setq in-reply-to (mail-header-extra header))
4296                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4297       (mail-header-set-references
4298        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4299
4300     (when gnus-alter-header-function
4301       (funcall gnus-alter-header-function header))
4302     (gnus-dependencies-add-header header dependencies force-new)))
4303
4304 (defun gnus-build-get-header (id)
4305   "Look through the buffer of NOV lines and find the header to ID.
4306 Enter this line into the dependencies hash table, and return
4307 the id of the parent article (if any)."
4308   (let ((deps gnus-newsgroup-dependencies)
4309         found header)
4310     (prog1
4311         (save-excursion
4312           (set-buffer nntp-server-buffer)
4313           (let ((case-fold-search nil))
4314             (goto-char (point-min))
4315             (while (and (not found)
4316                         (search-forward id nil t))
4317               (beginning-of-line)
4318               (setq found (looking-at
4319                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4320                                    (regexp-quote id))))
4321               (or found (beginning-of-line 2)))
4322             (when found
4323               (beginning-of-line)
4324               (and
4325                (setq header (gnus-nov-parse-line
4326                              (read (current-buffer)) deps))
4327                (gnus-parent-id (mail-header-references header))))))
4328       (when header
4329         (let ((number (mail-header-number header)))
4330           (push number gnus-newsgroup-limit)
4331           (push header gnus-newsgroup-headers)
4332           (if (memq number gnus-newsgroup-unselected)
4333               (progn
4334                 (setq gnus-newsgroup-unreads
4335                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4336                                                number))
4337                 (setq gnus-newsgroup-unselected
4338                       (delq number gnus-newsgroup-unselected)))
4339             (push number gnus-newsgroup-ancient)))))))
4340
4341 (defun gnus-build-all-threads ()
4342   "Read all the headers."
4343   (let ((gnus-summary-ignore-duplicates t)
4344         (mail-parse-charset gnus-newsgroup-charset)
4345         (dependencies gnus-newsgroup-dependencies)
4346         header article)
4347     (save-excursion
4348       (set-buffer nntp-server-buffer)
4349       (let ((case-fold-search nil))
4350         (goto-char (point-min))
4351         (while (not (eobp))
4352           (ignore-errors
4353             (setq article (read (current-buffer))
4354                   header (gnus-nov-parse-line article dependencies)))
4355           (when header
4356             (with-current-buffer gnus-summary-buffer
4357               (push header gnus-newsgroup-headers)
4358               (if (memq (setq article (mail-header-number header))
4359                         gnus-newsgroup-unselected)
4360                   (progn
4361                     (setq gnus-newsgroup-unreads
4362                           (gnus-add-to-sorted-list
4363                            gnus-newsgroup-unreads article))
4364                     (setq gnus-newsgroup-unselected
4365                           (delq article gnus-newsgroup-unselected)))
4366                 (push article gnus-newsgroup-ancient)))
4367             (forward-line 1)))))))
4368
4369 (defun gnus-summary-update-article-line (article header)
4370   "Update the line for ARTICLE using HEADER."
4371   (let* ((id (mail-header-id header))
4372          (thread (gnus-id-to-thread id)))
4373     (unless thread
4374       (error "Article in no thread"))
4375     ;; Update the thread.
4376     (setcar thread header)
4377     (gnus-summary-goto-subject article)
4378     (let* ((datal (gnus-data-find-list article))
4379            (data (car datal))
4380            (buffer-read-only nil)
4381            (level (gnus-summary-thread-level)))
4382       (gnus-delete-line)
4383       (let ((inserted (- (point)
4384                          (progn
4385                            (gnus-summary-insert-line
4386                             header level nil
4387                             (memq article gnus-newsgroup-undownloaded)
4388                             (gnus-article-mark article)
4389                             (memq article gnus-newsgroup-replied)
4390                             (memq article gnus-newsgroup-expirable)
4391                             ;; Only insert the Subject string when it's different
4392                             ;; from the previous Subject string.
4393                             (if (and
4394                                  gnus-show-threads
4395                                  (gnus-subject-equal
4396                                   (condition-case ()
4397                                       (mail-header-subject
4398                                        (gnus-data-header
4399                                         (cadr
4400                                          (gnus-data-find-list
4401                                           article
4402                                           (gnus-data-list t)))))
4403                                     ;; Error on the side of excessive subjects.
4404                                     (error ""))
4405                                   (mail-header-subject header)))
4406                                 ""
4407                               (mail-header-subject header))
4408                             nil (cdr (assq article gnus-newsgroup-scored))
4409                             (memq article gnus-newsgroup-processable))
4410                            (point)))))
4411         (when (cdr datal)
4412           (gnus-data-update-list
4413            (cdr datal)
4414            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4415
4416 (defun gnus-summary-update-article (article &optional iheader)
4417   "Update ARTICLE in the summary buffer."
4418   (set-buffer gnus-summary-buffer)
4419   (let* ((header (gnus-summary-article-header article))
4420          (id (mail-header-id header))
4421          (data (gnus-data-find article))
4422          (thread (gnus-id-to-thread id))
4423          (references (mail-header-references header))
4424          (parent
4425           (gnus-id-to-thread
4426            (or (gnus-parent-id
4427                 (when (and references
4428                            (not (equal "" references)))
4429                   references))
4430                "none")))
4431          (buffer-read-only nil)
4432          (old (car thread)))
4433     (when thread
4434       (unless iheader
4435         (setcar thread nil)
4436         (when parent
4437           (delq thread parent)))
4438       (if (gnus-summary-insert-subject id header)
4439           ;; Set the (possibly) new article number in the data structure.
4440           (gnus-data-set-number data (gnus-id-to-article id))
4441         (setcar thread old)
4442         nil))))
4443
4444 (defun gnus-rebuild-thread (id &optional line)
4445   "Rebuild the thread containing ID.
4446 If LINE, insert the rebuilt thread starting on line LINE."
4447   (let ((buffer-read-only nil)
4448         old-pos current thread data)
4449     (if (not gnus-show-threads)
4450         (setq thread (list (car (gnus-id-to-thread id))))
4451       ;; Get the thread this article is part of.
4452       (setq thread (gnus-remove-thread id)))
4453     (setq old-pos (point-at-bol))
4454     (setq current (save-excursion
4455                     (and (re-search-backward "[\r\n]" nil t)
4456                          (gnus-summary-article-number))))
4457     ;; If this is a gathered thread, we have to go some re-gathering.
4458     (when (stringp (car thread))
4459       (let ((subject (car thread))
4460             roots thr)
4461         (setq thread (cdr thread))
4462         (while thread
4463           (unless (memq (setq thr (gnus-id-to-thread
4464                                    (gnus-root-id
4465                                     (mail-header-id (caar thread)))))
4466                         roots)
4467             (push thr roots))
4468           (setq thread (cdr thread)))
4469         ;; We now have all (unique) roots.
4470         (if (= (length roots) 1)
4471             ;; All the loose roots are now one solid root.
4472             (setq thread (car roots))
4473           (setq thread (cons subject (gnus-sort-threads roots))))))
4474     (let (threads)
4475       ;; We then insert this thread into the summary buffer.
4476       (when line
4477         (goto-char (point-min))
4478         (forward-line (1- line)))
4479       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4480         (if gnus-show-threads
4481             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4482           (gnus-summary-prepare-unthreaded thread))
4483         (setq data (nreverse gnus-newsgroup-data))
4484         (setq threads gnus-newsgroup-threads))
4485       ;; We splice the new data into the data structure.
4486       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4487       ;;!!! then we want to insert at the beginning of the buffer.
4488       ;;!!! That happens to be true with Gnus now, but that may
4489       ;;!!! change in the future.  Perhaps.
4490       (gnus-data-enter-list
4491        (if line nil current) data (- (point) old-pos))
4492       (setq gnus-newsgroup-threads
4493             (nconc threads gnus-newsgroup-threads))
4494       (gnus-data-compute-positions))))
4495
4496 (defun gnus-number-to-header (number)
4497   "Return the header for article NUMBER."
4498   (let ((headers gnus-newsgroup-headers))
4499     (while (and headers
4500                 (not (= number (mail-header-number (car headers)))))
4501       (pop headers))
4502     (when headers
4503       (car headers))))
4504
4505 (defun gnus-parent-headers (in-headers &optional generation)
4506   "Return the headers of the GENERATIONeth parent of HEADERS."
4507   (unless generation
4508     (setq generation 1))
4509   (let ((parent t)
4510         (headers in-headers)
4511         references)
4512     (while (and parent
4513                 (not (zerop generation))
4514                 (setq references (mail-header-references headers)))
4515       (setq headers (if (and references
4516                              (setq parent (gnus-parent-id references)))
4517                         (car (gnus-id-to-thread parent))
4518                       nil))
4519       (decf generation))
4520     (and (not (eq headers in-headers))
4521          headers)))
4522
4523 (defun gnus-id-to-thread (id)
4524   "Return the (sub-)thread where ID appears."
4525   (gnus-gethash id gnus-newsgroup-dependencies))
4526
4527 (defun gnus-id-to-article (id)
4528   "Return the article number of ID."
4529   (let ((thread (gnus-id-to-thread id)))
4530     (when (and thread
4531                (car thread))
4532       (mail-header-number (car thread)))))
4533
4534 (defun gnus-id-to-header (id)
4535   "Return the article headers of ID."
4536   (car (gnus-id-to-thread id)))
4537
4538 (defun gnus-article-displayed-root-p (article)
4539   "Say whether ARTICLE is a root(ish) article."
4540   (let ((level (gnus-summary-thread-level article))
4541         (refs (mail-header-references  (gnus-summary-article-header article)))
4542         particle)
4543     (cond
4544      ((null level) nil)
4545      ((zerop level) t)
4546      ((null refs) t)
4547      ((null (gnus-parent-id refs)) t)
4548      ((and (= 1 level)
4549            (null (setq particle (gnus-id-to-article
4550                                  (gnus-parent-id refs))))
4551            (null (gnus-summary-thread-level particle)))))))
4552
4553 (defun gnus-root-id (id)
4554   "Return the id of the root of the thread where ID appears."
4555   (let (last-id prev)
4556     (while (and id (setq prev (car (gnus-id-to-thread id))))
4557       (setq last-id id
4558             id (gnus-parent-id (mail-header-references prev))))
4559     last-id))
4560
4561 (defun gnus-articles-in-thread (thread)
4562   "Return the list of articles in THREAD."
4563   (cons (mail-header-number (car thread))
4564         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4565
4566 (defun gnus-remove-thread (id &optional dont-remove)
4567   "Remove the thread that has ID in it."
4568   (let (headers thread last-id)
4569     ;; First go up in this thread until we find the root.
4570     (setq last-id (gnus-root-id id)
4571           headers (message-flatten-list (gnus-id-to-thread last-id)))
4572     ;; We have now found the real root of this thread.  It might have
4573     ;; been gathered into some loose thread, so we have to search
4574     ;; through the threads to find the thread we wanted.
4575     (let ((threads gnus-newsgroup-threads)
4576           sub)
4577       (while threads
4578         (setq sub (car threads))
4579         (if (stringp (car sub))
4580             ;; This is a gathered thread, so we look at the roots
4581             ;; below it to find whether this article is in this
4582             ;; gathered root.
4583             (progn
4584               (setq sub (cdr sub))
4585               (while sub
4586                 (when (member (caar sub) headers)
4587                   (setq thread (car threads)
4588                         threads nil
4589                         sub nil))
4590                 (setq sub (cdr sub))))
4591           ;; It's an ordinary thread, so we check it.
4592           (when (eq (car sub) (car headers))
4593             (setq thread sub
4594                   threads nil)))
4595         (setq threads (cdr threads)))
4596       ;; If this article is in no thread, then it's a root.
4597       (if thread
4598           (unless dont-remove
4599             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4600         (setq thread (gnus-id-to-thread last-id)))
4601       (when thread
4602         (prog1
4603             thread                      ; We return this thread.
4604           (unless dont-remove
4605             (if (stringp (car thread))
4606                 (progn
4607                   ;; If we use dummy roots, then we have to remove the
4608                   ;; dummy root as well.
4609                   (when (eq gnus-summary-make-false-root 'dummy)
4610                     ;; We go to the dummy root by going to
4611                     ;; the first sub-"thread", and then one line up.
4612                     (gnus-summary-goto-article
4613                      (mail-header-number (caadr thread)))
4614                     (forward-line -1)
4615                     (gnus-delete-line)
4616                     (gnus-data-compute-positions))
4617                   (setq thread (cdr thread))
4618                   (while thread
4619                     (gnus-remove-thread-1 (car thread))
4620                     (setq thread (cdr thread))))
4621               (gnus-remove-thread-1 thread))))))))
4622
4623 (defun gnus-remove-thread-1 (thread)
4624   "Remove the thread THREAD recursively."
4625   (let ((number (mail-header-number (pop thread)))
4626         d)
4627     (setq thread (reverse thread))
4628     (while thread
4629       (gnus-remove-thread-1 (pop thread)))
4630     (when (setq d (gnus-data-find number))
4631       (goto-char (gnus-data-pos d))
4632       (gnus-summary-show-thread)
4633       (gnus-data-remove
4634        number
4635        (- (point-at-bol)
4636           (prog1
4637               (1+ (point-at-eol))
4638             (gnus-delete-line)))))))
4639
4640 (defun gnus-sort-threads-1 (threads func)
4641   (sort (mapcar (lambda (thread)
4642                   (cons (car thread)
4643                         (and (cdr thread)
4644                              (gnus-sort-threads-1 (cdr thread) func))))
4645                 threads) func))
4646
4647 (defun gnus-sort-threads (threads)
4648   "Sort THREADS."
4649   (if (not gnus-thread-sort-functions)
4650       threads
4651     (gnus-message 8 "Sorting threads...")
4652     (let ((max-lisp-eval-depth 5000))
4653       (prog1 (gnus-sort-threads-1
4654          threads
4655          (gnus-make-sort-function gnus-thread-sort-functions))
4656         (gnus-message 8 "Sorting threads...done")))))
4657
4658 (defun gnus-sort-articles (articles)
4659   "Sort ARTICLES."
4660   (when gnus-article-sort-functions
4661     (gnus-message 7 "Sorting articles...")
4662     (prog1
4663         (setq gnus-newsgroup-headers
4664               (sort articles (gnus-make-sort-function
4665                               gnus-article-sort-functions)))
4666       (gnus-message 7 "Sorting articles...done"))))
4667
4668 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4669 (defmacro gnus-thread-header (thread)
4670   "Return header of first article in THREAD.
4671 Note that THREAD must never, ever be anything else than a variable -
4672 using some other form will lead to serious barfage."
4673   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4674   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4675   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4676         (vector thread) 2))
4677
4678 (defsubst gnus-article-sort-by-number (h1 h2)
4679   "Sort articles by article number."
4680   (< (mail-header-number h1)
4681      (mail-header-number h2)))
4682
4683 (defun gnus-thread-sort-by-number (h1 h2)
4684   "Sort threads by root article number."
4685   (gnus-article-sort-by-number
4686    (gnus-thread-header h1) (gnus-thread-header h2)))
4687
4688 (defsubst gnus-article-sort-by-random (h1 h2)
4689   "Sort articles by article number."
4690   (zerop (random 2)))
4691
4692 (defun gnus-thread-sort-by-random (h1 h2)
4693   "Sort threads by root article number."
4694   (gnus-article-sort-by-random
4695    (gnus-thread-header h1) (gnus-thread-header h2)))
4696
4697 (defsubst gnus-article-sort-by-lines (h1 h2)
4698   "Sort articles by article Lines header."
4699   (< (mail-header-lines h1)
4700      (mail-header-lines h2)))
4701
4702 (defun gnus-thread-sort-by-lines (h1 h2)
4703   "Sort threads by root article Lines header."
4704   (gnus-article-sort-by-lines
4705    (gnus-thread-header h1) (gnus-thread-header h2)))
4706
4707 (defsubst gnus-article-sort-by-chars (h1 h2)
4708   "Sort articles by octet length."
4709   (< (mail-header-chars h1)
4710      (mail-header-chars h2)))
4711
4712 (defun gnus-thread-sort-by-chars (h1 h2)
4713   "Sort threads by root article octet length."
4714   (gnus-article-sort-by-chars
4715    (gnus-thread-header h1) (gnus-thread-header h2)))
4716
4717 (defsubst gnus-article-sort-by-author (h1 h2)
4718   "Sort articles by root author."
4719   (string-lessp
4720    (let ((extract (funcall
4721                    gnus-extract-address-components
4722                    (mail-header-from h1))))
4723      (or (car extract) (cadr extract) ""))
4724    (let ((extract (funcall
4725                    gnus-extract-address-components
4726                    (mail-header-from h2))))
4727      (or (car extract) (cadr extract) ""))))
4728
4729 (defun gnus-thread-sort-by-author (h1 h2)
4730   "Sort threads by root author."
4731   (gnus-article-sort-by-author
4732    (gnus-thread-header h1)  (gnus-thread-header h2)))
4733
4734 (defsubst gnus-article-sort-by-recipient (h1 h2)
4735   "Sort articles by recipient."
4736   (string-lessp
4737    (let ((extract (funcall
4738                    gnus-extract-address-components
4739                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4740      (or (car extract) (cadr extract)))
4741    (let ((extract (funcall
4742                    gnus-extract-address-components
4743                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4744      (or (car extract) (cadr extract)))))
4745
4746 (defun gnus-thread-sort-by-recipient (h1 h2)
4747   "Sort threads by root recipient."
4748   (gnus-article-sort-by-recipient
4749    (gnus-thread-header h1) (gnus-thread-header h2)))
4750
4751 (defsubst gnus-article-sort-by-subject (h1 h2)
4752   "Sort articles by root subject."
4753   (string-lessp
4754    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4755    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4756
4757 (defun gnus-thread-sort-by-subject (h1 h2)
4758   "Sort threads by root subject."
4759   (gnus-article-sort-by-subject
4760    (gnus-thread-header h1) (gnus-thread-header h2)))
4761
4762 (defsubst gnus-article-sort-by-date (h1 h2)
4763   "Sort articles by root article date."
4764   (time-less-p
4765    (gnus-date-get-time (mail-header-date h1))
4766    (gnus-date-get-time (mail-header-date h2))))
4767
4768 (defun gnus-thread-sort-by-date (h1 h2)
4769   "Sort threads by root article date."
4770   (gnus-article-sort-by-date
4771    (gnus-thread-header h1) (gnus-thread-header h2)))
4772
4773 (defsubst gnus-article-sort-by-score (h1 h2)
4774   "Sort articles by root article score.
4775 Unscored articles will be counted as having a score of zero."
4776   (> (or (cdr (assq (mail-header-number h1)
4777                     gnus-newsgroup-scored))
4778          gnus-summary-default-score 0)
4779      (or (cdr (assq (mail-header-number h2)
4780                     gnus-newsgroup-scored))
4781          gnus-summary-default-score 0)))
4782
4783 (defun gnus-thread-sort-by-score (h1 h2)
4784   "Sort threads by root article score."
4785   (gnus-article-sort-by-score
4786    (gnus-thread-header h1) (gnus-thread-header h2)))
4787
4788 (defun gnus-thread-sort-by-total-score (h1 h2)
4789   "Sort threads by the sum of all scores in the thread.
4790 Unscored articles will be counted as having a score of zero."
4791   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4792
4793 (defun gnus-thread-total-score (thread)
4794   ;; This function find the total score of THREAD.
4795   (cond
4796    ((null thread)
4797     0)
4798    ((consp thread)
4799     (if (stringp (car thread))
4800         (apply gnus-thread-score-function 0
4801                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4802       (gnus-thread-total-score-1 thread)))
4803    (t
4804     (gnus-thread-total-score-1 (list thread)))))
4805
4806 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4807   "Sort threads such that the thread with the most recently arrived article comes first."
4808   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4809
4810 (defun gnus-thread-highest-number (thread)
4811   "Return the highest article number in THREAD."
4812   (apply 'max (mapcar (lambda (header)
4813                         (mail-header-number header))
4814                       (message-flatten-list thread))))
4815
4816 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4817   "Sort threads such that the thread with the most recently dated article comes first."
4818   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4819
4820 (defun gnus-thread-latest-date (thread)
4821   "Return the highest article date in THREAD."
4822   (let ((previous-time 0))
4823     (apply 'max
4824            (mapcar
4825             (lambda (header)
4826               (setq previous-time
4827                     (condition-case ()
4828                         (time-to-seconds (mail-header-parse-date
4829                                           (mail-header-date header)))
4830                       (error previous-time))))
4831             (sort
4832              (message-flatten-list thread)
4833              (lambda (h1 h2)
4834                (< (mail-header-number h1)
4835                   (mail-header-number h2))))))))
4836
4837 (defun gnus-thread-total-score-1 (root)
4838   ;; This function find the total score of the thread below ROOT.
4839   (setq root (car root))
4840   (apply gnus-thread-score-function
4841          (or (append
4842               (mapcar 'gnus-thread-total-score
4843                       (cdr (gnus-id-to-thread (mail-header-id root))))
4844               (when (> (mail-header-number root) 0)
4845                 (list (or (cdr (assq (mail-header-number root)
4846                                      gnus-newsgroup-scored))
4847                           gnus-summary-default-score 0))))
4848              (list gnus-summary-default-score)
4849              '(0))))
4850
4851 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4852 (defvar gnus-tmp-prev-subject nil)
4853 (defvar gnus-tmp-false-parent nil)
4854 (defvar gnus-tmp-root-expunged nil)
4855 (defvar gnus-tmp-dummy-line nil)
4856
4857 (eval-when-compile (defvar gnus-tmp-header))
4858 (defun gnus-extra-header (type &optional header)
4859   "Return the extra header of TYPE."
4860   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4861       ""))
4862
4863 (defvar gnus-tmp-thread-tree-header-string "")
4864
4865 (defcustom gnus-sum-thread-tree-root "> "
4866   "With %B spec, used for the root of a thread.
4867 If nil, use subject instead."
4868   :version "22.1"
4869   :type '(radio (const :format "%v  " nil) string)
4870   :group 'gnus-thread)
4871
4872 (defcustom gnus-sum-thread-tree-false-root "> "
4873   "With %B spec, used for a false root of a thread.
4874 If nil, use subject instead."
4875   :version "22.1"
4876   :type '(radio (const :format "%v  " nil) string)
4877   :group 'gnus-thread)
4878
4879 (defcustom gnus-sum-thread-tree-single-indent ""
4880   "With %B spec, used for a thread with just one message.
4881 If nil, use subject instead."
4882   :version "22.1"
4883   :type '(radio (const :format "%v  " nil) string)
4884   :group 'gnus-thread)
4885
4886 (defcustom gnus-sum-thread-tree-vertical "| "
4887   "With %B spec, used for drawing a vertical line."
4888   :version "22.1"
4889   :type 'string
4890   :group 'gnus-thread)
4891
4892 (defcustom gnus-sum-thread-tree-indent "  "
4893   "With %B spec, used for indenting."
4894   :version "22.1"
4895   :type 'string
4896   :group 'gnus-thread)
4897
4898 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4899   "With %B spec, used for a leaf with brothers."
4900   :version "22.1"
4901   :type 'string
4902   :group 'gnus-thread)
4903
4904 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4905   "With %B spec, used for a leaf without brothers."
4906   :version "22.1"
4907   :type 'string
4908   :group 'gnus-thread)
4909
4910 (defun gnus-summary-prepare-threads (threads)
4911   "Prepare summary buffer from THREADS and indentation LEVEL.
4912 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4913 or a straight list of headers."
4914   (gnus-message 7 "Generating summary...")
4915
4916   (setq gnus-newsgroup-threads threads)
4917   (beginning-of-line)
4918
4919   (let ((gnus-tmp-level 0)
4920         (default-score (or gnus-summary-default-score 0))
4921         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4922         (building-line-count gnus-summary-display-while-building)
4923         (building-count (integerp gnus-summary-display-while-building))
4924         thread number subject stack state gnus-tmp-gathered beg-match
4925         new-roots gnus-tmp-new-adopts thread-end simp-subject
4926         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4927         gnus-tmp-replied gnus-tmp-subject-or-nil
4928         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4929         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4930         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4931         tree-stack)
4932
4933     (setq gnus-tmp-prev-subject nil
4934           gnus-tmp-thread-tree-header-string "")
4935
4936     (if (vectorp (car threads))
4937         ;; If this is a straight (sic) list of headers, then a
4938         ;; threaded summary display isn't required, so we just create
4939         ;; an unthreaded one.
4940         (gnus-summary-prepare-unthreaded threads)
4941
4942       ;; Do the threaded display.
4943
4944       (if gnus-summary-display-while-building
4945           (switch-to-buffer (buffer-name)))
4946       (while (or threads stack gnus-tmp-new-adopts new-roots)
4947
4948         (if (and (= gnus-tmp-level 0)
4949                  (or (not stack)
4950                      (= (caar stack) 0))
4951                  (not gnus-tmp-false-parent)
4952                  (or gnus-tmp-new-adopts new-roots))
4953             (if gnus-tmp-new-adopts
4954                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4955                       thread (list (car gnus-tmp-new-adopts))
4956                       gnus-tmp-header (caar thread)
4957                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4958               (when new-roots
4959                 (setq thread (list (car new-roots))
4960                       gnus-tmp-header (caar thread)
4961                       new-roots (cdr new-roots))))
4962
4963           (if threads
4964               ;; If there are some threads, we do them before the
4965               ;; threads on the stack.
4966               (setq thread threads
4967                     gnus-tmp-header (caar thread))
4968             ;; There were no current threads, so we pop something off
4969             ;; the stack.
4970             (setq state (car stack)
4971                   gnus-tmp-level (car state)
4972                   tree-stack (cadr state)
4973                   thread (caddr state)
4974                   stack (cdr stack)
4975                   gnus-tmp-header (caar thread))))
4976
4977         (setq gnus-tmp-false-parent nil)
4978         (setq gnus-tmp-root-expunged nil)
4979         (setq thread-end nil)
4980
4981         (if (stringp gnus-tmp-header)
4982             ;; The header is a dummy root.
4983             (cond
4984              ((eq gnus-summary-make-false-root 'adopt)
4985               ;; We let the first article adopt the rest.
4986               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4987                                                (cddar thread)))
4988               (setq gnus-tmp-gathered
4989                     (nconc (mapcar
4990                             (lambda (h) (mail-header-number (car h)))
4991                             (cddar thread))
4992                            gnus-tmp-gathered))
4993               (setq thread (cons (list (caar thread)
4994                                        (cadar thread))
4995                                  (cdr thread)))
4996               (setq gnus-tmp-level -1
4997                     gnus-tmp-false-parent t))
4998              ((eq gnus-summary-make-false-root 'empty)
4999               ;; We print adopted articles with empty subject fields.
5000               (setq gnus-tmp-gathered
5001                     (nconc (mapcar
5002                             (lambda (h) (mail-header-number (car h)))
5003                             (cddar thread))
5004                            gnus-tmp-gathered))
5005               (setq gnus-tmp-level -1))
5006              ((eq gnus-summary-make-false-root 'dummy)
5007               ;; We remember that we probably want to output a dummy
5008               ;; root.
5009               (setq gnus-tmp-dummy-line gnus-tmp-header)
5010               (setq gnus-tmp-prev-subject gnus-tmp-header))
5011              (t
5012               ;; We do not make a root for the gathered
5013               ;; sub-threads at all.
5014               (setq gnus-tmp-level -1)))
5015
5016           (setq number (mail-header-number gnus-tmp-header)
5017                 subject (mail-header-subject gnus-tmp-header)
5018                 simp-subject (gnus-simplify-subject-fully subject))
5019
5020           (cond
5021            ;; If the thread has changed subject, we might want to make
5022            ;; this subthread into a root.
5023            ((and (null gnus-thread-ignore-subject)
5024                  (not (zerop gnus-tmp-level))
5025                  gnus-tmp-prev-subject
5026                  (not (string= gnus-tmp-prev-subject simp-subject)))
5027             (setq new-roots (nconc new-roots (list (car thread)))
5028                   thread-end t
5029                   gnus-tmp-header nil))
5030            ;; If the article lies outside the current limit,
5031            ;; then we do not display it.
5032            ((not (memq number gnus-newsgroup-limit))
5033             (setq gnus-tmp-gathered
5034                   (nconc (mapcar
5035                           (lambda (h) (mail-header-number (car h)))
5036                           (cdar thread))
5037                          gnus-tmp-gathered))
5038             (setq gnus-tmp-new-adopts (if (cdar thread)
5039                                           (append gnus-tmp-new-adopts
5040                                                   (cdar thread))
5041                                         gnus-tmp-new-adopts)
5042                   thread-end t
5043                   gnus-tmp-header nil)
5044             (when (zerop gnus-tmp-level)
5045               (setq gnus-tmp-root-expunged t)))
5046            ;; Perhaps this article is to be marked as read?
5047            ((and gnus-summary-mark-below
5048                  (< (or (cdr (assq number gnus-newsgroup-scored))
5049                         default-score)
5050                     gnus-summary-mark-below)
5051                  ;; Don't touch sparse articles.
5052                  (not (gnus-summary-article-sparse-p number))
5053                  (not (gnus-summary-article-ancient-p number)))
5054             (setq gnus-newsgroup-unreads
5055                   (delq number gnus-newsgroup-unreads))
5056             (if gnus-newsgroup-auto-expire
5057                 (setq gnus-newsgroup-expirable
5058                       (gnus-add-to-sorted-list
5059                        gnus-newsgroup-expirable number))
5060               (push (cons number gnus-low-score-mark)
5061                     gnus-newsgroup-reads))))
5062
5063           (when gnus-tmp-header
5064             ;; We may have an old dummy line to output before this
5065             ;; article.
5066             (when (and gnus-tmp-dummy-line
5067                        (gnus-subject-equal
5068                         gnus-tmp-dummy-line
5069                         (mail-header-subject gnus-tmp-header)))
5070               (gnus-summary-insert-dummy-line
5071                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5072               (setq gnus-tmp-dummy-line nil))
5073
5074             ;; Compute the mark.
5075             (setq gnus-tmp-unread (gnus-article-mark number))
5076
5077             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5078                                   gnus-tmp-header gnus-tmp-level)
5079                   gnus-newsgroup-data)
5080
5081             ;; Actually insert the line.
5082             (setq
5083              gnus-tmp-subject-or-nil
5084              (cond
5085               ((and gnus-thread-ignore-subject
5086                     gnus-tmp-prev-subject
5087                     (not (string= gnus-tmp-prev-subject simp-subject)))
5088                subject)
5089               ((zerop gnus-tmp-level)
5090                (if (and (eq gnus-summary-make-false-root 'empty)
5091                         (memq number gnus-tmp-gathered)
5092                         gnus-tmp-prev-subject
5093                         (string= gnus-tmp-prev-subject simp-subject))
5094                    gnus-summary-same-subject
5095                  subject))
5096               (t gnus-summary-same-subject)))
5097             (if (and (eq gnus-summary-make-false-root 'adopt)
5098                      (= gnus-tmp-level 1)
5099                      (memq number gnus-tmp-gathered))
5100                 (setq gnus-tmp-opening-bracket ?\<
5101                       gnus-tmp-closing-bracket ?\>)
5102               (setq gnus-tmp-opening-bracket ?\[
5103                     gnus-tmp-closing-bracket ?\]))
5104             (setq
5105              gnus-tmp-indentation
5106              (aref gnus-thread-indent-array gnus-tmp-level)
5107              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5108              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5109                                 gnus-summary-default-score 0)
5110              gnus-tmp-score-char
5111              (if (or (null gnus-summary-default-score)
5112                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5113                          gnus-summary-zcore-fuzz))
5114                  ?                      ;Whitespace
5115                (if (< gnus-tmp-score gnus-summary-default-score)
5116                    gnus-score-below-mark gnus-score-over-mark))
5117              gnus-tmp-replied
5118              (cond ((memq number gnus-newsgroup-processable)
5119                     gnus-process-mark)
5120                    ((memq number gnus-newsgroup-cached)
5121                     gnus-cached-mark)
5122                    ((memq number gnus-newsgroup-replied)
5123                     gnus-replied-mark)
5124                    ((memq number gnus-newsgroup-forwarded)
5125                     gnus-forwarded-mark)
5126                    ((memq number gnus-newsgroup-saved)
5127                     gnus-saved-mark)
5128                    ((memq number gnus-newsgroup-recent)
5129                     gnus-recent-mark)
5130                    ((memq number gnus-newsgroup-unseen)
5131                     gnus-unseen-mark)
5132                    (t gnus-no-mark))
5133              gnus-tmp-downloaded
5134              (cond ((memq number gnus-newsgroup-undownloaded)
5135                     gnus-undownloaded-mark)
5136                    (gnus-newsgroup-agentized
5137                     gnus-downloaded-mark)
5138                    (t
5139                     gnus-no-mark))
5140              gnus-tmp-from (mail-header-from gnus-tmp-header)
5141              gnus-tmp-name
5142              (cond
5143               ((string-match "<[^>]+> *$" gnus-tmp-from)
5144                (setq beg-match (match-beginning 0))
5145                (or (and (string-match "^\".+\"" gnus-tmp-from)
5146                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5147                    (substring gnus-tmp-from 0 beg-match)))
5148               ((string-match "(.+)" gnus-tmp-from)
5149                (substring gnus-tmp-from
5150                           (1+ (match-beginning 0)) (1- (match-end 0))))
5151               (t gnus-tmp-from))
5152
5153              ;; Do the %B string
5154              gnus-tmp-thread-tree-header-string
5155              (cond
5156               ((not gnus-show-threads) "")
5157               ((zerop gnus-tmp-level)
5158                (cond ((cdar thread)
5159                       (or gnus-sum-thread-tree-root subject))
5160                      (gnus-tmp-new-adopts
5161                       (or gnus-sum-thread-tree-false-root subject))
5162                      (t
5163                       (or gnus-sum-thread-tree-single-indent subject))))
5164               (t
5165                (concat (apply 'concat
5166                               (mapcar (lambda (item)
5167                                         (if (= item 1)
5168                                             gnus-sum-thread-tree-vertical
5169                                           gnus-sum-thread-tree-indent))
5170                                       (cdr (reverse tree-stack))))
5171                        (if (nth 1 thread)
5172                            gnus-sum-thread-tree-leaf-with-other
5173                          gnus-sum-thread-tree-single-leaf)))))
5174             (when (string= gnus-tmp-name "")
5175               (setq gnus-tmp-name gnus-tmp-from))
5176             (unless (numberp gnus-tmp-lines)
5177               (setq gnus-tmp-lines -1))
5178             (if (= gnus-tmp-lines -1)
5179                 (setq gnus-tmp-lines "?")
5180               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5181               (gnus-put-text-property
5182              (point)
5183              (progn (eval gnus-summary-line-format-spec) (point))
5184                'gnus-number number)
5185             (when gnus-visual-p
5186               (forward-line -1)
5187               (gnus-run-hooks 'gnus-summary-update-hook)
5188               (forward-line 1))
5189
5190             (setq gnus-tmp-prev-subject simp-subject)))
5191
5192         (when (nth 1 thread)
5193           (push (list (max 0 gnus-tmp-level)
5194                       (copy-sequence tree-stack)
5195                       (nthcdr 1 thread))
5196                 stack))
5197         (push (if (nth 1 thread) 1 0) tree-stack)
5198         (incf gnus-tmp-level)
5199         (setq threads (if thread-end nil (cdar thread)))
5200         (if gnus-summary-display-while-building
5201             (if building-count
5202                 (progn
5203                   ;; use a set frequency
5204                   (setq building-line-count (1- building-line-count))
5205                   (when (= building-line-count 0)
5206                     (sit-for 0)
5207                     (setq building-line-count
5208                           gnus-summary-display-while-building)))
5209               ;; always
5210               (sit-for 0)))
5211         (unless threads
5212           (setq gnus-tmp-level 0)))))
5213   (gnus-message 7 "Generating summary...done"))
5214
5215 (defun gnus-summary-prepare-unthreaded (headers)
5216   "Generate an unthreaded summary buffer based on HEADERS."
5217   (let (header number mark)
5218
5219     (beginning-of-line)
5220
5221     (while headers
5222       ;; We may have to root out some bad articles...
5223       (when (memq (setq number (mail-header-number
5224                                 (setq header (pop headers))))
5225                   gnus-newsgroup-limit)
5226         ;; Mark article as read when it has a low score.
5227         (when (and gnus-summary-mark-below
5228                    (< (or (cdr (assq number gnus-newsgroup-scored))
5229                           gnus-summary-default-score 0)
5230                       gnus-summary-mark-below)
5231                    (not (gnus-summary-article-ancient-p number)))
5232           (setq gnus-newsgroup-unreads
5233                 (delq number gnus-newsgroup-unreads))
5234           (if gnus-newsgroup-auto-expire
5235               (push number gnus-newsgroup-expirable)
5236             (push (cons number gnus-low-score-mark)
5237                   gnus-newsgroup-reads)))
5238
5239         (setq mark (gnus-article-mark number))
5240         (push (gnus-data-make number mark (1+ (point)) header 0)
5241               gnus-newsgroup-data)
5242         (gnus-summary-insert-line
5243          header 0 number
5244          (memq number gnus-newsgroup-undownloaded)
5245          mark (memq number gnus-newsgroup-replied)
5246          (memq number gnus-newsgroup-expirable)
5247          (mail-header-subject header) nil
5248          (cdr (assq number gnus-newsgroup-scored))
5249          (memq number gnus-newsgroup-processable))))))
5250
5251 (defun gnus-summary-remove-list-identifiers ()
5252   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5253   (let ((regexp (if (consp gnus-list-identifiers)
5254                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5255                   gnus-list-identifiers))
5256         changed subject)
5257     (when regexp
5258       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5259       (dolist (header gnus-newsgroup-headers)
5260         (setq subject (mail-header-subject header)
5261               changed nil)
5262         (while (string-match regexp subject)
5263           (setq subject
5264                 (concat (substring subject 0 (match-beginning 1))
5265                         (substring subject (match-end 0)))
5266                 changed t))
5267         (when changed
5268           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5269             (setq subject
5270                   (concat (substring subject 0 (match-beginning 1))
5271                           (substring subject (match-end 1)))))
5272           (mail-header-set-subject header subject))))))
5273
5274 (defun gnus-fetch-headers (articles)
5275   "Fetch headers of ARTICLES."
5276   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5277     (gnus-message 5 "Fetching headers for %s..." name)
5278     (prog1
5279         (if (eq 'nov
5280                 (setq gnus-headers-retrieved-by
5281                       (gnus-retrieve-headers
5282                        articles gnus-newsgroup-name
5283                        ;; We might want to fetch old headers, but
5284                        ;; not if there is only 1 article.
5285                        (and (or (and
5286                                  (not (eq gnus-fetch-old-headers 'some))
5287                                  (not (numberp gnus-fetch-old-headers)))
5288                                 (> (length articles) 1))
5289                             gnus-fetch-old-headers))))
5290             (gnus-get-newsgroup-headers-xover
5291              articles nil nil gnus-newsgroup-name t)
5292           (gnus-get-newsgroup-headers))
5293       (gnus-message 5 "Fetching headers for %s...done" name))))
5294
5295 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5296   "Select newsgroup GROUP.
5297 If READ-ALL is non-nil, all articles in the group are selected.
5298 If SELECT-ARTICLES, only select those articles from GROUP."
5299   (let* ((entry (gnus-group-entry group))
5300          ;;!!! Dirty hack; should be removed.
5301          (gnus-summary-ignore-duplicates
5302           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5303               t
5304             gnus-summary-ignore-duplicates))
5305          (info (nth 2 entry))
5306          articles fetched-articles cached)
5307
5308     (unless (gnus-check-server
5309              (set (make-local-variable 'gnus-current-select-method)
5310                   (gnus-find-method-for-group group)))
5311       (error "Couldn't open server"))
5312
5313     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5314         (gnus-activate-group group)     ; Or we can activate it...
5315         (progn                          ; Or we bug out.
5316           (when (equal major-mode 'gnus-summary-mode)
5317             (gnus-kill-buffer (current-buffer)))
5318           (error "Couldn't activate group %s: %s"
5319                  group (gnus-status-message group))))
5320
5321     (unless (gnus-request-group group t)
5322       (when (equal major-mode 'gnus-summary-mode)
5323         (gnus-kill-buffer (current-buffer)))
5324       (error "Couldn't request group %s: %s"
5325              group (gnus-status-message group)))
5326
5327     (when gnus-agent
5328       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5329
5330       (setq gnus-summary-use-undownloaded-faces
5331             (gnus-agent-find-parameter
5332              group
5333              'agent-enable-undownloaded-faces)))
5334
5335     (setq gnus-newsgroup-name group
5336           gnus-newsgroup-unselected nil
5337           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5338
5339     (let ((display (gnus-group-find-parameter group 'display)))
5340       (setq gnus-newsgroup-display
5341             (cond
5342              ((not (zerop (or (car-safe read-all) 0)))
5343               ;; The user entered the group with C-u SPC/RET, let's show
5344               ;; all articles.
5345               'gnus-not-ignore)
5346              ((eq display 'all)
5347               'gnus-not-ignore)
5348              ((arrayp display)
5349               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5350              ((numberp display)
5351               ;; The following is probably the "correct" solution, but
5352               ;; it makes Gnus fetch all headers and then limit the
5353               ;; articles (which is slow), so instead we hack the
5354               ;; select-articles parameter instead. -- Simon Josefsson
5355               ;; <jas@kth.se>
5356               ;;
5357               ;; (gnus-byte-compile
5358               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5359               ;;                         display)))))
5360               (setq select-articles
5361                     (gnus-uncompress-range
5362                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5363                              (if (> tmp 0)
5364                                  tmp
5365                                1))
5366                            (cdr (gnus-active group)))))
5367               nil)
5368              (t
5369               nil))))
5370
5371     (gnus-summary-setup-default-charset)
5372
5373     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5374     (when (gnus-virtual-group-p group)
5375       (setq cached gnus-newsgroup-cached))
5376
5377     (setq gnus-newsgroup-unreads
5378           (gnus-sorted-ndifference
5379            (gnus-sorted-ndifference gnus-newsgroup-unreads
5380                                     gnus-newsgroup-marked)
5381            gnus-newsgroup-dormant))
5382
5383     (setq gnus-newsgroup-processable nil)
5384
5385     (gnus-update-read-articles group gnus-newsgroup-unreads)
5386
5387     ;; Adjust and set lists of article marks.
5388     (when info
5389       (gnus-adjust-marked-articles info))
5390     (if (setq articles select-articles)
5391         (setq gnus-newsgroup-unselected
5392               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5393       (setq articles (gnus-articles-to-read group read-all)))
5394
5395     (cond
5396      ((null articles)
5397       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5398       'quit)
5399      ((eq articles 0) nil)
5400      (t
5401       ;; Init the dependencies hash table.
5402       (setq gnus-newsgroup-dependencies
5403             (gnus-make-hashtable (length articles)))
5404       (gnus-set-global-variables)
5405       ;; Retrieve the headers and read them in.
5406
5407       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5408
5409       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5410       (when cached
5411         (setq gnus-newsgroup-cached cached))
5412
5413       ;; Suppress duplicates?
5414       (when gnus-suppress-duplicates
5415         (gnus-dup-suppress-articles))
5416
5417       ;; Set the initial limit.
5418       (setq gnus-newsgroup-limit (copy-sequence articles))
5419       ;; Remove canceled articles from the list of unread articles.
5420       (setq fetched-articles
5421             (mapcar (lambda (headers) (mail-header-number headers))
5422                     gnus-newsgroup-headers))
5423       (setq gnus-newsgroup-articles fetched-articles)
5424       (setq gnus-newsgroup-unreads
5425             (gnus-sorted-nintersection
5426              gnus-newsgroup-unreads fetched-articles))
5427       (gnus-compute-unseen-list)
5428
5429       ;; Removed marked articles that do not exist.
5430       (gnus-update-missing-marks
5431        (gnus-sorted-difference articles fetched-articles))
5432       ;; We might want to build some more threads first.
5433       (when (and gnus-fetch-old-headers
5434                  (eq gnus-headers-retrieved-by 'nov))
5435         (if (eq gnus-fetch-old-headers 'invisible)
5436             (gnus-build-all-threads)
5437           (gnus-build-old-threads)))
5438       ;; Let the Gnus agent mark articles as read.
5439       (when gnus-agent
5440         (gnus-agent-get-undownloaded-list))
5441       ;; Remove list identifiers from subject
5442       (when gnus-list-identifiers
5443         (gnus-summary-remove-list-identifiers))
5444       ;; Check whether auto-expire is to be done in this group.
5445       (setq gnus-newsgroup-auto-expire
5446             (gnus-group-auto-expirable-p group))
5447       ;; Set up the article buffer now, if necessary.
5448       (unless gnus-single-article-buffer
5449         (gnus-article-setup-buffer))
5450       ;; First and last article in this newsgroup.
5451       (when gnus-newsgroup-headers
5452         (setq gnus-newsgroup-begin
5453               (mail-header-number (car gnus-newsgroup-headers))
5454               gnus-newsgroup-end
5455               (mail-header-number
5456                (gnus-last-element gnus-newsgroup-headers))))
5457       ;; GROUP is successfully selected.
5458       (or gnus-newsgroup-headers t)))))
5459
5460 (defun gnus-compute-unseen-list ()
5461   ;; The `seen' marks are treated specially.
5462   (if (not gnus-newsgroup-seen)
5463       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5464     (setq gnus-newsgroup-unseen
5465           (gnus-inverse-list-range-intersection
5466            gnus-newsgroup-articles gnus-newsgroup-seen))))
5467
5468 (defun gnus-summary-display-make-predicate (display)
5469   (require 'gnus-agent)
5470   (when (= (length display) 1)
5471     (setq display (car display)))
5472   (unless gnus-summary-display-cache
5473     (dolist (elem (append '((unread . unread)
5474                             (read . read)
5475                             (unseen . unseen))
5476                           gnus-article-mark-lists))
5477       (push (cons (cdr elem)
5478                   (gnus-byte-compile
5479                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5480             gnus-summary-display-cache)))
5481   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5482         (gnus-category-predicate-cache gnus-summary-display-cache))
5483     (gnus-get-predicate display)))
5484
5485 ;; Uses the dynamically bound `number' variable.
5486 (eval-when-compile
5487   (defvar number))
5488 (defun gnus-article-marked-p (type &optional article)
5489   (let ((article (or article number)))
5490     (cond
5491      ((eq type 'tick)
5492       (memq article gnus-newsgroup-marked))
5493      ((eq type 'spam)
5494       (memq article gnus-newsgroup-spam-marked))
5495      ((eq type 'unsend)
5496       (memq article gnus-newsgroup-unsendable))
5497      ((eq type 'undownload)
5498       (memq article gnus-newsgroup-undownloaded))
5499      ((eq type 'download)
5500       (memq article gnus-newsgroup-downloadable))
5501      ((eq type 'unread)
5502       (memq article gnus-newsgroup-unreads))
5503      ((eq type 'read)
5504       (memq article gnus-newsgroup-reads))
5505      ((eq type 'dormant)
5506       (memq article gnus-newsgroup-dormant) )
5507      ((eq type 'expire)
5508       (memq article gnus-newsgroup-expirable))
5509      ((eq type 'reply)
5510       (memq article gnus-newsgroup-replied))
5511      ((eq type 'killed)
5512       (memq article gnus-newsgroup-killed))
5513      ((eq type 'bookmark)
5514       (assq article gnus-newsgroup-bookmarks))
5515      ((eq type 'score)
5516       (assq article gnus-newsgroup-scored))
5517      ((eq type 'save)
5518       (memq article gnus-newsgroup-saved))
5519      ((eq type 'cache)
5520       (memq article gnus-newsgroup-cached))
5521      ((eq type 'forward)
5522       (memq article gnus-newsgroup-forwarded))
5523      ((eq type 'seen)
5524       (not (memq article gnus-newsgroup-unseen)))
5525      ((eq type 'recent)
5526       (memq article gnus-newsgroup-recent))
5527      (t t))))
5528
5529 (defun gnus-articles-to-read (group &optional read-all)
5530   "Find out what articles the user wants to read."
5531   (let* ((articles
5532           ;; Select all articles if `read-all' is non-nil, or if there
5533           ;; are no unread articles.
5534           (if (or read-all
5535                   (and (zerop (length gnus-newsgroup-marked))
5536                        (zerop (length gnus-newsgroup-unreads)))
5537                   ;; Fetch all if the predicate is non-nil.
5538                   gnus-newsgroup-display)
5539               ;; We want to select the headers for all the articles in
5540               ;; the group, so we select either all the active
5541               ;; articles in the group, or (if that's nil), the
5542               ;; articles in the cache.
5543               (or
5544                (gnus-uncompress-range (gnus-active group))
5545                (gnus-cache-articles-in-group group))
5546             ;; Select only the "normal" subset of articles.
5547             (gnus-sorted-nunion
5548              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5549              gnus-newsgroup-unreads)))
5550          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5551          (scored (length scored-list))
5552          (number (length articles))
5553          (marked (+ (length gnus-newsgroup-marked)
5554                     (length gnus-newsgroup-dormant)))
5555          (select
5556           (cond
5557            ((numberp read-all)
5558             read-all)
5559            ((numberp gnus-newsgroup-display)
5560             gnus-newsgroup-display)
5561            (t
5562             (condition-case ()
5563                 (cond
5564                  ((and (or (<= scored marked) (= scored number))
5565                        (numberp gnus-large-newsgroup)
5566                        (> number gnus-large-newsgroup))
5567                   (let* ((cursor-in-echo-area nil)
5568                          (initial (gnus-parameter-large-newsgroup-initial
5569                                    gnus-newsgroup-name))
5570                          (input
5571                           (read-string
5572                            (format
5573                             "How many articles from %s (%s %d): "
5574                             (gnus-limit-string
5575                              (gnus-group-decoded-name gnus-newsgroup-name)
5576                              35)
5577                             (if initial "max" "default")
5578                             number)
5579                            (if initial
5580                                (cons (number-to-string initial)
5581                                      0)))))
5582                     (if (string-match "^[ \t]*$" input) number input)))
5583                  ((and (> scored marked) (< scored number)
5584                        (> (- scored number) 20))
5585                   (let ((input
5586                          (read-string
5587                           (format "%s %s (%d scored, %d total): "
5588                                   "How many articles from"
5589                                   (gnus-group-decoded-name group)
5590                                   scored number))))
5591                     (if (string-match "^[ \t]*$" input)
5592                         number input)))
5593                  (t number))
5594               (quit
5595                (message "Quit getting the articles to read")
5596                nil))))))
5597     (setq select (if (stringp select) (string-to-number select) select))
5598     (if (or (null select) (zerop select))
5599         select
5600       (if (and (not (zerop scored)) (<= (abs select) scored))
5601           (progn
5602             (setq articles (sort scored-list '<))
5603             (setq number (length articles)))
5604         (setq articles (copy-sequence articles)))
5605
5606       (when (< (abs select) number)
5607         (if (< select 0)
5608             ;; Select the N oldest articles.
5609             (setcdr (nthcdr (1- (abs select)) articles) nil)
5610           ;; Select the N most recent articles.
5611           (setq articles (nthcdr (- number select) articles))))
5612       (setq gnus-newsgroup-unselected
5613             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5614       (when gnus-alter-articles-to-read-function
5615         (setq articles
5616               (sort
5617                (funcall gnus-alter-articles-to-read-function
5618                         gnus-newsgroup-name articles)
5619                '<)))
5620       articles)))
5621
5622 (defun gnus-killed-articles (killed articles)
5623   (let (out)
5624     (while articles
5625       (when (inline (gnus-member-of-range (car articles) killed))
5626         (push (car articles) out))
5627       (setq articles (cdr articles)))
5628     out))
5629
5630 (defun gnus-uncompress-marks (marks)
5631   "Uncompress the mark ranges in MARKS."
5632   (let ((uncompressed '(score bookmark))
5633         out)
5634     (while marks
5635       (if (memq (caar marks) uncompressed)
5636           (push (car marks) out)
5637         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5638       (setq marks (cdr marks)))
5639     out))
5640
5641 (defun gnus-article-mark-to-type (mark)
5642   "Return the type of MARK."
5643   (or (cadr (assq mark gnus-article-special-mark-lists))
5644       'list))
5645
5646 (defun gnus-article-unpropagatable-p (mark)
5647   "Return whether MARK should be propagated to back end."
5648   (memq mark gnus-article-unpropagated-mark-lists))
5649
5650 (defun gnus-adjust-marked-articles (info)
5651   "Set all article lists and remove all marks that are no longer valid."
5652   (let* ((marked-lists (gnus-info-marks info))
5653          (active (gnus-active (gnus-info-group info)))
5654          (min (car active))
5655          (max (cdr active))
5656          (types gnus-article-mark-lists)
5657          marks var articles article mark mark-type
5658          bgn end)
5659
5660     (dolist (marks marked-lists)
5661       (setq mark (car marks)
5662             mark-type (gnus-article-mark-to-type mark)
5663             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5664
5665       ;; We set the variable according to the type of the marks list,
5666       ;; and then adjust the marks to a subset of the active articles.
5667       (cond
5668        ;; Adjust "simple" lists - compressed yet unsorted
5669        ((eq mark-type 'list)
5670         ;; Simultaneously uncompress and clip to active range
5671         ;; See gnus-uncompress-range for a description of possible marks
5672         (let (l lh)
5673           (if (not (cadr marks))
5674               (set var nil)
5675             (setq articles (if (numberp (cddr marks))
5676                                (list (cdr marks))
5677                              (cdr marks))
5678                   lh (cons nil nil)
5679                   l lh)
5680
5681             (while (setq article (pop articles))
5682               (cond ((consp article)
5683                      (setq bgn (max (car article) min)
5684                            end (min (cdr article) max))
5685                      (while (<= bgn end)
5686                        (setq l (setcdr l (cons bgn nil))
5687                              bgn (1+ bgn))))
5688                     ((and (<= min article)
5689                           (>= max article))
5690                      (setq l (setcdr l (cons article nil))))))
5691             (set var (cdr lh)))))
5692        ;; Adjust assocs.
5693        ((eq mark-type 'tuple)
5694         (set var (setq articles (cdr marks)))
5695         (when (not (listp (cdr (symbol-value var))))
5696           (set var (list (symbol-value var))))
5697         (when (not (listp (cdr articles)))
5698           (setq articles (list articles)))
5699         (while articles
5700           (when (or (not (consp (setq article (pop articles))))
5701                     (< (car article) min)
5702                     (> (car article) max))
5703             (set var (delq article (symbol-value var))))))
5704        ;; Adjust ranges (sloppily).
5705        ((eq mark-type 'range)
5706         (cond
5707          ((eq mark 'seen)
5708           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5709           ;; It should be (seen (NUM1 . NUM2)).
5710           (when (numberp (cddr marks))
5711             (setcdr marks (list (cdr marks))))
5712           (setq articles (cdr marks))
5713           (while (and articles
5714                       (or (and (consp (car articles))
5715                                (> min (cdar articles)))
5716                           (and (numberp (car articles))
5717                                (> min (car articles)))))
5718             (pop articles))
5719           (set var articles))))))))
5720
5721 (defun gnus-update-missing-marks (missing)
5722   "Go through the list of MISSING articles and remove them from the mark lists."
5723   (when missing
5724     (let (var m)
5725       ;; Go through all types.
5726       (dolist (elem gnus-article-mark-lists)
5727         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5728           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5729           (when (symbol-value var)
5730             ;; This list has articles.  So we delete all missing
5731             ;; articles from it.
5732             (setq m missing)
5733             (while m
5734               (set var (delq (pop m) (symbol-value var))))))))))
5735
5736 (defun gnus-update-marks ()
5737   "Enter the various lists of marked articles into the newsgroup info list."
5738   (let ((types gnus-article-mark-lists)
5739         (info (gnus-get-info gnus-newsgroup-name))
5740         type list newmarked symbol delta-marks)
5741     (when info
5742       ;; Add all marks lists to the list of marks lists.
5743       (while (setq type (pop types))
5744         (setq list (symbol-value
5745                     (setq symbol
5746                           (intern (format "gnus-newsgroup-%s" (car type))))))
5747
5748         (when list
5749           ;; Get rid of the entries of the articles that have the
5750           ;; default score.
5751           (when (and (eq (cdr type) 'score)
5752                      gnus-save-score
5753                      list)
5754             (let* ((arts list)
5755                    (prev (cons nil list))
5756                    (all prev))
5757               (while arts
5758                 (if (or (not (consp (car arts)))
5759                         (= (cdar arts) gnus-summary-default-score))
5760                     (setcdr prev (cdr arts))
5761                   (setq prev arts))
5762                 (setq arts (cdr arts)))
5763               (setq list (cdr all)))))
5764
5765         (when (eq (cdr type) 'seen)
5766           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5767
5768         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5769           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5770
5771         (when (and (gnus-check-backend-function
5772                     'request-set-mark gnus-newsgroup-name)
5773                    (not (gnus-article-unpropagatable-p (cdr type))))
5774           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5775                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5776                  (add (gnus-remove-from-range
5777                        (gnus-copy-sequence list) old)))
5778             (when add
5779               (push (list add 'add (list (cdr type))) delta-marks))
5780             (when del
5781               (push (list del 'del (list (cdr type))) delta-marks))))
5782
5783         (when list
5784           (push (cons (cdr type) list) newmarked)))
5785
5786       (when delta-marks
5787         (unless (gnus-check-group gnus-newsgroup-name)
5788           (error "Can't open server for %s" gnus-newsgroup-name))
5789         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5790
5791       ;; Enter these new marks into the info of the group.
5792       (if (nthcdr 3 info)
5793           (setcar (nthcdr 3 info) newmarked)
5794         ;; Add the marks lists to the end of the info.
5795         (when newmarked
5796           (setcdr (nthcdr 2 info) (list newmarked))))
5797
5798       ;; Cut off the end of the info if there's nothing else there.
5799       (let ((i 5))
5800         (while (and (> i 2)
5801                     (not (nth i info)))
5802           (when (nthcdr (decf i) info)
5803             (setcdr (nthcdr i info) nil)))))))
5804
5805 (defun gnus-set-mode-line (where)
5806   "Set the mode line of the article or summary buffers.
5807 If WHERE is `summary', the summary mode line format will be used."
5808   ;; Is this mode line one we keep updated?
5809   (when (and (memq where gnus-updated-mode-lines)
5810              (symbol-value
5811               (intern (format "gnus-%s-mode-line-format-spec" where))))
5812     (let (mode-string)
5813       (save-excursion
5814         ;; We evaluate this in the summary buffer since these
5815         ;; variables are buffer-local to that buffer.
5816         (set-buffer gnus-summary-buffer)
5817        ;; We bind all these variables that are used in the `eval' form
5818         ;; below.
5819         (let* ((mformat (symbol-value
5820                          (intern
5821                           (format "gnus-%s-mode-line-format-spec" where))))
5822                (gnus-tmp-group-name (gnus-group-decoded-name
5823                                      gnus-newsgroup-name))
5824                (gnus-tmp-article-number (or gnus-current-article 0))
5825                (gnus-tmp-unread gnus-newsgroup-unreads)
5826                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5827                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5828                (gnus-tmp-unread-and-unselected
5829                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5830                             (zerop gnus-tmp-unselected))
5831                        "")
5832                       ((zerop gnus-tmp-unselected)
5833                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5834                       (t (format "{%d(+%d) more}"
5835                                  gnus-tmp-unread-and-unticked
5836                                  gnus-tmp-unselected))))
5837                (gnus-tmp-subject
5838                 (if (and gnus-current-headers
5839                          (vectorp gnus-current-headers))
5840                     (gnus-mode-string-quote
5841                      (mail-header-subject gnus-current-headers))
5842                   ""))
5843                bufname-length max-len
5844                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5845           (setq mode-string (eval mformat))
5846           (setq bufname-length (if (string-match "%b" mode-string)
5847                                    (- (length
5848                                        (buffer-name
5849                                         (if (eq where 'summary)
5850                                             nil
5851                                           (get-buffer gnus-article-buffer))))
5852                                       2)
5853                                  0))
5854           (setq max-len (max 4 (if gnus-mode-non-string-length
5855                                    (- (window-width)
5856                                       gnus-mode-non-string-length
5857                                       bufname-length)
5858                                  (length mode-string))))
5859           ;; We might have to chop a bit of the string off...
5860           (when (> (length mode-string) max-len)
5861             (setq mode-string
5862                   (concat (truncate-string-to-width mode-string (- max-len 3))
5863                           "...")))
5864           ;; Pad the mode string a bit.
5865           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5866       ;; Update the mode line.
5867       (setq mode-line-buffer-identification
5868             (gnus-mode-line-buffer-identification (list mode-string)))
5869       (set-buffer-modified-p t))))
5870
5871 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5872   "Go through the HEADERS list and add all Xrefs to a hash table.
5873 The resulting hash table is returned, or nil if no Xrefs were found."
5874   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5875          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5876          (xref-hashtb (gnus-make-hashtable))
5877          start group entry number xrefs header)
5878     (while headers
5879       (setq header (pop headers))
5880       (when (and (setq xrefs (mail-header-xref header))
5881                  (not (memq (setq number (mail-header-number header))
5882                             unreads)))
5883         (setq start 0)
5884         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5885           (setq start (match-end 0))
5886           (setq group (if prefix
5887                           (concat prefix (substring xrefs (match-beginning 1)
5888                                                     (match-end 1)))
5889                         (substring xrefs (match-beginning 1) (match-end 1))))
5890           (setq number
5891                 (string-to-number (substring xrefs (match-beginning 2)
5892                                           (match-end 2))))
5893           (if (setq entry (gnus-gethash group xref-hashtb))
5894               (setcdr entry (cons number (cdr entry)))
5895             (gnus-sethash group (cons number nil) xref-hashtb)))))
5896     (and start xref-hashtb)))
5897
5898 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5899   "Look through all the headers and mark the Xrefs as read."
5900   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5901         name info xref-hashtb idlist method nth4)
5902     (save-excursion
5903       (set-buffer gnus-group-buffer)
5904       (when (setq xref-hashtb
5905                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5906         (mapatoms
5907          (lambda (group)
5908            (unless (string= from-newsgroup (setq name (symbol-name group)))
5909              (setq idlist (symbol-value group))
5910              ;; Dead groups are not updated.
5911              (and (prog1
5912                       (setq info (gnus-get-info name))
5913                     (when (stringp (setq nth4 (gnus-info-method info)))
5914                       (setq nth4 (gnus-server-to-method nth4))))
5915                   ;; Only do the xrefs if the group has the same
5916                   ;; select method as the group we have just read.
5917                   (or (gnus-methods-equal-p
5918                        nth4 (gnus-find-method-for-group from-newsgroup))
5919                       virtual
5920                       (equal nth4 (setq method (gnus-find-method-for-group
5921                                                 from-newsgroup)))
5922                       (and (equal (car nth4) (car method))
5923                            (equal (nth 1 nth4) (nth 1 method))))
5924                   gnus-use-cross-reference
5925                   (or (not (eq gnus-use-cross-reference t))
5926                       virtual
5927                       ;; Only do cross-references on subscribed
5928                       ;; groups, if that is what is wanted.
5929                       (<= (gnus-info-level info) gnus-level-subscribed))
5930                   (gnus-group-make-articles-read name idlist))))
5931          xref-hashtb)))))
5932
5933 (defun gnus-compute-read-articles (group articles)
5934   (let* ((entry (gnus-group-entry group))
5935          (info (nth 2 entry))
5936          (active (gnus-active group))
5937          ninfo)
5938     (when entry
5939       ;; First peel off all invalid article numbers.
5940       (when active
5941         (let ((ids articles)
5942               id first)
5943           (while (setq id (pop ids))
5944             (when (and first (> id (cdr active)))
5945               ;; We'll end up in this situation in one particular
5946               ;; obscure situation.  If you re-scan a group and get
5947               ;; a new article that is cross-posted to a different
5948               ;; group that has not been re-scanned, you might get
5949               ;; crossposted article that has a higher number than
5950               ;; Gnus believes possible.  So we re-activate this
5951               ;; group as well.  This might mean doing the
5952               ;; crossposting thingy will *increase* the number
5953               ;; of articles in some groups.  Tsk, tsk.
5954               (setq active (or (gnus-activate-group group) active)))
5955             (when (or (> id (cdr active))
5956                       (< id (car active)))
5957               (setq articles (delq id articles))))))
5958       ;; If the read list is nil, we init it.
5959       (if (and active
5960                (null (gnus-info-read info))
5961                (> (car active) 1))
5962           (setq ninfo (cons 1 (1- (car active))))
5963         (setq ninfo (gnus-info-read info)))
5964       ;; Then we add the read articles to the range.
5965       (gnus-add-to-range
5966        ninfo (setq articles (sort articles '<))))))
5967
5968 (defun gnus-group-make-articles-read (group articles)
5969   "Update the info of GROUP to say that ARTICLES are read."
5970   (let* ((num 0)
5971          (entry (gnus-group-entry group))
5972          (info (nth 2 entry))
5973          (active (gnus-active group))
5974          range)
5975     (when entry
5976       (setq range (gnus-compute-read-articles group articles))
5977       (with-current-buffer gnus-group-buffer
5978         (gnus-undo-register
5979           `(progn
5980              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5981              (gnus-info-set-read ',info ',(gnus-info-read info))
5982              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5983              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5984              (gnus-group-update-group ,group t))))
5985       ;; Add the read articles to the range.
5986       (gnus-info-set-read info range)
5987       (gnus-request-set-mark group (list (list range 'add '(read))))
5988       ;; Then we have to re-compute how many unread
5989       ;; articles there are in this group.
5990       (when active
5991         (cond
5992          ((not range)
5993           (setq num (- (1+ (cdr active)) (car active))))
5994          ((not (listp (cdr range)))
5995           (setq num (- (cdr active) (- (1+ (cdr range))
5996                                        (car range)))))
5997          (t
5998           (while range
5999             (if (numberp (car range))
6000                 (setq num (1+ num))
6001               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6002             (setq range (cdr range)))
6003           (setq num (- (cdr active) num))))
6004         ;; Update the number of unread articles.
6005         (setcar entry num)
6006         ;; Update the group buffer.
6007         (unless (gnus-ephemeral-group-p group)
6008           (gnus-group-update-group group t))))))
6009
6010 (defvar gnus-newsgroup-none-id 0)
6011
6012 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6013   (let ((cur nntp-server-buffer)
6014         (dependencies
6015          (or dependencies
6016              (with-current-buffer gnus-summary-buffer
6017                gnus-newsgroup-dependencies)))
6018         headers id end ref number
6019         (mail-parse-charset gnus-newsgroup-charset)
6020         (mail-parse-ignored-charsets
6021          (save-excursion (condition-case nil
6022                              (set-buffer gnus-summary-buffer)
6023                            (error))
6024                          gnus-newsgroup-ignored-charsets)))
6025     (save-excursion
6026       (set-buffer nntp-server-buffer)
6027       ;; Translate all TAB characters into SPACE characters.
6028       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6029       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6030       (ietf-drums-unfold-fws)
6031       (gnus-run-hooks 'gnus-parse-headers-hook)
6032       (let ((case-fold-search t)
6033             in-reply-to header p lines chars)
6034         (goto-char (point-min))
6035         ;; Search to the beginning of the next header.  Error messages
6036         ;; do not begin with 2 or 3.
6037         (while (re-search-forward "^[23][0-9]+ " nil t)
6038           (setq id nil
6039                 ref nil)
6040           ;; This implementation of this function, with nine
6041           ;; search-forwards instead of the one re-search-forward and
6042           ;; a case (which basically was the old function) is actually
6043           ;; about twice as fast, even though it looks messier.  You
6044           ;; can't have everything, I guess.  Speed and elegance
6045           ;; doesn't always go hand in hand.
6046           (setq
6047            header
6048            (vector
6049             ;; Number.
6050             (prog1
6051                 (setq number (read cur))
6052               (end-of-line)
6053               (setq p (point))
6054               (narrow-to-region (point)
6055                                 (or (and (search-forward "\n.\n" nil t)
6056                                          (- (point) 2))
6057                                     (point))))
6058             ;; Subject.
6059             (progn
6060               (goto-char p)
6061               (if (search-forward "\nsubject:" nil t)
6062                   (funcall gnus-decode-encoded-word-function
6063                            (nnheader-header-value))
6064                 "(none)"))
6065             ;; From.
6066             (progn
6067               (goto-char p)
6068               (if (search-forward "\nfrom:" nil t)
6069                   (funcall gnus-decode-encoded-word-function
6070                            (nnheader-header-value))
6071                 "(nobody)"))
6072             ;; Date.
6073             (progn
6074               (goto-char p)
6075               (if (search-forward "\ndate:" nil t)
6076                   (nnheader-header-value) ""))
6077             ;; Message-ID.
6078             (progn
6079               (goto-char p)
6080               (setq id (if (re-search-forward
6081                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6082                            ;; We do it this way to make sure the Message-ID
6083                            ;; is (somewhat) syntactically valid.
6084                            (buffer-substring (match-beginning 1)
6085                                              (match-end 1))
6086                          ;; If there was no message-id, we just fake one
6087                          ;; to make subsequent routines simpler.
6088                          (nnheader-generate-fake-message-id number))))
6089             ;; References.
6090             (progn
6091               (goto-char p)
6092               (if (search-forward "\nreferences:" nil t)
6093                   (progn
6094                     (setq end (point))
6095                     (prog1
6096                         (nnheader-header-value)
6097                       (setq ref
6098                             (buffer-substring
6099                              (progn
6100                                (end-of-line)
6101                                (search-backward ">" end t)
6102                                (1+ (point)))
6103                              (progn
6104                                (search-backward "<" end t)
6105                                (point))))))
6106                 ;; Get the references from the in-reply-to header if there
6107                 ;; were no references and the in-reply-to header looks
6108                 ;; promising.
6109                 (if (and (search-forward "\nin-reply-to:" nil t)
6110                          (setq in-reply-to (nnheader-header-value))
6111                          (string-match "<[^>]+>" in-reply-to))
6112                     (let (ref2)
6113                       (setq ref (substring in-reply-to (match-beginning 0)
6114                                            (match-end 0)))
6115                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6116                         (setq ref2 (substring in-reply-to (match-beginning 0)
6117                                               (match-end 0)))
6118                         (when (> (length ref2) (length ref))
6119                           (setq ref ref2)))
6120                       ref)
6121                   (setq ref nil))))
6122             ;; Chars.
6123             (progn
6124               (goto-char p)
6125               (if (search-forward "\nchars: " nil t)
6126                   (if (numberp (setq chars (ignore-errors (read cur))))
6127                       chars -1)
6128                 -1))
6129             ;; Lines.
6130             (progn
6131               (goto-char p)
6132               (if (search-forward "\nlines: " nil t)
6133                   (if (numberp (setq lines (ignore-errors (read cur))))
6134                       lines -1)
6135                 -1))
6136             ;; Xref.
6137             (progn
6138               (goto-char p)
6139               (and (search-forward "\nxref:" nil t)
6140                    (nnheader-header-value)))
6141             ;; Extra.
6142             (when gnus-extra-headers
6143               (let ((extra gnus-extra-headers)
6144                     out)
6145                 (while extra
6146                   (goto-char p)
6147                   (when (search-forward
6148                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6149                     (push (cons (car extra) (nnheader-header-value))
6150                           out))
6151                   (pop extra))
6152                 out))))
6153           (when (equal id ref)
6154             (setq ref nil))
6155
6156           (when gnus-alter-header-function
6157             (funcall gnus-alter-header-function header)
6158             (setq id (mail-header-id header)
6159                   ref (gnus-parent-id (mail-header-references header))))
6160
6161           (when (setq header
6162                       (gnus-dependencies-add-header
6163                        header dependencies force-new))
6164             (push header headers))
6165           (goto-char (point-max))
6166           (widen))
6167         (nreverse headers)))))
6168
6169 ;; Goes through the xover lines and returns a list of vectors
6170 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6171                                                   force-new dependencies
6172                                                   group also-fetch-heads)
6173   "Parse the news overview data in the server buffer.
6174 Return a list of headers that match SEQUENCE (see
6175 `nntp-retrieve-headers')."
6176   ;; Get the Xref when the users reads the articles since most/some
6177   ;; NNTP servers do not include Xrefs when using XOVER.
6178   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6179   (let ((mail-parse-charset gnus-newsgroup-charset)
6180         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6181         (cur nntp-server-buffer)
6182         (dependencies (or dependencies gnus-newsgroup-dependencies))
6183         (allp (cond
6184                ((eq gnus-read-all-available-headers t)
6185                 t)
6186                ((and (stringp gnus-read-all-available-headers)
6187                      group)
6188                 (string-match gnus-read-all-available-headers group))
6189                (t
6190                 nil)))
6191         number headers header)
6192     (save-excursion
6193       (set-buffer nntp-server-buffer)
6194       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6195       ;; Allow the user to mangle the headers before parsing them.
6196       (gnus-run-hooks 'gnus-parse-headers-hook)
6197       (goto-char (point-min))
6198       (gnus-parse-without-error
6199         (while (and (or sequence allp)
6200                     (not (eobp)))
6201           (setq number (read cur))
6202           (when (not allp)
6203             (while (and sequence
6204                         (< (car sequence) number))
6205               (setq sequence (cdr sequence))))
6206           (when (and (or allp
6207                          (and sequence
6208                               (eq number (car sequence))))
6209                      (progn
6210                        (setq sequence (cdr sequence))
6211                        (setq header (inline
6212                                       (gnus-nov-parse-line
6213                                        number dependencies force-new)))))
6214             (push header headers))
6215           (forward-line 1)))
6216       ;; A common bug in inn is that if you have posted an article and
6217       ;; then retrieves the active file, it will answer correctly --
6218       ;; the new article is included.  However, a NOV entry for the
6219       ;; article may not have been generated yet, so this may fail.
6220       ;; We work around this problem by retrieving the last few
6221       ;; headers using HEAD.
6222       (if (or (not also-fetch-heads)
6223               (not sequence))
6224           ;; We (probably) got all the headers.
6225           (nreverse headers)
6226         (let ((gnus-nov-is-evil t))
6227           (nconc
6228            (nreverse headers)
6229            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6230              (gnus-get-newsgroup-headers))))))))
6231
6232 (defun gnus-article-get-xrefs ()
6233   "Fill in the Xref value in `gnus-current-headers', if necessary.
6234 This is meant to be called in `gnus-article-internal-prepare-hook'."
6235   (let ((headers (with-current-buffer gnus-summary-buffer
6236                    gnus-current-headers)))
6237     (or (not gnus-use-cross-reference)
6238         (not headers)
6239         (and (mail-header-xref headers)
6240              (not (string= (mail-header-xref headers) "")))
6241         (let ((case-fold-search t)
6242               xref)
6243           (save-restriction
6244             (nnheader-narrow-to-headers)
6245             (goto-char (point-min))
6246             (when (or (and (not (eobp))
6247                            (eq (downcase (char-after)) ?x)
6248                            (looking-at "Xref:"))
6249                       (search-forward "\nXref:" nil t))
6250               (goto-char (1+ (match-end 0)))
6251               (setq xref (buffer-substring (point) (point-at-eol)))
6252               (mail-header-set-xref headers xref)))))))
6253
6254 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6255   "Find article ID and insert the summary line for that article.
6256 OLD-HEADER can either be a header or a line number to insert
6257 the subject line on."
6258   (let* ((line (and (numberp old-header) old-header))
6259          (old-header (and (vectorp old-header) old-header))
6260          (header (cond ((and old-header use-old-header)
6261                         old-header)
6262                        ((and (numberp id)
6263                              (gnus-number-to-header id))
6264                         (gnus-number-to-header id))
6265                        (t
6266                         (gnus-read-header id))))
6267          (number (and (numberp id) id))
6268          d)
6269     (when header
6270       ;; Rebuild the thread that this article is part of and go to the
6271       ;; article we have fetched.
6272       (when (and (not gnus-show-threads)
6273                  old-header)
6274         (when (and number
6275                    (setq d (gnus-data-find (mail-header-number old-header))))
6276           (goto-char (gnus-data-pos d))
6277           (gnus-data-remove
6278            number
6279            (- (point-at-bol)
6280               (prog1
6281                   (1+ (point-at-eol))
6282                 (gnus-delete-line))))))
6283       ;; Remove list identifiers from subject.
6284       (when gnus-list-identifiers
6285         (let ((gnus-newsgroup-headers (list header)))
6286           (gnus-summary-remove-list-identifiers)))
6287       (when old-header
6288         (mail-header-set-number header (mail-header-number old-header)))
6289       (setq gnus-newsgroup-sparse
6290             (delq (setq number (mail-header-number header))
6291                   gnus-newsgroup-sparse))
6292       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6293       (push number gnus-newsgroup-limit)
6294       (gnus-rebuild-thread (mail-header-id header) line)
6295       (gnus-summary-goto-subject number nil t))
6296     (when (and (numberp number)
6297                (> number 0))
6298       ;; We have to update the boundaries even if we can't fetch the
6299       ;; article if ID is a number -- so that the next `P' or `N'
6300       ;; command will fetch the previous (or next) article even
6301       ;; if the one we tried to fetch this time has been canceled.
6302       (when (> number gnus-newsgroup-end)
6303         (setq gnus-newsgroup-end number))
6304       (when (< number gnus-newsgroup-begin)
6305         (setq gnus-newsgroup-begin number))
6306       (setq gnus-newsgroup-unselected
6307             (delq number gnus-newsgroup-unselected)))
6308     ;; Report back a success?
6309     (and header (mail-header-number header))))
6310
6311 ;;; Process/prefix in the summary buffer
6312
6313 (defun gnus-summary-work-articles (n)
6314   "Return a list of articles to be worked upon.
6315 The prefix argument, the list of process marked articles, and the
6316 current article will be taken into consideration."
6317   (save-excursion
6318     (set-buffer gnus-summary-buffer)
6319     (cond
6320      (n
6321       ;; A numerical prefix has been given.
6322       (setq n (prefix-numeric-value n))
6323       (let ((backward (< n 0))
6324             (n (abs (prefix-numeric-value n)))
6325             articles article)
6326         (save-excursion
6327           (while
6328               (and (> n 0)
6329                    (push (setq article (gnus-summary-article-number))
6330                          articles)
6331                    (if backward
6332                        (gnus-summary-find-prev nil article)
6333                      (gnus-summary-find-next nil article)))
6334             (decf n)))
6335         (nreverse articles)))
6336      ((and (gnus-region-active-p) (mark))
6337       (message "region active")
6338       ;; Work on the region between point and mark.
6339       (let ((max (max (point) (mark)))
6340             articles article)
6341         (save-excursion
6342           (goto-char (min (point) (mark)))
6343           (while
6344               (and
6345                (push (setq article (gnus-summary-article-number)) articles)
6346                (gnus-summary-find-next nil article)
6347                (< (point) max)))
6348           (nreverse articles))))
6349      (gnus-newsgroup-processable
6350       ;; There are process-marked articles present.
6351       ;; Save current state.
6352       (gnus-summary-save-process-mark)
6353       ;; Return the list.
6354       (reverse gnus-newsgroup-processable))
6355      (t
6356       ;; Just return the current article.
6357       (list (gnus-summary-article-number))))))
6358
6359 (defmacro gnus-summary-iterate (arg &rest forms)
6360   "Iterate over the process/prefixed articles and do FORMS.
6361 ARG is the interactive prefix given to the command.  FORMS will be
6362 executed with point over the summary line of the articles."
6363   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6364     `(let ((,articles (gnus-summary-work-articles ,arg)))
6365        (while ,articles
6366          (gnus-summary-goto-subject (car ,articles))
6367          ,@forms
6368          (pop ,articles)))))
6369
6370 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6371 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6372
6373 (defun gnus-summary-save-process-mark ()
6374   "Push the current set of process marked articles on the stack."
6375   (interactive)
6376   (push (copy-sequence gnus-newsgroup-processable)
6377         gnus-newsgroup-process-stack))
6378
6379 (defun gnus-summary-kill-process-mark ()
6380   "Push the current set of process marked articles on the stack and unmark."
6381   (interactive)
6382   (gnus-summary-save-process-mark)
6383   (gnus-summary-unmark-all-processable))
6384
6385 (defun gnus-summary-yank-process-mark ()
6386   "Pop the last process mark state off the stack and restore it."
6387   (interactive)
6388   (unless gnus-newsgroup-process-stack
6389     (error "Empty mark stack"))
6390   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6391
6392 (defun gnus-summary-process-mark-set (set)
6393   "Make SET into the current process marked articles."
6394   (gnus-summary-unmark-all-processable)
6395   (mapc 'gnus-summary-set-process-mark set))
6396
6397 ;;; Searching and stuff
6398
6399 (defun gnus-summary-search-group (&optional backward use-level)
6400   "Search for next unread newsgroup.
6401 If optional argument BACKWARD is non-nil, search backward instead."
6402   (save-excursion
6403     (set-buffer gnus-group-buffer)
6404     (when (gnus-group-search-forward
6405            backward nil (if use-level (gnus-group-group-level) nil))
6406       (gnus-group-group-name))))
6407
6408 (defun gnus-summary-best-group (&optional exclude-group)
6409   "Find the name of the best unread group.
6410 If EXCLUDE-GROUP, do not go to this group."
6411   (with-current-buffer gnus-group-buffer
6412     (save-excursion
6413       (gnus-group-best-unread-group exclude-group))))
6414
6415 (defun gnus-summary-find-next (&optional unread article backward)
6416   (if backward
6417       (gnus-summary-find-prev unread article)
6418     (let* ((dummy (gnus-summary-article-intangible-p))
6419            (article (or article (gnus-summary-article-number)))
6420            (data (gnus-data-find-list article))
6421            result)
6422       (when (and (not dummy)
6423                  (or (not gnus-summary-check-current)
6424                      (not unread)
6425                      (not (gnus-data-unread-p (car data)))))
6426         (setq data (cdr data)))
6427       (when (setq result
6428                   (if unread
6429                       (progn
6430                         (while data
6431                           (unless (memq (gnus-data-number (car data))
6432                                         (cond
6433                                          ((eq gnus-auto-goto-ignores
6434                                               'always-undownloaded)
6435                                           gnus-newsgroup-undownloaded)
6436                                          (gnus-plugged
6437                                           nil)
6438                                          ((eq gnus-auto-goto-ignores
6439                                               'unfetched)
6440                                           gnus-newsgroup-unfetched)
6441                                          ((eq gnus-auto-goto-ignores
6442                                               'undownloaded)
6443                                           gnus-newsgroup-undownloaded)))
6444                             (when (gnus-data-unread-p (car data))
6445                               (setq result (car data)
6446                                     data nil)))
6447                           (setq data (cdr data)))
6448                         result)
6449                     (car data)))
6450         (goto-char (gnus-data-pos result))
6451         (gnus-data-number result)))))
6452
6453 (defun gnus-summary-find-prev (&optional unread article)
6454   (let* ((eobp (eobp))
6455          (article (or article (gnus-summary-article-number)))
6456          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6457          result)
6458     (when (and (not eobp)
6459                (or (not gnus-summary-check-current)
6460                    (not unread)
6461                    (not (gnus-data-unread-p (car data)))))
6462       (setq data (cdr data)))
6463     (when (setq result
6464                 (if unread
6465                     (progn
6466                       (while data
6467                         (unless (memq (gnus-data-number (car data))
6468                                       (cond
6469                                        ((eq gnus-auto-goto-ignores
6470                                             'always-undownloaded)
6471                                         gnus-newsgroup-undownloaded)
6472                                        (gnus-plugged
6473                                         nil)
6474                                        ((eq gnus-auto-goto-ignores
6475                                             'unfetched)
6476                                         gnus-newsgroup-unfetched)
6477                                        ((eq gnus-auto-goto-ignores
6478                                             'undownloaded)
6479                                         gnus-newsgroup-undownloaded)))
6480                           (when (gnus-data-unread-p (car data))
6481                             (setq result (car data)
6482                                   data nil)))
6483                         (setq data (cdr data)))
6484                       result)
6485                   (car data)))
6486       (goto-char (gnus-data-pos result))
6487       (gnus-data-number result))))
6488
6489 (defun gnus-summary-find-subject (subject &optional unread backward article)
6490   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6491          (article (or article (gnus-summary-article-number)))
6492          (articles (gnus-data-list backward))
6493          (arts (gnus-data-find-list article articles))
6494          result)
6495     (when (or (not gnus-summary-check-current)
6496               (not unread)
6497               (not (gnus-data-unread-p (car arts))))
6498       (setq arts (cdr arts)))
6499     (while arts
6500       (and (or (not unread)
6501                (gnus-data-unread-p (car arts)))
6502            (vectorp (gnus-data-header (car arts)))
6503            (gnus-subject-equal
6504             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6505            (setq result (car arts)
6506                  arts nil))
6507       (setq arts (cdr arts)))
6508     (and result
6509          (goto-char (gnus-data-pos result))
6510          (gnus-data-number result))))
6511
6512 (defun gnus-summary-search-forward (&optional unread subject backward)
6513   "Search forward for an article.
6514 If UNREAD, look for unread articles.  If SUBJECT, look for
6515 articles with that subject.  If BACKWARD, search backward instead."
6516   (cond (subject (gnus-summary-find-subject subject unread backward))
6517         (backward (gnus-summary-find-prev unread))
6518         (t (gnus-summary-find-next unread))))
6519
6520 (defun gnus-recenter (&optional n)
6521   "Center point in window and redisplay frame.
6522 Also do horizontal recentering."
6523   (interactive "P")
6524   (when (and gnus-auto-center-summary
6525              (not (eq gnus-auto-center-summary 'vertical)))
6526     (gnus-horizontal-recenter))
6527   (recenter n))
6528
6529 (defun gnus-summary-recenter ()
6530   "Center point in the summary window.
6531 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6532 displayed, no centering will be performed."
6533   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6534   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6535   (interactive)
6536   ;; The user has to want it.
6537   (when gnus-auto-center-summary
6538     (let* ((top (cond ((< (window-height) 4) 0)
6539                       ((< (window-height) 7) 1)
6540                       (t (if (numberp gnus-auto-center-summary)
6541                              gnus-auto-center-summary
6542                            (/ (1- (window-height)) 2)))))
6543            (height (1- (window-height)))
6544            (bottom (save-excursion (goto-char (point-max))
6545                                    (forward-line (- height))
6546                                    (point)))
6547            (window (get-buffer-window (current-buffer))))
6548       (when (get-buffer-window gnus-article-buffer)
6549         ;; Only do recentering when the article buffer is displayed,
6550         ;; Set the window start to either `bottom', which is the biggest
6551         ;; possible valid number, or the second line from the top,
6552         ;; whichever is the least.
6553         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6554           (if (> bottom top-pos)
6555               ;; Keep the second line from the top visible
6556               (set-window-start window top-pos t)
6557             ;; Try to keep the bottom line visible; if it's partially
6558             ;; obscured, either scroll one more line to make it fully
6559             ;; visible, or revert to using TOP-POS.
6560             (save-excursion
6561               (goto-char (point-max))
6562               (forward-line -1)
6563               (let ((last-line-start (point)))
6564                 (goto-char bottom)
6565                 (set-window-start window (point) t)
6566                 (when (not (pos-visible-in-window-p last-line-start window))
6567                   (forward-line 1)
6568                   (set-window-start window (min (point) top-pos) t)))))))
6569       ;; Do horizontal recentering while we're at it.
6570       (when (and (get-buffer-window (current-buffer) t)
6571                  (not (eq gnus-auto-center-summary 'vertical)))
6572         (let ((selected (selected-window)))
6573           (select-window (get-buffer-window (current-buffer) t))
6574           (gnus-summary-position-point)
6575           (gnus-horizontal-recenter)
6576           (select-window selected))))))
6577
6578 (defun gnus-summary-jump-to-group (newsgroup)
6579   "Move point to NEWSGROUP in group mode buffer."
6580   ;; Keep update point of group mode buffer if visible.
6581   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6582       (save-window-excursion
6583         ;; Take care of tree window mode.
6584         (when (get-buffer-window gnus-group-buffer)
6585           (pop-to-buffer gnus-group-buffer))
6586         (gnus-group-jump-to-group newsgroup))
6587     (save-excursion
6588       ;; Take care of tree window mode.
6589       (if (get-buffer-window gnus-group-buffer)
6590           (pop-to-buffer gnus-group-buffer)
6591         (set-buffer gnus-group-buffer))
6592       (gnus-group-jump-to-group newsgroup))))
6593
6594 ;; This function returns a list of article numbers based on the
6595 ;; difference between the ranges of read articles in this group and
6596 ;; the range of active articles.
6597 (defun gnus-list-of-unread-articles (group)
6598   (let* ((read (gnus-info-read (gnus-get-info group)))
6599          (active (or (gnus-active group) (gnus-activate-group group)))
6600          (last (or (cdr active)
6601                    (error "Group %s couldn't be activated " group)))
6602          first nlast unread)
6603     ;; If none are read, then all are unread.
6604     (if (not read)
6605         (setq first (car active))
6606       ;; If the range of read articles is a single range, then the
6607       ;; first unread article is the article after the last read
6608       ;; article.  Sounds logical, doesn't it?
6609       (if (and (not (listp (cdr read)))
6610                (or (< (car read) (car active))
6611                    (progn (setq read (list read))
6612                           nil)))
6613           (setq first (max (car active) (1+ (cdr read))))
6614         ;; `read' is a list of ranges.
6615         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6616                                   (caar read)))
6617                   1)
6618           (setq first (car active)))
6619         (while read
6620           (when first
6621             (while (< first nlast)
6622               (setq unread (cons first unread)
6623                     first (1+ first))))
6624           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6625           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6626           (setq read (cdr read)))))
6627     ;; And add the last unread articles.
6628     (while (<= first last)
6629       (setq unread (cons first unread)
6630             first (1+ first)))
6631     ;; Return the list of unread articles.
6632     (delq 0 (nreverse unread))))
6633
6634 (defun gnus-list-of-read-articles (group)
6635   "Return a list of unread, unticked and non-dormant articles."
6636   (let* ((info (gnus-get-info group))
6637          (marked (gnus-info-marks info))
6638          (active (gnus-active group)))
6639     (and info active
6640          (gnus-list-range-difference
6641           (gnus-list-range-difference
6642            (gnus-sorted-complement
6643             (gnus-uncompress-range active)
6644             (gnus-list-of-unread-articles group))
6645            (cdr (assq 'dormant marked)))
6646           (cdr (assq 'tick marked))))))
6647
6648 ;; This function returns a sequence of article numbers based on the
6649 ;; difference between the ranges of read articles in this group and
6650 ;; the range of active articles.
6651 (defun gnus-sequence-of-unread-articles (group)
6652   (let* ((read (gnus-info-read (gnus-get-info group)))
6653          (active (or (gnus-active group) (gnus-activate-group group)))
6654          (last (cdr active))
6655          first nlast unread)
6656     ;; If none are read, then all are unread.
6657     (if (not read)
6658         (setq first (car active))
6659       ;; If the range of read articles is a single range, then the
6660       ;; first unread article is the article after the last read
6661       ;; article.  Sounds logical, doesn't it?
6662       (if (and (not (listp (cdr read)))
6663                (or (< (car read) (car active))
6664                    (progn (setq read (list read))
6665                           nil)))
6666           (setq first (max (car active) (1+ (cdr read))))
6667         ;; `read' is a list of ranges.
6668         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6669                                   (caar read)))
6670                   1)
6671           (setq first (car active)))
6672         (while read
6673           (when first
6674             (push (cons first nlast) unread))
6675           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6676           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6677           (setq read (cdr read)))))
6678     ;; And add the last unread articles.
6679     (cond ((not (and first last))
6680            nil)
6681           ((< first last)
6682            (push (cons first last) unread))
6683           ((= first last)
6684            (push first unread)))
6685     ;; Return the sequence of unread articles.
6686     (delq 0 (nreverse unread))))
6687
6688 ;; Various summary commands
6689
6690 (defun gnus-summary-select-article-buffer ()
6691   "Reconfigure windows to show article buffer."
6692   (interactive)
6693   (if (not (gnus-buffer-live-p gnus-article-buffer))
6694       (error "There is no article buffer for this summary buffer")
6695     (gnus-configure-windows 'article)
6696     (select-window (get-buffer-window gnus-article-buffer))))
6697
6698 (defun gnus-summary-universal-argument (arg)
6699   "Perform any operation on all articles that are process/prefixed."
6700   (interactive "P")
6701   (let ((articles (gnus-summary-work-articles arg))
6702         func article)
6703     (if (eq
6704          (setq
6705           func
6706           (key-binding
6707            (read-key-sequence
6708             (substitute-command-keys
6709              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6710          'undefined)
6711         (gnus-error 1 "Undefined key")
6712       (save-excursion
6713         (while articles
6714           (gnus-summary-goto-subject (setq article (pop articles)))
6715           (let (gnus-newsgroup-processable)
6716             (command-execute func))
6717           (gnus-summary-remove-process-mark article)))))
6718   (gnus-summary-position-point))
6719
6720 (defun gnus-summary-toggle-truncation (&optional arg)
6721   "Toggle truncation of summary lines.
6722 With ARG, turn line truncation on if ARG is positive."
6723   (interactive "P")
6724   (setq truncate-lines
6725         (if (null arg) (not truncate-lines)
6726           (> (prefix-numeric-value arg) 0)))
6727   (redraw-display))
6728
6729 (defun gnus-summary-find-for-reselect ()
6730   "Return the number of an article to stay on across a reselect.
6731 The current article is considered, then following articles, then previous
6732 articles.  An article is sought which is not cancelled and isn't a temporary
6733 insertion from another group.  If there's no such then return a dummy 0."
6734   (let (found)
6735     (dolist (rev '(nil t))
6736       (unless found      ; don't demand the reverse list if we don't need it
6737         (let ((data (gnus-data-find-list
6738                      (gnus-summary-article-number) (gnus-data-list rev))))
6739           (while (and data (not found))
6740             (if (and (< 0 (gnus-data-number (car data)))
6741                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6742                 (setq found (gnus-data-number (car data))))
6743             (setq data (cdr data))))))
6744     (or found 0)))
6745
6746 (defun gnus-summary-reselect-current-group (&optional all rescan)
6747   "Exit and then reselect the current newsgroup.
6748 The prefix argument ALL means to select all articles."
6749   (interactive "P")
6750   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6751     (error "Ephemeral groups can't be reselected"))
6752   (let ((current-subject (gnus-summary-find-for-reselect))
6753         (group gnus-newsgroup-name))
6754     (setq gnus-newsgroup-begin nil)
6755     (gnus-summary-exit nil 'leave-hidden)
6756     ;; We have to adjust the point of group mode buffer because
6757     ;; point was moved to the next unread newsgroup by exiting.
6758     (gnus-summary-jump-to-group group)
6759     (when rescan
6760       (save-excursion
6761         (gnus-group-get-new-news-this-group 1)))
6762     (gnus-group-read-group all t)
6763     (gnus-summary-goto-subject current-subject nil t)))
6764
6765 (defun gnus-summary-rescan-group (&optional all)
6766   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6767   (interactive "P")
6768   (gnus-summary-reselect-current-group all t))
6769
6770 (defun gnus-summary-update-info (&optional non-destructive)
6771   (save-excursion
6772     (let ((group gnus-newsgroup-name))
6773       (when group
6774         (when gnus-newsgroup-kill-headers
6775           (setq gnus-newsgroup-killed
6776                 (gnus-compress-sequence
6777                  (gnus-sorted-union
6778                   (gnus-list-range-intersection
6779                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6780                   gnus-newsgroup-unreads)
6781                  t)))
6782         (unless (listp (cdr gnus-newsgroup-killed))
6783           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6784         (let ((headers gnus-newsgroup-headers))
6785           ;; Set the new ranges of read articles.
6786           (with-current-buffer gnus-group-buffer
6787             (gnus-undo-force-boundary))
6788           (gnus-update-read-articles
6789            group (gnus-sorted-union
6790                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6791           ;; Set the current article marks.
6792           (let ((gnus-newsgroup-scored
6793                  (if (and (not gnus-save-score)
6794                           (not non-destructive))
6795                      nil
6796                    gnus-newsgroup-scored)))
6797             (save-excursion
6798               (gnus-update-marks)))
6799           ;; Do the cross-ref thing.
6800           (when gnus-use-cross-reference
6801             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6802           ;; Do not switch windows but change the buffer to work.
6803           (set-buffer gnus-group-buffer)
6804           (unless (gnus-ephemeral-group-p group)
6805             (gnus-group-update-group group)))))))
6806
6807 (defun gnus-summary-save-newsrc (&optional force)
6808   "Save the current number of read/marked articles in the dribble buffer.
6809 The dribble buffer will then be saved.
6810 If FORCE (the prefix), also save the .newsrc file(s)."
6811   (interactive "P")
6812   (gnus-summary-update-info t)
6813   (if force
6814       (gnus-save-newsrc-file)
6815     (gnus-dribble-save)))
6816
6817 (defun gnus-summary-exit (&optional temporary leave-hidden)
6818   "Exit reading current newsgroup, and then return to group selection mode.
6819 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6820   (interactive)
6821   (gnus-set-global-variables)
6822   (when (gnus-buffer-live-p gnus-article-buffer)
6823     (save-excursion
6824       (set-buffer gnus-article-buffer)
6825       (mm-destroy-parts gnus-article-mime-handles)
6826       ;; Set it to nil for safety reason.
6827       (setq gnus-article-mime-handle-alist nil)
6828       (setq gnus-article-mime-handles nil)))
6829   (gnus-kill-save-kill-buffer)
6830   (gnus-async-halt-prefetch)
6831   (let* ((group gnus-newsgroup-name)
6832          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6833          (gnus-group-is-exiting-p t)
6834          (mode major-mode)
6835          (group-point nil)
6836          (buf (current-buffer)))
6837     (unless quit-config
6838       ;; Do adaptive scoring, and possibly save score files.
6839       (when gnus-newsgroup-adaptive
6840         (gnus-score-adaptive))
6841       (when gnus-use-scoring
6842         (gnus-score-save)))
6843     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6844     ;; If we have several article buffers, we kill them at exit.
6845     (unless gnus-single-article-buffer
6846       (gnus-kill-buffer gnus-original-article-buffer)
6847       (setq gnus-article-current nil))
6848     (when gnus-use-cache
6849       (gnus-cache-possibly-remove-articles)
6850       (gnus-cache-save-buffers))
6851     (gnus-async-prefetch-remove-group group)
6852     (when gnus-suppress-duplicates
6853       (gnus-dup-enter-articles))
6854     (when gnus-use-trees
6855       (gnus-tree-close group))
6856     (when gnus-use-cache
6857       (gnus-cache-write-active))
6858     ;; Remove entries for this group.
6859     (nnmail-purge-split-history (gnus-group-real-name group))
6860     ;; Make all changes in this group permanent.
6861     (unless quit-config
6862       (gnus-run-hooks 'gnus-exit-group-hook)
6863       (gnus-summary-update-info))
6864     (gnus-close-group group)
6865     ;; Make sure where we were, and go to next newsgroup.
6866     (set-buffer gnus-group-buffer)
6867     (unless quit-config
6868       (gnus-group-jump-to-group group))
6869     (gnus-run-hooks 'gnus-summary-exit-hook)
6870     (unless (or quit-config
6871                 ;; If this group has disappeared from the summary
6872                 ;; buffer, don't skip forwards.
6873                 (not (string= group (gnus-group-group-name))))
6874       (gnus-group-next-unread-group 1))
6875     (setq group-point (point))
6876     (if temporary
6877         nil                             ;Nothing to do.
6878       ;; If we have several article buffers, we kill them at exit.
6879       (unless gnus-single-article-buffer
6880         (gnus-kill-buffer gnus-article-buffer)
6881         (gnus-kill-buffer gnus-original-article-buffer)
6882         (setq gnus-article-current nil))
6883       (set-buffer buf)
6884       (if (not gnus-kill-summary-on-exit)
6885           (progn
6886             (gnus-deaden-summary)
6887             (setq mode nil))
6888         ;; We set all buffer-local variables to nil.  It is unclear why
6889         ;; this is needed, but if we don't, buffer-local variables are
6890         ;; not garbage-collected, it seems.  This would the lead to en
6891         ;; ever-growing Emacs.
6892         (gnus-summary-clear-local-variables)
6893         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6894           (gnus-summary-clear-local-variables))
6895         (when (get-buffer gnus-article-buffer)
6896           (bury-buffer gnus-article-buffer))
6897         ;; Return to group mode buffer.
6898         (when (eq mode 'gnus-summary-mode)
6899           (gnus-kill-buffer buf)))
6900       (setq gnus-current-select-method gnus-select-method)
6901       (set-buffer gnus-group-buffer)
6902       (if quit-config
6903           (gnus-handle-ephemeral-exit quit-config)
6904         (goto-char group-point)
6905         ;; If gnus-group-buffer is already displayed, make sure we also move
6906         ;; the cursor in the window that displays it.
6907         (let ((win (get-buffer-window (current-buffer) 0)))
6908           (if win (set-window-point win (point))))
6909         (unless leave-hidden
6910           (gnus-configure-windows 'group 'force)))
6911       ;; Clear the current group name.
6912       (unless quit-config
6913         (setq gnus-newsgroup-name nil)))))
6914
6915 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6916 (defun gnus-summary-exit-no-update (&optional no-questions)
6917   "Quit reading current newsgroup without updating read article info."
6918   (interactive)
6919   (let* ((group gnus-newsgroup-name)
6920          (gnus-group-is-exiting-p t)
6921          (gnus-group-is-exiting-without-update-p t)
6922          (quit-config (gnus-group-quit-config group)))
6923     (when (or no-questions
6924               gnus-expert-user
6925               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6926       (gnus-async-halt-prefetch)
6927       (run-hooks 'gnus-summary-prepare-exit-hook)
6928       (when (gnus-buffer-live-p gnus-article-buffer)
6929         (save-excursion
6930           (set-buffer gnus-article-buffer)
6931           (mm-destroy-parts gnus-article-mime-handles)
6932           ;; Set it to nil for safety reason.
6933           (setq gnus-article-mime-handle-alist nil)
6934           (setq gnus-article-mime-handles nil)))
6935       ;; If we have several article buffers, we kill them at exit.
6936       (unless gnus-single-article-buffer
6937         (gnus-kill-buffer gnus-article-buffer)
6938         (gnus-kill-buffer gnus-original-article-buffer)
6939         (setq gnus-article-current nil))
6940       (if (not gnus-kill-summary-on-exit)
6941           (gnus-deaden-summary)
6942         (gnus-close-group group)
6943         (gnus-summary-clear-local-variables)
6944         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6945           (gnus-summary-clear-local-variables))
6946         (gnus-kill-buffer gnus-summary-buffer))
6947       (unless gnus-single-article-buffer
6948         (setq gnus-article-current nil))
6949       (when gnus-use-trees
6950         (gnus-tree-close group))
6951       (gnus-async-prefetch-remove-group group)
6952       (when (get-buffer gnus-article-buffer)
6953         (bury-buffer gnus-article-buffer))
6954       ;; Return to the group buffer.
6955       (gnus-configure-windows 'group 'force)
6956       ;; Clear the current group name.
6957       (setq gnus-newsgroup-name nil)
6958       (unless (gnus-ephemeral-group-p group)
6959         (gnus-group-update-group group))
6960       (when (equal (gnus-group-group-name) group)
6961         (gnus-group-next-unread-group 1))
6962       (when quit-config
6963         (gnus-handle-ephemeral-exit quit-config)))))
6964
6965 (defun gnus-handle-ephemeral-exit (quit-config)
6966   "Handle movement when leaving an ephemeral group.
6967 The state which existed when entering the ephemeral is reset."
6968   (if (not (buffer-name (car quit-config)))
6969       (gnus-configure-windows 'group 'force)
6970     (set-buffer (car quit-config))
6971     (cond ((eq major-mode 'gnus-summary-mode)
6972            (gnus-set-global-variables))
6973           ((eq major-mode 'gnus-article-mode)
6974            (save-excursion
6975              ;; The `gnus-summary-buffer' variable may point
6976              ;; to the old summary buffer when using a single
6977              ;; article buffer.
6978              (unless (gnus-buffer-live-p gnus-summary-buffer)
6979                (set-buffer gnus-group-buffer))
6980              (set-buffer gnus-summary-buffer)
6981              (gnus-set-global-variables))))
6982     (if (or (eq (cdr quit-config) 'article)
6983             (eq (cdr quit-config) 'pick))
6984         (progn
6985           ;; The current article may be from the ephemeral group
6986           ;; thus it is best that we reload this article
6987           ;;
6988           ;; If we're exiting from a large digest, this can be
6989           ;; extremely slow.  So, it's better not to reload it. -- jh.
6990           ;;(gnus-summary-show-article)
6991           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6992               (gnus-configure-windows 'pick 'force)
6993             (gnus-configure-windows (cdr quit-config) 'force)))
6994       (gnus-configure-windows (cdr quit-config) 'force))
6995     (when (eq major-mode 'gnus-summary-mode)
6996       (gnus-summary-next-subject 1 nil t)
6997       (gnus-summary-recenter)
6998       (gnus-summary-position-point))))
6999
7000 ;;; Dead summaries.
7001
7002 (defvar gnus-dead-summary-mode-map nil)
7003
7004 (unless gnus-dead-summary-mode-map
7005   (setq gnus-dead-summary-mode-map (make-keymap))
7006   (suppress-keymap gnus-dead-summary-mode-map)
7007   (substitute-key-definition
7008    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7009   (dolist (key '("\C-d" "\r" "\177" [delete]))
7010     (define-key gnus-dead-summary-mode-map
7011       key 'gnus-summary-wake-up-the-dead))
7012   (dolist (key '("q" "Q"))
7013     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7014
7015 (defvar gnus-dead-summary-mode nil
7016   "Minor mode for Gnus summary buffers.")
7017
7018 (defun gnus-dead-summary-mode (&optional arg)
7019   "Minor mode for Gnus summary buffers."
7020   (interactive "P")
7021   (when (eq major-mode 'gnus-summary-mode)
7022     (make-local-variable 'gnus-dead-summary-mode)
7023     (setq gnus-dead-summary-mode
7024           (if (null arg) (not gnus-dead-summary-mode)
7025             (> (prefix-numeric-value arg) 0)))
7026     (when gnus-dead-summary-mode
7027       (add-minor-mode
7028        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7029
7030 (defun gnus-deaden-summary ()
7031   "Make the current summary buffer into a dead summary buffer."
7032   ;; Kill any previous dead summary buffer.
7033   (when (and gnus-dead-summary
7034              (buffer-name gnus-dead-summary))
7035     (with-current-buffer gnus-dead-summary
7036       (when gnus-dead-summary-mode
7037         (kill-buffer (current-buffer)))))
7038   ;; Make this the current dead summary.
7039   (setq gnus-dead-summary (current-buffer))
7040   (gnus-dead-summary-mode 1)
7041   (let ((name (buffer-name)))
7042     (when (string-match "Summary" name)
7043       (rename-buffer
7044        (concat (substring name 0 (match-beginning 0)) "Dead "
7045                (substring name (match-beginning 0)))
7046        t)
7047       (bury-buffer))))
7048
7049 (defun gnus-kill-or-deaden-summary (buffer)
7050   "Kill or deaden the summary BUFFER."
7051   (save-excursion
7052     (when (and (buffer-name buffer)
7053                (not gnus-single-article-buffer))
7054       (with-current-buffer buffer
7055         (gnus-kill-buffer gnus-article-buffer)
7056         (gnus-kill-buffer gnus-original-article-buffer)))
7057     (cond
7058      ;; Kill the buffer.
7059      (gnus-kill-summary-on-exit
7060       (when (and gnus-use-trees
7061                  (gnus-buffer-exists-p buffer))
7062         (save-excursion
7063           (set-buffer buffer)
7064           (gnus-tree-close gnus-newsgroup-name)))
7065       (gnus-kill-buffer buffer))
7066      ;; Deaden the buffer.
7067      ((gnus-buffer-exists-p buffer)
7068       (save-excursion
7069         (set-buffer buffer)
7070         (gnus-deaden-summary))))))
7071
7072 (defun gnus-summary-wake-up-the-dead (&rest args)
7073   "Wake up the dead summary buffer."
7074   (interactive)
7075   (gnus-dead-summary-mode -1)
7076   (let ((name (buffer-name)))
7077     (when (string-match "Dead " name)
7078       (rename-buffer
7079        (concat (substring name 0 (match-beginning 0))
7080                (substring name (match-end 0)))
7081        t)))
7082   (gnus-message 3 "This dead summary is now alive again"))
7083
7084 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7085 (defun gnus-summary-fetch-faq (&optional faq-dir)
7086   "Fetch the FAQ for the current group.
7087 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7088 in."
7089   (interactive
7090    (list
7091     (when current-prefix-arg
7092       (completing-read
7093        "FAQ dir: " (and (listp gnus-group-faq-directory)
7094                         (mapcar 'list
7095                                 gnus-group-faq-directory))))))
7096   (let (gnus-faq-buffer)
7097     (when (setq gnus-faq-buffer
7098                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7099       (gnus-configure-windows 'summary-faq))))
7100
7101 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7102 (defun gnus-summary-describe-group (&optional force)
7103   "Describe the current newsgroup."
7104   (interactive "P")
7105   (gnus-group-describe-group force gnus-newsgroup-name))
7106
7107 (defun gnus-summary-describe-briefly ()
7108   "Describe summary mode commands briefly."
7109   (interactive)
7110   (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")))
7111
7112 ;; Walking around group mode buffer from summary mode.
7113
7114 (defun gnus-summary-next-group (&optional no-article target-group backward)
7115   "Exit current newsgroup and then select next unread newsgroup.
7116 If prefix argument NO-ARTICLE is non-nil, no article is selected
7117 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7118 previous group instead."
7119   (interactive "P")
7120   ;; Stop pre-fetching.
7121   (gnus-async-halt-prefetch)
7122   (let ((current-group gnus-newsgroup-name)
7123         (current-buffer (current-buffer))
7124         entered)
7125     ;; First we semi-exit this group to update Xrefs and all variables.
7126     ;; We can't do a real exit, because the window conf must remain
7127     ;; the same in case the user is prompted for info, and we don't
7128     ;; want the window conf to change before that...
7129     (gnus-summary-exit t)
7130     (while (not entered)
7131       ;; Then we find what group we are supposed to enter.
7132       (set-buffer gnus-group-buffer)
7133       (gnus-group-jump-to-group current-group)
7134       (setq target-group
7135             (or target-group
7136                 (if (eq gnus-keep-same-level 'best)
7137                     (gnus-summary-best-group gnus-newsgroup-name)
7138                   (gnus-summary-search-group backward gnus-keep-same-level))))
7139       (if (not target-group)
7140           ;; There are no further groups, so we return to the group
7141           ;; buffer.
7142           (progn
7143             (gnus-message 5 "Returning to the group buffer")
7144             (setq entered t)
7145             (when (gnus-buffer-live-p current-buffer)
7146               (set-buffer current-buffer)
7147               (gnus-summary-exit))
7148             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7149         ;; We try to enter the target group.
7150         (gnus-group-jump-to-group target-group)
7151         (let ((unreads (gnus-group-group-unread)))
7152           (if (and (or (eq t unreads)
7153                        (and unreads (not (zerop unreads))))
7154                    (gnus-summary-read-group
7155                     target-group nil no-article
7156                     (and (buffer-name current-buffer) current-buffer)
7157                     nil backward))
7158               (setq entered t)
7159             (setq current-group target-group
7160                   target-group nil)))))))
7161
7162 (defun gnus-summary-prev-group (&optional no-article)
7163   "Exit current newsgroup and then select previous unread newsgroup.
7164 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7165   (interactive "P")
7166   (gnus-summary-next-group no-article nil t))
7167
7168 ;; Walking around summary lines.
7169
7170 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7171   "Go to the first subject satisfying any non-nil constraint.
7172 If UNREAD is non-nil, the article should be unread.
7173 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7174 If UNSEEN is non-nil, the article should be unseen.
7175 Returns the article selected or nil if there are no matching articles."
7176   (interactive "P")
7177   (cond
7178    ;; Empty summary.
7179    ((null gnus-newsgroup-data)
7180     (gnus-message 3 "No articles in the group")
7181     nil)
7182    ;; Pick the first article.
7183    ((not (or unread undownloaded unseen))
7184     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7185     (gnus-data-number (car gnus-newsgroup-data)))
7186    ;; Find the first unread article.
7187    (t
7188     (let ((data gnus-newsgroup-data))
7189       (while (and data
7190                   (let ((num (gnus-data-number (car data))))
7191                     (or (memq num gnus-newsgroup-unfetched)
7192                         (not (or (and unread
7193                                       (memq num gnus-newsgroup-unreads))
7194                                  (and undownloaded
7195                                       (memq num gnus-newsgroup-undownloaded))
7196                                  (and unseen
7197                                       (memq num gnus-newsgroup-unseen)))))))
7198         (setq data (cdr data)))
7199       (prog1
7200           (if data
7201               (progn
7202                 (goto-char (gnus-data-pos (car data)))
7203                 (gnus-data-number (car data)))
7204             (gnus-message 3 "No more%s articles"
7205                           (let* ((r (when unread " unread"))
7206                                  (d (when undownloaded " undownloaded"))
7207                                  (s (when unseen " unseen"))
7208                                  (l (delq nil (list r d s))))
7209                             (cond ((= 3 (length l))
7210                                    (concat r "," d ", or" s))
7211                                   ((= 2 (length l))
7212                                    (concat (car l) ", or" (cadr l)))
7213                                   ((= 1 (length l))
7214                                    (car l))
7215                                   (t
7216                                    ""))))
7217             nil
7218             )
7219         (gnus-summary-position-point))))))
7220
7221 (defun gnus-summary-next-subject (n &optional unread dont-display)
7222   "Go to next N'th summary line.
7223 If N is negative, go to the previous N'th subject line.
7224 If UNREAD is non-nil, only unread articles are selected.
7225 The difference between N and the actual number of steps taken is
7226 returned."
7227   (interactive "p")
7228   (let ((backward (< n 0))
7229         (n (abs n)))
7230     (while (and (> n 0)
7231                 (if backward
7232                     (gnus-summary-find-prev unread)
7233                   (gnus-summary-find-next unread)))
7234       (unless (zerop (setq n (1- n)))
7235         (gnus-summary-show-thread)))
7236     (when (/= 0 n)
7237       (gnus-message 7 "No more%s articles"
7238                     (if unread " unread" "")))
7239     (unless dont-display
7240       (gnus-summary-recenter)
7241       (gnus-summary-position-point))
7242     n))
7243
7244 (defun gnus-summary-next-unread-subject (n)
7245   "Go to next N'th unread summary line."
7246   (interactive "p")
7247   (gnus-summary-next-subject n t))
7248
7249 (defun gnus-summary-prev-subject (n &optional unread)
7250   "Go to previous N'th summary line.
7251 If optional argument UNREAD is non-nil, only unread article is selected."
7252   (interactive "p")
7253   (gnus-summary-next-subject (- n) unread))
7254
7255 (defun gnus-summary-prev-unread-subject (n)
7256   "Go to previous N'th unread summary line."
7257   (interactive "p")
7258   (gnus-summary-next-subject (- n) t))
7259
7260 (defun gnus-summary-goto-subjects (articles)
7261   "Insert the subject header for ARTICLES in the current buffer."
7262   (save-excursion
7263     (dolist (article articles)
7264       (gnus-summary-goto-subject article t)))
7265   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7266   (gnus-summary-position-point))
7267
7268 (defun gnus-summary-goto-subject (article &optional force silent)
7269   "Go the subject line of ARTICLE.
7270 If FORCE, also allow jumping to articles not currently shown."
7271   (interactive "nArticle number: ")
7272   (unless (numberp article)
7273     (error "Article %s is not a number" article))
7274   (let ((b (point))
7275         (data (gnus-data-find article)))
7276     ;; We read in the article if we have to.
7277     (and (not data)
7278          force
7279          (gnus-summary-insert-subject
7280           article
7281           (if (or (numberp force) (vectorp force)) force)
7282           t)
7283          (setq data (gnus-data-find article)))
7284     (goto-char b)
7285     (if (not data)
7286         (progn
7287           (unless silent
7288             (gnus-message 3 "Can't find article %d" article))
7289           nil)
7290       (let ((pt (gnus-data-pos data)))
7291         (goto-char pt)
7292         (gnus-summary-set-article-display-arrow pt))
7293       (gnus-summary-position-point)
7294       article)))
7295
7296 ;; Walking around summary lines with displaying articles.
7297
7298 (defun gnus-summary-expand-window (&optional arg)
7299   "Make the summary buffer take up the entire Emacs frame.
7300 Given a prefix, will force an `article' buffer configuration."
7301   (interactive "P")
7302   (if arg
7303       (gnus-configure-windows 'article 'force)
7304     (gnus-configure-windows 'summary 'force)))
7305
7306 (defun gnus-summary-display-article (article &optional all-header)
7307   "Display ARTICLE in article buffer."
7308   (when (gnus-buffer-live-p gnus-article-buffer)
7309     (with-current-buffer gnus-article-buffer
7310       (mm-enable-multibyte)))
7311   (gnus-set-global-variables)
7312   (when (gnus-buffer-live-p gnus-article-buffer)
7313     (with-current-buffer gnus-article-buffer
7314       (setq gnus-article-charset gnus-newsgroup-charset)
7315       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7316       (mm-enable-multibyte)))
7317   (if (null article)
7318       nil
7319     (prog1
7320         (if gnus-summary-display-article-function
7321             (funcall gnus-summary-display-article-function article all-header)
7322           (gnus-article-prepare article all-header))
7323       (gnus-run-hooks 'gnus-select-article-hook)
7324       (when (and gnus-current-article
7325                  (not (zerop gnus-current-article)))
7326         (gnus-summary-goto-subject gnus-current-article))
7327       (gnus-summary-recenter)
7328       (when (and gnus-use-trees gnus-show-threads)
7329         (gnus-possibly-generate-tree article)
7330         (gnus-highlight-selected-tree article))
7331       ;; Successfully display article.
7332       (gnus-article-set-window-start
7333        (cdr (assq article gnus-newsgroup-bookmarks))))))
7334
7335 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7336   "Select the current article.
7337 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7338 non-nil, the article will be re-fetched even if it already present in
7339 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7340 be displayed."
7341   ;; Make sure we are in the summary buffer to work around bbdb bug.
7342   (unless (eq major-mode 'gnus-summary-mode)
7343     (set-buffer gnus-summary-buffer))
7344   (let ((article (or article (gnus-summary-article-number)))
7345         (all-headers (not (not all-headers))) ;Must be t or nil.
7346         gnus-summary-display-article-function)
7347     (and (not pseudo)
7348          (gnus-summary-article-pseudo-p article)
7349          (error "This is a pseudo-article"))
7350     (save-excursion
7351       (set-buffer gnus-summary-buffer)
7352       (if (or (and gnus-single-article-buffer
7353                    (or (null gnus-current-article)
7354                        (null gnus-article-current)
7355                        (null (get-buffer gnus-article-buffer))
7356                        (not (eq article (cdr gnus-article-current)))
7357                        (not (equal (car gnus-article-current)
7358                                    gnus-newsgroup-name))))
7359               (and (not gnus-single-article-buffer)
7360                    (or (null gnus-current-article)
7361                        (not (eq gnus-current-article article))))
7362               force)
7363           ;; The requested article is different from the current article.
7364           (progn
7365             (gnus-summary-display-article article all-headers)
7366             (when (gnus-buffer-live-p gnus-article-buffer)
7367               (with-current-buffer gnus-article-buffer
7368                 (if (not gnus-article-decoded-p) ;; a local variable
7369                     (mm-disable-multibyte))))
7370             (gnus-article-set-window-start
7371              (cdr (assq article gnus-newsgroup-bookmarks)))
7372             article)
7373         'old))))
7374
7375 (defun gnus-summary-force-verify-and-decrypt ()
7376   "Display buttons for signed/encrypted parts and verify/decrypt them."
7377   (interactive)
7378   (let ((mm-verify-option 'known)
7379         (mm-decrypt-option 'known)
7380         (gnus-article-emulate-mime t)
7381         (gnus-buttonized-mime-types (append (list "multipart/signed"
7382                                                   "multipart/encrypted")
7383                                             gnus-buttonized-mime-types)))
7384     (gnus-summary-select-article nil 'force)))
7385
7386 (defun gnus-summary-set-current-mark (&optional current-mark)
7387   "Obsolete function."
7388   nil)
7389
7390 (defun gnus-summary-next-article (&optional unread subject backward push)
7391   "Select the next article.
7392 If UNREAD, only unread articles are selected.
7393 If SUBJECT, only articles with SUBJECT are selected.
7394 If BACKWARD, the previous article is selected instead of the next."
7395   (interactive "P")
7396   (cond
7397    ;; Is there such an article?
7398    ((and (gnus-summary-search-forward unread subject backward)
7399          (or (gnus-summary-display-article (gnus-summary-article-number))
7400              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7401     (gnus-summary-position-point))
7402    ;; If not, we try the first unread, if that is wanted.
7403    ((and subject
7404          gnus-auto-select-same
7405          (gnus-summary-first-unread-article))
7406     (gnus-summary-position-point)
7407     (gnus-message 6 "Wrapped"))
7408    ;; Try to get next/previous article not displayed in this group.
7409    ((and gnus-auto-extend-newsgroup
7410          (not unread) (not subject))
7411     (gnus-summary-goto-article
7412      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7413      nil (count-lines (point-min) (point))))
7414    ;; Go to next/previous group.
7415    (t
7416     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7417       (gnus-summary-jump-to-group gnus-newsgroup-name))
7418     (let ((cmd last-command-char)
7419           (point
7420            (with-current-buffer gnus-group-buffer
7421              (point)))
7422           (group
7423            (if (eq gnus-keep-same-level 'best)
7424                (gnus-summary-best-group gnus-newsgroup-name)
7425              (gnus-summary-search-group backward gnus-keep-same-level))))
7426       ;; For some reason, the group window gets selected.  We change
7427       ;; it back.
7428       (select-window (get-buffer-window (current-buffer)))
7429       ;; Select next unread newsgroup automagically.
7430       (cond
7431        ((or (not gnus-auto-select-next)
7432             (not cmd))
7433         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7434        ((or (eq gnus-auto-select-next 'quietly)
7435             (and (eq gnus-auto-select-next 'slightly-quietly)
7436                  push)
7437             (and (eq gnus-auto-select-next 'almost-quietly)
7438                  (gnus-summary-last-article-p)))
7439         ;; Select quietly.
7440         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7441             (gnus-summary-exit)
7442           (gnus-message 7 "No more%s articles (%s)..."
7443                         (if unread " unread" "")
7444                         (if group (concat "selecting " group)
7445                           "exiting"))
7446           (gnus-summary-next-group nil group backward)))
7447        (t
7448         (when (gnus-key-press-event-p last-input-event)
7449           (gnus-summary-walk-group-buffer
7450            gnus-newsgroup-name cmd unread backward point))))))))
7451
7452 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7453   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7454                       (?\C-p (gnus-group-prev-unread-group 1))))
7455         (cursor-in-echo-area t)
7456         keve key group ended prompt)
7457     (save-excursion
7458       (set-buffer gnus-group-buffer)
7459       (goto-char start)
7460       (setq group
7461             (if (eq gnus-keep-same-level 'best)
7462                 (gnus-summary-best-group gnus-newsgroup-name)
7463               (gnus-summary-search-group backward gnus-keep-same-level))))
7464     (while (not ended)
7465       (setq prompt
7466             (format
7467              "No more%s articles%s " (if unread " unread" "")
7468              (if (and group
7469                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7470                  (format " (Type %s for %s [%s])"
7471                          (single-key-description cmd)
7472                          (gnus-group-decoded-name group)
7473                          (gnus-group-unread group))
7474                (format " (Type %s to exit %s)"
7475                        (single-key-description cmd)
7476                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7477       ;; Confirm auto selection.
7478       (setq key (car (setq keve (gnus-read-event-char prompt)))
7479             ended t)
7480       (cond
7481        ((assq key keystrokes)
7482         (let ((obuf (current-buffer)))
7483           (switch-to-buffer gnus-group-buffer)
7484           (when group
7485             (gnus-group-jump-to-group group))
7486           (eval (cadr (assq key keystrokes)))
7487           (setq group (gnus-group-group-name))
7488           (switch-to-buffer obuf))
7489         (setq ended nil))
7490        ((equal key cmd)
7491         (if (or (not group)
7492                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7493             (gnus-summary-exit)
7494           (gnus-summary-next-group nil group backward)))
7495        (t
7496         (push (cdr keve) unread-command-events))))))
7497
7498 (defun gnus-summary-next-unread-article ()
7499   "Select unread article after current one."
7500   (interactive)
7501   (gnus-summary-next-article
7502    (or (not (eq gnus-summary-goto-unread 'never))
7503        (gnus-summary-last-article-p (gnus-summary-article-number)))
7504    (and gnus-auto-select-same
7505         (gnus-summary-article-subject))))
7506
7507 (defun gnus-summary-prev-article (&optional unread subject)
7508   "Select the article after the current one.
7509 If UNREAD is non-nil, only unread articles are selected."
7510   (interactive "P")
7511   (gnus-summary-next-article unread subject t))
7512
7513 (defun gnus-summary-prev-unread-article ()
7514   "Select unread article before current one."
7515   (interactive)
7516   (gnus-summary-prev-article
7517    (or (not (eq gnus-summary-goto-unread 'never))
7518        (gnus-summary-first-article-p (gnus-summary-article-number)))
7519    (and gnus-auto-select-same
7520         (gnus-summary-article-subject))))
7521
7522 (defun gnus-summary-next-page (&optional lines circular stop)
7523   "Show next page of the selected article.
7524 If at the end of the current article, select the next article.
7525 LINES says how many lines should be scrolled up.
7526
7527 If CIRCULAR is non-nil, go to the start of the article instead of
7528 selecting the next article when reaching the end of the current
7529 article.
7530
7531 If STOP is non-nil, just stop when reaching the end of the message.
7532
7533 Also see the variable `gnus-article-skip-boring'."
7534   (interactive "P")
7535   (setq gnus-summary-buffer (current-buffer))
7536   (gnus-set-global-variables)
7537   (let ((article (gnus-summary-article-number))
7538         (article-window (get-buffer-window gnus-article-buffer t))
7539         endp)
7540     ;; If the buffer is empty, we have no article.
7541     (unless article
7542       (error "No article to select"))
7543     (gnus-configure-windows 'article)
7544     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7545         (if (and (eq gnus-summary-goto-unread 'never)
7546                  (not (gnus-summary-last-article-p article)))
7547             (gnus-summary-next-article)
7548           (gnus-summary-next-unread-article))
7549       (if (or (null gnus-current-article)
7550               (null gnus-article-current)
7551               (/= article (cdr gnus-article-current))
7552               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7553           ;; Selected subject is different from current article's.
7554           (gnus-summary-display-article article)
7555         (when article-window
7556           (gnus-eval-in-buffer-window gnus-article-buffer
7557             (setq endp (or (gnus-article-next-page lines)
7558                            (gnus-article-only-boring-p))))
7559           (when endp
7560             (cond (stop
7561                    (gnus-message 3 "End of message"))
7562                   (circular
7563                    (gnus-summary-beginning-of-article))
7564                   (lines
7565                    (gnus-message 3 "End of message"))
7566                   ((null lines)
7567                    (if (and (eq gnus-summary-goto-unread 'never)
7568                             (not (gnus-summary-last-article-p article)))
7569                        (gnus-summary-next-article)
7570                      (gnus-summary-next-unread-article))))))))
7571     (gnus-summary-recenter)
7572     (gnus-summary-position-point)))
7573
7574 (defun gnus-summary-prev-page (&optional lines move)
7575   "Show previous page of selected article.
7576 Argument LINES specifies lines to be scrolled down.
7577 If MOVE, move to the previous unread article if point is at
7578 the beginning of the buffer."
7579   (interactive "P")
7580   (let ((article (gnus-summary-article-number))
7581         (article-window (get-buffer-window gnus-article-buffer t))
7582         endp)
7583     (gnus-configure-windows 'article)
7584     (if (or (null gnus-current-article)
7585             (null gnus-article-current)
7586             (/= article (cdr gnus-article-current))
7587             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7588         ;; Selected subject is different from current article's.
7589         (gnus-summary-display-article article)
7590       (gnus-summary-recenter)
7591       (when article-window
7592         (gnus-eval-in-buffer-window gnus-article-buffer
7593           (setq endp (gnus-article-prev-page lines)))
7594         (when (and move endp)
7595           (cond (lines
7596                  (gnus-message 3 "Beginning of message"))
7597                 ((null lines)
7598                  (if (and (eq gnus-summary-goto-unread 'never)
7599                           (not (gnus-summary-first-article-p article)))
7600                      (gnus-summary-prev-article)
7601                    (gnus-summary-prev-unread-article))))))))
7602   (gnus-summary-position-point))
7603
7604 (defun gnus-summary-prev-page-or-article (&optional lines)
7605   "Show previous page of selected article.
7606 Argument LINES specifies lines to be scrolled down.
7607 If at the beginning of the article, go to the next article."
7608   (interactive "P")
7609   (gnus-summary-prev-page lines t))
7610
7611 (defun gnus-summary-scroll-up (lines)
7612   "Scroll up (or down) one line current article.
7613 Argument LINES specifies lines to be scrolled up (or down if negative)."
7614   (interactive "p")
7615   (gnus-configure-windows 'article)
7616   (gnus-summary-show-thread)
7617   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7618     (gnus-eval-in-buffer-window gnus-article-buffer
7619       (cond ((> lines 0)
7620              (when (gnus-article-next-page lines)
7621                (gnus-message 3 "End of message")))
7622             ((< lines 0)
7623              (gnus-article-prev-page (- lines))))))
7624   (gnus-summary-recenter)
7625   (gnus-summary-position-point))
7626
7627 (defun gnus-summary-scroll-down (lines)
7628   "Scroll down (or up) one line current article.
7629 Argument LINES specifies lines to be scrolled down (or up if negative)."
7630   (interactive "p")
7631   (gnus-summary-scroll-up (- lines)))
7632
7633 (defun gnus-summary-next-same-subject ()
7634   "Select next article which has the same subject as current one."
7635   (interactive)
7636   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7637
7638 (defun gnus-summary-prev-same-subject ()
7639   "Select previous article which has the same subject as current one."
7640   (interactive)
7641   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7642
7643 (defun gnus-summary-next-unread-same-subject ()
7644   "Select next unread article which has the same subject as current one."
7645   (interactive)
7646   (gnus-summary-next-article t (gnus-summary-article-subject)))
7647
7648 (defun gnus-summary-prev-unread-same-subject ()
7649   "Select previous unread article which has the same subject as current one."
7650   (interactive)
7651   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7652
7653 (defun gnus-summary-first-unread-article ()
7654   "Select the first unread article.
7655 Return nil if there are no unread articles."
7656   (interactive)
7657   (prog1
7658       (when (gnus-summary-first-subject t)
7659         (gnus-summary-show-thread)
7660         (gnus-summary-first-subject t)
7661         (gnus-summary-display-article (gnus-summary-article-number)))
7662     (gnus-summary-position-point)))
7663
7664 (defun gnus-summary-first-unread-subject ()
7665   "Place the point on the subject line of the first unread article.
7666 Return nil if there are no unread articles."
7667   (interactive)
7668   (prog1
7669       (when (gnus-summary-first-subject t)
7670         (gnus-summary-show-thread)
7671         (gnus-summary-first-subject t))
7672     (gnus-summary-position-point)))
7673
7674 (defun gnus-summary-first-unseen-subject ()
7675   "Place the point on the subject line of the first unseen article.
7676 Return nil if there are no unseen articles."
7677   (interactive)
7678   (prog1
7679       (when (gnus-summary-first-subject nil nil t)
7680         (gnus-summary-show-thread)
7681         (gnus-summary-first-subject nil nil t))
7682     (gnus-summary-position-point)))
7683
7684 (defun gnus-summary-first-unseen-or-unread-subject ()
7685   "Place the point on the subject line of the first unseen article or,
7686 if all article have been seen, on the subject line of the first unread
7687 article."
7688   (interactive)
7689   (prog1
7690       (unless (when (gnus-summary-first-subject nil nil t)
7691                 (gnus-summary-show-thread)
7692                 (gnus-summary-first-subject nil nil t))
7693         (when (gnus-summary-first-subject t)
7694           (gnus-summary-show-thread)
7695           (gnus-summary-first-subject t)))
7696     (gnus-summary-position-point)))
7697
7698 (defun gnus-summary-first-article ()
7699   "Select the first article.
7700 Return nil if there are no articles."
7701   (interactive)
7702   (prog1
7703       (when (gnus-summary-first-subject)
7704         (gnus-summary-show-thread)
7705         (gnus-summary-first-subject)
7706         (gnus-summary-display-article (gnus-summary-article-number)))
7707     (gnus-summary-position-point)))
7708
7709 (defun gnus-summary-best-unread-article (&optional arg)
7710   "Select the unread article with the highest score.
7711 If given a prefix argument, select the next unread article that has a
7712 score higher than the default score."
7713   (interactive "P")
7714   (let ((article (if arg
7715                      (gnus-summary-better-unread-subject)
7716                    (gnus-summary-best-unread-subject))))
7717     (if article
7718         (gnus-summary-goto-article article)
7719       (error "No unread articles"))))
7720
7721 (defun gnus-summary-best-unread-subject ()
7722   "Select the unread subject with the highest score."
7723   (interactive)
7724   (let ((best -1000000)
7725         (data gnus-newsgroup-data)
7726         article score)
7727     (while data
7728       (and (gnus-data-unread-p (car data))
7729            (> (setq score
7730                     (gnus-summary-article-score (gnus-data-number (car data))))
7731               best)
7732            (setq best score
7733                  article (gnus-data-number (car data))))
7734       (setq data (cdr data)))
7735     (when article
7736       (gnus-summary-goto-subject article))
7737     (gnus-summary-position-point)
7738     article))
7739
7740 (defun gnus-summary-better-unread-subject ()
7741   "Select the first unread subject that has a score over the default score."
7742   (interactive)
7743   (let ((data gnus-newsgroup-data)
7744         article score)
7745     (while (and (setq article (gnus-data-number (car data)))
7746                 (or (gnus-data-read-p (car data))
7747                     (not (> (gnus-summary-article-score article)
7748                             gnus-summary-default-score))))
7749       (setq data (cdr data)))
7750     (when article
7751       (gnus-summary-goto-subject article))
7752     (gnus-summary-position-point)
7753     article))
7754
7755 (defun gnus-summary-last-subject ()
7756   "Go to the last displayed subject line in the group."
7757   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7758     (when article
7759       (gnus-summary-goto-subject article))))
7760
7761 (defun gnus-summary-goto-article (article &optional all-headers force)
7762   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7763 If ALL-HEADERS is non-nil, no header lines are hidden.
7764 If FORCE, go to the article even if it isn't displayed.  If FORCE
7765 is a number, it is the line the article is to be displayed on."
7766   (interactive
7767    (list
7768     (completing-read
7769      "Article number or Message-ID: "
7770      (mapcar (lambda (number) (list (int-to-string number)))
7771              gnus-newsgroup-limit))
7772     current-prefix-arg
7773     t))
7774   (prog1
7775       (if (and (stringp article)
7776                (string-match "@\\|%40" article))
7777           (gnus-summary-refer-article article)
7778         (when (stringp article)
7779           (setq article (string-to-number article)))
7780         (if (gnus-summary-goto-subject article force)
7781             (gnus-summary-display-article article all-headers)
7782           (gnus-message 4 "Couldn't go to article %s" article) nil))
7783     (gnus-summary-position-point)))
7784
7785 (defun gnus-summary-goto-last-article ()
7786   "Go to the previously read article."
7787   (interactive)
7788   (prog1
7789       (when gnus-last-article
7790         (gnus-summary-goto-article gnus-last-article nil t))
7791     (gnus-summary-position-point)))
7792
7793 (defun gnus-summary-pop-article (number)
7794   "Pop one article off the history and go to the previous.
7795 NUMBER articles will be popped off."
7796   (interactive "p")
7797   (let (to)
7798     (setq gnus-newsgroup-history
7799           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7800     (if to
7801         (gnus-summary-goto-article (car to) nil t)
7802       (error "Article history empty")))
7803   (gnus-summary-position-point))
7804
7805 ;; Summary commands and functions for limiting the summary buffer.
7806
7807 (defun gnus-summary-limit-to-articles (n)
7808   "Limit the summary buffer to the next N articles.
7809 If not given a prefix, use the process marked articles instead."
7810   (interactive "P")
7811   (prog1
7812       (let ((articles (gnus-summary-work-articles n)))
7813         (setq gnus-newsgroup-processable nil)
7814         (gnus-summary-limit articles))
7815     (gnus-summary-position-point)))
7816
7817 (defun gnus-summary-pop-limit (&optional total)
7818   "Restore the previous limit.
7819 If given a prefix, remove all limits."
7820   (interactive "P")
7821   (when total
7822     (setq gnus-newsgroup-limits
7823           (list (mapcar (lambda (h) (mail-header-number h))
7824                         gnus-newsgroup-headers))))
7825   (unless gnus-newsgroup-limits
7826     (error "No limit to pop"))
7827   (prog1
7828       (gnus-summary-limit nil 'pop)
7829     (gnus-summary-position-point)))
7830
7831 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7832   "Limit the summary buffer to articles that have subjects that match a regexp.
7833 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7834   (interactive
7835    (list (read-string (if current-prefix-arg
7836                           "Exclude subject (regexp): "
7837                         "Limit to subject (regexp): "))
7838          nil current-prefix-arg))
7839   (unless header
7840     (setq header "subject"))
7841   (when (not (equal "" subject))
7842     (prog1
7843         (let ((articles (gnus-summary-find-matching
7844                          (or header "subject") subject 'all nil nil
7845                          not-matching)))
7846           (unless articles
7847             (error "Found no matches for \"%s\"" subject))
7848           (gnus-summary-limit articles))
7849       (gnus-summary-position-point))))
7850
7851 (defun gnus-summary-limit-to-author (from &optional not-matching)
7852   "Limit the summary buffer to articles that have authors that match a regexp.
7853 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7854   (interactive
7855    (list (read-string (if current-prefix-arg
7856                           "Exclude author (regexp): "
7857                         "Limit to author (regexp): "))
7858          current-prefix-arg))
7859   (gnus-summary-limit-to-subject from "from" not-matching))
7860
7861 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7862   "Limit the summary buffer to articles with the given RECIPIENT.
7863
7864 If NOT-MATCHING, exclude RECIPIENT.
7865
7866 To and Cc headers are checked.  You need to include them in
7867 `nnmail-extra-headers'."
7868   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7869   (interactive
7870    (list (read-string (format "%s recipient (regexp): "
7871                               (if current-prefix-arg "Exclude" "Limit to")))
7872          current-prefix-arg))
7873   (when (not (equal "" recipient))
7874     (prog1 (let* ((to
7875                    (if (memq 'To nnmail-extra-headers)
7876                        (gnus-summary-find-matching
7877                         (cons 'extra 'To) recipient 'all nil nil
7878                         not-matching)
7879                      (gnus-message
7880                       1 "`To' isn't present in `nnmail-extra-headers'")
7881                      (sit-for 1)
7882                      nil))
7883                   (cc
7884                    (if (memq 'Cc nnmail-extra-headers)
7885                        (gnus-summary-find-matching
7886                         (cons 'extra 'Cc) recipient 'all nil nil
7887                         not-matching)
7888                      (gnus-message
7889                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7890                      (sit-for 1)
7891                      nil))
7892                   (articles
7893                    (if not-matching
7894                        ;; We need the numbers that are in both lists:
7895                        (mapcar (lambda (a)
7896                                  (and (memq a to) a))
7897                                cc)
7898                      (nconc to cc))))
7899              (unless articles
7900                (error "Found no matches for \"%s\"" recipient))
7901              (gnus-summary-limit articles))
7902       (gnus-summary-position-point))))
7903
7904 (defun gnus-summary-limit-to-age (age &optional younger-p)
7905   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7906 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7907 articles that are younger than AGE days."
7908   (interactive
7909    (let ((younger current-prefix-arg)
7910          (days-got nil)
7911          days)
7912      (while (not days-got)
7913        (setq days (if younger
7914                       (read-string "Limit to articles younger than (in days, older when negative): ")
7915                     (read-string
7916                      "Limit to articles older than (in days, younger when negative): ")))
7917        (when (> (length days) 0)
7918          (setq days (read days)))
7919        (if (numberp days)
7920            (progn
7921              (setq days-got t)
7922              (when (< days 0)
7923                (setq younger (not younger))
7924                (setq days (* days -1))))
7925          (message "Please enter a number.")
7926          (sleep-for 1)))
7927      (list days younger)))
7928   (prog1
7929       (let ((data gnus-newsgroup-data)
7930             (cutoff (days-to-time age))
7931             articles d date is-younger)
7932         (while (setq d (pop data))
7933           (when (and (vectorp (gnus-data-header d))
7934                      (setq date (mail-header-date (gnus-data-header d))))
7935             (setq is-younger (time-less-p
7936                               (time-since (condition-case ()
7937                                               (date-to-time date)
7938                                             (error '(0 0))))
7939                               cutoff))
7940             (when (if younger-p
7941                       is-younger
7942                     (not is-younger))
7943               (push (gnus-data-number d) articles))))
7944         (gnus-summary-limit (nreverse articles)))
7945     (gnus-summary-position-point)))
7946
7947 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7948   "Limit the summary buffer to articles that match an 'extra' header."
7949   (interactive
7950    (let ((header
7951           (intern
7952            (gnus-completing-read-with-default
7953             (symbol-name (car gnus-extra-headers))
7954             (if current-prefix-arg
7955                 "Exclude extra header"
7956               "Limit extra header")
7957             (mapcar (lambda (x)
7958                       (cons (symbol-name x) x))
7959                     gnus-extra-headers)
7960             nil
7961             t))))
7962      (list header
7963            (read-string (format "%s header %s (regexp): "
7964                                 (if current-prefix-arg "Exclude" "Limit to")
7965                                 header))
7966            current-prefix-arg)))
7967   (when (not (equal "" regexp))
7968     (prog1
7969         (let ((articles (gnus-summary-find-matching
7970                          (cons 'extra header) regexp 'all nil nil
7971                          not-matching)))
7972           (unless articles
7973             (error "Found no matches for \"%s\"" regexp))
7974           (gnus-summary-limit articles))
7975       (gnus-summary-position-point))))
7976
7977 (defun gnus-summary-limit-to-display-predicate ()
7978   "Limit the summary buffer to the predicated in the `display' group parameter."
7979   (interactive)
7980   (unless gnus-newsgroup-display
7981     (error "There is no `display' group parameter"))
7982   (let (articles)
7983     (dolist (number gnus-newsgroup-articles)
7984       (when (funcall gnus-newsgroup-display)
7985         (push number articles)))
7986     (gnus-summary-limit articles))
7987   (gnus-summary-position-point))
7988
7989 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7990 (make-obsolete
7991  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7992
7993 (defun gnus-summary-limit-to-unread (&optional all)
7994   "Limit the summary buffer to articles that are not marked as read.
7995 If ALL is non-nil, limit strictly to unread articles."
7996   (interactive "P")
7997   (if all
7998       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7999     (gnus-summary-limit-to-marks
8000      ;; Concat all the marks that say that an article is read and have
8001      ;; those removed.
8002      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8003            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8004            gnus-low-score-mark gnus-expirable-mark
8005            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8006            gnus-duplicate-mark gnus-souped-mark)
8007      'reverse)))
8008
8009 (defun gnus-summary-limit-to-bodies (match &optional reverse)
8010   "Limit the summary buffer to articles that have bodies that match MATCH.
8011 If REVERSE (the prefix), limit to articles that don't match."
8012   (interactive "sMatch body (regexp): \nP")
8013   (let ((articles nil)
8014         (gnus-select-article-hook nil)  ;Disable hook.
8015         (gnus-article-prepare-hook nil)
8016         (gnus-use-article-prefetch nil)
8017         (gnus-keep-backlog nil)
8018         (gnus-break-pages nil)
8019         (gnus-summary-display-arrow nil)
8020         (gnus-updated-mode-lines nil)
8021         (gnus-auto-center-summary nil)
8022         (gnus-display-mime-function nil))
8023     (dolist (data gnus-newsgroup-data)
8024       (let (gnus-mark-article-hook)
8025         (gnus-summary-select-article t t nil (gnus-data-number data)))
8026       (save-excursion
8027         (set-buffer gnus-article-buffer)
8028         (article-goto-body)
8029         (let* ((case-fold-search t)
8030                (found (re-search-forward match nil t)))
8031           (when (or (and found
8032                          (not reverse))
8033                     (and (not found)
8034                          reverse))
8035             (push (gnus-data-number data) articles)))))
8036     (if (not articles)
8037         (message "No messages matched")
8038       (gnus-summary-limit articles)))
8039   (gnus-summary-position-point))
8040
8041 (defun gnus-summary-limit-to-replied (&optional unreplied)
8042   "Limit the summary buffer to replied articles.
8043 If UNREPLIED (the prefix), limit to unreplied articles."
8044   (interactive "P")
8045   (if unreplied
8046       (gnus-summary-limit
8047        (gnus-set-difference gnus-newsgroup-articles
8048         gnus-newsgroup-replied))
8049     (gnus-summary-limit gnus-newsgroup-replied))
8050   (gnus-summary-position-point))
8051
8052 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8053 (make-obsolete 'gnus-summary-delete-marked-with
8054                'gnus-summary-limit-exclude-marks)
8055
8056 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8057   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8058 If REVERSE, limit the summary buffer to articles that are marked
8059 with MARKS.  MARKS can either be a string of marks or a list of marks.
8060 Returns how many articles were removed."
8061   (interactive "sMarks: ")
8062   (gnus-summary-limit-to-marks marks t))
8063
8064 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8065   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8066 If REVERSE (the prefix), limit the summary buffer to articles that are
8067 not marked with MARKS.  MARKS can either be a string of marks or a
8068 list of marks.
8069 Returns how many articles were removed."
8070   (interactive "sMarks: \nP")
8071   (prog1
8072       (let ((data gnus-newsgroup-data)
8073             (marks (if (listp marks) marks
8074                      (append marks nil))) ; Transform to list.
8075             articles)
8076         (while data
8077           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8078                   (memq (gnus-data-mark (car data)) marks))
8079             (push (gnus-data-number (car data)) articles))
8080           (setq data (cdr data)))
8081         (gnus-summary-limit articles))
8082     (gnus-summary-position-point)))
8083
8084 (defun gnus-summary-limit-to-score (score)
8085   "Limit to articles with score at or above SCORE."
8086   (interactive "NLimit to articles with score of at least: ")
8087   (let ((data gnus-newsgroup-data)
8088         articles)
8089     (while data
8090       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8091                 score)
8092         (push (gnus-data-number (car data)) articles))
8093       (setq data (cdr data)))
8094     (prog1
8095         (gnus-summary-limit articles)
8096       (gnus-summary-position-point))))
8097
8098 (defun gnus-summary-limit-to-unseen ()
8099   "Limit to unseen articles."
8100   (interactive)
8101   (prog1
8102       (gnus-summary-limit gnus-newsgroup-unseen)
8103     (gnus-summary-position-point)))
8104
8105 (defun gnus-summary-limit-include-thread (id)
8106   "Display all the hidden articles that is in the thread with ID in it.
8107 When called interactively, ID is the Message-ID of the current
8108 article."
8109   (interactive (list (mail-header-id (gnus-summary-article-header))))
8110   (let ((articles (gnus-articles-in-thread
8111                    (gnus-id-to-thread (gnus-root-id id)))))
8112     (prog1
8113         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8114       (gnus-summary-limit-include-matching-articles
8115        "subject"
8116        (regexp-quote (gnus-simplify-subject-re
8117                       (mail-header-subject (gnus-id-to-header id)))))
8118       (gnus-summary-position-point))))
8119
8120 (defun gnus-summary-limit-include-matching-articles (header regexp)
8121   "Display all the hidden articles that have HEADERs that match REGEXP."
8122   (interactive (list (read-string "Match on header: ")
8123                      (read-string "Regexp: ")))
8124   (let ((articles (gnus-find-matching-articles header regexp)))
8125     (prog1
8126         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8127       (gnus-summary-position-point))))
8128
8129 (defun gnus-summary-insert-dormant-articles ()
8130   "Insert all the dormant articles for this group into the current buffer."
8131   (interactive)
8132   (let ((gnus-verbose (max 6 gnus-verbose)))
8133     (if (not gnus-newsgroup-dormant)
8134         (gnus-message 3 "No cached articles for this group")
8135       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8136
8137 (defun gnus-summary-limit-include-dormant ()
8138   "Display all the hidden articles that are marked as dormant.
8139 Note that this command only works on a subset of the articles currently
8140 fetched for this group."
8141   (interactive)
8142   (unless gnus-newsgroup-dormant
8143     (error "There are no dormant articles in this group"))
8144   (prog1
8145       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8146     (gnus-summary-position-point)))
8147
8148 (defun gnus-summary-limit-exclude-dormant ()
8149   "Hide all dormant articles."
8150   (interactive)
8151   (prog1
8152       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8153     (gnus-summary-position-point)))
8154
8155 (defun gnus-summary-limit-exclude-childless-dormant ()
8156   "Hide all dormant articles that have no children."
8157   (interactive)
8158   (let ((data (gnus-data-list t))
8159         articles d children)
8160     ;; Find all articles that are either not dormant or have
8161     ;; children.
8162     (while (setq d (pop data))
8163       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8164                 (and (setq children
8165                            (gnus-article-children (gnus-data-number d)))
8166                      (let (found)
8167                        (while children
8168                          (when (memq (car children) articles)
8169                            (setq children nil
8170                                  found t))
8171                          (pop children))
8172                        found)))
8173         (push (gnus-data-number d) articles)))
8174     ;; Do the limiting.
8175     (prog1
8176         (gnus-summary-limit articles)
8177       (gnus-summary-position-point))))
8178
8179 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8180   "Mark all unread excluded articles as read.
8181 If ALL, mark even excluded ticked and dormants as read."
8182   (interactive "P")
8183   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8184   (let ((articles (gnus-sorted-ndifference
8185                    (sort
8186                     (mapcar (lambda (h) (mail-header-number h))
8187                             gnus-newsgroup-headers)
8188                     '<)
8189                    gnus-newsgroup-limit))
8190         article)
8191     (setq gnus-newsgroup-unreads
8192           (gnus-sorted-intersection gnus-newsgroup-unreads
8193                                     gnus-newsgroup-limit))
8194     (if all
8195         (setq gnus-newsgroup-dormant nil
8196               gnus-newsgroup-marked nil
8197               gnus-newsgroup-reads
8198               (nconc
8199                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8200                gnus-newsgroup-reads))
8201       (while (setq article (pop articles))
8202         (unless (or (memq article gnus-newsgroup-dormant)
8203                     (memq article gnus-newsgroup-marked))
8204           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8205
8206 (defun gnus-summary-limit (articles &optional pop)
8207   (if pop
8208       ;; We pop the previous limit off the stack and use that.
8209       (setq articles (car gnus-newsgroup-limits)
8210             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8211     ;; We use the new limit, so we push the old limit on the stack.
8212     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8213   ;; Set the limit.
8214   (setq gnus-newsgroup-limit articles)
8215   (let ((total (length gnus-newsgroup-data))
8216         (data (gnus-data-find-list (gnus-summary-article-number)))
8217         (gnus-summary-mark-below nil)   ; Inhibit this.
8218         found)
8219     ;; This will do all the work of generating the new summary buffer
8220     ;; according to the new limit.
8221     (gnus-summary-prepare)
8222     ;; Hide any threads, possibly.
8223     (gnus-summary-maybe-hide-threads)
8224     ;; Try to return to the article you were at, or one in the
8225     ;; neighborhood.
8226     (when data
8227       ;; We try to find some article after the current one.
8228       (while data
8229         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8230           (setq data nil
8231                 found t))
8232         (setq data (cdr data))))
8233     (unless found
8234       ;; If there is no data, that means that we were after the last
8235       ;; article.  The same goes when we can't find any articles
8236       ;; after the current one.
8237       (goto-char (point-max))
8238       (gnus-summary-find-prev))
8239     (gnus-set-mode-line 'summary)
8240     ;; We return how many articles were removed from the summary
8241     ;; buffer as a result of the new limit.
8242     (- total (length gnus-newsgroup-data))))
8243
8244 (defsubst gnus-invisible-cut-children (threads)
8245   (let ((num 0))
8246     (while threads
8247       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8248         (incf num))
8249       (pop threads))
8250     (< num 2)))
8251
8252 (defsubst gnus-cut-thread (thread)
8253   "Go forwards in the thread until we find an article that we want to display."
8254   (when (or (eq gnus-fetch-old-headers 'some)
8255             (eq gnus-fetch-old-headers 'invisible)
8256             (numberp gnus-fetch-old-headers)
8257             (eq gnus-build-sparse-threads 'some)
8258             (eq gnus-build-sparse-threads 'more))
8259     ;; Deal with old-fetched headers and sparse threads.
8260     (while (and
8261             thread
8262             (or
8263              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8264              (gnus-summary-article-ancient-p
8265               (mail-header-number (car thread))))
8266             (if (or (<= (length (cdr thread)) 1)
8267                     (eq gnus-fetch-old-headers 'invisible))
8268                 (setq gnus-newsgroup-limit
8269                       (delq (mail-header-number (car thread))
8270                             gnus-newsgroup-limit)
8271                       thread (cadr thread))
8272               (when (gnus-invisible-cut-children (cdr thread))
8273                 (let ((th (cdr thread)))
8274                   (while th
8275                     (if (memq (mail-header-number (caar th))
8276                               gnus-newsgroup-limit)
8277                         (setq thread (car th)
8278                               th nil)
8279                       (setq th (cdr th))))))))))
8280   thread)
8281
8282 (defun gnus-cut-threads (threads)
8283   "Cut off all uninteresting articles from the beginning of THREADS."
8284   (when (or (eq gnus-fetch-old-headers 'some)
8285             (eq gnus-fetch-old-headers 'invisible)
8286             (numberp gnus-fetch-old-headers)
8287             (eq gnus-build-sparse-threads 'some)
8288             (eq gnus-build-sparse-threads 'more))
8289     (let ((th threads))
8290       (while th
8291         (setcar th (gnus-cut-thread (car th)))
8292         (setq th (cdr th)))))
8293   ;; Remove nixed out threads.
8294   (delq nil threads))
8295
8296 (defun gnus-summary-initial-limit (&optional show-if-empty)
8297   "Figure out what the initial limit is supposed to be on group entry.
8298 This entails weeding out unwanted dormants, low-scored articles,
8299 fetch-old-headers verbiage, and so on."
8300   ;; Most groups have nothing to remove.
8301   (if (or gnus-inhibit-limiting
8302           (and (null gnus-newsgroup-dormant)
8303                (eq gnus-newsgroup-display 'gnus-not-ignore)
8304                (not (eq gnus-fetch-old-headers 'some))
8305                (not (numberp gnus-fetch-old-headers))
8306                (not (eq gnus-fetch-old-headers 'invisible))
8307                (null gnus-summary-expunge-below)
8308                (not (eq gnus-build-sparse-threads 'some))
8309                (not (eq gnus-build-sparse-threads 'more))
8310                (null gnus-thread-expunge-below)
8311                (not gnus-use-nocem)))
8312       ()                                ; Do nothing.
8313     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8314     (setq gnus-newsgroup-limit nil)
8315     (mapatoms
8316      (lambda (node)
8317        (unless (car (symbol-value node))
8318          ;; These threads have no parents -- they are roots.
8319          (let ((nodes (cdr (symbol-value node)))
8320                thread)
8321            (while nodes
8322              (if (and gnus-thread-expunge-below
8323                       (< (gnus-thread-total-score (car nodes))
8324                          gnus-thread-expunge-below))
8325                  (gnus-expunge-thread (pop nodes))
8326                (setq thread (pop nodes))
8327                (gnus-summary-limit-children thread))))))
8328      gnus-newsgroup-dependencies)
8329     ;; If this limitation resulted in an empty group, we might
8330     ;; pop the previous limit and use it instead.
8331     (when (and (not gnus-newsgroup-limit)
8332                show-if-empty)
8333       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8334     gnus-newsgroup-limit))
8335
8336 (defun gnus-summary-limit-children (thread)
8337   "Return 1 if this subthread is visible and 0 if it is not."
8338   ;; First we get the number of visible children to this thread.  This
8339   ;; is done by recursing down the thread using this function, so this
8340   ;; will really go down to a leaf article first, before slowly
8341   ;; working its way up towards the root.
8342   (when thread
8343     (let* ((max-lisp-eval-depth 5000)
8344            (children
8345            (if (cdr thread)
8346                (apply '+ (mapcar 'gnus-summary-limit-children
8347                                  (cdr thread)))
8348              0))
8349           (number (mail-header-number (car thread)))
8350           score)
8351       (if (and
8352            (not (memq number gnus-newsgroup-marked))
8353            (or
8354             ;; If this article is dormant and has absolutely no visible
8355             ;; children, then this article isn't visible.
8356             (and (memq number gnus-newsgroup-dormant)
8357                  (zerop children))
8358             ;; If this is "fetch-old-headered" and there is no
8359             ;; visible children, then we don't want this article.
8360             (and (or (eq gnus-fetch-old-headers 'some)
8361                      (numberp gnus-fetch-old-headers))
8362                  (gnus-summary-article-ancient-p number)
8363                  (zerop children))
8364             ;; If this is "fetch-old-headered" and `invisible', then
8365             ;; we don't want this article.
8366             (and (eq gnus-fetch-old-headers 'invisible)
8367                  (gnus-summary-article-ancient-p number))
8368             ;; If this is a sparsely inserted article with no children,
8369             ;; we don't want it.
8370             (and (eq gnus-build-sparse-threads 'some)
8371                  (gnus-summary-article-sparse-p number)
8372                  (zerop children))
8373             ;; If we use expunging, and this article is really
8374             ;; low-scored, then we don't want this article.
8375             (when (and gnus-summary-expunge-below
8376                        (< (setq score
8377                                 (or (cdr (assq number gnus-newsgroup-scored))
8378                                     gnus-summary-default-score))
8379                           gnus-summary-expunge-below))
8380               ;; We increase the expunge-tally here, but that has
8381               ;; nothing to do with the limits, really.
8382               (incf gnus-newsgroup-expunged-tally)
8383               ;; We also mark as read here, if that's wanted.
8384               (when (and gnus-summary-mark-below
8385                          (< score gnus-summary-mark-below))
8386                 (setq gnus-newsgroup-unreads
8387                       (delq number gnus-newsgroup-unreads))
8388                 (if gnus-newsgroup-auto-expire
8389                     (push number gnus-newsgroup-expirable)
8390                   (push (cons number gnus-low-score-mark)
8391                         gnus-newsgroup-reads)))
8392               t)
8393             ;; Do the `display' group parameter.
8394             (and gnus-newsgroup-display
8395                  (not (funcall gnus-newsgroup-display)))
8396             ;; Check NoCeM things.
8397             (when (and gnus-use-nocem
8398                        (gnus-nocem-unwanted-article-p
8399                         (mail-header-id (car thread))))
8400               (setq gnus-newsgroup-unreads
8401                     (delq number gnus-newsgroup-unreads))
8402               t)))
8403           ;; Nope, invisible article.
8404           0
8405         ;; Ok, this article is to be visible, so we add it to the limit
8406         ;; and return 1.
8407         (push number gnus-newsgroup-limit)
8408         1))))
8409
8410 (defun gnus-expunge-thread (thread)
8411   "Mark all articles in THREAD as read."
8412   (let* ((number (mail-header-number (car thread))))
8413     (incf gnus-newsgroup-expunged-tally)
8414     ;; We also mark as read here, if that's wanted.
8415     (setq gnus-newsgroup-unreads
8416           (delq number gnus-newsgroup-unreads))
8417     (if gnus-newsgroup-auto-expire
8418         (push number gnus-newsgroup-expirable)
8419       (push (cons number gnus-low-score-mark)
8420             gnus-newsgroup-reads)))
8421   ;; Go recursively through all subthreads.
8422   (mapcar 'gnus-expunge-thread (cdr thread)))
8423
8424 ;; Summary article oriented commands
8425
8426 (defun gnus-summary-refer-parent-article (n)
8427   "Refer parent article N times.
8428 If N is negative, go to ancestor -N instead.
8429 The difference between N and the number of articles fetched is returned."
8430   (interactive "p")
8431   (let ((skip 1)
8432         error header ref)
8433     (when (not (natnump n))
8434       (setq skip (abs n)
8435             n 1))
8436     (while (and (> n 0)
8437                 (not error))
8438       (setq header (gnus-summary-article-header))
8439       (if (and (eq (mail-header-number header)
8440                    (cdr gnus-article-current))
8441                (equal gnus-newsgroup-name
8442                       (car gnus-article-current)))
8443           ;; If we try to find the parent of the currently
8444           ;; displayed article, then we take a look at the actual
8445           ;; References header, since this is slightly more
8446           ;; reliable than the References field we got from the
8447           ;; server.
8448           (save-excursion
8449             (set-buffer gnus-original-article-buffer)
8450             (nnheader-narrow-to-headers)
8451             (unless (setq ref (message-fetch-field "references"))
8452               (when (setq ref (message-fetch-field "in-reply-to"))
8453                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8454             (widen))
8455         (setq ref
8456               ;; It's not the current article, so we take a bet on
8457               ;; the value we got from the server.
8458               (mail-header-references header)))
8459       (if (and ref
8460                (not (equal ref "")))
8461           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8462             (gnus-message 1 "Couldn't find parent"))
8463         (gnus-message 1 "No references in article %d"
8464                       (gnus-summary-article-number))
8465         (setq error t))
8466       (decf n))
8467     (gnus-summary-position-point)
8468     n))
8469
8470 (defun gnus-summary-refer-references ()
8471   "Fetch all articles mentioned in the References header.
8472 Return the number of articles fetched."
8473   (interactive)
8474   (let ((ref (mail-header-references (gnus-summary-article-header)))
8475         (current (gnus-summary-article-number))
8476         (n 0))
8477     (if (or (not ref)
8478             (equal ref ""))
8479         (error "No References in the current article")
8480       ;; For each Message-ID in the References header...
8481       (while (string-match "<[^>]*>" ref)
8482         (incf n)
8483         ;; ... fetch that article.
8484         (gnus-summary-refer-article
8485          (prog1 (match-string 0 ref)
8486            (setq ref (substring ref (match-end 0))))))
8487       (gnus-summary-goto-subject current)
8488       (gnus-summary-position-point)
8489       n)))
8490
8491 (defun gnus-summary-refer-thread (&optional limit)
8492   "Fetch all articles in the current thread.
8493 If LIMIT (the numerical prefix), fetch that many old headers instead
8494 of what's specified by the `gnus-refer-thread-limit' variable."
8495   (interactive "P")
8496   (let ((id (mail-header-id (gnus-summary-article-header)))
8497         (limit (if limit (prefix-numeric-value limit)
8498                  gnus-refer-thread-limit)))
8499     (unless (eq gnus-fetch-old-headers 'invisible)
8500       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8501       ;; Retrieve the headers and read them in.
8502       (if (eq (if (numberp limit)
8503                   (gnus-retrieve-headers
8504                    (list (min
8505                           (+ (mail-header-number
8506                               (gnus-summary-article-header))
8507                              limit)
8508                           gnus-newsgroup-end))
8509                    gnus-newsgroup-name (* limit 2))
8510                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8511                 ;; headers.
8512                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8513                                        gnus-newsgroup-name limit))
8514               'nov)
8515           (gnus-build-all-threads)
8516         (error "Can't fetch thread from back ends that don't support NOV"))
8517       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8518     (gnus-summary-limit-include-thread id)))
8519
8520 (defun gnus-summary-refer-article (message-id)
8521   "Fetch an article specified by MESSAGE-ID."
8522   (interactive "sMessage-ID: ")
8523   (when (and (stringp message-id)
8524              (not (zerop (length message-id))))
8525     (setq message-id (gnus-replace-in-string message-id " " ""))
8526     ;; Construct the correct Message-ID if necessary.
8527     ;; Suggested by tale@pawl.rpi.edu.
8528     (unless (string-match "^<" message-id)
8529       (setq message-id (concat "<" message-id)))
8530     (unless (string-match ">$" message-id)
8531       (setq message-id (concat message-id ">")))
8532     ;; People often post MIDs from URLs, so unhex it:
8533     (unless (string-match "@" message-id)
8534       (setq message-id (gnus-url-unhex-string message-id)))
8535     (let* ((header (gnus-id-to-header message-id))
8536            (sparse (and header
8537                         (gnus-summary-article-sparse-p
8538                          (mail-header-number header))
8539                         (memq (mail-header-number header)
8540                               gnus-newsgroup-limit)))
8541            number)
8542       (cond
8543        ;; If the article is present in the buffer we just go to it.
8544        ((and header
8545              (or (not (gnus-summary-article-sparse-p
8546                        (mail-header-number header)))
8547                  sparse))
8548         (prog1
8549             (gnus-summary-goto-article
8550              (mail-header-number header) nil t)
8551           (when sparse
8552             (gnus-summary-update-article (mail-header-number header)))))
8553        (t
8554         ;; We fetch the article.
8555         (catch 'found
8556           (dolist (gnus-override-method (gnus-refer-article-methods))
8557             (when (and (gnus-check-server gnus-override-method)
8558                        ;; Fetch the header,
8559                        (setq number (gnus-summary-insert-subject message-id)))
8560               ;; and display the article.
8561               (gnus-summary-select-article nil nil nil number)
8562               (throw 'found t)))
8563           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8564
8565 (defun gnus-refer-article-methods ()
8566   "Return a list of referable methods."
8567   (cond
8568    ;; No method, so we default to current and native.
8569    ((null gnus-refer-article-method)
8570     (list gnus-current-select-method gnus-select-method))
8571    ;; Current.
8572    ((eq 'current gnus-refer-article-method)
8573     (list gnus-current-select-method))
8574    ;; List of select methods.
8575    ((not (and (symbolp (car gnus-refer-article-method))
8576               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8577     (let (out)
8578       (dolist (method gnus-refer-article-method)
8579         (push (if (eq 'current method)
8580                   gnus-current-select-method
8581                 method)
8582               out))
8583       (nreverse out)))
8584    ;; One single select method.
8585    (t
8586     (list gnus-refer-article-method))))
8587
8588 (defun gnus-summary-edit-parameters ()
8589   "Edit the group parameters of the current group."
8590   (interactive)
8591   (gnus-group-edit-group gnus-newsgroup-name 'params))
8592
8593 (defun gnus-summary-customize-parameters ()
8594   "Customize the group parameters of the current group."
8595   (interactive)
8596   (gnus-group-customize gnus-newsgroup-name))
8597
8598 (defun gnus-summary-enter-digest-group (&optional force)
8599   "Enter an nndoc group based on the current article.
8600 If FORCE, force a digest interpretation.  If not, try
8601 to guess what the document format is."
8602   (interactive "P")
8603   (let ((conf gnus-current-window-configuration))
8604     (save-window-excursion
8605       (save-excursion
8606         (let (gnus-article-prepare-hook
8607               gnus-display-mime-function
8608               gnus-break-pages)
8609           (gnus-summary-select-article))))
8610     (setq gnus-current-window-configuration conf)
8611     (let* ((name (format "%s-%d"
8612                          (gnus-group-prefixed-name
8613                           gnus-newsgroup-name (list 'nndoc ""))
8614                          (with-current-buffer gnus-summary-buffer
8615                            gnus-current-article)))
8616            (ogroup gnus-newsgroup-name)
8617            (params (append (gnus-info-params (gnus-get-info ogroup))
8618                            (list (cons 'to-group ogroup))
8619                            (list (cons 'parent-group ogroup))
8620                            (list (cons 'save-article-group ogroup))))
8621            (case-fold-search t)
8622            (buf (current-buffer))
8623            dig to-address)
8624       (save-excursion
8625         (set-buffer gnus-original-article-buffer)
8626         ;; Have the digest group inherit the main mail address of
8627         ;; the parent article.
8628         (when (setq to-address (or (gnus-fetch-field "reply-to")
8629                                    (gnus-fetch-field "from")))
8630           (setq params (append
8631                         (list (cons 'to-address
8632                                     (funcall gnus-decode-encoded-word-function
8633                                              to-address))))))
8634         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8635         (insert-buffer-substring gnus-original-article-buffer)
8636         ;; Remove lines that may lead nndoc to misinterpret the
8637         ;; document type.
8638         (narrow-to-region
8639          (goto-char (point-min))
8640          (or (search-forward "\n\n" nil t) (point)))
8641         (goto-char (point-min))
8642         (delete-matching-lines "^Path:\\|^From ")
8643         (widen))
8644       (unwind-protect
8645           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8646                     (gnus-newsgroup-ephemeral-ignored-charsets
8647                      gnus-newsgroup-ignored-charsets))
8648                 (gnus-group-read-ephemeral-group
8649                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8650                               (nndoc-article-type
8651                                ,(if force 'mbox 'guess)))
8652                  t nil nil nil
8653                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8654                                                         "ADAPT")))))
8655               ;; Make all postings to this group go to the parent group.
8656               (nconc (gnus-info-params (gnus-get-info name))
8657                      params)
8658             ;; Couldn't select this doc group.
8659             (switch-to-buffer buf)
8660             (gnus-set-global-variables)
8661             (gnus-configure-windows 'summary)
8662             (gnus-message 3 "Article couldn't be entered?"))
8663         (kill-buffer dig)))))
8664
8665 (defun gnus-summary-read-document (n)
8666   "Open a new group based on the current article(s).
8667 This will allow you to read digests and other similar
8668 documents as newsgroups.
8669 Obeys the standard process/prefix convention."
8670   (interactive "P")
8671   (let* ((ogroup gnus-newsgroup-name)
8672          (params (append (gnus-info-params (gnus-get-info ogroup))
8673                          (list (cons 'to-group ogroup))))
8674          group egroup groups vgroup)
8675     (dolist (article (gnus-summary-work-articles n))
8676       (setq group (format "%s-%d" gnus-newsgroup-name article))
8677       (gnus-summary-remove-process-mark article)
8678       (when (gnus-summary-display-article article)
8679         (save-excursion
8680           (with-temp-buffer
8681             (insert-buffer-substring gnus-original-article-buffer)
8682             ;; Remove some headers that may lead nndoc to make
8683             ;; the wrong guess.
8684             (message-narrow-to-head)
8685             (goto-char (point-min))
8686             (delete-matching-lines "^Path:\\|^From ")
8687             (widen)
8688             (if (setq egroup
8689                       (gnus-group-read-ephemeral-group
8690                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8691                                      (nndoc-article-type guess))
8692                        t nil t))
8693                 (progn
8694             ;; Make all postings to this group go to the parent group.
8695                   (nconc (gnus-info-params (gnus-get-info egroup))
8696                          params)
8697                   (push egroup groups))
8698               ;; Couldn't select this doc group.
8699               (gnus-error 3 "Article couldn't be entered"))))))
8700     ;; Now we have selected all the documents.
8701     (cond
8702      ((not groups)
8703       (error "None of the articles could be interpreted as documents"))
8704      ((gnus-group-read-ephemeral-group
8705        (setq vgroup (format
8706                      "nnvirtual:%s-%s" gnus-newsgroup-name
8707                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8708        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8709        t
8710        (cons (current-buffer) 'summary)))
8711      (t
8712       (error "Couldn't select virtual nndoc group")))))
8713
8714 (defun gnus-summary-isearch-article (&optional regexp-p)
8715   "Do incremental search forward on the current article.
8716 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8717   (interactive "P")
8718   (gnus-summary-select-article)
8719   (gnus-configure-windows 'article)
8720   (gnus-eval-in-buffer-window gnus-article-buffer
8721     (save-restriction
8722       (widen)
8723       (isearch-forward regexp-p))))
8724
8725 (defun gnus-summary-repeat-search-article-forward ()
8726   "Repeat the previous search forwards."
8727   (interactive)
8728   (unless gnus-last-search-regexp
8729     (error "No previous search"))
8730   (gnus-summary-search-article-forward gnus-last-search-regexp))
8731
8732 (defun gnus-summary-repeat-search-article-backward ()
8733   "Repeat the previous search backwards."
8734   (interactive)
8735   (unless gnus-last-search-regexp
8736     (error "No previous search"))
8737   (gnus-summary-search-article-forward gnus-last-search-regexp t))
8738
8739 (defun gnus-summary-search-article-forward (regexp &optional backward)
8740   "Search for an article containing REGEXP forward.
8741 If BACKWARD, search backward instead."
8742   (interactive
8743    (list (read-string
8744           (format "Search article %s (regexp%s): "
8745                   (if current-prefix-arg "backward" "forward")
8746                   (if gnus-last-search-regexp
8747                       (concat ", default " gnus-last-search-regexp)
8748                     "")))
8749          current-prefix-arg))
8750   (if (string-equal regexp "")
8751       (setq regexp (or gnus-last-search-regexp ""))
8752     (setq gnus-last-search-regexp regexp)
8753     (setq gnus-article-before-search gnus-current-article))
8754   ;; Intentionally set gnus-last-article.
8755   (setq gnus-last-article gnus-article-before-search)
8756   (let ((gnus-last-article gnus-last-article))
8757     (if (gnus-summary-search-article regexp backward)
8758         (gnus-summary-show-thread)
8759       (signal 'search-failed (list regexp)))))
8760
8761 (defun gnus-summary-search-article-backward (regexp)
8762   "Search for an article containing REGEXP backward."
8763   (interactive
8764    (list (read-string
8765           (format "Search article backward (regexp%s): "
8766                   (if gnus-last-search-regexp
8767                       (concat ", default " gnus-last-search-regexp)
8768                     "")))))
8769   (gnus-summary-search-article-forward regexp 'backward))
8770
8771 (defun gnus-summary-search-article (regexp &optional backward)
8772   "Search for an article containing REGEXP.
8773 Optional argument BACKWARD means do search for backward.
8774 `gnus-select-article-hook' is not called during the search."
8775   ;; We have to require this here to make sure that the following
8776   ;; dynamic binding isn't shadowed by autoloading.
8777   (require 'gnus-async)
8778   (require 'gnus-art)
8779   (let ((gnus-select-article-hook nil)  ;Disable hook.
8780         (gnus-article-prepare-hook nil)
8781         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8782         (gnus-use-article-prefetch nil)
8783         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8784         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8785         (gnus-visual nil)
8786         (gnus-keep-backlog nil)
8787         (gnus-break-pages nil)
8788         (gnus-summary-display-arrow nil)
8789         (gnus-updated-mode-lines nil)
8790         (gnus-auto-center-summary nil)
8791         (sum (current-buffer))
8792         (gnus-display-mime-function nil)
8793         (found nil)
8794         point)
8795     (gnus-save-hidden-threads
8796       (gnus-summary-select-article)
8797       (set-buffer gnus-article-buffer)
8798       (goto-char (window-point (get-buffer-window (current-buffer))))
8799       (when backward
8800         (forward-line -1))
8801       (while (not found)
8802         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8803         (if (if backward
8804                 (re-search-backward regexp nil t)
8805               (re-search-forward regexp nil t))
8806             ;; We found the regexp.
8807             (progn
8808               (setq found 'found)
8809               (beginning-of-line)
8810               (set-window-start
8811                (get-buffer-window (current-buffer))
8812                (point))
8813               (forward-line 1)
8814               (set-window-point
8815                (get-buffer-window (current-buffer))
8816                (point))
8817               (set-buffer sum)
8818               (setq point (point)))
8819           ;; We didn't find it, so we go to the next article.
8820           (set-buffer sum)
8821           (setq found 'not)
8822           (while (eq found 'not)
8823             (if (not (if backward (gnus-summary-find-prev)
8824                        (gnus-summary-find-next)))
8825                 ;; No more articles.
8826                 (setq found t)
8827               ;; Select the next article and adjust point.
8828               (unless (gnus-summary-article-sparse-p
8829                        (gnus-summary-article-number))
8830                 (setq found nil)
8831                 (gnus-summary-select-article)
8832                 (set-buffer gnus-article-buffer)
8833                 (widen)
8834                 (goto-char (if backward (point-max) (point-min))))))))
8835       (gnus-message 7 ""))
8836     ;; Return whether we found the regexp.
8837     (when (eq found 'found)
8838       (goto-char point)
8839       (gnus-summary-show-thread)
8840       (gnus-summary-goto-subject gnus-current-article)
8841       (gnus-summary-position-point)
8842       t)))
8843
8844 (defun gnus-find-matching-articles (header regexp)
8845   "Return a list of all articles that match REGEXP on HEADER.
8846 This search includes all articles in the current group that Gnus has
8847 fetched headers for, whether they are displayed or not."
8848   (let ((articles nil)
8849         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8850         (case-fold-search t))
8851     (dolist (header gnus-newsgroup-headers)
8852       (when (string-match regexp (funcall func header))
8853         (push (mail-header-number header) articles)))
8854     (nreverse articles)))
8855
8856 (defun gnus-summary-find-matching (header regexp &optional backward unread
8857                                           not-case-fold not-matching)
8858   "Return a list of all articles that match REGEXP on HEADER.
8859 The search stars on the current article and goes forwards unless
8860 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8861 If UNREAD is non-nil, only unread articles will
8862 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8863 in the comparisons. If NOT-MATCHING, return a list of all articles that
8864 not match REGEXP on HEADER."
8865   (let ((case-fold-search (not not-case-fold))
8866         articles d func)
8867     (if (consp header)
8868         (if (eq (car header) 'extra)
8869             (setq func
8870                   `(lambda (h)
8871                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8872                          "")))
8873           (error "%s is an invalid header" header))
8874       (unless (fboundp (intern (concat "mail-header-" header)))
8875         (error "%s is not a valid header" header))
8876       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8877     (dolist (d (if (eq backward 'all)
8878                    gnus-newsgroup-data
8879                  (gnus-data-find-list
8880                   (gnus-summary-article-number)
8881                   (gnus-data-list backward))))
8882       (when (and (or (not unread)       ; We want all articles...
8883                      (gnus-data-unread-p d)) ; Or just unreads.
8884                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8885                  (if not-matching
8886                      (not (string-match
8887                            regexp
8888                            (funcall func (gnus-data-header d))))
8889                    (string-match regexp
8890                                  (funcall func (gnus-data-header d)))))
8891         (push (gnus-data-number d) articles))) ; Success!
8892     (nreverse articles)))
8893
8894 (defun gnus-summary-execute-command (header regexp command &optional backward)
8895   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8896 If HEADER is an empty string (or nil), the match is done on the entire
8897 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8898   (interactive
8899    (list (let ((completion-ignore-case t))
8900            (completing-read
8901             "Header name: "
8902             (mapcar (lambda (header) (list (format "%s" header)))
8903                     (append
8904                      '("Number" "Subject" "From" "Lines" "Date"
8905                        "Message-ID" "Xref" "References" "Body")
8906                      gnus-extra-headers))
8907             nil 'require-match))
8908          (read-string "Regexp: ")
8909          (read-key-sequence "Command: ")
8910          current-prefix-arg))
8911   (when (equal header "Body")
8912     (setq header ""))
8913   ;; Hidden thread subtrees must be searched as well.
8914   (gnus-summary-show-all-threads)
8915   ;; We don't want to change current point nor window configuration.
8916   (save-excursion
8917     (save-window-excursion
8918       (let (gnus-visual
8919             gnus-treat-strip-trailing-blank-lines
8920             gnus-treat-strip-leading-blank-lines
8921             gnus-treat-strip-multiple-blank-lines
8922             gnus-treat-hide-boring-headers
8923             gnus-treat-fold-newsgroups
8924             gnus-article-prepare-hook)
8925         (gnus-message 6 "Executing %s..." (key-description command))
8926         ;; We'd like to execute COMMAND interactively so as to give arguments.
8927         (gnus-execute header regexp
8928                       `(call-interactively ',(key-binding command))
8929                       backward)
8930         (gnus-message 6 "Executing %s...done" (key-description command))))))
8931
8932 (defun gnus-summary-beginning-of-article ()
8933   "Scroll the article back to the beginning."
8934   (interactive)
8935   (gnus-summary-select-article)
8936   (gnus-configure-windows 'article)
8937   (gnus-eval-in-buffer-window gnus-article-buffer
8938     (widen)
8939     (goto-char (point-min))
8940     (when gnus-break-pages
8941       (gnus-narrow-to-page))))
8942
8943 (defun gnus-summary-end-of-article ()
8944   "Scroll to the end of the article."
8945   (interactive)
8946   (gnus-summary-select-article)
8947   (gnus-configure-windows 'article)
8948   (gnus-eval-in-buffer-window gnus-article-buffer
8949     (widen)
8950     (goto-char (point-max))
8951     (recenter -3)
8952     (when gnus-break-pages
8953       (when (re-search-backward page-delimiter nil t)
8954         (narrow-to-region (match-end 0) (point-max)))
8955       (gnus-narrow-to-page))))
8956
8957 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8958   "Truncate to LEN and quote all \"(\"'s in STRING."
8959   (gnus-replace-in-string (if (and len (> (length string) len))
8960                               (substring string 0 len)
8961                             string)
8962                           "[()]" "\\\\\\&"))
8963
8964 (defun gnus-summary-print-article (&optional filename n)
8965   "Generate and print a PostScript image of the process-marked (mail) articles.
8966
8967 If used interactively, print the current article if none are
8968 process-marked.  With prefix arg, prompt the user for the name of the
8969 file to save in.
8970
8971 When used from Lisp, accept two optional args FILENAME and N.  N means
8972 to print the next N articles.  If N is negative, print the N previous
8973 articles.  If N is nil and articles have been marked with the process
8974 mark, print these instead.
8975
8976 If the optional first argument FILENAME is nil, send the image to the
8977 printer.  If FILENAME is a string, save the PostScript image in a file with
8978 that name.  If FILENAME is a number, prompt the user for the name of the file
8979 to save in."
8980   (interactive (list (ps-print-preprint current-prefix-arg)))
8981   (dolist (article (gnus-summary-work-articles n))
8982     (gnus-summary-select-article nil nil 'pseudo article)
8983     (gnus-eval-in-buffer-window gnus-article-buffer
8984       (gnus-print-buffer))
8985     (gnus-summary-remove-process-mark article))
8986   (ps-despool filename))
8987
8988 (defun gnus-print-buffer ()
8989   (let ((buffer (generate-new-buffer " *print*")))
8990     (unwind-protect
8991         (progn
8992           (copy-to-buffer buffer (point-min) (point-max))
8993           (set-buffer buffer)
8994           (gnus-remove-text-with-property 'gnus-decoration)
8995           (when (gnus-visual-p 'article-highlight 'highlight)
8996             ;; Copy-to-buffer doesn't copy overlay.  So redo
8997             ;; highlight.
8998             (let ((gnus-article-buffer buffer))
8999               (gnus-article-highlight-citation t)
9000               (gnus-article-highlight-signature)
9001               (gnus-article-emphasize)
9002               (gnus-article-delete-invisible-text)))
9003           (let ((ps-left-header
9004                  (list
9005                   (concat "("
9006                           (gnus-summary-print-truncate-and-quote
9007                            (mail-header-subject gnus-current-headers)
9008                            66) ")")
9009                   (concat "("
9010                           (gnus-summary-print-truncate-and-quote
9011                            (mail-header-from gnus-current-headers)
9012                            45) ")")))
9013                 (ps-right-header
9014                  (list
9015                   "/pagenumberstring load"
9016                   (concat "("
9017                           (mail-header-date gnus-current-headers) ")"))))
9018             (gnus-run-hooks 'gnus-ps-print-hook)
9019             (save-excursion
9020               (if window-system
9021                   (ps-spool-buffer-with-faces)
9022                 (ps-spool-buffer)))))
9023       (kill-buffer buffer))))
9024
9025 (defun gnus-summary-show-article (&optional arg)
9026   "Force redisplaying of the current article.
9027 If ARG (the prefix) is a number, show the article with the charset
9028 defined in `gnus-summary-show-article-charset-alist', or the charset
9029 input.
9030 If ARG (the prefix) is non-nil and not a number, show the raw article
9031 without any article massaging functions being run.  Normally, the key
9032 strokes are `C-u g'."
9033   (interactive "P")
9034   (cond
9035    ((numberp arg)
9036     (gnus-summary-show-article t)
9037     (let ((gnus-newsgroup-charset
9038            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9039                (mm-read-coding-system
9040                 "View as charset: " ;; actually it is coding system.
9041                 (with-current-buffer gnus-article-buffer
9042                   (mm-detect-coding-region (point) (point-max))))))
9043           (gnus-newsgroup-ignored-charsets 'gnus-all))
9044       (gnus-summary-select-article nil 'force)
9045       (let ((deps gnus-newsgroup-dependencies)
9046             head header lines)
9047         (save-excursion
9048           (set-buffer gnus-original-article-buffer)
9049           (save-restriction
9050             (message-narrow-to-head)
9051             (setq head (buffer-string))
9052             (goto-char (point-min))
9053             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9054               (goto-char (point-max))
9055               (widen)
9056               (setq lines (1- (count-lines (point) (point-max))))))
9057           (with-temp-buffer
9058             (insert (format "211 %d Article retrieved.\n"
9059                             (cdr gnus-article-current)))
9060             (insert head)
9061             (if lines (insert (format "Lines: %d\n" lines)))
9062             (insert ".\n")
9063             (let ((nntp-server-buffer (current-buffer)))
9064               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9065         (gnus-data-set-header
9066          (gnus-data-find (cdr gnus-article-current))
9067          header)
9068         (gnus-summary-update-article-line
9069          (cdr gnus-article-current) header)
9070         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9071           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9072    ((not arg)
9073     ;; Select the article the normal way.
9074     (gnus-summary-select-article nil 'force))
9075    (t
9076     ;; We have to require this here to make sure that the following
9077     ;; dynamic binding isn't shadowed by autoloading.
9078     (require 'gnus-async)
9079     (require 'gnus-art)
9080     ;; Bind the article treatment functions to nil.
9081     (let ((gnus-have-all-headers t)
9082           gnus-article-prepare-hook
9083           gnus-article-decode-hook
9084           gnus-display-mime-function
9085           gnus-break-pages)
9086       ;; Destroy any MIME parts.
9087       (when (gnus-buffer-live-p gnus-article-buffer)
9088         (save-excursion
9089           (set-buffer gnus-article-buffer)
9090           (mm-destroy-parts gnus-article-mime-handles)
9091           ;; Set it to nil for safety reason.
9092           (setq gnus-article-mime-handle-alist nil)
9093           (setq gnus-article-mime-handles nil)))
9094       (gnus-summary-select-article nil 'force))))
9095   (gnus-summary-goto-subject gnus-current-article)
9096   (gnus-summary-position-point))
9097
9098 (defun gnus-summary-show-raw-article ()
9099   "Show the raw article without any article massaging functions being run."
9100   (interactive)
9101   (gnus-summary-show-article t))
9102
9103 (defun gnus-summary-verbose-headers (&optional arg)
9104   "Toggle permanent full header display.
9105 If ARG is a positive number, turn header display on.
9106 If ARG is a negative number, turn header display off."
9107   (interactive "P")
9108   (setq gnus-show-all-headers
9109         (cond ((or (not (numberp arg))
9110                    (zerop arg))
9111                (not gnus-show-all-headers))
9112               ((natnump arg)
9113                t)))
9114   (gnus-summary-show-article))
9115
9116 (defun gnus-summary-toggle-header (&optional arg)
9117   "Show the headers if they are hidden, or hide them if they are shown.
9118 If ARG is a positive number, show the entire header.
9119 If ARG is a negative number, hide the unwanted header lines."
9120   (interactive "P")
9121   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9122                      (get-buffer-window gnus-article-buffer t))))
9123     (with-current-buffer gnus-article-buffer
9124       (widen)
9125       (article-narrow-to-head)
9126       (let* ((buffer-read-only nil)
9127              (inhibit-point-motion-hooks t)
9128              (hidden (if (numberp arg)
9129                          (>= arg 0)
9130                        (or (not (looking-at "[^ \t\n]+:"))
9131                            (gnus-article-hidden-text-p 'headers))))
9132              s e)
9133         (delete-region (point-min) (point-max))
9134         (with-current-buffer gnus-original-article-buffer
9135           (goto-char (setq s (point-min)))
9136           (setq e (if (search-forward "\n\n" nil t)
9137                       (1- (point))
9138                     (point-max))))
9139         (insert-buffer-substring gnus-original-article-buffer s e)
9140         (run-hooks 'gnus-article-decode-hook)
9141         (if hidden
9142             (let ((gnus-treat-hide-headers nil)
9143                   (gnus-treat-hide-boring-headers nil))
9144               (gnus-delete-wash-type 'headers)
9145               (gnus-treat-article 'head))
9146           (gnus-treat-article 'head))
9147         (widen)
9148         (if window
9149             (set-window-start window (goto-char (point-min))))
9150         (if gnus-break-pages
9151             (gnus-narrow-to-page)
9152           (when (gnus-visual-p 'page-marker)
9153             (let ((buffer-read-only nil))
9154               (gnus-remove-text-with-property 'gnus-prev)
9155               (gnus-remove-text-with-property 'gnus-next))))
9156         (gnus-set-mode-line 'article)))))
9157
9158 (defun gnus-summary-show-all-headers ()
9159   "Make all header lines visible."
9160   (interactive)
9161   (gnus-summary-toggle-header 1))
9162
9163 (defun gnus-summary-caesar-message (&optional arg)
9164   "Caesar rotate the current article by 13.
9165 The numerical prefix specifies how many places to rotate each letter
9166 forward."
9167   (interactive "P")
9168   (gnus-summary-select-article)
9169   (let ((mail-header-separator ""))
9170     (gnus-eval-in-buffer-window gnus-article-buffer
9171       (save-restriction
9172         (widen)
9173         (let ((start (window-start))
9174               buffer-read-only)
9175           (message-caesar-buffer-body arg)
9176           (set-window-start (get-buffer-window (current-buffer)) start)))))
9177   ;; Create buttons and stuff...
9178   (gnus-treat-article nil))
9179
9180 (defun gnus-summary-idna-message (&optional arg)
9181   "Decode IDNA encoded domain names in the current articles.
9182 IDNA encoded domain names looks like `xn--bar'.  If a string
9183 remain unencoded after running this function, it is likely an
9184 invalid IDNA string (`xn--bar' is invalid).
9185
9186 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9187 installed for this command to work."
9188   (interactive "P")
9189   (if (not (and (condition-case nil (require 'idna)
9190                   (file-error))
9191                 (mm-coding-system-p 'utf-8)
9192                 (executable-find (symbol-value 'idna-program))))
9193       (gnus-message
9194        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9195     (gnus-summary-select-article)
9196     (let ((mail-header-separator ""))
9197       (gnus-eval-in-buffer-window gnus-article-buffer
9198         (save-restriction
9199           (widen)
9200           (let ((start (window-start))
9201                 buffer-read-only)
9202             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9203               (replace-match (idna-to-unicode (match-string 1))))
9204             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9205
9206 (defun gnus-summary-morse-message (&optional arg)
9207   "Morse decode the current article."
9208   (interactive "P")
9209   (gnus-summary-select-article)
9210   (let ((mail-header-separator ""))
9211     (gnus-eval-in-buffer-window gnus-article-buffer
9212       (save-excursion
9213         (save-restriction
9214           (widen)
9215           (let ((pos (window-start))
9216                 buffer-read-only)
9217             (goto-char (point-min))
9218             (when (message-goto-body)
9219               (gnus-narrow-to-body))
9220             (goto-char (point-min))
9221             (while (search-forward "·" (point-max) t)
9222               (replace-match "."))
9223             (unmorse-region (point-min) (point-max))
9224             (widen)
9225             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9226
9227 (defun gnus-summary-stop-page-breaking ()
9228   "Stop page breaking in the current article."
9229   (interactive)
9230   (gnus-summary-select-article)
9231   (gnus-eval-in-buffer-window gnus-article-buffer
9232     (widen)
9233     (when (gnus-visual-p 'page-marker)
9234       (let ((buffer-read-only nil))
9235         (gnus-remove-text-with-property 'gnus-prev)
9236         (gnus-remove-text-with-property 'gnus-next))
9237       (setq gnus-page-broken nil))))
9238
9239 (defun gnus-summary-move-article (&optional n to-newsgroup
9240                                             select-method action)
9241   "Move the current article to a different newsgroup.
9242 If N is a positive number, move the N next articles.
9243 If N is a negative number, move the N previous articles.
9244 If N is nil and any articles have been marked with the process mark,
9245 move those articles instead.
9246 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9247 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9248 re-spool using this method.
9249
9250 When called interactively with TO-NEWSGROUP being nil, the value of
9251 the variable `gnus-move-split-methods' is used for finding a default
9252 for the target newsgroup.
9253
9254 For this function to work, both the current newsgroup and the
9255 newsgroup that you want to move to have to support the `request-move'
9256 and `request-accept' functions.
9257
9258 ACTION can be either `move' (the default), `crosspost' or `copy'."
9259   (interactive "P")
9260   (unless action
9261     (setq action 'move))
9262   ;; Check whether the source group supports the required functions.
9263   (cond ((and (eq action 'move)
9264               (not (gnus-check-backend-function
9265                     'request-move-article gnus-newsgroup-name)))
9266          (error "The current group does not support article moving"))
9267         ((and (eq action 'crosspost)
9268               (not (gnus-check-backend-function
9269                     'request-replace-article gnus-newsgroup-name)))
9270          (error "The current group does not support article editing")))
9271   (let ((articles (gnus-summary-work-articles n))
9272         (prefix (if (gnus-check-backend-function
9273                      'request-move-article gnus-newsgroup-name)
9274                     (funcall gnus-move-group-prefix-function
9275                              gnus-newsgroup-name)
9276                   ""))
9277         (names '((move "Move" "Moving")
9278                  (copy "Copy" "Copying")
9279                  (crosspost "Crosspost" "Crossposting")))
9280         (copy-buf (save-excursion
9281                     (nnheader-set-temp-buffer " *copy article*")))
9282         art-group to-method new-xref article to-groups articles-to-update-marks)
9283     (unless (assq action names)
9284       (error "Unknown action %s" action))
9285     ;; Read the newsgroup name.
9286     (when (and (not to-newsgroup)
9287                (not select-method))
9288       (if (and gnus-move-split-methods
9289                (not
9290                 (and (memq gnus-current-article articles)
9291                      (gnus-buffer-live-p gnus-original-article-buffer))))
9292           ;; When `gnus-move-split-methods' is non-nil, we have to
9293           ;; select an article to give `gnus-read-move-group-name' an
9294           ;; opportunity to suggest an appropriate default.  However,
9295           ;; we needn't render or mark the article.
9296           (let ((gnus-display-mime-function nil)
9297                 (gnus-article-prepare-hook nil)
9298                 (gnus-mark-article-hook nil))
9299             (gnus-summary-select-article nil nil nil (car articles))))
9300       (setq to-newsgroup
9301             (gnus-read-move-group-name
9302              (cadr (assq action names))
9303              (symbol-value (intern (format "gnus-current-%s-group" action)))
9304              articles prefix))
9305       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9306     (setq to-method (or select-method
9307                         (gnus-server-to-method
9308                          (gnus-group-method to-newsgroup))))
9309     ;; Check the method we are to move this article to...
9310     (unless (gnus-check-backend-function
9311              'request-accept-article (car to-method))
9312       (error "%s does not support article copying" (car to-method)))
9313     (unless (gnus-check-server to-method)
9314       (error "Can't open server %s" (car to-method)))
9315     (gnus-message 6 "%s to %s: %s..."
9316                   (caddr (assq action names))
9317                   (or (car select-method) to-newsgroup) articles)
9318     (while articles
9319       (setq article (pop articles))
9320       (setq
9321        art-group
9322        (cond
9323         ;; Move the article.
9324         ((eq action 'move)
9325          ;; Remove this article from future suppression.
9326          (gnus-dup-unsuppress-article article)
9327          (let* ((from-method (gnus-find-method-for-group
9328                               gnus-newsgroup-name))
9329                 (to-method (gnus-find-method-for-group
9330                             to-newsgroup))
9331                 (move-is-internal (gnus-method-equal from-method to-method)))
9332          (gnus-request-move-article
9333           article                       ; Article to move
9334           gnus-newsgroup-name           ; From newsgroup
9335           (nth 1 (gnus-find-method-for-group
9336                   gnus-newsgroup-name)) ; Server
9337           (list 'gnus-request-accept-article
9338                 to-newsgroup (list 'quote select-method)
9339                 (not articles) t)       ; Accept form
9340           (not articles)                ; Only save nov last time
9341           move-is-internal)))           ; is this move internal?
9342         ;; Copy the article.
9343         ((eq action 'copy)
9344          (save-excursion
9345            (set-buffer copy-buf)
9346            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9347              (save-restriction
9348                (nnheader-narrow-to-headers)
9349                (dolist (hdr gnus-copy-article-ignored-headers)
9350                  (message-remove-header hdr t)))
9351              (gnus-request-accept-article
9352               to-newsgroup select-method (not articles) t))))
9353         ;; Crosspost the article.
9354         ((eq action 'crosspost)
9355          (let ((xref (message-tokenize-header
9356                       (mail-header-xref (gnus-summary-article-header article))
9357                       " ")))
9358            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9359                                   ":" (number-to-string article)))
9360            (unless xref
9361              (setq xref (list (system-name))))
9362            (setq new-xref
9363                  (concat
9364                   (mapconcat 'identity
9365                              (delete "Xref:" (delete new-xref xref))
9366                              " ")
9367                   " " new-xref))
9368            (save-excursion
9369              (set-buffer copy-buf)
9370              ;; First put the article in the destination group.
9371              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9372              (when (consp (setq art-group
9373                                 (gnus-request-accept-article
9374                                  to-newsgroup select-method (not articles))))
9375                (setq new-xref (concat new-xref " " (car art-group)
9376                                       ":"
9377                                       (number-to-string (cdr art-group))))
9378                ;; Now we have the new Xrefs header, so we insert
9379                ;; it and replace the new article.
9380                (nnheader-replace-header "Xref" new-xref)
9381                (gnus-request-replace-article
9382                 (cdr art-group) to-newsgroup (current-buffer))
9383                art-group))))))
9384       (cond
9385        ((not art-group)
9386         (gnus-message 1 "Couldn't %s article %s: %s"
9387                       (cadr (assq action names)) article
9388                       (nnheader-get-report (car to-method))))
9389        ((eq art-group 'junk)
9390         (when (eq action 'move)
9391           (gnus-summary-mark-article article gnus-canceled-mark)
9392           (gnus-message 4 "Deleted article %s" article)
9393           ;; run the delete hook
9394           (run-hook-with-args 'gnus-summary-article-delete-hook
9395                               action
9396                               (gnus-data-header
9397                                (assoc article (gnus-data-list nil)))
9398                               gnus-newsgroup-name nil
9399                               select-method)))
9400        (t
9401         (let* ((pto-group (gnus-group-prefixed-name
9402                            (car art-group) to-method))
9403                (info (gnus-get-info pto-group))
9404                (to-group (gnus-info-group info))
9405                to-marks)
9406           ;; Update the group that has been moved to.
9407           (when (and info
9408                      (memq action '(move copy)))
9409             (unless (member to-group to-groups)
9410               (push to-group to-groups))
9411
9412             (unless (memq article gnus-newsgroup-unreads)
9413               (push 'read to-marks)
9414               (gnus-info-set-read
9415                info (gnus-add-to-range (gnus-info-read info)
9416                                        (list (cdr art-group)))))
9417
9418             ;; See whether the article is to be put in the cache.
9419             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9420                              gnus-article-mark-lists
9421                            (delete '(expirable . expire)
9422                                    (copy-sequence gnus-article-mark-lists))))
9423                   (to-article (cdr art-group)))
9424
9425               ;; Enter the article into the cache in the new group,
9426               ;; if that is required.
9427               (when gnus-use-cache
9428                 (gnus-cache-possibly-enter-article
9429                  to-group to-article
9430                  (memq article gnus-newsgroup-marked)
9431                  (memq article gnus-newsgroup-dormant)
9432                  (memq article gnus-newsgroup-unreads)))
9433
9434               (when gnus-preserve-marks
9435                 ;; Copy any marks over to the new group.
9436                 (when (and (equal to-group gnus-newsgroup-name)
9437                            (not (memq article gnus-newsgroup-unreads)))
9438                   ;; Mark this article as read in this group.
9439                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9440                   (setcdr (gnus-active to-group) to-article)
9441                   (setcdr gnus-newsgroup-active to-article))
9442
9443                 (while marks
9444                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9445                     (when (memq article (symbol-value
9446                                          (intern (format "gnus-newsgroup-%s"
9447                                                          (caar marks)))))
9448                       (push (cdar marks) to-marks)
9449                       ;; If the other group is the same as this group,
9450                       ;; then we have to add the mark to the list.
9451                       (when (equal to-group gnus-newsgroup-name)
9452                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9453                              (cons to-article
9454                                    (symbol-value
9455                                     (intern (format "gnus-newsgroup-%s"
9456                                                     (caar marks)))))))
9457                       ;; Copy the marks to other group.
9458                       (gnus-add-marked-articles
9459                        to-group (cdar marks) (list to-article) info)))
9460                   (setq marks (cdr marks)))
9461
9462                 (gnus-request-set-mark
9463                  to-group (list (list (list to-article) 'add to-marks))))
9464
9465               (gnus-dribble-enter
9466                (concat "(gnus-group-set-info '"
9467                        (gnus-prin1-to-string (gnus-get-info to-group))
9468                        ")"))))
9469
9470           ;; Update the Xref header in this article to point to
9471           ;; the new crossposted article we have just created.
9472           (when (eq action 'crosspost)
9473             (save-excursion
9474               (set-buffer copy-buf)
9475               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9476               (nnheader-replace-header "Xref" new-xref)
9477               (gnus-request-replace-article
9478                article gnus-newsgroup-name (current-buffer))))
9479
9480           ;; run the move/copy/crosspost/respool hook
9481           (run-hook-with-args 'gnus-summary-article-move-hook
9482                               action
9483                               (gnus-data-header
9484                                (assoc article (gnus-data-list nil)))
9485                               gnus-newsgroup-name
9486                               to-newsgroup
9487                               select-method))
9488
9489         ;;;!!!Why is this necessary?
9490         (set-buffer gnus-summary-buffer)
9491         
9492         (gnus-summary-goto-subject article)
9493         (when (eq action 'move)
9494           (gnus-summary-mark-article article gnus-canceled-mark))))
9495       (push article articles-to-update-marks))
9496
9497     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9498     ;; Re-activate all groups that have been moved to.
9499     (save-excursion
9500       (set-buffer gnus-group-buffer)
9501       (let ((gnus-group-marked to-groups))
9502         (gnus-group-get-new-news-this-group nil t)))
9503     
9504     (gnus-kill-buffer copy-buf)
9505     (gnus-summary-position-point)
9506     (gnus-set-mode-line 'summary)))
9507
9508 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9509   "Copy the current article to some other group.
9510 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9511 When called interactively, if TO-NEWSGROUP is nil, use the value of
9512 the variable `gnus-move-split-methods' for finding a default target
9513 newsgroup.
9514 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9515 re-spool using this method."
9516   (interactive "P")
9517   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9518
9519 (defun gnus-summary-crosspost-article (&optional n)
9520   "Crosspost the current article to some other group."
9521   (interactive "P")
9522   (gnus-summary-move-article n nil nil 'crosspost))
9523
9524 (defcustom gnus-summary-respool-default-method nil
9525   "Default method type for respooling an article.
9526 If nil, use to the current newsgroup method."
9527   :type 'symbol
9528   :group 'gnus-summary-mail)
9529
9530 (defcustom gnus-summary-display-while-building nil
9531   "If non-nil, show and update the summary buffer as it's being built.
9532 If the value is t, update the buffer after every line is inserted.  If
9533 the value is an integer (N), update the display every N lines."
9534   :version "22.1"
9535   :group 'gnus-thread
9536   :type '(choice (const :tag "off" nil)
9537                  number
9538                  (const :tag "frequently" t)))
9539
9540 (defun gnus-summary-respool-article (&optional n method)
9541   "Respool the current article.
9542 The article will be squeezed through the mail spooling process again,
9543 which means that it will be put in some mail newsgroup or other
9544 depending on `nnmail-split-methods'.
9545 If N is a positive number, respool the N next articles.
9546 If N is a negative number, respool the N previous articles.
9547 If N is nil and any articles have been marked with the process mark,
9548 respool those articles instead.
9549
9550 Respooling can be done both from mail groups and \"real\" newsgroups.
9551 In the former case, the articles in question will be moved from the
9552 current group into whatever groups they are destined to.  In the
9553 latter case, they will be copied into the relevant groups."
9554   (interactive
9555    (list current-prefix-arg
9556          (let* ((methods (gnus-methods-using 'respool))
9557                 (methname
9558                  (symbol-name (or gnus-summary-respool-default-method
9559                                   (car (gnus-find-method-for-group
9560                                         gnus-newsgroup-name)))))
9561                 (method
9562                  (gnus-completing-read-with-default
9563                   methname "Backend to use when respooling"
9564                   methods nil t nil 'gnus-mail-method-history))
9565                 ms)
9566            (cond
9567             ((zerop (length (setq ms (gnus-servers-using-backend
9568                                       (intern method)))))
9569              (list (intern method) ""))
9570             ((= 1 (length ms))
9571              (car ms))
9572             (t
9573              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9574                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9575                            ms-alist))))))))
9576   (unless method
9577     (error "No method given for respooling"))
9578   (if (assoc (symbol-name
9579               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9580              (gnus-methods-using 'respool))
9581       (gnus-summary-move-article n nil method)
9582     (gnus-summary-copy-article n nil method)))
9583
9584 (defun gnus-summary-import-article (file &optional edit)
9585   "Import an arbitrary file into a mail newsgroup."
9586   (interactive "fImport file: \nP")
9587   (let ((group gnus-newsgroup-name)
9588         (now (current-time))
9589         atts lines group-art)
9590     (unless (gnus-check-backend-function 'request-accept-article group)
9591       (error "%s does not support article importing" group))
9592     (or (file-readable-p file)
9593         (not (file-regular-p file))
9594         (error "Can't read %s" file))
9595     (save-excursion
9596       (set-buffer (gnus-get-buffer-create " *import file*"))
9597       (erase-buffer)
9598       (nnheader-insert-file-contents file)
9599       (goto-char (point-min))
9600       (if (nnheader-article-p)
9601           (save-restriction
9602             (goto-char (point-min))
9603             (search-forward "\n\n" nil t)
9604             (narrow-to-region (point-min) (1- (point)))
9605             (goto-char (point-min))
9606             (unless (re-search-forward "^date:" nil t)
9607               (goto-char (point-max))
9608               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9609        ;; This doesn't look like an article, so we fudge some headers.
9610         (setq atts (file-attributes file)
9611               lines (count-lines (point-min) (point-max)))
9612         (insert "From: " (read-string "From: ") "\n"
9613                 "Subject: " (read-string "Subject: ") "\n"
9614                 "Date: " (message-make-date (nth 5 atts)) "\n"
9615                 "Message-ID: " (message-make-message-id) "\n"
9616                 "Lines: " (int-to-string lines) "\n"
9617                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9618       (setq group-art (gnus-request-accept-article group nil t))
9619       (kill-buffer (current-buffer)))
9620     (setq gnus-newsgroup-active (gnus-activate-group group))
9621     (forward-line 1)
9622     (gnus-summary-goto-article (cdr group-art) nil t)
9623     (when edit
9624       (gnus-summary-edit-article))))
9625
9626 (defun gnus-summary-create-article ()
9627   "Create an article in a mail newsgroup."
9628   (interactive)
9629   (let ((group gnus-newsgroup-name)
9630         (now (current-time))
9631         group-art)
9632     (unless (gnus-check-backend-function 'request-accept-article group)
9633       (error "%s does not support article importing" group))
9634     (save-excursion
9635       (set-buffer (gnus-get-buffer-create " *import file*"))
9636       (erase-buffer)
9637       (goto-char (point-min))
9638       ;; This doesn't look like an article, so we fudge some headers.
9639       (insert "From: " (read-string "From: ") "\n"
9640               "Subject: " (read-string "Subject: ") "\n"
9641               "Date: " (message-make-date now) "\n"
9642               "Message-ID: " (message-make-message-id) "\n")
9643       (setq group-art (gnus-request-accept-article group nil t))
9644       (kill-buffer (current-buffer)))
9645     (setq gnus-newsgroup-active (gnus-activate-group group))
9646     (forward-line 1)
9647     (gnus-summary-goto-article (cdr group-art) nil t)
9648     (gnus-summary-edit-article)))
9649
9650 (defun gnus-summary-article-posted-p ()
9651   "Say whether the current (mail) article is available from news as well.
9652 This will be the case if the article has both been mailed and posted."
9653   (interactive)
9654   (let ((id (mail-header-references (gnus-summary-article-header)))
9655         (gnus-override-method (car (gnus-refer-article-methods))))
9656     (if (gnus-request-head id "")
9657         (gnus-message 2 "The current message was found on %s"
9658                       gnus-override-method)
9659       (gnus-message 2 "The current message couldn't be found on %s"
9660                     gnus-override-method)
9661       nil)))
9662
9663 (defun gnus-summary-expire-articles (&optional now)
9664   "Expire all articles that are marked as expirable in the current group."
9665   (interactive)
9666   (when (and (not gnus-group-is-exiting-without-update-p)
9667              (gnus-check-backend-function
9668               'request-expire-articles gnus-newsgroup-name))
9669     ;; This backend supports expiry.
9670     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9671            (expirable (if total
9672                           (progn
9673                             ;; We need to update the info for
9674                             ;; this group for `gnus-list-of-read-articles'
9675                             ;; to give us the right answer.
9676                             (gnus-run-hooks 'gnus-exit-group-hook)
9677                             (gnus-summary-update-info)
9678                             (gnus-list-of-read-articles gnus-newsgroup-name))
9679                         (setq gnus-newsgroup-expirable
9680                               (sort gnus-newsgroup-expirable '<))))
9681            (expiry-wait (if now 'immediate
9682                           (gnus-group-find-parameter
9683                            gnus-newsgroup-name 'expiry-wait)))
9684            (nnmail-expiry-target
9685             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9686                 nnmail-expiry-target))
9687            es)
9688       (when expirable
9689         ;; There are expirable articles in this group, so we run them
9690         ;; through the expiry process.
9691         (gnus-message 6 "Expiring articles...")
9692         (unless (gnus-check-group gnus-newsgroup-name)
9693           (error "Can't open server for %s" gnus-newsgroup-name))
9694         ;; The list of articles that weren't expired is returned.
9695         (save-excursion
9696           (if expiry-wait
9697               (let ((nnmail-expiry-wait-function nil)
9698                     (nnmail-expiry-wait expiry-wait))
9699                 (setq es (gnus-request-expire-articles
9700                           expirable gnus-newsgroup-name)))
9701             (setq es (gnus-request-expire-articles
9702                       expirable gnus-newsgroup-name)))
9703           (unless total
9704             (setq gnus-newsgroup-expirable es))
9705           ;; We go through the old list of expirable, and mark all
9706           ;; really expired articles as nonexistent.
9707           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9708             (let ((gnus-use-cache nil))
9709               (dolist (article expirable)
9710                 (when (and (not (memq article es))
9711                            (gnus-data-find article))
9712                   (gnus-summary-mark-article article gnus-canceled-mark)
9713                   (run-hook-with-args 'gnus-summary-article-expire-hook
9714                                       'delete
9715                                       (gnus-data-header
9716                                        (assoc article (gnus-data-list nil)))
9717                                       gnus-newsgroup-name
9718                                       nil
9719                                       nil))))))
9720         (gnus-message 6 "Expiring articles...done")))))
9721
9722 (defun gnus-summary-expire-articles-now ()
9723   "Expunge all expirable articles in the current group.
9724 This means that *all* articles that are marked as expirable will be
9725 deleted forever, right now."
9726   (interactive)
9727   (or gnus-expert-user
9728       (gnus-yes-or-no-p
9729        "Are you really, really sure you want to delete all expirable messages? ")
9730       (error "Phew!"))
9731   (gnus-summary-expire-articles t))
9732
9733 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9734 (defun gnus-summary-delete-article (&optional n)
9735   "Delete the N next (mail) articles.
9736 This command actually deletes articles.  This is not a marking
9737 command.  The article will disappear forever from your life, never to
9738 return.
9739
9740 If N is negative, delete backwards.
9741 If N is nil and articles have been marked with the process mark,
9742 delete these instead.
9743
9744 If `gnus-novice-user' is non-nil you will be asked for
9745 confirmation before the articles are deleted."
9746   (interactive "P")
9747   (unless (gnus-check-backend-function 'request-expire-articles
9748                                        gnus-newsgroup-name)
9749     (error "The current newsgroup does not support article deletion"))
9750   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9751     (error "Couldn't open server"))
9752   ;; Compute the list of articles to delete.
9753   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9754         (nnmail-expiry-target 'delete)
9755         not-deleted)
9756     (if (and gnus-novice-user
9757              (not (gnus-yes-or-no-p
9758                    (format "Do you really want to delete %s forever? "
9759                            (if (> (length articles) 1)
9760                                (format "these %s articles" (length articles))
9761                              "this article")))))
9762         ()
9763       ;; Delete the articles.
9764       (setq not-deleted (gnus-request-expire-articles
9765                          articles gnus-newsgroup-name 'force))
9766       (while articles
9767         (gnus-summary-remove-process-mark (car articles))
9768         ;; The backend might not have been able to delete the article
9769         ;; after all.
9770         (unless (memq (car articles) not-deleted)
9771           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9772         (let* ((article (car articles))
9773                (ghead  (gnus-data-header
9774                                     (assoc article (gnus-data-list nil)))))
9775           (run-hook-with-args 'gnus-summary-article-delete-hook
9776                               'delete ghead gnus-newsgroup-name nil
9777                               nil))
9778         (setq articles (cdr articles)))
9779       (when not-deleted
9780         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9781     (gnus-summary-position-point)
9782     (gnus-set-mode-line 'summary)
9783     not-deleted))
9784
9785 (defun gnus-summary-edit-article (&optional arg)
9786   "Edit the current article.
9787 This will have permanent effect only in mail groups.
9788 If ARG is nil, edit the decoded articles.
9789 If ARG is 1, edit the raw articles.
9790 If ARG is 2, edit the raw articles even in read-only groups.
9791 If ARG is 3, edit the articles with the current handles.
9792 Otherwise, allow editing of articles even in read-only
9793 groups."
9794   (interactive "P")
9795   (let (force raw current-handles)
9796     (cond
9797      ((null arg))
9798      ((eq arg 1)
9799       (setq raw t))
9800      ((eq arg 2)
9801       (setq raw t
9802             force t))
9803      ((eq arg 3)
9804       (setq current-handles
9805             (and (gnus-buffer-live-p gnus-article-buffer)
9806                  (with-current-buffer gnus-article-buffer
9807                    (prog1
9808                        gnus-article-mime-handles
9809                      (setq gnus-article-mime-handles nil))))))
9810      (t
9811       (setq force t)))
9812     (when (and raw (not force)
9813                (member gnus-newsgroup-name '("nndraft:delayed"
9814                                              "nndraft:drafts"
9815                                              "nndraft:queue")))
9816       (error "Can't edit the raw article in group %s"
9817              gnus-newsgroup-name))
9818     (save-excursion
9819       (set-buffer gnus-summary-buffer)
9820       (let ((mail-parse-charset gnus-newsgroup-charset)
9821             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9822         (gnus-set-global-variables)
9823         (when (and (not force)
9824                    (gnus-group-read-only-p))
9825           (error "The current newsgroup does not support article editing"))
9826         (gnus-summary-show-article t)
9827         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9828           (with-current-buffer gnus-article-buffer
9829             (mm-enable-multibyte)))
9830         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9831             (setq raw t))
9832         (gnus-article-edit-article
9833          (if raw 'ignore
9834            `(lambda ()
9835               (let ((mbl mml-buffer-list))
9836                 (setq mml-buffer-list nil)
9837                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9838                   (mime-to-mml ,'current-handles))
9839                 (let ((mbl1 mml-buffer-list))
9840                   (setq mml-buffer-list mbl)
9841                   (set (make-local-variable 'mml-buffer-list) mbl1))
9842                 (gnus-make-local-hook 'kill-buffer-hook)
9843                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9844          `(lambda (no-highlight)
9845             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9846                   (message-options message-options)
9847                   (message-options-set-recipient)
9848                   (mail-parse-ignored-charsets
9849                    ',gnus-newsgroup-ignored-charsets))
9850               ,(if (not raw) '(progn
9851                                 (mml-to-mime)
9852                                 (mml-destroy-buffers)
9853                                 (remove-hook 'kill-buffer-hook
9854                                              'mml-destroy-buffers t)
9855                                 (kill-local-variable 'mml-buffer-list)))
9856               (gnus-summary-edit-article-done
9857                ,(or (mail-header-references gnus-current-headers) "")
9858                ,(gnus-group-read-only-p)
9859                ,gnus-summary-buffer no-highlight))))))))
9860
9861 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9862
9863 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9864                                                  no-highlight)
9865   "Make edits to the current article permanent."
9866   (interactive)
9867   (save-excursion
9868    ;; The buffer restriction contains the entire article if it exists.
9869     (when (article-goto-body)
9870       (let ((lines (count-lines (point) (point-max)))
9871             (length (- (point-max) (point)))
9872             (case-fold-search t)
9873             (body (copy-marker (point))))
9874         (goto-char (point-min))
9875         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9876           (delete-region (match-beginning 1) (match-end 1))
9877           (insert (number-to-string length)))
9878         (goto-char (point-min))
9879         (when (re-search-forward
9880                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9881           (delete-region (match-beginning 1) (match-end 1))
9882           (insert (number-to-string length)))
9883         (goto-char (point-min))
9884         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9885           (delete-region (match-beginning 1) (match-end 1))
9886           (insert (number-to-string lines))))))
9887   ;; Replace the article.
9888   (let ((buf (current-buffer)))
9889     (with-temp-buffer
9890       (insert-buffer-substring buf)
9891
9892       (if (and (not read-only)
9893                (not (gnus-request-replace-article
9894                      (cdr gnus-article-current) (car gnus-article-current)
9895                      (current-buffer) t)))
9896           (error "Couldn't replace article")
9897         ;; Update the summary buffer.
9898         (if (and references
9899                  (equal (message-tokenize-header references " ")
9900                         (message-tokenize-header
9901                          (or (message-fetch-field "references") "") " ")))
9902             ;; We only have to update this line.
9903             (save-excursion
9904               (save-restriction
9905                 (message-narrow-to-head)
9906                 (let ((head (buffer-substring-no-properties
9907                              (point-min) (point-max)))
9908                       header)
9909                   (with-temp-buffer
9910                     (insert (format "211 %d Article retrieved.\n"
9911                                     (cdr gnus-article-current)))
9912                     (insert head)
9913                     (insert ".\n")
9914                     (let ((nntp-server-buffer (current-buffer)))
9915                       (setq header (car (gnus-get-newsgroup-headers
9916                                          nil t))))
9917                     (save-excursion
9918                       (set-buffer gnus-summary-buffer)
9919                       (gnus-data-set-header
9920                        (gnus-data-find (cdr gnus-article-current))
9921                        header)
9922                       (gnus-summary-update-article-line
9923                        (cdr gnus-article-current) header)
9924                       (if (gnus-summary-goto-subject
9925                            (cdr gnus-article-current) nil t)
9926                           (gnus-summary-update-secondary-mark
9927                            (cdr gnus-article-current))))))))
9928           ;; Update threads.
9929           (set-buffer (or buffer gnus-summary-buffer))
9930           (gnus-summary-update-article (cdr gnus-article-current))
9931           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9932               (gnus-summary-update-secondary-mark
9933                (cdr gnus-article-current))))
9934         ;; Prettify the article buffer again.
9935         (unless no-highlight
9936           (save-excursion
9937             (set-buffer gnus-article-buffer)
9938             ;;;!!! Fix this -- article should be rehighlighted.
9939             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9940             (set-buffer gnus-original-article-buffer)
9941             (gnus-request-article
9942              (cdr gnus-article-current)
9943              (car gnus-article-current) (current-buffer))))
9944         ;; Prettify the summary buffer line.
9945         (when (gnus-visual-p 'summary-highlight 'highlight)
9946           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9947
9948 (defun gnus-summary-edit-wash (key)
9949   "Perform editing command KEY in the article buffer."
9950   (interactive
9951    (list
9952     (progn
9953       (message "%s" (concat (this-command-keys) "- "))
9954       (read-char))))
9955   (message "")
9956   (gnus-summary-edit-article)
9957   (execute-kbd-macro (concat (this-command-keys) key))
9958   (gnus-article-edit-done))
9959
9960 ;;; Respooling
9961
9962 (defun gnus-summary-respool-query (&optional silent trace)
9963   "Query where the respool algorithm would put this article."
9964   (interactive)
9965   (let (gnus-mark-article-hook)
9966     (gnus-summary-select-article)
9967     (save-excursion
9968       (set-buffer gnus-original-article-buffer)
9969       (let ((groups (nnmail-article-group 'identity trace)))
9970         (unless silent
9971           (if groups
9972               (message "This message would go to %s"
9973                        (mapconcat 'car groups ", "))
9974             (message "This message would go to no groups"))
9975           groups)))))
9976
9977 (defun gnus-summary-respool-trace ()
9978   "Trace where the respool algorithm would put this article.
9979 Display a buffer showing all fancy splitting patterns which matched."
9980   (interactive)
9981   (gnus-summary-respool-query nil t))
9982
9983 ;; Summary marking commands.
9984
9985 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9986   "Mark articles which has the same subject as read, and then select the next.
9987 If UNMARK is positive, remove any kind of mark.
9988 If UNMARK is negative, tick articles."
9989   (interactive "P")
9990   (when unmark
9991     (setq unmark (prefix-numeric-value unmark)))
9992   (let ((count
9993          (gnus-summary-mark-same-subject
9994           (gnus-summary-article-subject) unmark)))
9995     ;; Select next unread article.  If auto-select-same mode, should
9996     ;; select the first unread article.
9997     (gnus-summary-next-article t (and gnus-auto-select-same
9998                                       (gnus-summary-article-subject)))
9999     (gnus-message 7 "%d article%s marked as %s"
10000                   count (if (= count 1) " is" "s are")
10001                   (if unmark "unread" "read"))))
10002
10003 (defun gnus-summary-kill-same-subject (&optional unmark)
10004   "Mark articles which has the same subject as read.
10005 If UNMARK is positive, remove any kind of mark.
10006 If UNMARK is negative, tick articles."
10007   (interactive "P")
10008   (when unmark
10009     (setq unmark (prefix-numeric-value unmark)))
10010   (let ((count
10011          (gnus-summary-mark-same-subject
10012           (gnus-summary-article-subject) unmark)))
10013     ;; If marked as read, go to next unread subject.
10014     (when (null unmark)
10015       ;; Go to next unread subject.
10016       (gnus-summary-next-subject 1 t))
10017     (gnus-message 7 "%d articles are marked as %s"
10018                   count (if unmark "unread" "read"))))
10019
10020 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10021   "Mark articles with same SUBJECT as read, and return marked number.
10022 If optional argument UNMARK is positive, remove any kinds of marks.
10023 If optional argument UNMARK is negative, mark articles as unread instead."
10024   (let ((count 1))
10025     (save-excursion
10026       (cond
10027        ((null unmark)                   ; Mark as read.
10028         (while (and
10029                 (progn
10030                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10031                   (gnus-summary-show-thread) t)
10032                 (gnus-summary-find-subject subject))
10033           (setq count (1+ count))))
10034        ((> unmark 0)                    ; Tick.
10035         (while (and
10036                 (progn
10037                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10038                   (gnus-summary-show-thread) t)
10039                 (gnus-summary-find-subject subject))
10040           (setq count (1+ count))))
10041        (t                               ; Mark as unread.
10042         (while (and
10043                 (progn
10044                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10045                   (gnus-summary-show-thread) t)
10046                 (gnus-summary-find-subject subject))
10047           (setq count (1+ count)))))
10048       (gnus-set-mode-line 'summary)
10049       ;; Return the number of marked articles.
10050       count)))
10051
10052 (defun gnus-summary-mark-as-processable (n &optional unmark)
10053   "Set the process mark on the next N articles.
10054 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10055 the process mark instead.  The difference between N and the actual
10056 number of articles marked is returned."
10057   (interactive "P")
10058   (if (and (null n) (gnus-region-active-p))
10059       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10060     (setq n (prefix-numeric-value n))
10061     (let ((backward (< n 0))
10062           (n (abs n)))
10063       (while (and
10064               (> n 0)
10065               (if unmark
10066                   (gnus-summary-remove-process-mark
10067                    (gnus-summary-article-number))
10068                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10069               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10070         (setq n (1- n)))
10071       (when (/= 0 n)
10072         (gnus-message 7 "No more articles"))
10073       (gnus-summary-recenter)
10074       (gnus-summary-position-point)
10075       n)))
10076
10077 (defun gnus-summary-unmark-as-processable (n)
10078   "Remove the process mark from the next N articles.
10079 If N is negative, unmark backward instead.  The difference between N and
10080 the actual number of articles unmarked is returned."
10081   (interactive "P")
10082   (gnus-summary-mark-as-processable n t))
10083
10084 (defun gnus-summary-unmark-all-processable ()
10085   "Remove the process mark from all articles."
10086   (interactive)
10087   (save-excursion
10088     (while gnus-newsgroup-processable
10089       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10090   (gnus-summary-position-point))
10091
10092 (defun gnus-summary-add-mark (article type)
10093   "Mark ARTICLE with a mark of TYPE."
10094   (let ((vtype (car (assq type gnus-article-mark-lists)))
10095         var)
10096     (if (not vtype)
10097         (error "No such mark type: %s" type)
10098       (setq var (intern (format "gnus-newsgroup-%s" type)))
10099       (set var (cons article (symbol-value var)))
10100       (if (memq type '(processable cached replied forwarded recent saved))
10101           (gnus-summary-update-secondary-mark article)
10102         ;;; !!! This is bogus.  We should find out what primary
10103         ;;; !!! mark we want to set.
10104         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10105
10106 (defun gnus-summary-mark-as-expirable (n)
10107   "Mark N articles forward as expirable.
10108 If N is negative, mark backward instead.  The difference between N and
10109 the actual number of articles marked is returned."
10110   (interactive "p")
10111   (gnus-summary-mark-forward n gnus-expirable-mark))
10112
10113 (defun gnus-summary-mark-as-spam (n)
10114   "Mark N articles forward as spam.
10115 If N is negative, mark backward instead.  The difference between N and
10116 the actual number of articles marked is returned."
10117   (interactive "p")
10118   (gnus-summary-mark-forward n gnus-spam-mark))
10119
10120 (defun gnus-summary-mark-article-as-replied (article)
10121   "Mark ARTICLE as replied to and update the summary line.
10122 ARTICLE can also be a list of articles."
10123   (interactive (list (gnus-summary-article-number)))
10124   (let ((articles (if (listp article) article (list article))))
10125     (dolist (article articles)
10126       (unless (numberp article)
10127         (error "%s is not a number" article))
10128       (push article gnus-newsgroup-replied)
10129       (let ((buffer-read-only nil))
10130         (when (gnus-summary-goto-subject article nil t)
10131           (gnus-summary-update-secondary-mark article))))))
10132
10133 (defun gnus-summary-mark-article-as-forwarded (article)
10134   "Mark ARTICLE as forwarded and update the summary line.
10135 ARTICLE can also be a list of articles."
10136   (let ((articles (if (listp article) article (list article))))
10137     (dolist (article articles)
10138       (push article gnus-newsgroup-forwarded)
10139       (let ((buffer-read-only nil))
10140         (when (gnus-summary-goto-subject article nil t)
10141           (gnus-summary-update-secondary-mark article))))))
10142
10143 (defun gnus-summary-set-bookmark (article)
10144   "Set a bookmark in current article."
10145   (interactive (list (gnus-summary-article-number)))
10146   (when (or (not (get-buffer gnus-article-buffer))
10147             (not gnus-current-article)
10148             (not gnus-article-current)
10149             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10150     (error "No current article selected"))
10151   ;; Remove old bookmark, if one exists.
10152   (gnus-pull article gnus-newsgroup-bookmarks)
10153   ;; Set the new bookmark, which is on the form
10154   ;; (article-number . line-number-in-body).
10155   (push
10156    (cons article
10157          (with-current-buffer gnus-article-buffer
10158            (count-lines
10159             (min (point)
10160                  (save-excursion
10161                    (article-goto-body)
10162                    (point)))
10163             (point))))
10164    gnus-newsgroup-bookmarks)
10165   (gnus-message 6 "A bookmark has been added to the current article."))
10166
10167 (defun gnus-summary-remove-bookmark (article)
10168   "Remove the bookmark from the current article."
10169   (interactive (list (gnus-summary-article-number)))
10170   ;; Remove old bookmark, if one exists.
10171   (if (not (assq article gnus-newsgroup-bookmarks))
10172       (gnus-message 6 "No bookmark in current article.")
10173     (gnus-pull article gnus-newsgroup-bookmarks)
10174     (gnus-message 6 "Removed bookmark.")))
10175
10176 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10177 (defun gnus-summary-mark-as-dormant (n)
10178   "Mark N articles forward as dormant.
10179 If N is negative, mark backward instead.  The difference between N and
10180 the actual number of articles marked is returned."
10181   (interactive "p")
10182   (gnus-summary-mark-forward n gnus-dormant-mark))
10183
10184 (defun gnus-summary-set-process-mark (article)
10185   "Set the process mark on ARTICLE and update the summary line."
10186   (setq gnus-newsgroup-processable
10187         (cons article
10188               (delq article gnus-newsgroup-processable)))
10189   (when (gnus-summary-goto-subject article)
10190     (gnus-summary-show-thread)
10191     (gnus-summary-goto-subject article)
10192     (gnus-summary-update-secondary-mark article)))
10193
10194 (defun gnus-summary-remove-process-mark (&rest articles)
10195   "Remove the process mark from ARTICLES and update the summary line."
10196   (dolist (article articles)
10197     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10198     (when (gnus-summary-goto-subject article)
10199       (gnus-summary-show-thread)
10200       (gnus-summary-goto-subject article)
10201       (gnus-summary-update-secondary-mark article)))
10202   t)
10203
10204 (defun gnus-summary-set-saved-mark (article)
10205   "Set the process mark on ARTICLE and update the summary line."
10206   (push article gnus-newsgroup-saved)
10207   (when (gnus-summary-goto-subject article)
10208     (gnus-summary-update-secondary-mark article)))
10209
10210 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10211   "Mark N articles as read forwards.
10212 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10213 The difference between N and the actual number of articles marked is
10214 returned.
10215 If NO-EXPIRE, auto-expiry will be inhibited."
10216   (interactive "p")
10217   (gnus-summary-show-thread)
10218   (let ((backward (< n 0))
10219         (gnus-summary-goto-unread
10220          (and gnus-summary-goto-unread
10221               (not (eq gnus-summary-goto-unread 'never))
10222               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10223                                     gnus-ticked-mark gnus-dormant-mark)))))
10224         (n (abs n))
10225         (mark (or mark gnus-del-mark)))
10226     (while (and (> n 0)
10227                 (gnus-summary-mark-article nil mark no-expire)
10228                 (zerop (gnus-summary-next-subject
10229                         (if backward -1 1)
10230                         (and gnus-summary-goto-unread
10231                              (not (eq gnus-summary-goto-unread 'never)))
10232                         t)))
10233       (setq n (1- n)))
10234     (when (/= 0 n)
10235       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10236     (gnus-summary-recenter)
10237     (gnus-summary-position-point)
10238     (gnus-set-mode-line 'summary)
10239     n))
10240
10241 (defun gnus-summary-mark-article-as-read (mark)
10242   "Mark the current article quickly as read with MARK."
10243   (let ((article (gnus-summary-article-number)))
10244     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10245     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10246     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10247     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10248     (push (cons article mark) gnus-newsgroup-reads)
10249     ;; Possibly remove from cache, if that is used.
10250     (when gnus-use-cache
10251       (gnus-cache-enter-remove-article article))
10252     ;; Allow the backend to change the mark.
10253     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10254     ;; Check for auto-expiry.
10255     (when (and gnus-newsgroup-auto-expire
10256                (memq mark gnus-auto-expirable-marks))
10257       (setq mark gnus-expirable-mark)
10258       ;; Let the backend know about the mark change.
10259       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10260       (push article gnus-newsgroup-expirable))
10261     ;; Set the mark in the buffer.
10262     (gnus-summary-update-mark mark 'unread)
10263     t))
10264
10265 (defun gnus-summary-mark-article-as-unread (mark)
10266   "Mark the current article quickly as unread with MARK."
10267   (let* ((article (gnus-summary-article-number))
10268          (old-mark (gnus-summary-article-mark article)))
10269     ;; Allow the backend to change the mark.
10270     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10271     (if (eq mark old-mark)
10272         t
10273       (if (<= article 0)
10274           (progn
10275             (gnus-error 1 "Can't mark negative article numbers")
10276             nil)
10277         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10278         (setq gnus-newsgroup-spam-marked
10279               (delq article gnus-newsgroup-spam-marked))
10280         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10281         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10282         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10283         (cond ((= mark gnus-ticked-mark)
10284                (setq gnus-newsgroup-marked
10285                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10286                                               article)))
10287               ((= mark gnus-spam-mark)
10288                (setq gnus-newsgroup-spam-marked
10289                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10290                                               article)))
10291               ((= mark gnus-dormant-mark)
10292                (setq gnus-newsgroup-dormant
10293                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10294                                               article)))
10295               (t
10296                (setq gnus-newsgroup-unreads
10297                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10298                                               article))))
10299         (gnus-pull article gnus-newsgroup-reads)
10300
10301         ;; See whether the article is to be put in the cache.
10302         (and gnus-use-cache
10303              (vectorp (gnus-summary-article-header article))
10304              (save-excursion
10305                (gnus-cache-possibly-enter-article
10306                 gnus-newsgroup-name article
10307                 (= mark gnus-ticked-mark)
10308                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10309
10310         ;; Fix the mark.
10311         (gnus-summary-update-mark mark 'unread)
10312         t))))
10313
10314 (defun gnus-summary-mark-article (&optional article mark no-expire)
10315   "Mark ARTICLE with MARK.  MARK can be any character.
10316 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10317 `??' (dormant) and `?E' (expirable).
10318 If MARK is nil, then the default character `?r' is used.
10319 If ARTICLE is nil, then the article on the current line will be
10320 marked.
10321 If NO-EXPIRE, auto-expiry will be inhibited."
10322   ;; The mark might be a string.
10323   (when (stringp mark)
10324     (setq mark (aref mark 0)))
10325   ;; If no mark is given, then we check auto-expiring.
10326   (when (null mark)
10327     (setq mark gnus-del-mark))
10328   (when (and (not no-expire)
10329              gnus-newsgroup-auto-expire
10330              (memq mark gnus-auto-expirable-marks))
10331     (setq mark gnus-expirable-mark))
10332   (let ((article (or article (gnus-summary-article-number)))
10333         (old-mark (gnus-summary-article-mark article)))
10334     ;; Allow the backend to change the mark.
10335     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10336     (if (eq mark old-mark)
10337         t
10338       (unless article
10339         (error "No article on current line"))
10340       (if (not (if (or (= mark gnus-unread-mark)
10341                        (= mark gnus-ticked-mark)
10342                        (= mark gnus-spam-mark)
10343                        (= mark gnus-dormant-mark))
10344                    (gnus-mark-article-as-unread article mark)
10345                  (gnus-mark-article-as-read article mark)))
10346           t
10347         ;; See whether the article is to be put in the cache.
10348         (and gnus-use-cache
10349              (not (= mark gnus-canceled-mark))
10350              (vectorp (gnus-summary-article-header article))
10351              (save-excursion
10352                (gnus-cache-possibly-enter-article
10353                 gnus-newsgroup-name article
10354                 (= mark gnus-ticked-mark)
10355                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10356
10357         (when (gnus-summary-goto-subject article nil t)
10358           (let ((buffer-read-only nil))
10359             (gnus-summary-show-thread)
10360             ;; Fix the mark.
10361             (gnus-summary-update-mark mark 'unread)
10362             t))))))
10363
10364 (defun gnus-summary-update-secondary-mark (article)
10365   "Update the secondary (read, process, cache) mark."
10366   (gnus-summary-update-mark
10367    (cond ((memq article gnus-newsgroup-processable)
10368           gnus-process-mark)
10369          ((memq article gnus-newsgroup-cached)
10370           gnus-cached-mark)
10371          ((memq article gnus-newsgroup-replied)
10372           gnus-replied-mark)
10373          ((memq article gnus-newsgroup-forwarded)
10374           gnus-forwarded-mark)
10375          ((memq article gnus-newsgroup-saved)
10376           gnus-saved-mark)
10377          ((memq article gnus-newsgroup-recent)
10378           gnus-recent-mark)
10379          ((memq article gnus-newsgroup-unseen)
10380           gnus-unseen-mark)
10381          (t gnus-no-mark))
10382    'replied)
10383   (when (gnus-visual-p 'summary-highlight 'highlight)
10384     (gnus-run-hooks 'gnus-summary-update-hook))
10385   t)
10386
10387 (defun gnus-summary-update-download-mark (article)
10388   "Update the download mark."
10389   (gnus-summary-update-mark
10390    (cond ((memq article gnus-newsgroup-undownloaded)
10391           gnus-undownloaded-mark)
10392          (gnus-newsgroup-agentized
10393           gnus-downloaded-mark)
10394          (t
10395           gnus-no-mark))
10396    'download)
10397   (gnus-summary-update-line t)
10398   t)
10399
10400 (defun gnus-summary-update-mark (mark type)
10401   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10402         (buffer-read-only nil))
10403     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10404     (when forward
10405       (when (looking-at "\r")
10406         (incf forward))
10407       (when (<= (+ forward (point)) (point-max))
10408         ;; Go to the right position on the line.
10409         (goto-char (+ forward (point)))
10410         ;; Replace the old mark with the new mark.
10411         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10412         ;; Optionally update the marks by some user rule.
10413         (when (eq type 'unread)
10414           (gnus-data-set-mark
10415            (gnus-data-find (gnus-summary-article-number)) mark)
10416           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10417
10418 (defun gnus-mark-article-as-read (article &optional mark)
10419   "Enter ARTICLE in the pertinent lists and remove it from others."
10420   ;; Make the article expirable.
10421   (let ((mark (or mark gnus-del-mark)))
10422     (setq gnus-newsgroup-expirable
10423           (if (= mark gnus-expirable-mark)
10424               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10425             (delq article gnus-newsgroup-expirable)))
10426     ;; Remove from unread and marked lists.
10427     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10428     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10429     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10430     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10431     (push (cons article mark) gnus-newsgroup-reads)
10432     ;; Possibly remove from cache, if that is used.
10433     (when gnus-use-cache
10434       (gnus-cache-enter-remove-article article))
10435     t))
10436
10437 (defun gnus-mark-article-as-unread (article &optional mark)
10438   "Enter ARTICLE in the pertinent lists and remove it from others."
10439   (let ((mark (or mark gnus-ticked-mark)))
10440     (if (<= article 0)
10441         (progn
10442           (gnus-error 1 "Can't mark negative article numbers")
10443           nil)
10444       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10445             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10446             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10447             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10448             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10449
10450       ;; Unsuppress duplicates?
10451       (when gnus-suppress-duplicates
10452         (gnus-dup-unsuppress-article article))
10453
10454       (cond ((= mark gnus-ticked-mark)
10455              (setq gnus-newsgroup-marked
10456                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10457             ((= mark gnus-spam-mark)
10458              (setq gnus-newsgroup-spam-marked
10459                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10460                                             article)))
10461             ((= mark gnus-dormant-mark)
10462              (setq gnus-newsgroup-dormant
10463                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10464             (t
10465              (setq gnus-newsgroup-unreads
10466                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10467       (gnus-pull article gnus-newsgroup-reads)
10468       t)))
10469
10470 (defalias 'gnus-summary-mark-as-unread-forward
10471   'gnus-summary-tick-article-forward)
10472 (make-obsolete 'gnus-summary-mark-as-unread-forward
10473                'gnus-summary-tick-article-forward)
10474 (defun gnus-summary-tick-article-forward (n)
10475   "Tick N articles forwards.
10476 If N is negative, tick backwards instead.
10477 The difference between N and the number of articles ticked is returned."
10478   (interactive "p")
10479   (gnus-summary-mark-forward n gnus-ticked-mark))
10480
10481 (defalias 'gnus-summary-mark-as-unread-backward
10482   'gnus-summary-tick-article-backward)
10483 (make-obsolete 'gnus-summary-mark-as-unread-backward
10484                'gnus-summary-tick-article-backward)
10485 (defun gnus-summary-tick-article-backward (n)
10486   "Tick N articles backwards.
10487 The difference between N and the number of articles ticked is returned."
10488   (interactive "p")
10489   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10490
10491 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10492 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10493 (defun gnus-summary-tick-article (&optional article clear-mark)
10494   "Mark current article as unread.
10495 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10496 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10497   (interactive)
10498   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10499                                        gnus-ticked-mark)))
10500
10501 (defun gnus-summary-mark-as-read-forward (n)
10502   "Mark N articles as read forwards.
10503 If N is negative, mark backwards instead.
10504 The difference between N and the actual number of articles marked is
10505 returned."
10506   (interactive "p")
10507   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10508
10509 (defun gnus-summary-mark-as-read-backward (n)
10510   "Mark the N articles as read backwards.
10511 The difference between N and the actual number of articles marked is
10512 returned."
10513   (interactive "p")
10514   (gnus-summary-mark-forward
10515    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10516
10517 (defun gnus-summary-mark-as-read (&optional article mark)
10518   "Mark current article as read.
10519 ARTICLE specifies the article to be marked as read.
10520 MARK specifies a string to be inserted at the beginning of the line."
10521   (gnus-summary-mark-article article mark))
10522
10523 (defun gnus-summary-clear-mark-forward (n)
10524   "Clear marks from N articles forward.
10525 If N is negative, clear backward instead.
10526 The difference between N and the number of marks cleared is returned."
10527   (interactive "p")
10528   (gnus-summary-mark-forward n gnus-unread-mark))
10529
10530 (defun gnus-summary-clear-mark-backward (n)
10531   "Clear marks from N articles backward.
10532 The difference between N and the number of marks cleared is returned."
10533   (interactive "p")
10534   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10535
10536 (defun gnus-summary-mark-unread-as-read ()
10537   "Intended to be used by `gnus-summary-mark-article-hook'."
10538   (when (memq gnus-current-article gnus-newsgroup-unreads)
10539     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10540
10541 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10542   "Intended to be used by `gnus-summary-mark-article-hook'."
10543   (let ((mark (gnus-summary-article-mark)))
10544     (when (or (gnus-unread-mark-p mark)
10545               (gnus-read-mark-p mark))
10546       (gnus-summary-mark-article gnus-current-article
10547                                  (or new-mark gnus-read-mark)))))
10548
10549 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10550   "Intended to be used by `gnus-summary-mark-article-hook'."
10551   (let ((mark (gnus-summary-article-mark)))
10552     (when (or (gnus-unread-mark-p mark)
10553               (gnus-read-mark-p mark))
10554       (gnus-summary-mark-article (gnus-summary-article-number)
10555                                  (or new-mark gnus-read-mark)))))
10556
10557 (defun gnus-summary-mark-unread-as-ticked ()
10558   "Intended to be used by `gnus-summary-mark-article-hook'."
10559   (when (memq gnus-current-article gnus-newsgroup-unreads)
10560     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10561
10562 (defun gnus-summary-mark-region-as-read (point mark all)
10563   "Mark all unread articles between point and mark as read.
10564 If given a prefix, mark all articles between point and mark as read,
10565 even ticked and dormant ones."
10566   (interactive "r\nP")
10567   (save-excursion
10568     (let (article)
10569       (goto-char point)
10570       (beginning-of-line)
10571       (while (and
10572               (< (point) mark)
10573               (progn
10574                 (when (or all
10575                           (memq (setq article (gnus-summary-article-number))
10576                                 gnus-newsgroup-unreads))
10577                   (gnus-summary-mark-article article gnus-del-mark))
10578                 t)
10579               (gnus-summary-find-next))))))
10580
10581 (defun gnus-summary-mark-below (score mark)
10582   "Mark articles with score less than SCORE with MARK."
10583   (interactive "P\ncMark: ")
10584   (setq score (if score
10585                   (prefix-numeric-value score)
10586                 (or gnus-summary-default-score 0)))
10587   (save-excursion
10588     (set-buffer gnus-summary-buffer)
10589     (goto-char (point-min))
10590     (while
10591         (progn
10592           (and (< (gnus-summary-article-score) score)
10593                (gnus-summary-mark-article nil mark))
10594           (gnus-summary-find-next)))))
10595
10596 (defun gnus-summary-kill-below (&optional score)
10597   "Mark articles with score below SCORE as read."
10598   (interactive "P")
10599   (gnus-summary-mark-below score gnus-killed-mark))
10600
10601 (defun gnus-summary-clear-above (&optional score)
10602   "Clear all marks from articles with score above SCORE."
10603   (interactive "P")
10604   (gnus-summary-mark-above score gnus-unread-mark))
10605
10606 (defun gnus-summary-tick-above (&optional score)
10607   "Tick all articles with score above SCORE."
10608   (interactive "P")
10609   (gnus-summary-mark-above score gnus-ticked-mark))
10610
10611 (defun gnus-summary-mark-above (score mark)
10612   "Mark articles with score over SCORE with MARK."
10613   (interactive "P\ncMark: ")
10614   (setq score (if score
10615                   (prefix-numeric-value score)
10616                 (or gnus-summary-default-score 0)))
10617   (save-excursion
10618     (set-buffer gnus-summary-buffer)
10619     (goto-char (point-min))
10620     (while (and (progn
10621                   (when (> (gnus-summary-article-score) score)
10622                     (gnus-summary-mark-article nil mark))
10623                   t)
10624                 (gnus-summary-find-next)))))
10625
10626 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10627 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10628 (defun gnus-summary-limit-include-expunged (&optional no-error)
10629   "Display all the hidden articles that were expunged for low scores."
10630   (interactive)
10631   (let ((buffer-read-only nil))
10632     (let ((scored gnus-newsgroup-scored)
10633           headers h)
10634       (while scored
10635         (unless (gnus-summary-article-header (caar scored))
10636           (and (setq h (gnus-number-to-header (caar scored)))
10637                (< (cdar scored) gnus-summary-expunge-below)
10638                (push h headers)))
10639         (setq scored (cdr scored)))
10640       (if (not headers)
10641           (when (not no-error)
10642             (error "No expunged articles hidden"))
10643         (goto-char (point-min))
10644         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10645         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10646         (mapcar (lambda (x) (push (mail-header-number x)
10647                                   gnus-newsgroup-limit))
10648                 headers)
10649         (gnus-summary-prepare-unthreaded (nreverse headers))
10650         (goto-char (point-min))
10651         (gnus-summary-position-point)
10652         t))))
10653
10654 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10655   "Mark all unread articles in this newsgroup as read.
10656 If prefix argument ALL is non-nil, ticked and dormant articles will
10657 also be marked as read.
10658 If QUIETLY is non-nil, no questions will be asked.
10659
10660 If TO-HERE is non-nil, it should be a point in the buffer.  All
10661 articles before (after, if REVERSE is set) this point will be marked
10662 as read.
10663
10664 Note that this function will only catch up the unread article
10665 in the current summary buffer limitation.
10666
10667 The number of articles marked as read is returned."
10668   (interactive "P")
10669   (prog1
10670       (save-excursion
10671         (when (or quietly
10672                   (not gnus-interactive-catchup) ;Without confirmation?
10673                   gnus-expert-user
10674                   (gnus-y-or-n-p
10675                    (if all
10676                        "Mark absolutely all articles as read? "
10677                      "Mark all unread articles as read? ")))
10678           (if (and not-mark
10679                    (not gnus-newsgroup-adaptive)
10680                    (not gnus-newsgroup-auto-expire)
10681                    (not gnus-suppress-duplicates)
10682                    (or (not gnus-use-cache)
10683                        (eq gnus-use-cache 'passive)))
10684               (progn
10685                 (when all
10686                   (setq gnus-newsgroup-marked nil
10687                         gnus-newsgroup-spam-marked nil
10688                         gnus-newsgroup-dormant nil))
10689                 (setq gnus-newsgroup-unreads
10690                       (gnus-sorted-nunion
10691                        (gnus-intersection gnus-newsgroup-unreads
10692                                           gnus-newsgroup-downloadable)
10693                        gnus-newsgroup-unfetched)))
10694             ;; We actually mark all articles as canceled, which we
10695             ;; have to do when using auto-expiry or adaptive scoring.
10696             (gnus-summary-show-all-threads)
10697             (if (and to-here reverse)
10698                 (progn
10699                   (goto-char to-here)
10700                   (gnus-summary-mark-current-read-and-unread-as-read
10701                    gnus-catchup-mark)
10702                   (while (gnus-summary-find-next (not all))
10703                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10704               (when (gnus-summary-first-subject (not all))
10705                 (while (and
10706                         (if to-here (< (point) to-here) t)
10707                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10708                         (gnus-summary-find-next (not all))))))
10709             (gnus-set-mode-line 'summary))
10710           t))
10711     (gnus-summary-position-point)))
10712
10713 (defun gnus-summary-catchup-to-here (&optional all)
10714   "Mark all unticked articles before the current one as read.
10715 If ALL is non-nil, also mark ticked and dormant articles as read."
10716   (interactive "P")
10717   (save-excursion
10718     (gnus-save-hidden-threads
10719       (let ((beg (point)))
10720         ;; We check that there are unread articles.
10721         (when (or all (gnus-summary-find-prev))
10722           (gnus-summary-catchup all t beg)))))
10723   (gnus-summary-position-point))
10724
10725 (defun gnus-summary-catchup-from-here (&optional all)
10726   "Mark all unticked articles after (and including) the current one as read.
10727 If ALL is non-nil, also mark ticked and dormant articles as read."
10728   (interactive "P")
10729   (save-excursion
10730     (gnus-save-hidden-threads
10731       (let ((beg (point)))
10732         ;; We check that there are unread articles.
10733         (when (or all (gnus-summary-find-next))
10734           (gnus-summary-catchup all t beg nil t)))))
10735   (gnus-summary-position-point))
10736
10737 (defun gnus-summary-catchup-all (&optional quietly)
10738   "Mark all articles in this newsgroup as read.
10739 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10740 instead, which marks only unread articles as read."
10741   (interactive "P")
10742   (gnus-summary-catchup t quietly))
10743
10744 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10745   "Mark all unread articles in this group as read, then exit.
10746 If prefix argument ALL is non-nil, all articles are marked as read.
10747 If QUIETLY is non-nil, no questions will be asked."
10748   (interactive "P")
10749   (when (gnus-summary-catchup all quietly nil 'fast)
10750     ;; Select next newsgroup or exit.
10751     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10752              (eq gnus-auto-select-next 'quietly))
10753         (gnus-summary-next-group nil)
10754       (gnus-summary-exit))))
10755
10756 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10757   "Mark all articles in this newsgroup as read, and then exit.
10758 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10759 instead, which marks only unread articles as read."
10760   (interactive "P")
10761   (gnus-summary-catchup-and-exit t quietly))
10762
10763 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10764   "Mark all articles in this group as read and select the next group.
10765 If given a prefix, mark all articles, unread as well as ticked, as
10766 read."
10767   (interactive "P")
10768   (save-excursion
10769     (gnus-summary-catchup all))
10770   (gnus-summary-next-group))
10771
10772 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
10773   "Mark all articles in this group as read and select the previous group.
10774 If given a prefix, mark all articles, unread as well as ticked, as
10775 read."
10776   (interactive "P")
10777   (save-excursion
10778     (gnus-summary-catchup all))
10779   (gnus-summary-next-group nil nil t))
10780
10781 ;;;
10782 ;;; with article
10783 ;;;
10784
10785 (defmacro gnus-with-article (article &rest forms)
10786   "Select ARTICLE and perform FORMS in the original article buffer.
10787 Then replace the article with the result."
10788   `(progn
10789      ;; We don't want the article to be marked as read.
10790      (let (gnus-mark-article-hook)
10791        (gnus-summary-select-article t t nil ,article))
10792      (set-buffer gnus-original-article-buffer)
10793      ,@forms
10794      (if (not (gnus-check-backend-function
10795                'request-replace-article (car gnus-article-current)))
10796          (gnus-message 5 "Read-only group; not replacing")
10797        (unless (gnus-request-replace-article
10798                 ,article (car gnus-article-current)
10799                 (current-buffer) t)
10800          (error "Couldn't replace article")))
10801      ;; The cache and backlog have to be flushed somewhat.
10802      (when gnus-keep-backlog
10803        (gnus-backlog-remove-article
10804         (car gnus-article-current) (cdr gnus-article-current)))
10805      (when gnus-use-cache
10806        (gnus-cache-update-article
10807         (car gnus-article-current) (cdr gnus-article-current)))))
10808
10809 (put 'gnus-with-article 'lisp-indent-function 1)
10810 (put 'gnus-with-article 'edebug-form-spec '(form body))
10811
10812 ;; Thread-based commands.
10813
10814 (defun gnus-summary-articles-in-thread (&optional article)
10815   "Return a list of all articles in the current thread.
10816 If ARTICLE is non-nil, return all articles in the thread that starts
10817 with that article."
10818   (let* ((article (or article (gnus-summary-article-number)))
10819          (data (gnus-data-find-list article))
10820          (top-level (gnus-data-level (car data)))
10821          (top-subject
10822           (cond ((null gnus-thread-operation-ignore-subject)
10823                  (gnus-simplify-subject-re
10824                   (mail-header-subject (gnus-data-header (car data)))))
10825                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10826                  (gnus-simplify-subject-fuzzy
10827                   (mail-header-subject (gnus-data-header (car data)))))
10828                 (t nil)))
10829          (end-point (save-excursion
10830                       (if (gnus-summary-go-to-next-thread)
10831                           (point) (point-max))))
10832          articles)
10833     (while (and data
10834                 (< (gnus-data-pos (car data)) end-point))
10835       (when (or (not top-subject)
10836                 (string= top-subject
10837                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10838                              (gnus-simplify-subject-fuzzy
10839                               (mail-header-subject
10840                                (gnus-data-header (car data))))
10841                            (gnus-simplify-subject-re
10842                             (mail-header-subject
10843                              (gnus-data-header (car data)))))))
10844         (push (gnus-data-number (car data)) articles))
10845       (unless (and (setq data (cdr data))
10846                    (> (gnus-data-level (car data)) top-level))
10847         (setq data nil)))
10848     ;; Return the list of articles.
10849     (nreverse articles)))
10850
10851 (defun gnus-summary-rethread-current ()
10852   "Rethread the thread the current article is part of."
10853   (interactive)
10854   (let* ((gnus-show-threads t)
10855          (article (gnus-summary-article-number))
10856          (id (mail-header-id (gnus-summary-article-header)))
10857          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10858     (unless id
10859       (error "No article on the current line"))
10860     (gnus-rebuild-thread id)
10861     (gnus-summary-goto-subject article)))
10862
10863 (defun gnus-summary-reparent-thread ()
10864   "Make the current article child of the marked (or previous) article.
10865
10866 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10867 is non-nil or the Subject: of both articles are the same."
10868   (interactive)
10869   (unless (not (gnus-group-read-only-p))
10870     (error "The current newsgroup does not support article editing"))
10871   (unless (<= (length gnus-newsgroup-processable) 1)
10872     (error "No more than one article may be marked"))
10873   (let ((child (gnus-summary-article-number))
10874         ;; First grab the marked article, otherwise one line up.
10875         (parent (if (not (null gnus-newsgroup-processable))
10876                     (car gnus-newsgroup-processable)
10877                   (save-excursion
10878                     (if (eq (forward-line -1) 0)
10879                         (gnus-summary-article-number)
10880                       (error "Beginning of summary buffer"))))))
10881     (gnus-summary-reparent-children parent (list child))))
10882
10883 (defun gnus-summary-reparent-children (parent children)
10884   "Make PARENT the parent of CHILDREN.
10885 When called interactively, PARENT is is current article and
10886 CHILDREN are the process-marked articles."
10887   (interactive
10888    (list (gnus-summary-article-number)
10889          (gnus-summary-work-articles nil)))
10890   (dolist (child children)
10891     (save-window-excursion
10892       (let ((gnus-article-buffer " *reparent*"))
10893         (unless (not (eq parent child))
10894           (error "An article may not be self-referential"))
10895         (let ((message-id (mail-header-id
10896                            (gnus-summary-article-header parent))))
10897           (unless (and message-id (not (equal message-id "")))
10898             (error "No message-id in desired parent"))
10899           (gnus-with-article child
10900             (save-restriction
10901               (goto-char (point-min))
10902               (message-narrow-to-head)
10903               (if (re-search-forward "^References: " nil t)
10904                   (progn
10905                     (re-search-forward "^[^ \t]" nil t)
10906                     (forward-line -1)
10907                     (end-of-line)
10908                     (insert " " message-id))
10909                 (insert "References: " message-id "\n"))))
10910           (set-buffer gnus-summary-buffer)
10911           (gnus-summary-unmark-all-processable)
10912           (gnus-summary-update-article child)
10913           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10914             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10915           (gnus-summary-rethread-current)
10916           (gnus-message 3 "Article %d is now the child of article %d"
10917                         child parent))))))
10918
10919 (defun gnus-summary-toggle-threads (&optional arg)
10920   "Toggle showing conversation threads.
10921 If ARG is positive number, turn showing conversation threads on."
10922   (interactive "P")
10923   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10924     (setq gnus-show-threads
10925           (if (null arg) (not gnus-show-threads)
10926             (> (prefix-numeric-value arg) 0)))
10927     (gnus-summary-prepare)
10928     (gnus-summary-goto-subject current)
10929     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10930     (gnus-summary-position-point)))
10931
10932 (defun gnus-summary-show-all-threads ()
10933   "Show all threads."
10934   (interactive)
10935   (save-excursion
10936     (let ((buffer-read-only nil))
10937       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10938   (gnus-summary-position-point))
10939
10940 (defun gnus-summary-show-thread ()
10941   "Show thread subtrees.
10942 Returns nil if no thread was there to be shown."
10943   (interactive)
10944   (let ((buffer-read-only nil)
10945         (orig (point))
10946         (end (point-at-eol))
10947         ;; Leave point at bol
10948         (beg (progn (beginning-of-line) (point))))
10949     (prog1
10950         ;; Any hidden lines here?
10951         (search-forward "\r" end t)
10952       (subst-char-in-region beg end ?\^M ?\n t)
10953       (goto-char orig)
10954       (gnus-summary-position-point))))
10955
10956 (defun gnus-summary-maybe-hide-threads ()
10957   "If requested, hide the threads that should be hidden."
10958   (when (and gnus-show-threads
10959              gnus-thread-hide-subtree)
10960     (gnus-summary-hide-all-threads
10961      (if (or (consp gnus-thread-hide-subtree)
10962              (functionp gnus-thread-hide-subtree))
10963          (gnus-make-predicate gnus-thread-hide-subtree)
10964        nil))))
10965
10966 ;;; Hiding predicates.
10967
10968 (defun gnus-article-unread-p (header)
10969   (memq (mail-header-number header) gnus-newsgroup-unreads))
10970
10971 (defun gnus-article-unseen-p (header)
10972   (memq (mail-header-number header) gnus-newsgroup-unseen))
10973
10974 (defun gnus-map-articles (predicate articles)
10975   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10976   (apply 'gnus-or (mapcar predicate
10977                           (mapcar (lambda (number)
10978                                     (gnus-summary-article-header number))
10979                                   articles))))
10980
10981 (defun gnus-summary-hide-all-threads (&optional predicate)
10982   "Hide all thread subtrees.
10983 If PREDICATE is supplied, threads that satisfy this predicate
10984 will not be hidden."
10985   (interactive)
10986   (save-excursion
10987     (goto-char (point-min))
10988     (let ((end nil))
10989       (while (not end)
10990         (when (or (not predicate)
10991                   (gnus-map-articles
10992                    predicate (gnus-summary-article-children)))
10993             (gnus-summary-hide-thread))
10994         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10995   (gnus-summary-position-point))
10996
10997 (defun gnus-summary-hide-thread ()
10998   "Hide thread subtrees.
10999 If PREDICATE is supplied, threads that satisfy this predicate
11000 will not be hidden.
11001 Returns nil if no threads were there to be hidden."
11002   (interactive)
11003   (let ((buffer-read-only nil)
11004         (start (point))
11005         (article (gnus-summary-article-number)))
11006     (goto-char start)
11007     ;; Go forward until either the buffer ends or the subthread
11008     ;; ends.
11009     (when (and (not (eobp))
11010                (or (zerop (gnus-summary-next-thread 1 t))
11011                    (goto-char (point-max))))
11012       (prog1
11013           (if (and (> (point) start)
11014                    (search-backward "\n" start t))
11015               (progn
11016                 (subst-char-in-region start (point) ?\n ?\^M)
11017                 (gnus-summary-goto-subject article))
11018             (goto-char start)
11019             nil)))))
11020
11021 (defun gnus-summary-go-to-next-thread (&optional previous)
11022   "Go to the same level (or less) next thread.
11023 If PREVIOUS is non-nil, go to previous thread instead.
11024 Return the article number moved to, or nil if moving was impossible."
11025   (let ((level (gnus-summary-thread-level))
11026         (way (if previous -1 1))
11027         (beg (point)))
11028     (forward-line way)
11029     (while (and (not (eobp))
11030                 (< level (gnus-summary-thread-level)))
11031       (forward-line way))
11032     (if (eobp)
11033         (progn
11034           (goto-char beg)
11035           nil)
11036       (setq beg (point))
11037       (prog1
11038           (gnus-summary-article-number)
11039         (goto-char beg)))))
11040
11041 (defun gnus-summary-next-thread (n &optional silent)
11042   "Go to the same level next N'th thread.
11043 If N is negative, search backward instead.
11044 Returns the difference between N and the number of skips actually
11045 done.
11046
11047 If SILENT, don't output messages."
11048   (interactive "p")
11049   (let ((backward (< n 0))
11050         (n (abs n)))
11051     (while (and (> n 0)
11052                 (gnus-summary-go-to-next-thread backward))
11053       (decf n))
11054     (unless silent
11055       (gnus-summary-position-point))
11056     (when (and (not silent) (/= 0 n))
11057       (gnus-message 7 "No more threads"))
11058     n))
11059
11060 (defun gnus-summary-prev-thread (n)
11061   "Go to the same level previous N'th thread.
11062 Returns the difference between N and the number of skips actually
11063 done."
11064   (interactive "p")
11065   (gnus-summary-next-thread (- n)))
11066
11067 (defun gnus-summary-go-down-thread ()
11068   "Go down one level in the current thread."
11069   (let ((children (gnus-summary-article-children)))
11070     (when children
11071       (gnus-summary-goto-subject (car children)))))
11072
11073 (defun gnus-summary-go-up-thread ()
11074   "Go up one level in the current thread."
11075   (let ((parent (gnus-summary-article-parent)))
11076     (when parent
11077       (gnus-summary-goto-subject parent))))
11078
11079 (defun gnus-summary-down-thread (n)
11080   "Go down thread N steps.
11081 If N is negative, go up instead.
11082 Returns the difference between N and how many steps down that were
11083 taken."
11084   (interactive "p")
11085   (let ((up (< n 0))
11086         (n (abs n)))
11087     (while (and (> n 0)
11088                 (if up (gnus-summary-go-up-thread)
11089                   (gnus-summary-go-down-thread)))
11090       (setq n (1- n)))
11091     (gnus-summary-position-point)
11092     (when (/= 0 n)
11093       (gnus-message 7 "Can't go further"))
11094     n))
11095
11096 (defun gnus-summary-up-thread (n)
11097   "Go up thread N steps.
11098 If N is negative, go down instead.
11099 Returns the difference between N and how many steps down that were
11100 taken."
11101   (interactive "p")
11102   (gnus-summary-down-thread (- n)))
11103
11104 (defun gnus-summary-top-thread ()
11105   "Go to the top of the thread."
11106   (interactive)
11107   (while (gnus-summary-go-up-thread))
11108   (gnus-summary-article-number))
11109
11110 (defun gnus-summary-kill-thread (&optional unmark)
11111   "Mark articles under current thread as read.
11112 If the prefix argument is positive, remove any kinds of marks.
11113 If the prefix argument is negative, tick articles instead."
11114   (interactive "P")
11115   (when unmark
11116     (setq unmark (prefix-numeric-value unmark)))
11117   (let ((articles (gnus-summary-articles-in-thread)))
11118     (save-excursion
11119       ;; Expand the thread.
11120       (gnus-summary-show-thread)
11121       ;; Mark all the articles.
11122       (while articles
11123         (gnus-summary-goto-subject (car articles))
11124         (cond ((null unmark)
11125                (gnus-summary-mark-article-as-read gnus-killed-mark))
11126               ((> unmark 0)
11127                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11128               (t
11129                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11130         (setq articles (cdr articles))))
11131     ;; Hide killed subtrees.
11132     (and (null unmark)
11133          gnus-thread-hide-killed
11134          (gnus-summary-hide-thread))
11135     ;; If marked as read, go to next unread subject.
11136     (when (null unmark)
11137       ;; Go to next unread subject.
11138       (gnus-summary-next-subject 1 t)))
11139   (gnus-set-mode-line 'summary))
11140
11141 ;; Summary sorting commands
11142
11143 (defun gnus-summary-sort-by-number (&optional reverse)
11144   "Sort the summary buffer by article number.
11145 Argument REVERSE means reverse order."
11146   (interactive "P")
11147   (gnus-summary-sort 'number reverse))
11148
11149 (defun gnus-summary-sort-by-random (&optional reverse)
11150   "Randomize the order in the summary buffer.
11151 Argument REVERSE means to randomize in reverse order."
11152   (interactive "P")
11153   (gnus-summary-sort 'random reverse))
11154
11155 (defun gnus-summary-sort-by-author (&optional reverse)
11156   "Sort the summary buffer by author name alphabetically.
11157 If `case-fold-search' is non-nil, case of letters is ignored.
11158 Argument REVERSE means reverse order."
11159   (interactive "P")
11160   (gnus-summary-sort 'author reverse))
11161
11162 (defun gnus-summary-sort-by-recipient (&optional reverse)
11163   "Sort the summary buffer by recipient name alphabetically.
11164 If `case-fold-search' is non-nil, case of letters is ignored.
11165 Argument REVERSE means reverse order."
11166   (interactive "P")
11167   (gnus-summary-sort 'recipient reverse))
11168
11169 (defun gnus-summary-sort-by-subject (&optional reverse)
11170   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11171 If `case-fold-search' is non-nil, case of letters is ignored.
11172 Argument REVERSE means reverse order."
11173   (interactive "P")
11174   (gnus-summary-sort 'subject reverse))
11175
11176 (defun gnus-summary-sort-by-date (&optional reverse)
11177   "Sort the summary buffer by date.
11178 Argument REVERSE means reverse order."
11179   (interactive "P")
11180   (gnus-summary-sort 'date reverse))
11181
11182 (defun gnus-summary-sort-by-score (&optional reverse)
11183   "Sort the summary buffer by score.
11184 Argument REVERSE means reverse order."
11185   (interactive "P")
11186   (gnus-summary-sort 'score reverse))
11187
11188 (defun gnus-summary-sort-by-lines (&optional reverse)
11189   "Sort the summary buffer by the number of lines.
11190 Argument REVERSE means reverse order."
11191   (interactive "P")
11192   (gnus-summary-sort 'lines reverse))
11193
11194 (defun gnus-summary-sort-by-chars (&optional reverse)
11195   "Sort the summary buffer by article length.
11196 Argument REVERSE means reverse order."
11197   (interactive "P")
11198   (gnus-summary-sort 'chars reverse))
11199
11200 (defun gnus-summary-sort-by-original (&optional reverse)
11201   "Sort the summary buffer using the default sorting method.
11202 Argument REVERSE means reverse order."
11203   (interactive "P")
11204   (let* ((buffer-read-only)
11205          (gnus-summary-prepare-hook nil))
11206     ;; We do the sorting by regenerating the threads.
11207     (gnus-summary-prepare)
11208     ;; Hide subthreads if needed.
11209     (gnus-summary-maybe-hide-threads)))
11210
11211 (defun gnus-summary-sort (predicate reverse)
11212   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11213   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11214          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11215          (gnus-thread-sort-functions
11216           (if (not reverse)
11217               thread
11218             `(lambda (t1 t2)
11219                (,thread t2 t1))))
11220          (gnus-sort-gathered-threads-function
11221           gnus-thread-sort-functions)
11222          (gnus-article-sort-functions
11223           (if (not reverse)
11224               article
11225             `(lambda (t1 t2)
11226                (,article t2 t1))))
11227          (buffer-read-only)
11228          (gnus-summary-prepare-hook nil))
11229     ;; We do the sorting by regenerating the threads.
11230     (gnus-summary-prepare)
11231     ;; Hide subthreads if needed.
11232     (gnus-summary-maybe-hide-threads)))
11233
11234 ;; Summary saving commands.
11235
11236 (defun gnus-summary-save-article (&optional n not-saved)
11237   "Save the current article using the default saver function.
11238 If N is a positive number, save the N next articles.
11239 If N is a negative number, save the N previous articles.
11240 If N is nil and any articles have been marked with the process mark,
11241 save those articles instead.
11242 The variable `gnus-default-article-saver' specifies the saver function."
11243   (interactive "P")
11244   (let* ((articles (gnus-summary-work-articles n))
11245          (save-buffer (save-excursion
11246                         (nnheader-set-temp-buffer " *Gnus Save*")))
11247          (num (length articles))
11248          header file)
11249     (dolist (article articles)
11250       (setq header (gnus-summary-article-header article))
11251       (if (not (vectorp header))
11252           ;; This is a pseudo-article.
11253           (if (assq 'name header)
11254               (gnus-copy-file (cdr (assq 'name header)))
11255             (gnus-message 1 "Article %d is unsaveable" article))
11256         ;; This is a real article.
11257         (save-window-excursion
11258           (let ((gnus-display-mime-function nil)
11259                 (gnus-article-prepare-hook nil))
11260             (gnus-summary-select-article t nil nil article)))
11261         (save-excursion
11262           (set-buffer save-buffer)
11263           (erase-buffer)
11264           (insert-buffer-substring gnus-original-article-buffer))
11265         (setq file (gnus-article-save save-buffer file num))
11266         (gnus-summary-remove-process-mark article)
11267         (unless not-saved
11268           (gnus-summary-set-saved-mark article))))
11269     (gnus-kill-buffer save-buffer)
11270     (gnus-summary-position-point)
11271     (gnus-set-mode-line 'summary)
11272     n))
11273
11274 (defun gnus-summary-pipe-output (&optional arg headers)
11275   "Pipe the current article to a subprocess.
11276 If N is a positive number, pipe the N next articles.
11277 If N is a negative number, pipe the N previous articles.
11278 If N is nil and any articles have been marked with the process mark,
11279 pipe those articles instead.
11280 If HEADERS (the symbolic prefix), include the headers, too."
11281   (interactive (gnus-interactive "P\ny"))
11282   (require 'gnus-art)
11283   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11284         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11285     (gnus-summary-save-article arg t))
11286   (let ((buffer (get-buffer "*Shell Command Output*")))
11287     (when (and buffer
11288                (not (zerop (buffer-size buffer))))
11289       (gnus-configure-windows 'pipe))))
11290
11291 (defun gnus-summary-save-article-mail (&optional arg)
11292   "Append the current article to an mail file.
11293 If N is a positive number, save the N next articles.
11294 If N is a negative number, save the N previous articles.
11295 If N is nil and any articles have been marked with the process mark,
11296 save those articles instead."
11297   (interactive "P")
11298   (require 'gnus-art)
11299   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11300     (gnus-summary-save-article arg)))
11301
11302 (defun gnus-summary-save-article-rmail (&optional arg)
11303   "Append the current article to an rmail file.
11304 If N is a positive number, save the N next articles.
11305 If N is a negative number, save the N previous articles.
11306 If N is nil and any articles have been marked with the process mark,
11307 save those articles instead."
11308   (interactive "P")
11309   (require 'gnus-art)
11310   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11311     (gnus-summary-save-article arg)))
11312
11313 (defun gnus-summary-save-article-file (&optional arg)
11314   "Append the current article to a file.
11315 If N is a positive number, save the N next articles.
11316 If N is a negative number, save the N previous articles.
11317 If N is nil and any articles have been marked with the process mark,
11318 save those articles instead."
11319   (interactive "P")
11320   (require 'gnus-art)
11321   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11322     (gnus-summary-save-article arg)))
11323
11324 (defun gnus-summary-write-article-file (&optional arg)
11325   "Write the current article to a file, deleting the previous file.
11326 If N is a positive number, save the N next articles.
11327 If N is a negative number, save the N previous articles.
11328 If N is nil and any articles have been marked with the process mark,
11329 save those articles instead."
11330   (interactive "P")
11331   (require 'gnus-art)
11332   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11333     (gnus-summary-save-article arg)))
11334
11335 (defun gnus-summary-save-article-body-file (&optional arg)
11336   "Append the current article body to a file.
11337 If N is a positive number, save the N next articles.
11338 If N is a negative number, save the N previous articles.
11339 If N is nil and any articles have been marked with the process mark,
11340 save those articles instead."
11341   (interactive "P")
11342   (require 'gnus-art)
11343   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11344     (gnus-summary-save-article arg)))
11345
11346 (defun gnus-summary-muttprint (&optional arg)
11347   "Print the current article using Muttprint.
11348 If N is a positive number, save the N next articles.
11349 If N is a negative number, save the N previous articles.
11350 If N is nil and any articles have been marked with the process mark,
11351 save those articles instead."
11352   (interactive "P")
11353   (require 'gnus-art)
11354   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11355     (gnus-summary-save-article arg t)))
11356
11357 (defun gnus-summary-pipe-message (program)
11358   "Pipe the current article through PROGRAM."
11359   (interactive "sProgram: ")
11360   (gnus-summary-select-article)
11361   (let ((mail-header-separator ""))
11362     (gnus-eval-in-buffer-window gnus-article-buffer
11363       (save-restriction
11364         (widen)
11365         (let ((start (window-start))
11366               buffer-read-only)
11367           (message-pipe-buffer-body program)
11368           (set-window-start (get-buffer-window (current-buffer)) start))))))
11369
11370 (defun gnus-get-split-value (methods)
11371   "Return a value based on the split METHODS."
11372   (let (split-name method result match)
11373     (when methods
11374       (save-excursion
11375         (set-buffer gnus-original-article-buffer)
11376         (save-restriction
11377           (nnheader-narrow-to-headers)
11378           (while (and methods (not split-name))
11379             (goto-char (point-min))
11380             (setq method (pop methods))
11381             (setq match (car method))
11382             (when (cond
11383                    ((stringp match)
11384                     ;; Regular expression.
11385                     (ignore-errors
11386                       (re-search-forward match nil t)))
11387                    ((functionp match)
11388                     ;; Function.
11389                     (save-restriction
11390                       (widen)
11391                       (setq result (funcall match gnus-newsgroup-name))))
11392                    ((consp match)
11393                     ;; Form.
11394                     (save-restriction
11395                       (widen)
11396                       (setq result (eval match)))))
11397               (setq split-name (cdr method))
11398               (cond ((stringp result)
11399                      (push (expand-file-name
11400                             result gnus-article-save-directory)
11401                            split-name))
11402                     ((consp result)
11403                      (setq split-name (append result split-name)))))))))
11404     (nreverse split-name)))
11405
11406 (defun gnus-valid-move-group-p (group)
11407   (and (boundp group)
11408        (symbol-name group)
11409        (symbol-value group)
11410        (gnus-get-function (gnus-find-method-for-group
11411                            (symbol-name group)) 'request-accept-article t)))
11412
11413 (defun gnus-read-move-group-name (prompt default articles prefix)
11414   "Read a group name."
11415   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11416          (minibuffer-confirm-incomplete nil) ; XEmacs
11417          (prom
11418           (format "%s %s to"
11419                   prompt
11420                   (if (> (length articles) 1)
11421                       (format "these %d articles" (length articles))
11422                     "this article")))
11423          (to-newsgroup
11424           (cond
11425            ((null split-name)
11426             (gnus-completing-read-with-default
11427              default prom
11428              gnus-active-hashtb
11429              'gnus-valid-move-group-p
11430              nil prefix
11431              'gnus-group-history))
11432            ((= 1 (length split-name))
11433             (gnus-completing-read-with-default
11434              (car split-name) prom
11435              gnus-active-hashtb
11436              'gnus-valid-move-group-p
11437              nil nil
11438              'gnus-group-history))
11439            (t
11440             (gnus-completing-read-with-default
11441              nil prom
11442              (mapcar 'list (nreverse split-name))
11443              nil nil nil
11444              'gnus-group-history))))
11445          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11446     (when to-newsgroup
11447       (if (or (string= to-newsgroup "")
11448               (string= to-newsgroup prefix))
11449           (setq to-newsgroup default))
11450       (unless to-newsgroup
11451         (error "No group name entered"))
11452       (or (gnus-active to-newsgroup)
11453           (gnus-activate-group to-newsgroup nil nil to-method)
11454           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11455                                      to-newsgroup))
11456               (or (and (gnus-request-create-group to-newsgroup to-method)
11457                        (gnus-activate-group
11458                         to-newsgroup nil nil to-method)
11459                        (gnus-subscribe-group to-newsgroup))
11460                   (error "Couldn't create group %s" to-newsgroup)))
11461           (error "No such group: %s" to-newsgroup)))
11462     to-newsgroup))
11463
11464 (defvar gnus-summary-save-parts-counter)
11465
11466 (defun gnus-summary-save-parts (type dir n &optional reverse)
11467   "Save parts matching TYPE to DIR.
11468 If REVERSE, save parts that do not match TYPE."
11469   (interactive
11470    (list (read-string "Save parts of type: "
11471                       (or (car gnus-summary-save-parts-type-history)
11472                           gnus-summary-save-parts-default-mime)
11473                       'gnus-summary-save-parts-type-history)
11474          (setq gnus-summary-save-parts-last-directory
11475                (read-file-name "Save to directory: "
11476                                gnus-summary-save-parts-last-directory
11477                                nil t))
11478          current-prefix-arg))
11479   (gnus-summary-iterate n
11480     (let ((gnus-display-mime-function nil)
11481           (gnus-inhibit-treatment t))
11482       (gnus-summary-select-article))
11483     (save-excursion
11484       (set-buffer gnus-article-buffer)
11485       (let ((handles (or gnus-article-mime-handles
11486                          (mm-dissect-buffer nil gnus-article-loose-mime)
11487                          (and gnus-article-emulate-mime
11488                               (mm-uu-dissect))))
11489             (gnus-summary-save-parts-counter 1))
11490         (when handles
11491           (gnus-summary-save-parts-1 type dir handles reverse)
11492           (unless gnus-article-mime-handles ;; Don't destroy this case.
11493             (mm-destroy-parts handles)))))))
11494
11495 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11496   (if (stringp (car handle))
11497       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11498               (cdr handle))
11499     (when (if reverse
11500               (not (string-match type (mm-handle-media-type handle)))
11501             (string-match type (mm-handle-media-type handle)))
11502       (let ((file (expand-file-name
11503                    (gnus-map-function
11504                     mm-file-name-rewrite-functions
11505                     (file-name-nondirectory
11506                      (or
11507                       (mail-content-type-get
11508                        (mm-handle-disposition handle) 'filename)
11509                       (mail-content-type-get
11510                        (mm-handle-type handle) 'name)
11511                       (format "%s.%d.%d" gnus-newsgroup-name
11512                               (cdr gnus-article-current)
11513                               gnus-summary-save-parts-counter))))
11514                    dir)))
11515         (incf gnus-summary-save-parts-counter)
11516         (unless (file-exists-p file)
11517           (mm-save-part-to-file handle file))))))
11518
11519 ;; Summary extract commands
11520
11521 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11522   (let ((buffer-read-only nil)
11523         (article (gnus-summary-article-number))
11524         after-article b e)
11525     (unless (gnus-summary-goto-subject article)
11526       (error "No such article: %d" article))
11527     (gnus-summary-position-point)
11528     ;; If all commands are to be bunched up on one line, we collect
11529     ;; them here.
11530     (unless gnus-view-pseudos-separately
11531       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11532             files action)
11533         (while ps
11534           (setq action (cdr (assq 'action (car ps))))
11535           (setq files (list (cdr (assq 'name (car ps)))))
11536           (while (and ps (cdr ps)
11537                       (string= (or action "1")
11538                                (or (cdr (assq 'action (cadr ps))) "2")))
11539             (push (cdr (assq 'name (cadr ps))) files)
11540             (setcdr ps (cddr ps)))
11541           (when files
11542             (when (not (string-match "%s" action))
11543               (push " " files))
11544             (push " " files)
11545             (when (assq 'execute (car ps))
11546               (setcdr (assq 'execute (car ps))
11547                       (funcall (if (string-match "%s" action)
11548                                    'format 'concat)
11549                                action
11550                                (mapconcat
11551                                 (lambda (f)
11552                                   (if (equal f " ")
11553                                       f
11554                                     (shell-quote-argument f)))
11555                                 files " ")))))
11556           (setq ps (cdr ps)))))
11557     (if (and gnus-view-pseudos (not not-view))
11558         (while pslist
11559           (when (assq 'execute (car pslist))
11560             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11561                                   (eq gnus-view-pseudos 'not-confirm)))
11562           (setq pslist (cdr pslist)))
11563       (save-excursion
11564         (while pslist
11565           (setq after-article (or (cdr (assq 'article (car pslist)))
11566                                   (gnus-summary-article-number)))
11567           (gnus-summary-goto-subject after-article)
11568           (forward-line 1)
11569           (setq b (point))
11570           (insert "    " (file-name-nondirectory
11571                           (cdr (assq 'name (car pslist))))
11572                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11573           (setq e (point))
11574           (forward-line -1)             ; back to `b'
11575           (gnus-add-text-properties
11576            b (1- e) (list 'gnus-number gnus-reffed-article-number
11577                           gnus-mouse-face-prop gnus-mouse-face))
11578           (gnus-data-enter
11579            after-article gnus-reffed-article-number
11580            gnus-unread-mark b (car pslist) 0 (- e b))
11581           (setq gnus-newsgroup-unreads
11582                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11583                                          gnus-reffed-article-number))
11584           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11585           (setq pslist (cdr pslist)))))))
11586
11587 (defun gnus-pseudos< (p1 p2)
11588   (let ((c1 (cdr (assq 'action p1)))
11589         (c2 (cdr (assq 'action p2))))
11590     (and c1 c2 (string< c1 c2))))
11591
11592 (defun gnus-request-pseudo-article (props)
11593   (cond ((assq 'execute props)
11594          (gnus-execute-command (cdr (assq 'execute props)))))
11595   (let ((gnus-current-article (gnus-summary-article-number)))
11596     (gnus-run-hooks 'gnus-mark-article-hook)))
11597
11598 (defun gnus-execute-command (command &optional automatic)
11599   (save-excursion
11600     (gnus-article-setup-buffer)
11601     (set-buffer gnus-article-buffer)
11602     (setq buffer-read-only nil)
11603     (let ((command (if automatic command
11604                      (read-string "Command: " (cons command 0)))))
11605       (erase-buffer)
11606       (insert "$ " command "\n\n")
11607       (if gnus-view-pseudo-asynchronously
11608           (start-process "gnus-execute" (current-buffer) shell-file-name
11609                          shell-command-switch command)
11610         (call-process shell-file-name nil t nil
11611                       shell-command-switch command)))))
11612
11613 ;; Summary kill commands.
11614
11615 (defun gnus-summary-edit-global-kill (article)
11616   "Edit the \"global\" kill file."
11617   (interactive (list (gnus-summary-article-number)))
11618   (gnus-group-edit-global-kill article))
11619
11620 (defun gnus-summary-edit-local-kill ()
11621   "Edit a local kill file applied to the current newsgroup."
11622   (interactive)
11623   (setq gnus-current-headers (gnus-summary-article-header))
11624   (gnus-group-edit-local-kill
11625    (gnus-summary-article-number) gnus-newsgroup-name))
11626
11627 ;;; Header reading.
11628
11629 (defun gnus-read-header (id &optional header)
11630   "Read the headers of article ID and enter them into the Gnus system."
11631   (let ((group gnus-newsgroup-name)
11632         (gnus-override-method
11633          (or
11634           gnus-override-method
11635           (and (gnus-news-group-p gnus-newsgroup-name)
11636                (car (gnus-refer-article-methods)))))
11637         where)
11638     ;; First we check to see whether the header in question is already
11639     ;; fetched.
11640     (if (stringp id)
11641         ;; This is a Message-ID.
11642         (setq header (or header (gnus-id-to-header id)))
11643       ;; This is an article number.
11644       (setq header (or header (gnus-summary-article-header id))))
11645     (if (and header
11646              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11647         ;; We have found the header.
11648         header
11649       ;; We have to really fetch the header to this article.
11650       (save-excursion
11651         (set-buffer nntp-server-buffer)
11652         (when (setq where (gnus-request-head id group))
11653           (nnheader-fold-continuation-lines)
11654           (goto-char (point-max))
11655           (insert ".\n")
11656           (goto-char (point-min))
11657           (insert "211 ")
11658           (princ (cond
11659                   ((numberp id) id)
11660                   ((cdr where) (cdr where))
11661                   (header (mail-header-number header))
11662                   (t gnus-reffed-article-number))
11663                  (current-buffer))
11664           (insert " Article retrieved.\n"))
11665         (if (or (not where)
11666                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11667             ()                          ; Malformed head.
11668           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11669             (when (and (stringp id)
11670                        (or
11671                         (not (string= (gnus-group-real-name group)
11672                                       (car where)))
11673                         (not (gnus-server-equal gnus-override-method
11674                                                 (gnus-group-method group)))))
11675               ;; If we fetched by Message-ID and the article came from
11676               ;; a different group (or server), we fudge some bogus
11677               ;; article numbers for this article.
11678               (mail-header-set-number header gnus-reffed-article-number))
11679             (save-excursion
11680               (set-buffer gnus-summary-buffer)
11681               (decf gnus-reffed-article-number)
11682               (gnus-remove-header (mail-header-number header))
11683               (push header gnus-newsgroup-headers)
11684               (setq gnus-current-headers header)
11685               (push (mail-header-number header) gnus-newsgroup-limit)))
11686           header)))))
11687
11688 (defun gnus-remove-header (number)
11689   "Remove header NUMBER from `gnus-newsgroup-headers'."
11690   (if (and gnus-newsgroup-headers
11691            (= number (mail-header-number (car gnus-newsgroup-headers))))
11692       (pop gnus-newsgroup-headers)
11693     (let ((headers gnus-newsgroup-headers))
11694       (while (and (cdr headers)
11695                   (not (= number (mail-header-number (cadr headers)))))
11696         (pop headers))
11697       (when (cdr headers)
11698         (setcdr headers (cddr headers))))))
11699
11700 ;;;
11701 ;;; summary highlights
11702 ;;;
11703
11704 (defun gnus-highlight-selected-summary ()
11705   "Highlight selected article in summary buffer."
11706   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11707   (when gnus-summary-selected-face
11708     (save-excursion
11709       (let* ((beg (point-at-bol))
11710              (end (point-at-eol))
11711              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11712              (from (if (get-text-property beg gnus-mouse-face-prop)
11713                        beg
11714                      (or (next-single-property-change
11715                           beg gnus-mouse-face-prop nil end)
11716                          beg)))
11717              (to
11718               (if (= from end)
11719                   (- from 2)
11720                 (or (next-single-property-change
11721                      from gnus-mouse-face-prop nil end)
11722                     end))))
11723         ;; If no mouse-face prop on line we will have to = from = end,
11724         ;; so we highlight the entire line instead.
11725         (when (= (+ to 2) from)
11726           (setq from beg)
11727           (setq to end))
11728         (if gnus-newsgroup-selected-overlay
11729             ;; Move old overlay.
11730             (gnus-move-overlay
11731              gnus-newsgroup-selected-overlay from to (current-buffer))
11732           ;; Create new overlay.
11733           (gnus-overlay-put
11734            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11735            'face gnus-summary-selected-face))))))
11736
11737 (defvar gnus-summary-highlight-line-cached nil)
11738 (defvar gnus-summary-highlight-line-trigger nil)
11739
11740 (defun gnus-summary-highlight-line-0 ()
11741   (if (and (eq gnus-summary-highlight-line-trigger
11742                gnus-summary-highlight)
11743            gnus-summary-highlight-line-cached)
11744       gnus-summary-highlight-line-cached
11745     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11746           gnus-summary-highlight-line-cached
11747           (let* ((cond (list 'cond))
11748                  (c cond)
11749                  (list gnus-summary-highlight))
11750             (while list
11751               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11752                               nil))
11753               (setq c (cdr c)
11754                     list (cdr list)))
11755             (gnus-byte-compile (list 'lambda nil cond))))))
11756
11757 (defun gnus-summary-highlight-line ()
11758   "Highlight current line according to `gnus-summary-highlight'."
11759   (let* ((beg (point-at-bol))
11760          (article (or (gnus-summary-article-number) gnus-current-article))
11761          (score (or (cdr (assq article
11762                                gnus-newsgroup-scored))
11763                     gnus-summary-default-score 0))
11764          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11765          (inhibit-read-only t)
11766          (default gnus-summary-default-score)
11767          (default-high gnus-summary-default-high-score)
11768          (default-low gnus-summary-default-low-score)
11769          (uncached (and gnus-summary-use-undownloaded-faces
11770                         (memq article gnus-newsgroup-undownloaded)
11771                         (not (memq article gnus-newsgroup-cached)))))
11772     (let ((face (funcall (gnus-summary-highlight-line-0))))
11773       (unless (eq face (get-text-property beg 'face))
11774         (gnus-put-text-property-excluding-characters-with-faces
11775          beg (point-at-eol) 'face
11776          (setq face (if (boundp face) (symbol-value face) face)))
11777         (when gnus-summary-highlight-line-function
11778           (funcall gnus-summary-highlight-line-function article face))))))
11779
11780 (defun gnus-update-read-articles (group unread &optional compute)
11781   "Update the list of read articles in GROUP.
11782 UNREAD is a sorted list."
11783   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11784         (info (gnus-get-info group))
11785         (prev 1)
11786         read)
11787     (if (or (not info) (not active))
11788         ;; There is no info on this group if it was, in fact,
11789         ;; killed.  Gnus stores no information on killed groups, so
11790         ;; there's nothing to be done.
11791         ;; One could store the information somewhere temporarily,
11792         ;; perhaps...  Hmmm...
11793         ()
11794       ;; Remove any negative articles numbers.
11795       (while (and unread (< (car unread) 0))
11796         (setq unread (cdr unread)))
11797       ;; Remove any expired article numbers
11798       (while (and unread (< (car unread) (car active)))
11799         (setq unread (cdr unread)))
11800       ;; Compute the ranges of read articles by looking at the list of
11801       ;; unread articles.
11802       (while unread
11803         (when (/= (car unread) prev)
11804           (push (if (= prev (1- (car unread))) prev
11805                   (cons prev (1- (car unread))))
11806                 read))
11807         (setq prev (1+ (car unread)))
11808         (setq unread (cdr unread)))
11809       (when (<= prev (cdr active))
11810         (push (cons prev (cdr active)) read))
11811       (setq read (if (> (length read) 1) (nreverse read) read))
11812       (if compute
11813           read
11814         (save-excursion
11815           (let (setmarkundo)
11816             ;; Propagate the read marks to the backend.
11817             (when (gnus-check-backend-function 'request-set-mark group)
11818               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11819                     (add (gnus-remove-from-range read (gnus-info-read info))))
11820                 (when (or add del)
11821                   (unless (gnus-check-group group)
11822                     (error "Can't open server for %s" group))
11823                   (gnus-request-set-mark
11824                    group (delq nil (list (if add (list add 'add '(read)))
11825                                          (if del (list del 'del '(read))))))
11826                   (setq setmarkundo
11827                         `(gnus-request-set-mark
11828                           ,group
11829                           ',(delq nil (list
11830                                        (if del (list del 'add '(read)))
11831                                        (if add (list add 'del '(read))))))))))
11832             (set-buffer gnus-group-buffer)
11833             (gnus-undo-register
11834               `(progn
11835                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11836                  (gnus-info-set-read ',info ',(gnus-info-read info))
11837                  (gnus-get-unread-articles-in-group ',info
11838                                                     (gnus-active ,group))
11839                  (gnus-group-update-group ,group t)
11840                  ,setmarkundo))))
11841         ;; Enter this list into the group info.
11842         (gnus-info-set-read info read)
11843         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11844         (gnus-get-unread-articles-in-group info (gnus-active group))
11845         t))))
11846
11847 (defun gnus-offer-save-summaries ()
11848   "Offer to save all active summary buffers."
11849   (let (buffers)
11850     ;; Go through all buffers and find all summaries.
11851     (dolist (buffer (buffer-list))
11852       (when (and (setq buffer (buffer-name buffer))
11853                  (string-match "Summary" buffer)
11854                  (with-current-buffer buffer
11855                    ;; We check that this is, indeed, a summary buffer.
11856                    (and (eq major-mode 'gnus-summary-mode)
11857                         ;; Also make sure this isn't bogus.
11858                         gnus-newsgroup-prepared
11859                         ;; Also make sure that this isn't a
11860                         ;; dead summary buffer.
11861                         (not gnus-dead-summary-mode))))
11862         (push buffer buffers)))
11863     ;; Go through all these summary buffers and offer to save them.
11864     (when buffers
11865       (save-excursion
11866         (map-y-or-n-p
11867          "Update summary buffer %s? "
11868          (lambda (buf)
11869            (switch-to-buffer buf)
11870            (gnus-summary-exit))
11871          buffers)))))
11872
11873 (defun gnus-summary-setup-default-charset ()
11874   "Setup newsgroup default charset."
11875   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11876       (setq gnus-newsgroup-charset nil)
11877     (let* ((ignored-charsets
11878             (or gnus-newsgroup-ephemeral-ignored-charsets
11879                 (append
11880                  (and gnus-newsgroup-name
11881                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11882                  gnus-newsgroup-ignored-charsets))))
11883       (setq gnus-newsgroup-charset
11884             (or gnus-newsgroup-ephemeral-charset
11885                 (and gnus-newsgroup-name
11886                      (gnus-parameter-charset gnus-newsgroup-name))
11887                 gnus-default-charset))
11888       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11889            ignored-charsets))))
11890
11891 ;;;
11892 ;;; Mime Commands
11893 ;;;
11894
11895 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11896   "Display the current article buffer fully MIME-buttonized.
11897 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11898 treated as multipart/mixed."
11899   (interactive "P")
11900   (require 'gnus-art)
11901   (let ((gnus-unbuttonized-mime-types nil)
11902         (gnus-mime-display-multipart-as-mixed show-all-parts))
11903     (gnus-summary-show-article)))
11904
11905 (defun gnus-summary-repair-multipart (article)
11906   "Add a Content-Type header to a multipart article without one."
11907   (interactive (list (gnus-summary-article-number)))
11908   (gnus-with-article article
11909     (message-narrow-to-head)
11910     (message-remove-header "Mime-Version")
11911     (goto-char (point-max))
11912     (insert "Mime-Version: 1.0\n")
11913     (widen)
11914     (when (search-forward "\n--" nil t)
11915       (let ((separator (buffer-substring (point) (point-at-eol))))
11916         (message-narrow-to-head)
11917         (message-remove-header "Content-Type")
11918         (goto-char (point-max))
11919         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11920                         separator))
11921         (widen))))
11922   (let (gnus-mark-article-hook)
11923     (gnus-summary-select-article t t nil article)))
11924
11925 (defun gnus-summary-toggle-display-buttonized ()
11926   "Toggle the buttonizing of the article buffer."
11927   (interactive)
11928   (require 'gnus-art)
11929   (if (setq gnus-inhibit-mime-unbuttonizing
11930             (not gnus-inhibit-mime-unbuttonizing))
11931       (let ((gnus-unbuttonized-mime-types nil))
11932         (gnus-summary-show-article))
11933     (gnus-summary-show-article)))
11934
11935 ;;;
11936 ;;; Generic summary marking commands
11937 ;;;
11938
11939 (defvar gnus-summary-marking-alist
11940   '((read gnus-del-mark "d")
11941     (unread gnus-unread-mark "u")
11942     (ticked gnus-ticked-mark "!")
11943     (dormant gnus-dormant-mark "?")
11944     (expirable gnus-expirable-mark "e"))
11945   "An alist of names/marks/keystrokes.")
11946
11947 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11948 (defvar gnus-summary-mark-map)
11949
11950 (defun gnus-summary-make-all-marking-commands ()
11951   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11952   (dolist (elem gnus-summary-marking-alist)
11953     (apply 'gnus-summary-make-marking-command elem)))
11954
11955 (defun gnus-summary-make-marking-command (name mark keystroke)
11956   (let ((map (make-sparse-keymap)))
11957     (define-key gnus-summary-generic-mark-map keystroke map)
11958     (dolist (lway `((next "next" next nil "n")
11959                     (next-unread "next unread" next t "N")
11960                     (prev "previous" prev nil "p")
11961                     (prev-unread "previous unread" prev t "P")
11962                     (nomove "" nil nil ,keystroke)))
11963       (let ((func (gnus-summary-make-marking-command-1
11964                    mark (car lway) lway name)))
11965         (setq func (eval func))
11966         (define-key map (nth 4 lway) func)))))
11967
11968 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11969   `(defun ,(intern
11970             (format "gnus-summary-put-mark-as-%s%s"
11971                     name (if (eq way 'nomove)
11972                              ""
11973                            (concat "-" (symbol-name way)))))
11974      (n)
11975      ,(format
11976        "Mark the current article as %s%s.
11977 If N, the prefix, then repeat N times.
11978 If N is negative, move in reverse order.
11979 The difference between N and the actual number of articles marked is
11980 returned."
11981        name (cadr lway))
11982      (interactive "p")
11983      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11984
11985 (defun gnus-summary-generic-mark (n mark move unread)
11986   "Mark N articles with MARK."
11987   (unless (eq major-mode 'gnus-summary-mode)
11988     (error "This command can only be used in the summary buffer"))
11989   (gnus-summary-show-thread)
11990   (let ((nummove
11991          (cond
11992           ((eq move 'next) 1)
11993           ((eq move 'prev) -1)
11994           (t 0))))
11995     (if (zerop nummove)
11996         (setq n 1)
11997       (when (< n 0)
11998         (setq n (abs n)
11999               nummove (* -1 nummove))))
12000     (while (and (> n 0)
12001                 (gnus-summary-mark-article nil mark)
12002                 (zerop (gnus-summary-next-subject nummove unread t)))
12003       (setq n (1- n)))
12004     (when (/= 0 n)
12005       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12006     (gnus-summary-recenter)
12007     (gnus-summary-position-point)
12008     (gnus-set-mode-line 'summary)
12009     n))
12010
12011 (defun gnus-summary-insert-articles (articles)
12012   (when (setq articles
12013               (gnus-sorted-difference articles
12014                                       (mapcar (lambda (h)
12015                                                 (mail-header-number h))
12016                                               gnus-newsgroup-headers)))
12017     (setq gnus-newsgroup-headers
12018           (gnus-merge 'list
12019                       gnus-newsgroup-headers
12020                       (gnus-fetch-headers articles)
12021                       'gnus-article-sort-by-number))
12022     ;; Suppress duplicates?
12023     (when gnus-suppress-duplicates
12024       (gnus-dup-suppress-articles))
12025
12026     ;; We might want to build some more threads first.
12027     (when (and gnus-fetch-old-headers
12028                (eq gnus-headers-retrieved-by 'nov))
12029       (if (eq gnus-fetch-old-headers 'invisible)
12030           (gnus-build-all-threads)
12031         (gnus-build-old-threads)))
12032     ;; Let the Gnus agent mark articles as read.
12033     (when gnus-agent
12034       (gnus-agent-get-undownloaded-list))
12035     ;; Remove list identifiers from subject
12036     (when gnus-list-identifiers
12037       (gnus-summary-remove-list-identifiers))
12038     ;; First and last article in this newsgroup.
12039     (when gnus-newsgroup-headers
12040       (setq gnus-newsgroup-begin
12041             (mail-header-number (car gnus-newsgroup-headers))
12042             gnus-newsgroup-end
12043             (mail-header-number
12044              (gnus-last-element gnus-newsgroup-headers))))
12045     (when gnus-use-scoring
12046       (gnus-possibly-score-headers))))
12047
12048 (defun gnus-summary-insert-old-articles (&optional all)
12049   "Insert all old articles in this group.
12050 If ALL is non-nil, already read articles become readable.
12051 If ALL is a number, fetch this number of articles."
12052   (interactive "P")
12053   (prog1
12054       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12055             older len)
12056         (setq older
12057               ;; Some nntp servers lie about their active range.  When
12058               ;; this happens, the active range can be in the millions.
12059               ;; Use a compressed range to avoid creating a huge list.
12060               (gnus-range-difference (list gnus-newsgroup-active) old))
12061         (setq len (gnus-range-length older))
12062         (cond
12063          ((null older) nil)
12064          ((numberp all)
12065           (if (< all len)
12066               (let ((older-range (nreverse older)))
12067                 (setq older nil)
12068
12069                 (while (> all 0)
12070                   (let* ((r (pop older-range))
12071                          (min (if (numberp r) r (car r)))
12072                          (max (if (numberp r) r (cdr r))))
12073                     (while (and (<= min max)
12074                                 (> all 0))
12075                       (push max older)
12076                       (setq all (1- all)
12077                             max (1- max))))))
12078             (setq older (gnus-uncompress-range older))))
12079          (all
12080           (setq older (gnus-uncompress-range older)))
12081          (t
12082           (when (and (numberp gnus-large-newsgroup)
12083                    (> len gnus-large-newsgroup))
12084               (let* ((cursor-in-echo-area nil)
12085                      (initial (gnus-parameter-large-newsgroup-initial
12086                                gnus-newsgroup-name))
12087                      (input
12088                       (read-string
12089                        (format
12090                         "How many articles from %s (%s %d): "
12091                         (gnus-limit-string
12092                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12093                         (if initial "max" "default")
12094                         len)
12095                        (if initial
12096                            (cons (number-to-string initial)
12097                                  0)))))
12098                 (unless (string-match "^[ \t]*$" input)
12099                   (setq all (string-to-number input))
12100                   (if (< all len)
12101                       (let ((older-range (nreverse older)))
12102                         (setq older nil)
12103
12104                         (while (> all 0)
12105                           (let* ((r (pop older-range))
12106                                  (min (if (numberp r) r (car r)))
12107                                  (max (if (numberp r) r (cdr r))))
12108                             (while (and (<= min max)
12109                                         (> all 0))
12110                               (push max older)
12111                               (setq all (1- all)
12112                                     max (1- max))))))))))
12113           (setq older (gnus-uncompress-range older))))
12114         (if (not older)
12115             (message "No old news.")
12116           (gnus-summary-insert-articles older)
12117           (gnus-summary-limit (gnus-sorted-nunion old older))))
12118     (gnus-summary-position-point)))
12119
12120 (defun gnus-summary-insert-new-articles ()
12121   "Insert all new articles in this group."
12122   (interactive)
12123   (prog1
12124       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12125             (old-active gnus-newsgroup-active)
12126             (nnmail-fetched-sources (list t))
12127             i new)
12128         (setq gnus-newsgroup-active
12129               (gnus-activate-group gnus-newsgroup-name 'scan))
12130         (setq i (cdr gnus-newsgroup-active))
12131         (while (> i (cdr old-active))
12132           (push i new)
12133           (decf i))
12134         (if (not new)
12135             (message "No gnus is bad news")
12136           (gnus-summary-insert-articles new)
12137           (setq gnus-newsgroup-unreads
12138                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12139           (gnus-summary-limit (gnus-sorted-nunion old new))))
12140     (gnus-summary-position-point)))
12141
12142 (gnus-summary-make-all-marking-commands)
12143
12144 (gnus-ems-redefine)
12145
12146 (provide 'gnus-sum)
12147
12148 (run-hooks 'gnus-sum-load-hook)
12149
12150 ;; Local Variables:
12151 ;; coding: iso-8859-1
12152 ;; End:
12153
12154 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12155 ;;; gnus-sum.el ends here